@rsc-kit/core 0.42.0 → 0.43.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/dist/apiPrerender.js +25 -12
- package/dist/apiPrerender.js.map +1 -1
- package/dist/buildProcess.d.ts +12 -0
- package/dist/buildProcess.js +202 -0
- package/dist/buildProcess.js.map +1 -0
- package/dist/buildScope.d.ts +61 -0
- package/dist/buildScope.js +172 -0
- package/dist/buildScope.js.map +1 -0
- package/dist/conformance.d.ts +3 -0
- package/dist/conformance.js +23 -3
- package/dist/conformance.js.map +1 -1
- package/dist/conformanceBuild.d.ts +5 -0
- package/dist/conformanceBuild.js +154 -0
- package/dist/conformanceBuild.js.map +1 -0
- package/dist/csp.d.ts +11 -2
- package/dist/csp.js +55 -6
- package/dist/csp.js.map +1 -1
- package/dist/formArgs.d.ts +10 -0
- package/dist/formArgs.js +41 -0
- package/dist/formArgs.js.map +1 -0
- package/dist/hostCalls.d.ts +17 -0
- package/dist/hostCalls.js +103 -103
- package/dist/hostCalls.js.map +1 -1
- package/dist/hostSources.d.ts +23 -0
- package/dist/hostSources.js +65 -0
- package/dist/hostSources.js.map +1 -0
- package/dist/prerender.d.ts +18 -5
- package/dist/prerender.js +95 -16
- package/dist/prerender.js.map +1 -1
- package/dist/typegen.js +11 -2
- package/dist/typegen.js.map +1 -1
- package/dist/vite.d.ts +17 -2
- package/dist/vite.js +245 -105
- package/dist/vite.js.map +1 -1
- package/package.json +5 -1
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// rpc() during a build.
|
|
2
|
+
//
|
|
3
|
+
// A page whose data is the same for everyone can be stored, and for an app
|
|
4
|
+
// with a backend that data is behind rpc(). So the build asks: each call is
|
|
5
|
+
// answered by the backend's own process (buildProcess.ts), and the answer is
|
|
6
|
+
// part of the page. A call that read the visitor - the session, a cookie, who
|
|
7
|
+
// is signed in - has no answer a build could store, and the backend says so;
|
|
8
|
+
// that call suspends, exactly as connection() would, and the page streams
|
|
9
|
+
// there.
|
|
10
|
+
//
|
|
11
|
+
// Two copies of this module exist during a build: the plugin's, which starts
|
|
12
|
+
// the backend and times each render, and the one inside the bundle, where
|
|
13
|
+
// rpc() is. What they share is on the global, under keys both copies agree on.
|
|
14
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
15
|
+
import { interpretReply } from './hostCalls.js';
|
|
16
|
+
const ANSWERER = Symbol.for('rsc-kit.build-answerer');
|
|
17
|
+
const SCOPES = Symbol.for('rsc-kit.build-scope');
|
|
18
|
+
const shared = globalThis;
|
|
19
|
+
function scopes() {
|
|
20
|
+
return (shared[SCOPES] ??= new AsyncLocalStorage());
|
|
21
|
+
}
|
|
22
|
+
/** Whether this build has a backend to ask. */
|
|
23
|
+
export function buildAnswerer() {
|
|
24
|
+
return shared[ANSWERER] ?? null;
|
|
25
|
+
}
|
|
26
|
+
/** Have rpc() answered by this, until what is returned is called. */
|
|
27
|
+
export function installBuildAnswerer(answerer) {
|
|
28
|
+
shared[ANSWERER] = answerer;
|
|
29
|
+
return () => {
|
|
30
|
+
if (shared[ANSWERER] === answerer)
|
|
31
|
+
delete shared[ANSWERER];
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** The call as the build writes it, with what it read when that is why. */
|
|
35
|
+
export function saidCall(name, read = []) {
|
|
36
|
+
return 'rpc(' + JSON.stringify(name) + ')' + (read.length ? ' (' + read.join(', ') + ')' : '');
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Run one route's renders, noting the calls they make.
|
|
40
|
+
*
|
|
41
|
+
* The notes are read while the render may still be going - a render with a
|
|
42
|
+
* hole never ends - so they are handed over as a function.
|
|
43
|
+
*/
|
|
44
|
+
export function withBuildCalls(run) {
|
|
45
|
+
const scope = { pending: 0, listeners: new Set(), answered: new Set(), visitors: new Map(), failed: new Map() };
|
|
46
|
+
return scopes().run(scope, () => run(() => ({
|
|
47
|
+
answered: [...scope.answered].map((name) => saidCall(name)),
|
|
48
|
+
visitors: [...scope.visitors].map(([name, read]) => saidCall(name, read)),
|
|
49
|
+
failed: [...scope.failed].map(([name, message]) => saidCall(name) + ' failed: ' + message),
|
|
50
|
+
})));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Answer one rpc() of a build.
|
|
54
|
+
*
|
|
55
|
+
* `needsVisitor` decides what a call that read the visitor does to the render
|
|
56
|
+
* that made it: suspend, for the probe deciding what a page's shell is.
|
|
57
|
+
* Outside any route - generateStaticParams, register() - there is no render
|
|
58
|
+
* to leave a hole in, so it is an error that says what was read.
|
|
59
|
+
*/
|
|
60
|
+
export async function answerAtBuild(args, needsVisitor) {
|
|
61
|
+
const answerer = buildAnswerer();
|
|
62
|
+
const name = String(args[0]);
|
|
63
|
+
if (!answerer)
|
|
64
|
+
throw new Error(saidCall(name) + ' cannot be answered: this build has no backend to ask.');
|
|
65
|
+
const scope = scopes().getStore();
|
|
66
|
+
const changed = () => {
|
|
67
|
+
for (const listener of [...(scope?.listeners ?? [])])
|
|
68
|
+
listener();
|
|
69
|
+
};
|
|
70
|
+
if (scope) {
|
|
71
|
+
scope.pending++;
|
|
72
|
+
changed();
|
|
73
|
+
}
|
|
74
|
+
let answer;
|
|
75
|
+
try {
|
|
76
|
+
answer = await answerer.ask(name, args.slice(1));
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
scope?.failed.set(name, error instanceof Error ? error.message : String(error));
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
if (scope) {
|
|
84
|
+
scope.pending--;
|
|
85
|
+
changed();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const read = visitorRead(answer);
|
|
89
|
+
if (read) {
|
|
90
|
+
scope?.visitors.set(name, read);
|
|
91
|
+
if (needsVisitor)
|
|
92
|
+
return needsVisitor(saidCall(name, read));
|
|
93
|
+
// In a render whose probe said nothing: an api route, whose budget turns
|
|
94
|
+
// waiting into "dynamic".
|
|
95
|
+
if (scope)
|
|
96
|
+
return new Promise(() => { });
|
|
97
|
+
throw new Error(saidCall(name) + ' reads ' + read.join(', ') + ', and a build has no visitor. ' +
|
|
98
|
+
'Nothing is being rendered here that could wait for one.');
|
|
99
|
+
}
|
|
100
|
+
const settled = answer;
|
|
101
|
+
if (settled.reply?.error !== undefined && settled.reply.refusalStatus === undefined) {
|
|
102
|
+
scope?.failed.set(name, settled.reply.error);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
scope?.answered.add(name);
|
|
106
|
+
}
|
|
107
|
+
// What it revalidated is nothing: there is no page in a browser to refresh.
|
|
108
|
+
return interpretReply(name, settled, () => { });
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* What of a visitor an answer says it read. A refusal to someone not signed
|
|
112
|
+
* in is one too: the build is not, and the visitor may be.
|
|
113
|
+
*/
|
|
114
|
+
export function visitorRead(answer) {
|
|
115
|
+
if ('visitor' in answer)
|
|
116
|
+
return answer.visitor.length ? answer.visitor : ['the visitor'];
|
|
117
|
+
if (answer.reply?.unauthenticated || answer.status === 401)
|
|
118
|
+
return ['who is signed in'];
|
|
119
|
+
if (answer.reply?.unauthorized || answer.status === 403)
|
|
120
|
+
return ['what they may do'];
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* A render's budget, not counting the time it spends waiting for the backend.
|
|
125
|
+
*
|
|
126
|
+
* The budget asks what a page cannot finish at build time, and a call being
|
|
127
|
+
* answered is not that: without this a query slower than two seconds - or
|
|
128
|
+
* just behind others in the queue - turned a page that could be stored into
|
|
129
|
+
* one that streams, depending on how busy the build machine was.
|
|
130
|
+
*/
|
|
131
|
+
export function buildBudget(ms, expire) {
|
|
132
|
+
const scope = scopes().getStore();
|
|
133
|
+
let timer;
|
|
134
|
+
let left = ms;
|
|
135
|
+
let since = 0;
|
|
136
|
+
const pause = () => {
|
|
137
|
+
if (timer === undefined)
|
|
138
|
+
return;
|
|
139
|
+
clearTimeout(timer);
|
|
140
|
+
timer = undefined;
|
|
141
|
+
// What an answer unblocked still has to render.
|
|
142
|
+
left = Math.max(left - (performance.now() - since), Math.min(ms, 100));
|
|
143
|
+
};
|
|
144
|
+
const update = () => {
|
|
145
|
+
if (scope?.pending) {
|
|
146
|
+
pause();
|
|
147
|
+
}
|
|
148
|
+
else if (timer === undefined) {
|
|
149
|
+
since = performance.now();
|
|
150
|
+
timer = setTimeout(expire, left);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
scope?.listeners.add(update);
|
|
154
|
+
update();
|
|
155
|
+
return {
|
|
156
|
+
stop() {
|
|
157
|
+
scope?.listeners.delete(update);
|
|
158
|
+
pause();
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/** Whether the route being rendered is waiting for the backend. */
|
|
163
|
+
export function awaitingBuildAnswer() {
|
|
164
|
+
return (scopes().getStore()?.pending ?? 0) > 0;
|
|
165
|
+
}
|
|
166
|
+
/** Told whenever that may have changed, until the function returned is called. */
|
|
167
|
+
export function onBuildAnswer(listener) {
|
|
168
|
+
const scope = scopes().getStore();
|
|
169
|
+
scope?.listeners.add(listener);
|
|
170
|
+
return () => scope?.listeners.delete(listener);
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=buildScope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildScope.js","sourceRoot":"","sources":["../src/buildScope.ts"],"names":[],"mappings":"AAAA,wBAAwB;AACxB,EAAE;AACF,2EAA2E;AAC3E,4EAA4E;AAC5E,6EAA6E;AAC7E,8EAA8E;AAC9E,6EAA6E;AAC7E,0EAA0E;AAC1E,SAAS;AACT,EAAE;AACF,6EAA6E;AAC7E,0EAA0E;AAC1E,+EAA+E;AAE/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAAE,cAAc,EAAsB,MAAM,gBAAgB,CAAA;AA+BnE,MAAM,QAAQ,GAAG,MAAM,C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GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAAA;IACxE,CAAC,CAAA;IAED,MAAM,MAAM,GAAG,GAAG,EAAE;QAClB,IAAI,KAAK,EAAE,OAAO,EAAE,CAAC;YACnB,KAAK,EAAE,CAAA;QACT,CAAC;aAAM,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YAC/B,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,CAAA;YACzB,KAAK,GAAG,UAAU,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;QAClC,CAAC;IACH,CAAC,CAAA;IAED,KAAK,EAAE,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IAC5B,MAAM,EAAE,CAAA;IAER,OAAO;QACL,IAAI;YACF,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;YAC/B,KAAK,EAAE,CAAA;QACT,CAAC;KACF,CAAA;AACH,CAAC;AAED,mEAAmE;AACnE,MAAM,UAAU,mBAAmB;IACjC,OAAO,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,EAAE,OAAO,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;AAChD,CAAC;AAED,kFAAkF;AAClF,MAAM,UAAU,aAAa,CAAC,QAAoB;IAChD,MAAM,KAAK,GAAG,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAA;IAEjC,KAAK,EAAE,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IAE9B,OAAO,GAAG,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;AAChD,CAAC","sourcesContent":["// rpc() during a build.\n//\n// A page whose data is the same for everyone can be stored, and for an app\n// with a backend that data is behind rpc(). So the build asks: each call is\n// answered by the backend's own process (buildProcess.ts), and the answer is\n// part of the page. A call that read the visitor - the session, a cookie, who\n// is signed in - has no answer a build could store, and the backend says so;\n// that call suspends, exactly as connection() would, and the page streams\n// there.\n//\n// Two copies of this module exist during a build: the plugin's, which starts\n// the backend and times each render, and the one inside the bundle, where\n// rpc() is. What they share is on the global, under keys both copies agree on.\n\nimport { AsyncLocalStorage } from 'node:async_hooks'\nimport { interpretReply, type HostCallReply } from './hostCalls.js'\n\n/** What the backend said: an answer, or what of a visitor the call read. */\nexport type BuildAnswer =\n | { visitor: string[] }\n | { status: number; reply: HostCallReply | null; text: string }\n\n/** Whatever answers a build's calls. */\nexport interface BuildAnswerer {\n ask(name: string, args: unknown[]): Promise<BuildAnswer>\n}\n\n/** What one route's renders called, and how many answers they are waiting for. */\ninterface Scope {\n pending: number\n listeners: Set<() => void>\n /** Answered, and in what the build stored. */\n answered: Set<string>\n /** Needs a visitor: the call, and what it read. */\n visitors: Map<string, string[]>\n /** Calls the backend failed, with its message. */\n failed: Map<string, string>\n}\n\nexport interface BuildCallsMade {\n answered: string[]\n /** Each already says what it read: `rpc(\"Cart.items\") (the session)`. */\n visitors: string[]\n failed: string[]\n}\n\nconst ANSWERER = Symbol.for('rsc-kit.build-answerer')\nconst SCOPES = Symbol.for('rsc-kit.build-scope')\n\nconst shared = globalThis as Record<symbol, unknown>\n\nfunction scopes(): AsyncLocalStorage<Scope> {\n return (shared[SCOPES] ??= new AsyncLocalStorage<Scope>()) as AsyncLocalStorage<Scope>\n}\n\n/** Whether this build has a backend to ask. */\nexport function buildAnswerer(): BuildAnswerer | null {\n return (shared[ANSWERER] as BuildAnswerer | undefined) ?? null\n}\n\n/** Have rpc() answered by this, until what is returned is called. */\nexport function installBuildAnswerer(answerer: BuildAnswerer): () => void {\n shared[ANSWERER] = answerer\n\n return () => {\n if (shared[ANSWERER] === answerer) delete shared[ANSWERER]\n }\n}\n\n/** The call as the build writes it, with what it read when that is why. */\nexport function saidCall(name: string, read: string[] = []): string {\n return 'rpc(' + JSON.stringify(name) + ')' + (read.length ? ' (' + read.join(', ') + ')' : '')\n}\n\n/**\n * Run one route's renders, noting the calls they make.\n *\n * The notes are read while the render may still be going - a render with a\n * hole never ends - so they are handed over as a function.\n */\nexport function withBuildCalls<T>(run: (made: () => BuildCallsMade) => Promise<T>): Promise<T> {\n const scope: Scope = { pending: 0, listeners: new Set(), answered: new Set(), visitors: new Map(), failed: new Map() }\n\n return scopes().run(scope, () =>\n run(() => ({\n answered: [...scope.answered].map((name) => saidCall(name)),\n visitors: [...scope.visitors].map(([name, read]) => saidCall(name, read)),\n failed: [...scope.failed].map(([name, message]) => saidCall(name) + ' failed: ' + message),\n })),\n )\n}\n\n/**\n * Answer one rpc() of a build.\n *\n * `needsVisitor` decides what a call that read the visitor does to the render\n * that made it: suspend, for the probe deciding what a page's shell is.\n * Outside any route - generateStaticParams, register() - there is no render\n * to leave a hole in, so it is an error that says what was read.\n */\nexport async function answerAtBuild(\n args: unknown[],\n needsVisitor?: (said: string) => Promise<unknown>,\n): Promise<unknown> {\n const answerer = buildAnswerer()\n const name = String(args[0])\n\n if (!answerer) throw new Error(saidCall(name) + ' cannot be answered: this build has no backend to ask.')\n\n const scope = scopes().getStore()\n const changed = () => {\n for (const listener of [...(scope?.listeners ?? [])]) listener()\n }\n\n if (scope) {\n scope.pending++\n changed()\n }\n\n let answer: BuildAnswer\n\n try {\n answer = await answerer.ask(name, args.slice(1))\n } catch (error) {\n scope?.failed.set(name, error instanceof Error ? error.message : String(error))\n\n throw error\n } finally {\n if (scope) {\n scope.pending--\n changed()\n }\n }\n\n const read = visitorRead(answer)\n\n if (read) {\n scope?.visitors.set(name, read)\n\n if (needsVisitor) return needsVisitor(saidCall(name, read))\n // In a render whose probe said nothing: an api route, whose budget turns\n // waiting into \"dynamic\".\n if (scope) return new Promise(() => {})\n\n throw new Error(\n saidCall(name) + ' reads ' + read.join(', ') + ', and a build has no visitor. ' +\n 'Nothing is being rendered here that could wait for one.',\n )\n }\n\n const settled = answer as Exclude<BuildAnswer, { visitor: string[] }>\n\n if (settled.reply?.error !== undefined && settled.reply.refusalStatus === undefined) {\n scope?.failed.set(name, settled.reply.error)\n } else {\n scope?.answered.add(name)\n }\n\n // What it revalidated is nothing: there is no page in a browser to refresh.\n return interpretReply(name, settled, () => {})\n}\n\n/**\n * What of a visitor an answer says it read. A refusal to someone not signed\n * in is one too: the build is not, and the visitor may be.\n */\nexport function visitorRead(answer: BuildAnswer): string[] | null {\n if ('visitor' in answer) return answer.visitor.length ? answer.visitor : ['the visitor']\n if (answer.reply?.unauthenticated || answer.status === 401) return ['who is signed in']\n if (answer.reply?.unauthorized || answer.status === 403) return ['what they may do']\n\n return null\n}\n\n/**\n * A render's budget, not counting the time it spends waiting for the backend.\n *\n * The budget asks what a page cannot finish at build time, and a call being\n * answered is not that: without this a query slower than two seconds - or\n * just behind others in the queue - turned a page that could be stored into\n * one that streams, depending on how busy the build machine was.\n */\nexport function buildBudget(ms: number, expire: () => void): { stop(): void } {\n const scope = scopes().getStore()\n let timer: ReturnType<typeof setTimeout> | undefined\n let left = ms\n let since = 0\n\n const pause = () => {\n if (timer === undefined) return\n\n clearTimeout(timer)\n timer = undefined\n // What an answer unblocked still has to render.\n left = Math.max(left - (performance.now() - since), Math.min(ms, 100))\n }\n\n const update = () => {\n if (scope?.pending) {\n pause()\n } else if (timer === undefined) {\n since = performance.now()\n timer = setTimeout(expire, left)\n }\n }\n\n scope?.listeners.add(update)\n update()\n\n return {\n stop() {\n scope?.listeners.delete(update)\n pause()\n },\n }\n}\n\n/** Whether the route being rendered is waiting for the backend. */\nexport function awaitingBuildAnswer(): boolean {\n return (scopes().getStore()?.pending ?? 0) > 0\n}\n\n/** Told whenever that may have changed, until the function returned is called. */\nexport function onBuildAnswer(listener: () => void): () => void {\n const scope = scopes().getStore()\n\n scope?.listeners.add(listener)\n\n return () => scope?.listeners.delete(listener)\n}\n"]}
|
package/dist/conformance.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ export interface ConformanceOptions {
|
|
|
18
18
|
result?: Schema;
|
|
19
19
|
}>;
|
|
20
20
|
defs?: Record<string, Schema>;
|
|
21
|
+
calls?: string[];
|
|
21
22
|
};
|
|
22
23
|
/** For a test: the fetch the client uses. */
|
|
23
24
|
fetch?: typeof fetch;
|
|
@@ -25,6 +26,8 @@ export interface ConformanceOptions {
|
|
|
25
26
|
purgePort?: number;
|
|
26
27
|
/** The adapter was started with its debug on (Laravel: APP_DEBUG=true; Go: RSC_DEBUG=1). */
|
|
27
28
|
debug?: boolean;
|
|
29
|
+
/** The command that answers a build's calls, for an adapter that has one; see conformanceBuild.ts. */
|
|
30
|
+
calls?: string[];
|
|
28
31
|
}
|
|
29
32
|
/** A purge as it arrived: what an adapter posted to CDN_PURGE_URL. */
|
|
30
33
|
export interface ArrivedPurge {
|
package/dist/conformance.js
CHANGED
|
@@ -32,11 +32,16 @@
|
|
|
32
32
|
// CDN keeping its pages itself. Start the adapter with CDN_PURGE_URL
|
|
33
33
|
// pointing at that port.
|
|
34
34
|
//
|
|
35
|
+
// --calls is the command that answers rpc() during a build, as a build runs
|
|
36
|
+
// it: "vendor/bin/testbench rsc:host-manifest --calls". Required of an
|
|
37
|
+
// adapter whose manifest has "calls".
|
|
38
|
+
//
|
|
35
39
|
// The test host's hostReply passes the same suite (conformance.test.ts), so
|
|
36
40
|
// a page test faking the backend fakes what a real adapter does.
|
|
37
41
|
import { readFileSync } from 'node:fs';
|
|
38
42
|
import { argv, exit, stderr, stdout } from 'node:process';
|
|
39
43
|
import { pathToFileURL } from 'node:url';
|
|
44
|
+
import { buildConformance } from './conformanceBuild.js';
|
|
40
45
|
import { httpHostCalls } from './hostCalls.js';
|
|
41
46
|
import { formInputs } from './hostInputs.js';
|
|
42
47
|
import { isNotFoundSignal } from './notFound.js';
|
|
@@ -594,11 +599,26 @@ export async function runConformance(options) {
|
|
|
594
599
|
const doFetch = options.fetch ?? globalThis.fetch;
|
|
595
600
|
const listening = options.purgePort ? await listenForPurges(options.purgePort) : null;
|
|
596
601
|
try {
|
|
597
|
-
return await suite();
|
|
602
|
+
return [...(await suite()), ...(await build())];
|
|
598
603
|
}
|
|
599
604
|
finally {
|
|
600
605
|
await listening?.close();
|
|
601
606
|
}
|
|
607
|
+
async function build() {
|
|
608
|
+
if (options.calls)
|
|
609
|
+
return buildConformance(options.calls);
|
|
610
|
+
const manifest = typeof options.manifest === 'string' ? JSON.parse(readFileSync(options.manifest, 'utf-8')) : options.manifest;
|
|
611
|
+
// It says it answers a build's calls, and nothing here asked it any.
|
|
612
|
+
return manifest?.calls
|
|
613
|
+
? [
|
|
614
|
+
{
|
|
615
|
+
case: 'a build: the command the manifest names is run',
|
|
616
|
+
ok: false,
|
|
617
|
+
detail: `the manifest's "calls" is ${JSON.stringify(manifest.calls)}: run the suite with --calls "<the manifest command> ${manifest.calls.join(' ')}"`,
|
|
618
|
+
},
|
|
619
|
+
]
|
|
620
|
+
: [];
|
|
621
|
+
}
|
|
602
622
|
async function suite() {
|
|
603
623
|
return await conformance(client(options.secret), {
|
|
604
624
|
purges: listening?.inbox,
|
|
@@ -708,11 +728,11 @@ if (import.meta.url === pathToFileURL(argv[1] ?? '').href || argv[1]?.endsWith('
|
|
|
708
728
|
const endpoint = flag('endpoint');
|
|
709
729
|
const secret = flag('secret');
|
|
710
730
|
if (!endpoint || !secret) {
|
|
711
|
-
stderr.write('usage: rsc-kit-conformance --endpoint <url> --secret <secret> --previous-secret <old> --purge-port <port> [--manifest rsc-host.json] [--debug]\n');
|
|
731
|
+
stderr.write('usage: rsc-kit-conformance --endpoint <url> --secret <secret> --previous-secret <old> --purge-port <port> [--manifest rsc-host.json] [--calls "<command>"] [--debug]\n');
|
|
712
732
|
exit(2);
|
|
713
733
|
}
|
|
714
734
|
const purgePort = flag('purge-port');
|
|
715
|
-
runConformance({ endpoint, secret, previousSecret: flag('previous-secret'), manifest: flag('manifest'), purgePort: purgePort ? Number(purgePort) : undefined, debug: argv.includes('--debug') })
|
|
735
|
+
runConformance({ endpoint, secret, previousSecret: flag('previous-secret'), manifest: flag('manifest'), purgePort: purgePort ? Number(purgePort) : undefined, debug: argv.includes('--debug'), calls: flag('calls')?.split(/\s+/).filter(Boolean) })
|
|
716
736
|
.then((results) => exit(report(results) ? 0 : 1))
|
|
717
737
|
.catch((error) => {
|
|
718
738
|
stderr.write('rsc-kit-conformance: ' + (error instanceof Error ? error.message : String(error)) + '\n');
|