@niwa-labs/host 0.0.0-stage → 0.0.0
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/LICENSE +21 -0
- package/README.md +2 -2
- package/package.json +16 -4
- package/src/access-actions.ts +193 -0
- package/src/access-control.ts +333 -0
- package/src/call-operations.ts +176 -0
- package/src/cockpit-catalog.ts +99 -0
- package/src/describe.ts +55 -0
- package/src/doors.ts +272 -0
- package/src/host-types.ts +43 -0
- package/src/host.ts +3346 -0
- package/src/index.ts +10 -0
- package/src/plugin-install.ts +249 -0
- package/src/state.ts +261 -0
- package/src/stores.ts +131 -0
- package/test/abandon.test.ts +58 -0
- package/test/action-refusal-restart.test.ts +46 -0
- package/test/action-step.test.ts +363 -0
- package/test/action-wait-restart.test.ts +96 -0
- package/test/builtin-actions.test.ts +21 -0
- package/test/cancel-steps.test.ts +150 -0
- package/test/catch-up.test.ts +378 -0
- package/test/causes.test.ts +138 -0
- package/test/client.test.ts +190 -0
- package/test/cockpit-catalog.test.ts +118 -0
- package/test/crash-between-steps.test.ts +88 -0
- package/test/declared-values.test.ts +175 -0
- package/test/deep-nested-pause.test.ts +147 -0
- package/test/fixtures/action-step-host.ts +166 -0
- package/test/fixtures/action-wait-restart-host.ts +62 -0
- package/test/fixtures/catch-up-host.ts +91 -0
- package/test/fixtures/causes-host.ts +78 -0
- package/test/fixtures/client-host.ts +52 -0
- package/test/fixtures/crash-between-steps-host.ts +71 -0
- package/test/fixtures/declared-values-host.ts +110 -0
- package/test/fixtures/intercept-order-host.ts +112 -0
- package/test/fixtures/intercept-step-host.ts +106 -0
- package/test/fixtures/live-frames-host.ts +81 -0
- package/test/fixtures/long-wait-host.ts +78 -0
- package/test/fixtures/nested-wait-host.ts +120 -0
- package/test/fixtures/no-repeat-crash-host.ts +79 -0
- package/test/fixtures/plugin-replace-host.ts +176 -0
- package/test/fixtures/record-refusal-crash.ts +50 -0
- package/test/fixtures/recovery-contracts-host.ts +79 -0
- package/test/fixtures/repeated-write-host.ts +39 -0
- package/test/fixtures/replay-mismatch-host.ts +66 -0
- package/test/fixtures/single-writer-host.ts +113 -0
- package/test/fixtures/state-host.ts +118 -0
- package/test/fixtures/wait-restart-host.ts +82 -0
- package/test/guards.test.ts +448 -0
- package/test/handler-emit.test.ts +116 -0
- package/test/host.test.ts +114 -0
- package/test/intercept-adjust.test.ts +318 -0
- package/test/intercept-order.test.ts +276 -0
- package/test/intercept-step.test.ts +144 -0
- package/test/large-outcome.test.ts +60 -0
- package/test/live-frames.test.ts +150 -0
- package/test/live-ready.test.ts +61 -0
- package/test/long-wait.test.ts +168 -0
- package/test/nested-outcome.test.ts +75 -0
- package/test/nested-wait.test.ts +111 -0
- package/test/no-repeat-crash.test.ts +132 -0
- package/test/offer-replace.test.ts +39 -0
- package/test/plugin-async-setup.test.ts +22 -0
- package/test/plugin-copies.test.ts +87 -0
- package/test/plugin-emit.test.ts +43 -0
- package/test/plugin-install-failure.test.ts +121 -0
- package/test/plugin-install-hash.test.ts +57 -0
- package/test/plugin-needs.test.ts +80 -0
- package/test/plugin-replace.test.ts +225 -0
- package/test/plugin-uninstall.test.ts +70 -0
- package/test/plugin-version-copies.test.ts +53 -0
- package/test/plugin-version.test.ts +180 -0
- package/test/read-filter.test.ts +136 -0
- package/test/read-query.test.ts +92 -0
- package/test/record-admission.test.ts +294 -0
- package/test/recovery-contracts.test.ts +107 -0
- package/test/repeated-write.test.ts +119 -0
- package/test/replay-mismatch.test.ts +79 -0
- package/test/replay.test.ts +135 -0
- package/test/single-writer.test.ts +267 -0
- package/test/state.test.ts +246 -0
- package/test/step-by-name.test.ts +122 -0
- package/test/stores.test.ts +102 -0
- package/test/stream.test.ts +154 -0
- package/test/subscriber-stream.test.ts +71 -0
- package/test/subscribers.test.ts +101 -0
- package/test/subscriptions.test.ts +632 -0
- package/test/trace-call.test.ts +124 -0
- package/test/trigger-filter.test.ts +42 -0
- package/test/use-many.test.ts +151 -0
- package/test/wait-query.test.ts +117 -0
- package/test/wait-restart.test.ts +187 -0
- package/test/wire-read-access.test.ts +85 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// What a client reads over the wire, `records.read`, `records.since`, `trace` and `causes`, is held
|
|
2
|
+
// to its grant as the same actions are when they are called: the owner reads everything, a subject
|
|
3
|
+
// reads when its grant lets it call the action, and a subject whose grant does not is refused.
|
|
4
|
+
|
|
5
|
+
import { afterEach, expect, test } from "bun:test";
|
|
6
|
+
|
|
7
|
+
import { grant, principalOf, seq } from "@niwa-labs/core";
|
|
8
|
+
import { connect } from "@niwa-labs/client";
|
|
9
|
+
import type { NiwaApi } from "@niwa-labs/client";
|
|
10
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
11
|
+
import type { Host } from "@niwa-labs/host";
|
|
12
|
+
import { record } from "@niwa-labs/core";
|
|
13
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
14
|
+
import { connectAsOwner, tempHome } from "@niwa-labs/testing";
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
|
|
17
|
+
const Fact = record("wire-read.fact", z.object({ n: z.number() }), { open: true });
|
|
18
|
+
|
|
19
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
20
|
+
const hosts: Host[] = [];
|
|
21
|
+
const clients: NiwaApi[] = [];
|
|
22
|
+
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
for (const client of clients.splice(0)) await client.close().catch(() => {});
|
|
25
|
+
for (const host of hosts.splice(0)) await host.close().catch(() => {});
|
|
26
|
+
for (const home of homes.splice(0)) await home.cleanup();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
async function served() {
|
|
30
|
+
const home = await tempHome();
|
|
31
|
+
homes.push(home);
|
|
32
|
+
const host = createNiwa({ home: home.path });
|
|
33
|
+
host.use(plugin("wire-read", (api) => api.declare(Fact)));
|
|
34
|
+
await host.serve();
|
|
35
|
+
hosts.push(host);
|
|
36
|
+
const owner = await connectAsOwner(home.path);
|
|
37
|
+
clients.push(owner);
|
|
38
|
+
const fact = await host.emit(Fact, { n: 1 });
|
|
39
|
+
const as = async (name: string, rules: string[]): Promise<NiwaApi> => {
|
|
40
|
+
const token = host.issueToken(principalOf("session", name), { grant: grant(rules) });
|
|
41
|
+
const client = await connect({ home: home.path, id: `session:${name}`, token });
|
|
42
|
+
clients.push(client);
|
|
43
|
+
return client;
|
|
44
|
+
};
|
|
45
|
+
return { owner, fact, as };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
test("the owner reads over the wire", async () => {
|
|
49
|
+
const { owner, fact } = await served();
|
|
50
|
+
expect((await owner.records.read({ types: [Fact] })).map((one) => one.id)).toEqual([fact.id]);
|
|
51
|
+
expect((await owner.trace(fact.id)).map((one) => one.id)).toContain(fact.id);
|
|
52
|
+
expect(await owner.causes(fact.id)).toBeDefined();
|
|
53
|
+
const seen = [];
|
|
54
|
+
for await (const one of owner.records.since({ types: [Fact] })) {
|
|
55
|
+
seen.push(one.id);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
expect(seen).toEqual([fact.id]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("a subject whose grant does not let it call the reads is refused them over the wire", async () => {
|
|
62
|
+
const { fact, as } = await served();
|
|
63
|
+
const none = await as("none", ["task.*"]);
|
|
64
|
+
await expect(none.records.read({ types: [Fact] })).rejects.toThrow("is not in the grant");
|
|
65
|
+
await expect(none.trace(fact.id)).rejects.toThrow("is not in the grant");
|
|
66
|
+
await expect(none.causes(fact.id)).rejects.toThrow("is not in the grant");
|
|
67
|
+
|
|
68
|
+
const subscriber = await as("subscriber", ["task.*"]);
|
|
69
|
+
const stream = subscriber.records.since({ types: [Fact] })[Symbol.asyncIterator]();
|
|
70
|
+
await expect(stream.next()).rejects.toThrow("is not in the grant");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("a subject whose grant lets it call the reads gets them over the wire", async () => {
|
|
74
|
+
const { fact, as } = await served();
|
|
75
|
+
const reader = await as("reader", ["niwa.records.read", "niwa.trace", "niwa.causes"]);
|
|
76
|
+
expect((await reader.records.read({ types: [Fact] })).map((one) => one.id)).toEqual([fact.id]);
|
|
77
|
+
expect((await reader.trace(fact.id)).map((one) => one.id)).toContain(fact.id);
|
|
78
|
+
expect(await reader.causes(fact.id)).toBeDefined();
|
|
79
|
+
const seen = [];
|
|
80
|
+
for await (const one of reader.records.since({ types: [Fact] })) {
|
|
81
|
+
seen.push(one.id);
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
expect(seen).toEqual([fact.id]);
|
|
85
|
+
});
|