mirror of
https://git.sr.ht/~cadence/bibliogram
synced 2024-11-22 08:07:30 +00:00
Update stream push hook
This commit is contained in:
parent
642e543fd6
commit
43c083da9a
@ -1,4 +1,6 @@
|
||||
const {Readable} = require("stream")
|
||||
const constants = require("../../lib/constants")
|
||||
const push_webhook_token = constants.secrets.push_webhook_token
|
||||
|
||||
const streams = new Set()
|
||||
|
||||
@ -10,6 +12,37 @@ setInterval((new (function() {
|
||||
})).constructor, 50000).unref()
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
route: "/api/hooks/push/1.0", methods: ["POST"], code: async ({url}) => {
|
||||
if (push_webhook_token && url.searchParams.get("token") === push_webhook_token) {
|
||||
for (const stream of streams.values()) {
|
||||
stream.push(`event: push\ndata: push ${Date.now()}\n:[.] Update available!\n\n`)
|
||||
}
|
||||
return {
|
||||
statusCode: 200,
|
||||
contentType: "application/json",
|
||||
content: {
|
||||
status: "ok",
|
||||
version: "1.0",
|
||||
generatedAt: Date.now(),
|
||||
data: null
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
statusCode: 401,
|
||||
contentType: "application/json",
|
||||
content: {
|
||||
status: "fail",
|
||||
version: "1.0",
|
||||
generatedAt: Date.now(),
|
||||
fields: ["q:token"],
|
||||
message: "query parameter `token` is required for authentication"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
route: "/api/update_stream", methods: ["GET"], code: async () => {
|
||||
const stream = new Readable({
|
||||
|
Loading…
Reference in New Issue
Block a user