@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.
- package/README.md +54 -60
- package/bin/sockethub +4 -3
- package/package.json +42 -54
- package/res/socket.io.js +4908 -0
- package/res/sockethub-client.js +602 -0
- package/res/sockethub-client.min.js +19 -0
- package/sockethub.config.example.json +2 -3
- package/src/bootstrap/init.d.ts +16 -13
- package/src/bootstrap/init.test.ts +211 -0
- package/src/bootstrap/init.ts +152 -76
- package/src/bootstrap/load-platforms.ts +151 -0
- package/src/config.test.ts +27 -22
- package/src/config.ts +82 -86
- package/src/defaults.json +24 -16
- package/src/index.ts +61 -22
- package/src/janitor.test.ts +191 -169
- package/src/janitor.ts +141 -118
- package/src/listener.ts +148 -58
- package/src/middleware/create-activity-object.test.ts +28 -8
- package/src/middleware/create-activity-object.ts +16 -10
- package/src/middleware/expand-activity-stream.test.data.ts +331 -345
- package/src/middleware/expand-activity-stream.test.ts +65 -66
- package/src/middleware/expand-activity-stream.ts +26 -21
- package/src/middleware/store-credentials.test.ts +74 -60
- package/src/middleware/store-credentials.ts +14 -8
- package/src/middleware/validate.test.data.ts +240 -242
- package/src/middleware/validate.test.ts +39 -78
- package/src/middleware/validate.ts +62 -36
- package/src/middleware.test.ts +168 -138
- package/src/middleware.ts +57 -55
- package/src/platform-instance.test.ts +508 -214
- package/src/platform-instance.ts +324 -231
- package/src/platform.test.ts +375 -0
- package/src/platform.ts +306 -117
- package/src/process-manager.ts +75 -51
- package/src/routes.test.ts +43 -89
- package/src/routes.ts +40 -78
- package/src/sentry.test.ts +106 -0
- package/src/sentry.ts +19 -0
- package/src/sockethub.ts +190 -129
- package/src/util.ts +5 -0
- package/coverage/tmp/coverage-39338-1663949520416-0.json +0 -1
- package/dist/bootstrap/init.d.ts +0 -18
- package/dist/bootstrap/init.js +0 -64
- package/dist/bootstrap/init.js.map +0 -1
- package/dist/bootstrap/platforms.js +0 -75
- package/dist/config.d.ts +0 -12
- package/dist/config.js +0 -107
- package/dist/config.js.map +0 -1
- package/dist/defaults.json +0 -28
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -29
- package/dist/index.js.map +0 -1
- package/dist/janitor.d.ts +0 -30
- package/dist/janitor.js +0 -120
- package/dist/janitor.js.map +0 -1
- package/dist/listener.d.ts +0 -31
- package/dist/listener.js +0 -94
- package/dist/listener.js.map +0 -1
- package/dist/middleware/create-activity-object.d.ts +0 -8
- package/dist/middleware/create-activity-object.js +0 -19
- package/dist/middleware/create-activity-object.js.map +0 -1
- package/dist/middleware/expand-activity-stream.d.ts +0 -3
- package/dist/middleware/expand-activity-stream.js +0 -36
- package/dist/middleware/expand-activity-stream.js.map +0 -1
- package/dist/middleware/expand-activity-stream.test.data.d.ts +0 -480
- package/dist/middleware/expand-activity-stream.test.data.js +0 -360
- package/dist/middleware/expand-activity-stream.test.data.js.map +0 -1
- package/dist/middleware/store-credentials.d.ts +0 -3
- package/dist/middleware/store-credentials.js +0 -9
- package/dist/middleware/store-credentials.js.map +0 -1
- package/dist/middleware/validate.d.ts +0 -2
- package/dist/middleware/validate.js +0 -56
- package/dist/middleware/validate.js.map +0 -1
- package/dist/middleware/validate.test.data.d.ts +0 -532
- package/dist/middleware/validate.test.data.js +0 -263
- package/dist/middleware/validate.test.data.js.map +0 -1
- package/dist/middleware.d.ts +0 -21
- package/dist/middleware.js +0 -56
- package/dist/middleware.js.map +0 -1
- package/dist/platform-instance.d.ts +0 -78
- package/dist/platform-instance.js +0 -226
- package/dist/platform-instance.js.map +0 -1
- package/dist/platform.d.ts +0 -6
- package/dist/platform.js +0 -176
- package/dist/platform.js.map +0 -1
- package/dist/process-manager.d.ts +0 -11
- package/dist/process-manager.js +0 -82
- package/dist/process-manager.js.map +0 -1
- package/dist/routes.d.ts +0 -13
- package/dist/routes.js +0 -83
- package/dist/routes.js.map +0 -1
- package/dist/sockethub.d.ts +0 -18
- package/dist/sockethub.js +0 -112
- package/dist/sockethub.js.map +0 -1
- package/src/bootstrap/platforms.js +0 -75
- package/test/init-suite.js +0 -41
- package/test/sockethub-suite.js +0 -25
- package/tsconfig.json +0 -18
- package/views/examples/dummy.ejs +0 -95
- package/views/examples/feeds.ejs +0 -90
- package/views/examples/irc.ejs +0 -239
- package/views/examples/shared.js +0 -72
- package/views/examples/xmpp.ejs +0 -217
- package/views/index.ejs +0 -17
package/src/platform.ts
CHANGED
|
@@ -1,169 +1,358 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
|
20
|
-
let
|
|
21
|
-
let parentSecret1: string
|
|
50
|
+
let jobWorker: JobWorker;
|
|
51
|
+
let jobWorkerStarted = false;
|
|
52
|
+
let parentSecret1: string;
|
|
53
|
+
let parentSecret2: string;
|
|
22
54
|
|
|
23
|
-
logger(`platform handler
|
|
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
|
-
|
|
32
|
-
|
|
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.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
*
|
|
49
|
-
* method to call, the rest are params.
|
|
127
|
+
* In the case of a parent disconnect, terminate child process.
|
|
50
128
|
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
|
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
|
-
|
|
130
|
-
|
|
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
|
|
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
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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
|
}
|
package/src/process-manager.ts
CHANGED
|
@@ -1,64 +1,88 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { getPlatformId } from "@sockethub/crypto";
|
|
2
|
+
|
|
3
|
+
import type { IInitObject } from "./bootstrap/init.js";
|
|
2
4
|
import PlatformInstance, {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
+
platformInstances,
|
|
6
|
+
type PlatformInstanceParams,
|
|
7
|
+
type MessageFromParent,
|
|
8
|
+
} from "./platform-instance.js";
|
|
5
9
|
|
|
6
10
|
class ProcessManager {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
private readonly parentId: string;
|
|
12
|
+
private readonly parentSecret1: string;
|
|
13
|
+
private readonly parentSecret2: string;
|
|
14
|
+
private init: IInitObject;
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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;
|