-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mjs
90 lines (74 loc) · 2.08 KB
/
main.mjs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { connect, JSONCodec } from 'nats';
import Application from './framework/application.mjs';
import ai from './test/ai.mjs';
// await new Promise((resolve) => setTimeout(resolve, 2000));
const NatsConnection = await connect({ servers: ['nats://localhost:4222'] });
const JetStreamManager = await NatsConnection.jetstreamManager();
const JetStreamClient = JetStreamManager.jetstream();
const intervals = [];
const timeouts = [];
const patch = (original, patch) => (...args) => patch(original(...args));
const sandbox = {
ai,
nats: NatsConnection,
js: JetStreamClient,
jsm: JetStreamManager,
json: JSONCodec(),
setInterval: patch(setInterval, (interval) => intervals.push(interval)),
setTimeout: patch(setTimeout, (timeout) => timeouts.push(timeout)),
};
const beforeHook = async (req, next) => {
// console.log('before request', req);
req.age *= 2;
// console.log('before', this);
// for (const interval of intervals) clearInterval(interval);
const reply = await next(req);
// console.log('before reply', reply);
return reply;
};
const afterHook = async (res) => {
// console.log('after request', res);
res.afterHook = true;
return res;
};
const before = [
['domain/completion', beforeHook]
];
const after = [
['domain/completion', afterHook]
];
const tokens = new Map([
['foo', { userId: 'foo' }],
['bar', { userId: 'bar' }],
['buz', { userId: 'buz' }],
]);
const input = {
http: {
port: 3000,
endpoints: ['api'],
auth: (token) => tokens.get(token),
},
ws: {
port: 8000,
endpoints: ['api'],
},
// nats: {
// streams: [],
// subjects: [],
// consumers: [],
// producers: [],
// }
};
const app = new Application({ sandbox, before, after, input });
app.ac.signal.onabort = () => {
for (const interval of intervals) clearInterval(interval);
for (const timeout of timeouts) clearTimeout(timeout);
};
const graceful = async () => {
app.logger.warn('Graceful shutdown');
await app.close();
await NatsConnection.close();
};
process.once('SIGINT', graceful);
process.once('SIGTERM', graceful);
await app.start();