@sockethub/server 5.0.0-alpha.4 → 5.0.0-alpha.6

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.
Files changed (105) hide show
  1. package/README.md +54 -60
  2. package/bin/sockethub +4 -3
  3. package/package.json +42 -54
  4. package/res/socket.io.js +4908 -0
  5. package/res/sockethub-client.js +602 -0
  6. package/res/sockethub-client.min.js +19 -0
  7. package/sockethub.config.example.json +2 -3
  8. package/src/bootstrap/init.d.ts +16 -13
  9. package/src/bootstrap/init.test.ts +211 -0
  10. package/src/bootstrap/init.ts +152 -76
  11. package/src/bootstrap/load-platforms.ts +151 -0
  12. package/src/config.test.ts +27 -22
  13. package/src/config.ts +82 -86
  14. package/src/defaults.json +24 -16
  15. package/src/index.ts +61 -22
  16. package/src/janitor.test.ts +191 -169
  17. package/src/janitor.ts +141 -118
  18. package/src/listener.ts +148 -58
  19. package/src/middleware/create-activity-object.test.ts +28 -8
  20. package/src/middleware/create-activity-object.ts +16 -10
  21. package/src/middleware/expand-activity-stream.test.data.ts +331 -345
  22. package/src/middleware/expand-activity-stream.test.ts +65 -66
  23. package/src/middleware/expand-activity-stream.ts +26 -21
  24. package/src/middleware/store-credentials.test.ts +74 -60
  25. package/src/middleware/store-credentials.ts +14 -8
  26. package/src/middleware/validate.test.data.ts +240 -242
  27. package/src/middleware/validate.test.ts +39 -78
  28. package/src/middleware/validate.ts +62 -36
  29. package/src/middleware.test.ts +168 -138
  30. package/src/middleware.ts +57 -55
  31. package/src/platform-instance.test.ts +508 -214
  32. package/src/platform-instance.ts +324 -231
  33. package/src/platform.test.ts +375 -0
  34. package/src/platform.ts +306 -117
  35. package/src/process-manager.ts +75 -51
  36. package/src/routes.test.ts +43 -89
  37. package/src/routes.ts +40 -78
  38. package/src/sentry.test.ts +106 -0
  39. package/src/sentry.ts +19 -0
  40. package/src/sockethub.ts +190 -129
  41. package/src/util.ts +5 -0
  42. package/coverage/tmp/coverage-39338-1663949520416-0.json +0 -1
  43. package/dist/bootstrap/init.d.ts +0 -18
  44. package/dist/bootstrap/init.js +0 -64
  45. package/dist/bootstrap/init.js.map +0 -1
  46. package/dist/bootstrap/platforms.js +0 -75
  47. package/dist/config.d.ts +0 -12
  48. package/dist/config.js +0 -107
  49. package/dist/config.js.map +0 -1
  50. package/dist/defaults.json +0 -28
  51. package/dist/index.d.ts +0 -1
  52. package/dist/index.js +0 -29
  53. package/dist/index.js.map +0 -1
  54. package/dist/janitor.d.ts +0 -30
  55. package/dist/janitor.js +0 -120
  56. package/dist/janitor.js.map +0 -1
  57. package/dist/listener.d.ts +0 -31
  58. package/dist/listener.js +0 -94
  59. package/dist/listener.js.map +0 -1
  60. package/dist/middleware/create-activity-object.d.ts +0 -8
  61. package/dist/middleware/create-activity-object.js +0 -19
  62. package/dist/middleware/create-activity-object.js.map +0 -1
  63. package/dist/middleware/expand-activity-stream.d.ts +0 -3
  64. package/dist/middleware/expand-activity-stream.js +0 -36
  65. package/dist/middleware/expand-activity-stream.js.map +0 -1
  66. package/dist/middleware/expand-activity-stream.test.data.d.ts +0 -480
  67. package/dist/middleware/expand-activity-stream.test.data.js +0 -360
  68. package/dist/middleware/expand-activity-stream.test.data.js.map +0 -1
  69. package/dist/middleware/store-credentials.d.ts +0 -3
  70. package/dist/middleware/store-credentials.js +0 -9
  71. package/dist/middleware/store-credentials.js.map +0 -1
  72. package/dist/middleware/validate.d.ts +0 -2
  73. package/dist/middleware/validate.js +0 -56
  74. package/dist/middleware/validate.js.map +0 -1
  75. package/dist/middleware/validate.test.data.d.ts +0 -532
  76. package/dist/middleware/validate.test.data.js +0 -263
  77. package/dist/middleware/validate.test.data.js.map +0 -1
  78. package/dist/middleware.d.ts +0 -21
  79. package/dist/middleware.js +0 -56
  80. package/dist/middleware.js.map +0 -1
  81. package/dist/platform-instance.d.ts +0 -78
  82. package/dist/platform-instance.js +0 -226
  83. package/dist/platform-instance.js.map +0 -1
  84. package/dist/platform.d.ts +0 -6
  85. package/dist/platform.js +0 -176
  86. package/dist/platform.js.map +0 -1
  87. package/dist/process-manager.d.ts +0 -11
  88. package/dist/process-manager.js +0 -82
  89. package/dist/process-manager.js.map +0 -1
  90. package/dist/routes.d.ts +0 -13
  91. package/dist/routes.js +0 -83
  92. package/dist/routes.js.map +0 -1
  93. package/dist/sockethub.d.ts +0 -18
  94. package/dist/sockethub.js +0 -112
  95. package/dist/sockethub.js.map +0 -1
  96. package/src/bootstrap/platforms.js +0 -75
  97. package/test/init-suite.js +0 -41
  98. package/test/sockethub-suite.js +0 -25
  99. package/tsconfig.json +0 -18
  100. package/views/examples/dummy.ejs +0 -95
  101. package/views/examples/feeds.ejs +0 -90
  102. package/views/examples/irc.ejs +0 -239
  103. package/views/examples/shared.js +0 -72
  104. package/views/examples/xmpp.ejs +0 -217
  105. package/views/index.ejs +0 -17
package/src/platform.ts CHANGED
@@ -1,169 +1,358 @@
1
- import debug from 'debug';
2
- import {IActivityStream, CallbackInterface} from "@sockethub/schemas";
3
- import crypto, {getPlatformId} from "@sockethub/crypto";
4
- import {CredentialsStore, JobQueue, RedisConfig} from "@sockethub/data-layer";
5
- import {JobDataDecrypted} from "@sockethub/data-layer/dist";
1
+ /**
2
+ * This runs as a stand-alone separate process that handles:
3
+ * 1. Starting up an instance of a given platform
4
+ * 2. Connecting to the redis job queue
5
+ * 3. Sending the platform jobs
6
+ * 4. Handling the result by putting it in the outgoing queue for
7
+ * sockethub core to send back to the client.
8
+ *
9
+ * If an exception is thrown by the platform, this process will die along with
10
+ * it and sockethub will start up another process. This ensures memory safety.
11
+ */
12
+ import { crypto, getPlatformId } from "@sockethub/crypto";
13
+ import {
14
+ CredentialsStore,
15
+ type JobDataDecrypted,
16
+ JobWorker,
17
+ } from "@sockethub/data-layer";
18
+ import type { JobHandler } from "@sockethub/data-layer";
19
+ import type {
20
+ ActivityStream,
21
+ CredentialsObject,
22
+ PersistentPlatformInterface,
23
+ PlatformCallback,
24
+ PlatformInterface,
25
+ PlatformSession,
26
+ } from "@sockethub/schemas";
27
+ import debug from "debug";
28
+ import config from "./config";
6
29
 
7
30
  // command-line params
8
31
  const parentId = process.argv[2];
9
32
  const platformName = process.argv[3];
10
33
  let identifier = process.argv[4];
34
+ const redisUrl = process.env.REDIS_URL;
35
+
11
36
  const loggerPrefix = `sockethub:platform:${platformName}:${identifier}`;
12
37
  let logger = debug(loggerPrefix);
13
38
 
14
- const redisConfig = process.env.REDIS_URL ? process.env.REDIS_URL
15
- : { host: process.env.REDIS_HOST, port: process.env.REDIS_PORT };
16
- // eslint-disable-next-line @typescript-eslint/no-var-requires
17
- const PlatformModule = require(`@sockethub/platform-${platformName}`);
39
+ // conditionally initialize sentry
40
+ let sentry: { readonly reportError: (err: Error) => void } = {
41
+ reportError: (err: Error) => {},
42
+ };
43
+ (async () => {
44
+ if (config.get("sentry:dsn")) {
45
+ logger("initializing sentry");
46
+ sentry = await import("./sentry");
47
+ }
48
+ })();
18
49
 
19
- let jobQueue: JobQueue;
20
- let jobQueueStarted = false;
21
- let parentSecret1: string, parentSecret2: string;
50
+ let jobWorker: JobWorker;
51
+ let jobWorkerStarted = false;
52
+ let parentSecret1: string;
53
+ let parentSecret2: string;
22
54
 
23
- logger(`platform handler initialized for ${platformName} ${identifier}`);
55
+ logger(`platform handler initializing for ${platformName} ${identifier}`);
24
56
 
25
- export interface PlatformSession {
26
- debug(msg: string): void;
27
- sendToClient(msg: IActivityStream, special?: string): void;
28
- updateActor(credentials: object): void;
29
- }
30
57
  interface SecretInterface {
31
- parentSecret1: string,
32
- parentSecret2: string
58
+ parentSecret1: string;
59
+ parentSecret2: string;
60
+ }
61
+
62
+ interface SecretFromParent extends Array<string | SecretInterface> {
63
+ 0: string;
64
+ 1: SecretInterface;
65
+ }
66
+
67
+ /**
68
+ * Initialize platform module
69
+ */
70
+ const platformSession: PlatformSession = {
71
+ debug: debug(`sockethub:platform:${platformName}:${identifier}`),
72
+ sendToClient: getSendFunction("message"),
73
+ updateActor: updateActor,
74
+ };
75
+
76
+ const platform: PlatformInterface = await (async () => {
77
+ const PlatformModule = await import(`@sockethub/platform-${platformName}`);
78
+ const p = new PlatformModule.default(platformSession) as PlatformInterface;
79
+ logger(`platform handler loaded for ${platformName} ${identifier}`);
80
+ return p as PlatformInterface;
81
+ })();
82
+
83
+ /**
84
+ * Safely send error message to parent process, handling IPC channel closure
85
+ */
86
+ function safeProcessSend(message: [string, string]) {
87
+ if (process.send && process.connected) {
88
+ try {
89
+ process.send(message);
90
+ } catch (ipcErr) {
91
+ console.error(`Failed to report error via IPC: ${ipcErr.message}`);
92
+ }
93
+ } else {
94
+ console.error("Cannot report error: IPC channel not available");
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Type guard to check if a platform is persistent and has credentialsHash.
100
+ */
101
+ function isPersistentPlatform(
102
+ platform: PlatformInterface,
103
+ ): platform is PersistentPlatformInterface {
104
+ return platform.config.persist === true;
33
105
  }
34
- interface SecretFromParent extends Array<string|SecretInterface>{0: string, 1: SecretInterface}
35
106
 
36
107
  /**
37
108
  * Handle any uncaught errors from the platform by alerting the worker and shutting down.
38
109
  */
39
- process.on('uncaughtException', (err) => {
40
- console.log('EXCEPTION IN PLATFORM');
41
- // eslint-disable-next-line security-node/detect-crlf
42
- console.log(err.stack);
43
- process.send(['error', err.toString()]);
44
- process.exit(1);
110
+ process.once("uncaughtException", (err: Error) => {
111
+ console.log("EXCEPTION IN PLATFORM");
112
+ sentry.reportError(err);
113
+ console.log("error:\n", err.stack);
114
+ safeProcessSend(["error", err.toString()]);
115
+ process.exit(1);
116
+ });
117
+
118
+ process.once("unhandledRejection", (err: Error) => {
119
+ console.log("EXCEPTION IN PLATFORM");
120
+ sentry.reportError(err);
121
+ console.log("error:\n", err.stack);
122
+ safeProcessSend(["error", err.toString()]);
123
+ process.exit(1);
45
124
  });
46
125
 
47
126
  /**
48
- * Incoming messages from the worker to this platform. Data is an array, the first property is the
49
- * method to call, the rest are params.
127
+ * In the case of a parent disconnect, terminate child process.
50
128
  */
51
- process.on('message', async (data: SecretFromParent) => {
52
- if (data[0] === 'secrets') {
53
- const {parentSecret2: parentSecret3, parentSecret1: parentSecret} = data[1];
54
- parentSecret1 = parentSecret;
55
- parentSecret2 = parentSecret3;
56
- await startQueueListener();
57
- } else {
58
- throw new Error('received unknown command from parent thread');
59
- }
129
+ // Detect parent death via IPC disconnect
130
+ process.on("disconnect", () => {
131
+ console.log(`Parent disconnected. Child ${process.pid} exiting.`);
132
+ process.exit(1);
60
133
  });
61
134
 
62
135
  /**
63
- * Initialize platform module
136
+ * Incoming messages from the worker to this platform. Data is an array, the first property is the
137
+ * method to call, the rest are params.
64
138
  */
65
- const platformSession: PlatformSession = {
66
- debug: debug(`sockethub:platform:${platformName}:${identifier}`),
67
- sendToClient: getSendFunction('message'),
68
- updateActor: updateActor
69
- };
70
- const platform = new PlatformModule(platformSession);
139
+ process.on("message", async (data: SecretFromParent) => {
140
+ if (data[0] === "secrets") {
141
+ const { parentSecret2: parentSecret3, parentSecret1: parentSecret } =
142
+ data[1];
143
+ parentSecret1 = parentSecret;
144
+ parentSecret2 = parentSecret3;
145
+ await startQueueListener();
146
+ } else {
147
+ throw new Error("received unknown command from parent thread");
148
+ }
149
+ });
71
150
 
72
151
  /**
73
152
  * Returns a function used to handle completed jobs from the platform code (the `done` callback).
74
153
  */
75
- function getJobHandler() {
76
- return (job: JobDataDecrypted, done: CallbackInterface) => {
77
- const jobLog = debug(`${loggerPrefix}:${job.sessionId}`);
78
- jobLog(`received ${job.title} ${job.msg.type}`);
79
- const credentialStore = new CredentialsStore(
80
- parentId, job.sessionId, parentSecret1 + job.msg.sessionSecret, redisConfig as RedisConfig
81
- );
82
- delete job.msg.sessionSecret;
83
-
84
- let jobCallbackCalled = false;
85
- const doneCallback = (err, result) => {
86
- if (jobCallbackCalled) { return; }
87
- jobCallbackCalled = true;
88
- if (err) {
89
- jobLog(`failed ${job.title} ${job.msg.type}`);
90
- let errMsg;
91
- // some error objects (eg. TimeoutError) don't interpolate correctly to human-readable
92
- // so we have to do this little dance
93
- try {
94
- errMsg = err.toString();
95
- } catch (e) {
96
- errMsg = err;
97
- }
98
- done(new Error(errMsg));
99
- } else {
100
- jobLog(`completed ${job.title} ${job.msg.type}`);
101
- done(null, result);
102
- }
103
- };
154
+ function getJobHandler(): JobHandler {
155
+ return async (
156
+ job: JobDataDecrypted,
157
+ ): Promise<string | undefined | ActivityStream> => {
158
+ return new Promise((resolve, reject) => {
159
+ const jobLog = debug(`${loggerPrefix}:${job.sessionId}`);
160
+ jobLog(`received ${job.title} ${job.msg.type}`);
161
+ const credentialStore = new CredentialsStore(
162
+ parentId,
163
+ job.sessionId,
164
+ parentSecret1 + job.msg.sessionSecret,
165
+ {
166
+ url: redisUrl,
167
+ },
168
+ );
169
+ // biome-ignore lint/performance/noDelete: <explanation>
170
+ delete job.msg.sessionSecret;
104
171
 
105
- platform.config.requireCredentials ? platform.config.requireCredentials : [];
106
- if (platform.config.requireCredentials.includes(job.msg.type)) {
107
- // this method requires credentials and should be called even if the platform is not
108
- // yet initialized, because they need to authenticate before they are initialized.
109
- credentialStore.get(job.msg.actor.id, platform.credentialsHash).then((credentials) => {
110
- platform[job.msg.type](job.msg, credentials, doneCallback);
111
- }).catch((err) => {
112
- jobLog('error ' + err.toString());
113
- return done(new Error(err.toString()));
114
- });
115
- } else if ((platform.config.persist) && (!platform.initialized)) {
116
- done(new Error(`${job.msg.type} called on uninitialized platform`));
117
- } else {
118
- platform[job.msg.type](job.msg, doneCallback);
119
- }
120
- };
172
+ let jobCallbackCalled = false;
173
+ const doneCallback: PlatformCallback = (
174
+ err: Error | null,
175
+ result: null | ActivityStream,
176
+ ): void => {
177
+ if (jobCallbackCalled) {
178
+ resolve(null);
179
+ return;
180
+ }
181
+ jobCallbackCalled = true;
182
+ if (err) {
183
+ jobLog(`failed ${job.title} ${job.msg.type}`);
184
+ let errMsg: string | Error;
185
+ // some error objects (e.g. TimeoutError) don't interpolate correctly
186
+ // to being human-readable, so we have to do this little dance
187
+ try {
188
+ errMsg = err.toString();
189
+ } catch (err) {
190
+ errMsg = err;
191
+ }
192
+ sentry.reportError(new Error(errMsg as string));
193
+ reject(new Error(errMsg as string));
194
+ } else {
195
+ jobLog(`completed ${job.title} ${job.msg.type}`);
196
+ resolve(result);
197
+ }
198
+ };
199
+
200
+ if (platform.config.requireCredentials?.includes(job.msg.type)) {
201
+ // This method requires credentials and should be called even if the platform is not
202
+ // yet initialized, because they need to authenticate before they are initialized.
203
+
204
+ // Get credentialsHash for validation.
205
+ // For persistent platforms: undefined (or empty string) initially, then we set to hash after first
206
+ // successful call.
207
+ // For stateless platforms: always undefined (no validation, credentials used once per request)
208
+ // CredentialsStore skips validation when credentialsHash is falsy (undefined or empty string)
209
+ const credentialsHash = isPersistentPlatform(platform)
210
+ ? platform.credentialsHash
211
+ : undefined;
212
+
213
+ credentialStore
214
+ .get(job.msg.actor.id, credentialsHash)
215
+ .then((credentials) => {
216
+ // Create wrapper callback that updates credentialsHash after successful call
217
+ const wrappedCallback: PlatformCallback = (
218
+ err: Error | null,
219
+ result: null | ActivityStream,
220
+ ): void => {
221
+ if (!err && isPersistentPlatform(platform)) {
222
+ // Update credentialsHash after successful platform call.
223
+ // Only persistent platforms track credential state across requests.
224
+ platform.credentialsHash = crypto.objectHash(
225
+ credentials.object,
226
+ );
227
+ }
228
+ doneCallback(err, result);
229
+ };
230
+
231
+ // Proceed with platform method call
232
+ platform[job.msg.type](
233
+ job.msg,
234
+ credentials,
235
+ wrappedCallback,
236
+ );
237
+ })
238
+ .catch((err) => {
239
+ // Credential store error (invalid/missing credentials)
240
+ jobLog(`credential error ${err.toString()}`);
241
+
242
+ /**
243
+ * Critical distinction: handle credential errors differently based on platform state.
244
+ *
245
+ * For INITIALIZED platforms (already running):
246
+ * - Reject ONLY this job via doneCallback(err, null)
247
+ * - Keep the platform process running
248
+ * - Why: Platform instances can be shared by multiple clients (sessions).
249
+ * Terminating on credential error would crash the platform for ALL users,
250
+ * including those with valid credentials. This would create a DoS vector
251
+ * where one user's mistake (browser refresh with wrong creds, mistyped
252
+ * password, expired token) would break the service for everyone sharing
253
+ * that platform instance.
254
+ * - The failing client receives an error message, while other clients
255
+ * continue operating normally.
256
+ *
257
+ * For UNINITIALIZED platforms (not yet started):
258
+ * - Terminate the platform process via reject(err)
259
+ * - Why: If the initial connection fails due to invalid credentials, there's
260
+ * no valid session to preserve. The platform instance was created specifically
261
+ * for this connection attempt and has no other users. Terminating allows
262
+ * proper cleanup and a fresh start on the next attempt.
263
+ * - Error is reported to Sentry for monitoring authentication issues.
264
+ */
265
+ if (platform.config.initialized) {
266
+ // Platform already running - reject job only, preserve platform instance
267
+ doneCallback(err, null);
268
+ } else {
269
+ // Platform not initialized - terminate platform process
270
+ sentry.reportError(err);
271
+ reject(err);
272
+ }
273
+ });
274
+ } else if (
275
+ platform.config.persist &&
276
+ !platform.config.initialized
277
+ ) {
278
+ reject(
279
+ new Error(
280
+ `${job.msg.type} called on uninitialized platform`,
281
+ ),
282
+ );
283
+ } else {
284
+ try {
285
+ platform[job.msg.type](job.msg, doneCallback);
286
+ } catch (err) {
287
+ jobLog(`platform call failed ${err.toString()}`);
288
+ sentry.reportError(err);
289
+ reject(err);
290
+ }
291
+ }
292
+ });
293
+ };
121
294
  }
122
295
 
123
296
  /**
124
- * Get an function which sends a message to the parent thread (PlatformInstance). The platform
297
+ * Get a function which sends a message to the parent thread (PlatformInstance). The platform
125
298
  * can call that function to send messages back to the client.
126
299
  * @param command string containing the type of command to be sent. 'message' or 'close'
127
300
  */
128
301
  function getSendFunction(command: string) {
129
- return function (msg: IActivityStream, special?: string) {
130
- process.send([command, msg, special]);
131
- };
302
+ return (msg: ActivityStream, special?: string) => {
303
+ if (platform.config.persist) {
304
+ process.send([command, msg, special]);
305
+ } else {
306
+ logger(
307
+ "sendToClient called on non-persistent platform, rejecting.",
308
+ );
309
+ }
310
+ };
132
311
  }
133
312
 
134
313
  /**
135
- * When a user changes it's actor name, the channel identifier changes, we need to ensure that
314
+ * When a user changes its actor name, the channel identifier changes, we need to ensure that
136
315
  * both the queue thread (listening on the channel for jobs) and the logging object are updated.
137
316
  * @param credentials
138
317
  */
139
- async function updateActor(credentials) {
140
- identifier = getPlatformId(platformName, credentials.actor.id);
141
- logger(`platform actor updated to ${credentials.actor.id} identifier ${identifier}`);
142
- logger = debug(`sockethub:platform:${identifier}`);
143
- platform.credentialsHash = crypto.objectHash(credentials.object);
144
- platform.debug = debug(`sockethub:platform:${platformName}:${identifier}`);
145
- process.send(['updateActor', undefined, identifier]);
146
- await startQueueListener(true);
318
+ async function updateActor(credentials: CredentialsObject): Promise<void> {
319
+ identifier = getPlatformId(platformName, credentials.actor.id);
320
+ logger(
321
+ `platform actor updated to ${credentials.actor.id} identifier ${identifier}`,
322
+ );
323
+ logger = debug(`sockethub:platform:${identifier}`);
324
+
325
+ // Update credentialsHash for persistent platforms (tracks actor-specific state)
326
+ if (isPersistentPlatform(platform)) {
327
+ platform.credentialsHash = crypto.objectHash(credentials.object);
328
+ }
329
+
330
+ platform.debug = debug(`sockethub:platform:${platformName}:${identifier}`);
331
+ process.send(["updateActor", undefined, identifier]);
332
+ await startQueueListener(true);
147
333
  }
148
334
 
149
335
  /**
150
336
  * Starts listening on the queue for incoming jobs
151
- * @param refresh boolean if the param is true, we re-init the queue.process
337
+ * @param refresh boolean if the param is true, we re-init the `queue.process`
152
338
  * (used when identifier changes)
153
339
  */
154
340
  async function startQueueListener(refresh = false) {
155
- if (jobQueueStarted) {
156
- if (refresh) {
157
- await jobQueue.shutdown();
158
- } else {
159
- logger('start queue called multiple times, skipping');
160
- return;
341
+ if (jobWorkerStarted) {
342
+ if (refresh) {
343
+ await jobWorker.shutdown();
344
+ } else {
345
+ logger("start queue called multiple times, skipping");
346
+ return;
347
+ }
161
348
  }
162
- }
163
- jobQueue = new JobQueue(
164
- parentId, identifier, parentSecret1 + parentSecret2, redisConfig as RedisConfig
165
- );
166
- logger('listening on the queue for incoming jobs');
167
- jobQueue.onJob(getJobHandler());
168
- jobQueueStarted = true;
349
+ jobWorker = new JobWorker(
350
+ parentId,
351
+ identifier,
352
+ parentSecret1 + parentSecret2,
353
+ { url: redisUrl },
354
+ );
355
+ logger("listening on the queue for incoming jobs");
356
+ jobWorker.onJob(getJobHandler());
357
+ jobWorkerStarted = true;
169
358
  }
@@ -1,64 +1,88 @@
1
- import init from './bootstrap/init';
1
+ import { getPlatformId } from "@sockethub/crypto";
2
+
3
+ import type { IInitObject } from "./bootstrap/init.js";
2
4
  import PlatformInstance, {
3
- platformInstances, PlatformInstanceParams, MessageFromParent } from "./platform-instance";
4
- import {getPlatformId} from "@sockethub/crypto";
5
+ platformInstances,
6
+ type PlatformInstanceParams,
7
+ type MessageFromParent,
8
+ } from "./platform-instance.js";
5
9
 
6
10
  class ProcessManager {
7
- private readonly parentId: string;
8
- private readonly parentSecret1: string;
9
- private readonly parentSecret2: string;
11
+ private readonly parentId: string;
12
+ private readonly parentSecret1: string;
13
+ private readonly parentSecret2: string;
14
+ private init: IInitObject;
10
15
 
11
- constructor(parentId: string, parentSecret1: string, parentSecret2: string) {
12
- this.parentId = parentId;
13
- this.parentSecret1 = parentSecret1;
14
- this.parentSecret2 = parentSecret2;
15
- }
16
+ constructor(
17
+ parentId: string,
18
+ parentSecret1: string,
19
+ parentSecret2: string,
20
+ init: IInitObject,
21
+ ) {
22
+ this.parentId = parentId;
23
+ this.parentSecret1 = parentSecret1;
24
+ this.parentSecret2 = parentSecret2;
25
+ this.init = init;
26
+ }
16
27
 
17
- get(platform: string, actorId: string, sessionId?: string): PlatformInstance {
18
- const platformDetails = init.platforms.get(platform);
19
- let pi;
28
+ get(
29
+ platform: string,
30
+ actorId: string,
31
+ sessionId?: string,
32
+ ): PlatformInstance {
33
+ const platformDetails = this.init.platforms.get(platform);
34
+ let pi: PlatformInstance;
20
35
 
21
- if (platformDetails.config.persist) {
22
- // ensure process is started - one for each actor
23
- pi = this.ensureProcess(platform, sessionId, actorId);
24
- } else {
25
- // ensure process is started - one for all jobs
26
- pi = this.ensureProcess(platform);
36
+ if (platformDetails.config.persist) {
37
+ // ensure process is started - one for each actor
38
+ pi = this.ensureProcess(platform, sessionId, actorId);
39
+ } else {
40
+ // ensure process is started - one for all jobs
41
+ pi = this.ensureProcess(platform);
42
+ }
43
+ pi.config = platformDetails.config;
44
+ return pi;
27
45
  }
28
- pi.config = platformDetails.config;
29
- return pi;
30
- }
31
46
 
32
- private createPlatformInstance(identifier: string, platform: string,
33
- actor?: string): PlatformInstance {
34
- const secrets: MessageFromParent = [
35
- 'secrets', {
36
- parentSecret1: this.parentSecret1,
37
- parentSecret2: this.parentSecret2
38
- }
39
- ];
40
- const platformInstanceConfig: PlatformInstanceParams = {
41
- identifier: identifier,
42
- platform: platform,
43
- parentId: this.parentId,
44
- actor: actor
45
- };
46
- const platformInstance = new PlatformInstance(platformInstanceConfig);
47
- platformInstance.initQueue(this.parentSecret1 + this.parentSecret2);
48
- platformInstance.process.send(secrets);
49
- return platformInstance;
50
- }
47
+ private createPlatformInstance(
48
+ identifier: string,
49
+ platform: string,
50
+ actor?: string,
51
+ ): PlatformInstance {
52
+ const secrets: MessageFromParent = [
53
+ "secrets",
54
+ {
55
+ parentSecret1: this.parentSecret1,
56
+ parentSecret2: this.parentSecret2,
57
+ },
58
+ ];
59
+ const platformInstanceConfig: PlatformInstanceParams = {
60
+ identifier: identifier,
61
+ platform: platform,
62
+ parentId: this.parentId,
63
+ actor: actor,
64
+ };
65
+ const platformInstance = new PlatformInstance(platformInstanceConfig);
66
+ platformInstance.initQueue(this.parentSecret1 + this.parentSecret2);
67
+ platformInstance.process.send(secrets);
68
+ return platformInstance;
69
+ }
51
70
 
52
- private ensureProcess(platform: string, sessionId?: string, actor?: string): PlatformInstance {
53
- const identifier = getPlatformId(platform, actor);
54
- const platformInstance = platformInstances.get(identifier) ||
55
- this.createPlatformInstance(identifier, platform, actor);
56
- if (sessionId) {
57
- platformInstance.registerSession(sessionId);
71
+ private ensureProcess(
72
+ platform: string,
73
+ sessionId?: string,
74
+ actor?: string,
75
+ ): PlatformInstance {
76
+ const identifier = getPlatformId(platform, actor);
77
+ const platformInstance =
78
+ platformInstances.get(identifier) ||
79
+ this.createPlatformInstance(identifier, platform, actor);
80
+ if (sessionId) {
81
+ platformInstance.registerSession(sessionId);
82
+ }
83
+ platformInstances.set(identifier, platformInstance);
84
+ return platformInstance;
58
85
  }
59
- platformInstances.set(identifier, platformInstance);
60
- return platformInstance;
61
- }
62
86
  }
63
87
 
64
88
  export default ProcessManager;