Skip to content

Commit

Permalink
fix: exec on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Elin Angelow committed Feb 2, 2024
1 parent 8fe2cdd commit f34305e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
8 changes: 7 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const {v4: uuidv4} = require('uuid');

function mergeDeep(...objects) {
const isObject = obj => obj && typeof obj === 'object';

Expand Down Expand Up @@ -48,6 +50,9 @@ const remoteMsgLen = (data) => {
const remoteMsgDecode = (data) => {
return JSON.parse(data.slice(2));
};
const initMeta = () => ({
passTrough: {traceId: uuidv4(), time: process.hrtime()},
});

async function sleep(timeInSeconds) {
return new Promise(
Expand All @@ -61,5 +66,6 @@ module.exports = {
counter,
remoteMsgEncode,
remoteMsgLen,
remoteMsgDecode
remoteMsgDecode,
initMeta
};
8 changes: 7 additions & 1 deletion lib/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const Sql = ({
cwd: (config?.cwd || []).map((p) => join(require.main.path, p))
});
this.exposed = await exposeMethods(config);
await Promise.all((config?.execOnBoot || []).map((name) => {
if (!this.exposed.methods[name]) {
throw new Error('ExecOnBootMethodNotFound');
}
return this.exposed.methods[name]();
}));
await Promise.all(Object.keys(this.exposed.methods).map((method) => {
const fn = this.exposed.methods[method];
this.add({
Expand Down Expand Up @@ -44,7 +50,7 @@ const Sql = ({
return super.init && await super.init();
}
async stop () {
await this.exposed?.end();
await this.exposed?.stop();
return super.stop && await super.stop();
}
}
Expand Down
22 changes: 10 additions & 12 deletions lib/wss.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {createWriteStream} = require('node:fs');
const {WebSocketServer} = require('ws');
const {v4: uuidv4} = require('uuid');
const {counter} = require('bridg-wrong/lib/helpers.js');
const {initMeta} = require('./helpers.js');

const requestCounter = counter();
const dumpingComm = (fn) => {
Expand Down Expand Up @@ -93,7 +94,7 @@ const Wss = ({
}
// request, it will wait for response, and send data back
// here we are creating response matcher also
const rq = this.requestAdd(message.params.connectionId);
const rq = this.requestAdd(message.params.connectionId, message);
try {
const encoded = this.encode({
...message.params,
Expand All @@ -120,11 +121,12 @@ const Wss = ({
}});
return super.init && await super.init();
}
requestAdd(connectionId) {
requestAdd(connectionId, message) {
const id = requestCounter();
const wait = new Promise((resolve, reject) => {
const timeOut = setTimeout(
() => {
console.log(JSON.stringify(message));
this.requests.delete(id);
reject(new Error('ResponseTimeout'));
},
Expand Down Expand Up @@ -159,9 +161,8 @@ const Wss = ({
method: `${config.sink}.disconnected`,
params: {},
meta: {
passTrough: {traceId: uuidv4(), time: process.hrtime()
},
connectionId
...initMeta(),
connectionId
}});
} catch (e) {}
if (conn?.timeOut) {
Expand Down Expand Up @@ -194,10 +195,8 @@ const Wss = ({
method: `${config.sink}.message`,
params: decoded,
meta: {
passTrough: {
traceId: uuidv4(),
time: process.hrtime()
}, connectionId
...initMeta(),
connectionId
}
});
// it is notification, return nothing but pass notification request further
Expand Down Expand Up @@ -279,9 +278,8 @@ const Wss = ({
method: `${config.sink}.connected`,
params: {},
meta: {
timeout: 30000,
passTrough: {traceId: uuidv4(), time: process.hrtime()
},
...initMeta(),
timeout: 30000,
connectionId
}
});
Expand Down

0 comments on commit f34305e

Please sign in to comment.