@buildable/messages
Advanced tools
+52
-22
@@ -15,3 +15,3 @@ "use strict"; | ||
| const emitPath = "/emit"; | ||
| const SEPERATOR = "---"; | ||
| const SEPERATOR = process.env.BLD_LISTENER_SEPARATOR || "---"; | ||
| function createClient(secret) { | ||
@@ -22,8 +22,14 @@ if (!secret) { | ||
| let listeners = {}; | ||
| const getListenerId = ({ eventName, txKey }) => { | ||
| return eventName + SEPERATOR + txKey; | ||
| let fullEventNameCache = {}; | ||
| const getFullEventName = ({ eventName, platform, label }) => { | ||
| return fullEventNameCache[eventName + platform + label]; | ||
| }; | ||
| const runTransactions = async ({ eventName, txKey, since = Date.now(), sleepTime = 3000 }) => { | ||
| const getListenerId = ({ eventName, txKey, platform, label }) => { | ||
| const fullEventName = getFullEventName({ eventName, platform, label }); | ||
| return fullEventName + SEPERATOR + txKey; | ||
| }; | ||
| const runTransactions = async ({ eventName, txKey, since = Date.now(), sleepTime = 3000, platform, label }) => { | ||
| var _a, _b, _c, _d, _e, _f; | ||
| try { | ||
| const listener = listeners[getListenerId({ eventName, txKey })]; | ||
| const listener = listeners[getListenerId({ eventName, txKey, platform, label })]; | ||
| if (!listener) { | ||
@@ -47,2 +53,4 @@ return; //deregistered | ||
| txKey, | ||
| platform, | ||
| label, | ||
| since, | ||
@@ -64,6 +72,6 @@ txState: "does-not-exist", | ||
| hasMorePages = false; | ||
| console.error("Error fetching events for ", { eventName, txKey }, e === null || e === void 0 ? void 0 : e.response); | ||
| console.error("Error fetching events for ", { eventName, txKey }, JSON.stringify({ data: (_a = e === null || e === void 0 ? void 0 : e.response) === null || _a === void 0 ? void 0 : _a.data, status: (_b = e === null || e === void 0 ? void 0 : e.response) === null || _b === void 0 ? void 0 : _b.status }, null, 2)); | ||
| } | ||
| for (const event of events) { | ||
| const { headers, query, payload, key } = event; | ||
| const { headers, query, payload, key, eventName } = event; | ||
| try { | ||
@@ -83,3 +91,3 @@ await (0, axios_1.default)({ | ||
| catch (e) { | ||
| console.error(`Error initializing transaction for event: ${event.eventName}`, e === null || e === void 0 ? void 0 : e.response); | ||
| console.error(`Error initializing transaction for event: ${eventName}, with key: ${key} and txKey: ${txKey}`, JSON.stringify({ data: (_c = e === null || e === void 0 ? void 0 : e.response) === null || _c === void 0 ? void 0 : _c.data, status: (_d = e === null || e === void 0 ? void 0 : e.response) === null || _d === void 0 ? void 0 : _d.status }, null, 2)); | ||
| continue; | ||
@@ -110,3 +118,3 @@ } | ||
| txKey, | ||
| output: (0, utils_1.stringify)(error), | ||
| output: error, | ||
| state: "failed" | ||
@@ -126,3 +134,3 @@ } | ||
| txKey, | ||
| output: (0, utils_1.stringify)(output), | ||
| output: output || {}, | ||
| state: "finished" | ||
@@ -136,8 +144,8 @@ } | ||
| catch (e) { | ||
| console.error("Error occurred in query interval", e); | ||
| console.error("Error occurred in query interval", (e === null || e === void 0 ? void 0 : e.response) ? JSON.stringify({ data: (_e = e === null || e === void 0 ? void 0 : e.response) === null || _e === void 0 ? void 0 : _e.data, status: (_f = e === null || e === void 0 ? void 0 : e.response) === null || _f === void 0 ? void 0 : _f.status }, null, 2) : e); | ||
| } | ||
| await (0, utils_1.sleep)(sleepTime); | ||
| const listener = listeners[getListenerId({ eventName, txKey })]; | ||
| const listener = listeners[getListenerId({ eventName, txKey, platform, label })]; | ||
| if (listener && listener.interval) { | ||
| listener.interval.then(() => runTransactions({ eventName, txKey, since })); // doesn't affect stack size | ||
| listener.interval.then(() => runTransactions({ eventName, txKey, since, sleepTime, platform, label })); // doesn't affect stack size | ||
| } | ||
@@ -166,20 +174,42 @@ }; | ||
| }, | ||
| on: (eventName, handler, options = {}) => { | ||
| const txKey = options.txKey || `js-sdk.${eventName}`; | ||
| const id = getListenerId({ eventName, txKey }); | ||
| on: async (eventName, handler, options = {}) => { | ||
| let fullEventName = getFullEventName({ eventName, platform: options.platform, label: options.label }); | ||
| if (!fullEventName) { | ||
| const { data } = await (0, axios_1.default)({ | ||
| method: "post", | ||
| url: baseURL + "/get-full-event-name", | ||
| headers: { | ||
| "X-BUILDABLE-SECRET": secret | ||
| }, | ||
| data: { | ||
| eventName, | ||
| platform: options.platform, | ||
| label: options.label | ||
| } | ||
| }); | ||
| if (!data || !data.fullEventName) { | ||
| throw new Error("Could not grab required fullEventName from the server"); | ||
| } | ||
| const [, , ...splitEventName] = data.fullEventName.split("."); | ||
| fullEventName = splitEventName.join("."); | ||
| fullEventNameCache[eventName + options.platform + options.label] = fullEventName; | ||
| } | ||
| const txKey = options.txKey || `js-sdk.${fullEventName}`; | ||
| const id = getListenerId({ eventName, txKey, platform: options.platform, label: options.label }); | ||
| if (listeners[id]) { | ||
| throw new Error(`Listener already exists with eventName: ${eventName}, txKey: ${txKey}`); | ||
| throw new Error(`Listener already exists with for ${fullEventName} with txKey: ${txKey}`); | ||
| } | ||
| listeners[id] = { eventName, handler, txKey, options, id }; | ||
| listeners[id].interval = runTransactions({ eventName, txKey, since: options.since }); | ||
| listeners[id].interval = runTransactions({ eventName, txKey, since: options.since, platform: options.platform, label: options.label }); | ||
| return { eventName, handler, txKey, options, id }; | ||
| }, | ||
| deregister: ({ eventName, txKey }) => { | ||
| deregister: async ({ eventName, txKey, platform, label }) => { | ||
| const removeKeys = Object.keys(listeners).filter(id => { | ||
| const [_eventName, _txKey] = id.split(SEPERATOR); | ||
| const [_fullEventName, _txKey] = id.split(SEPERATOR); | ||
| const fullEventName = getFullEventName({ eventName, platform, label }); | ||
| if (eventName && txKey) { | ||
| return _eventName === eventName && _txKey === txKey; | ||
| return _fullEventName === fullEventName && _txKey === txKey; | ||
| } | ||
| if (eventName) { | ||
| return _eventName === eventName; | ||
| return _fullEventName === fullEventName; | ||
| } | ||
@@ -186,0 +216,0 @@ if (txKey) { |
+1
-1
| { | ||
| "name": "@buildable/messages", | ||
| "version": "1.0.1", | ||
| "version": "1.0.2", | ||
| "description": "A fully managed messaging service that lets you easily exchange event data across any app or resource.", | ||
@@ -5,0 +5,0 @@ "main": "dist/src/index.js", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
18893
15.05%236
15.69%4
33.33%