@shivaedev/test-story 0.3.0 → 0.4.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +43 -7
  3. package/dist/server/domEnvironment.config.d.ts.map +1 -1
  4. package/dist/server/domEnvironment.config.js +38 -28
  5. package/dist/server/domEnvironment.config.js.map +1 -1
  6. package/dist/server/domFetch.d.ts.map +1 -1
  7. package/dist/server/domFetch.js +18 -1
  8. package/dist/server/domFetch.js.map +1 -1
  9. package/dist/server/domNavigate.d.ts +2 -0
  10. package/dist/server/domNavigate.d.ts.map +1 -0
  11. package/dist/server/domNavigate.js +10 -0
  12. package/dist/server/domNavigate.js.map +1 -0
  13. package/dist/server/internal/documentRedirect.d.ts +7 -0
  14. package/dist/server/internal/documentRedirect.d.ts.map +1 -0
  15. package/dist/server/internal/documentRedirect.js +26 -0
  16. package/dist/server/internal/documentRedirect.js.map +1 -0
  17. package/dist/server/internal/documentRequest.d.ts +6 -0
  18. package/dist/server/internal/documentRequest.d.ts.map +1 -0
  19. package/dist/server/internal/documentRequest.js +21 -0
  20. package/dist/server/internal/documentRequest.js.map +1 -0
  21. package/dist/server/internal/documentResponse.d.ts +4 -0
  22. package/dist/server/internal/documentResponse.d.ts.map +1 -0
  23. package/dist/server/internal/documentResponse.js +42 -0
  24. package/dist/server/internal/documentResponse.js.map +1 -0
  25. package/dist/server/internal/documentTransport.d.ts +3 -0
  26. package/dist/server/internal/documentTransport.d.ts.map +1 -0
  27. package/dist/server/internal/documentTransport.js +40 -0
  28. package/dist/server/internal/documentTransport.js.map +1 -0
  29. package/dist/server/internal/errors.d.ts +1 -0
  30. package/dist/server/internal/errors.d.ts.map +1 -1
  31. package/dist/server/internal/errors.js +3 -0
  32. package/dist/server/internal/errors.js.map +1 -1
  33. package/dist/server/internal/routeOutbound.d.ts +1 -0
  34. package/dist/server/internal/routeOutbound.d.ts.map +1 -1
  35. package/dist/server/internal/routeOutbound.js +20 -4
  36. package/dist/server/internal/routeOutbound.js.map +1 -1
  37. package/dist/server/internal/storyIt.js +1 -1
  38. package/dist/server/internal/storyIt.js.map +1 -1
  39. package/package.json +8 -1
  40. package/src/server/domEnvironment.config.ts +39 -28
  41. package/src/server/domFetch.ts +21 -1
  42. package/src/server/domNavigate.ts +9 -0
  43. package/src/server/internal/documentRedirect.ts +28 -0
  44. package/src/server/internal/documentRequest.ts +26 -0
  45. package/src/server/internal/documentResponse.ts +51 -0
  46. package/src/server/internal/documentTransport.ts +41 -0
  47. package/src/server/internal/errors.ts +9 -0
  48. package/src/server/internal/routeOutbound.ts +22 -4
  49. package/src/server/internal/storyIt.ts +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0 - 2026-10-17
4
+
5
+ ### Added
6
+
7
+ - `domNavigate(url?: string | URL): Promise<Response>` follows document redirects, keeps cookies and loads the final HTML for DOM stories, including social sign-in through an outbound OAuth fake.
8
+
9
+ ### Fixed
10
+
11
+ - Keep Node fetch for in-process hosts and route the DOM document's asynchronous HTTP requests through the same outbound fakes. Unrouted external hosts fail DOM stories with the hostname, even when application code catches the request error.
12
+ - Pass the `domFetch` result explicitly to browser clients (for Better Auth, `fetchOptions.customFetchImpl`); global fetch now belongs to the host. Happy-dom remains an optional peer for the DOM environment.
13
+
3
14
  ## 0.3.0 - 2026-10-17
4
15
 
5
16
  ### Added
package/README.md CHANGED
@@ -107,7 +107,7 @@ This is the bakery kit this package tests itself with. `emptyBakery`, `stillWait
107
107
  | Hook | Says |
108
108
  | --- | --- |
109
109
  | `name` | What the engine is called in failures, "the bakery", and the kit's genre, "Bakery Story". |
110
- | `outbound` | Optional. A map of exact hostnames to fake server origins for the host's Node built-in `fetch`. |
110
+ | `outbound` | Optional. A map of exact hostnames to fake server origins for Node fetch and the DOM environment's document requests. |
111
111
  | `around(effect)` | Optional. Wraps creation, staged seeding and the body; worker Layer acquisition remains outside it. It may require the worker services and per-test Scope. |
112
112
  | `clock` | Optional. `"test"` by default, or `"live"` for HTTP and DOM stories. A story’s `options.clock` takes precedence. |
113
113
  | `create()` | A fresh engine. Every story starts from a new one. |
@@ -142,9 +142,9 @@ const app = storyKit({
142
142
  });
143
143
  ```
144
144
 
145
- `appDefinition` holds the application's kit hooks, and `fakeUrl` is the listening fake server's HTTP or HTTPS origin, such as `http://127.0.0.1:4321`. The map matches exact URL hostnames, across ports. It replaces the destination origin while keeping the request's path, query, method, headers and body. For example, a POST to `https://github.com/login/oauth/access_token` reaches the fake's `/login/oauth/access_token`. Other hostnames pass through the previous dispatcher unchanged.
145
+ `appDefinition` holds the application's kit hooks, and `fakeUrl` is the listening fake server's HTTP or HTTPS origin, such as `http://127.0.0.1:4321`. The map matches exact URL hostnames, across ports. It replaces the destination origin while keeping the request's path, query, method, headers and body. For example, a POST to `https://github.com/login/oauth/access_token` reaches the fake's `/login/oauth/access_token`. In Node stories, other hostnames pass through the previous dispatcher unchanged. In the DOM environment, only loopback hosts (`localhost`, IPv4 loopback addresses and `[::1]`) may pass through without a route; every other hostname must have an `outbound` fake. A refused request fails the story and names its hostname, even when application code catches the fetch error.
146
146
 
147
- Routing uses the global dispatcher that Node's built-in `fetch` already uses. It is installed before `around`, creation, traits and the body, stays active during scoped cleanup, and restores the previous dispatcher after each story, including a failing story. Stories within one worker must run one at a time because that dispatcher is shared; parallelism belongs between workers or inside one story. Routing affects host-side Node fetches.
147
+ Routing uses the global dispatcher that Node's built-in `fetch` already uses. It is installed before `around`, creation, traits and the body, stays active during scoped cleanup, and restores the previous dispatcher after each story, including a failing story. Stories within one worker must run one at a time because that dispatcher is shared; parallelism belongs between workers or inside one story. The DOM environment uses the same routing for host-side Node fetch and document requests, including redirect destinations. It refuses external hosts even when the kit omits `outbound`.
148
148
 
149
149
  ### 2. Once per feature: traits and verbs
150
150
 
@@ -303,13 +303,14 @@ export default defineConfig({
303
303
  });
304
304
  ```
305
305
 
306
- The environment runs happy-dom, then restores Node's global `Request`, `Response`, `Headers`, `AbortController` and `AbortSignal` for the host and client request construction. Better Auth can emit `Set-Cookie`, and the host can read incoming cookie headers. Fetch still runs through happy-dom's internal window and cookie jar. The environment translates Node `Headers` and `Request` inputs for that fetch, including requests made by Better Auth's client. Client responses remain happy-dom responses: inspect their status, headers and body methods rather than comparing them with the global Node constructors. The starter's [Vitest config](https://github.com/ShivaeDev/platform/blob/main/examples/starter/vitest.config.ts) selects this environment only for its DOM project while retaining the shared test projects. Use Vitest's standard forks or threads pool.
306
+ The environment keeps Node's global `fetch`, `Request`, `Response`, `Headers`, `AbortController` and `AbortSignal` for the in-process host. The separate `window` keeps happy-dom's constructors and cookie jar. Its asynchronous HTTP requests use Node fetch through the same `outbound` dispatcher, including XMLHttpRequest. Synchronous document HTTP requests are refused. The starter's [Vitest config](https://github.com/ShivaeDev/platform/blob/main/examples/starter/vitest.config.ts) selects this environment only for its DOM project while retaining the shared test projects. Use Vitest's standard forks or threads pool.
307
307
 
308
- `domFetch(baseUrl)` from `@shivaedev/test-story/domFetch.ts` sets the current window's location to the real HTTP host and returns its bound fetch, resolving relative request strings against that URL. The window keeps and sends cookies, including HttpOnly session cookies. Keep the default `same-origin` credentials policy for this same-origin setup. Set the page origin before creating an auth client or rendering a screen; application code can use the global fetch of that same window.
308
+ `domFetch(baseUrl)` from `@shivaedev/test-story/domFetch.ts` sets the document's location to the real HTTP host and returns a document fetch that resolves relative request strings against that URL. It translates Node `Request` and `Headers` inputs for happy-dom. Responses have happy-dom's status, headers and body methods; they are not instances of Node's global `Response`. The document keeps and sends cookies, including HttpOnly session cookies. Keep the default `same-origin` credentials policy for same-origin requests, and pass this fetch explicitly to browser clients, such as Better Auth's `fetchOptions.customFetchImpl`. The host continues to use global Node fetch without browser cookies.
309
309
 
310
310
  Call it in the kit's `create` hook after starting the host:
311
311
 
312
312
  ```ts
313
+ import { createAuthClient } from "better-auth/react";
313
314
  import { domFetch } from "@shivaedev/test-story/domFetch.ts";
314
315
 
315
316
  const cookieApp = storyKit({
@@ -317,12 +318,14 @@ const cookieApp = storyKit({
317
318
  clock: "live",
318
319
  *create() {
319
320
  const { baseUrl } = yield* startHost();
320
- return { fetch: domFetch(baseUrl) };
321
+ const fetch = domFetch(baseUrl);
322
+ const client = createAuthClient({ baseURL: baseUrl, fetchOptions: { customFetchImpl: fetch } });
323
+ return { client, fetch };
321
324
  },
322
325
  layer: postgres,
323
326
  name: "cookie auth",
324
327
  stages: ["account"],
325
- verbs: ({ fetch }, story) => authVerbs(fetch, story.tell),
328
+ verbs: ({ client, fetch }, story) => authVerbs(client, fetch, story.tell),
326
329
  });
327
330
  ```
328
331
 
@@ -343,6 +346,39 @@ cookieApp.it("sign-up and sign-in keep their cookie sessions against the Postgre
343
346
 
344
347
  This story proves Better Auth cookie sessions across real loopback HTTP requests from happy-dom with an in-process PostgreSQL host. It also reads sessions through Better Auth's client and checks the fetch response's status, headers and JSON body.
345
348
 
349
+ #### Social sign-in through an outbound provider
350
+
351
+ Happy-dom's detached window records a location change without loading the next document. `domNavigate(url = window.location.href)` from `@shivaedev/test-story/domNavigate.ts` performs that load: it follows HTTP redirects through the document transport, keeps cookies for each destination, sets the final location, writes the returned HTML and returns the happy-dom response. Call it after the sign-in client has set the provider location, and before checking the returned page. An unrouted external redirect fails with its hostname.
352
+
353
+ The starter's [social application kit](https://github.com/ShivaeDev/platform/blob/main/examples/starter/src/test-support/socialApp.ts) runs a fixture Better Auth host on real PostgreSQL, with a rendered sign-in button and a fake GitHub provider. Its `outbound` map routes both `github.com` and `api.github.com` to the fake's loopback origin. The fake's authorize endpoint returns the application's callback URL with the received state and a test code. The real callback exchanges that code and fetches the user against the fake, creates the account and session in PostgreSQL, and redirects to the signed-in page.
354
+
355
+ ```ts
356
+ import { domNavigate } from "@shivaedev/test-story/domNavigate.ts";
357
+
358
+ const socialApp = storyKit({
359
+ ...socialDefinition,
360
+ outbound: { "github.com": fakeUrl, "api.github.com": fakeUrl },
361
+ verbs: (engine, story) => ({
362
+ signInWithGitHub: () => Effect.promise(async () => {
363
+ story.tell("Alice clicks Sign in with GitHub");
364
+ await engine.screen.click();
365
+ await domNavigate();
366
+ }),
367
+ signedInPageIs: (email: string) => Effect.promise(async () => {
368
+ expect(document.querySelector("h1")?.textContent).toBe(`Signed in as ${email}`);
369
+ expect(await (await engine.fetch("/api/auth/get-session")).json()).toMatchObject({ user: { email } });
370
+ }),
371
+ }),
372
+ });
373
+
374
+ socialApp.it("a social sign-in returns with a cookie session", [], function* ({ signInWithGitHub, signedInPageIs }) {
375
+ yield* signInWithGitHub();
376
+ yield* signedInPageIs("alice@social.test");
377
+ });
378
+ ```
379
+
380
+ `socialDefinition` starts the host and renders the screen with a Better Auth client whose `customFetchImpl` is `domFetch(baseUrl)`. The [story](https://github.com/ShivaeDev/platform/blob/main/examples/starter/src/server/socialSessions.dom.spec.ts) also checks the authorize, token and user requests at the fake, persisted PostgreSQL rows, and an HttpOnly session cookie read through a later document request. It does not copy cookies into request headers.
381
+
346
382
  `domFetch` uses the current window and its cookie jar; it does not isolate multiple users. Run stories using that window serially, and let the application's cleanup end its sessions, restore the page location and close its host. Cookies follow hostnames, not listener ports, so a different loopback port alone does not isolate accounts.
347
383
 
348
384
  ### Install and limits
@@ -1 +1 @@
1
- {"version":3,"file":"domEnvironment.config.d.ts","sourceRoot":"","sources":["../../src/server/domEnvironment.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAEvE,QAAA,MAAM,cAAc,EAAE,WAmCrB,CAAC;eAEa,cAAc"}
1
+ {"version":3,"file":"domEnvironment.config.d.ts","sourceRoot":"","sources":["../../src/server/domEnvironment.config.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,WAAW,EAAkB,MAAM,gBAAgB,CAAC;AAElE,QAAA,MAAM,cAAc,EAAE,WA4CrB,CAAC;eAEa,cAAc"}
@@ -1,37 +1,47 @@
1
- import { builtinEnvironments } from "vitest/runtime";
1
+ import { createRequire } from "node:module";
2
+ import { GlobalWindow } from "happy-dom";
3
+ import { populateGlobal } from "vitest/runtime";
2
4
  const domEnvironment = {
3
5
  name: "test-story-dom",
4
- async setup(global, options) {
5
- const { AbortController, AbortSignal, Headers, Request, Response } = global;
6
- const environment = await builtinEnvironments["happy-dom"].setup(global, options);
7
- // Vitest's setters also replace happy-dom's internal classes, which its fetch needs.
8
- for (const [name, value] of Object.entries({ AbortController, AbortSignal, Headers, Request, Response })) {
6
+ async setup(global, { happyDOM = {} }) {
7
+ // Vitest loads environments outside the project's source resolution conditions.
8
+ const load = createRequire(import.meta.url);
9
+ const { documentTransport } = load("#internal/documentTransport.ts");
10
+ const { installOutbound } = load("#internal/routeOutbound.ts");
11
+ const native = Object.fromEntries(["fetch", "AbortController", "AbortSignal", "Headers", "Request", "Response"].map((name) => [name, Reflect.get(global, name)]));
12
+ const restoreOutbound = await installOutbound({}, true);
13
+ const win = new GlobalWindow({
14
+ ...happyDOM,
15
+ console: global.console,
16
+ settings: { ...happyDOM.settings, disableErrorCapturing: true },
17
+ url: happyDOM.url ?? "http://localhost:3000",
18
+ });
19
+ win.happyDOM.settings.fetch.interceptor = documentTransport(global.fetch);
20
+ const { keys, originals } = populateGlobal(global, win, { bindFunctions: true });
21
+ for (const [name, value] of Object.entries(native)) {
9
22
  Object.defineProperty(global, name, { configurable: true, value, writable: true });
10
23
  }
11
- const transport = global.fetch.bind(global);
12
- Object.defineProperty(global, "fetch", {
13
- configurable: true,
14
- value: (input, init) => {
15
- if (input instanceof Request) {
16
- const request = new Request(input, init);
17
- return transport(request.url, {
18
- body: request.body,
19
- cache: request.cache,
20
- credentials: request.credentials,
21
- headers: [...request.headers],
22
- method: request.method,
23
- mode: request.mode,
24
- redirect: request.redirect,
25
- referrer: request.referrer,
26
- referrerPolicy: request.referrerPolicy,
27
- signal: request.signal,
28
- });
24
+ Object.defineProperty(global, "window", { configurable: true, value: win, writable: true });
25
+ Object.defineProperty(win.document, "defaultView", { configurable: true, get: () => win });
26
+ Object.defineProperty(global, Symbol.for("test-story.dom"), { configurable: true, value: true });
27
+ return {
28
+ async teardown(target) {
29
+ try {
30
+ await win.happyDOM.abort();
31
+ win.close();
32
+ }
33
+ finally {
34
+ for (const key of keys) {
35
+ Reflect.deleteProperty(target, key);
36
+ }
37
+ for (const [key, descriptor] of originals) {
38
+ Object.defineProperty(target, key, descriptor);
39
+ }
40
+ Reflect.deleteProperty(target, Symbol.for("test-story.dom"));
41
+ restoreOutbound();
29
42
  }
30
- return transport(input, init?.headers instanceof Headers ? { ...init, headers: [...init.headers] } : init);
31
43
  },
32
- writable: true,
33
- });
34
- return environment;
44
+ };
35
45
  },
36
46
  viteEnvironment: "client",
37
47
  };
@@ -1 +1 @@
1
- {"version":3,"file":"domEnvironment.config.js","sourceRoot":"","sources":["../../src/server/domEnvironment.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAoB,MAAM,gBAAgB,CAAC;AAEvE,MAAM,cAAc,GAAgB;IACnC,IAAI,EAAE,gBAAgB;IACtB,KAAK,CAAC,KAAK,CAAC,MAAyB,EAAE,OAAO;QAC7C,MAAM,EAAE,eAAe,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC;QAC5E,MAAM,WAAW,GAAG,MAAM,mBAAmB,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAClF,qFAAqF;QACrF,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,EAAE,eAAe,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC;YAC1G,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,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AM,CAAC,cAAc,CAAC,MAAM,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;oBAChD,CAAC;oBACD,OAAO,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC;oBAC7D,eAAe,EAAE,CAAC;gBACnB,CAAC;YACF,CAAC;SACD,CAAC;IACH,CAAC;IACD,eAAe,EAAE,QAAQ;CACzB,CAAC;AAEF,eAAe,cAAc,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"domFetch.d.ts","sourceRoot":"","sources":["../../src/server/domFetch.ts"],"names":[],"mappings":"AAAA,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,UAAU,CAAC,KAAK,CAKjE"}
1
+ {"version":3,"file":"domFetch.d.ts","sourceRoot":"","sources":["../../src/server/domFetch.ts"],"names":[],"mappings":"AAAA,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,UAAU,CAAC,KAAK,CAyBjE"}
@@ -2,6 +2,23 @@ export function domFetch(baseUrl) {
2
2
  const base = new URL(baseUrl);
3
3
  window.location.href = base.href;
4
4
  const transport = window.fetch.bind(window);
5
- return (input, init) => transport(typeof input === "string" ? new URL(input, base).href : input, init);
5
+ return (input, init) => {
6
+ if (input instanceof Request) {
7
+ const request = new Request(input, init);
8
+ return transport(request.url, {
9
+ body: request.body,
10
+ cache: request.cache,
11
+ credentials: request.credentials,
12
+ headers: [...request.headers],
13
+ method: request.method,
14
+ mode: request.mode,
15
+ redirect: request.redirect,
16
+ referrer: request.referrer,
17
+ referrerPolicy: request.referrerPolicy,
18
+ signal: request.signal,
19
+ });
20
+ }
21
+ return transport(typeof input === "string" ? new URL(input, base).href : input, init?.headers instanceof Headers ? { ...init, headers: [...init.headers] } : init);
22
+ };
6
23
  }
7
24
  //# sourceMappingURL=domFetch.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"domFetch.js","sourceRoot":"","sources":["../../src/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M;gBACtB,IAAI,EAAE,OAAO,CAAC,IAAI;gBAClB,QAAQ,EAAE,OAAO,CAAC,QAAQ;gBAC1B,QAAQ,EAAE,OAAO,CAAC,QAAQ;gBAC1B,cAAc,EAAE,OAAO,CAAC,cAAc;gBACtC,MAAM,EAAE,OAAO,CAAC,MAAM;aACtB,CAAC,CAAC;QACJ,CAAC;QACD,OAAO,SAAS,CACf,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,EAC7D,IAAI,EAAE,OAAO,YAAY,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CACjF,CAAC;IACH,CAAC,CAAC;AACH,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function domNavigate(url?: string | URL): Promise<Response>;
2
+ //# sourceMappingURL=domNavigate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domNavigate.d.ts","sourceRoot":"","sources":["../../src/server/domNavigate.ts"],"names":[],"mappings":"AAAA,wBAAsB,WAAW,CAAC,GAAG,GAAE,MAAM,GAAG,GAA0B,GAAG,OAAO,CAAC,QAAQ,CAAC,CAQ7F"}
@@ -0,0 +1,10 @@
1
+ export async function domNavigate(url = window.location.href) {
2
+ const response = await window.fetch(new URL(url, window.location.href), { credentials: "include" });
3
+ const html = await response.clone().text();
4
+ window.location.href = response.url;
5
+ document.open();
6
+ document.write(html);
7
+ document.close();
8
+ return response;
9
+ }
10
+ //# sourceMappingURL=domNavigate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domNavigate.js","sourceRoot":"","sources":["../../src/server/domNavigate.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,GAAG,GAAiB,MAAM,CAAC,QAAQ,CAAC,IAAI;IACzE,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,CAAC;IACpG,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC;IAC3C,MAAM,CAAC,QAAQ,CAAC,IAAI,GAAG,QAAQ,CAAC,GAAG,CAAC;IACpC,QAAQ,CAAC,IAAI,EAAE,CAAC;IAChB,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,CAAC;IACjB,OAAO,QAAQ,CAAC;AACjB,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { Request } from "happy-dom";
2
+ import type BrowserWindow from "happy-dom/lib/window/BrowserWindow.js";
3
+ export declare function documentRedirect(request: Request, window: BrowserWindow, target: URL, status: number, body: Buffer<ArrayBuffer> | null): {
4
+ body: Buffer<ArrayBuffer> | null;
5
+ request: Request;
6
+ };
7
+ //# sourceMappingURL=documentRedirect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"documentRedirect.d.ts","sourceRoot":"","sources":["../../../src/server/internal/documentRedirect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,KAAK,aAAa,MAAM,uCAAuC,CAAC;AAEvE,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,WAAW,CAAC,GAAG,IAAI;IAcrI,IAAI;IACJ,OAAO;EASR"}
@@ -0,0 +1,26 @@
1
+ export function documentRedirect(request, window, target, status, body) {
2
+ const headers = new window.Headers(request.headers);
3
+ if (target.origin !== new URL(request.url).origin) {
4
+ for (const name of ["authorization", "cookie", "proxy-authorization"]) {
5
+ headers.delete(name);
6
+ }
7
+ }
8
+ const get = (status === 303 && request.method !== "HEAD") || ([301, 302].includes(status) && request.method === "POST");
9
+ if (get) {
10
+ headers.delete("content-type");
11
+ headers.delete("content-length");
12
+ }
13
+ const nextBody = get ? null : body;
14
+ return {
15
+ body: nextBody,
16
+ request: new window.Request(target.href, {
17
+ body: nextBody,
18
+ credentials: request.credentials,
19
+ headers,
20
+ method: get ? "GET" : request.method,
21
+ redirect: request.redirect,
22
+ signal: request.signal,
23
+ }),
24
+ };
25
+ }
26
+ //# sourceMappingURL=documentRedirect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"documentRedirect.js","sourceRoot":"","sources":["../../../src/server/internal/documentRedirect.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,gBAAgB,CAAC,OAAgB,EAAE,MAAqB,EAAE,MAAW,EAAE,MAAc,EAAE,IAAgC;IACtI,MAAM,OAAO,GAAG,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACpD,IAAI,MAAM,CAAC,MAAM,KAAK,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC;QACnD,KAAK,MAAM,IAAI,IAAI,CAAC,eAAe,EAAE,QAAQ,EAAE,qBAAqB,CAAC,EAAE,CAAC;YACvE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtB,CAAC;IACF,CAAC;IACD,MAAM,GAAG,GAAG,CAAC,MAAM,KAAK,GAAG,IAAI,OAAO,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC;IACxH,IAAI,GAAG,EAAE,CAAC;QACT,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;QAC/B,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;IAClC,CAAC;IACD,MAAM,QAAQ,GAAG,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IACnC,OAAO;QACN,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE;YACxC,IAAI,EAAE,QAAQ;YACd,WAAW,EAAE,OAAO,CAAC,WAAW;YAChC,OAAO;YACP,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM;YACpC,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,MAAM,EAAE,OAAO,CAAC,MAAM;SACtB,CAAC;KACF,CAAC;AACH,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { Request } from "happy-dom";
2
+ export declare function documentRequest(fetch: typeof globalThis.fetch, request: Request, body: Buffer<ArrayBuffer> | null, headers: Readonly<Record<string, string>>): Promise<{
3
+ release: () => void;
4
+ response: Response;
5
+ }>;
6
+ //# sourceMappingURL=documentRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"documentRequest.d.ts","sourceRoot":"","sources":["../../../src/server/internal/documentRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEzC,wBAAsB,eAAe,CACpC,KAAK,EAAE,OAAO,UAAU,CAAC,KAAK,EAC9B,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,MAAM,CAAC,WAAW,CAAC,GAAG,IAAI,EAChC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;;;GAmBzC"}
@@ -0,0 +1,21 @@
1
+ export async function documentRequest(fetch, request, body, headers) {
2
+ const controller = new AbortController();
3
+ function abort() {
4
+ controller.abort(request.signal.reason);
5
+ }
6
+ function release() {
7
+ request.signal.removeEventListener("abort", abort);
8
+ }
9
+ if (request.signal.aborted) {
10
+ abort();
11
+ }
12
+ request.signal.addEventListener("abort", abort, { once: true });
13
+ try {
14
+ return { release, response: await fetch(request.url, { body, headers, method: request.method, redirect: "manual", signal: controller.signal }) };
15
+ }
16
+ catch (error) {
17
+ release();
18
+ throw error;
19
+ }
20
+ }
21
+ //# sourceMappingURL=documentRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"documentRequest.js","sourceRoot":"","sources":["../../../src/server/internal/documentRequest.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,KAAK,UAAU,eAAe,CACpC,KAA8B,EAC9B,OAAgB,EAChB,IAAgC,EAChC,OAAyC;IAEzC,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;IACzC,SAAS,KAAK;QACb,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACzC,CAAC;IACD,SAAS,OAAO;QACf,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IACpD,CAAC;IACD,IAAI,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAC5B,KAAK,EAAE,CAAC;IACT,CAAC;IACD,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAChE,IAAI,CAAC;QACJ,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC;IAClJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,OAAO,EAAE,CAAC;QACV,MAAM,KAAK,CAAC;IACb,CAAC;AACF,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { Headers, IBrowserFrame, Request } from "happy-dom";
2
+ export declare function documentResponseHeaders(response: Response, request: Request, frame: IBrowserFrame): Headers;
3
+ export declare function documentResponse(response: Response, request: Request, frame: IBrowserFrame, release: () => void, redirected: boolean, headers: Headers): import("happy-dom").Response;
4
+ //# sourceMappingURL=documentResponse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"documentResponse.d.ts","sourceRoot":"","sources":["../../../src/server/internal/documentResponse.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGjE,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,aAAa,WAQjG;AAED,wBAAgB,gBAAgB,CAC/B,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,aAAa,EACpB,OAAO,EAAE,MAAM,IAAI,EACnB,UAAU,EAAE,OAAO,EACnB,OAAO,EAAE,OAAO,gCAahB"}
@@ -0,0 +1,42 @@
1
+ import { Readable } from "node:stream";
2
+ import FetchResponseHeaderUtility from "happy-dom/lib/fetch/utilities/FetchResponseHeaderUtility.js";
3
+ export function documentResponseHeaders(response, request, frame) {
4
+ return FetchResponseHeaderUtility.parseResponseHeaders({
5
+ browserFrame: frame,
6
+ rawHeaders: [...response.headers]
7
+ .flatMap(([name, value]) => (name === "set-cookie" ? [] : [name, value]))
8
+ .concat(response.headers.getSetCookie().flatMap((cookie) => ["set-cookie", cookie])),
9
+ "requestURL": new URL(request.url),
10
+ });
11
+ }
12
+ export function documentResponse(response, request, frame, release, redirected, headers) {
13
+ if (response.body === null) {
14
+ release();
15
+ }
16
+ const result = new frame.window.Response(response.body === null ? null : Readable.toWeb(Readable.from(chunks(response.body, release))), {
17
+ headers,
18
+ status: response.status,
19
+ statusText: response.statusText,
20
+ });
21
+ Object.defineProperty(result, "url", { value: request.url });
22
+ Object.defineProperty(result, "redirected", { value: redirected });
23
+ return result;
24
+ }
25
+ async function* chunks(stream, release) {
26
+ const reader = stream.getReader();
27
+ try {
28
+ while (true) {
29
+ const { done, value } = await reader.read();
30
+ if (done) {
31
+ return;
32
+ }
33
+ yield value;
34
+ }
35
+ }
36
+ finally {
37
+ release();
38
+ await reader.cancel();
39
+ reader.releaseLock();
40
+ }
41
+ }
42
+ //# sourceMappingURL=documentResponse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"documentResponse.js","sourceRoot":"","sources":["../../../src/server/internal/documentResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,0BAA0B,MAAM,6DAA6D,CAAC;AAErG,MAAM,UAAU,uBAAuB,CAAC,QAAkB,EAAE,OAAgB,EAAE,KAAoB;IACjG,OAAO,0BAA0B,CAAC,oBAAoB,CAAC;QACtD,YAAY,EAAE,KAAK;QACnB,UAAU,EAAE,CAAC,GAAG,QAAQ,CAAC,OAAO,CAAC;aAC/B,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,KAAK,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;aACxE,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC,CAAC;QACrF,YAAY,EAAE,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC;KAClC,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,gBAAgB,CAC/B,QAAkB,EAClB,OAAgB,EAChB,KAAoB,EACpB,OAAmB,EACnB,UAAmB,EACnB,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om";
2
+ export declare function documentTransport(fetch: typeof globalThis.fetch): IFetchInterceptor;
3
+ //# sourceMappingURL=documentTransport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"documentTransport.d.ts","sourceRoot":"","sources":["../../../src/server/internal/documentTransport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAOnD,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,UAAU,CAAC,KAAK,GAAG,iBAAiB,CAiCnF"}
@@ -0,0 +1,40 @@
1
+ import FetchRequestHeaderUtility from "happy-dom/lib/fetch/utilities/FetchRequestHeaderUtility.js";
2
+ import WindowBrowserContext from "happy-dom/lib/window/WindowBrowserContext.js";
3
+ import { documentRedirect } from "#internal/documentRedirect.ts";
4
+ import { documentRequest } from "#internal/documentRequest.ts";
5
+ import { documentResponse, documentResponseHeaders } from "#internal/documentResponse.ts";
6
+ export function documentTransport(fetch) {
7
+ return {
8
+ async beforeAsyncRequest({ request, window }) {
9
+ const frame = new WindowBrowserContext(window).getBrowserFrame();
10
+ if (!frame) {
11
+ throw new Error("test-story cannot fetch after the document has closed");
12
+ }
13
+ let current = request;
14
+ let body = current.body === null ? null : Buffer.from(await current.arrayBuffer());
15
+ for (let redirects = 0; redirects <= 20; redirects += 1) {
16
+ const headers = FetchRequestHeaderUtility.getRequestHeaders({ browserFrame: frame, request: current, window });
17
+ delete headers.Connection;
18
+ const { release, response } = await documentRequest(fetch, current, body, headers);
19
+ const responseHeaders = documentResponseHeaders(response, current, frame);
20
+ const location = response.headers.get("location");
21
+ if (![301, 302, 303, 307, 308].includes(response.status) || location === null || current.redirect === "manual") {
22
+ return documentResponse(response, current, frame, release, redirects > 0, responseHeaders);
23
+ }
24
+ await response.body?.cancel();
25
+ release();
26
+ if (current.redirect === "error" || redirects === 20) {
27
+ throw new Error(`test-story cannot follow redirect from ${current.url}`);
28
+ }
29
+ const next = documentRedirect(current, window, new URL(location, current.url), response.status, body);
30
+ current = next.request;
31
+ body = next.body;
32
+ }
33
+ throw new Error(`test-story reached the redirect limit for ${request.url}`);
34
+ },
35
+ beforeSyncRequest({ request }) {
36
+ throw new Error(`test-story refuses synchronous document requests to ${new URL(request.url).hostname}; use fetch instead`);
37
+ },
38
+ };
39
+ }
40
+ //# sourceMappingURL=documentTransport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"documentTransport.js","sourceRoot":"","sources":["../../../src/server/internal/documentTransport.ts"],"names":[],"mappings":"AACA,OAAO,yBAAyB,MAAM,4DAA4D,CAAC;AACnG,OAAO,oBAAoB,MAAM,8CAA8C,CAAC;AAChF,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,MAAM,+BAA+B,CAAC;AAE1F,MAAM,UAAU,iBAAiB,CAAC,KAA8B;IAC/D,OAAO;QACN,KAAK,CAAC,kBAAkB,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE;YAC3C,MAAM,KAAK,GAAG,IAAI,oBAAoB,CAAC,MAAM,CAAC,CAAC,eAAe,EAAE,CAAC;YACjE,IAAI,CAAC,KAAK,EAAE,CAAC;gBACZ,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;YAC1E,CAAC;YACD,IAAI,OAAO,GAAG,OAAO,CAAC;YACtB,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;YACnF,KAAK,IAAI,SAAS,GAAG,CAAC,EAAE,SAAS,IAAI,EAAE,EAAE,SAAS,IAAI,CAAC,EAAE,CAAC;gBACzD,MAAM,OAAO,GAAG,yBAAyB,CAAC,iBAAiB,CAAC,EAAE,YAAY,EAA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AC;YAClB,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,6CAA6C,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;QAC7E,CAAC;QACD,iBAAiB,CAAC,EAAE,OAAO,EAAE;YAC5B,MAAM,IAAI,KAAK,CAAC,uDAAuD,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,QAAQ,qBAAqB,CAAC,CAAC;QAC5H,CAAC;KACD,CAAC;AACH,CAAC"}
@@ -1,3 +1,4 @@
1
+ export declare function unrouted(hostname: string): Error;
1
2
  export declare function refusal(name: string, line: string, cause: unknown): Error;
2
3
  export declare function broken(name: string, steps: number, reason: string): Error;
3
4
  export declare function unfinished(name: string, steps: number, diagnosis: string | undefined): Error;
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../src/server/internal/errors.ts"],"names":[],"mappings":"AAGA,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,CAQzE;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,KAAK,CAOzE;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,KAAK,CAO5F;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAO7C"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../src/server/internal/errors.ts"],"names":[],"mappings":"AAGA,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,KAAK,CAOhD;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,CAQzE;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,KAAK,CAOzE;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,KAAK,CAO5F;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAO7C"}
@@ -1,5 +1,8 @@
1
1
  import { describeValue } from "#internal/render.ts";
2
2
  import { explained } from "#internal/wrap.ts";
3
+ export function unrouted(hostname) {
4
+ return new Error(explained(`test-story refused an outbound request to ${hostname}`, "Add this hostname to the story kit's outbound map and point it at a fake server. Only loopback requests may run without a route."));
5
+ }
3
6
  export function refusal(name, line, cause) {
4
7
  return new Error(explained(`the trait "${line}" refused to set up the ${name}: ${describeValue(cause)}`, `a trait throws when the ${name} it asks for cannot exist. Give the test traits that fit together, or fix the trait in the ${name} story kit if this ${name} should be possible.`), { cause });
5
8
  }
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/server/internal/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE9C,MAAM,UAAU,OAAO,CAAC,IAAY,EAAE,IAAY,EAAE,KAAc;IACjE,OAAO,IAAI,KAAK,CACf,SAAS,CACR,cAAc,IAAI,2BAA2B,IAAI,KAAK,aAAa,CAAC,KAAK,CAAC,EAAE,EAC5E,2BAA2B,IAAI,8FAA8F,IAAI,sBAAsB,IAAI,sBAAsB,CACjL,EACD,EAAE,KAAK,EAAE,CACT,CAAC;AACH,CAAC;AAED,MAAM,UAAU,MAAM,CAAC,IAAY,EAAE,KAAa,EAAE,MAAc;IACjE,OAAO,IAAI,KAAK,CACf,SAAS,CACR,OAAO,IAAI,gBAAgB,KAAK,WAAW,MAAM,EAAE,EACnD,qBAAqB,IAAI,kCAAkC,IAAI,oGAAoG,CACnK,CACD,CAAC;AACH,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,IAAY,EAAE,KAAa,EAAE,SAA6B;IACpF,OAAO,IAAI,KAAK,CACf,SAAS,CACR,OAAO,IAAI,QAAQ,KAAK,mDAAmD,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,EAAE,EAC5H,cAAc,IAAI,sHAAsH,CACxI,CACD,CAAC;AACH,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,IAAY;IACrC,OAAO,IAAI,KAAK,CACf,SAAS,CACR,OAAO,IAAI,4DAA4D,IAAI,EAAE,EAC7E,YAAY,IAAI,+DAA+D,IAAI,eAAe,CAClG,CACD,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/server/internal/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE9C,MAAM,UAAU,QAAQ,CAAC,QAAgB;IACxC,OAAO,IAAI,KAAK,CACf,SAAS,CACR,6CAA6C,QAAQ,EAAE,EACvD,kIAAkI,CAClI,CACD,CAAC;AACH,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,IAAY,EAAE,IAAY,EAAE,KAAc;IACjE,OAAO,IAAI,KAAK,CACf,SAAS,CACR,cAAc,IAAI,2BAA2B,IAAI,KAAK,aAAa,CAAC,KAAK,CAAC,EAAE,EAC5E,2BAA2B,IAAI,8FAA8F,IAAI,sBAAsB,IAAI,sBAAsB,CACjL,EACD,EAAE,KAAK,EAAE,CACT,CAAC;AACH,CAAC;AAED,MAAM,UAAU,MAAM,CAAC,IAAY,EAAE,KAAa,EAAE,MAAc;IACjE,OAAO,IAAI,KAAK,CACf,SAAS,CACR,OAAO,IAAI,gBAAgB,KAAK,WAAW,MAAM,EAAE,EACnD,qBAAqB,IAAI,kCAAkC,IAAI,oGAAoG,CACnK,CACD,CAAC;AACH,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,IAAY,EAAE,KAAa,EAAE,SAA6B;IACpF,OAAO,IAAI,KAAK,CACf,SAAS,CACR,OAAO,IAAI,QAAQ,KAAK,mDAAmD,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,EAAE,EAC5H,cAAc,IAAI,sHAAsH,CACxI,CACD,CAAC;AACH,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,IAAY;IACrC,OAAO,IAAI,KAAK,CACf,SAAS,CACR,OAAO,IAAI,4DAA4D,IAAI,EAAE,EAC7E,YAAY,IAAI,+DAA+D,IAAI,eAAe,CAClG,CACD,CAAC;AACH,CAAC"}
@@ -1,3 +1,4 @@
1
1
  import { Effect } from "effect";
2
+ export declare function installOutbound(routes: Readonly<Record<string, string>>, strict?: boolean): Promise<() => void>;
2
3
  export declare function routeOutbound(routes: Readonly<Record<string, string>>): Effect.Effect<void, never, import("effect/Scope").Scope>;
3
4
  //# sourceMappingURL=routeOutbound.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"routeOutbound.d.ts","sourceRoot":"","sources":["../../../src/server/internal/routeOutbound.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAgDhC,wBAAgB,aAAa,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,4DAKrE"}
1
+ {"version":3,"file":"routeOutbound.d.ts","sourceRoot":"","sources":["../../../src/server/internal/routeOutbound.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAehC,wBAAsB,eAAe,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,UAAQ,uBAgD7F;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,4DAKrE"}
@@ -1,9 +1,12 @@
1
+ import { isIP } from "node:net";
1
2
  import { Effect } from "effect";
3
+ import { unrouted } from "#internal/errors.ts";
2
4
  function isDispatcher(value) {
3
5
  return typeof value === "object" && value !== null && "dispatch" in value && typeof value.dispatch === "function";
4
6
  }
5
- async function install(routes) {
7
+ export async function installOutbound(routes, strict = false) {
6
8
  const origins = new Map(Object.entries(routes).map(([hostname, target]) => [hostname, new URL(target).origin]));
9
+ let refused;
7
10
  // A data URL initializes Node's bundled Undici without sending an HTTP request.
8
11
  await fetch("data:,");
9
12
  const slots = ["undici.globalDispatcher.1", "undici.globalDispatcher.2"].flatMap((name) => {
@@ -23,7 +26,15 @@ async function install(routes) {
23
26
  for (const { key, previous } of slots) {
24
27
  const router = {
25
28
  dispatch(options, handler) {
26
- const target = origins.get(new URL(options.origin).hostname);
29
+ const hostname = new URL(options.origin).hostname;
30
+ const target = origins.get(hostname);
31
+ if (strict
32
+ && target === undefined
33
+ && !["localhost", "[::1]"].includes(hostname)
34
+ && !(isIP(hostname) === 4 && hostname.startsWith("127."))) {
35
+ refused = unrouted(hostname);
36
+ throw refused;
37
+ }
27
38
  return previous.dispatch(target === undefined ? options : { ...options, origin: target }, handler);
28
39
  },
29
40
  };
@@ -34,9 +45,14 @@ async function install(routes) {
34
45
  restore();
35
46
  throw error;
36
47
  }
37
- return restore;
48
+ return () => {
49
+ restore();
50
+ if (refused !== undefined) {
51
+ throw refused;
52
+ }
53
+ };
38
54
  }
39
55
  export function routeOutbound(routes) {
40
- return Effect.acquireRelease(Effect.promise(() => install(routes)), (restore) => Effect.sync(restore)).pipe(Effect.asVoid);
56
+ return Effect.acquireRelease(Effect.promise(() => installOutbound(routes, Reflect.get(globalThis, Symbol.for("test-story.dom")) === true)), (restore) => Effect.sync(restore)).pipe(Effect.asVoid);
41
57
  }
42
58
  //# sourceMappingURL=routeOutbound.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"routeOutbound.js","sourceRoot":"","sources":["../../../src/server/internal/routeOutbound.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAUhC,SAAS,YAAY,CAAC,KAAc;IACnC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,UAAU,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,QAAQ,KAAK,UAAU,CAAC;AACnH,CAAC;AAED,KAAK,UAAU,OAAO,CAAC,MAAwC;IAC9D,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,QAAQ,EAAE,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAChH,gFAAgF;IAChF,MAAM,KAAK,CAAC,QAAQ,CAAC,CAAC;IACtB,MAAM,KAAK,GAAG,CAAC,2BAA2B,EAAE,2BAA2B,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QACzF,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC7B,MAAM,QAAQ,GAAY,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;QACvD,OAAO,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC1D,CAAC,CAAC,CAAC;IACH,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,6GAA6G,CAAC,CAAC;IAChI,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AAC,OAAO,KAAK,EAAE,CAAC;QAChB,OAAO,EAAE,CAAC;QACV,MAAM,KAAK,CAAC;IACb,CAAC;IACD,OAAO,GAAG,EAAE;QACX,OAAO,EAAE,CAAC;QACV,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC3B,MAAM,OAAO,CAAC;QACf,CAAC;IACF,CAAC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,MAAwC;IACrE,OAAO,MAAM,CAAC,cAAc,CAC3B,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,eAAe,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,EAC7G,CAAC,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CACjC,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AACvB,CAAC"}
@@ -35,7 +35,7 @@ function declaring(definition, genre, declare) {
35
35
  }
36
36
  export function storyIt(definition) {
37
37
  const genre = genreOf(definition.name);
38
- const outbound = definition.outbound;
38
+ const outbound = definition.outbound ?? (Reflect.get(globalThis, Symbol.for("test-story.dom")) ? {} : undefined);
39
39
  const { effectApp } = makeEffectIt({
40
40
  around: outbound === undefined
41
41
  ? definition.around
@@ -1 +1 @@
1
- {"version":3,"file":"storyIt.js","sourceRoot":"","sources":["../../../src/server/internal/storyIt.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAEvC,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAc,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAkB,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,OAAO,EA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,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QACvF,IAAI,EAAE,SAAS,CAAC,UAAU,EAAE,KAAK,EAAE,SAAS,CAAC,IAAI,CAAC;QAClD,MAAM,EAAE,CAAC,SAAkB,EAAE,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;KACzF,CAAC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/test-story",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Tests that read like stories over the real engine: a story kit per engine with its own it, traits per feature, and a failure that prints the story, where it stopped and the engine's state",
5
5
  "homepage": "https://github.com/ShivaeDev/platform/tree/main/packages/test-story#readme",
6
6
  "bugs": {
@@ -51,14 +51,21 @@
51
51
  "@types/node": "24.19.1",
52
52
  "@typescript/native": "npm:typescript@7.0.2",
53
53
  "effect": "4.0.4",
54
+ "happy-dom": "20.14.6",
54
55
  "typescript": "npm:@typescript/typescript6@6.0.2",
55
56
  "vitest": "5.0.3"
56
57
  },
57
58
  "peerDependencies": {
58
59
  "@effect/vitest": "4.0.4",
59
60
  "effect": "4.0.4",
61
+ "happy-dom": "20.14.6",
60
62
  "vitest": "5.0.3"
61
63
  },
64
+ "peerDependenciesMeta": {
65
+ "happy-dom": {
66
+ "optional": true
67
+ }
68
+ },
62
69
  "engines": {
63
70
  "node": ">=24"
64
71
  },
@@ -1,38 +1,49 @@
1
- import { builtinEnvironments, type Environment } from "vitest/runtime";
1
+ import { createRequire } from "node:module";
2
+ import { GlobalWindow } from "happy-dom";
3
+ import { type Environment, populateGlobal } from "vitest/runtime";
2
4
 
3
5
  const domEnvironment: Environment = {
4
6
  name: "test-story-dom",
5
- async setup(global: typeof globalThis, options) {
6
- const { AbortController, AbortSignal, Headers, Request, Response } = global;
7
- const environment = await builtinEnvironments["happy-dom"].setup(global, options);
8
- // Vitest's setters also replace happy-dom's internal classes, which its fetch needs.
9
- for (const [name, value] of Object.entries({ AbortController, AbortSignal, Headers, Request, Response })) {
7
+ async setup(global: typeof globalThis, { happyDOM = {} }) {
8
+ // Vitest loads environments outside the project's source resolution conditions.
9
+ const load = createRequire(import.meta.url);
10
+ const { documentTransport }: typeof import("#internal/documentTransport.ts") = load("#internal/documentTransport.ts");
11
+ const { installOutbound }: typeof import("#internal/routeOutbound.ts") = load("#internal/routeOutbound.ts");
12
+ const native = Object.fromEntries(
13
+ ["fetch", "AbortController", "AbortSignal", "Headers", "Request", "Response"].map((name) => [name, Reflect.get(global, name)]),
14
+ );
15
+ const restoreOutbound = await installOutbound({}, true);
16
+ const win = new GlobalWindow({
17
+ ...happyDOM,
18
+ console: global.console,
19
+ settings: { ...happyDOM.settings, disableErrorCapturing: true },
20
+ url: happyDOM.url ?? "http://localhost:3000",
21
+ });
22
+ win.happyDOM.settings.fetch.interceptor = documentTransport(global.fetch);
23
+ const { keys, originals } = populateGlobal(global, win, { bindFunctions: true });
24
+ for (const [name, value] of Object.entries(native)) {
10
25
  Object.defineProperty(global, name, { configurable: true, value, writable: true });
11
26
  }
12
- const transport = global.fetch.bind(global);
13
- Object.defineProperty(global, "fetch", {
14
- configurable: true,
15
- value: (input: RequestInfo | URL, init?: RequestInit) => {
16
- if (input instanceof Request) {
17
- const request = new Request(input, init);
18
- return transport(request.url, {
19
- body: request.body,
20
- cache: request.cache,
21
- credentials: request.credentials,
22
- headers: [...request.headers],
23
- method: request.method,
24
- mode: request.mode,
25
- redirect: request.redirect,
26
- referrer: request.referrer,
27
- referrerPolicy: request.referrerPolicy,
28
- signal: request.signal,
29
- });
27
+ Object.defineProperty(global, "window", { configurable: true, value: win, writable: true });
28
+ Object.defineProperty(win.document, "defaultView", { configurable: true, get: () => win });
29
+ Object.defineProperty(global, Symbol.for("test-story.dom"), { configurable: true, value: true });
30
+ return {
31
+ async teardown(target) {
32
+ try {
33
+ await win.happyDOM.abort();
34
+ win.close();
35
+ } finally {
36
+ for (const key of keys) {
37
+ Reflect.deleteProperty(target, key);
38
+ }
39
+ for (const [key, descriptor] of originals) {
40
+ Object.defineProperty(target, key, descriptor);
41
+ }
42
+ Reflect.deleteProperty(target, Symbol.for("test-story.dom"));
43
+ restoreOutbound();
30
44
  }
31
- return transport(input, init?.headers instanceof Headers ? { ...init, headers: [...init.headers] } : init);
32
45
  },
33
- writable: true,
34
- });
35
- return environment;
46
+ };
36
47
  },
37
48
  viteEnvironment: "client",
38
49
  };
@@ -2,5 +2,25 @@ export function domFetch(baseUrl: string): typeof globalThis.fetch {
2
2
  const base = new URL(baseUrl);
3
3
  window.location.href = base.href;
4
4
  const transport = window.fetch.bind(window);
5
- return (input, init) => transport(typeof input === "string" ? new URL(input, base).href : input, init);
5
+ return (input, init) => {
6
+ if (input instanceof Request) {
7
+ const request = new Request(input, init);
8
+ return transport(request.url, {
9
+ body: request.body,
10
+ cache: request.cache,
11
+ credentials: request.credentials,
12
+ headers: [...request.headers],
13
+ method: request.method,
14
+ mode: request.mode,
15
+ redirect: request.redirect,
16
+ referrer: request.referrer,
17
+ referrerPolicy: request.referrerPolicy,
18
+ signal: request.signal,
19
+ });
20
+ }
21
+ return transport(
22
+ typeof input === "string" ? new URL(input, base).href : input,
23
+ init?.headers instanceof Headers ? { ...init, headers: [...init.headers] } : init,
24
+ );
25
+ };
6
26
  }
@@ -0,0 +1,9 @@
1
+ export async function domNavigate(url: string | URL = window.location.href): Promise<Response> {
2
+ const response = await window.fetch(new URL(url, window.location.href), { credentials: "include" });
3
+ const html = await response.clone().text();
4
+ window.location.href = response.url;
5
+ document.open();
6
+ document.write(html);
7
+ document.close();
8
+ return response;
9
+ }
@@ -0,0 +1,28 @@
1
+ import type { Request } from "happy-dom";
2
+ import type BrowserWindow from "happy-dom/lib/window/BrowserWindow.js";
3
+
4
+ export function documentRedirect(request: Request, window: BrowserWindow, target: URL, status: number, body: Buffer<ArrayBuffer> | null) {
5
+ const headers = new window.Headers(request.headers);
6
+ if (target.origin !== new URL(request.url).origin) {
7
+ for (const name of ["authorization", "cookie", "proxy-authorization"]) {
8
+ headers.delete(name);
9
+ }
10
+ }
11
+ const get = (status === 303 && request.method !== "HEAD") || ([301, 302].includes(status) && request.method === "POST");
12
+ if (get) {
13
+ headers.delete("content-type");
14
+ headers.delete("content-length");
15
+ }
16
+ const nextBody = get ? null : body;
17
+ return {
18
+ body: nextBody,
19
+ request: new window.Request(target.href, {
20
+ body: nextBody,
21
+ credentials: request.credentials,
22
+ headers,
23
+ method: get ? "GET" : request.method,
24
+ redirect: request.redirect,
25
+ signal: request.signal,
26
+ }),
27
+ };
28
+ }
@@ -0,0 +1,26 @@
1
+ import type { Request } from "happy-dom";
2
+
3
+ export async function documentRequest(
4
+ fetch: typeof globalThis.fetch,
5
+ request: Request,
6
+ body: Buffer<ArrayBuffer> | null,
7
+ headers: Readonly<Record<string, string>>,
8
+ ) {
9
+ const controller = new AbortController();
10
+ function abort() {
11
+ controller.abort(request.signal.reason);
12
+ }
13
+ function release() {
14
+ request.signal.removeEventListener("abort", abort);
15
+ }
16
+ if (request.signal.aborted) {
17
+ abort();
18
+ }
19
+ request.signal.addEventListener("abort", abort, { once: true });
20
+ try {
21
+ return { release, response: await fetch(request.url, { body, headers, method: request.method, redirect: "manual", signal: controller.signal }) };
22
+ } catch (error) {
23
+ release();
24
+ throw error;
25
+ }
26
+ }
@@ -0,0 +1,51 @@
1
+ import { Readable } from "node:stream";
2
+ import type { Headers, IBrowserFrame, Request } from "happy-dom";
3
+ import FetchResponseHeaderUtility from "happy-dom/lib/fetch/utilities/FetchResponseHeaderUtility.js";
4
+
5
+ export function documentResponseHeaders(response: Response, request: Request, frame: IBrowserFrame) {
6
+ return FetchResponseHeaderUtility.parseResponseHeaders({
7
+ browserFrame: frame,
8
+ rawHeaders: [...response.headers]
9
+ .flatMap(([name, value]) => (name === "set-cookie" ? [] : [name, value]))
10
+ .concat(response.headers.getSetCookie().flatMap((cookie) => ["set-cookie", cookie])),
11
+ "requestURL": new URL(request.url),
12
+ });
13
+ }
14
+
15
+ export function documentResponse(
16
+ response: Response,
17
+ request: Request,
18
+ frame: IBrowserFrame,
19
+ release: () => void,
20
+ redirected: boolean,
21
+ headers: Headers,
22
+ ) {
23
+ if (response.body === null) {
24
+ release();
25
+ }
26
+ const result = new frame.window.Response(response.body === null ? null : Readable.toWeb(Readable.from(chunks(response.body, release))), {
27
+ headers,
28
+ status: response.status,
29
+ statusText: response.statusText,
30
+ });
31
+ Object.defineProperty(result, "url", { value: request.url });
32
+ Object.defineProperty(result, "redirected", { value: redirected });
33
+ return result;
34
+ }
35
+
36
+ async function* chunks(stream: ReadableStream<Uint8Array>, release: () => void) {
37
+ const reader = stream.getReader();
38
+ try {
39
+ while (true) {
40
+ const { done, value } = await reader.read();
41
+ if (done) {
42
+ return;
43
+ }
44
+ yield value;
45
+ }
46
+ } finally {
47
+ release();
48
+ await reader.cancel();
49
+ reader.releaseLock();
50
+ }
51
+ }
@@ -0,0 +1,41 @@
1
+ import type { IFetchInterceptor } from "happy-dom";
2
+ import FetchRequestHeaderUtility from "happy-dom/lib/fetch/utilities/FetchRequestHeaderUtility.js";
3
+ import WindowBrowserContext from "happy-dom/lib/window/WindowBrowserContext.js";
4
+ import { documentRedirect } from "#internal/documentRedirect.ts";
5
+ import { documentRequest } from "#internal/documentRequest.ts";
6
+ import { documentResponse, documentResponseHeaders } from "#internal/documentResponse.ts";
7
+
8
+ export function documentTransport(fetch: typeof globalThis.fetch): IFetchInterceptor {
9
+ return {
10
+ async beforeAsyncRequest({ request, window }) {
11
+ const frame = new WindowBrowserContext(window).getBrowserFrame();
12
+ if (!frame) {
13
+ throw new Error("test-story cannot fetch after the document has closed");
14
+ }
15
+ let current = request;
16
+ let body = current.body === null ? null : Buffer.from(await current.arrayBuffer());
17
+ for (let redirects = 0; redirects <= 20; redirects += 1) {
18
+ const headers = FetchRequestHeaderUtility.getRequestHeaders({ browserFrame: frame, request: current, window });
19
+ delete headers.Connection;
20
+ const { release, response } = await documentRequest(fetch, current, body, headers);
21
+ const responseHeaders = documentResponseHeaders(response, current, frame);
22
+ const location = response.headers.get("location");
23
+ if (![301, 302, 303, 307, 308].includes(response.status) || location === null || current.redirect === "manual") {
24
+ return documentResponse(response, current, frame, release, redirects > 0, responseHeaders);
25
+ }
26
+ await response.body?.cancel();
27
+ release();
28
+ if (current.redirect === "error" || redirects === 20) {
29
+ throw new Error(`test-story cannot follow redirect from ${current.url}`);
30
+ }
31
+ const next = documentRedirect(current, window, new URL(location, current.url), response.status, body);
32
+ current = next.request;
33
+ body = next.body;
34
+ }
35
+ throw new Error(`test-story reached the redirect limit for ${request.url}`);
36
+ },
37
+ beforeSyncRequest({ request }) {
38
+ throw new Error(`test-story refuses synchronous document requests to ${new URL(request.url).hostname}; use fetch instead`);
39
+ },
40
+ };
41
+ }
@@ -1,6 +1,15 @@
1
1
  import { describeValue } from "#internal/render.ts";
2
2
  import { explained } from "#internal/wrap.ts";
3
3
 
4
+ export function unrouted(hostname: string): Error {
5
+ return new Error(
6
+ explained(
7
+ `test-story refused an outbound request to ${hostname}`,
8
+ "Add this hostname to the story kit's outbound map and point it at a fake server. Only loopback requests may run without a route.",
9
+ ),
10
+ );
11
+ }
12
+
4
13
  export function refusal(name: string, line: string, cause: unknown): Error {
5
14
  return new Error(
6
15
  explained(
@@ -1,4 +1,6 @@
1
+ import { isIP } from "node:net";
1
2
  import { Effect } from "effect";
3
+ import { unrouted } from "#internal/errors.ts";
2
4
 
3
5
  interface DispatchOptions {
4
6
  readonly origin: string | URL;
@@ -12,8 +14,9 @@ function isDispatcher(value: unknown): value is Dispatcher {
12
14
  return typeof value === "object" && value !== null && "dispatch" in value && typeof value.dispatch === "function";
13
15
  }
14
16
 
15
- async function install(routes: Readonly<Record<string, string>>) {
17
+ export async function installOutbound(routes: Readonly<Record<string, string>>, strict = false) {
16
18
  const origins = new Map(Object.entries(routes).map(([hostname, target]) => [hostname, new URL(target).origin]));
19
+ let refused: Error | undefined;
17
20
  // A data URL initializes Node's bundled Undici without sending an HTTP request.
18
21
  await fetch("data:,");
19
22
  const slots = ["undici.globalDispatcher.1", "undici.globalDispatcher.2"].flatMap((name) => {
@@ -33,7 +36,17 @@ async function install(routes: Readonly<Record<string, string>>) {
33
36
  for (const { key, previous } of slots) {
34
37
  const router: Dispatcher = {
35
38
  dispatch(options, handler) {
36
- const target = origins.get(new URL(options.origin).hostname);
39
+ const hostname = new URL(options.origin).hostname;
40
+ const target = origins.get(hostname);
41
+ if (
42
+ strict
43
+ && target === undefined
44
+ && !["localhost", "[::1]"].includes(hostname)
45
+ && !(isIP(hostname) === 4 && hostname.startsWith("127."))
46
+ ) {
47
+ refused = unrouted(hostname);
48
+ throw refused;
49
+ }
37
50
  return previous.dispatch(target === undefined ? options : { ...options, origin: target }, handler);
38
51
  },
39
52
  };
@@ -43,12 +56,17 @@ async function install(routes: Readonly<Record<string, string>>) {
43
56
  restore();
44
57
  throw error;
45
58
  }
46
- return restore;
59
+ return () => {
60
+ restore();
61
+ if (refused !== undefined) {
62
+ throw refused;
63
+ }
64
+ };
47
65
  }
48
66
 
49
67
  export function routeOutbound(routes: Readonly<Record<string, string>>) {
50
68
  return Effect.acquireRelease(
51
- Effect.promise(() => install(routes)),
69
+ Effect.promise(() => installOutbound(routes, Reflect.get(globalThis, Symbol.for("test-story.dom")) === true)),
52
70
  (restore) => Effect.sync(restore),
53
71
  ).pipe(Effect.asVoid);
54
72
  }
@@ -53,7 +53,7 @@ function declaring(definition: LooseDefinition, genre: Genre, declare: EffectTes
53
53
 
54
54
  export function storyIt(definition: LooseDefinition): LooseIt {
55
55
  const genre = genreOf(definition.name);
56
- const outbound = definition.outbound;
56
+ const outbound = definition.outbound ?? (Reflect.get(globalThis, Symbol.for("test-story.dom")) ? {} : undefined);
57
57
  const { effectApp } = makeEffectIt({
58
58
  around:
59
59
  outbound === undefined