@seanhogg/builderforce-webcontainers 2026.10.7 → 2026.10.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets.js +3 -3
- package/dist/cdnModules.d.ts +16 -0
- package/dist/cdnModules.d.ts.map +1 -0
- package/dist/cdnModules.js +25 -0
- package/dist/cdnModules.js.map +1 -0
- package/dist/componentCompilers.d.ts +5 -5
- package/dist/componentCompilers.d.ts.map +1 -1
- package/dist/componentCompilers.js +5 -23
- package/dist/componentCompilers.js.map +1 -1
- package/dist/host.d.ts +17 -3
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +36 -22
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/node/childRelay.d.ts +22 -0
- package/dist/node/childRelay.d.ts.map +1 -0
- package/dist/node/childRelay.js +54 -0
- package/dist/node/childRelay.js.map +1 -0
- package/dist/node/index.d.ts +4 -8
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +1 -25
- package/dist/node/index.js.map +1 -1
- package/dist/node/portResponder.d.ts +45 -0
- package/dist/node/portResponder.d.ts.map +1 -0
- package/dist/node/portResponder.js +136 -0
- package/dist/node/portResponder.js.map +1 -0
- package/dist/node/processHost.d.ts +13 -2
- package/dist/node/processHost.d.ts.map +1 -1
- package/dist/node/processHost.js +79 -47
- package/dist/node/processHost.js.map +1 -1
- package/dist/node/protocol.d.ts +69 -13
- package/dist/node/protocol.d.ts.map +1 -1
- package/dist/node/protocol.js.map +1 -1
- package/dist/node/responseWire.d.ts +27 -0
- package/dist/node/responseWire.d.ts.map +1 -0
- package/dist/node/responseWire.js +99 -0
- package/dist/node/responseWire.js.map +1 -0
- package/dist/node/worker.js +145 -126
- package/dist/node/workerProcess.d.ts +3 -1
- package/dist/node/workerProcess.d.ts.map +1 -1
- package/dist/node/workerProcess.js +79 -22
- package/dist/node/workerProcess.js.map +1 -1
- package/dist/previewBridge.d.ts +37 -0
- package/dist/previewBridge.d.ts.map +1 -0
- package/dist/previewBridge.js +116 -0
- package/dist/previewBridge.js.map +1 -0
- package/dist/previewMessages.d.ts +5 -0
- package/dist/previewMessages.d.ts.map +1 -1
- package/dist/previewMessages.js +18 -11
- package/dist/previewMessages.js.map +1 -1
- package/dist/previewUpdates.d.ts +10 -0
- package/dist/previewUpdates.d.ts.map +1 -0
- package/dist/previewUpdates.js +27 -0
- package/dist/previewUpdates.js.map +1 -0
- package/dist/process-worker.js +145 -126
- package/dist/protocol.d.ts +25 -3
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +10 -2
- package/dist/protocol.js.map +1 -1
- package/dist/relay.html +1 -1
- package/dist/snapshotStore.d.ts +30 -0
- package/dist/snapshotStore.d.ts.map +1 -0
- package/dist/snapshotStore.js +115 -0
- package/dist/snapshotStore.js.map +1 -0
- package/dist/styleCompilers.d.ts +19 -0
- package/dist/styleCompilers.d.ts.map +1 -0
- package/dist/styleCompilers.js +22 -0
- package/dist/styleCompilers.js.map +1 -0
- package/dist/sw.js +1 -1
- package/dist/swRouter.d.ts +8 -1
- package/dist/swRouter.d.ts.map +1 -1
- package/dist/swRouter.js +40 -5
- package/dist/swRouter.js.map +1 -1
- package/dist/transport.d.ts +3 -2
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +4 -4
- package/dist/transport.js.map +1 -1
- package/dist/worker.d.ts +6 -2
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +5 -5
- package/dist/worker.js.map +1 -1
- package/package.json +3 -2
- package/src/cdnModules.ts +34 -0
- package/src/componentCompilers.ts +8 -32
- package/src/host.ts +51 -25
- package/src/index.ts +9 -2
- package/src/node/childRelay.ts +63 -0
- package/src/node/index.ts +4 -23
- package/src/node/portResponder.ts +175 -0
- package/src/node/processHost.ts +96 -46
- package/src/node/protocol.ts +46 -9
- package/src/node/responseWire.ts +97 -0
- package/src/node/workerProcess.ts +88 -28
- package/src/previewBridge.ts +148 -0
- package/src/previewMessages.ts +17 -9
- package/src/previewUpdates.ts +28 -0
- package/src/protocol.ts +37 -3
- package/src/relay.ts +6 -5
- package/src/snapshotStore.ts +135 -0
- package/src/styleCompilers.ts +45 -0
- package/src/sw.ts +2 -0
- package/src/swRouter.ts +35 -6
- package/src/transport.ts +7 -6
- package/src/worker.ts +5 -5
package/dist/node/protocol.d.ts
CHANGED
|
@@ -2,8 +2,19 @@
|
|
|
2
2
|
* Messages between the page (the process host, which owns the master file
|
|
3
3
|
* system and the port table) and one process Worker. Everything is plain
|
|
4
4
|
* structured-clone data; file contents travel as strings or bytes.
|
|
5
|
+
*
|
|
6
|
+
* HTTP responses stream in both directions with the same three messages: a
|
|
7
|
+
* `response-head`, `response-chunk`s as the server writes, a `response-end`.
|
|
8
|
+
* Worker → page they answer an `http-request` (ids the page allocated); page →
|
|
9
|
+
* worker they answer a `loopback` (ids the worker allocated). `request-cancel`
|
|
10
|
+
* goes the other way: the reader hung up. Upgraded connections (WebSockets)
|
|
11
|
+
* are opened by the page (`socket-open`) and carry raw bytes both ways.
|
|
12
|
+
*
|
|
13
|
+
* A message channel (fork, worker threads) is `ipc` / `ipc-disconnect` between a
|
|
14
|
+
* process and the page (its parent's side), and `child-ipc` /
|
|
15
|
+
* `child-ipc-disconnect` between a parent process and the page (its child's side).
|
|
5
16
|
*/
|
|
6
|
-
import type { FileContent, HttpRequestData,
|
|
17
|
+
import type { FileContent, HttpRequestData, HttpResponseHead, StdioChunk, ThreadInfo } from '@seanhogg/builderforce-webcontainers-core';
|
|
7
18
|
export type FsOp = {
|
|
8
19
|
op: 'write';
|
|
9
20
|
path: string;
|
|
@@ -26,6 +37,9 @@ export interface StartMessage {
|
|
|
26
37
|
cols: number;
|
|
27
38
|
rows: number;
|
|
28
39
|
};
|
|
40
|
+
/** A message channel to the parent is open (`process.send`, a thread's `parentPort`). */
|
|
41
|
+
ipc?: boolean;
|
|
42
|
+
thread?: ThreadInfo;
|
|
29
43
|
files: Array<[string, FileContent]>;
|
|
30
44
|
dirs: string[];
|
|
31
45
|
/** Ports already listening anywhere in the runtime (so `listen` can fail fast with EADDRINUSE). */
|
|
@@ -38,10 +52,47 @@ export interface StartMessage {
|
|
|
38
52
|
/** Use Cache Storage for npm tarballs. */
|
|
39
53
|
packageCache: boolean;
|
|
40
54
|
}
|
|
55
|
+
/** A streamed response, chunk by chunk; `head: null` means nothing listens on that port (loopback only). */
|
|
56
|
+
export type ResponseMessage = {
|
|
57
|
+
type: 'response-head';
|
|
58
|
+
reqId: number;
|
|
59
|
+
head: HttpResponseHead | null;
|
|
60
|
+
} | {
|
|
61
|
+
type: 'response-chunk';
|
|
62
|
+
reqId: number;
|
|
63
|
+
data: Uint8Array;
|
|
64
|
+
} | {
|
|
65
|
+
type: 'response-end';
|
|
66
|
+
reqId: number;
|
|
67
|
+
error?: string;
|
|
68
|
+
};
|
|
69
|
+
/** Raw bytes of an upgraded connection, either way; ids allocated by the page. */
|
|
70
|
+
export type SocketMessage = {
|
|
71
|
+
type: 'socket-data';
|
|
72
|
+
connId: number;
|
|
73
|
+
data: Uint8Array;
|
|
74
|
+
} | {
|
|
75
|
+
type: 'socket-close';
|
|
76
|
+
connId: number;
|
|
77
|
+
};
|
|
78
|
+
/** Message-channel traffic: to/from this process's parent (`ipc`), or one of its children (`child-ipc`). */
|
|
79
|
+
export type IpcMessage = {
|
|
80
|
+
type: 'ipc';
|
|
81
|
+
data: unknown;
|
|
82
|
+
} | {
|
|
83
|
+
type: 'ipc-disconnect';
|
|
84
|
+
} | {
|
|
85
|
+
type: 'child-ipc';
|
|
86
|
+
childId: number;
|
|
87
|
+
data: unknown;
|
|
88
|
+
} | {
|
|
89
|
+
type: 'child-ipc-disconnect';
|
|
90
|
+
childId: number;
|
|
91
|
+
};
|
|
41
92
|
/** page → worker */
|
|
42
93
|
export type HostToWorker = StartMessage | {
|
|
43
94
|
type: 'stdin';
|
|
44
|
-
data:
|
|
95
|
+
data: StdioChunk | null;
|
|
45
96
|
} | {
|
|
46
97
|
type: 'kill';
|
|
47
98
|
signal: string;
|
|
@@ -60,7 +111,7 @@ export type HostToWorker = StartMessage | {
|
|
|
60
111
|
type: 'child-out';
|
|
61
112
|
childId: number;
|
|
62
113
|
stream: 'stdout' | 'stderr';
|
|
63
|
-
data:
|
|
114
|
+
data: StdioChunk;
|
|
64
115
|
} | {
|
|
65
116
|
type: 'child-exit';
|
|
66
117
|
childId: number;
|
|
@@ -76,14 +127,18 @@ export type HostToWorker = StartMessage | {
|
|
|
76
127
|
port: number;
|
|
77
128
|
request: HttpRequestData;
|
|
78
129
|
} | {
|
|
79
|
-
type: '
|
|
130
|
+
type: 'request-cancel';
|
|
80
131
|
reqId: number;
|
|
81
|
-
|
|
82
|
-
|
|
132
|
+
} | {
|
|
133
|
+
type: 'socket-open';
|
|
134
|
+
connId: number;
|
|
135
|
+
port: number;
|
|
136
|
+
request: HttpRequestData;
|
|
137
|
+
} | ResponseMessage | SocketMessage | IpcMessage;
|
|
83
138
|
/** worker → page */
|
|
84
139
|
export type WorkerToHost = {
|
|
85
140
|
type: 'stdout' | 'stderr';
|
|
86
|
-
data:
|
|
141
|
+
data: StdioChunk;
|
|
87
142
|
} | {
|
|
88
143
|
type: 'exit';
|
|
89
144
|
code: number;
|
|
@@ -101,10 +156,12 @@ export type WorkerToHost = {
|
|
|
101
156
|
cols: number;
|
|
102
157
|
rows: number;
|
|
103
158
|
};
|
|
159
|
+
ipc?: boolean;
|
|
160
|
+
thread?: ThreadInfo;
|
|
104
161
|
} | {
|
|
105
162
|
type: 'child-stdin';
|
|
106
163
|
childId: number;
|
|
107
|
-
data:
|
|
164
|
+
data: StdioChunk | null;
|
|
108
165
|
} | {
|
|
109
166
|
type: 'child-kill';
|
|
110
167
|
childId: number;
|
|
@@ -120,20 +177,19 @@ export type WorkerToHost = {
|
|
|
120
177
|
} | {
|
|
121
178
|
type: 'unlisten';
|
|
122
179
|
port: number;
|
|
123
|
-
} | {
|
|
124
|
-
type: 'http-response';
|
|
125
|
-
reqId: number;
|
|
126
|
-
response: HttpResponseData;
|
|
127
180
|
} | {
|
|
128
181
|
type: 'loopback';
|
|
129
182
|
reqId: number;
|
|
130
183
|
port: number;
|
|
131
184
|
request: HttpRequestData;
|
|
185
|
+
} | {
|
|
186
|
+
type: 'request-cancel';
|
|
187
|
+
reqId: number;
|
|
132
188
|
} | {
|
|
133
189
|
type: 'listening';
|
|
134
190
|
port: number;
|
|
135
191
|
url: string;
|
|
136
|
-
};
|
|
192
|
+
} | ResponseMessage | SocketMessage | IpcMessage;
|
|
137
193
|
/** The preview-relative URL a virtual server on `port` is reachable at. */
|
|
138
194
|
export declare function portPath(previewBase: string, port: number): string;
|
|
139
195
|
//# sourceMappingURL=protocol.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/node/protocol.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/node/protocol.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,gBAAgB,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,2CAA2C,CAAC;AAExI,MAAM,MAAM,IAAI,GAAG;IAAE,EAAE,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,WAAW,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAExI,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,OAAO,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,M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AC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACvD;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACrE;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAChC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAClC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,eAAe,CAAA;CAAE,GAC3E;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACzC;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAChD,eAAe,GACf,aAAa,GACb,UAAU,CAAC;AAEf,2EAA2E;AAC3E,wBAAgB,QAAQ,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAElE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../src/node/protocol.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../src/node/protocol.ts"],"names":[],"mappings":"AAgGA,2EAA2E;AAC3E,MAAM,UAAU,QAAQ,CAAC,WAAmB,EAAE,IAAY;IACxD,OAAO,GAAG,WAAW,UAAU,IAAI,GAAG,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streamed HTTP responses over postMessage, used on both sides of the
|
|
3
|
+
* page ↔ process-worker boundary: the side that holds a response sends it as
|
|
4
|
+
* head / chunk / end messages as the server writes it, and the side that asked
|
|
5
|
+
* rebuilds an `HttpStreamResponse` whose body stream is fed by them. A reader
|
|
6
|
+
* that cancels the body sends `request-cancel` back, so the server hears the
|
|
7
|
+
* client hang up.
|
|
8
|
+
*/
|
|
9
|
+
import { type HttpStreamResponse } from '@seanhogg/builderforce-webcontainers-core';
|
|
10
|
+
import type { ResponseMessage } from './protocol.js';
|
|
11
|
+
/**
|
|
12
|
+
* Send `response` as messages once it resolves (`undefined`: nothing listens).
|
|
13
|
+
* A rejected handler becomes a 500 carrying the error. Returns a cancel for
|
|
14
|
+
* when the requester hangs up; `onDone` runs once, when the exchange is over.
|
|
15
|
+
*/
|
|
16
|
+
export declare function sendResponse(post: (message: ResponseMessage) => void, reqId: number, response: Promise<HttpStreamResponse | undefined>, onDone?: () => void): () => void;
|
|
17
|
+
/** Rebuilds streamed responses from the messages answering requests this side sent. */
|
|
18
|
+
export declare class ResponseReceiver {
|
|
19
|
+
private readonly heads;
|
|
20
|
+
private readonly bodies;
|
|
21
|
+
/** The response to request `reqId`; `cancel` runs if its reader hangs up before the end. */
|
|
22
|
+
expect(reqId: number, cancel: () => void): Promise<HttpStreamResponse | undefined>;
|
|
23
|
+
receive(message: ResponseMessage): void;
|
|
24
|
+
/** The other side is gone: waiting requests get `fallback`, bodies in flight break off with `reason`. */
|
|
25
|
+
abort(fallback: () => HttpStreamResponse | undefined, reason: string): void;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=responseWire.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"responseWire.d.ts","sourceRoot":"","sources":["../../src/node/responseWire.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAiC,KAAK,kBAAkB,EAAmB,MAAM,2CAA2C,CAAC;AACpI,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAIrD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,CAAC,kBAAkB,GAAG,SAAS,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAoCxK;AAED,uFAAuF;AACvF,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAkG;IACxH,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiC;IAExD,4FAA4F;IAC5F,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,kBAAkB,GAAG,SAAS,CAAC;IAIlF,OAAO,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI;IAuBvC,yGAAyG;IACzG,KAAK,CAAC,QAAQ,EAAE,MAAM,kBAAkB,GAAG,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;CAM5E"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streamed HTTP responses over postMessage, used on both sides of the
|
|
3
|
+
* page ↔ process-worker boundary: the side that holds a response sends it as
|
|
4
|
+
* head / chunk / end messages as the server writes it, and the side that asked
|
|
5
|
+
* rebuilds an `HttpStreamResponse` whose body stream is fed by them. A reader
|
|
6
|
+
* that cancels the body sends `request-cancel` back, so the server hears the
|
|
7
|
+
* client hang up.
|
|
8
|
+
*/
|
|
9
|
+
import { drainBody, headOf, pushStream } from '@seanhogg/builderforce-webcontainers-core';
|
|
10
|
+
const encoder = new TextEncoder();
|
|
11
|
+
/**
|
|
12
|
+
* Send `response` as messages once it resolves (`undefined`: nothing listens).
|
|
13
|
+
* A rejected handler becomes a 500 carrying the error. Returns a cancel for
|
|
14
|
+
* when the requester hangs up; `onDone` runs once, when the exchange is over.
|
|
15
|
+
*/
|
|
16
|
+
export function sendResponse(post, reqId, response, onDone) {
|
|
17
|
+
let finished = false;
|
|
18
|
+
let stop;
|
|
19
|
+
const done = () => {
|
|
20
|
+
if (finished)
|
|
21
|
+
return;
|
|
22
|
+
finished = true;
|
|
23
|
+
onDone?.();
|
|
24
|
+
};
|
|
25
|
+
response.then((res) => {
|
|
26
|
+
if (finished)
|
|
27
|
+
return void res?.body.cancel().catch(() => undefined);
|
|
28
|
+
if (!res) {
|
|
29
|
+
post({ type: 'response-head', reqId, head: null });
|
|
30
|
+
return done();
|
|
31
|
+
}
|
|
32
|
+
post({ type: 'response-head', reqId, head: headOf(res) });
|
|
33
|
+
stop = drainBody(res.body, {
|
|
34
|
+
chunk: (data) => post({ type: 'response-chunk', reqId, data }),
|
|
35
|
+
end: (error) => {
|
|
36
|
+
post(error ? { type: 'response-end', reqId, error } : { type: 'response-end', reqId });
|
|
37
|
+
done();
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}, (error) => {
|
|
41
|
+
if (finished)
|
|
42
|
+
return;
|
|
43
|
+
post({ type: 'response-head', reqId, head: { status: 500, headers: { 'content-type': 'text/plain' } } });
|
|
44
|
+
post({ type: 'response-chunk', reqId, data: encoder.encode(String(error instanceof Error ? error.stack : error)) });
|
|
45
|
+
post({ type: 'response-end', reqId });
|
|
46
|
+
done();
|
|
47
|
+
});
|
|
48
|
+
return () => {
|
|
49
|
+
stop?.();
|
|
50
|
+
done();
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** Rebuilds streamed responses from the messages answering requests this side sent. */
|
|
54
|
+
export class ResponseReceiver {
|
|
55
|
+
heads = new Map();
|
|
56
|
+
bodies = new Map();
|
|
57
|
+
/** The response to request `reqId`; `cancel` runs if its reader hangs up before the end. */
|
|
58
|
+
expect(reqId, cancel) {
|
|
59
|
+
return new Promise((resolve) => this.heads.set(reqId, { resolve, cancel }));
|
|
60
|
+
}
|
|
61
|
+
receive(message) {
|
|
62
|
+
const { reqId } = message;
|
|
63
|
+
if (message.type === 'response-head') {
|
|
64
|
+
const waiting = this.heads.get(reqId);
|
|
65
|
+
this.heads.delete(reqId);
|
|
66
|
+
if (!waiting)
|
|
67
|
+
return;
|
|
68
|
+
if (!message.head)
|
|
69
|
+
return waiting.resolve(undefined);
|
|
70
|
+
const body = pushStream(() => {
|
|
71
|
+
this.bodies.delete(reqId);
|
|
72
|
+
waiting.cancel();
|
|
73
|
+
});
|
|
74
|
+
this.bodies.set(reqId, body);
|
|
75
|
+
waiting.resolve({ ...message.head, body: body.stream });
|
|
76
|
+
}
|
|
77
|
+
else if (message.type === 'response-chunk') {
|
|
78
|
+
this.bodies.get(reqId)?.push(message.data);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
const body = this.bodies.get(reqId);
|
|
82
|
+
this.bodies.delete(reqId);
|
|
83
|
+
if (message.error)
|
|
84
|
+
body?.fail(new Error(message.error));
|
|
85
|
+
else
|
|
86
|
+
body?.end();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** The other side is gone: waiting requests get `fallback`, bodies in flight break off with `reason`. */
|
|
90
|
+
abort(fallback, reason) {
|
|
91
|
+
for (const { resolve } of this.heads.values())
|
|
92
|
+
resolve(fallback());
|
|
93
|
+
this.heads.clear();
|
|
94
|
+
for (const body of this.bodies.values())
|
|
95
|
+
body.fail(new Error(reason));
|
|
96
|
+
this.bodies.clear();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=responseWire.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"responseWire.js","sourceRoot":"","sources":["../../src/node/responseWire.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAA4C,MAAM,2CAA2C,CAAC;AAGpI,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAElC;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,IAAwC,EAAE,KAAa,EAAE,QAAiD,EAAE,MAAmB;IAC1J,IAAI,QAAQ,GAA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