-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcatalog_subprocess.js
More file actions
72 lines (66 loc) · 2.07 KB
/
Copy pathcatalog_subprocess.js
File metadata and controls
72 lines (66 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const messageQueue = require('./lib/message_queue');
const catalogDomain = require('./catalog_domain');
const { createCatalogAuthRuntime } = require('./catalog_auth_runtime');
const ALLOWED_METHODS = new Set([
'emitCatalogSnapshot',
'resetCatalogSnapshot',
'rehydrateCatalogRuntimeFromProjection',
'ensureCatalogRuntimeLoaded',
]);
const catalogAuthRuntime = createCatalogAuthRuntime();
function safeSend(payload = {}) {
if (typeof process.send !== 'function' || process.connected === false) return;
try {
process.send(payload);
} catch (_) {}
}
async function handleCatalogWorkerRequest(message) {
const requestId = String(message?.requestId || '').trim();
const method = String(message?.method || '').trim();
const args = Array.isArray(message?.args) ? message.args : [];
if (!requestId || !ALLOWED_METHODS.has(method)) {
safeSend({
type: 'catalog_worker_response',
requestId,
ok: false,
error: `unsupported catalog worker method: ${method || 'unknown'}`,
});
return;
}
try {
const result = await catalogDomain[method](...args);
safeSend({
type: 'catalog_worker_response',
requestId,
ok: true,
result,
});
} catch (error) {
safeSend({
type: 'catalog_worker_response',
requestId,
ok: false,
error: String(error?.message || error || `${method} failed`),
});
}
}
process.on('message', (message) => {
if (messageQueue.handleChildProcessMessage(process, message)) return;
if (!message || typeof message !== 'object') return;
if (message.type === 'auth.snapshot' || message.type === 'auth.logged_in' || message.type === 'auth.logged_out') {
catalogAuthRuntime.applySnapshot(message.snapshot || {}, {
source: String(message?.source || message.type || 'auth_snapshot'),
});
return;
}
if (String(message.type || '') !== 'catalog_worker_request') return;
void handleCatalogWorkerRequest(message);
});
safeSend({
type: 'auth.query',
source: 'catalog_worker_startup',
});
safeSend({
type: 'catalog_worker_ready',
pid: Number(process.pid || 0),
});