@kwirthmagnify/kwirth-provider-business 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/back.js +110 -0
- package/package.json +7 -0
package/back.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
BusinessProvider: () => BusinessProvider,
|
|
34
|
+
default: () => index_default
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
var import_express = __toESM(require("express"), 1);
|
|
38
|
+
var BusinessProvider = class {
|
|
39
|
+
constructor(_clusterInfo, _kwirthData) {
|
|
40
|
+
this.id = "business";
|
|
41
|
+
this.providesRouter = true;
|
|
42
|
+
this.router = import_express.default.Router();
|
|
43
|
+
this.routerAlias = "business";
|
|
44
|
+
this.requiresApiKeyApi = false;
|
|
45
|
+
this.apiKeyApi = void 0;
|
|
46
|
+
// almacén acumulado: espacio -> tipo -> eventos
|
|
47
|
+
this.data = /* @__PURE__ */ new Map();
|
|
48
|
+
this.subscribers = /* @__PURE__ */ new Map();
|
|
49
|
+
/*
|
|
50
|
+
Procesa un evento entrante: lo acumula en el store y hace fan-out a los subscribers
|
|
51
|
+
interesados en ese espacio/tipo. Devuelve false si el body es inválido (falta space o type).
|
|
52
|
+
Formato esperado: { space: string, type: string, data: unknown }
|
|
53
|
+
*/
|
|
54
|
+
this.ingest = (body) => {
|
|
55
|
+
if (!body || !body.space || !body.type) return false;
|
|
56
|
+
const space = this.data.get(body.space);
|
|
57
|
+
if (space) {
|
|
58
|
+
const type = space.get(body.type);
|
|
59
|
+
if (type)
|
|
60
|
+
type.push(body);
|
|
61
|
+
else
|
|
62
|
+
space.set(body.type, [body]);
|
|
63
|
+
} else {
|
|
64
|
+
const newSpace = /* @__PURE__ */ new Map();
|
|
65
|
+
newSpace.set(body.type, [body]);
|
|
66
|
+
this.data.set(body.space, newSpace);
|
|
67
|
+
}
|
|
68
|
+
for (const [subscriber, config] of this.subscribers) {
|
|
69
|
+
const subSpace = config.spaces.find((s) => s.name === body.space);
|
|
70
|
+
if (subSpace && subSpace.types.includes(body.type)) {
|
|
71
|
+
subscriber.processProviderEvent(this.id, {
|
|
72
|
+
last: {
|
|
73
|
+
type: "event",
|
|
74
|
+
timestamp: Date.now().toString(),
|
|
75
|
+
event: body
|
|
76
|
+
},
|
|
77
|
+
all: this.data
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
};
|
|
83
|
+
this.addSubscriber = async (c, config) => {
|
|
84
|
+
this.subscribers.set(c, { spaces: config?.spaces ?? [] });
|
|
85
|
+
};
|
|
86
|
+
this.removeSubscriber = async (c) => {
|
|
87
|
+
this.subscribers.delete(c);
|
|
88
|
+
};
|
|
89
|
+
this.startProvider = async () => {
|
|
90
|
+
};
|
|
91
|
+
this.stopProvider = async () => {
|
|
92
|
+
this.subscribers.clear();
|
|
93
|
+
this.data.clear();
|
|
94
|
+
};
|
|
95
|
+
console.log(`[business] Instantiating provider ${this.id}`);
|
|
96
|
+
this.router.route("/").post(async (req, res) => {
|
|
97
|
+
try {
|
|
98
|
+
res.status(this.ingest(req.body) ? 200 : 400).json();
|
|
99
|
+
} catch (err) {
|
|
100
|
+
res.status(500).send();
|
|
101
|
+
console.error("[business] Error managing business event:", err);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
var index_default = BusinessProvider;
|
|
107
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
108
|
+
0 && (module.exports = {
|
|
109
|
+
BusinessProvider
|
|
110
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "business",
|
|
3
|
+
"name": "@kwirthmagnify/kwirth-provider-business",
|
|
4
|
+
"displayName": "Business Provider",
|
|
5
|
+
"version": "0.1.0",
|
|
6
|
+
"description": "Business provider plugin for Kwirth — ingests arbitrary business events over HTTP and dispatches them to subscribers grouped by spaces and types"
|
|
7
|
+
}
|