@copilotkit/aimock 1.46.0 → 1.47.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 (183) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/CHANGELOG.md +12 -1
  4. package/README.md +4 -1
  5. package/dist/aws-event-stream.cjs +20 -23
  6. package/dist/aws-event-stream.cjs.map +1 -1
  7. package/dist/aws-event-stream.d.cts +0 -1
  8. package/dist/aws-event-stream.d.ts +0 -1
  9. package/dist/aws-event-stream.js +20 -24
  10. package/dist/aws-event-stream.js.map +1 -1
  11. package/dist/bedrock-converse.cjs +79 -82
  12. package/dist/bedrock-converse.cjs.map +1 -1
  13. package/dist/bedrock-converse.d.cts.map +1 -1
  14. package/dist/bedrock-converse.d.ts.map +1 -1
  15. package/dist/bedrock-converse.js +80 -83
  16. package/dist/bedrock-converse.js.map +1 -1
  17. package/dist/bedrock.cjs +112 -121
  18. package/dist/bedrock.cjs.map +1 -1
  19. package/dist/bedrock.d.cts.map +1 -1
  20. package/dist/bedrock.d.ts.map +1 -1
  21. package/dist/bedrock.js +113 -122
  22. package/dist/bedrock.js.map +1 -1
  23. package/dist/cohere.cjs +58 -106
  24. package/dist/cohere.cjs.map +1 -1
  25. package/dist/cohere.d.cts.map +1 -1
  26. package/dist/cohere.d.ts.map +1 -1
  27. package/dist/cohere.js +60 -108
  28. package/dist/cohere.js.map +1 -1
  29. package/dist/config-loader.d.cts.map +1 -1
  30. package/dist/config-loader.d.ts.map +1 -1
  31. package/dist/fixture-loader.cjs +103 -2
  32. package/dist/fixture-loader.cjs.map +1 -1
  33. package/dist/fixture-loader.d.cts.map +1 -1
  34. package/dist/fixture-loader.d.ts.map +1 -1
  35. package/dist/fixture-loader.js +104 -4
  36. package/dist/fixture-loader.js.map +1 -1
  37. package/dist/gemini-interactions.cjs +63 -121
  38. package/dist/gemini-interactions.cjs.map +1 -1
  39. package/dist/gemini-interactions.d.cts.map +1 -1
  40. package/dist/gemini-interactions.d.ts.map +1 -1
  41. package/dist/gemini-interactions.js +65 -123
  42. package/dist/gemini-interactions.js.map +1 -1
  43. package/dist/gemini.cjs +242 -155
  44. package/dist/gemini.cjs.map +1 -1
  45. package/dist/gemini.d.cts.map +1 -1
  46. package/dist/gemini.d.ts.map +1 -1
  47. package/dist/gemini.js +244 -157
  48. package/dist/gemini.js.map +1 -1
  49. package/dist/helpers.cjs +400 -7
  50. package/dist/helpers.cjs.map +1 -1
  51. package/dist/helpers.d.cts +51 -1
  52. package/dist/helpers.d.cts.map +1 -1
  53. package/dist/helpers.d.ts +51 -1
  54. package/dist/helpers.d.ts.map +1 -1
  55. package/dist/helpers.js +391 -8
  56. package/dist/helpers.js.map +1 -1
  57. package/dist/index.cjs +4 -2
  58. package/dist/index.d.cts +4 -4
  59. package/dist/index.d.ts +4 -4
  60. package/dist/index.js +4 -4
  61. package/dist/mcp-fakes.d.cts.map +1 -1
  62. package/dist/messages.cjs +159 -141
  63. package/dist/messages.cjs.map +1 -1
  64. package/dist/messages.d.cts.map +1 -1
  65. package/dist/messages.d.ts.map +1 -1
  66. package/dist/messages.js +161 -143
  67. package/dist/messages.js.map +1 -1
  68. package/dist/misbehavior.cjs +62 -3
  69. package/dist/misbehavior.cjs.map +1 -1
  70. package/dist/misbehavior.d.cts.map +1 -1
  71. package/dist/misbehavior.d.ts.map +1 -1
  72. package/dist/misbehavior.js +63 -4
  73. package/dist/misbehavior.js.map +1 -1
  74. package/dist/ndjson-writer.cjs +21 -23
  75. package/dist/ndjson-writer.cjs.map +1 -1
  76. package/dist/ndjson-writer.d.cts +0 -1
  77. package/dist/ndjson-writer.d.cts.map +1 -1
  78. package/dist/ndjson-writer.d.ts +0 -1
  79. package/dist/ndjson-writer.d.ts.map +1 -1
  80. package/dist/ndjson-writer.js +21 -24
  81. package/dist/ndjson-writer.js.map +1 -1
  82. package/dist/ollama.cjs +153 -147
  83. package/dist/ollama.cjs.map +1 -1
  84. package/dist/ollama.d.cts.map +1 -1
  85. package/dist/ollama.d.ts.map +1 -1
  86. package/dist/ollama.js +155 -149
  87. package/dist/ollama.js.map +1 -1
  88. package/dist/openrouter-chat.cjs +1 -1
  89. package/dist/openrouter-chat.js +1 -1
  90. package/dist/raw-validation.cjs +177 -0
  91. package/dist/raw-validation.cjs.map +1 -0
  92. package/dist/raw-validation.js +176 -0
  93. package/dist/raw-validation.js.map +1 -0
  94. package/dist/response/deliver.cjs +144 -0
  95. package/dist/response/deliver.cjs.map +1 -0
  96. package/dist/response/deliver.js +143 -0
  97. package/dist/response/deliver.js.map +1 -0
  98. package/dist/response/frame-loop.cjs +102 -0
  99. package/dist/response/frame-loop.cjs.map +1 -0
  100. package/dist/response/frame-loop.js +100 -0
  101. package/dist/response/frame-loop.js.map +1 -0
  102. package/dist/response/lifecycle.cjs +223 -0
  103. package/dist/response/lifecycle.cjs.map +1 -0
  104. package/dist/response/lifecycle.js +211 -0
  105. package/dist/response/lifecycle.js.map +1 -0
  106. package/dist/response/proxy.cjs +71 -0
  107. package/dist/response/proxy.cjs.map +1 -0
  108. package/dist/response/proxy.js +70 -0
  109. package/dist/response/proxy.js.map +1 -0
  110. package/dist/response/raw.cjs +228 -0
  111. package/dist/response/raw.cjs.map +1 -0
  112. package/dist/response/raw.js +227 -0
  113. package/dist/response/raw.js.map +1 -0
  114. package/dist/response/timing.cjs +157 -0
  115. package/dist/response/timing.cjs.map +1 -0
  116. package/dist/response/timing.js +150 -0
  117. package/dist/response/timing.js.map +1 -0
  118. package/dist/responses.cjs +269 -148
  119. package/dist/responses.cjs.map +1 -1
  120. package/dist/responses.d.cts.map +1 -1
  121. package/dist/responses.d.ts.map +1 -1
  122. package/dist/responses.js +270 -149
  123. package/dist/responses.js.map +1 -1
  124. package/dist/router.cjs +20 -4
  125. package/dist/router.cjs.map +1 -1
  126. package/dist/router.d.cts +7 -1
  127. package/dist/router.d.cts.map +1 -1
  128. package/dist/router.d.ts +7 -1
  129. package/dist/router.d.ts.map +1 -1
  130. package/dist/router.js +21 -5
  131. package/dist/router.js.map +1 -1
  132. package/dist/server.cjs +89 -120
  133. package/dist/server.cjs.map +1 -1
  134. package/dist/server.d.cts.map +1 -1
  135. package/dist/server.d.ts.map +1 -1
  136. package/dist/server.js +91 -122
  137. package/dist/server.js.map +1 -1
  138. package/dist/sse-writer.cjs +49 -30
  139. package/dist/sse-writer.cjs.map +1 -1
  140. package/dist/sse-writer.d.cts +0 -1
  141. package/dist/sse-writer.d.cts.map +1 -1
  142. package/dist/sse-writer.d.ts +0 -1
  143. package/dist/sse-writer.d.ts.map +1 -1
  144. package/dist/sse-writer.js +49 -31
  145. package/dist/sse-writer.js.map +1 -1
  146. package/dist/transcription.cjs +19 -33
  147. package/dist/transcription.cjs.map +1 -1
  148. package/dist/transcription.d.cts.map +1 -1
  149. package/dist/transcription.d.ts.map +1 -1
  150. package/dist/transcription.js +20 -34
  151. package/dist/transcription.js.map +1 -1
  152. package/dist/types.cjs.map +1 -1
  153. package/dist/types.d.cts +173 -10
  154. package/dist/types.d.cts.map +1 -1
  155. package/dist/types.d.ts +173 -10
  156. package/dist/types.d.ts.map +1 -1
  157. package/dist/types.js.map +1 -1
  158. package/dist/ws-framing.cjs +12 -6
  159. package/dist/ws-framing.cjs.map +1 -1
  160. package/dist/ws-framing.d.cts +2 -0
  161. package/dist/ws-framing.d.cts.map +1 -1
  162. package/dist/ws-framing.d.ts +2 -0
  163. package/dist/ws-framing.d.ts.map +1 -1
  164. package/dist/ws-framing.js +12 -6
  165. package/dist/ws-framing.js.map +1 -1
  166. package/dist/ws-gemini-live.cjs +120 -93
  167. package/dist/ws-gemini-live.cjs.map +1 -1
  168. package/dist/ws-gemini-live.js +120 -93
  169. package/dist/ws-gemini-live.js.map +1 -1
  170. package/dist/ws-realtime.cjs +80 -70
  171. package/dist/ws-realtime.cjs.map +1 -1
  172. package/dist/ws-realtime.d.cts.map +1 -1
  173. package/dist/ws-realtime.d.ts.map +1 -1
  174. package/dist/ws-realtime.js +80 -70
  175. package/dist/ws-realtime.js.map +1 -1
  176. package/dist/ws-responses.cjs +72 -53
  177. package/dist/ws-responses.cjs.map +1 -1
  178. package/dist/ws-responses.d.cts.map +1 -1
  179. package/dist/ws-responses.d.ts.map +1 -1
  180. package/dist/ws-responses.js +73 -54
  181. package/dist/ws-responses.js.map +1 -1
  182. package/package.json +1 -1
  183. package/skills/write-fixtures/SKILL.md +34 -2
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lifecycle.cjs","names":["responseGoneReason","createInterruptionSignal"],"sources":["../../src/response/lifecycle.ts"],"sourcesContent":["import type * as http from \"node:http\";\nimport type { Fixture, JournalEntry } from \"../types.js\";\nimport type { WebSocketConnection } from \"../ws-framing.js\";\nimport { createInterruptionSignal, type InterruptionControl } from \"../interruption.js\";\nimport { responseGoneReason } from \"../chaos.js\";\n\n/** The journal `interruptReason` for a client that left before the reply finished. */\nexport const CLIENT_ABORTED = \"client aborted\";\n\n/**\n * The journal `interruptReason` for a response aimock tore down itself: the\n * chaos `disconnect` action or a server-side `destroy`. It is the name the\n * chaos journal entry records (`chaosAction: \"disconnect\"`), so a teardown\n * aimock caused is never blamed on the client.\n */\nexport const AIMOCK_DISCONNECTED = \"disconnect\";\n\n/**\n * Which side stopped a delivery first: the client, the fixture's own\n * interruption, or aimock tearing the response down (`\"server\"`).\n */\nexport type InterruptCause = \"client\" | \"fixture\" | \"server\";\n\n/**\n * One delivery's stop signal. It merges the fixture interruption\n * (`truncateAfterChunks` / `disconnectAfterMs`) with the client going away\n * and with aimock tearing the response down (`\"server\"`).\n * The first cause wins: a later cause never reclassifies it.\n */\nexport interface DeliveryLifecycle {\n /** Aborts on the first cause: fixture, client, or server. */\n readonly signal: AbortSignal;\n /** The cause that aborted `signal`, or `undefined` while it has not aborted. */\n cause(): InterruptCause | undefined;\n /**\n * The fixture's reason, {@link CLIENT_ABORTED}, {@link AIMOCK_DISCONNECTED},\n * or `undefined` while running.\n */\n reason(): string | undefined;\n /** Forwards a sent chunk to the fixture interruption (counts toward `truncateAfterChunks`). */\n tick(): void;\n /** Removes the close and fixture-abort listeners and clears the fixture's `disconnectAfterMs` timer. */\n dispose(): void;\n}\n\n/** The part of an EventEmitter a close source may expose. Test doubles may carry less. */\ninterface CloseSource {\n once?: (event: \"close\", listener: () => void) => unknown;\n off?: (event: \"close\", listener: () => void) => unknown;\n removeListener?: (event: \"close\", listener: () => void) => unknown;\n}\n\n/**\n * Subscribe to `close`, guarded the same way as `awaitChaosLatency`: a partial\n * test double without `once` is not subscribed, and only a listener that was\n * added is ever removed, through a remover that exists.\n */\nfunction subscribeClose(source: CloseSource, onClose: () => void): (() => void) | undefined {\n if (typeof source.once !== \"function\") return undefined;\n source.once(\"close\", onClose);\n if (typeof source.off === \"function\") return () => source.off?.(\"close\", onClose);\n if (typeof source.removeListener === \"function\")\n return () => source.removeListener?.(\"close\", onClose);\n return undefined;\n}\n\nfunction createLifecycle(\n interruption: InterruptionControl | null,\n goneAtStart: InterruptCause | undefined,\n subscribe: (onGone: (cause: InterruptCause) => void) => (() => void) | undefined,\n): DeliveryLifecycle {\n const controller = new AbortController();\n let firstCause: InterruptCause | undefined;\n const abortWith = (cause: InterruptCause): void => {\n if (controller.signal.aborted) return;\n firstCause = cause;\n controller.abort();\n };\n const onFixtureAbort = (): void => abortWith(\"fixture\");\n interruption?.signal.addEventListener(\"abort\", onFixtureAbort, { once: true });\n let unsubscribe: (() => void) | undefined;\n if (goneAtStart !== undefined) abortWith(goneAtStart);\n else unsubscribe = subscribe(abortWith);\n return {\n signal: controller.signal,\n cause: () => firstCause,\n reason: () => {\n if (firstCause === \"fixture\") return interruption?.reason();\n if (firstCause === \"client\") return CLIENT_ABORTED;\n if (firstCause === \"server\") return AIMOCK_DISCONNECTED;\n return undefined;\n },\n tick: () => {\n interruption?.tick();\n },\n dispose: () => {\n unsubscribe?.();\n unsubscribe = undefined;\n interruption?.signal.removeEventListener(\"abort\", onFixtureAbort);\n interruption?.cleanup();\n },\n };\n}\n\n/** The request socket fields the teardown rule and the status-line rule read. */\nexport interface PeerSocket {\n readonly errored?: Error | null;\n readonly writableEnded?: boolean;\n readonly bytesWritten?: number;\n}\n\n/** Where a response's request socket can be read from. Test doubles may carry neither. */\ninterface SocketOwner {\n readonly req?: { readonly socket?: PeerSocket | null };\n readonly socket?: PeerSocket | null;\n}\n\n/** The request socket of `res`, or `undefined` when it carries none. */\nexport function requestSocket(res: http.ServerResponse): PeerSocket | undefined {\n const owner: SocketOwner = res;\n return owner.req?.socket ?? owner.socket ?? undefined;\n}\n\n/**\n * THE client hang-up rule, shared by the delivery lifecycle and the proxy\n * relay: the client's socket closed or reset.\n *\n * - A reset, or a write into a socket the peer closed, leaves `errored` set\n * (ECONNRESET / EPIPE).\n * - aimock's server keeps Node's default `httpAllowHalfOpen: false`, so a\n * client FIN makes Node end our write side: the socket is `writableEnded`\n * while the response is not. That is the evidence the client closed.\n *\n * A FIN alone (`readableEnded` with neither of those) is not a hang-up: it is\n * what the socket shows when aimock tore the response down before Node\n * answered the FIN. A real client FIN that Node has not answered yet looks the\n * same, so this rule also classifies it as not a hang-up. aimock's own\n * `destroy()` leaves neither signal.\n */\nexport function closedByClient(socket: PeerSocket): boolean {\n return (socket.errored ?? null) !== null || socket.writableEnded === true;\n}\n\n/**\n * Who tore down a response that is gone without having ended. After the fact\n * `res.destroyed` reads the same either way, so this reads the evidence:\n *\n * - a chaos `disconnect` marked the response before destroying it: aimock;\n * - the request socket meets {@link closedByClient}: the client;\n * - neither: aimock destroyed it server-side.\n *\n * `socket` defaults to the response's request socket. With no socket to read\n * (a partial test double) the client attribution is kept.\n */\nexport function teardownCause(\n res: http.ServerResponse,\n socket: PeerSocket | null | undefined = requestSocket(res),\n): \"client\" | \"server\" {\n if (responseGoneReason(res) === \"chaos-disconnected\") return \"server\";\n if (!socket) return \"client\";\n return closedByClient(socket) ? \"client\" : \"server\";\n}\n\n/**\n * The request socket's `bytesWritten` when each delivery started, so the\n * status-line rule counts only this response's bytes on a keep-alive socket.\n */\nconst deliveryStartBytes = new WeakMap<http.ServerResponse, number>();\n\n/**\n * Record where this response's bytes start on its socket. A response whose\n * head was already committed when the delivery started is not recorded:\n * {@link statusLineSent} keeps the `headersSent` rule for it.\n */\nexport function noteDeliveryStart(res: http.ServerResponse): void {\n if (deliveryStartBytes.has(res) || res.headersSent) return;\n const bytes = requestSocket(res)?.bytesWritten;\n if (typeof bytes === \"number\") deliveryStartBytes.set(res, bytes);\n}\n\n/**\n * Whether this response's status line reached the client's socket: a byte of\n * it was handed to the socket after {@link noteDeliveryStart}. `headersSent`\n * is not that test: `writeHead` sets it before any byte is written. With no\n * recorded start, or no socket to read, `headersSent` is the fallback.\n */\nexport function statusLineSent(res: http.ServerResponse): boolean {\n const start = deliveryStartBytes.get(res);\n const bytes = requestSocket(res)?.bytesWritten;\n if (start === undefined || typeof bytes !== \"number\") return res.headersSent;\n return bytes > start;\n}\n\n/** The journal `interruptReason` for {@link teardownCause}. */\nexport function teardownReason(res: http.ServerResponse): string {\n return teardownCause(res) === \"client\" ? CLIENT_ABORTED : AIMOCK_DISCONNECTED;\n}\n\n/**\n * HTTP delivery lifecycle. A `close` before `res.writableFinished` aborts it,\n * and {@link teardownCause} decides whose teardown it was: the client\n * (`\"client\"`) or aimock itself (`\"server\"`). A response that is already gone\n * at construction aborts at once with the same attribution. A chaos\n * `disconnect` destroys without ending, so no `writableEnded` check stops a\n * writer for it. Only an already-ended response does not abort: the frame\n * loop exits on its `writableEnded` check. The fixture interruption is\n * created only when `streaming` is true (it is streaming-only).\n */\nexport function httpLifecycle(\n res: http.ServerResponse,\n fixture: Fixture | null,\n opts: { streaming: boolean },\n): DeliveryLifecycle {\n const interruption = opts.streaming && fixture ? createInterruptionSignal(fixture) : null;\n const gone = responseGoneReason(res);\n const goneAtStart = gone === null || gone === \"already-ended\" ? undefined : teardownCause(res);\n return createLifecycle(interruption, goneAtStart, (onGone) =>\n subscribeClose(res, () => {\n if (!res.writableFinished) onGone(teardownCause(res));\n }),\n );\n}\n\n/**\n * Per watched connection: whether its `close` event has fired. Only sockets\n * watched while open are recorded, so an entry is evidence, not a guess.\n */\nconst wsCloseEmitted = new WeakMap<WebSocketConnection, boolean>();\n\n/**\n * Start recording when `ws` emits `close`, so {@link wsClosedCause} can tell\n * aimock's own close from the client's. Each WS transport calls it when the\n * connection opens. Idempotent, and a no-op once closed.\n */\nexport function watchWsClose(ws: WebSocketConnection): void {\n if (ws.isClosed || wsCloseEmitted.has(ws)) return;\n const source: CloseSource = ws;\n if (typeof source.once !== \"function\") return;\n wsCloseEmitted.set(ws, false);\n source.once(\"close\", () => wsCloseEmitted.set(ws, true));\n}\n\n/**\n * Who closed a socket that reads `isClosed`, or `undefined` while it is open.\n *\n * `WebSocketConnection` marks itself closed and emits `close` in the same step\n * for a client close frame, a lost connection, and `destroy()`. Only aimock's\n * own `close()` (a write or buffer limit, a bad frame, a strict miss, shutdown)\n * marks it closed at once and emits `close` about 100ms later. So a socket\n * watched while open ({@link watchWsClose}) that is closed with no `close` yet\n * was closed by aimock (`\"server\"`). Any other closed socket reads as the\n * client leaving, as before.\n */\nexport function wsClosedCause(ws: WebSocketConnection): \"client\" | \"server\" | undefined {\n if (!ws.isClosed) return undefined;\n return wsCloseEmitted.get(ws) === false ? \"server\" : \"client\";\n}\n\n/**\n * WebSocket turn lifecycle. A WS turn is always a stream, so it always owns\n * the fixture interruption. A `close` means the client left. A socket already\n * closed at construction aborts at once with {@link wsClosedCause}. Callers\n * never create their own interruption object.\n */\nexport function wsLifecycle(ws: WebSocketConnection, fixture: Fixture | null): DeliveryLifecycle {\n const interruption = fixture ? createInterruptionSignal(fixture) : null;\n return createLifecycle(interruption, wsClosedCause(ws), (onGone) =>\n subscribeClose(ws, () => onGone(\"client\")),\n );\n}\n\n/**\n * Journal a WS turn that stopped because its lifecycle aborted or its socket\n * closed. This is the closed-socket rule every WS transport uses: the\n * lifecycle's reason when it has one, else the closed socket's cause\n * ({@link wsClosedCause}): {@link AIMOCK_DISCONNECTED} for aimock's own close,\n * {@link CLIENT_ABORTED} for the client. A turn with no lifecycle (it never\n * waits) passes none.\n */\nexport function markWsTurnStopped(\n entry: JournalEntry,\n ws: WebSocketConnection,\n lifecycle?: DeliveryLifecycle,\n): void {\n const cause = wsClosedCause(ws);\n const socketReason =\n cause === \"server\" ? AIMOCK_DISCONNECTED : cause === \"client\" ? CLIENT_ABORTED : undefined;\n markInterrupted(entry, lifecycle?.reason() ?? socketReason);\n}\n\n/**\n * Mark a journal entry interrupted. The reason is set only when the entry has\n * none, so a later `undefined` never erases an earlier reason. The status is\n * never changed: the entry keeps the status it was journaled with.\n */\nexport function markInterrupted(entry: JournalEntry, reason: string | undefined): void {\n entry.response.interrupted = true;\n if (entry.response.interruptReason === undefined && reason !== undefined) {\n entry.response.interruptReason = reason;\n }\n}\n"],"mappings":"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"}
@@ -0,0 +1,211 @@
1
+ import { responseGoneReason } from "../chaos.js";
2
+ import { createInterruptionSignal } from "../interruption.js";
3
+
4
+ //#region src/response/lifecycle.ts
5
+ /** The journal `interruptReason` for a client that left before the reply finished. */
6
+ const CLIENT_ABORTED = "client aborted";
7
+ /**
8
+ * The journal `interruptReason` for a response aimock tore down itself: the
9
+ * chaos `disconnect` action or a server-side `destroy`. It is the name the
10
+ * chaos journal entry records (`chaosAction: "disconnect"`), so a teardown
11
+ * aimock caused is never blamed on the client.
12
+ */
13
+ const AIMOCK_DISCONNECTED = "disconnect";
14
+ /**
15
+ * Subscribe to `close`, guarded the same way as `awaitChaosLatency`: a partial
16
+ * test double without `once` is not subscribed, and only a listener that was
17
+ * added is ever removed, through a remover that exists.
18
+ */
19
+ function subscribeClose(source, onClose) {
20
+ if (typeof source.once !== "function") return void 0;
21
+ source.once("close", onClose);
22
+ if (typeof source.off === "function") return () => source.off?.("close", onClose);
23
+ if (typeof source.removeListener === "function") return () => source.removeListener?.("close", onClose);
24
+ }
25
+ function createLifecycle(interruption, goneAtStart, subscribe) {
26
+ const controller = new AbortController();
27
+ let firstCause;
28
+ const abortWith = (cause) => {
29
+ if (controller.signal.aborted) return;
30
+ firstCause = cause;
31
+ controller.abort();
32
+ };
33
+ const onFixtureAbort = () => abortWith("fixture");
34
+ interruption?.signal.addEventListener("abort", onFixtureAbort, { once: true });
35
+ let unsubscribe;
36
+ if (goneAtStart !== void 0) abortWith(goneAtStart);
37
+ else unsubscribe = subscribe(abortWith);
38
+ return {
39
+ signal: controller.signal,
40
+ cause: () => firstCause,
41
+ reason: () => {
42
+ if (firstCause === "fixture") return interruption?.reason();
43
+ if (firstCause === "client") return CLIENT_ABORTED;
44
+ if (firstCause === "server") return AIMOCK_DISCONNECTED;
45
+ },
46
+ tick: () => {
47
+ interruption?.tick();
48
+ },
49
+ dispose: () => {
50
+ unsubscribe?.();
51
+ unsubscribe = void 0;
52
+ interruption?.signal.removeEventListener("abort", onFixtureAbort);
53
+ interruption?.cleanup();
54
+ }
55
+ };
56
+ }
57
+ /** The request socket of `res`, or `undefined` when it carries none. */
58
+ function requestSocket(res) {
59
+ const owner = res;
60
+ return owner.req?.socket ?? owner.socket ?? void 0;
61
+ }
62
+ /**
63
+ * THE client hang-up rule, shared by the delivery lifecycle and the proxy
64
+ * relay: the client's socket closed or reset.
65
+ *
66
+ * - A reset, or a write into a socket the peer closed, leaves `errored` set
67
+ * (ECONNRESET / EPIPE).
68
+ * - aimock's server keeps Node's default `httpAllowHalfOpen: false`, so a
69
+ * client FIN makes Node end our write side: the socket is `writableEnded`
70
+ * while the response is not. That is the evidence the client closed.
71
+ *
72
+ * A FIN alone (`readableEnded` with neither of those) is not a hang-up: it is
73
+ * what the socket shows when aimock tore the response down before Node
74
+ * answered the FIN. A real client FIN that Node has not answered yet looks the
75
+ * same, so this rule also classifies it as not a hang-up. aimock's own
76
+ * `destroy()` leaves neither signal.
77
+ */
78
+ function closedByClient(socket) {
79
+ return (socket.errored ?? null) !== null || socket.writableEnded === true;
80
+ }
81
+ /**
82
+ * Who tore down a response that is gone without having ended. After the fact
83
+ * `res.destroyed` reads the same either way, so this reads the evidence:
84
+ *
85
+ * - a chaos `disconnect` marked the response before destroying it: aimock;
86
+ * - the request socket meets {@link closedByClient}: the client;
87
+ * - neither: aimock destroyed it server-side.
88
+ *
89
+ * `socket` defaults to the response's request socket. With no socket to read
90
+ * (a partial test double) the client attribution is kept.
91
+ */
92
+ function teardownCause(res, socket = requestSocket(res)) {
93
+ if (responseGoneReason(res) === "chaos-disconnected") return "server";
94
+ if (!socket) return "client";
95
+ return closedByClient(socket) ? "client" : "server";
96
+ }
97
+ /**
98
+ * The request socket's `bytesWritten` when each delivery started, so the
99
+ * status-line rule counts only this response's bytes on a keep-alive socket.
100
+ */
101
+ const deliveryStartBytes = /* @__PURE__ */ new WeakMap();
102
+ /**
103
+ * Record where this response's bytes start on its socket. A response whose
104
+ * head was already committed when the delivery started is not recorded:
105
+ * {@link statusLineSent} keeps the `headersSent` rule for it.
106
+ */
107
+ function noteDeliveryStart(res) {
108
+ if (deliveryStartBytes.has(res) || res.headersSent) return;
109
+ const bytes = requestSocket(res)?.bytesWritten;
110
+ if (typeof bytes === "number") deliveryStartBytes.set(res, bytes);
111
+ }
112
+ /**
113
+ * Whether this response's status line reached the client's socket: a byte of
114
+ * it was handed to the socket after {@link noteDeliveryStart}. `headersSent`
115
+ * is not that test: `writeHead` sets it before any byte is written. With no
116
+ * recorded start, or no socket to read, `headersSent` is the fallback.
117
+ */
118
+ function statusLineSent(res) {
119
+ const start = deliveryStartBytes.get(res);
120
+ const bytes = requestSocket(res)?.bytesWritten;
121
+ if (start === void 0 || typeof bytes !== "number") return res.headersSent;
122
+ return bytes > start;
123
+ }
124
+ /** The journal `interruptReason` for {@link teardownCause}. */
125
+ function teardownReason(res) {
126
+ return teardownCause(res) === "client" ? CLIENT_ABORTED : AIMOCK_DISCONNECTED;
127
+ }
128
+ /**
129
+ * HTTP delivery lifecycle. A `close` before `res.writableFinished` aborts it,
130
+ * and {@link teardownCause} decides whose teardown it was: the client
131
+ * (`"client"`) or aimock itself (`"server"`). A response that is already gone
132
+ * at construction aborts at once with the same attribution. A chaos
133
+ * `disconnect` destroys without ending, so no `writableEnded` check stops a
134
+ * writer for it. Only an already-ended response does not abort: the frame
135
+ * loop exits on its `writableEnded` check. The fixture interruption is
136
+ * created only when `streaming` is true (it is streaming-only).
137
+ */
138
+ function httpLifecycle(res, fixture, opts) {
139
+ const interruption = opts.streaming && fixture ? createInterruptionSignal(fixture) : null;
140
+ const gone = responseGoneReason(res);
141
+ return createLifecycle(interruption, gone === null || gone === "already-ended" ? void 0 : teardownCause(res), (onGone) => subscribeClose(res, () => {
142
+ if (!res.writableFinished) onGone(teardownCause(res));
143
+ }));
144
+ }
145
+ /**
146
+ * Per watched connection: whether its `close` event has fired. Only sockets
147
+ * watched while open are recorded, so an entry is evidence, not a guess.
148
+ */
149
+ const wsCloseEmitted = /* @__PURE__ */ new WeakMap();
150
+ /**
151
+ * Start recording when `ws` emits `close`, so {@link wsClosedCause} can tell
152
+ * aimock's own close from the client's. Each WS transport calls it when the
153
+ * connection opens. Idempotent, and a no-op once closed.
154
+ */
155
+ function watchWsClose(ws) {
156
+ if (ws.isClosed || wsCloseEmitted.has(ws)) return;
157
+ const source = ws;
158
+ if (typeof source.once !== "function") return;
159
+ wsCloseEmitted.set(ws, false);
160
+ source.once("close", () => wsCloseEmitted.set(ws, true));
161
+ }
162
+ /**
163
+ * Who closed a socket that reads `isClosed`, or `undefined` while it is open.
164
+ *
165
+ * `WebSocketConnection` marks itself closed and emits `close` in the same step
166
+ * for a client close frame, a lost connection, and `destroy()`. Only aimock's
167
+ * own `close()` (a write or buffer limit, a bad frame, a strict miss, shutdown)
168
+ * marks it closed at once and emits `close` about 100ms later. So a socket
169
+ * watched while open ({@link watchWsClose}) that is closed with no `close` yet
170
+ * was closed by aimock (`"server"`). Any other closed socket reads as the
171
+ * client leaving, as before.
172
+ */
173
+ function wsClosedCause(ws) {
174
+ if (!ws.isClosed) return void 0;
175
+ return wsCloseEmitted.get(ws) === false ? "server" : "client";
176
+ }
177
+ /**
178
+ * WebSocket turn lifecycle. A WS turn is always a stream, so it always owns
179
+ * the fixture interruption. A `close` means the client left. A socket already
180
+ * closed at construction aborts at once with {@link wsClosedCause}. Callers
181
+ * never create their own interruption object.
182
+ */
183
+ function wsLifecycle(ws, fixture) {
184
+ return createLifecycle(fixture ? createInterruptionSignal(fixture) : null, wsClosedCause(ws), (onGone) => subscribeClose(ws, () => onGone("client")));
185
+ }
186
+ /**
187
+ * Journal a WS turn that stopped because its lifecycle aborted or its socket
188
+ * closed. This is the closed-socket rule every WS transport uses: the
189
+ * lifecycle's reason when it has one, else the closed socket's cause
190
+ * ({@link wsClosedCause}): {@link AIMOCK_DISCONNECTED} for aimock's own close,
191
+ * {@link CLIENT_ABORTED} for the client. A turn with no lifecycle (it never
192
+ * waits) passes none.
193
+ */
194
+ function markWsTurnStopped(entry, ws, lifecycle) {
195
+ const cause = wsClosedCause(ws);
196
+ const socketReason = cause === "server" ? AIMOCK_DISCONNECTED : cause === "client" ? CLIENT_ABORTED : void 0;
197
+ markInterrupted(entry, lifecycle?.reason() ?? socketReason);
198
+ }
199
+ /**
200
+ * Mark a journal entry interrupted. The reason is set only when the entry has
201
+ * none, so a later `undefined` never erases an earlier reason. The status is
202
+ * never changed: the entry keeps the status it was journaled with.
203
+ */
204
+ function markInterrupted(entry, reason) {
205
+ entry.response.interrupted = true;
206
+ if (entry.response.interruptReason === void 0 && reason !== void 0) entry.response.interruptReason = reason;
207
+ }
208
+
209
+ //#endregion
210
+ export { AIMOCK_DISCONNECTED, CLIENT_ABORTED, closedByClient, httpLifecycle, markInterrupted, markWsTurnStopped, noteDeliveryStart, requestSocket, statusLineSent, teardownCause, teardownReason, watchWsClose, wsLifecycle };
211
+ //# sourceMappingURL=lifecycle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lifecycle.js","names":[],"sources":["../../src/response/lifecycle.ts"],"sourcesContent":["import type * as http from \"node:http\";\nimport type { Fixture, JournalEntry } from \"../types.js\";\nimport type { WebSocketConnection } from \"../ws-framing.js\";\nimport { createInterruptionSignal, type InterruptionControl } from \"../interruption.js\";\nimport { responseGoneReason } from \"../chaos.js\";\n\n/** The journal `interruptReason` for a client that left before the reply finished. */\nexport const CLIENT_ABORTED = \"client aborted\";\n\n/**\n * The journal `interruptReason` for a response aimock tore down itself: the\n * chaos `disconnect` action or a server-side `destroy`. It is the name the\n * chaos journal entry records (`chaosAction: \"disconnect\"`), so a teardown\n * aimock caused is never blamed on the client.\n */\nexport const AIMOCK_DISCONNECTED = \"disconnect\";\n\n/**\n * Which side stopped a delivery first: the client, the fixture's own\n * interruption, or aimock tearing the response down (`\"server\"`).\n */\nexport type InterruptCause = \"client\" | \"fixture\" | \"server\";\n\n/**\n * One delivery's stop signal. It merges the fixture interruption\n * (`truncateAfterChunks` / `disconnectAfterMs`) with the client going away\n * and with aimock tearing the response down (`\"server\"`).\n * The first cause wins: a later cause never reclassifies it.\n */\nexport interface DeliveryLifecycle {\n /** Aborts on the first cause: fixture, client, or server. */\n readonly signal: AbortSignal;\n /** The cause that aborted `signal`, or `undefined` while it has not aborted. */\n cause(): InterruptCause | undefined;\n /**\n * The fixture's reason, {@link CLIENT_ABORTED}, {@link AIMOCK_DISCONNECTED},\n * or `undefined` while running.\n */\n reason(): string | undefined;\n /** Forwards a sent chunk to the fixture interruption (counts toward `truncateAfterChunks`). */\n tick(): void;\n /** Removes the close and fixture-abort listeners and clears the fixture's `disconnectAfterMs` timer. */\n dispose(): void;\n}\n\n/** The part of an EventEmitter a close source may expose. Test doubles may carry less. */\ninterface CloseSource {\n once?: (event: \"close\", listener: () => void) => unknown;\n off?: (event: \"close\", listener: () => void) => unknown;\n removeListener?: (event: \"close\", listener: () => void) => unknown;\n}\n\n/**\n * Subscribe to `close`, guarded the same way as `awaitChaosLatency`: a partial\n * test double without `once` is not subscribed, and only a listener that was\n * added is ever removed, through a remover that exists.\n */\nfunction subscribeClose(source: CloseSource, onClose: () => void): (() => void) | undefined {\n if (typeof source.once !== \"function\") return undefined;\n source.once(\"close\", onClose);\n if (typeof source.off === \"function\") return () => source.off?.(\"close\", onClose);\n if (typeof source.removeListener === \"function\")\n return () => source.removeListener?.(\"close\", onClose);\n return undefined;\n}\n\nfunction createLifecycle(\n interruption: InterruptionControl | null,\n goneAtStart: InterruptCause | undefined,\n subscribe: (onGone: (cause: InterruptCause) => void) => (() => void) | undefined,\n): DeliveryLifecycle {\n const controller = new AbortController();\n let firstCause: InterruptCause | undefined;\n const abortWith = (cause: InterruptCause): void => {\n if (controller.signal.aborted) return;\n firstCause = cause;\n controller.abort();\n };\n const onFixtureAbort = (): void => abortWith(\"fixture\");\n interruption?.signal.addEventListener(\"abort\", onFixtureAbort, { once: true });\n let unsubscribe: (() => void) | undefined;\n if (goneAtStart !== undefined) abortWith(goneAtStart);\n else unsubscribe = subscribe(abortWith);\n return {\n signal: controller.signal,\n cause: () => firstCause,\n reason: () => {\n if (firstCause === \"fixture\") return interruption?.reason();\n if (firstCause === \"client\") return CLIENT_ABORTED;\n if (firstCause === \"server\") return AIMOCK_DISCONNECTED;\n return undefined;\n },\n tick: () => {\n interruption?.tick();\n },\n dispose: () => {\n unsubscribe?.();\n unsubscribe = undefined;\n interruption?.signal.removeEventListener(\"abort\", onFixtureAbort);\n interruption?.cleanup();\n },\n };\n}\n\n/** The request socket fields the teardown rule and the status-line rule read. */\nexport interface PeerSocket {\n readonly errored?: Error | null;\n readonly writableEnded?: boolean;\n readonly bytesWritten?: number;\n}\n\n/** Where a response's request socket can be read from. Test doubles may carry neither. */\ninterface SocketOwner {\n readonly req?: { readonly socket?: PeerSocket | null };\n readonly socket?: PeerSocket | null;\n}\n\n/** The request socket of `res`, or `undefined` when it carries none. */\nexport function requestSocket(res: http.ServerResponse): PeerSocket | undefined {\n const owner: SocketOwner = res;\n return owner.req?.socket ?? owner.socket ?? undefined;\n}\n\n/**\n * THE client hang-up rule, shared by the delivery lifecycle and the proxy\n * relay: the client's socket closed or reset.\n *\n * - A reset, or a write into a socket the peer closed, leaves `errored` set\n * (ECONNRESET / EPIPE).\n * - aimock's server keeps Node's default `httpAllowHalfOpen: false`, so a\n * client FIN makes Node end our write side: the socket is `writableEnded`\n * while the response is not. That is the evidence the client closed.\n *\n * A FIN alone (`readableEnded` with neither of those) is not a hang-up: it is\n * what the socket shows when aimock tore the response down before Node\n * answered the FIN. A real client FIN that Node has not answered yet looks the\n * same, so this rule also classifies it as not a hang-up. aimock's own\n * `destroy()` leaves neither signal.\n */\nexport function closedByClient(socket: PeerSocket): boolean {\n return (socket.errored ?? null) !== null || socket.writableEnded === true;\n}\n\n/**\n * Who tore down a response that is gone without having ended. After the fact\n * `res.destroyed` reads the same either way, so this reads the evidence:\n *\n * - a chaos `disconnect` marked the response before destroying it: aimock;\n * - the request socket meets {@link closedByClient}: the client;\n * - neither: aimock destroyed it server-side.\n *\n * `socket` defaults to the response's request socket. With no socket to read\n * (a partial test double) the client attribution is kept.\n */\nexport function teardownCause(\n res: http.ServerResponse,\n socket: PeerSocket | null | undefined = requestSocket(res),\n): \"client\" | \"server\" {\n if (responseGoneReason(res) === \"chaos-disconnected\") return \"server\";\n if (!socket) return \"client\";\n return closedByClient(socket) ? \"client\" : \"server\";\n}\n\n/**\n * The request socket's `bytesWritten` when each delivery started, so the\n * status-line rule counts only this response's bytes on a keep-alive socket.\n */\nconst deliveryStartBytes = new WeakMap<http.ServerResponse, number>();\n\n/**\n * Record where this response's bytes start on its socket. A response whose\n * head was already committed when the delivery started is not recorded:\n * {@link statusLineSent} keeps the `headersSent` rule for it.\n */\nexport function noteDeliveryStart(res: http.ServerResponse): void {\n if (deliveryStartBytes.has(res) || res.headersSent) return;\n const bytes = requestSocket(res)?.bytesWritten;\n if (typeof bytes === \"number\") deliveryStartBytes.set(res, bytes);\n}\n\n/**\n * Whether this response's status line reached the client's socket: a byte of\n * it was handed to the socket after {@link noteDeliveryStart}. `headersSent`\n * is not that test: `writeHead` sets it before any byte is written. With no\n * recorded start, or no socket to read, `headersSent` is the fallback.\n */\nexport function statusLineSent(res: http.ServerResponse): boolean {\n const start = deliveryStartBytes.get(res);\n const bytes = requestSocket(res)?.bytesWritten;\n if (start === undefined || typeof bytes !== \"number\") return res.headersSent;\n return bytes > start;\n}\n\n/** The journal `interruptReason` for {@link teardownCause}. */\nexport function teardownReason(res: http.ServerResponse): string {\n return teardownCause(res) === \"client\" ? CLIENT_ABORTED : AIMOCK_DISCONNECTED;\n}\n\n/**\n * HTTP delivery lifecycle. A `close` before `res.writableFinished` aborts it,\n * and {@link teardownCause} decides whose teardown it was: the client\n * (`\"client\"`) or aimock itself (`\"server\"`). A response that is already gone\n * at construction aborts at once with the same attribution. A chaos\n * `disconnect` destroys without ending, so no `writableEnded` check stops a\n * writer for it. Only an already-ended response does not abort: the frame\n * loop exits on its `writableEnded` check. The fixture interruption is\n * created only when `streaming` is true (it is streaming-only).\n */\nexport function httpLifecycle(\n res: http.ServerResponse,\n fixture: Fixture | null,\n opts: { streaming: boolean },\n): DeliveryLifecycle {\n const interruption = opts.streaming && fixture ? createInterruptionSignal(fixture) : null;\n const gone = responseGoneReason(res);\n const goneAtStart = gone === null || gone === \"already-ended\" ? undefined : teardownCause(res);\n return createLifecycle(interruption, goneAtStart, (onGone) =>\n subscribeClose(res, () => {\n if (!res.writableFinished) onGone(teardownCause(res));\n }),\n );\n}\n\n/**\n * Per watched connection: whether its `close` event has fired. Only sockets\n * watched while open are recorded, so an entry is evidence, not a guess.\n */\nconst wsCloseEmitted = new WeakMap<WebSocketConnection, boolean>();\n\n/**\n * Start recording when `ws` emits `close`, so {@link wsClosedCause} can tell\n * aimock's own close from the client's. Each WS transport calls it when the\n * connection opens. Idempotent, and a no-op once closed.\n */\nexport function watchWsClose(ws: WebSocketConnection): void {\n if (ws.isClosed || wsCloseEmitted.has(ws)) return;\n const source: CloseSource = ws;\n if (typeof source.once !== \"function\") return;\n wsCloseEmitted.set(ws, false);\n source.once(\"close\", () => wsCloseEmitted.set(ws, true));\n}\n\n/**\n * Who closed a socket that reads `isClosed`, or `undefined` while it is open.\n *\n * `WebSocketConnection` marks itself closed and emits `close` in the same step\n * for a client close frame, a lost connection, and `destroy()`. Only aimock's\n * own `close()` (a write or buffer limit, a bad frame, a strict miss, shutdown)\n * marks it closed at once and emits `close` about 100ms later. So a socket\n * watched while open ({@link watchWsClose}) that is closed with no `close` yet\n * was closed by aimock (`\"server\"`). Any other closed socket reads as the\n * client leaving, as before.\n */\nexport function wsClosedCause(ws: WebSocketConnection): \"client\" | \"server\" | undefined {\n if (!ws.isClosed) return undefined;\n return wsCloseEmitted.get(ws) === false ? \"server\" : \"client\";\n}\n\n/**\n * WebSocket turn lifecycle. A WS turn is always a stream, so it always owns\n * the fixture interruption. A `close` means the client left. A socket already\n * closed at construction aborts at once with {@link wsClosedCause}. Callers\n * never create their own interruption object.\n */\nexport function wsLifecycle(ws: WebSocketConnection, fixture: Fixture | null): DeliveryLifecycle {\n const interruption = fixture ? createInterruptionSignal(fixture) : null;\n return createLifecycle(interruption, wsClosedCause(ws), (onGone) =>\n subscribeClose(ws, () => onGone(\"client\")),\n );\n}\n\n/**\n * Journal a WS turn that stopped because its lifecycle aborted or its socket\n * closed. This is the closed-socket rule every WS transport uses: the\n * lifecycle's reason when it has one, else the closed socket's cause\n * ({@link wsClosedCause}): {@link AIMOCK_DISCONNECTED} for aimock's own close,\n * {@link CLIENT_ABORTED} for the client. A turn with no lifecycle (it never\n * waits) passes none.\n */\nexport function markWsTurnStopped(\n entry: JournalEntry,\n ws: WebSocketConnection,\n lifecycle?: DeliveryLifecycle,\n): void {\n const cause = wsClosedCause(ws);\n const socketReason =\n cause === \"server\" ? AIMOCK_DISCONNECTED : cause === \"client\" ? CLIENT_ABORTED : undefined;\n markInterrupted(entry, lifecycle?.reason() ?? socketReason);\n}\n\n/**\n * Mark a journal entry interrupted. The reason is set only when the entry has\n * none, so a later `undefined` never erases an earlier reason. The status is\n * never changed: the entry keeps the status it was journaled with.\n */\nexport function markInterrupted(entry: JournalEntry, reason: string | undefined): void {\n entry.response.interrupted = true;\n if (entry.response.interruptReason === undefined && reason !== undefined) {\n entry.response.interruptReason = reason;\n }\n}\n"],"mappings":"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"}
@@ -0,0 +1,71 @@
1
+ const require_lifecycle = require('./lifecycle.cjs');
2
+
3
+ //#region src/response/proxy.ts
4
+ /**
5
+ * Responses torn down before they finished, judged at entry, on `close`, and
6
+ * when the relay returns. Reading `res.writableFinished` only when journaling
7
+ * is too late: on Node 20 and 22 a relay that calls `end()` on a destroyed
8
+ * response makes it read `true` (Node 24+ keeps it `false`).
9
+ */
10
+ const tornDownBeforeFinish = /* @__PURE__ */ new WeakSet();
11
+ /**
12
+ * Run a proxy relay while watching for a client hang-up.
13
+ *
14
+ * WHO killed a mid-flight stream decides what the journal says, and after the
15
+ * fact the two look identical: `req.aborted`, `req.destroyed` and
16
+ * `res.destroyed` all read the same whether the peer hung up or the recorder
17
+ * tore the response down. So the hang-up is decided when the request socket
18
+ * actually closes before the response ended, by the same rule the delivery
19
+ * lifecycle uses ({@link teardownCause}): the client's socket closed or reset.
20
+ * A FIN alone is not a hang-up. A response already gone when this is entered
21
+ * is judged by that rule too, so a response aimock destroyed is not blamed on
22
+ * the client. The listener is attached before `run` and removed after it: a
23
+ * keep-alive socket outlives this request.
24
+ */
25
+ async function withHangUpDetection(req, res, run) {
26
+ const socket = req.socket;
27
+ require_lifecycle.noteDeliveryStart(res);
28
+ const clientCaused = () => !res.writableEnded && require_lifecycle.teardownCause(res, socket) === "client";
29
+ let clientHungUp = (res.destroyed || socket?.destroyed === true) && clientCaused();
30
+ const noteSocketClose = () => {
31
+ if (clientCaused()) clientHungUp = true;
32
+ };
33
+ const noteTearDown = () => {
34
+ if (res.destroyed && !res.writableFinished) tornDownBeforeFinish.add(res);
35
+ };
36
+ noteTearDown();
37
+ socket?.on("close", noteSocketClose);
38
+ res.on("close", noteTearDown);
39
+ try {
40
+ return {
41
+ outcome: await run(),
42
+ clientHungUp
43
+ };
44
+ } finally {
45
+ noteTearDown();
46
+ socket?.off("close", noteSocketClose);
47
+ res.off("close", noteTearDown);
48
+ }
49
+ }
50
+ /**
51
+ * The journal fields for a proxied response. A stream torn down before it
52
+ * finished (destroyed, even when the relay called `end()` on it afterwards)
53
+ * is marked interrupted and names the side that killed it: a client hang-up
54
+ * is normal caller behaviour, an upstream/recorder tear-down is a fault worth
55
+ * chasing. When no byte of the response reached the socket
56
+ * ({@link statusLineSent}) the status is 0: the client never got a status
57
+ * line. A response that finished, or was never torn down, gets no fields.
58
+ */
59
+ function proxyInterruption(res, clientHungUp) {
60
+ if (!(res.destroyed && (tornDownBeforeFinish.has(res) || !res.writableFinished))) return {};
61
+ return {
62
+ ...require_lifecycle.statusLineSent(res) ? {} : { status: 0 },
63
+ interrupted: true,
64
+ interruptReason: clientHungUp ? require_lifecycle.CLIENT_ABORTED : "proxy stream destroyed"
65
+ };
66
+ }
67
+
68
+ //#endregion
69
+ exports.proxyInterruption = proxyInterruption;
70
+ exports.withHangUpDetection = withHangUpDetection;
71
+ //# sourceMappingURL=proxy.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.cjs","names":["teardownCause","statusLineSent","CLIENT_ABORTED"],"sources":["../../src/response/proxy.ts"],"sourcesContent":["import type * as http from \"node:http\";\nimport { CLIENT_ABORTED, noteDeliveryStart, statusLineSent, teardownCause } from \"./lifecycle.js\";\n\n/**\n * Responses torn down before they finished, judged at entry, on `close`, and\n * when the relay returns. Reading `res.writableFinished` only when journaling\n * is too late: on Node 20 and 22 a relay that calls `end()` on a destroyed\n * response makes it read `true` (Node 24+ keeps it `false`).\n */\nconst tornDownBeforeFinish = new WeakSet<http.ServerResponse>();\n\n/**\n * Run a proxy relay while watching for a client hang-up.\n *\n * WHO killed a mid-flight stream decides what the journal says, and after the\n * fact the two look identical: `req.aborted`, `req.destroyed` and\n * `res.destroyed` all read the same whether the peer hung up or the recorder\n * tore the response down. So the hang-up is decided when the request socket\n * actually closes before the response ended, by the same rule the delivery\n * lifecycle uses ({@link teardownCause}): the client's socket closed or reset.\n * A FIN alone is not a hang-up. A response already gone when this is entered\n * is judged by that rule too, so a response aimock destroyed is not blamed on\n * the client. The listener is attached before `run` and removed after it: a\n * keep-alive socket outlives this request.\n */\nexport async function withHangUpDetection<T>(\n req: http.IncomingMessage,\n res: http.ServerResponse,\n run: () => Promise<T>,\n): Promise<{ outcome: T; clientHungUp: boolean }> {\n const socket = req.socket;\n noteDeliveryStart(res);\n const clientCaused = (): boolean => !res.writableEnded && teardownCause(res, socket) === \"client\";\n let clientHungUp = (res.destroyed || socket?.destroyed === true) && clientCaused();\n const noteSocketClose = (): void => {\n if (clientCaused()) clientHungUp = true;\n };\n const noteTearDown = (): void => {\n if (res.destroyed && !res.writableFinished) tornDownBeforeFinish.add(res);\n };\n noteTearDown();\n socket?.on(\"close\", noteSocketClose);\n res.on(\"close\", noteTearDown);\n try {\n const outcome = await run();\n return { outcome, clientHungUp };\n } finally {\n // The relay may have destroyed the response in its last tick, before\n // `close` is emitted.\n noteTearDown();\n socket?.off(\"close\", noteSocketClose);\n res.off(\"close\", noteTearDown);\n }\n}\n\n/**\n * The journal fields for a proxied response. A stream torn down before it\n * finished (destroyed, even when the relay called `end()` on it afterwards)\n * is marked interrupted and names the side that killed it: a client hang-up\n * is normal caller behaviour, an upstream/recorder tear-down is a fault worth\n * chasing. When no byte of the response reached the socket\n * ({@link statusLineSent}) the status is 0: the client never got a status\n * line. A response that finished, or was never torn down, gets no fields.\n */\nexport function proxyInterruption(\n res: http.ServerResponse,\n clientHungUp: boolean,\n): { status?: 0; interrupted?: true; interruptReason?: string } {\n const tornDown = res.destroyed && (tornDownBeforeFinish.has(res) || !res.writableFinished);\n if (!tornDown) return {};\n return {\n ...(statusLineSent(res) ? {} : { status: 0 as const }),\n interrupted: true,\n interruptReason: clientHungUp ? CLIENT_ABORTED : \"proxy stream destroyed\",\n };\n}\n"],"mappings":";;;;;;;;;AASA,MAAM,uCAAuB,IAAI,SAA8B;;;;;;;;;;;;;;;AAgB/D,eAAsB,oBACpB,KACA,KACA,KACgD;CAChD,MAAM,SAAS,IAAI;AACnB,qCAAkB,IAAI;CACtB,MAAM,qBAA8B,CAAC,IAAI,iBAAiBA,gCAAc,KAAK,OAAO,KAAK;CACzF,IAAI,gBAAgB,IAAI,aAAa,QAAQ,cAAc,SAAS,cAAc;CAClF,MAAM,wBAA8B;AAClC,MAAI,cAAc,CAAE,gBAAe;;CAErC,MAAM,qBAA2B;AAC/B,MAAI,IAAI,aAAa,CAAC,IAAI,iBAAkB,sBAAqB,IAAI,IAAI;;AAE3E,eAAc;AACd,SAAQ,GAAG,SAAS,gBAAgB;AACpC,KAAI,GAAG,SAAS,aAAa;AAC7B,KAAI;AAEF,SAAO;GAAE,SADO,MAAM,KAAK;GACT;GAAc;WACxB;AAGR,gBAAc;AACd,UAAQ,IAAI,SAAS,gBAAgB;AACrC,MAAI,IAAI,SAAS,aAAa;;;;;;;;;;;;AAalC,SAAgB,kBACd,KACA,cAC8D;AAE9D,KAAI,EADa,IAAI,cAAc,qBAAqB,IAAI,IAAI,IAAI,CAAC,IAAI,mBAC1D,QAAO,EAAE;AACxB,QAAO;EACL,GAAIC,iCAAe,IAAI,GAAG,EAAE,GAAG,EAAE,QAAQ,GAAY;EACrD,aAAa;EACb,iBAAiB,eAAeC,mCAAiB;EAClD"}
@@ -0,0 +1,70 @@
1
+ import { CLIENT_ABORTED, noteDeliveryStart, statusLineSent, teardownCause } from "./lifecycle.js";
2
+
3
+ //#region src/response/proxy.ts
4
+ /**
5
+ * Responses torn down before they finished, judged at entry, on `close`, and
6
+ * when the relay returns. Reading `res.writableFinished` only when journaling
7
+ * is too late: on Node 20 and 22 a relay that calls `end()` on a destroyed
8
+ * response makes it read `true` (Node 24+ keeps it `false`).
9
+ */
10
+ const tornDownBeforeFinish = /* @__PURE__ */ new WeakSet();
11
+ /**
12
+ * Run a proxy relay while watching for a client hang-up.
13
+ *
14
+ * WHO killed a mid-flight stream decides what the journal says, and after the
15
+ * fact the two look identical: `req.aborted`, `req.destroyed` and
16
+ * `res.destroyed` all read the same whether the peer hung up or the recorder
17
+ * tore the response down. So the hang-up is decided when the request socket
18
+ * actually closes before the response ended, by the same rule the delivery
19
+ * lifecycle uses ({@link teardownCause}): the client's socket closed or reset.
20
+ * A FIN alone is not a hang-up. A response already gone when this is entered
21
+ * is judged by that rule too, so a response aimock destroyed is not blamed on
22
+ * the client. The listener is attached before `run` and removed after it: a
23
+ * keep-alive socket outlives this request.
24
+ */
25
+ async function withHangUpDetection(req, res, run) {
26
+ const socket = req.socket;
27
+ noteDeliveryStart(res);
28
+ const clientCaused = () => !res.writableEnded && teardownCause(res, socket) === "client";
29
+ let clientHungUp = (res.destroyed || socket?.destroyed === true) && clientCaused();
30
+ const noteSocketClose = () => {
31
+ if (clientCaused()) clientHungUp = true;
32
+ };
33
+ const noteTearDown = () => {
34
+ if (res.destroyed && !res.writableFinished) tornDownBeforeFinish.add(res);
35
+ };
36
+ noteTearDown();
37
+ socket?.on("close", noteSocketClose);
38
+ res.on("close", noteTearDown);
39
+ try {
40
+ return {
41
+ outcome: await run(),
42
+ clientHungUp
43
+ };
44
+ } finally {
45
+ noteTearDown();
46
+ socket?.off("close", noteSocketClose);
47
+ res.off("close", noteTearDown);
48
+ }
49
+ }
50
+ /**
51
+ * The journal fields for a proxied response. A stream torn down before it
52
+ * finished (destroyed, even when the relay called `end()` on it afterwards)
53
+ * is marked interrupted and names the side that killed it: a client hang-up
54
+ * is normal caller behaviour, an upstream/recorder tear-down is a fault worth
55
+ * chasing. When no byte of the response reached the socket
56
+ * ({@link statusLineSent}) the status is 0: the client never got a status
57
+ * line. A response that finished, or was never torn down, gets no fields.
58
+ */
59
+ function proxyInterruption(res, clientHungUp) {
60
+ if (!(res.destroyed && (tornDownBeforeFinish.has(res) || !res.writableFinished))) return {};
61
+ return {
62
+ ...statusLineSent(res) ? {} : { status: 0 },
63
+ interrupted: true,
64
+ interruptReason: clientHungUp ? CLIENT_ABORTED : "proxy stream destroyed"
65
+ };
66
+ }
67
+
68
+ //#endregion
69
+ export { proxyInterruption, withHangUpDetection };
70
+ //# sourceMappingURL=proxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.js","names":[],"sources":["../../src/response/proxy.ts"],"sourcesContent":["import type * as http from \"node:http\";\nimport { CLIENT_ABORTED, noteDeliveryStart, statusLineSent, teardownCause } from \"./lifecycle.js\";\n\n/**\n * Responses torn down before they finished, judged at entry, on `close`, and\n * when the relay returns. Reading `res.writableFinished` only when journaling\n * is too late: on Node 20 and 22 a relay that calls `end()` on a destroyed\n * response makes it read `true` (Node 24+ keeps it `false`).\n */\nconst tornDownBeforeFinish = new WeakSet<http.ServerResponse>();\n\n/**\n * Run a proxy relay while watching for a client hang-up.\n *\n * WHO killed a mid-flight stream decides what the journal says, and after the\n * fact the two look identical: `req.aborted`, `req.destroyed` and\n * `res.destroyed` all read the same whether the peer hung up or the recorder\n * tore the response down. So the hang-up is decided when the request socket\n * actually closes before the response ended, by the same rule the delivery\n * lifecycle uses ({@link teardownCause}): the client's socket closed or reset.\n * A FIN alone is not a hang-up. A response already gone when this is entered\n * is judged by that rule too, so a response aimock destroyed is not blamed on\n * the client. The listener is attached before `run` and removed after it: a\n * keep-alive socket outlives this request.\n */\nexport async function withHangUpDetection<T>(\n req: http.IncomingMessage,\n res: http.ServerResponse,\n run: () => Promise<T>,\n): Promise<{ outcome: T; clientHungUp: boolean }> {\n const socket = req.socket;\n noteDeliveryStart(res);\n const clientCaused = (): boolean => !res.writableEnded && teardownCause(res, socket) === \"client\";\n let clientHungUp = (res.destroyed || socket?.destroyed === true) && clientCaused();\n const noteSocketClose = (): void => {\n if (clientCaused()) clientHungUp = true;\n };\n const noteTearDown = (): void => {\n if (res.destroyed && !res.writableFinished) tornDownBeforeFinish.add(res);\n };\n noteTearDown();\n socket?.on(\"close\", noteSocketClose);\n res.on(\"close\", noteTearDown);\n try {\n const outcome = await run();\n return { outcome, clientHungUp };\n } finally {\n // The relay may have destroyed the response in its last tick, before\n // `close` is emitted.\n noteTearDown();\n socket?.off(\"close\", noteSocketClose);\n res.off(\"close\", noteTearDown);\n }\n}\n\n/**\n * The journal fields for a proxied response. A stream torn down before it\n * finished (destroyed, even when the relay called `end()` on it afterwards)\n * is marked interrupted and names the side that killed it: a client hang-up\n * is normal caller behaviour, an upstream/recorder tear-down is a fault worth\n * chasing. When no byte of the response reached the socket\n * ({@link statusLineSent}) the status is 0: the client never got a status\n * line. A response that finished, or was never torn down, gets no fields.\n */\nexport function proxyInterruption(\n res: http.ServerResponse,\n clientHungUp: boolean,\n): { status?: 0; interrupted?: true; interruptReason?: string } {\n const tornDown = res.destroyed && (tornDownBeforeFinish.has(res) || !res.writableFinished);\n if (!tornDown) return {};\n return {\n ...(statusLineSent(res) ? {} : { status: 0 as const }),\n interrupted: true,\n interruptReason: clientHungUp ? CLIENT_ABORTED : \"proxy stream destroyed\",\n };\n}\n"],"mappings":";;;;;;;;;AASA,MAAM,uCAAuB,IAAI,SAA8B;;;;;;;;;;;;;;;AAgB/D,eAAsB,oBACpB,KACA,KACA,KACgD;CAChD,MAAM,SAAS,IAAI;AACnB,mBAAkB,IAAI;CACtB,MAAM,qBAA8B,CAAC,IAAI,iBAAiB,cAAc,KAAK,OAAO,KAAK;CACzF,IAAI,gBAAgB,IAAI,aAAa,QAAQ,cAAc,SAAS,cAAc;CAClF,MAAM,wBAA8B;AAClC,MAAI,cAAc,CAAE,gBAAe;;CAErC,MAAM,qBAA2B;AAC/B,MAAI,IAAI,aAAa,CAAC,IAAI,iBAAkB,sBAAqB,IAAI,IAAI;;AAE3E,eAAc;AACd,SAAQ,GAAG,SAAS,gBAAgB;AACpC,KAAI,GAAG,SAAS,aAAa;AAC7B,KAAI;AAEF,SAAO;GAAE,SADO,MAAM,KAAK;GACT;GAAc;WACxB;AAGR,gBAAc;AACd,UAAQ,IAAI,SAAS,gBAAgB;AACrC,MAAI,IAAI,SAAS,aAAa;;;;;;;;;;;;AAalC,SAAgB,kBACd,KACA,cAC8D;AAE9D,KAAI,EADa,IAAI,cAAc,qBAAqB,IAAI,IAAI,IAAI,CAAC,IAAI,mBAC1D,QAAO,EAAE;AACxB,QAAO;EACL,GAAI,eAAe,IAAI,GAAG,EAAE,GAAG,EAAE,QAAQ,GAAY;EACrD,aAAa;EACb,iBAAiB,eAAe,iBAAiB;EAClD"}