2020-05-21 15:44:52 +00:00
|
|
|
const {Readable} = require("stream")
|
2020-07-08 02:56:06 +00:00
|
|
|
const constants = require("../../lib/constants")
|
|
|
|
const push_webhook_token = constants.secrets.push_webhook_token
|
2020-05-21 15:44:52 +00:00
|
|
|
|
|
|
|
const streams = new Set()
|
|
|
|
|
|
|
|
setInterval((new (function() {
|
|
|
|
const payload = `:keepalive ${Date.now()}\n\n`
|
|
|
|
for (const stream of streams.values()) {
|
|
|
|
stream.push(payload)
|
|
|
|
}
|
2020-06-20 16:09:36 +00:00
|
|
|
})).constructor, 50000).unref()
|
2020-05-21 15:44:52 +00:00
|
|
|
|
|
|
|
module.exports = [
|
2020-07-08 02:56:06 +00:00
|
|
|
{
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-05-21 15:44:52 +00:00
|
|
|
{
|
|
|
|
route: "/api/update_stream", methods: ["GET"], code: async () => {
|
|
|
|
const stream = new Readable({
|
|
|
|
read: function() {},
|
|
|
|
destroy: function() {
|
|
|
|
streams.delete(stream)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
streams.add(stream)
|
2020-05-21 16:24:21 +00:00
|
|
|
stream.push(":connected\n\n")
|
2020-05-21 15:44:52 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: "text/event-stream",
|
2020-05-21 16:24:21 +00:00
|
|
|
headers: {
|
|
|
|
"X-Accel-Buffering": "no"
|
|
|
|
},
|
2020-05-21 15:44:52 +00:00
|
|
|
stream
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|