1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const function_1 = require("./function");
|
19 | const log_1 = require("./log");
|
20 | const payload_1 = require("./payload");
|
21 | const util_1 = require("./util");
|
22 | const HandlerRegistry = {
|
23 | events: {},
|
24 | commands: {},
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 | function registerCommand(name, loader) {
|
30 | HandlerRegistry.commands[name] = loader;
|
31 | }
|
32 | exports.registerCommand = registerCommand;
|
33 |
|
34 |
|
35 |
|
36 | function registerEvent(name, loader) {
|
37 | HandlerRegistry.events[name] = loader;
|
38 | }
|
39 | exports.registerEvent = registerEvent;
|
40 | exports.bundle = async (pubSubEvent, context) => {
|
41 | const attributes = Object.assign(Object.assign({}, (pubSubEvent.attributes || {})), { eventId: context.eventId });
|
42 | log_1.debug(`atm:attributes=${JSON.stringify(attributes)}`);
|
43 | const payload = JSON.parse(Buffer.from(pubSubEvent.data, "base64").toString());
|
44 | log_1.info(`Incoming pub/sub message: ${JSON.stringify(payload, util_1.replacer)}`);
|
45 | if (payload_1.isEventIncoming(payload)) {
|
46 | return function_1.processEvent(payload, context, async () => {
|
47 | const loader = HandlerRegistry.events[payload.extensions.operationName];
|
48 | if (loader) {
|
49 | return loader();
|
50 | }
|
51 | else {
|
52 | throw new Error(`Event handler with name '${payload.extensions.operationName}' not registered`);
|
53 | }
|
54 | });
|
55 | }
|
56 | else if (payload_1.isCommandIncoming(payload)) {
|
57 | return function_1.processCommand(payload, context, async () => {
|
58 | const loader = HandlerRegistry.commands[payload.command];
|
59 | if (loader) {
|
60 | return loader();
|
61 | }
|
62 | else {
|
63 | throw new Error(`Command handler with name '${payload.command}' not registered`);
|
64 | }
|
65 | });
|
66 | }
|
67 | };
|
68 |
|
\ | No newline at end of file |