@sockethub/server 5.0.0-alpha.3 → 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 -60
- 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 +20 -7
- package/src/bootstrap/init.test.ts +211 -0
- package/src/bootstrap/init.ts +152 -75
- package/src/bootstrap/load-platforms.ts +151 -0
- package/src/config.test.ts +27 -22
- package/src/config.ts +82 -78
- package/src/defaults.json +24 -16
- package/src/index.ts +67 -27
- package/src/janitor.test.ts +211 -0
- package/src/janitor.ts +145 -77
- package/src/listener.ts +151 -57
- package/src/middleware/create-activity-object.test.ts +28 -8
- package/src/middleware/create-activity-object.ts +17 -8
- package/src/middleware/expand-activity-stream.test.data.ts +332 -346
- package/src/middleware/expand-activity-stream.test.ts +65 -66
- package/src/middleware/expand-activity-stream.ts +29 -19
- package/src/middleware/store-credentials.test.ts +74 -62
- package/src/middleware/store-credentials.ts +15 -15
- package/src/middleware/validate.test.data.ts +240 -242
- package/src/middleware/validate.test.ts +39 -78
- package/src/middleware/validate.ts +63 -39
- package/src/middleware.test.ts +168 -138
- package/src/middleware.ts +62 -43
- package/src/platform-instance.test.ts +507 -213
- package/src/platform-instance.ts +337 -219
- package/src/platform.test.ts +375 -0
- package/src/platform.ts +306 -139
- package/src/process-manager.ts +75 -51
- package/src/routes.test.ts +43 -89
- package/src/routes.ts +40 -77
- package/src/sentry.test.ts +106 -0
- package/src/sentry.ts +19 -0
- package/src/sockethub.ts +186 -153
- package/src/util.ts +5 -0
- package/coverage/tmp/coverage-93126-1649152190997-0.json +0 -1
- package/dist/bootstrap/init.d.ts +0 -18
- package/dist/bootstrap/init.js +0 -63
- package/dist/bootstrap/init.js.map +0 -1
- package/dist/bootstrap/platforms.js +0 -75
- package/dist/common.d.ts +0 -3
- package/dist/common.js +0 -20
- package/dist/common.js.map +0 -1
- package/dist/config.d.ts +0 -6
- package/dist/config.js +0 -102
- package/dist/config.js.map +0 -1
- package/dist/crypto.d.ts +0 -10
- package/dist/crypto.js +0 -38
- package/dist/crypto.js.map +0 -1
- package/dist/defaults.json +0 -28
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -25
- package/dist/index.js.map +0 -1
- package/dist/janitor.d.ts +0 -15
- package/dist/janitor.js +0 -89
- package/dist/janitor.js.map +0 -1
- package/dist/listener.d.ts +0 -28
- package/dist/listener.js +0 -91
- package/dist/listener.js.map +0 -1
- package/dist/middleware/create-activity-object.d.ts +0 -6
- 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 -2
- package/dist/middleware/expand-activity-stream.js +0 -33
- 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 -19
- package/dist/middleware/store-credentials.js.map +0 -1
- package/dist/middleware/validate.d.ts +0 -2
- package/dist/middleware/validate.js +0 -58
- 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 -10
- package/dist/middleware.js +0 -54
- package/dist/middleware.js.map +0 -1
- package/dist/platform-instance.d.ts +0 -77
- package/dist/platform-instance.js +0 -211
- package/dist/platform-instance.js.map +0 -1
- package/dist/platform.d.ts +0 -6
- package/dist/platform.js +0 -187
- package/dist/platform.js.map +0 -1
- package/dist/process-manager.d.ts +0 -11
- package/dist/process-manager.js +0 -78
- 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 -39
- package/dist/sockethub.js +0 -119
- package/dist/sockethub.js.map +0 -1
- package/dist/store.d.ts +0 -5
- package/dist/store.js +0 -17
- package/dist/store.js.map +0 -1
- package/src/bootstrap/platforms.js +0 -75
- package/src/common.test.ts +0 -54
- package/src/common.ts +0 -14
- package/src/config.d.ts +0 -2
- package/src/crypto.d.ts +0 -5
- package/src/crypto.test.ts +0 -41
- package/src/crypto.ts +0 -41
- package/src/janitor.d.ts +0 -8
- package/src/middleware/validate.d.ts +0 -1
- package/src/middleware.d.ts +0 -21
- package/src/sockethub.d.ts +0 -1
- package/src/store.test.ts +0 -28
- package/src/store.ts +0 -17
- package/test/init-suite.js +0 -41
- package/test/queue.functional.test.js +0 -0
- package/test/sockethub-suite.js +0 -25
- package/tsconfig.json +0 -18
- package/views/examples/dummy.ejs +0 -93
- 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 -191
- package/views/index.ejs +0 -17
package/src/platform.ts
CHANGED
|
@@ -1,191 +1,358 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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";
|
|
9
29
|
|
|
10
30
|
// command-line params
|
|
11
31
|
const parentId = process.argv[2];
|
|
12
32
|
const platformName = process.argv[3];
|
|
13
33
|
let identifier = process.argv[4];
|
|
34
|
+
const redisUrl = process.env.REDIS_URL;
|
|
35
|
+
|
|
14
36
|
const loggerPrefix = `sockethub:platform:${platformName}:${identifier}`;
|
|
15
37
|
let logger = debug(loggerPrefix);
|
|
16
38
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
+
})();
|
|
49
|
+
|
|
50
|
+
let jobWorker: JobWorker;
|
|
51
|
+
let jobWorkerStarted = false;
|
|
52
|
+
let parentSecret1: string;
|
|
53
|
+
let parentSecret2: string;
|
|
20
54
|
|
|
21
|
-
|
|
22
|
-
let parentSecret1: string, parentSecret2: string;
|
|
55
|
+
logger(`platform handler initializing for ${platformName} ${identifier}`);
|
|
23
56
|
|
|
24
|
-
|
|
57
|
+
interface SecretInterface {
|
|
58
|
+
parentSecret1: string;
|
|
59
|
+
parentSecret2: string;
|
|
60
|
+
}
|
|
25
61
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
updateActor(credentials: object): void;
|
|
62
|
+
interface SecretFromParent extends Array<string | SecretInterface> {
|
|
63
|
+
0: string;
|
|
64
|
+
1: SecretInterface;
|
|
30
65
|
}
|
|
31
66
|
|
|
32
67
|
/**
|
|
33
|
-
*
|
|
68
|
+
* Initialize platform module
|
|
34
69
|
*/
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
+
})();
|
|
42
82
|
|
|
43
83
|
/**
|
|
44
|
-
*
|
|
45
|
-
* method to call, the rest are params.
|
|
84
|
+
* Safely send error message to parent process, handling IPC channel closure
|
|
46
85
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
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
|
+
}
|
|
54
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;
|
|
105
|
+
}
|
|
55
106
|
|
|
56
107
|
/**
|
|
57
|
-
*
|
|
108
|
+
* Handle any uncaught errors from the platform by alerting the worker and shutting down.
|
|
58
109
|
*/
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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);
|
|
124
|
+
});
|
|
65
125
|
|
|
66
126
|
/**
|
|
67
|
-
*
|
|
68
|
-
* sessionSecret.
|
|
69
|
-
* @param actorId
|
|
70
|
-
* @param sessionId
|
|
71
|
-
* @param sessionSecret
|
|
72
|
-
* @param cb
|
|
127
|
+
* In the case of a parent disconnect, terminate child process.
|
|
73
128
|
*/
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
// don't continue if we don't get credentials
|
|
80
|
-
if (err) { return cb(err); }
|
|
81
|
-
} else if (! credentials) {
|
|
82
|
-
// also skip if this is a non-persist platform with no credentials
|
|
83
|
-
return cb();
|
|
84
|
-
}
|
|
129
|
+
// Detect parent death via IPC disconnect
|
|
130
|
+
process.on("disconnect", () => {
|
|
131
|
+
console.log(`Parent disconnected. Child ${process.pid} exiting.`);
|
|
132
|
+
process.exit(1);
|
|
133
|
+
});
|
|
85
134
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
135
|
+
/**
|
|
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.
|
|
138
|
+
*/
|
|
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();
|
|
91
146
|
} else {
|
|
92
|
-
|
|
147
|
+
throw new Error("received unknown command from parent thread");
|
|
93
148
|
}
|
|
94
|
-
|
|
95
|
-
});
|
|
96
|
-
}
|
|
149
|
+
});
|
|
97
150
|
|
|
98
151
|
/**
|
|
99
152
|
* Returns a function used to handle completed jobs from the platform code (the `done` callback).
|
|
100
|
-
* @param secret the secret used to decrypt credentials
|
|
101
153
|
*/
|
|
102
|
-
function getJobHandler(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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;
|
|
171
|
+
|
|
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
|
+
}
|
|
126
291
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
jobLog(`completed ${jobData.title} ${jobData.msg.type}`);
|
|
130
|
-
done(null, result);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
if ((Array.isArray(platform.config.requireCredentials)) &&
|
|
134
|
-
(platform.config.requireCredentials.includes(jobData.msg.type))) {
|
|
135
|
-
// add the credentials object if this method requires it
|
|
136
|
-
platform[jobData.msg.type](jobData.msg, credentials, doneCallback);
|
|
137
|
-
} else if (platform.config.persist) {
|
|
138
|
-
if (platform.initialized) {
|
|
139
|
-
platform[jobData.msg.type](jobData.msg, doneCallback);
|
|
140
|
-
} else {
|
|
141
|
-
done(new Error(`${jobData.msg.type} called on uninitialized platform`));
|
|
142
|
-
}
|
|
143
|
-
} else {
|
|
144
|
-
platform[jobData.msg.type](jobData.msg, doneCallback);
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
};
|
|
292
|
+
});
|
|
293
|
+
};
|
|
148
294
|
}
|
|
149
295
|
|
|
150
296
|
/**
|
|
151
|
-
* Get
|
|
297
|
+
* Get a function which sends a message to the parent thread (PlatformInstance). The platform
|
|
152
298
|
* can call that function to send messages back to the client.
|
|
153
299
|
* @param command string containing the type of command to be sent. 'message' or 'close'
|
|
154
300
|
*/
|
|
155
301
|
function getSendFunction(command: string) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
+
};
|
|
159
311
|
}
|
|
160
312
|
|
|
161
313
|
/**
|
|
162
|
-
* When a user changes
|
|
314
|
+
* When a user changes its actor name, the channel identifier changes, we need to ensure that
|
|
163
315
|
* both the queue thread (listening on the channel for jobs) and the logging object are updated.
|
|
164
316
|
* @param credentials
|
|
165
317
|
*/
|
|
166
|
-
function updateActor(credentials) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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);
|
|
174
333
|
}
|
|
175
334
|
|
|
176
335
|
/**
|
|
177
|
-
*
|
|
178
|
-
* @param refresh boolean if the param is true, we re-init the queue.process
|
|
336
|
+
* Starts listening on the queue for incoming jobs
|
|
337
|
+
* @param refresh boolean if the param is true, we re-init the `queue.process`
|
|
179
338
|
* (used when identifier changes)
|
|
180
339
|
*/
|
|
181
|
-
function startQueueListener(refresh
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
340
|
+
async function startQueueListener(refresh = false) {
|
|
341
|
+
if (jobWorkerStarted) {
|
|
342
|
+
if (refresh) {
|
|
343
|
+
await jobWorker.shutdown();
|
|
344
|
+
} else {
|
|
345
|
+
logger("start queue called multiple times, skipping");
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
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;
|
|
191
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;
|