@rsc-kit/core 0.44.0 → 0.45.1

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.
@@ -67,16 +67,18 @@ export declare const CASES: {
67
67
  readonly 'Conformance.update': string;
68
68
  readonly 'Conformance.redirect': 'Send the visitor to /login.';
69
69
  readonly 'Conformance.revalidate': 'Mark the region "orders" stale, and return "ok".';
70
+ readonly 'Conformance.revalidateThenRedirect': string;
70
71
  readonly 'Conformance.fail': 'Fail unexpectedly: an ordinary error, not a refusal.';
71
72
  readonly 'Conformance.authorization': "Return the request's Authorization header, as the renderer forwarded it.";
72
73
  readonly 'Conformance.cookie': "Return the request's Cookie header, as the renderer forwarded it.";
73
74
  readonly 'Conformance.ip': string;
74
75
  readonly 'Conformance.userAgent': string;
75
76
  readonly 'Conformance.login': 'Set a cookie named "conformance_login" on the response, the way a login sets its session cookie, and return "ok".';
76
- readonly 'Conformance.flash': 'Leave the one-time message "Saved." of kind "success" for the next page - the adapter\'s flash, which @rsc-kit/core/useFlash shows - and return "ok".';
77
+ readonly 'Conformance.flash': string;
77
78
  readonly 'Conformance.double': 'Take one integer and return it doubled. Arguments that do not fit - a string, none - fail it: a 500, not a refusal.';
78
79
  readonly 'Conformance.invalidNested': 'Refuse the input with errors on the nested field "address.city" and on the form itself, under "".';
79
80
  readonly '__rsc.middleware': string;
81
+ readonly 'Conformance.onPage': string;
80
82
  readonly 'Conformance.change': 'Say the name "conformance:changed" changed, the way the adapter does from a webhook, and return "ok".';
81
83
  readonly '__rsc.changed': string;
82
84
  };
@@ -47,7 +47,7 @@ import { formInputs } from './hostInputs.js';
47
47
  import { isNotFoundSignal } from './notFound.js';
48
48
  import { isActionRefusal } from './action.js';
49
49
  import { withRedirect } from './redirect.js';
50
- import { withRequest, withResponseDraft } from './request.js';
50
+ import { postedFrom, withRequest, withResponseDraft } from './request.js';
51
51
  import { FLASH_COOKIE, readFlash } from './flashCookie.js';
52
52
  import { ServerAuthenticationError, ServerAuthorizationError } from './js/errors.js';
53
53
  /**
@@ -75,6 +75,8 @@ export const CASES = {
75
75
  'for "7", as a unique rule ignoring the record being edited does. Otherwise return the key, a colon, and the name.',
76
76
  'Conformance.redirect': 'Send the visitor to /login.',
77
77
  'Conformance.revalidate': 'Mark the region "orders" stale, and return "ok".',
78
+ 'Conformance.revalidateThenRedirect': 'Mark the region "orders" stale, then send the visitor to /login - an action that changed what a layout shows, ' +
79
+ "then redirected (Laravel: Rsc::revalidate('all') then to_route()). The mark goes with the redirect.",
78
80
  'Conformance.fail': 'Fail unexpectedly: an ordinary error, not a refusal.',
79
81
  'Conformance.authorization': "Return the request's Authorization header, as the renderer forwarded it.",
80
82
  'Conformance.cookie': "Return the request's Cookie header, as the renderer forwarded it.",
@@ -83,18 +85,28 @@ export const CASES = {
83
85
  'Conformance.userAgent': "Return the visitor's User-Agent the way the app reads it - Laravel's $request->userAgent(), Go's rsckit.ClientUserAgent(ctx) - " +
84
86
  'which is the X-RSC-Client-User-Agent the renderer sent, not the User-Agent of the renderer calling.',
85
87
  'Conformance.login': 'Set a cookie named "conformance_login" on the response, the way a login sets its session cookie, and return "ok".',
86
- 'Conformance.flash': 'Leave the one-time message "Saved." of kind "success" for the next page - the adapter\'s flash, which @rsc-kit/core/useFlash shows - and return "ok".',
88
+ 'Conformance.flash': 'Leave the one-time message "Saved." of kind "success" for the next page - the adapter\'s flash, which @rsc-kit/core/useFlash shows - and return "ok". ' +
89
+ "Optional: a backend whose framework has a flash of its own (Laravel's session) leaves it out of its manifest, and its app uses that.",
87
90
  'Conformance.double': 'Take one integer and return it doubled. Arguments that do not fit - a string, none - fail it: a 500, not a refusal.',
88
91
  'Conformance.invalidNested': 'Refuse the input with errors on the nested field "address.city" and on the form itself, under "".',
89
92
  '__rsc.middleware': 'Guards, run in the order named, stopping at the first that does not pass - none at all passes: "conformance-allow" passes; ' +
90
93
  '"conformance-deny" refuses; "conformance-redirect" sends the visitor to /conformance-login; a name with no guard refuses; ' +
91
94
  '"conformance-page" passes only for the page /conformance/orders/{order} with order 7, read from the second argument ' +
92
95
  '{ url, params } the way a guard reads its page (Laravel: the request and its route; Go: rsckit.PageFrom(ctx)).',
96
+ 'Conformance.onPage': 'An action guarded by "conformance-page", as an app guards one with its own middleware, returning "ok". Its guard reads the page ' +
97
+ 'the action was posted from - the x-rsc-page header, { url, params } - not the call (Laravel: #[Middleware] on the method; ' +
98
+ 'Go: rsckit.PageFrom(ctx)). From no page it is refused.',
93
99
  'Conformance.change': 'Say the name "conformance:changed" changed, the way the adapter does from a webhook, and return "ok".',
94
100
  '__rsc.changed': 'Given { since: { name: version }, wait }: answer { versions } with every name whose version differs from since now, ' +
95
101
  'and no other. A name never changed is at 0. A change moves a version to the larger of one past it and the time in ' +
96
102
  'ms - never + 1 alone. May hold the call up to wait ms for one to differ; may answer at once.',
97
103
  };
104
+ /**
105
+ * Cases a backend leaves out when its framework already does the job: an
106
+ * adapter does not ship a second way to do what its framework does. Left out
107
+ * of the manifest, they are not checked.
108
+ */
109
+ const OPTIONAL = new Set(['Conformance.flash']);
98
110
  const INSTANT = Date.parse('2026-01-02T03:04:05Z');
99
111
  /** Two JSON values alike, whatever order an object's keys were written in. */
100
112
  function sameValue(a, b) {
@@ -152,7 +164,7 @@ export async function conformance(call, options) {
152
164
  await check('the manifest lists every Conformance function', async () => {
153
165
  if (!types)
154
166
  return;
155
- const missing = Object.keys(CASES).filter((n) => !n.startsWith('__') && !types.functions?.includes(n));
167
+ const missing = Object.keys(CASES).filter((n) => !n.startsWith('__') && !OPTIONAL.has(n) && !types.functions?.includes(n));
156
168
  expect(missing.length === 0, 'not in functions: ' + missing.join(', '));
157
169
  });
158
170
  await check('the manifest\'s actions is an object, even with none', async () => {
@@ -269,6 +281,17 @@ export async function conformance(call, options) {
269
281
  expect(value === 'ok', 'got ' + JSON.stringify(value));
270
282
  expect(revalidated.some((r) => r.includes('orders')), 'revalidated ' + JSON.stringify(revalidated));
271
283
  });
284
+ await check('a revalidation rides with a redirect', async () => {
285
+ // Answered without it, a profile form that renamed the visitor and then
286
+ // redirected left the sidebar showing the old name.
287
+ const before = revalidated.length;
288
+ const taken = await withRedirect(async (seen) => {
289
+ await rejection(() => call('Conformance.revalidateThenRedirect'));
290
+ return seen();
291
+ });
292
+ expect(taken?.location === '/login', 'redirect ' + JSON.stringify(taken));
293
+ expect(revalidated.slice(before).some((r) => r.includes('orders')), 'the redirect carried no revalidate: ' + JSON.stringify(revalidated.slice(before)));
294
+ });
272
295
  await check('an unexpected failure is an error, not a refusal', async () => {
273
296
  const error = (await rejection(() => call('Conformance.fail')));
274
297
  expect(error instanceof Error && !(error instanceof ServerAuthenticationError) && !(error instanceof ServerAuthorizationError) && !error.refusalStatus && !isNotFoundSignal(error), 'got ' + String(error));
@@ -311,6 +334,10 @@ export async function conformance(call, options) {
311
334
  expect(cookies.some((c) => c.startsWith('conformance_login=')), 'Set-Cookie ' + JSON.stringify(cookies));
312
335
  });
313
336
  await check('a flash is a cookie the browser reads, kept after one the visitor still carries', async () => {
337
+ // Left out of the manifest by a backend whose framework flashes itself.
338
+ // Without a manifest there is nothing to say it was left out on purpose.
339
+ if (types && !types.functions?.includes('Conformance.flash'))
340
+ return;
314
341
  // A message from the page before, not yet shown, is in the visitor's
315
342
  // cookie; the new one goes after it rather than over it.
316
343
  const carried = encodeURIComponent(JSON.stringify([{ message: 'Earlier.', kind: 'info' }]));
@@ -404,6 +431,23 @@ export async function conformance(call, options) {
404
431
  const none = await call('__rsc.middleware', ['conformance-page']).catch((e) => e);
405
432
  expect(none !== true, 'no page answered true');
406
433
  });
434
+ // An action's guards judge the page it was posted from, which the renderer
435
+ // names in x-rsc-page: Laravel's `signed` checks the link the form was on.
436
+ const fromPage = (page) => withRequest(new Request('https://app.test/'), () => {
437
+ postedFrom(page);
438
+ return call('Conformance.onPage');
439
+ }).catch((e) => e);
440
+ await check("an action's guard reads the page it was posted from", async () => {
441
+ const value = await fromPage({ url: 'https://app.test/conformance/orders/7?tab=items', params: { order: '7' } });
442
+ expect(value === 'ok', 'the page it was posted from was refused: ' + String(value));
443
+ const other = await fromPage({ url: 'https://app.test/conformance/orders/8', params: { order: '8' } });
444
+ expect(other !== 'ok', 'another order answered ok');
445
+ const none = await fromPage(null);
446
+ expect(none !== 'ok', 'no page answered ok');
447
+ // Non-ASCII arrives escaped in the header, and is read back as it was.
448
+ const named = await fromPage({ url: 'https://app.test/conformance/orders/7', params: { order: '7', note: 'café' } });
449
+ expect(named === 'ok', 'a page with a non-ASCII param was refused: ' + String(named));
450
+ });
407
451
  await check('a guard nobody registered refuses: fail closed', async () => {
408
452
  const value = await call('__rsc.middleware', ['conformance-nobody-registered-this']).catch((e) => e);
409
453
  expect(value !== true, 'it answered true');
@@ -605,9 +649,12 @@ export async function runConformance(options) {
605
649
  await listening?.close();
606
650
  }
607
651
  async function build() {
608
- if (options.calls)
609
- return buildConformance(options.calls);
610
652
  const manifest = typeof options.manifest === 'string' ? JSON.parse(readFileSync(options.manifest, 'utf-8')) : options.manifest;
653
+ if (options.calls) {
654
+ const functions = manifest?.functions;
655
+ const leftOut = Array.isArray(functions) ? [...OPTIONAL].filter((name) => !functions.includes(name)) : [];
656
+ return buildConformance(options.calls, { leftOut });
657
+ }
611
658
  // It says it answers a build's calls, and nothing here asked it any.
612
659
  return manifest?.calls
613
660
  ? [