mirror of
https://github.com/a-sync/game-server-watcher.git
synced 2026-07-31 17:38:55 -04:00
91 lines
2.9 KiB
JavaScript
91 lines
2.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.defaultAdapter = exports.adapters = void 0;
|
|
const SECRET_HEADER = "X-Telegram-Bot-Api-Secret-Token";
|
|
/** Node.js native 'http' and 'https' modules */
|
|
const http = (req, res) => {
|
|
const secretHeaderFromRequest = req.headers[SECRET_HEADER.toLowerCase()];
|
|
return {
|
|
update: new Promise((resolve, reject) => {
|
|
const chunks = [];
|
|
req.on("data", (chunk) => chunks.push(chunk))
|
|
.once("end", () => {
|
|
const raw = Buffer.concat(chunks).toString("utf-8");
|
|
resolve(JSON.parse(raw));
|
|
})
|
|
.once("error", reject);
|
|
}),
|
|
header: Array.isArray(secretHeaderFromRequest)
|
|
? secretHeaderFromRequest[0]
|
|
: secretHeaderFromRequest,
|
|
end: () => res.end(),
|
|
respond: (json) => res
|
|
.writeHead(200, { "Content-Type": "application/json" })
|
|
.end(json),
|
|
unauthorized: () => res.writeHead(401).end("secret token is wrong"),
|
|
};
|
|
};
|
|
/** express web framework */
|
|
const express = (req, res) => ({
|
|
update: Promise.resolve(req.body),
|
|
header: req.header(SECRET_HEADER),
|
|
end: () => res.end(),
|
|
respond: (json) => {
|
|
res.set("Content-Type", "application/json");
|
|
res.send(json);
|
|
},
|
|
unauthorized: () => {
|
|
res.send(401, "secret token is wrong");
|
|
},
|
|
});
|
|
/** koa web framework */
|
|
const koa = (ctx) => ({
|
|
update: Promise.resolve(ctx.request.body),
|
|
header: ctx.get(SECRET_HEADER),
|
|
end: () => {
|
|
ctx.body = "";
|
|
},
|
|
respond: (json) => {
|
|
ctx.set("Content-Type", "application/json");
|
|
ctx.response.body = json;
|
|
},
|
|
unauthorized: () => {
|
|
ctx.status = 401;
|
|
},
|
|
});
|
|
/** fastify web framework */
|
|
const fastify = (req, reply) => ({
|
|
update: Promise.resolve(req.body),
|
|
header: req.headers[SECRET_HEADER.toLowerCase()],
|
|
end: () => reply.status(200).send(),
|
|
respond: (json) => reply.send(json),
|
|
unauthorized: () => reply.code(401).send("secret token is wrong"),
|
|
});
|
|
/** worktop CloudFlare workers framework */
|
|
const worktop = (req, res) => ({
|
|
update: Promise.resolve(req.body.json()),
|
|
header: req.headers.get(SECRET_HEADER),
|
|
end: () => res.end(),
|
|
respond: (json) => res.send(200, json),
|
|
unauthorized: () => res.send(401, "secret token is wrong"),
|
|
});
|
|
/** AWS lambda serverless functions */
|
|
const awsLambda = (event, _context, callback) => ({
|
|
update: JSON.parse(event.body),
|
|
header: event.headers[SECRET_HEADER],
|
|
end: () => callback(null, { statusCode: 200 }),
|
|
respond: (json) => callback(null, { statusCode: 200, body: json }),
|
|
unauthorized: () => callback(null, { statusCode: 401 }),
|
|
});
|
|
// please open a PR if you want to add another
|
|
exports.adapters = {
|
|
http,
|
|
https: http,
|
|
express,
|
|
koa,
|
|
fastify,
|
|
worktop,
|
|
"aws-lambda": awsLambda,
|
|
};
|
|
exports.defaultAdapter = "express";
|