@powerhousedao/switchboard 6.2.3-dev.89 → 6.2.3-dev.90
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/.tsbuild/test/boot-desktop-auth.test.d.ts +2 -0
- package/.tsbuild/test/boot-desktop-auth.test.d.ts.map +1 -0
- package/.tsbuild/test/delegated-publish.test.d.ts +2 -0
- package/.tsbuild/test/delegated-publish.test.d.ts.map +1 -0
- package/.tsbuild/tsconfig.tsbuildinfo +1 -1
- package/CHANGELOG.md +35 -0
- package/dist/boot.d.mts +2 -2
- package/dist/boot.mjs +2 -2
- package/dist/config/from-env.d.mts +1 -1
- package/dist/config/from-env.mjs +3 -3
- package/dist/config/schema.d.mts +1 -1
- package/dist/config/schema.mjs +3 -3
- package/dist/{from-env-DuUwngOC.mjs → from-env-DJ4iQ3QZ.mjs} +5 -5
- package/dist/from-env-DJ4iQ3QZ.mjs.map +1 -0
- package/dist/index.mjs +3 -3
- package/dist/{plugin-host-C0bSz1Oe.d.mts → plugin-host-BHEUSn9G.d.mts} +2 -2
- package/dist/{plugin-host-C0bSz1Oe.d.mts.map → plugin-host-BHEUSn9G.d.mts.map} +1 -1
- package/dist/plugins/ai.d.mts +1 -1
- package/dist/plugins/ai.d.mts.map +1 -1
- package/dist/plugins/ai.mjs +68 -20
- package/dist/plugins/ai.mjs.map +1 -1
- package/dist/plugins/attachments.d.mts +1 -1
- package/dist/plugins/attachments.mjs +3 -3
- package/dist/plugins/mcp.d.mts +1 -1
- package/dist/plugins/privacy.d.mts +1 -1
- package/dist/plugins/workflows.d.mts +3 -3
- package/dist/plugins/workflows.mjs +3 -3
- package/dist/{schema-XEZrdmag.mjs → schema-1yj85yQD.mjs} +15 -5
- package/dist/schema-1yj85yQD.mjs.map +1 -0
- package/dist/{schema-DhempFjb.d.mts → schema-ugrJupVH.d.mts} +5 -1
- package/dist/schema-ugrJupVH.d.mts.map +1 -0
- package/dist/{server-BJk9-9n4.mjs → server-CgDvrUqE.mjs} +129 -30
- package/dist/server-CgDvrUqE.mjs.map +1 -0
- package/dist/server.d.mts +8 -306
- package/dist/server.d.mts.map +1 -1
- package/dist/server.mjs +4 -4
- package/dist/types-DzO3_320.d.mts +308 -0
- package/dist/types-DzO3_320.d.mts.map +1 -0
- package/dist/utils-Cm0TE6dP.mjs +148 -0
- package/dist/utils-Cm0TE6dP.mjs.map +1 -0
- package/dist/utils.d.mts +21 -3
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +4 -4
- package/dist/{worker-pool-BWHBLBJd.mjs → worker-pool-gyxJnCxA.mjs} +4 -4
- package/dist/worker-pool-gyxJnCxA.mjs.map +1 -0
- package/dist/workflow-runtime-CF5pc8mt.d.mts.map +1 -1
- package/dist/{workflows-krankcQT.mjs → workflows-C9BQlyT0.mjs} +31 -7
- package/dist/workflows-C9BQlyT0.mjs.map +1 -0
- package/package.json +29 -19
- package/test/ai-gateway.test.ts +58 -1
- package/test/boot-desktop-auth.test.ts +278 -0
- package/test/boot-preconditions.test.ts +13 -0
- package/test/config-merge.test.ts +11 -0
- package/test/default-drive-protocol-versions.test.ts +1 -24
- package/test/default-drive-slug.test.ts +170 -2
- package/test/delegated-publish.test.ts +435 -0
- package/test/fatal-shutdown.test.ts +40 -0
- package/test/pglite-store.test.ts +179 -2
- package/test/workflow-runtime.test.ts +119 -0
- package/dist/from-env-DuUwngOC.mjs.map +0 -1
- package/dist/schema-DhempFjb.d.mts.map +0 -1
- package/dist/schema-XEZrdmag.mjs.map +0 -1
- package/dist/server-BJk9-9n4.mjs.map +0 -1
- package/dist/utils-C6AIF38Z.mjs +0 -133
- package/dist/utils-C6AIF38Z.mjs.map +0 -1
- package/dist/worker-pool-BWHBLBJd.mjs.map +0 -1
- package/dist/workflows-krankcQT.mjs.map +0 -1
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
// A delegate signing publishes as a person, over a real reactor with real
|
|
2
|
+
// keys and credentials, and everything around it (ADR 0008). The host has no
|
|
3
|
+
// address, as a container with no `ph login` has none.
|
|
4
|
+
import {
|
|
5
|
+
ReactorBuilder,
|
|
6
|
+
ReactorClientBuilder,
|
|
7
|
+
type IReactorClient,
|
|
8
|
+
type InProcessReactorClientModule,
|
|
9
|
+
type SignatureTrustPolicy,
|
|
10
|
+
type SignerConfig,
|
|
11
|
+
} from "@powerhousedao/reactor";
|
|
12
|
+
import { publishRunUser } from "@powerhousedao/reactor-workflow";
|
|
13
|
+
import {
|
|
14
|
+
deserializeSignature,
|
|
15
|
+
serializeSignature,
|
|
16
|
+
type Action,
|
|
17
|
+
type DocumentModelModule,
|
|
18
|
+
type Operation,
|
|
19
|
+
} from "@powerhousedao/shared/document-model";
|
|
20
|
+
import {
|
|
21
|
+
Workflow,
|
|
22
|
+
actions as workflowActions,
|
|
23
|
+
} from "@powerhousedao/workflow/document-models/workflow";
|
|
24
|
+
import {
|
|
25
|
+
buildAndSignCredential,
|
|
26
|
+
createDelegation,
|
|
27
|
+
MemoryKeyStorage,
|
|
28
|
+
RenownCryptoBuilder,
|
|
29
|
+
RenownCryptoSigner,
|
|
30
|
+
signDelegatedAction,
|
|
31
|
+
type IRenown,
|
|
32
|
+
type IRenownCrypto,
|
|
33
|
+
type PowerhouseVerifiableCredential,
|
|
34
|
+
type SignCredentialTypedData,
|
|
35
|
+
} from "@renown/sdk/node";
|
|
36
|
+
import { createRequire } from "node:module";
|
|
37
|
+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
38
|
+
import {
|
|
39
|
+
getDelegationTrustPolicyConfig,
|
|
40
|
+
getRenownTrustPolicyConfig,
|
|
41
|
+
} from "../src/renown.js";
|
|
42
|
+
|
|
43
|
+
// viem is the SDK's own dependency; the switchboard does not declare it.
|
|
44
|
+
type Account = {
|
|
45
|
+
address: `0x${string}`;
|
|
46
|
+
signTypedData(args: unknown): Promise<`0x${string}`>;
|
|
47
|
+
};
|
|
48
|
+
const { privateKeyToAccount } = createRequire(
|
|
49
|
+
import.meta.resolve("@renown/sdk/node"),
|
|
50
|
+
)("viem/accounts") as {
|
|
51
|
+
privateKeyToAccount: (key: `0x${string}`) => Account;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Well-known Anvil dev keys; never used for anything real.
|
|
55
|
+
const PERSON = privateKeyToAccount(
|
|
56
|
+
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
|
|
57
|
+
);
|
|
58
|
+
const USER = { address: PERSON.address, networkId: "eip155", chainId: 1 };
|
|
59
|
+
const SCOPE = { ops: ["PUBLISH_WORKFLOW"], res: "sektar/integration:i-1" };
|
|
60
|
+
|
|
61
|
+
const newCrypto = (): Promise<IRenownCrypto> =>
|
|
62
|
+
new RenownCryptoBuilder().withKeyPairStorage(new MemoryKeyStorage()).build();
|
|
63
|
+
|
|
64
|
+
const publish = () =>
|
|
65
|
+
workflowActions.publishWorkflow({ publishedAt: new Date().toISOString() });
|
|
66
|
+
|
|
67
|
+
type Trust = Pick<SignerConfig, "trustPolicy" | "workerTrustPolicy">;
|
|
68
|
+
|
|
69
|
+
async function reactorOf(
|
|
70
|
+
hostSigner: RenownCryptoSigner,
|
|
71
|
+
trust: Trust,
|
|
72
|
+
featureFlags = {},
|
|
73
|
+
): Promise<InProcessReactorClientModule> {
|
|
74
|
+
return new ReactorClientBuilder()
|
|
75
|
+
.withSigner(hostSigner)
|
|
76
|
+
.withCreateSignaturePolicy("v2-required")
|
|
77
|
+
.withReactorBuilder(
|
|
78
|
+
new ReactorBuilder()
|
|
79
|
+
.withDocumentModelSources([Workflow as unknown as DocumentModelModule])
|
|
80
|
+
.withExecutorConfig({ featureFlags })
|
|
81
|
+
.withTrustPolicy(trust.trustPolicy as SignatureTrustPolicy),
|
|
82
|
+
)
|
|
83
|
+
.buildModule();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function clientOver(
|
|
87
|
+
over: InProcessReactorClientModule,
|
|
88
|
+
signer?: RenownCryptoSigner,
|
|
89
|
+
): Promise<IReactorClient> {
|
|
90
|
+
const builder = new ReactorClientBuilder().withReactor(
|
|
91
|
+
over.reactor,
|
|
92
|
+
over.eventBus,
|
|
93
|
+
over.documentIndexer,
|
|
94
|
+
over.documentView,
|
|
95
|
+
);
|
|
96
|
+
return (signer ? builder.withSigner(signer) : builder).build();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
describe("delegated publishes", () => {
|
|
100
|
+
let session: IRenownCrypto;
|
|
101
|
+
let delegate: IRenownCrypto;
|
|
102
|
+
let hostCrypto: IRenownCrypto;
|
|
103
|
+
let hostSigner: RenownCryptoSigner;
|
|
104
|
+
let credential: PowerhouseVerifiableCredential;
|
|
105
|
+
let statement: string;
|
|
106
|
+
let module: InProcessReactorClientModule;
|
|
107
|
+
|
|
108
|
+
const statementFor = (
|
|
109
|
+
overrides: Partial<Parameters<typeof createDelegation>[0]> = {},
|
|
110
|
+
) =>
|
|
111
|
+
createDelegation({
|
|
112
|
+
issuer: session.issuer,
|
|
113
|
+
credential,
|
|
114
|
+
delegate: delegate.did,
|
|
115
|
+
environment: hostCrypto.did,
|
|
116
|
+
scope: SCOPE,
|
|
117
|
+
...overrides,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const signed = (
|
|
121
|
+
action: Action,
|
|
122
|
+
documentId: string,
|
|
123
|
+
by: { crypto?: IRenownCrypto; statement?: string } = {},
|
|
124
|
+
) =>
|
|
125
|
+
signDelegatedAction({
|
|
126
|
+
action,
|
|
127
|
+
target: { documentId, branch: "main" },
|
|
128
|
+
crypto: by.crypto ?? delegate,
|
|
129
|
+
appName: "sektar-runner",
|
|
130
|
+
user: USER,
|
|
131
|
+
statement: by.statement ?? statement,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const newWorkflow = async (
|
|
135
|
+
over: InProcessReactorClientModule = module,
|
|
136
|
+
): Promise<string> => {
|
|
137
|
+
const created = await over.client.create(Workflow.utils.createDocument());
|
|
138
|
+
return created.header.id;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const submit = async (id: string, actions: Action[]) => {
|
|
142
|
+
const job = await module.reactor.execute(id, "main", actions);
|
|
143
|
+
return module.client.waitForJob(job);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const hostIdentity = () => ({ key: hostCrypto.did });
|
|
147
|
+
|
|
148
|
+
const publishes = async (id: string): Promise<Operation[]> => {
|
|
149
|
+
const page = await module.client.getOperations(
|
|
150
|
+
id,
|
|
151
|
+
{ scopes: ["global"] },
|
|
152
|
+
{ actionTypes: ["PUBLISH_WORKFLOW"] },
|
|
153
|
+
);
|
|
154
|
+
return page.results;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
beforeAll(async () => {
|
|
158
|
+
session = await newCrypto();
|
|
159
|
+
delegate = await newCrypto();
|
|
160
|
+
hostCrypto = await newCrypto();
|
|
161
|
+
hostSigner = new RenownCryptoSigner(hostCrypto, "switchboard");
|
|
162
|
+
credential = await buildAndSignCredential({
|
|
163
|
+
signTypedData: ((args) =>
|
|
164
|
+
PERSON.signTypedData(args)) as SignCredentialTypedData,
|
|
165
|
+
address: PERSON.address,
|
|
166
|
+
chainId: 1,
|
|
167
|
+
app: "connect",
|
|
168
|
+
appId: session.did,
|
|
169
|
+
});
|
|
170
|
+
statement = await statementFor();
|
|
171
|
+
module = await reactorOf(
|
|
172
|
+
hostSigner,
|
|
173
|
+
getDelegationTrustPolicyConfig({
|
|
174
|
+
signer: hostSigner,
|
|
175
|
+
} as unknown as IRenown),
|
|
176
|
+
);
|
|
177
|
+
}, 60_000);
|
|
178
|
+
|
|
179
|
+
afterAll(() => {
|
|
180
|
+
module.reactor.kill();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe("with authEnforcement off", () => {
|
|
184
|
+
it("makes the person the run user of a publish the delegate signed", async () => {
|
|
185
|
+
const id = await newWorkflow();
|
|
186
|
+
const job = await submit(id, [await signed(publish(), id)]);
|
|
187
|
+
expect(job.status).not.toBe("FAILED");
|
|
188
|
+
|
|
189
|
+
const runUser = await publishRunUser(module.client, id, hostIdentity());
|
|
190
|
+
expect(runUser?.address).toBe(PERSON.address);
|
|
191
|
+
expect(runUser?.subject.key).toBe(delegate.did);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("keeps the statement on the stored operation", async () => {
|
|
195
|
+
const id = await newWorkflow();
|
|
196
|
+
await submit(id, [await signed(publish(), id)]);
|
|
197
|
+
const [operation] = await publishes(id);
|
|
198
|
+
const signatures = operation.action.context!.signer!.signatures;
|
|
199
|
+
expect(signatures).toHaveLength(2);
|
|
200
|
+
expect(signatures[0][3]).toBe(statement);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("takes a batch mixing the host's actions and a delegate's", async () => {
|
|
204
|
+
const id = await newWorkflow();
|
|
205
|
+
const client = await clientOver(module, hostSigner);
|
|
206
|
+
await client.execute(id, "main", [
|
|
207
|
+
workflowActions.setWorkflowName({ name: "Mixed" }),
|
|
208
|
+
await signed(publish(), id),
|
|
209
|
+
workflowActions.setWorkflowStatus({ status: "DISABLED" }),
|
|
210
|
+
]);
|
|
211
|
+
|
|
212
|
+
const page = await module.client.getOperations(id, {
|
|
213
|
+
scopes: ["global"],
|
|
214
|
+
});
|
|
215
|
+
const keys = page.results.map(
|
|
216
|
+
(operation) => operation.action.context?.signer?.app.key,
|
|
217
|
+
);
|
|
218
|
+
expect(keys).toEqual([hostCrypto.did, delegate.did, hostCrypto.did]);
|
|
219
|
+
const runUser = await publishRunUser(module.client, id, hostIdentity());
|
|
220
|
+
expect(runUser?.address).toBe(PERSON.address);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("refuses a stolen statement used by another key", async () => {
|
|
224
|
+
const id = await newWorkflow();
|
|
225
|
+
const thief = await newCrypto();
|
|
226
|
+
const job = await submit(id, [
|
|
227
|
+
await signed(publish(), id, { crypto: thief }),
|
|
228
|
+
]);
|
|
229
|
+
expect(job.status).toBe("FAILED");
|
|
230
|
+
expect(JSON.stringify(job.error)).toMatch(/SIGNER_UNAUTHORIZED/);
|
|
231
|
+
expect(await publishes(id)).toEqual([]);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("refuses a statement for another environment at first write", async () => {
|
|
235
|
+
const id = await newWorkflow();
|
|
236
|
+
const elsewhere = await newCrypto();
|
|
237
|
+
const job = await submit(id, [
|
|
238
|
+
await signed(publish(), id, {
|
|
239
|
+
statement: await statementFor({ environment: elsewhere.did }),
|
|
240
|
+
}),
|
|
241
|
+
]);
|
|
242
|
+
expect(job.status).toBe("FAILED");
|
|
243
|
+
expect(JSON.stringify(job.error)).toMatch(/SIGNER_UNAUTHORIZED/);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("refuses a delegate signing outside its scope", async () => {
|
|
247
|
+
const id = await newWorkflow();
|
|
248
|
+
const job = await submit(id, [
|
|
249
|
+
await signed(
|
|
250
|
+
workflowActions.setWorkflowStatus({ status: "ENABLED" }),
|
|
251
|
+
id,
|
|
252
|
+
),
|
|
253
|
+
]);
|
|
254
|
+
expect(job.status).toBe("FAILED");
|
|
255
|
+
expect(JSON.stringify(job.error)).toMatch(/SIGNER_UNAUTHORIZED/);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("refuses an unsigned caller claiming a delegate", async () => {
|
|
259
|
+
const id = await newWorkflow();
|
|
260
|
+
const action = await signed(publish(), id);
|
|
261
|
+
const claimed: Action = {
|
|
262
|
+
...action,
|
|
263
|
+
context: {
|
|
264
|
+
signer: {
|
|
265
|
+
user: USER,
|
|
266
|
+
app: { name: "sektar-runner", key: "" },
|
|
267
|
+
signatures: [],
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
const job = await submit(id, [claimed]);
|
|
272
|
+
expect(job.status).toBe("FAILED");
|
|
273
|
+
// A v2-required document refuses it before the claim is even read.
|
|
274
|
+
expect(JSON.stringify(job.error)).toMatch(/UNSIGNED_REQUIRED/);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("refuses a claim of the delegate's key with no valid signature", async () => {
|
|
278
|
+
const id = await newWorkflow();
|
|
279
|
+
const action = await signed(publish(), id);
|
|
280
|
+
const signer = action.context!.signer!;
|
|
281
|
+
const tuple = signer.signatures.at(-1)!;
|
|
282
|
+
const forged: Action = {
|
|
283
|
+
...action,
|
|
284
|
+
context: {
|
|
285
|
+
signer: {
|
|
286
|
+
...signer,
|
|
287
|
+
signatures: [
|
|
288
|
+
signer.signatures[0],
|
|
289
|
+
[tuple[0], tuple[1], tuple[2], tuple[3], `0x${"ab".repeat(64)}`],
|
|
290
|
+
],
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
const job = await submit(id, [forged]);
|
|
295
|
+
expect(job.status).toBe("FAILED");
|
|
296
|
+
expect(JSON.stringify(job.error)).toMatch(/BAD_SIGNATURE/);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("stores a replayed operation once", async () => {
|
|
300
|
+
const id = await newWorkflow();
|
|
301
|
+
const action = await signed(publish(), id);
|
|
302
|
+
await submit(id, [action]);
|
|
303
|
+
await submit(id, [action]);
|
|
304
|
+
expect(await publishes(id)).toHaveLength(1);
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
describe("a host with no delegations", () => {
|
|
309
|
+
it("signs a publish itself, which has no run user", async () => {
|
|
310
|
+
const id = await newWorkflow();
|
|
311
|
+
const client = await clientOver(module, hostSigner);
|
|
312
|
+
await client.execute(id, "main", [publish()]);
|
|
313
|
+
expect(
|
|
314
|
+
await publishRunUser(module.client, id, hostIdentity()),
|
|
315
|
+
).toBeNull();
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("still accepts a signed write with no statement, as the default did", async () => {
|
|
319
|
+
const id = await newWorkflow();
|
|
320
|
+
const someone = new RenownCryptoSigner(await newCrypto(), "app", USER);
|
|
321
|
+
const client = await clientOver(module, someone);
|
|
322
|
+
await client.execute(id, "main", [publish()]);
|
|
323
|
+
const runUser = await publishRunUser(module.client, id, hostIdentity());
|
|
324
|
+
expect(runUser?.address).toBe(PERSON.address);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it("asks nothing of Renown", () => {
|
|
328
|
+
const trust = getDelegationTrustPolicyConfig({
|
|
329
|
+
signer: hostSigner,
|
|
330
|
+
} as unknown as IRenown);
|
|
331
|
+
expect(trust.workerTrustPolicy?.initArgs).toMatchObject({
|
|
332
|
+
delegationOnly: true,
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
describe("on load", () => {
|
|
338
|
+
it("takes a delegated operation from a peer, across the wire format", async () => {
|
|
339
|
+
const id = await newWorkflow();
|
|
340
|
+
await submit(id, [await signed(publish(), id)]);
|
|
341
|
+
|
|
342
|
+
// Another host: its environment is not the statement's.
|
|
343
|
+
const peerCrypto = await newCrypto();
|
|
344
|
+
const peerSigner = new RenownCryptoSigner(peerCrypto, "switchboard");
|
|
345
|
+
const peer = await reactorOf(
|
|
346
|
+
peerSigner,
|
|
347
|
+
getDelegationTrustPolicyConfig({
|
|
348
|
+
signer: peerSigner,
|
|
349
|
+
} as unknown as IRenown),
|
|
350
|
+
);
|
|
351
|
+
try {
|
|
352
|
+
for (const scope of ["document", "global"]) {
|
|
353
|
+
const page = await module.client.getOperations(id, {
|
|
354
|
+
scopes: [scope],
|
|
355
|
+
});
|
|
356
|
+
const overTheWire = page.results.map((operation) => {
|
|
357
|
+
const signer = operation.action.context?.signer;
|
|
358
|
+
if (!signer) return operation;
|
|
359
|
+
return {
|
|
360
|
+
...operation,
|
|
361
|
+
action: {
|
|
362
|
+
...operation.action,
|
|
363
|
+
context: {
|
|
364
|
+
...operation.action.context,
|
|
365
|
+
signer: {
|
|
366
|
+
...signer,
|
|
367
|
+
signatures: signer.signatures
|
|
368
|
+
.map((tuple) => serializeSignature(tuple))
|
|
369
|
+
.map((wire) => deserializeSignature(wire)),
|
|
370
|
+
},
|
|
371
|
+
},
|
|
372
|
+
},
|
|
373
|
+
};
|
|
374
|
+
});
|
|
375
|
+
const job = await peer.reactor.load(id, "main", overTheWire);
|
|
376
|
+
const done = await peer.client.waitForJob(job);
|
|
377
|
+
expect(done.status).not.toBe("FAILED");
|
|
378
|
+
}
|
|
379
|
+
const runUser = await publishRunUser(peer.client, id, {
|
|
380
|
+
key: peerCrypto.did,
|
|
381
|
+
});
|
|
382
|
+
expect(runUser?.address).toBe(PERSON.address);
|
|
383
|
+
expect(runUser?.subject.key).toBe(delegate.did);
|
|
384
|
+
} finally {
|
|
385
|
+
peer.reactor.kill();
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
describe("with authEnforcement on", () => {
|
|
391
|
+
let enforcing: InProcessReactorClientModule;
|
|
392
|
+
|
|
393
|
+
const submitted = async (id: string, action: Action) => {
|
|
394
|
+
const job = await enforcing.reactor.execute(id, "main", [action]);
|
|
395
|
+
return enforcing.client.waitForJob(job);
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
beforeAll(async () => {
|
|
399
|
+
const trust = await getRenownTrustPolicyConfig(
|
|
400
|
+
{
|
|
401
|
+
source: "self",
|
|
402
|
+
// Renown knows of no credential for the delegate, and is not asked.
|
|
403
|
+
request: () => Promise.resolve({ renownCredentials: [] }),
|
|
404
|
+
},
|
|
405
|
+
{ signer: hostSigner } as unknown as IRenown,
|
|
406
|
+
);
|
|
407
|
+
enforcing = await reactorOf(hostSigner, trust, {
|
|
408
|
+
documentDecisions: true,
|
|
409
|
+
authEnforcement: true,
|
|
410
|
+
});
|
|
411
|
+
}, 60_000);
|
|
412
|
+
|
|
413
|
+
afterAll(() => {
|
|
414
|
+
enforcing.reactor.kill();
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it("accepts a valid chain, and refuses it with the statement stripped", async () => {
|
|
418
|
+
const id = await newWorkflow(enforcing);
|
|
419
|
+
const action = await signed(publish(), id);
|
|
420
|
+
expect((await submitted(id, action)).status).not.toBe("FAILED");
|
|
421
|
+
|
|
422
|
+
const again = await signed(publish(), id);
|
|
423
|
+
const signer = again.context!.signer!;
|
|
424
|
+
const stripped: Action = {
|
|
425
|
+
...again,
|
|
426
|
+
context: {
|
|
427
|
+
signer: { ...signer, signatures: signer.signatures.slice(-1) },
|
|
428
|
+
},
|
|
429
|
+
};
|
|
430
|
+
const job = await submitted(id, stripped);
|
|
431
|
+
expect(job.status).toBe("FAILED");
|
|
432
|
+
expect(JSON.stringify(job.error)).toMatch(/SIGNER_UNAUTHORIZED/);
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
});
|
|
@@ -28,6 +28,46 @@ describe("installFatalErrorShutdown", () => {
|
|
|
28
28
|
vi.useRealTimers();
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
+
it("doesn't log a closed pipe during shutdown, as that only raises it again", () => {
|
|
32
|
+
const proc = makeProc();
|
|
33
|
+
const error = vi.fn();
|
|
34
|
+
installFatalErrorShutdown({ error } as never, proc as never);
|
|
35
|
+
const closed = Object.assign(new Error("write EPIPE"), { code: "EPIPE" });
|
|
36
|
+
|
|
37
|
+
proc.emit("uncaughtException", closed);
|
|
38
|
+
proc.emit("uncaughtException", closed);
|
|
39
|
+
proc.emit("uncaughtException", new Error("something else"));
|
|
40
|
+
|
|
41
|
+
expect(error).toHaveBeenCalledTimes(2);
|
|
42
|
+
expect(error.mock.calls[0][1]).toBe(closed);
|
|
43
|
+
expect(error.mock.calls[1][1]).not.toBe(closed);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("on Windows, where killing a process ends it on the spot", () => {
|
|
47
|
+
it("calls SIGTERM's listeners, so the stores close first", () => {
|
|
48
|
+
const proc = Object.assign(makeProc(), { platform: "win32" as const });
|
|
49
|
+
const onSigterm = vi.fn();
|
|
50
|
+
proc.on("SIGTERM", onSigterm);
|
|
51
|
+
installFatalErrorShutdown(logger, proc as never);
|
|
52
|
+
|
|
53
|
+
proc.emit("uncaughtException", new Error("boom"));
|
|
54
|
+
|
|
55
|
+
expect(onSigterm).toHaveBeenCalledOnce();
|
|
56
|
+
expect(proc.kill).not.toHaveBeenCalled();
|
|
57
|
+
expect(proc.exit).not.toHaveBeenCalled();
|
|
58
|
+
expect(proc.exitCode).toBe(1);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("exits if nothing is listening to shut anything down", () => {
|
|
62
|
+
const proc = Object.assign(makeProc(), { platform: "win32" as const });
|
|
63
|
+
installFatalErrorShutdown(logger, proc as never);
|
|
64
|
+
|
|
65
|
+
proc.emit("uncaughtException", new Error("boom"));
|
|
66
|
+
|
|
67
|
+
expect(proc.exit).toHaveBeenCalledWith(1);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
31
71
|
it.each(["uncaughtException", "unhandledRejection"])(
|
|
32
72
|
"routes %s through SIGTERM with exit code 1",
|
|
33
73
|
(event) => {
|
|
@@ -6,13 +6,16 @@ import {
|
|
|
6
6
|
} from "@powerhousedao/reactor-api/pglite-node";
|
|
7
7
|
import type { ILogger } from "document-model";
|
|
8
8
|
import { EventEmitter } from "node:events";
|
|
9
|
-
import { mkdtemp, rm } from "node:fs/promises";
|
|
9
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
10
10
|
import { tmpdir } from "node:os";
|
|
11
11
|
import { join } from "node:path";
|
|
12
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
12
|
+
import { afterEach, describe, expect, it, vi, type Mock } from "vitest";
|
|
13
13
|
import { installFatalErrorShutdown } from "../src/fatal-shutdown.mjs";
|
|
14
14
|
import {
|
|
15
15
|
createPgliteStoreFactory,
|
|
16
|
+
openStoresTogether,
|
|
17
|
+
READ_STORE,
|
|
18
|
+
REACTOR_STORE,
|
|
16
19
|
routePgliteWorkerFailures,
|
|
17
20
|
terminateOnPoisoned,
|
|
18
21
|
type PgliteStoreOptions,
|
|
@@ -231,6 +234,180 @@ describe("createPgliteStoreFactory", () => {
|
|
|
231
234
|
);
|
|
232
235
|
});
|
|
233
236
|
|
|
237
|
+
describe("openStoresTogether", () => {
|
|
238
|
+
function fakeStore(waitReady: Promise<void> = Promise.resolve()) {
|
|
239
|
+
return {
|
|
240
|
+
waitReady,
|
|
241
|
+
close: vi.fn(() => Promise.resolve()),
|
|
242
|
+
} as unknown as Omit<PGliteLike, "close"> & {
|
|
243
|
+
close: Mock<() => Promise<void>>;
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function failing(error: Error): Promise<void> {
|
|
248
|
+
const opening = Promise.reject(error);
|
|
249
|
+
opening.catch(() => undefined);
|
|
250
|
+
return opening;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
it("leaves both open when both open", async () => {
|
|
254
|
+
const reactor = fakeStore();
|
|
255
|
+
const readModel = fakeStore();
|
|
256
|
+
const settled = vi.fn();
|
|
257
|
+
const stores = openStoresTogether(
|
|
258
|
+
() => reactor,
|
|
259
|
+
() => readModel,
|
|
260
|
+
);
|
|
261
|
+
stores.failed.then(settled, settled);
|
|
262
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
263
|
+
expect(stores.reactor).toBe(reactor);
|
|
264
|
+
expect(stores.readModel).toBe(readModel);
|
|
265
|
+
expect(settled).not.toHaveBeenCalled();
|
|
266
|
+
expect(reactor.close).not.toHaveBeenCalled();
|
|
267
|
+
expect(readModel.close).not.toHaveBeenCalled();
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it.each([
|
|
271
|
+
["reactor", "readModel"],
|
|
272
|
+
["readModel", "reactor"],
|
|
273
|
+
] as const)("%s failing closes %s, with the failure", async (bad, good) => {
|
|
274
|
+
const error = new Error("the disk is full");
|
|
275
|
+
const fakes = {
|
|
276
|
+
reactor: fakeStore(bad === "reactor" ? failing(error) : undefined),
|
|
277
|
+
readModel: fakeStore(bad === "readModel" ? failing(error) : undefined),
|
|
278
|
+
};
|
|
279
|
+
const stores = openStoresTogether(
|
|
280
|
+
() => fakes.reactor,
|
|
281
|
+
() => fakes.readModel,
|
|
282
|
+
);
|
|
283
|
+
await expect(stores.failed).rejects.toBe(error);
|
|
284
|
+
expect(fakes[good].close).toHaveBeenCalled();
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("reports the failure only once the survivor has closed", async () => {
|
|
288
|
+
let closed!: () => void;
|
|
289
|
+
const survivor = fakeStore();
|
|
290
|
+
survivor.close.mockReturnValue(
|
|
291
|
+
new Promise((resolve) => (closed = resolve)),
|
|
292
|
+
);
|
|
293
|
+
const stores = openStoresTogether(
|
|
294
|
+
() => fakeStore(failing(new Error("no"))),
|
|
295
|
+
() => survivor,
|
|
296
|
+
);
|
|
297
|
+
const settled = vi.fn();
|
|
298
|
+
stores.failed.catch(settled);
|
|
299
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
300
|
+
expect(settled).not.toHaveBeenCalled();
|
|
301
|
+
closed();
|
|
302
|
+
await vi.waitFor(() => expect(settled).toHaveBeenCalled());
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("still reports the failure when the survivor won't close", async () => {
|
|
306
|
+
const error = new Error("no");
|
|
307
|
+
const survivor = fakeStore();
|
|
308
|
+
survivor.close.mockRejectedValue(new Error("won't"));
|
|
309
|
+
const stores = openStoresTogether(
|
|
310
|
+
() => survivor,
|
|
311
|
+
() => fakeStore(failing(error)),
|
|
312
|
+
);
|
|
313
|
+
await expect(stores.failed).rejects.toBe(error);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("closes the reactor store if the read store can't start", () => {
|
|
317
|
+
const reactor = fakeStore();
|
|
318
|
+
const error = new Error("no worker");
|
|
319
|
+
expect(() =>
|
|
320
|
+
openStoresTogether(
|
|
321
|
+
() => reactor,
|
|
322
|
+
() => {
|
|
323
|
+
throw error;
|
|
324
|
+
},
|
|
325
|
+
),
|
|
326
|
+
).toThrow(error);
|
|
327
|
+
expect(reactor.close).toHaveBeenCalled();
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("closes both for a boot that failed elsewhere", async () => {
|
|
331
|
+
const reactor = fakeStore();
|
|
332
|
+
const readModel = fakeStore();
|
|
333
|
+
await openStoresTogether(
|
|
334
|
+
() => reactor,
|
|
335
|
+
() => readModel,
|
|
336
|
+
).close();
|
|
337
|
+
expect(reactor.close).toHaveBeenCalled();
|
|
338
|
+
expect(readModel.close).toHaveBeenCalled();
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
describe("on real stores", () => {
|
|
342
|
+
let root: string;
|
|
343
|
+
const logger = stubLogger() as unknown as ILogger;
|
|
344
|
+
const opener = async (store: typeof READ_STORE | typeof REACTOR_STORE) => {
|
|
345
|
+
const open = await createPgliteStoreFactory({
|
|
346
|
+
...store,
|
|
347
|
+
major: 17,
|
|
348
|
+
inMemory: false,
|
|
349
|
+
threaded: true,
|
|
350
|
+
logger,
|
|
351
|
+
});
|
|
352
|
+
return open;
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
afterEach(async () => {
|
|
356
|
+
await rm(root, { recursive: true, force: true });
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it(
|
|
360
|
+
"opens both, each in a thread of its own",
|
|
361
|
+
async () => {
|
|
362
|
+
root = await mkdtemp(join(tmpdir(), "sb-stores-"));
|
|
363
|
+
const openReactor = await opener(REACTOR_STORE);
|
|
364
|
+
const openRead = await opener(READ_STORE);
|
|
365
|
+
const stores = openStoresTogether(
|
|
366
|
+
() => openReactor(join(root, "reactor-storage")),
|
|
367
|
+
() => openRead(join(root, "read-storage")),
|
|
368
|
+
);
|
|
369
|
+
await Promise.all([
|
|
370
|
+
stores.reactor.waitReady,
|
|
371
|
+
stores.readModel.waitReady,
|
|
372
|
+
]);
|
|
373
|
+
expect((stores.reactor as ThreadedPGlite).threadId).not.toBe(
|
|
374
|
+
(stores.readModel as ThreadedPGlite).threadId,
|
|
375
|
+
);
|
|
376
|
+
await stores.reactor.exec("create table kept (v text)");
|
|
377
|
+
await stores.readModel.query("select 1");
|
|
378
|
+
await stores.close();
|
|
379
|
+
expect(stores.reactor.closed).toBe(true);
|
|
380
|
+
expect(stores.readModel.closed).toBe(true);
|
|
381
|
+
},
|
|
382
|
+
BOOT * 2,
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
it(
|
|
386
|
+
"closes a store that's still opening when the other can't, and it opens again",
|
|
387
|
+
async () => {
|
|
388
|
+
root = await mkdtemp(join(tmpdir(), "sb-stores-"));
|
|
389
|
+
const notAStore = join(root, "read-storage");
|
|
390
|
+
await writeFile(notAStore, "a file where a store should be");
|
|
391
|
+
const reactorDir = join(root, "reactor-storage");
|
|
392
|
+
const openReactor = await opener(REACTOR_STORE);
|
|
393
|
+
const openRead = await opener(READ_STORE);
|
|
394
|
+
const stores = openStoresTogether(
|
|
395
|
+
() => openReactor(reactorDir),
|
|
396
|
+
() => openRead(notAStore),
|
|
397
|
+
);
|
|
398
|
+
await expect(stores.failed).rejects.toThrow();
|
|
399
|
+
expect(stores.reactor.closed).toBe(true);
|
|
400
|
+
|
|
401
|
+
const again = openReactor(reactorDir);
|
|
402
|
+
await again.waitReady;
|
|
403
|
+
await again.query("select 1");
|
|
404
|
+
await again.close();
|
|
405
|
+
},
|
|
406
|
+
BOOT * 2,
|
|
407
|
+
);
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
|
|
234
411
|
describe("vacuumFullTimingFromEnv", () => {
|
|
235
412
|
it("reads both timings, and leaves unset or invalid values to the defaults", () => {
|
|
236
413
|
expect(
|