forked from planetarium/Corvette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testWebhookReceiver.ts
72 lines (62 loc) · 1.73 KB
/
testWebhookReceiver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { ConsoleHandler } from "std/log/handlers.ts";
import { getLogger, setup as setupLog } from "std/log/mod.ts";
import { Application as OakApplication } from "oak";
import { parse, stringify as losslessJsonStringify } from "npm:lossless-json";
import {
defaultLogFormatter,
TestWebhookReceiverLoggerName,
} from "./logUtils.ts";
import { runAndCleanup } from "./runHelpers.ts";
function numberParser(value: string) {
const n = Number(value);
if (!Number.isInteger(n) || Number.isSafeInteger(n)) return n;
return BigInt(value);
}
export async function testWebhookReceiver() {
const logger = getLogger(TestWebhookReceiverLoggerName);
const abortController = new AbortController();
const app = new OakApplication();
app.use(async (ctx) => {
logger.info(
"Received Webhook:",
losslessJsonStringify(
parse(
await ctx.request.body({ type: "text" }).value,
undefined,
numberParser,
),
undefined,
2,
),
);
ctx.response.body = "";
});
const port = 8888;
logger.info(`Test webhook receiver listening on port ${port}.`);
const runningPromise = app.listen({
port,
signal: abortController.signal,
});
async function cleanup() {
logger.info(`Stopping test webhook receiver.`);
abortController.abort();
await runningPromise;
}
return await Promise.resolve({ runningPromise, cleanup });
}
if (import.meta.main) {
setupLog({
handlers: {
console: new ConsoleHandler("DEBUG", {
formatter: defaultLogFormatter,
}),
},
loggers: {
[TestWebhookReceiverLoggerName]: {
level: "DEBUG",
handlers: ["console"],
},
},
});
await runAndCleanup(testWebhookReceiver);
}