@shivaedev/test-story 0.25.0 → 0.26.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/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.26.0 - 2026-10-30
4
+
5
+ ### Added
6
+
7
+ - Browser story kits can use `browserReads` to count and inspect queries and subscription openings on a page's shared live WebSocket, inspect received change hints, and await hints with scoped `watchHints` subscriptions without polling.
8
+ - Hold a forwarding proxy's connection with `hold()` and restore traffic with `online()`, including shared WebSocket RPC and live updates. `disconnect()` now returns a promise that resolves on the reconnected client's first WebSocket frame after `online()`.
9
+
10
+ ### Breaking
11
+
12
+ - Replace calls to the forwarding proxy's `resume()` with `online()`. Use the returned `url` on IPv4 loopback instead of connecting to the proxy over IPv6; proxy startup no longer requires IPv6 support.
13
+
14
+ ## 0.25.1 - 2026-10-30
15
+
16
+ ### Changed
17
+
18
+ - Release with updated workspace dependencies.
19
+
3
20
  ## 0.25.0 - 2026-10-30
4
21
 
5
22
  ### Added
package/README.md CHANGED
@@ -750,6 +750,19 @@ For React, pass `act: async (action) => { await act(action); }` using React's `a
750
750
 
751
751
  Settlement has a live-time limit of 3 seconds, configurable with the driver's `timeoutMs` or per call with `settle({ timeoutMs })`, and fails with the current screen text and advice when that limit expires. Use the DOM environment above and scoped `routeOutbound` routing to reach `fakeHttpService`; the driver does not install another transport. The driver acts on happy-dom over loopback HTTP; it does not drive a real browser.
752
752
 
753
+ ### Counting a page's live reads
754
+
755
+ Acquire `browserReads` from `server/browserReads.ts` in the browser story kit's `create` hook. Give it the page's RPC URL and contracts, then pass its `protocol` to each `makeClient` used by that page. Those clients share one native WebSocket transport. In Node stories, pass `layerWebSocketConstructorWS` from `@effect/platform-node/NodeSocket`; the default uses the global browser constructor. Optional `headers` travel on the socket handshake.
756
+
757
+ ```ts
758
+ const observed = yield* browserReads({ contracts: [Notes], url });
759
+ const client = makeClient(Notes, { protocol: observed.protocol, url });
760
+ ```
761
+
762
+ Expose `observed.readCount`, `observed.reads`, `observed.hints` and `observed.watchHints` through the kit's verbs. `readCount()` counts query requests and `$changes` subscription openings; `readCount("notes.list")` selects one tag. `reads()` returns their `tag`, encoded `payload` and `kind` (`"query"` or `"subscription"`), in send order. Commands and protocol acknowledgements do not count. `hints()` returns the `LiveHint` values received for those subscriptions, including `Resync`.
763
+
764
+ A story calls `const hints = yield* watchHints()` before the write, then `yield* hints.next` to await its browser delivery. Each watcher buffers its own future deliveries until the calling Scope closes. The wait uses the transport's signal with no polling budget. A hint means the browser received it; wait on the application's query or screen signal separately before asserting its rendered result or refetch count. Give another page its own readout to keep its traffic separate.
765
+
753
766
  ### API by module
754
767
 
755
768
  Import each module with the prefix `@shivaedev/test-story/`; there is no root entry.
@@ -758,6 +771,7 @@ Import each module with the prefix `@shivaedev/test-story/`; there is no root en
758
771
  | --------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
759
772
  | `server/storyKit.ts` | `storyKit`, `Trait`, `RunUntilOptions`, `Story`, `RunHooks`, `StoryKitDefinition`, `Told`, `StoryBody`, `StoryTest`, `StoryIt`, `StoryKit` |
760
773
  | `server/outcomes.ts` | `outcomes(rows)` |
774
+ | `server/browserReads.ts` | `browserReads(options)`, `BrowserReadsOptions`, `BrowserRead` |
761
775
  | `server/domFetch.ts` | `domFetch(baseUrl, target?)` |
762
776
  | `server/seedCookies.ts` | `seedCookies(cookies, target?)`, `DomCookie` |
763
777
  | `server/routeOutbound.ts` | `routeOutbound(routes)` |
@@ -0,0 +1,12 @@
1
+ import { type IncomingMessage } from "node:http";
2
+ import type { ForwardingProxyOptions } from "#forwardingProxy.ts";
3
+ export declare function makeForwardingProxy(upstream: string, options: ForwardingProxyOptions): {
4
+ close: () => void;
5
+ disconnect: () => Promise<void>;
6
+ hold: () => void;
7
+ online: () => void;
8
+ track: (socket: import("stream").Duplex) => void;
9
+ upgrade: (downstream: import("stream").Duplex, upstream: import("stream").Duplex, head: Buffer, upstreamHead: Buffer) => void;
10
+ server: import("http").Server<typeof IncomingMessage, typeof import("http").ServerResponse>;
11
+ };
12
+ //# sourceMappingURL=makeForwardingProxy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeForwardingProxy.d.ts","sourceRoot":"","sources":["../../../src/server/adapters/makeForwardingProxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,eAAe,EAAW,MAAM,WAAW,CAAC;AAGxE,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAGlE,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,sBAAsB;;;;;;;;EAoDpF"}
@@ -0,0 +1,59 @@
1
+ import { createServer, request } from "node:http";
2
+ import { isIP } from "node:net";
3
+ import * as HttpStatus from "effect/http/HttpStatus";
4
+ import { proxyConnections } from "./proxyConnections.js";
5
+ export function makeForwardingProxy(upstream, options) {
6
+ const connections = proxyConnections();
7
+ function requestOptions(incoming) {
8
+ return {
9
+ family: options.localAddress === undefined ? undefined : isIP(options.localAddress),
10
+ headers: {
11
+ ...incoming.headers,
12
+ "x-forwarded-for": [incoming.headers["x-forwarded-for"], ...(options.forwardedHops ?? [incoming.socket.remoteAddress])]
13
+ .filter(Boolean)
14
+ .join(", "),
15
+ ...options.headers,
16
+ },
17
+ localAddress: options.localAddress,
18
+ method: incoming.method,
19
+ };
20
+ }
21
+ const server = createServer((incoming, response) => {
22
+ const outgoing = request(`${upstream}${incoming.url}`, requestOptions(incoming), (result) => {
23
+ response.writeHead(result.statusCode ?? HttpStatus.fromLiteral("BadGateway"), result.headers);
24
+ result.pipe(response);
25
+ });
26
+ outgoing.on("error", (error) => {
27
+ if (response.headersSent) {
28
+ response.destroy(error);
29
+ }
30
+ else {
31
+ response.writeHead(HttpStatus.fromLiteral("BadGateway")).end();
32
+ }
33
+ });
34
+ response.on("close", () => outgoing.destroy());
35
+ incoming.pipe(outgoing);
36
+ })
37
+ .on("connection", connections.track)
38
+ .on("upgrade", (incoming, downstream, head) => {
39
+ const outgoing = request(`${upstream}${incoming.url}`, requestOptions(incoming));
40
+ outgoing.on("upgrade", (result, upstreamSocket, upstreamHead) => {
41
+ downstream.write(`HTTP/1.1 ${result.statusCode} ${result.statusMessage}\r\n`);
42
+ for (let index = 0; index < result.rawHeaders.length; index += 2) {
43
+ downstream.write(`${result.rawHeaders[index]}: ${result.rawHeaders[index + 1]}\r\n`);
44
+ }
45
+ downstream.write("\r\n");
46
+ connections.upgrade(downstream, upstreamSocket, head, upstreamHead);
47
+ });
48
+ outgoing.on("response", (result) => {
49
+ downstream.end(`HTTP/1.1 ${result.statusCode} ${result.statusMessage}\r\nConnection: close\r\n\r\n`);
50
+ result.resume();
51
+ });
52
+ outgoing.on("error", () => downstream.destroy());
53
+ downstream.on("error", () => outgoing.destroy());
54
+ downstream.on("close", () => outgoing.destroy());
55
+ outgoing.end();
56
+ });
57
+ return { ...connections, server };
58
+ }
59
+ //# sourceMappingURL=makeForwardingProxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeForwardingProxy.js","sourceRoot":"","sources":["../../../src/server/adapters/makeForwardingProxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAwB,OAAO,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAChC,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AAErD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,MAAM,UAAU,mBAAmB,CAAC,QAAgB,EAAE,OAA+B;IACpF,MAAM,WAAW,GAAG,gBAAgB,EAAE,CAAC;IACvC,SAAS,cAAc,CAAC,QAAyB;QAChD,OAAO;YACN,MAAM,EAAE,OAAO,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,C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AAC,CAAC;QACH,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;QACjD,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QACjD,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QACjD,QAAQ,CAAC,GAAG,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;IACJ,OAAO,EAAE,GAAG,WAAW,EAAE,MAAM,EAAE,CAAC;AACnC,CAAC"}
@@ -0,0 +1,10 @@
1
+ import type { Duplex } from "node:stream";
2
+ export declare function proxyConnections(): {
3
+ close: () => void;
4
+ disconnect: () => Promise<void>;
5
+ hold: () => void;
6
+ online: () => void;
7
+ track: (socket: Duplex) => void;
8
+ upgrade: (downstream: Duplex, upstream: Duplex, head: Buffer, upstreamHead: Buffer) => void;
9
+ };
10
+ //# sourceMappingURL=proxyConnections.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxyConnections.d.ts","sourceRoot":"","sources":["../../../src/server/adapters/proxyConnections.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,wBAAgB,gBAAgB;;;;;oBAaR,MAAM;0BAgCN,MAAM,YAAY,MAAM,QAAQ,MAAM,gBAAgB,MAAM;EAkBnF"}
@@ -0,0 +1,66 @@
1
+ export function proxyConnections() {
2
+ const sockets = new Set();
3
+ let mode = "online";
4
+ let reconnected;
5
+ function close() {
6
+ for (const socket of sockets) {
7
+ socket.destroy();
8
+ }
9
+ }
10
+ function firstFrame() {
11
+ reconnected?.();
12
+ reconnected = undefined;
13
+ }
14
+ function track(socket) {
15
+ sockets.add(socket);
16
+ socket.on("close", () => sockets.delete(socket));
17
+ if (mode === "held") {
18
+ socket.pause();
19
+ }
20
+ else if (mode === "offline") {
21
+ socket.destroy();
22
+ }
23
+ }
24
+ return {
25
+ close,
26
+ disconnect: () => {
27
+ mode = "offline";
28
+ const nextFrame = new Promise((resolve) => {
29
+ reconnected = resolve;
30
+ });
31
+ close();
32
+ return nextFrame;
33
+ },
34
+ hold: () => {
35
+ mode = "held";
36
+ for (const socket of sockets) {
37
+ socket.pause();
38
+ }
39
+ },
40
+ online: () => {
41
+ mode = "online";
42
+ for (const socket of sockets) {
43
+ socket.resume();
44
+ }
45
+ },
46
+ track,
47
+ upgrade: (downstream, upstream, head, upstreamHead) => {
48
+ track(upstream);
49
+ downstream.once("data", firstFrame);
50
+ if (head.length > 0) {
51
+ firstFrame();
52
+ }
53
+ downstream.write(upstreamHead);
54
+ upstream.write(head);
55
+ downstream.pipe(upstream).pipe(downstream);
56
+ downstream.on("close", () => upstream.destroy());
57
+ upstream.on("close", () => downstream.destroy());
58
+ upstream.on("error", () => downstream.destroy());
59
+ if (mode === "held") {
60
+ downstream.pause();
61
+ upstream.pause();
62
+ }
63
+ },
64
+ };
65
+ }
66
+ //# sourceMappingURL=proxyConnections.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxyConnections.js","sourceRoot":"","sources":["../../../src/server/adapters/proxyConnections.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,gBAAgB;IAC/B,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAClC,IAAI,IAAI,GAAkC,QAAQ,CAAC;IACnD,IAAI,WAAqC,CAAC;IAC1C,SAAS,KAAK;QACb,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC9B,MAAM,CAAC,OAAO,EAAE,CAAC;QAClB,CAAC;IACF,CAAC;IACD,SAAS,UAAU;QAClB,WAAW,EAAE,EAAE,CAAC;QAChB,WAAW,GAAG,SAAS,CAAC;IACzB,CAAC;IACD,SAAS,KAAK,CAAC,MAAc;QAC5B,OAAO,CAAC,GAAG,CAAC,MAAM,C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C;YAC/B,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACrB,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC3C,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;YACjD,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;YACjD,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;YACjD,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;gBACrB,UAAU,CAAC,KAAK,EAAE,CAAC;gBACnB,QAAQ,CAAC,KAAK,EAAE,CAAC;YAClB,CAAC;QACF,CAAC;KACD,CAAC;AACH,CAAC"}
@@ -0,0 +1,67 @@
1
+ import { Effect, Layer } from "effect";
2
+ import { RpcClient } from "effect/rpc";
3
+ import * as Socket from "effect/socket/Socket";
4
+ export interface BrowserRead {
5
+ readonly kind: "query" | "subscription";
6
+ readonly payload: unknown;
7
+ readonly tag: string;
8
+ }
9
+ export interface BrowserReadsOptions {
10
+ readonly contracts: readonly {
11
+ readonly declaration: {
12
+ readonly name: string;
13
+ readonly queries: readonly {
14
+ readonly name: string;
15
+ }[];
16
+ };
17
+ }[];
18
+ readonly headers?: Readonly<Record<string, string>>;
19
+ readonly url: string;
20
+ readonly webSocketConstructor?: Layer.Layer<Socket.WebSocketConstructor>;
21
+ }
22
+ export declare const browserReads: (options: BrowserReadsOptions) => Effect.Effect<{
23
+ hints: () => ({
24
+ readonly _tag: "Changed";
25
+ readonly keys: readonly ({
26
+ readonly _tag: "Bucket";
27
+ readonly collection: string;
28
+ readonly field: string;
29
+ readonly value: string | number;
30
+ } | {
31
+ readonly _tag: "List";
32
+ readonly collection: string;
33
+ } | {
34
+ readonly _tag: "Item";
35
+ readonly collection: string;
36
+ readonly id: string | number;
37
+ })[];
38
+ readonly queryIndexes?: readonly number[] | undefined;
39
+ } | {
40
+ readonly _tag: "Resync";
41
+ })[];
42
+ protocol: Layer.Layer<RpcClient.Protocol, never, never>;
43
+ readCount: (tag?: string) => number;
44
+ reads: () => BrowserRead[];
45
+ watchHints: () => Effect.Effect<{
46
+ next: Effect.Effect<{
47
+ readonly _tag: "Changed";
48
+ readonly keys: readonly ({
49
+ readonly _tag: "Bucket";
50
+ readonly collection: string;
51
+ readonly field: string;
52
+ readonly value: string | number;
53
+ } | {
54
+ readonly _tag: "List";
55
+ readonly collection: string;
56
+ } | {
57
+ readonly _tag: "Item";
58
+ readonly collection: string;
59
+ readonly id: string | number;
60
+ })[];
61
+ readonly queryIndexes?: readonly number[] | undefined;
62
+ } | {
63
+ readonly _tag: "Resync";
64
+ }, never, never>;
65
+ }, never, import("effect/Scope").Scope>;
66
+ }, never, import("effect/Scope").Scope>;
67
+ //# sourceMappingURL=browserReads.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browserReads.d.ts","sourceRoot":"","sources":["../../src/server/browserReads.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,MAAM,EAAE,KAAK,EAAkB,MAAM,QAAQ,CAAC;AAChE,OAAO,EAAE,SAAS,EAAoB,MAAM,YAAY,CAAC;AACzD,OAAO,KAAK,MAAM,MAAM,sBAAsB,CAAC;AAG/C,MAAM,WAAW,WAAW;IAC3B,QAAQ,CAAC,IAAI,EAAE,OAAO,GAAG,cAAc,CAAC;IACxC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,mBAAmB;IACnC,QAAQ,CAAC,SAAS,EAAE,SAAS;QAC5B,QAAQ,CAAC,WAAW,EAAE;YAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;YAAC,QAAQ,CAAC,OAAO,EAAE,SAAS;gBAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;aAAE,EAAE,CAAA;SAAE,CAAC;KACxG,EAAE,CAAC;IACJ,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACpD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,oBAAoB,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;CACzE;AAED,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;sBAoDL,MAAM;;;;;;;;;;;;;;;;;;;;;;;uCAIxB,CAAC"}
@@ -0,0 +1,51 @@
1
+ import { Context, Effect, Layer, PubSub, Schema } from "effect";
2
+ import { RpcClient, RpcSerialization } from "effect/rpc";
3
+ import * as Socket from "effect/socket/Socket";
4
+ import { LiveHint } from "@shivaedev/contract/live.ts";
5
+ export const browserReads = Effect.fn("BrowserStory.browserReads")(function* (options) {
6
+ const queries = new Set(options.contracts.flatMap(({ declaration }) => declaration.queries.map(({ name }) => `${declaration.name}.${name}`)));
7
+ const subscriptions = new Set(options.contracts.map(({ declaration }) => `${declaration.name}.$changes`));
8
+ const reads = [];
9
+ const hints = [];
10
+ const deliveries = yield* PubSub.unbounded();
11
+ yield* Effect.addFinalizer(() => PubSub.shutdown(deliveries));
12
+ const context = yield* Layer.build(RpcClient.layerProtocolSocket().pipe(Layer.provide(Socket.layerWebSocket(options.url.replace(/^http/u, "ws"), { headers: options.headers }).pipe(Layer.provide(options.webSocketConstructor ?? Socket.layerWebSocketConstructorGlobal))), Layer.provide(RpcSerialization.layerNdjson)));
13
+ const underlying = Context.get(context, RpcClient.Protocol);
14
+ const listening = new Set();
15
+ const protocol = Layer.succeed(RpcClient.Protocol, {
16
+ ...underlying,
17
+ run: (clientId, receive) => underlying.run(clientId, (message) => Effect.gen(function* () {
18
+ if (message._tag === "Chunk" && listening.has(`${clientId}:${message.requestId}`)) {
19
+ for (const value of message.values) {
20
+ const hint = yield* Schema.decodeUnknownEffect(LiveHint)(value).pipe(Effect.orDie);
21
+ hints.push(hint);
22
+ yield* PubSub.publish(deliveries, hint);
23
+ }
24
+ }
25
+ else if (message._tag === "Exit") {
26
+ listening.delete(`${clientId}:${message.requestId}`);
27
+ }
28
+ yield* receive(message);
29
+ })),
30
+ send: (clientId, request, transferables) => Effect.gen(function* () {
31
+ if (request._tag === "Request") {
32
+ if (subscriptions.has(request.tag)) {
33
+ listening.add(`${clientId}:${request.id}`);
34
+ reads.push({ kind: "subscription", payload: request.payload, tag: request.tag });
35
+ }
36
+ else if (queries.has(request.tag)) {
37
+ reads.push({ kind: "query", payload: request.payload, tag: request.tag });
38
+ }
39
+ }
40
+ yield* underlying.send(clientId, request, transferables);
41
+ }),
42
+ });
43
+ return {
44
+ hints: () => [...hints],
45
+ protocol,
46
+ readCount: (tag) => reads.filter((read) => tag === undefined || read.tag === tag).length,
47
+ reads: () => [...reads],
48
+ watchHints: () => Effect.map(PubSub.subscribe(deliveries), (queue) => ({ next: PubSub.take(queue) })),
49
+ };
50
+ });
51
+ //# sourceMappingURL=browserReads.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browserReads.js","sourceRoot":"","sources":["../../src/server/browserReads.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,KAAK,MAAM,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AAiBvD,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,EAAE,CAAC,2BAA2B,CAAC,CAAC,QAAQ,CAAC,EAAE,OAA4B;IACzG,MAAM,OAAO,GAAG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interface ForwardingProxyOptions {
5
5
  readonly localAddress?: string;
6
6
  }
7
7
  export declare const forwardingProxy: (upstream: string, options?: ForwardingProxyOptions | undefined) => Effect.Effect<{
8
- disconnect: () => void;
9
- resume: () => void;
8
+ disconnect: () => Promise<void>;
9
+ hold: () => void;
10
+ online: () => void;
10
11
  url: string;
11
12
  }, Error, import("effect/Scope").Scope>;
12
13
  //# sourceMappingURL=forwardingProxy.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"forwardingProxy.d.ts","sourceRoot":"","sources":["../../src/server/forwardingProxy.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIhC,MAAM,WAAW,sBAAsB;IACtC,QAAQ,CAAC,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC3C,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACpD,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,eAAO,MAAM,eAAe;;;;uCAgG1B,CAAC"}
1
+ {"version":3,"file":"forwardingProxy.d.ts","sourceRoot":"","sources":["../../src/server/forwardingProxy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIhC,MAAM,WAAW,sBAAsB;IACtC,QAAQ,CAAC,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC3C,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACpD,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,eAAO,MAAM,eAAe;;;;;uCAU1B,CAAC"}
@@ -1,100 +1,14 @@
1
- import { createServer, request } from "node:http";
2
- import { isIP } from "node:net";
1
+ import { createServer } from "node:http";
3
2
  import { Effect } from "effect";
4
- import * as HttpStatus from "effect/http/HttpStatus";
3
+ import { makeForwardingProxy } from "#adapters/makeForwardingProxy.ts";
5
4
  import { loopbackServer } from "#internal/loopback.ts";
6
5
  export const forwardingProxy = Effect.fnUntraced(function* (upstream, options = {}) {
7
6
  if (options.localAddress !== undefined) {
8
7
  yield* Effect.scoped(loopbackServer(createServer(), { host: options.localAddress })).pipe(Effect.mapError((cause) => new Error(`Cannot acquire forwarding proxy with local address ${options.localAddress}`, { cause })));
9
8
  }
10
- function requestOptions(incoming) {
11
- return {
12
- family: options.localAddress === undefined ? undefined : isIP(options.localAddress),
13
- headers: {
14
- ...incoming.headers,
15
- "x-forwarded-for": [incoming.headers["x-forwarded-for"], ...(options.forwardedHops ?? [incoming.socket.remoteAddress])]
16
- .filter(Boolean)
17
- .join(", "),
18
- ...options.headers,
19
- },
20
- localAddress: options.localAddress,
21
- method: incoming.method,
22
- };
23
- }
24
- let offline = false;
25
- const forward = (incoming, response) => {
26
- if (offline) {
27
- incoming.destroy();
28
- return;
29
- }
30
- const outgoing = request(`${upstream}${incoming.url}`, requestOptions(incoming), (result) => {
31
- response.writeHead(result.statusCode ?? HttpStatus.fromLiteral("BadGateway"), result.headers);
32
- result.pipe(response);
33
- });
34
- outgoing.on("error", (error) => {
35
- if (response.headersSent) {
36
- response.destroy(error);
37
- }
38
- else {
39
- response.writeHead(HttpStatus.fromLiteral("BadGateway")).end();
40
- }
41
- });
42
- response.on("close", () => outgoing.destroy());
43
- incoming.pipe(outgoing);
44
- };
45
- const sockets = new Set();
46
- function server() {
47
- return createServer(forward)
48
- .on("connection", (socket) => {
49
- sockets.add(socket);
50
- socket.on("close", () => sockets.delete(socket));
51
- })
52
- .on("upgrade", (incoming, downstream, head) => {
53
- if (offline) {
54
- downstream.destroy();
55
- return;
56
- }
57
- const outgoing = request(`${upstream}${incoming.url}`, requestOptions(incoming));
58
- outgoing.on("upgrade", (result, upstreamSocket, upstreamHead) => {
59
- downstream.write(`HTTP/1.1 ${result.statusCode} ${result.statusMessage}\r\n`);
60
- for (let index = 0; index < result.rawHeaders.length; index += 2) {
61
- downstream.write(`${result.rawHeaders[index]}: ${result.rawHeaders[index + 1]}\r\n`);
62
- }
63
- downstream.write("\r\n");
64
- downstream.write(upstreamHead);
65
- upstreamSocket.write(head);
66
- downstream.pipe(upstreamSocket).pipe(downstream);
67
- downstream.on("close", () => upstreamSocket.destroy());
68
- upstreamSocket.on("error", () => downstream.destroy());
69
- });
70
- outgoing.on("response", (result) => {
71
- downstream.end(`HTTP/1.1 ${result.statusCode} ${result.statusMessage}\r\nConnection: close\r\n\r\n`);
72
- result.resume();
73
- });
74
- outgoing.on("error", () => downstream.destroy());
75
- downstream.on("error", () => outgoing.destroy());
76
- downstream.on("close", () => outgoing.destroy());
77
- outgoing.end();
78
- });
79
- }
80
- const url = yield* loopbackServer(server());
81
- yield* loopbackServer(server(), { host: "::1", port: Number(new URL(url).port) });
82
- yield* Effect.addFinalizer(() => Effect.sync(() => {
83
- for (const socket of sockets) {
84
- socket.destroy();
85
- }
86
- }));
87
- return {
88
- disconnect: () => {
89
- offline = true;
90
- for (const socket of sockets) {
91
- socket.destroy();
92
- }
93
- },
94
- resume: () => {
95
- offline = false;
96
- },
97
- url,
98
- };
9
+ const proxy = yield* Effect.sync(() => makeForwardingProxy(upstream, options));
10
+ const url = yield* loopbackServer(proxy.server);
11
+ yield* Effect.addFinalizer(() => Effect.sync(proxy.close));
12
+ return { disconnect: proxy.disconnect, hold: proxy.hold, online: proxy.online, url };
99
13
  });
100
14
  //# sourceMappingURL=forwardingProxy.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"forwardingProxy.js","sourceRoot":"","sources":["../../src/server/forwardingProxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA8C,OAAO,EAAE,MAAM,WAAW,CAAC;AAC9F,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAEhC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OA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AC,KAAK,CAAC,CAAC,CAAC;IAC3D,OAAO,EAAE,UAAU,EAAE,KAAK,CAAC,UAAU,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC;AACtF,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/test-story",
3
- "version": "0.25.0",
3
+ "version": "0.26.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": {
@@ -42,15 +42,16 @@
42
42
  "README.md"
43
43
  ],
44
44
  "dependencies": {
45
- "@shivaedev/test": "0.11.0",
46
- "@shivaedev/test-sql": "0.12.0",
45
+ "@shivaedev/contract": "0.23.0",
46
+ "@shivaedev/test": "0.12.0",
47
+ "@shivaedev/test-sql": "0.14.0",
47
48
  "dom-accessibility-api": "0.7.1"
48
49
  },
49
50
  "devDependencies": {
50
51
  "@effect/atom-react": "4.0.6",
51
52
  "@effect/platform-node": "4.0.6",
52
53
  "@effect/vitest": "4.0.6",
53
- "@shivaedev/quality": "0.46.0",
54
+ "@shivaedev/quality": "0.47.0",
54
55
  "@types/node": "24.19.2",
55
56
  "@types/react": "19.3.0",
56
57
  "@types/react-dom": "19.3.0",
@@ -0,0 +1,59 @@
1
+ import { createServer, type IncomingMessage, request } from "node:http";
2
+ import { isIP } from "node:net";
3
+ import * as HttpStatus from "effect/http/HttpStatus";
4
+ import type { ForwardingProxyOptions } from "#forwardingProxy.ts";
5
+ import { proxyConnections } from "./proxyConnections.ts";
6
+
7
+ export function makeForwardingProxy(upstream: string, options: ForwardingProxyOptions) {
8
+ const connections = proxyConnections();
9
+ function requestOptions(incoming: IncomingMessage) {
10
+ return {
11
+ family: options.localAddress === undefined ? undefined : isIP(options.localAddress),
12
+ headers: {
13
+ ...incoming.headers,
14
+ "x-forwarded-for": [incoming.headers["x-forwarded-for"], ...(options.forwardedHops ?? [incoming.socket.remoteAddress])]
15
+ .filter(Boolean)
16
+ .join(", "),
17
+ ...options.headers,
18
+ },
19
+ localAddress: options.localAddress,
20
+ method: incoming.method,
21
+ };
22
+ }
23
+ const server = createServer((incoming, response) => {
24
+ const outgoing = request(`${upstream}${incoming.url}`, requestOptions(incoming), (result) => {
25
+ response.writeHead(result.statusCode ?? HttpStatus.fromLiteral("BadGateway"), result.headers);
26
+ result.pipe(response);
27
+ });
28
+ outgoing.on("error", (error) => {
29
+ if (response.headersSent) {
30
+ response.destroy(error);
31
+ } else {
32
+ response.writeHead(HttpStatus.fromLiteral("BadGateway")).end();
33
+ }
34
+ });
35
+ response.on("close", () => outgoing.destroy());
36
+ incoming.pipe(outgoing);
37
+ })
38
+ .on("connection", connections.track)
39
+ .on("upgrade", (incoming, downstream, head) => {
40
+ const outgoing = request(`${upstream}${incoming.url}`, requestOptions(incoming));
41
+ outgoing.on("upgrade", (result, upstreamSocket, upstreamHead) => {
42
+ downstream.write(`HTTP/1.1 ${result.statusCode} ${result.statusMessage}\r\n`);
43
+ for (let index = 0; index < result.rawHeaders.length; index += 2) {
44
+ downstream.write(`${result.rawHeaders[index]}: ${result.rawHeaders[index + 1]}\r\n`);
45
+ }
46
+ downstream.write("\r\n");
47
+ connections.upgrade(downstream, upstreamSocket, head, upstreamHead);
48
+ });
49
+ outgoing.on("response", (result) => {
50
+ downstream.end(`HTTP/1.1 ${result.statusCode} ${result.statusMessage}\r\nConnection: close\r\n\r\n`);
51
+ result.resume();
52
+ });
53
+ outgoing.on("error", () => downstream.destroy());
54
+ downstream.on("error", () => outgoing.destroy());
55
+ downstream.on("close", () => outgoing.destroy());
56
+ outgoing.end();
57
+ });
58
+ return { ...connections, server };
59
+ }
@@ -0,0 +1,66 @@
1
+ import type { Duplex } from "node:stream";
2
+
3
+ export function proxyConnections() {
4
+ const sockets = new Set<Duplex>();
5
+ let mode: "online" | "held" | "offline" = "online";
6
+ let reconnected: (() => void) | undefined;
7
+ function close() {
8
+ for (const socket of sockets) {
9
+ socket.destroy();
10
+ }
11
+ }
12
+ function firstFrame() {
13
+ reconnected?.();
14
+ reconnected = undefined;
15
+ }
16
+ function track(socket: Duplex) {
17
+ sockets.add(socket);
18
+ socket.on("close", () => sockets.delete(socket));
19
+ if (mode === "held") {
20
+ socket.pause();
21
+ } else if (mode === "offline") {
22
+ socket.destroy();
23
+ }
24
+ }
25
+ return {
26
+ close,
27
+ disconnect: () => {
28
+ mode = "offline";
29
+ const nextFrame = new Promise<void>((resolve) => {
30
+ reconnected = resolve;
31
+ });
32
+ close();
33
+ return nextFrame;
34
+ },
35
+ hold: () => {
36
+ mode = "held";
37
+ for (const socket of sockets) {
38
+ socket.pause();
39
+ }
40
+ },
41
+ online: () => {
42
+ mode = "online";
43
+ for (const socket of sockets) {
44
+ socket.resume();
45
+ }
46
+ },
47
+ track,
48
+ upgrade: (downstream: Duplex, upstream: Duplex, head: Buffer, upstreamHead: Buffer) => {
49
+ track(upstream);
50
+ downstream.once("data", firstFrame);
51
+ if (head.length > 0) {
52
+ firstFrame();
53
+ }
54
+ downstream.write(upstreamHead);
55
+ upstream.write(head);
56
+ downstream.pipe(upstream).pipe(downstream);
57
+ downstream.on("close", () => upstream.destroy());
58
+ upstream.on("close", () => downstream.destroy());
59
+ upstream.on("error", () => downstream.destroy());
60
+ if (mode === "held") {
61
+ downstream.pause();
62
+ upstream.pause();
63
+ }
64
+ },
65
+ };
66
+ }
@@ -0,0 +1,77 @@
1
+ import { Context, Effect, Layer, PubSub, Schema } from "effect";
2
+ import { RpcClient, RpcSerialization } from "effect/rpc";
3
+ import * as Socket from "effect/socket/Socket";
4
+ import { LiveHint } from "@shivaedev/contract/live.ts";
5
+
6
+ export interface BrowserRead {
7
+ readonly kind: "query" | "subscription";
8
+ readonly payload: unknown;
9
+ readonly tag: string;
10
+ }
11
+
12
+ export interface BrowserReadsOptions {
13
+ readonly contracts: readonly {
14
+ readonly declaration: { readonly name: string; readonly queries: readonly { readonly name: string }[] };
15
+ }[];
16
+ readonly headers?: Readonly<Record<string, string>>;
17
+ readonly url: string;
18
+ readonly webSocketConstructor?: Layer.Layer<Socket.WebSocketConstructor>;
19
+ }
20
+
21
+ export const browserReads = Effect.fn("BrowserStory.browserReads")(function* (options: BrowserReadsOptions) {
22
+ const queries = new Set(options.contracts.flatMap(({ declaration }) => declaration.queries.map(({ name }) => `${declaration.name}.${name}`)));
23
+ const subscriptions = new Set(options.contracts.map(({ declaration }) => `${declaration.name}.$changes`));
24
+ const reads: BrowserRead[] = [];
25
+ const hints: LiveHint[] = [];
26
+ const deliveries = yield* PubSub.unbounded<LiveHint>();
27
+ yield* Effect.addFinalizer(() => PubSub.shutdown(deliveries));
28
+ const context = yield* Layer.build(
29
+ RpcClient.layerProtocolSocket().pipe(
30
+ Layer.provide(
31
+ Socket.layerWebSocket(options.url.replace(/^http/u, "ws"), { headers: options.headers }).pipe(
32
+ Layer.provide(options.webSocketConstructor ?? Socket.layerWebSocketConstructorGlobal),
33
+ ),
34
+ ),
35
+ Layer.provide(RpcSerialization.layerNdjson),
36
+ ),
37
+ );
38
+ const underlying = Context.get(context, RpcClient.Protocol);
39
+ const listening = new Set<string>();
40
+ const protocol = Layer.succeed(RpcClient.Protocol, {
41
+ ...underlying,
42
+ run: (clientId, receive) =>
43
+ underlying.run(clientId, (message) =>
44
+ Effect.gen(function* () {
45
+ if (message._tag === "Chunk" && listening.has(`${clientId}:${message.requestId}`)) {
46
+ for (const value of message.values) {
47
+ const hint = yield* Schema.decodeUnknownEffect(LiveHint)(value).pipe(Effect.orDie);
48
+ hints.push(hint);
49
+ yield* PubSub.publish(deliveries, hint);
50
+ }
51
+ } else if (message._tag === "Exit") {
52
+ listening.delete(`${clientId}:${message.requestId}`);
53
+ }
54
+ yield* receive(message);
55
+ }),
56
+ ),
57
+ send: (clientId, request, transferables) =>
58
+ Effect.gen(function* () {
59
+ if (request._tag === "Request") {
60
+ if (subscriptions.has(request.tag)) {
61
+ listening.add(`${clientId}:${request.id}`);
62
+ reads.push({ kind: "subscription", payload: request.payload, tag: request.tag });
63
+ } else if (queries.has(request.tag)) {
64
+ reads.push({ kind: "query", payload: request.payload, tag: request.tag });
65
+ }
66
+ }
67
+ yield* underlying.send(clientId, request, transferables);
68
+ }),
69
+ });
70
+ return {
71
+ hints: () => [...hints],
72
+ protocol,
73
+ readCount: (tag?: string) => reads.filter((read) => tag === undefined || read.tag === tag).length,
74
+ reads: () => [...reads],
75
+ watchHints: () => Effect.map(PubSub.subscribe(deliveries), (queue) => ({ next: PubSub.take(queue) })),
76
+ };
77
+ });
@@ -1,8 +1,6 @@
1
- import { createServer, type IncomingMessage, type RequestListener, request } from "node:http";
2
- import { isIP } from "node:net";
3
- import type { Duplex } from "node:stream";
1
+ import { createServer } from "node:http";
4
2
  import { Effect } from "effect";
5
- import * as HttpStatus from "effect/http/HttpStatus";
3
+ import { makeForwardingProxy } from "#adapters/makeForwardingProxy.ts";
6
4
  import { loopbackServer } from "#internal/loopback.ts";
7
5
 
8
6
  export interface ForwardingProxyOptions {
@@ -17,94 +15,8 @@ export const forwardingProxy = Effect.fnUntraced(function* (upstream: string, op
17
15
  Effect.mapError((cause) => new Error(`Cannot acquire forwarding proxy with local address ${options.localAddress}`, { cause })),
18
16
  );
19
17
  }
20
- function requestOptions(incoming: IncomingMessage) {
21
- return {
22
- family: options.localAddress === undefined ? undefined : isIP(options.localAddress),
23
- headers: {
24
- ...incoming.headers,
25
- "x-forwarded-for": [incoming.headers["x-forwarded-for"], ...(options.forwardedHops ?? [incoming.socket.remoteAddress])]
26
- .filter(Boolean)
27
- .join(", "),
28
- ...options.headers,
29
- },
30
- localAddress: options.localAddress,
31
- method: incoming.method,
32
- };
33
- }
34
- let offline = false;
35
- const forward: RequestListener = (incoming, response) => {
36
- if (offline) {
37
- incoming.destroy();
38
- return;
39
- }
40
- const outgoing = request(`${upstream}${incoming.url}`, requestOptions(incoming), (result) => {
41
- response.writeHead(result.statusCode ?? HttpStatus.fromLiteral("BadGateway"), result.headers);
42
- result.pipe(response);
43
- });
44
- outgoing.on("error", (error) => {
45
- if (response.headersSent) {
46
- response.destroy(error);
47
- } else {
48
- response.writeHead(HttpStatus.fromLiteral("BadGateway")).end();
49
- }
50
- });
51
- response.on("close", () => outgoing.destroy());
52
- incoming.pipe(outgoing);
53
- };
54
- const sockets = new Set<Duplex>();
55
- function server() {
56
- return createServer(forward)
57
- .on("connection", (socket) => {
58
- sockets.add(socket);
59
- socket.on("close", () => sockets.delete(socket));
60
- })
61
- .on("upgrade", (incoming, downstream, head) => {
62
- if (offline) {
63
- downstream.destroy();
64
- return;
65
- }
66
- const outgoing = request(`${upstream}${incoming.url}`, requestOptions(incoming));
67
- outgoing.on("upgrade", (result, upstreamSocket, upstreamHead) => {
68
- downstream.write(`HTTP/1.1 ${result.statusCode} ${result.statusMessage}\r\n`);
69
- for (let index = 0; index < result.rawHeaders.length; index += 2) {
70
- downstream.write(`${result.rawHeaders[index]}: ${result.rawHeaders[index + 1]}\r\n`);
71
- }
72
- downstream.write("\r\n");
73
- downstream.write(upstreamHead);
74
- upstreamSocket.write(head);
75
- downstream.pipe(upstreamSocket).pipe(downstream);
76
- downstream.on("close", () => upstreamSocket.destroy());
77
- upstreamSocket.on("error", () => downstream.destroy());
78
- });
79
- outgoing.on("response", (result) => {
80
- downstream.end(`HTTP/1.1 ${result.statusCode} ${result.statusMessage}\r\nConnection: close\r\n\r\n`);
81
- result.resume();
82
- });
83
- outgoing.on("error", () => downstream.destroy());
84
- downstream.on("error", () => outgoing.destroy());
85
- downstream.on("close", () => outgoing.destroy());
86
- outgoing.end();
87
- });
88
- }
89
- const url = yield* loopbackServer(server());
90
- yield* loopbackServer(server(), { host: "::1", port: Number(new URL(url).port) });
91
- yield* Effect.addFinalizer(() =>
92
- Effect.sync(() => {
93
- for (const socket of sockets) {
94
- socket.destroy();
95
- }
96
- }),
97
- );
98
- return {
99
- disconnect: () => {
100
- offline = true;
101
- for (const socket of sockets) {
102
- socket.destroy();
103
- }
104
- },
105
- resume: () => {
106
- offline = false;
107
- },
108
- url,
109
- };
18
+ const proxy = yield* Effect.sync(() => makeForwardingProxy(upstream, options));
19
+ const url = yield* loopbackServer(proxy.server);
20
+ yield* Effect.addFinalizer(() => Effect.sync(proxy.close));
21
+ return { disconnect: proxy.disconnect, hold: proxy.hold, online: proxy.online, url };
110
22
  });