1
0
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:
Cadence Ember 2020-07-08 14:56:06 +12:00
parent 642e543fd6
commit 43c083da9a
No known key found for this signature in database
GPG Key ID: 128B99B1B74A6412

View File

@ -1,4 +1,6 @@
const {Readable} = require("stream") const {Readable} = require("stream")
const constants = require("../../lib/constants")
const push_webhook_token = constants.secrets.push_webhook_token
const streams = new Set() const streams = new Set()
@ -10,6 +12,37 @@ setInterval((new (function() {
})).constructor, 50000).unref() })).constructor, 50000).unref()
module.exports = [ 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 () => { route: "/api/update_stream", methods: ["GET"], code: async () => {
const stream = new Readable({ const stream = new Readable({