From 43c083da9a4393c0aad1ee869406cf4ac3b42805 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 8 Jul 2020 14:56:06 +1200 Subject: [PATCH] Update stream push hook --- src/site/api/update_stream.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/site/api/update_stream.js b/src/site/api/update_stream.js index da419e0..87b6204 100644 --- a/src/site/api/update_stream.js +++ b/src/site/api/update_stream.js @@ -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({