@powerhousedao/reactor-workflow 6.2.3-dev.44 → 6.2.3-dev.45
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 +17 -3
- package/dist/index.d.ts +67 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +809 -331
- package/dist/index.js.map +1 -1
- package/dist/{piece-registry-DMhdxKrs.js → piece-registry-Cv8Zw8ex.js} +92 -118
- package/dist/piece-registry-Cv8Zw8ex.js.map +1 -0
- package/dist/{piece-registry-ChjdczOf.d.ts → piece-registry-wQBhKJUl.d.ts} +59 -44
- package/dist/piece-registry-wQBhKJUl.d.ts.map +1 -0
- package/dist/{redact-BlKAG3f0.js → reactor-rpc-BqAjCK1p.js} +64 -17
- package/dist/reactor-rpc-BqAjCK1p.js.map +1 -0
- package/dist/testing.d.ts +2 -2
- package/dist/testing.js +2 -2
- package/dist/worker-entry.js +375 -270
- package/dist/worker-entry.js.map +1 -1
- package/package.json +6 -6
- package/dist/piece-registry-ChjdczOf.d.ts.map +0 -1
- package/dist/piece-registry-DMhdxKrs.js.map +0 -1
- package/dist/redact-BlKAG3f0.js.map +0 -1
package/dist/worker-entry.js
CHANGED
|
@@ -1,256 +1,9 @@
|
|
|
1
|
-
import { A as
|
|
1
|
+
import { A as assertWithinLimit, B as UnsupportedPieceFeatureError, C as buildTriggerContext, D as StagedFilesService, E as DataUriFilesService, F as InMemoryKeyValueStore, G as throwingStub, H as unsupportedAuth, I as buildActionContext, J as getTriggers, K as withTouchTracking, L as jsonSafe, M as maxFileBytes, N as setMaxFileBytes, P as InMemoryConnectionsProvider, R as buildDescriptor, S as STORE_PUT, T as runTriggerHook, U as unsupportedTrigger, V as authMethodFor, W as UnsupportedContextMemberError, b as STORE_DELETE, d as installEgressGuard, g as MODEL_ENTRIES, h as LOG_WRITE, m as runWithEgressPolicy, n as isReactorRpcEnvelope, o as redactError, q as getActions, s as redactMessage, v as OUTPUT_UPDATE, x as STORE_GET, y as REACTOR_RPC, z as describeProperties } from "./reactor-rpc-BqAjCK1p.js";
|
|
2
2
|
import { n as loadPieceFromDir, t as loadPiece } from "./loader-Gkx6I_Bo.js";
|
|
3
|
+
import { DocumentModelUnavailableError, ReactorAccessDeniedError, ReactorActionsFailedError, ReactorJobFailedError, ReactorJobPendingError, ReactorRequestClosedError } from "@powerhousedao/pieces-framework";
|
|
3
4
|
import { readFile } from "node:fs/promises";
|
|
4
5
|
import { format } from "node:util";
|
|
5
6
|
import { arrayZipperProcessor, formatPieceError, processors, validateProperty } from "@powerhousedao/pieces-framework/host";
|
|
6
|
-
//#region src/pieces/activepieces/worker/host-call.ts
|
|
7
|
-
const DEFAULT_HOST_CALL_TIMEOUT_MS = 1e4;
|
|
8
|
-
let fromWire;
|
|
9
|
-
function setHostCallTimeout(timeoutMs) {
|
|
10
|
-
fromWire = timeoutMs;
|
|
11
|
-
}
|
|
12
|
-
function hostCallTimeoutMs() {
|
|
13
|
-
return fromWire ?? DEFAULT_HOST_CALL_TIMEOUT_MS;
|
|
14
|
-
}
|
|
15
|
-
const pending = /* @__PURE__ */ new Map();
|
|
16
|
-
let nextId = 1;
|
|
17
|
-
let listening = false;
|
|
18
|
-
var HostCallError = class extends Error {
|
|
19
|
-
constructor(method, detail) {
|
|
20
|
-
super(`Host call "${method}" failed: ${detail}`);
|
|
21
|
-
this.name = "HostCallError";
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
var HostCallTimeoutError = class extends Error {
|
|
25
|
-
constructor(method, timeoutMs) {
|
|
26
|
-
super(`Host call "${method}" got no answer within ${timeoutMs}ms`);
|
|
27
|
-
this.name = "HostCallTimeoutError";
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
function isHostCallResponse(value) {
|
|
31
|
-
return typeof value === "object" && value !== null && value.type === "host-result";
|
|
32
|
-
}
|
|
33
|
-
function ensureListening() {
|
|
34
|
-
if (listening) return;
|
|
35
|
-
listening = true;
|
|
36
|
-
process.on("message", (message) => {
|
|
37
|
-
if (!isHostCallResponse(message)) return;
|
|
38
|
-
const entry = pending.get(message.id);
|
|
39
|
-
if (!entry) return;
|
|
40
|
-
pending.delete(message.id);
|
|
41
|
-
clearTimeout(entry.timer);
|
|
42
|
-
if (message.error !== void 0) {
|
|
43
|
-
entry.reject(new HostCallError(entry.method, message.error));
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
entry.resolve(message.value);
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
function callHost(method, payload, timeoutMs = hostCallTimeoutMs()) {
|
|
50
|
-
if (!process.send) return Promise.reject(new HostCallError(method, "the worker has no channel to its host"));
|
|
51
|
-
ensureListening();
|
|
52
|
-
const id = nextId++;
|
|
53
|
-
return new Promise((resolve, reject) => {
|
|
54
|
-
const timer = setTimeout(() => {
|
|
55
|
-
pending.delete(id);
|
|
56
|
-
reject(new HostCallTimeoutError(method, timeoutMs));
|
|
57
|
-
}, timeoutMs);
|
|
58
|
-
pending.set(id, {
|
|
59
|
-
method,
|
|
60
|
-
resolve,
|
|
61
|
-
reject,
|
|
62
|
-
timer
|
|
63
|
-
});
|
|
64
|
-
process.send?.({
|
|
65
|
-
id,
|
|
66
|
-
type: "host-call",
|
|
67
|
-
method,
|
|
68
|
-
payload
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
function notifyHost(method, payload) {
|
|
73
|
-
try {
|
|
74
|
-
process.send?.({
|
|
75
|
-
type: "host-notify",
|
|
76
|
-
method,
|
|
77
|
-
payload
|
|
78
|
-
});
|
|
79
|
-
} catch {}
|
|
80
|
-
}
|
|
81
|
-
//#endregion
|
|
82
|
-
//#region src/pieces/activepieces/context/reactor.ts
|
|
83
|
-
const MAX_POLL_WAIT_MS = 4e3;
|
|
84
|
-
const MIN_DEADLINE_MARGIN_MS = 250;
|
|
85
|
-
const MAX_DEADLINE_MARGIN_MS = 2e3;
|
|
86
|
-
function deadlineMargin(budgetMs) {
|
|
87
|
-
return Math.min(Math.max(Math.floor(budgetMs / 10), MIN_DEADLINE_MARGIN_MS), MAX_DEADLINE_MARGIN_MS);
|
|
88
|
-
}
|
|
89
|
-
var ReactorJobPendingError = class extends Error {
|
|
90
|
-
jobId;
|
|
91
|
-
status;
|
|
92
|
-
constructor(jobId, status) {
|
|
93
|
-
super(status === "UNKNOWN" ? `Reactor job ${jobId} is unknown to the reactor, as after a restart; its actions may or may not have been written` : `Reactor job ${jobId} was still ${status} at the step deadline; its actions may yet be written`);
|
|
94
|
-
this.name = "ReactorJobPendingError";
|
|
95
|
-
this.jobId = jobId;
|
|
96
|
-
this.status = status;
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
var ReactorSubmitUnconfirmedError = class extends Error {
|
|
100
|
-
constructor(what) {
|
|
101
|
-
super(`Submitting ${what} to the reactor got no answer before the step deadline; it may have been submitted`);
|
|
102
|
-
this.name = "ReactorSubmitUnconfirmedError";
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
var ReactorBudgetExhaustedError = class extends Error {
|
|
106
|
-
constructor(what) {
|
|
107
|
-
super(`No time was left before the step deadline to submit ${what}; nothing was submitted`);
|
|
108
|
-
this.name = "ReactorBudgetExhaustedError";
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
var ReactorStateUnreadError = class extends Error {
|
|
112
|
-
jobId;
|
|
113
|
-
constructor(jobId) {
|
|
114
|
-
super(`Reactor job ${jobId} applied its actions, but the document was not read back before the step deadline`);
|
|
115
|
-
this.name = "ReactorStateUnreadError";
|
|
116
|
-
this.jobId = jobId;
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
var ReactorJobFailedError = class extends Error {
|
|
120
|
-
jobId;
|
|
121
|
-
constructor(jobId, detail) {
|
|
122
|
-
super(`Reactor job ${jobId} failed: ${detail ?? "unknown error"}`);
|
|
123
|
-
this.name = "ReactorJobFailedError";
|
|
124
|
-
this.jobId = jobId;
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
function assertActionsApplied(state, submission, actions) {
|
|
128
|
-
if (!state.actions) throw new Error(`Reactor job ${state.jobId} reported no outcome for its ${submission.actionIds.length} action(s)`);
|
|
129
|
-
const outcomes = new Map(state.actions.map((outcome) => [outcome.actionId, outcome]));
|
|
130
|
-
const failed = submission.actionIds.flatMap((actionId, index) => {
|
|
131
|
-
const type = actions[index]?.type ?? actionId;
|
|
132
|
-
const outcome = outcomes.get(actionId);
|
|
133
|
-
if (!outcome) return [`Action ${type} produced no operation`];
|
|
134
|
-
if (outcome.kind === "reducer-error") return [`Action ${type} failed: ${outcome.message ?? "unknown error"}`];
|
|
135
|
-
if (outcome.kind === "denied") return [`Action ${type} was denied: ${outcome.reason ?? "no reason"}`];
|
|
136
|
-
return [];
|
|
137
|
-
});
|
|
138
|
-
if (failed.length > 0) throw new Error(failed.join("; "));
|
|
139
|
-
}
|
|
140
|
-
function jobKey(stepName) {
|
|
141
|
-
return `reactor.job/${stepName ?? "step"}`;
|
|
142
|
-
}
|
|
143
|
-
var RemoteReactorService = class {
|
|
144
|
-
options;
|
|
145
|
-
stopAt;
|
|
146
|
-
margin;
|
|
147
|
-
constructor(options = {}) {
|
|
148
|
-
this.options = options;
|
|
149
|
-
const { deadline } = options;
|
|
150
|
-
this.margin = deadline === void 0 ? 0 : deadlineMargin(deadline - Date.now());
|
|
151
|
-
this.stopAt = deadline === void 0 ? Number.POSITIVE_INFINITY : deadline - this.margin;
|
|
152
|
-
}
|
|
153
|
-
waitCallMs(maxWaitMs) {
|
|
154
|
-
if (this.options.deadline === void 0) return hostCallTimeoutMs();
|
|
155
|
-
return Math.min(hostCallTimeoutMs(), maxWaitMs + this.margin / 4);
|
|
156
|
-
}
|
|
157
|
-
afterJobMs() {
|
|
158
|
-
return Math.min(hostCallTimeoutMs(), Math.max(this.stopAt - Date.now(), this.margin / 2));
|
|
159
|
-
}
|
|
160
|
-
models() {
|
|
161
|
-
return callHost(REACTOR_MODELS, {});
|
|
162
|
-
}
|
|
163
|
-
model(documentType) {
|
|
164
|
-
return callHost(REACTOR_MODEL, { documentType });
|
|
165
|
-
}
|
|
166
|
-
get(input) {
|
|
167
|
-
return callHost(REACTOR_GET, input);
|
|
168
|
-
}
|
|
169
|
-
find(input) {
|
|
170
|
-
return callHost(REACTOR_FIND, input);
|
|
171
|
-
}
|
|
172
|
-
async create(input) {
|
|
173
|
-
const submission = await this.submitWithin(REACTOR_SUBMIT_CREATE, input, `a ${input.documentType} document`);
|
|
174
|
-
const { documentId, jobIds } = submission;
|
|
175
|
-
this.record({
|
|
176
|
-
documentId,
|
|
177
|
-
jobIds
|
|
178
|
-
});
|
|
179
|
-
let landed;
|
|
180
|
-
try {
|
|
181
|
-
for (const jobId of jobIds) {
|
|
182
|
-
await this.settle(jobId);
|
|
183
|
-
landed = jobId;
|
|
184
|
-
}
|
|
185
|
-
for (const followUp of submission.followUps) landed = await this.write(followUp);
|
|
186
|
-
return await this.readBack(documentId, void 0, landed ?? documentId);
|
|
187
|
-
} catch (error) {
|
|
188
|
-
if (landed && error instanceof Error) error.message = `Document ${documentId} was created, but: ${error.message}`;
|
|
189
|
-
throw error;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
async execute(input) {
|
|
193
|
-
const jobId = await this.write(input, true);
|
|
194
|
-
return this.readBack(input.documentId, input.branch, jobId);
|
|
195
|
-
}
|
|
196
|
-
async write(input, recorded = false) {
|
|
197
|
-
const submission = await this.submitWithin(REACTOR_SUBMIT, input, `${input.actions.length} action(s)`);
|
|
198
|
-
if (recorded) this.record(submission);
|
|
199
|
-
assertActionsApplied(await this.settle(submission.jobId), submission, input.actions);
|
|
200
|
-
return submission.jobId;
|
|
201
|
-
}
|
|
202
|
-
async readBack(documentId, branch, jobId) {
|
|
203
|
-
try {
|
|
204
|
-
return await callHost(REACTOR_GET, {
|
|
205
|
-
documentId,
|
|
206
|
-
...branch ? { branch } : {}
|
|
207
|
-
}, this.afterJobMs());
|
|
208
|
-
} catch (error) {
|
|
209
|
-
if (error instanceof HostCallTimeoutError) throw new ReactorStateUnreadError(jobId);
|
|
210
|
-
throw error;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
async submitWithin(method, input, what) {
|
|
214
|
-
const remaining = this.stopAt - Date.now();
|
|
215
|
-
if (remaining <= 0) throw new ReactorBudgetExhaustedError(what);
|
|
216
|
-
try {
|
|
217
|
-
return await callHost(method, input, Math.min(hostCallTimeoutMs(), remaining));
|
|
218
|
-
} catch (error) {
|
|
219
|
-
if (error instanceof HostCallTimeoutError) throw new ReactorSubmitUnconfirmedError(what);
|
|
220
|
-
throw error;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
async record(value) {
|
|
224
|
-
const { store, stepName } = this.options;
|
|
225
|
-
if (!store) return;
|
|
226
|
-
try {
|
|
227
|
-
await store.put(jobKey(stepName), value, "FLOW", this.afterJobMs());
|
|
228
|
-
} catch {}
|
|
229
|
-
}
|
|
230
|
-
async settle(jobId) {
|
|
231
|
-
let status = "PENDING";
|
|
232
|
-
for (;;) {
|
|
233
|
-
const remaining = this.stopAt - Date.now();
|
|
234
|
-
if (remaining <= 0) throw new ReactorJobPendingError(jobId, status);
|
|
235
|
-
const maxWaitMs = Math.min(MAX_POLL_WAIT_MS, Math.floor(hostCallTimeoutMs() / 2), remaining);
|
|
236
|
-
let state;
|
|
237
|
-
try {
|
|
238
|
-
state = await callHost(REACTOR_WAIT, {
|
|
239
|
-
jobId,
|
|
240
|
-
maxWaitMs
|
|
241
|
-
}, this.waitCallMs(maxWaitMs));
|
|
242
|
-
} catch (error) {
|
|
243
|
-
if (error instanceof HostCallTimeoutError) throw new ReactorJobPendingError(jobId, status);
|
|
244
|
-
throw error;
|
|
245
|
-
}
|
|
246
|
-
if (state.status === "READ_READY") return state;
|
|
247
|
-
if (state.status === "FAILED") throw new ReactorJobFailedError(jobId, state.error);
|
|
248
|
-
if (state.status === "UNKNOWN") throw new ReactorJobPendingError(jobId, "UNKNOWN");
|
|
249
|
-
status = state.status;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
//#endregion
|
|
254
7
|
//#region src/pieces/activepieces/context/check.ts
|
|
255
8
|
function buildCheckConnectionContext(options = {}) {
|
|
256
9
|
const touched = /* @__PURE__ */ new Set();
|
|
@@ -550,6 +303,82 @@ async function resolveDynamicProperty(params) {
|
|
|
550
303
|
return await resolver(params.refresherValues ?? {}, params.context);
|
|
551
304
|
}
|
|
552
305
|
//#endregion
|
|
306
|
+
//#region src/pieces/activepieces/worker/host-call.ts
|
|
307
|
+
const DEFAULT_HOST_CALL_TIMEOUT_MS = 1e4;
|
|
308
|
+
let fromWire;
|
|
309
|
+
function setHostCallTimeout(timeoutMs) {
|
|
310
|
+
fromWire = timeoutMs;
|
|
311
|
+
}
|
|
312
|
+
function hostCallTimeoutMs() {
|
|
313
|
+
return fromWire ?? DEFAULT_HOST_CALL_TIMEOUT_MS;
|
|
314
|
+
}
|
|
315
|
+
const pending = /* @__PURE__ */ new Map();
|
|
316
|
+
let nextId = 1;
|
|
317
|
+
let listening = false;
|
|
318
|
+
var HostCallError = class extends Error {
|
|
319
|
+
constructor(method, detail) {
|
|
320
|
+
super(`Host call "${method}" failed: ${detail}`);
|
|
321
|
+
this.name = "HostCallError";
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
var HostCallTimeoutError = class extends Error {
|
|
325
|
+
constructor(method, timeoutMs) {
|
|
326
|
+
super(`Host call "${method}" got no answer within ${timeoutMs}ms`);
|
|
327
|
+
this.name = "HostCallTimeoutError";
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
function isHostCallResponse(value) {
|
|
331
|
+
return typeof value === "object" && value !== null && value.type === "host-result";
|
|
332
|
+
}
|
|
333
|
+
function ensureListening() {
|
|
334
|
+
if (listening) return;
|
|
335
|
+
listening = true;
|
|
336
|
+
process.on("message", (message) => {
|
|
337
|
+
if (!isHostCallResponse(message)) return;
|
|
338
|
+
const entry = pending.get(message.id);
|
|
339
|
+
if (!entry) return;
|
|
340
|
+
pending.delete(message.id);
|
|
341
|
+
clearTimeout(entry.timer);
|
|
342
|
+
if (message.error !== void 0) {
|
|
343
|
+
entry.reject(new HostCallError(entry.method, message.error));
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
entry.resolve(message.value);
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
function callHost(method, payload, timeoutMs = hostCallTimeoutMs()) {
|
|
350
|
+
if (!process.send) return Promise.reject(new HostCallError(method, "the worker has no channel to its host"));
|
|
351
|
+
ensureListening();
|
|
352
|
+
const id = nextId++;
|
|
353
|
+
return new Promise((resolve, reject) => {
|
|
354
|
+
const timer = setTimeout(() => {
|
|
355
|
+
pending.delete(id);
|
|
356
|
+
reject(new HostCallTimeoutError(method, timeoutMs));
|
|
357
|
+
}, timeoutMs);
|
|
358
|
+
pending.set(id, {
|
|
359
|
+
method,
|
|
360
|
+
resolve,
|
|
361
|
+
reject,
|
|
362
|
+
timer
|
|
363
|
+
});
|
|
364
|
+
process.send?.({
|
|
365
|
+
id,
|
|
366
|
+
type: "host-call",
|
|
367
|
+
method,
|
|
368
|
+
payload
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
function notifyHost(method, payload) {
|
|
373
|
+
try {
|
|
374
|
+
process.send?.({
|
|
375
|
+
type: "host-notify",
|
|
376
|
+
method,
|
|
377
|
+
payload
|
|
378
|
+
});
|
|
379
|
+
} catch {}
|
|
380
|
+
}
|
|
381
|
+
//#endregion
|
|
553
382
|
//#region src/pieces/activepieces/context/remote-store.ts
|
|
554
383
|
var RemoteKeyValueStore = class {
|
|
555
384
|
async put(key, value, scope, timeoutMs) {
|
|
@@ -619,8 +448,18 @@ function captureConsole() {
|
|
|
619
448
|
};
|
|
620
449
|
}
|
|
621
450
|
//#endregion
|
|
451
|
+
//#region src/pieces/activepieces/worker/reactor-provider.ts
|
|
452
|
+
let provider;
|
|
453
|
+
function installReactorProvider(next) {
|
|
454
|
+
provider = next;
|
|
455
|
+
}
|
|
456
|
+
function openWorkerReactor(binding, declared) {
|
|
457
|
+
if (!binding || !provider) return Promise.resolve(void 0);
|
|
458
|
+
if (declared !== "read" && declared !== "write") return Promise.resolve(void 0);
|
|
459
|
+
return provider(binding, declared);
|
|
460
|
+
}
|
|
461
|
+
//#endregion
|
|
622
462
|
//#region src/pieces/activepieces/worker/entry.ts
|
|
623
|
-
installEgressGuard();
|
|
624
463
|
const loadedPieces = /* @__PURE__ */ new Map();
|
|
625
464
|
const stores = /* @__PURE__ */ new Map();
|
|
626
465
|
function storeForScope(scope) {
|
|
@@ -648,12 +487,26 @@ function loadCached(ref) {
|
|
|
648
487
|
function redactValuesOf(message) {
|
|
649
488
|
return message.request.redactValues ?? [];
|
|
650
489
|
}
|
|
490
|
+
const REACTOR_ERROR_NAMES = /* @__PURE__ */ new Set([
|
|
491
|
+
DocumentModelUnavailableError,
|
|
492
|
+
ReactorAccessDeniedError,
|
|
493
|
+
ReactorActionsFailedError,
|
|
494
|
+
ReactorJobFailedError,
|
|
495
|
+
ReactorJobPendingError,
|
|
496
|
+
ReactorRequestClosedError
|
|
497
|
+
]);
|
|
498
|
+
function errorName(error, fallback) {
|
|
499
|
+
if (typeof error !== "object" || error === null) return fallback || "Error";
|
|
500
|
+
const named = error.name;
|
|
501
|
+
if (typeof named === "string" && REACTOR_ERROR_NAMES.has(named)) return named;
|
|
502
|
+
return error.constructor.name || fallback || "Error";
|
|
503
|
+
}
|
|
651
504
|
function serializeError(error, values = []) {
|
|
652
505
|
const properties = {};
|
|
653
506
|
if (typeof error === "object" && error !== null) for (const key of Object.keys(error)) properties[key] = jsonSafe(error[key]);
|
|
654
|
-
const { __apErrorVersion, message, errorName, ...http } = formatPieceError(error);
|
|
507
|
+
const { __apErrorVersion, message, errorName: formattedName, ...http } = formatPieceError(error);
|
|
655
508
|
return {
|
|
656
|
-
name:
|
|
509
|
+
name: errorName(error, formattedName),
|
|
657
510
|
message: redactMessage(message, { values }),
|
|
658
511
|
properties: redactError({
|
|
659
512
|
...properties,
|
|
@@ -669,14 +522,34 @@ function consumeTlsFlag() {
|
|
|
669
522
|
delete process.env.NODE_TLS_REJECT_UNAUTHORIZED;
|
|
670
523
|
return poisoned;
|
|
671
524
|
}
|
|
525
|
+
function reactorOption(session) {
|
|
526
|
+
if (!session) return {};
|
|
527
|
+
return session.requireReactor === "write" ? {
|
|
528
|
+
requireReactor: "write",
|
|
529
|
+
reactor: session.client
|
|
530
|
+
} : {
|
|
531
|
+
requireReactor: "read",
|
|
532
|
+
reactor: session.client
|
|
533
|
+
};
|
|
534
|
+
}
|
|
672
535
|
async function handleResolveOptions(message) {
|
|
673
536
|
const { request } = message;
|
|
674
537
|
const { piece } = await loadCached(request);
|
|
538
|
+
const block = request.kind === "trigger" ? getTriggers(piece)[request.actionName] : getActions(piece)[request.actionName];
|
|
539
|
+
const session = await openWorkerReactor(request.reactor, block?.requireReactor);
|
|
540
|
+
try {
|
|
541
|
+
return await resolveOptions(message, piece, session);
|
|
542
|
+
} finally {
|
|
543
|
+
session?.close();
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
async function resolveOptions(message, piece, session) {
|
|
547
|
+
const { request } = message;
|
|
675
548
|
const { context, touched } = buildPropertyContext({
|
|
676
549
|
searchValue: request.searchValue,
|
|
677
550
|
projectId: request.projectId,
|
|
678
551
|
flows: { list: () => Promise.resolve({ data: [] }) },
|
|
679
|
-
...
|
|
552
|
+
...session ? { reactor: session.client } : {}
|
|
680
553
|
});
|
|
681
554
|
const refresherValues = {
|
|
682
555
|
...request.auth !== void 0 ? { auth: request.auth } : {},
|
|
@@ -731,11 +604,21 @@ async function handleRun(message) {
|
|
|
731
604
|
const files = request.stagingDir ? new StagedFilesService(request.stagingDir) : new DataUriFilesService();
|
|
732
605
|
const durableStore = request.durableStore ? new RemoteKeyValueStore() : void 0;
|
|
733
606
|
const liveOutput = request.liveOutput ? new RemoteOutput() : void 0;
|
|
734
|
-
const
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
607
|
+
const session = await openWorkerReactor(request.reactor, action.requireReactor);
|
|
608
|
+
try {
|
|
609
|
+
return await runAction(message, action, {
|
|
610
|
+
files,
|
|
611
|
+
durableStore,
|
|
612
|
+
liveOutput,
|
|
613
|
+
reactor: reactorOption(session)
|
|
614
|
+
});
|
|
615
|
+
} finally {
|
|
616
|
+
session?.close();
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
async function runAction(message, action, services) {
|
|
620
|
+
const { request } = message;
|
|
621
|
+
const { files, durableStore, liveOutput, reactor } = services;
|
|
739
622
|
const restoreConsole = request.captureLogs ? captureConsole() : void 0;
|
|
740
623
|
const { context, touched } = buildActionContext({
|
|
741
624
|
propsValue: await preparePropsValue(`action "${request.actionName}"`, action.props, request.propsValue, { resolveRef: stagedInputResolver(request.stagedInputs) }),
|
|
@@ -744,7 +627,7 @@ async function handleRun(message) {
|
|
|
744
627
|
files,
|
|
745
628
|
connections: request.connections ? new InMemoryConnectionsProvider(request.connections) : void 0,
|
|
746
629
|
output: liveOutput,
|
|
747
|
-
reactor,
|
|
630
|
+
...reactor,
|
|
748
631
|
resumePayload: request.resumePayload,
|
|
749
632
|
executionType: request.executionType,
|
|
750
633
|
identity: request.identity
|
|
@@ -775,18 +658,13 @@ async function handleTriggerHook(message) {
|
|
|
775
658
|
trigger
|
|
776
659
|
});
|
|
777
660
|
const snapshot = request.durableStore ? void 0 : new InMemoryKeyValueStore(request.storeState);
|
|
778
|
-
const durableStore = snapshot ? void 0 : new RemoteKeyValueStore();
|
|
779
|
-
const reactor = request.reactorAccess ? new RemoteReactorService({
|
|
780
|
-
deadline: request.deadline,
|
|
781
|
-
store: durableStore,
|
|
782
|
-
stepName: request.identity?.stepName
|
|
783
|
-
}) : void 0;
|
|
784
661
|
const runsPiece = request.hook === "run" || request.hook === "test";
|
|
785
662
|
const propsValue = request.hook === "onDisable" ? await normalizePropsValue(trigger.props, request.propsValue) : await preparePropsValue(`trigger "${request.triggerName}"`, trigger.props, request.propsValue);
|
|
663
|
+
const session = await openWorkerReactor(request.reactor, trigger.requireReactor);
|
|
786
664
|
const handle = buildTriggerContext({
|
|
787
665
|
propsValue,
|
|
788
666
|
auth: request.auth,
|
|
789
|
-
store: snapshot ??
|
|
667
|
+
store: snapshot ?? new RemoteKeyValueStore(),
|
|
790
668
|
hostPartitionedStore: request.durableStore,
|
|
791
669
|
storePrefix: request.hook === "test" ? "test" : "",
|
|
792
670
|
identity: request.identity,
|
|
@@ -795,9 +673,14 @@ async function handleTriggerHook(message) {
|
|
|
795
673
|
webhookUrl: request.webhookUrl,
|
|
796
674
|
server: request.server,
|
|
797
675
|
files: runsPiece ? new DataUriFilesService() : void 0,
|
|
798
|
-
|
|
676
|
+
...reactorOption(session)
|
|
799
677
|
});
|
|
800
|
-
|
|
678
|
+
let output;
|
|
679
|
+
try {
|
|
680
|
+
output = await runTriggerHook(trigger, request.hook, handle);
|
|
681
|
+
} finally {
|
|
682
|
+
session?.close();
|
|
683
|
+
}
|
|
801
684
|
return {
|
|
802
685
|
id: message.id,
|
|
803
686
|
type: "result",
|
|
@@ -895,7 +778,11 @@ function dispatch(message) {
|
|
|
895
778
|
case "describe": return handleDescribe(message);
|
|
896
779
|
}
|
|
897
780
|
}
|
|
898
|
-
|
|
781
|
+
function startPieceWorker() {
|
|
782
|
+
installEgressGuard();
|
|
783
|
+
process.on("message", onMessage);
|
|
784
|
+
}
|
|
785
|
+
function onMessage(message) {
|
|
899
786
|
if (!isWorkerMessage(message)) return;
|
|
900
787
|
Promise.resolve().then(() => {
|
|
901
788
|
setMaxFileBytes(message.request.maxFileBytes);
|
|
@@ -907,7 +794,225 @@ process.on("message", (message) => {
|
|
|
907
794
|
error: serializeError(error, redactValuesOf(message)),
|
|
908
795
|
tlsPoisoned: consumeTlsFlag()
|
|
909
796
|
})).then((response) => process.send?.(response)).catch(() => process.exit(1));
|
|
910
|
-
}
|
|
797
|
+
}
|
|
798
|
+
//#endregion
|
|
799
|
+
//#region src/worker/reactor.ts
|
|
800
|
+
function named(name, message, cause) {
|
|
801
|
+
const error = new Error(message, cause === void 0 ? {} : { cause });
|
|
802
|
+
error.name = name;
|
|
803
|
+
return error;
|
|
804
|
+
}
|
|
805
|
+
const manifest = /* @__PURE__ */ new Map();
|
|
806
|
+
const loading = /* @__PURE__ */ new Map();
|
|
807
|
+
const lookups = /* @__PURE__ */ new Map();
|
|
808
|
+
function isManifestEntry(value) {
|
|
809
|
+
if (typeof value !== "object" || value === null) return false;
|
|
810
|
+
const entry = value;
|
|
811
|
+
return typeof entry.documentType === "string" && typeof entry.spec === "object" && entry.spec !== null;
|
|
812
|
+
}
|
|
813
|
+
function addManifestEntries(entries) {
|
|
814
|
+
for (const entry of entries) {
|
|
815
|
+
if (!isManifestEntry(entry)) continue;
|
|
816
|
+
const known = manifest.get(entry.documentType) ?? [];
|
|
817
|
+
if (known.some((other) => other.version === entry.version)) continue;
|
|
818
|
+
manifest.set(entry.documentType, [...known, entry]);
|
|
819
|
+
loading.delete(entry.documentType);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
function lookUpEntries(documentType) {
|
|
823
|
+
let pending = lookups.get(documentType);
|
|
824
|
+
if (!pending) {
|
|
825
|
+
pending = callHost(MODEL_ENTRIES, documentType === void 0 ? {} : { documentType }).then((entries) => {
|
|
826
|
+
if (Array.isArray(entries)) addManifestEntries(entries);
|
|
827
|
+
}).finally(() => lookups.delete(documentType));
|
|
828
|
+
lookups.set(documentType, pending);
|
|
829
|
+
}
|
|
830
|
+
return pending;
|
|
831
|
+
}
|
|
832
|
+
function isManifestMessage(value) {
|
|
833
|
+
return typeof value === "object" && value !== null && value.type === "model-manifest" && Array.isArray(value.entries);
|
|
834
|
+
}
|
|
835
|
+
let runtime;
|
|
836
|
+
function reactorRuntime() {
|
|
837
|
+
runtime ??= Promise.all([
|
|
838
|
+
import("@powerhousedao/reactor/rpc"),
|
|
839
|
+
import("@powerhousedao/reactor"),
|
|
840
|
+
import("@powerhousedao/shared/document-model")
|
|
841
|
+
]).then(([rpc, reactor, model]) => ({
|
|
842
|
+
rpc,
|
|
843
|
+
loadSpec: reactor.loadDocumentModelSpec,
|
|
844
|
+
registry: new reactor.DocumentModelRegistry(),
|
|
845
|
+
versionOf: (document) => model.normalizeDocumentModelVersion(document.state.document?.version)
|
|
846
|
+
}));
|
|
847
|
+
return runtime;
|
|
848
|
+
}
|
|
849
|
+
function unavailable(documentType, cause) {
|
|
850
|
+
return named(DocumentModelUnavailableError, cause === void 0 ? `No document model for "${documentType}" is available to this piece` : `The document model for "${documentType}" could not be loaded: ${cause instanceof Error ? cause.message : JSON.stringify(cause)}`, cause);
|
|
851
|
+
}
|
|
852
|
+
async function ensureType(rt, documentType, version) {
|
|
853
|
+
const local = manifest.get(documentType) ?? [];
|
|
854
|
+
if (local.length === 0 || version !== void 0 && !local.some((entry) => entry.version === String(version))) try {
|
|
855
|
+
await lookUpEntries(documentType);
|
|
856
|
+
} catch (error) {
|
|
857
|
+
throw unavailable(documentType, error);
|
|
858
|
+
}
|
|
859
|
+
const entries = manifest.get(documentType);
|
|
860
|
+
if (!entries || entries.length === 0) throw unavailable(documentType);
|
|
861
|
+
let pending = loading.get(documentType);
|
|
862
|
+
if (!pending) {
|
|
863
|
+
pending = Promise.all(entries.map((entry) => rt.loadSpec(entry.spec))).then((modules) => {
|
|
864
|
+
rt.registry.registerModules(...modules);
|
|
865
|
+
});
|
|
866
|
+
loading.set(documentType, pending);
|
|
867
|
+
pending.catch(() => {
|
|
868
|
+
if (loading.get(documentType) === pending) loading.delete(documentType);
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
try {
|
|
872
|
+
await pending;
|
|
873
|
+
} catch (error) {
|
|
874
|
+
throw unavailable(documentType, error);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
function moduleOf(documentType, lookup) {
|
|
878
|
+
return lookup().catch((error) => {
|
|
879
|
+
if (error instanceof Error && error.name === "ModuleNotFoundError") throw unavailable(documentType);
|
|
880
|
+
throw error;
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
function processTransport(requestId) {
|
|
884
|
+
let closed = false;
|
|
885
|
+
const detachers = /* @__PURE__ */ new Set();
|
|
886
|
+
return {
|
|
887
|
+
post(message) {
|
|
888
|
+
if (closed) throw requestClosed();
|
|
889
|
+
process.send?.({
|
|
890
|
+
type: REACTOR_RPC,
|
|
891
|
+
requestId,
|
|
892
|
+
message
|
|
893
|
+
});
|
|
894
|
+
},
|
|
895
|
+
onMessage(listener) {
|
|
896
|
+
const handler = (value) => {
|
|
897
|
+
if (closed || !isReactorRpcEnvelope(value, requestId)) return;
|
|
898
|
+
listener(value.message);
|
|
899
|
+
};
|
|
900
|
+
process.on("message", handler);
|
|
901
|
+
const detach = () => {
|
|
902
|
+
process.off("message", handler);
|
|
903
|
+
detachers.delete(detach);
|
|
904
|
+
};
|
|
905
|
+
detachers.add(detach);
|
|
906
|
+
return detach;
|
|
907
|
+
},
|
|
908
|
+
close() {
|
|
909
|
+
closed = true;
|
|
910
|
+
for (const detach of [...detachers]) detach();
|
|
911
|
+
}
|
|
912
|
+
};
|
|
913
|
+
}
|
|
914
|
+
function requestClosed() {
|
|
915
|
+
return named(ReactorRequestClosedError, "This ctx.reactor belongs to a step that has finished; use the ctx.reactor of the running step");
|
|
916
|
+
}
|
|
917
|
+
var RequestGuard = class {
|
|
918
|
+
closed = false;
|
|
919
|
+
rejectClosed = () => void 0;
|
|
920
|
+
whenClosed = new Promise((_, reject) => {
|
|
921
|
+
this.rejectClosed = reject;
|
|
922
|
+
});
|
|
923
|
+
constructor() {
|
|
924
|
+
this.whenClosed.catch(() => void 0);
|
|
925
|
+
}
|
|
926
|
+
call(run) {
|
|
927
|
+
if (this.closed) return Promise.reject(requestClosed());
|
|
928
|
+
let result;
|
|
929
|
+
try {
|
|
930
|
+
result = Promise.resolve(run());
|
|
931
|
+
} catch (error) {
|
|
932
|
+
return Promise.reject(error);
|
|
933
|
+
}
|
|
934
|
+
return Promise.race([result.then((value) => this.paged(value)), this.whenClosed]);
|
|
935
|
+
}
|
|
936
|
+
close() {
|
|
937
|
+
this.closed = true;
|
|
938
|
+
this.rejectClosed(requestClosed());
|
|
939
|
+
}
|
|
940
|
+
paged(value) {
|
|
941
|
+
if (typeof value !== "object" || value === null) return value;
|
|
942
|
+
const next = value.next;
|
|
943
|
+
if (typeof next !== "function") return value;
|
|
944
|
+
return {
|
|
945
|
+
...value,
|
|
946
|
+
next: () => this.call(() => next.call(value))
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
};
|
|
950
|
+
function sessionClient(rt, proxy, guard) {
|
|
951
|
+
const local = {
|
|
952
|
+
getDocumentModelModule: (documentType) => guard.call(async () => {
|
|
953
|
+
const type = String(documentType);
|
|
954
|
+
await ensureType(rt, type);
|
|
955
|
+
return moduleOf(type, () => proxy.getDocumentModelModule(type));
|
|
956
|
+
}),
|
|
957
|
+
getDocumentModelModuleForDocument: (document) => guard.call(async () => {
|
|
958
|
+
const type = document.header.documentType;
|
|
959
|
+
await ensureType(rt, type, rt.versionOf(document));
|
|
960
|
+
return moduleOf(type, () => proxy.getDocumentModelModuleForDocument(document));
|
|
961
|
+
}),
|
|
962
|
+
getDocumentModelModules: (namespace, paging) => guard.call(async () => {
|
|
963
|
+
await lookUpEntries();
|
|
964
|
+
for (const documentType of manifest.keys()) await ensureType(rt, documentType).catch((error) => {
|
|
965
|
+
console.warn(error instanceof Error ? error.message : error);
|
|
966
|
+
});
|
|
967
|
+
return proxy.getDocumentModelModules(namespace, paging);
|
|
968
|
+
}),
|
|
969
|
+
subscribe: () => {
|
|
970
|
+
throw named(ReactorAccessDeniedError, "Reactor method \"subscribe\" is not offered to pieces");
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
return new Proxy({}, { get: (_target, prop) => {
|
|
974
|
+
if (typeof prop !== "string" || prop === "then") return void 0;
|
|
975
|
+
if (prop === "drives") return void 0;
|
|
976
|
+
if (prop in local) return local[prop];
|
|
977
|
+
return (...args) => guard.call(() => proxy[prop](...args));
|
|
978
|
+
} });
|
|
979
|
+
}
|
|
980
|
+
async function openSession(binding, requireReactor) {
|
|
981
|
+
const rt = await reactorRuntime();
|
|
982
|
+
const transport = processTransport(binding.requestId);
|
|
983
|
+
const router = new rt.rpc.MessageRouter("w");
|
|
984
|
+
router.attach(transport);
|
|
985
|
+
const proxy = rt.rpc.createReactorClientProxy(router, { registry: rt.registry });
|
|
986
|
+
const guard = new RequestGuard();
|
|
987
|
+
const client = sessionClient(rt, proxy, guard);
|
|
988
|
+
const close = () => {
|
|
989
|
+
guard.close();
|
|
990
|
+
router.detach();
|
|
991
|
+
transport.close();
|
|
992
|
+
};
|
|
993
|
+
return requireReactor === "write" ? {
|
|
994
|
+
requireReactor,
|
|
995
|
+
client,
|
|
996
|
+
close
|
|
997
|
+
} : {
|
|
998
|
+
requireReactor,
|
|
999
|
+
client,
|
|
1000
|
+
close
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
let installed = false;
|
|
1004
|
+
function installWorkerReactor() {
|
|
1005
|
+
if (installed) return;
|
|
1006
|
+
installed = true;
|
|
1007
|
+
process.on("message", (message) => {
|
|
1008
|
+
if (isManifestMessage(message)) addManifestEntries(message.entries);
|
|
1009
|
+
});
|
|
1010
|
+
installReactorProvider(openSession);
|
|
1011
|
+
}
|
|
1012
|
+
//#endregion
|
|
1013
|
+
//#region src/worker/entry.ts
|
|
1014
|
+
startPieceWorker();
|
|
1015
|
+
installWorkerReactor();
|
|
911
1016
|
//#endregion
|
|
912
1017
|
export {};
|
|
913
1018
|
|