@procedures/client 0.6.0 → 0.8.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 +66 -0
- package/README.md +63 -7
- package/dist/.tsbuildinfo +1 -1
- package/dist/adapters/http/client/transport.d.ts +5 -0
- package/dist/adapters/http/client/transport.d.ts.map +1 -1
- package/dist/adapters/http/client/transport.js +10 -4
- package/dist/adapters/http/client/transport.js.map +1 -1
- package/dist/adapters/http/server/ndjson.d.ts +73 -0
- package/dist/adapters/http/server/ndjson.d.ts.map +1 -0
- package/dist/adapters/http/server/ndjson.js +228 -0
- package/dist/adapters/http/server/ndjson.js.map +1 -0
- package/dist/adapters/http/server/transport.d.ts +15 -4
- package/dist/adapters/http/server/transport.d.ts.map +1 -1
- package/dist/adapters/http/server/transport.js +29 -69
- package/dist/adapters/http/server/transport.js.map +1 -1
- package/dist/adapters/http/server/types.d.ts +15 -0
- package/dist/adapters/http/server/types.d.ts.map +1 -1
- package/dist/adapters/sequence.d.ts +14 -0
- package/dist/adapters/sequence.d.ts.map +1 -1
- package/dist/adapters/sequence.js +16 -0
- package/dist/adapters/sequence.js.map +1 -1
- package/dist/adapters/websocket/client/transport.d.ts +7 -3
- package/dist/adapters/websocket/client/transport.d.ts.map +1 -1
- package/dist/adapters/websocket/client/transport.js +9 -3
- package/dist/adapters/websocket/client/transport.js.map +1 -1
- package/dist/adapters/websocket/server/transport.d.ts +10 -0
- package/dist/adapters/websocket/server/transport.d.ts.map +1 -1
- package/dist/adapters/websocket/server/transport.js +22 -2
- package/dist/adapters/websocket/server/transport.js.map +1 -1
- package/dist/browser.d.ts +5 -5
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +4 -3
- package/dist/browser.js.map +1 -1
- package/dist/client/errors/registry.d.ts.map +1 -1
- package/dist/client/errors/registry.js +1 -0
- package/dist/client/errors/registry.js.map +1 -1
- package/dist/client/middleware/retry.d.ts +4 -0
- package/dist/client/middleware/retry.d.ts.map +1 -1
- package/dist/client/middleware/retry.js +86 -78
- package/dist/client/middleware/retry.js.map +1 -1
- package/dist/events/bus.d.ts +32 -14
- package/dist/events/bus.d.ts.map +1 -1
- package/dist/events/bus.js +119 -65
- package/dist/events/bus.js.map +1 -1
- package/dist/events/index.d.ts +3 -2
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js.map +1 -1
- package/dist/events/publisher.d.ts.map +1 -1
- package/dist/events/publisher.js +20 -6
- package/dist/events/publisher.js.map +1 -1
- package/dist/events/remote.d.ts +80 -18
- package/dist/events/remote.d.ts.map +1 -1
- package/dist/events/remote.js +448 -58
- package/dist/events/remote.js.map +1 -1
- package/dist/events/types.d.ts +13 -2
- package/dist/events/types.d.ts.map +1 -1
- package/dist/node.d.ts +6 -0
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +5 -0
- package/dist/node.js.map +1 -1
- package/dist/procedures/caller.d.ts +33 -0
- package/dist/procedures/caller.d.ts.map +1 -0
- package/dist/procedures/caller.js +27 -0
- package/dist/procedures/caller.js.map +1 -0
- package/dist/procedures/collection/index.d.ts +2 -2
- package/dist/procedures/collection/index.d.ts.map +1 -1
- package/dist/procedures/collection/index.js +1 -1
- package/dist/procedures/collection/index.js.map +1 -1
- package/dist/procedures/collection/procedures.d.ts.map +1 -1
- package/dist/procedures/collection/procedures.js +21 -1
- package/dist/procedures/collection/procedures.js.map +1 -1
- package/dist/procedures/collection/schemas.d.ts +9 -0
- package/dist/procedures/collection/schemas.d.ts.map +1 -1
- package/dist/procedures/collection/schemas.js +28 -0
- package/dist/procedures/collection/schemas.js.map +1 -1
- package/dist/procedures/collection/storage.d.ts +6 -0
- package/dist/procedures/collection/storage.d.ts.map +1 -1
- package/dist/procedures/index.d.ts +3 -1
- package/dist/procedures/index.d.ts.map +1 -1
- package/dist/procedures/index.js +2 -0
- package/dist/procedures/index.js.map +1 -1
- package/dist/procedures/invoke.d.ts.map +1 -1
- package/dist/procedures/invoke.js +3 -0
- package/dist/procedures/invoke.js.map +1 -1
- package/dist/procedures/types.d.ts +50 -0
- package/dist/procedures/types.d.ts.map +1 -1
- package/dist/procedures/types.js.map +1 -1
- package/dist/protocol.d.ts +1 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +1 -0
- package/dist/protocol.js.map +1 -1
- package/dist/server/procedure-server.d.ts +41 -5
- package/dist/server/procedure-server.d.ts.map +1 -1
- package/dist/server/procedure-server.js +94 -9
- package/dist/server/procedure-server.js.map +1 -1
- package/dist/timers.d.ts +9 -0
- package/dist/timers.d.ts.map +1 -1
- package/dist/timers.js +6 -1
- package/dist/timers.js.map +1 -1
- package/package.json +1 -1
- package/src/adapters/http/client/transport.ts +10 -4
- package/src/adapters/http/server/ndjson.ts +259 -0
- package/src/adapters/http/server/transport.ts +31 -69
- package/src/adapters/http/server/types.ts +17 -0
- package/src/adapters/sequence.ts +18 -0
- package/src/adapters/websocket/client/transport.ts +9 -3
- package/src/adapters/websocket/server/transport.ts +23 -2
- package/src/browser.ts +9 -2
- package/src/client/errors/registry.ts +1 -0
- package/src/client/middleware/retry.ts +89 -78
- package/src/events/bus.ts +141 -73
- package/src/events/index.ts +3 -0
- package/src/events/publisher.ts +23 -6
- package/src/events/remote.ts +575 -73
- package/src/events/types.ts +15 -2
- package/src/node.ts +6 -0
- package/src/procedures/caller.ts +45 -0
- package/src/procedures/collection/index.ts +3 -0
- package/src/procedures/collection/procedures.ts +22 -0
- package/src/procedures/collection/schemas.ts +37 -0
- package/src/procedures/collection/storage.ts +6 -0
- package/src/procedures/index.ts +4 -0
- package/src/procedures/invoke.ts +2 -0
- package/src/procedures/types.ts +53 -0
- package/src/protocol.ts +3 -1
- package/src/server/procedure-server.ts +111 -11
- package/src/timers.ts +15 -1
|
@@ -51,6 +51,11 @@ export declare class HttpTransport implements Transport {
|
|
|
51
51
|
* or an `{"type":"error"}` line. A stream that ends without one of the last two is an error
|
|
52
52
|
* that a retry can repair (the connection broke). An `item` or a `done` line whose `seq` is not
|
|
53
53
|
* above the last one gives nothing. The item after a gap has `stream-gap` in its metadata.
|
|
54
|
+
*
|
|
55
|
+
* A blank line is a keep-alive line of the server. The reader skips it: it has no `seq`, so the
|
|
56
|
+
* check sees no gap, and the `timeout` of the first item continues. Each item of the stream,
|
|
57
|
+
* also its error item, has the metadata key `stream` with the value `true`
|
|
58
|
+
* (`STREAM_ITEM_METADATA_KEY`).
|
|
54
59
|
*/
|
|
55
60
|
private readNdjson;
|
|
56
61
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../../../src/adapters/http/client/transport.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,SAAS,EACT,OAAO,EACP,YAAY,EACb,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAqBvD,OAAO,EACL,IAAI,EAcL,MAAM,oBAAoB,CAAC;AAI5B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,aAAc,YAAW,SAAS;IAC7C,QAAQ,CAAC,IAAI,EAAE,OAAO,IAAI,CAAQ;IAElC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAc;IAC1C,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAqB;IACxD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAkB;IAClD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAyB;IACxD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;IAE7C,YAAY,OAAO,EAAE,oBAAoB,EASxC;IAED;;;;;OAKG;IACI,IAAI,CAAC,IAAI,EAAE,IAAI,EACpB,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,GACrB,aAAa,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAqHnC;IAED
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../../../src/adapters/http/client/transport.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,SAAS,EACT,OAAO,EACP,YAAY,EACb,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAqBvD,OAAO,EACL,IAAI,EAcL,MAAM,oBAAoB,CAAC;AAI5B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,aAAc,YAAW,SAAS;IAC7C,QAAQ,CAAC,IAAI,EAAE,OAAO,IAAI,CAAQ;IAElC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAc;IAC1C,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAqB;IACxD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAkB;IAClD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAyB;IACxD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;IAE7C,YAAY,OAAO,EAAE,oBAAoB,EASxC;IAED;;;;;OAKG;IACI,IAAI,CAAC,IAAI,EAAE,IAAI,EACpB,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,GACrB,aAAa,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAqHnC;IAED;;;;;;;;;;OAUG;YACY,UAAU;IAkEzB;;OAEG;IACH,OAAO,CAAC,qBAAqB;IA0B7B;;OAEG;IACH,OAAO,CAAC,eAAe;IAUvB;;OAEG;YACW,iBAAiB;IA0B/B;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAE3B;CACF"}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* shared utilities and injectable strategies.
|
|
6
6
|
*/
|
|
7
7
|
import { DELIVERY_METADATA_KEY, RETRY_AFTER_METADATA_KEY, isNotSentError } from "../../../client/middleware/items.js";
|
|
8
|
-
import { StreamSequence,
|
|
8
|
+
import { StreamSequence, streamItemMetadata } from "../../sequence.js";
|
|
9
9
|
/**
|
|
10
10
|
* The wait that an error response asks for, in milliseconds: `retryAfterMs` of the body of the
|
|
11
11
|
* server transport, or else the Retry-After header (seconds, or an HTTP date).
|
|
@@ -194,6 +194,11 @@ export class HttpTransport {
|
|
|
194
194
|
* or an `{"type":"error"}` line. A stream that ends without one of the last two is an error
|
|
195
195
|
* that a retry can repair (the connection broke). An `item` or a `done` line whose `seq` is not
|
|
196
196
|
* above the last one gives nothing. The item after a gap has `stream-gap` in its metadata.
|
|
197
|
+
*
|
|
198
|
+
* A blank line is a keep-alive line of the server. The reader skips it: it has no `seq`, so the
|
|
199
|
+
* check sees no gap, and the `timeout` of the first item continues. Each item of the stream,
|
|
200
|
+
* also its error item, has the metadata key `stream` with the value `true`
|
|
201
|
+
* (`STREAM_ITEM_METADATA_KEY`).
|
|
197
202
|
*/
|
|
198
203
|
async *readNdjson(id, body, metadata, onEnd, onFirstItem) {
|
|
199
204
|
const reader = body.getReader();
|
|
@@ -210,6 +215,7 @@ export class HttpTransport {
|
|
|
210
215
|
while ((newline = buffer.indexOf("\n")) >= 0) {
|
|
211
216
|
const line = buffer.slice(0, newline).trim();
|
|
212
217
|
buffer = buffer.slice(newline + 1);
|
|
218
|
+
// A keep-alive line of the server (WIRE-PROTOCOL.md)
|
|
213
219
|
if (!line)
|
|
214
220
|
continue;
|
|
215
221
|
const frame = JSON.parse(line);
|
|
@@ -219,7 +225,7 @@ export class HttpTransport {
|
|
|
219
225
|
if (gap < 0)
|
|
220
226
|
continue;
|
|
221
227
|
if (frame.type === "item") {
|
|
222
|
-
yield { id, status: { type: "success", code: 200 }, payload: frame.payload, metadata:
|
|
228
|
+
yield { id, status: { type: "success", code: 200 }, payload: frame.payload, metadata: streamItemMetadata(metadata, gap) };
|
|
223
229
|
}
|
|
224
230
|
else if (frame.type === "done") {
|
|
225
231
|
onEnd();
|
|
@@ -236,7 +242,7 @@ export class HttpTransport {
|
|
|
236
242
|
retryable: frame.error?.retryable ?? false,
|
|
237
243
|
},
|
|
238
244
|
payload: null,
|
|
239
|
-
metadata,
|
|
245
|
+
metadata: streamItemMetadata(metadata, 0),
|
|
240
246
|
};
|
|
241
247
|
return;
|
|
242
248
|
}
|
|
@@ -247,7 +253,7 @@ export class HttpTransport {
|
|
|
247
253
|
id,
|
|
248
254
|
status: { type: "error", code: "STREAM_ENDED", message: "The stream ended without its end line", retryable: true },
|
|
249
255
|
payload: null,
|
|
250
|
-
metadata: { ...metadata, [DELIVERY_METADATA_KEY]: "indeterminate" },
|
|
256
|
+
metadata: { ...streamItemMetadata(metadata, 0), [DELIVERY_METADATA_KEY]: "indeterminate" },
|
|
251
257
|
};
|
|
252
258
|
}
|
|
253
259
|
finally {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../../../../src/adapters/http/client/transport.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AASH,OAAO,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,cAAc,EAAE,MAAM,qCAAqC,CAAC;AACtH,OAAO,EAAE,cAAc,EAAE,
|
|
1
|
+
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../../../../src/adapters/http/client/transport.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AASH,OAAO,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,cAAc,EAAE,MAAM,qCAAqC,CAAC;AACtH,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvE;;;GAGG;AACH,SAAS,cAAc,CAAC,MAAqB,EAAE,IAAa;IAC1D,MAAM,QAAQ,GAAI,IAAsD,EAAE,YAAY,CAAC;IACvF,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,QAAQ,IAAI,CAAC;QAAE,OAAO,QAAQ,CAAC;IAChG,IAAI,MAAM,KAAK,IAAI,IAAI,MAA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EAAE,EAAE,CAAC;YAChB,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,IAAI,WAAW,EAAE,QAAQ,CAAC,kBAAkB,CAAC,EAAE,CAAC;YAC9C,IAAI,CAAC;gBACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YAC1B,CAAC;YAAC,MAAM,CAAC;gBACP,uEAAuE;gBACvE,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAK;QACT,0DAA0D;IAC5D,CAAC;CACF"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The writer of an NDJSON stream response (wire protocol v1, `documentation/WIRE-PROTOCOL.md`).
|
|
3
|
+
*
|
|
4
|
+
* `HttpServerTransport` writes each NDJSON stream with `writeNdjsonStream`. A server without
|
|
5
|
+
* Express can use the function too: it takes a `ServerResponse` of `node:http`. This module
|
|
6
|
+
* imports only the types of `node:http`, so it loads no Node code at run time.
|
|
7
|
+
*/
|
|
8
|
+
import type { ServerResponse } from "node:http";
|
|
9
|
+
import { type Clock } from "../../../clock.js";
|
|
10
|
+
import { type Duration } from "../../../duration.js";
|
|
11
|
+
/** The media type of a stream response: one JSON value on each line. */
|
|
12
|
+
export declare const NDJSON_MEDIA_TYPE = "application/x-ndjson";
|
|
13
|
+
/** The keep-alive line of an NDJSON stream: a blank line. A reader skips it. It has no `seq`. */
|
|
14
|
+
export declare const KEEP_ALIVE_LINE = "\n";
|
|
15
|
+
/** The error of an `error` line: its code, its message and its retryable flag. */
|
|
16
|
+
export interface NdjsonError {
|
|
17
|
+
code: string;
|
|
18
|
+
message: string;
|
|
19
|
+
retryable: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** The options of `writeNdjsonStream`. */
|
|
22
|
+
export interface WriteNdjsonStreamOptions {
|
|
23
|
+
/**
|
|
24
|
+
* The signal of the request. Its abort stops the stream. The iterator of the items gets
|
|
25
|
+
* `return()`, and a response that is still open gets the error line `ABORTED`.
|
|
26
|
+
*/
|
|
27
|
+
readonly signal?: AbortSignal | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* The quiet time after which the writer sends a keep-alive line. Each line starts the time again.
|
|
30
|
+
* `false`, or a duration of zero or less, sends no keep-alive line.
|
|
31
|
+
* @default seconds(15) (`TIMER_DEFAULTS.httpServer.keepAliveMs`)
|
|
32
|
+
*/
|
|
33
|
+
readonly keepAlive?: Duration | false | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* The function that gives the `error` line of an error of the stream. The default gives the code
|
|
36
|
+
* of the error (`errorCodeOf`), its message and its own retryable flag, or `false`.
|
|
37
|
+
*/
|
|
38
|
+
readonly errorOf?: ((error: unknown) => NdjsonError) | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* The clock of the keep-alive timer (roadmap R35). The default is the clock of the platform. A
|
|
41
|
+
* test gives a fake clock.
|
|
42
|
+
*/
|
|
43
|
+
readonly clock?: Clock | undefined;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* This function gives the `error` line of a thrown error: its code, its message and its retryable
|
|
47
|
+
* flag (deep dive TRN-7). A code that is not a string gives `HANDLER_ERROR`.
|
|
48
|
+
*/
|
|
49
|
+
export declare function defaultNdjsonError(error: unknown): NdjsonError;
|
|
50
|
+
/**
|
|
51
|
+
* This function gives the keep-alive time in milliseconds, or 0 for no keep-alive. A value that is
|
|
52
|
+
* not a duration throws a `TypeError`.
|
|
53
|
+
*/
|
|
54
|
+
export declare function keepAliveMsOf(keepAlive: Duration | false | undefined): number;
|
|
55
|
+
/**
|
|
56
|
+
* This function writes a stream of items as an NDJSON response of wire protocol v1.
|
|
57
|
+
*
|
|
58
|
+
* - It writes the status 200, the headers `Content-Type: application/x-ndjson`,
|
|
59
|
+
* `Cache-Control: no-cache` and `Procedures-Protocol: 1`, and it sends them at once.
|
|
60
|
+
* - Each item gives the line `{"type":"item","seq":n,"payload":...}`. The `seq` counts from 1.
|
|
61
|
+
* - The end of the items gives `{"type":"done","seq":n}`. An error gives
|
|
62
|
+
* `{"type":"error","error":{"code":...,"message":...,"retryable":...}}`, without `seq`.
|
|
63
|
+
* - After each quiet time of `keepAlive`, it writes a keep-alive line: a blank line. Each line
|
|
64
|
+
* starts the time again. No keep-alive line comes after the last line.
|
|
65
|
+
* - When `write()` gives false, it waits for `drain` before it reads the next item.
|
|
66
|
+
* - A closed connection or an abort of `signal` stops it. The iterator gets `return()`. After an
|
|
67
|
+
* abort, a response that is still open gets the error line `ABORTED`.
|
|
68
|
+
*
|
|
69
|
+
* The promise resolves when the response ends. It does not reject: an error of the items becomes
|
|
70
|
+
* the error line.
|
|
71
|
+
*/
|
|
72
|
+
export declare function writeNdjsonStream(res: ServerResponse, items: AsyncIterable<unknown>, options?: WriteNdjsonStreamOptions): Promise<void>;
|
|
73
|
+
//# sourceMappingURL=ndjson.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ndjson.d.ts","sourceRoot":"","sources":["../../../../src/adapters/http/server/ndjson.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAA0B,KAAK,KAAK,EAAE,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAQ,KAAK,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAI3D,wEAAwE;AACxE,eAAO,MAAM,iBAAiB,yBAAyB,CAAC;AAExD,iGAAiG;AACjG,eAAO,MAAM,eAAe,OAAO,CAAC;AAKpC,kFAAkF;AAClF,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;CACpB;AAED,0CAA0C;AAC1C,MAAM,WAAW,wBAAwB;IACvC;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC;IAC1C;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,QAAQ,GAAG,KAAK,GAAG,SAAS,CAAC;IAClD;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,OAAO,KAAK,WAAW,CAAC,GAAG,SAAS,CAAC;IACjE;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC;CACpC;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,WAAW,CAO9D;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,QAAQ,GAAG,KAAK,GAAG,SAAS,GAAG,MAAM,CAK7E;AAiDD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,iBAAiB,CAAC,GAAG,EAAE,cAAc,EAAE,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,OAAO,GAAE,wBAA6B,GAAG,OAAO,CAAC,IAAI,CAAC,CAmHjJ"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The writer of an NDJSON stream response (wire protocol v1, `documentation/WIRE-PROTOCOL.md`).
|
|
3
|
+
*
|
|
4
|
+
* `HttpServerTransport` writes each NDJSON stream with `writeNdjsonStream`. A server without
|
|
5
|
+
* Express can use the function too: it takes a `ServerResponse` of `node:http`. This module
|
|
6
|
+
* imports only the types of `node:http`, so it loads no Node code at run time.
|
|
7
|
+
*/
|
|
8
|
+
import { realClock } from "../../../clock.js";
|
|
9
|
+
import { toMs } from "../../../duration.js";
|
|
10
|
+
import { PROTOCOL_HEADER, PROTOCOL_VERSION, errorCodeOf } from "../../../protocol.js";
|
|
11
|
+
import { TIMER_DEFAULTS } from "../../../timers.js";
|
|
12
|
+
/** The media type of a stream response: one JSON value on each line. */
|
|
13
|
+
export const NDJSON_MEDIA_TYPE = "application/x-ndjson";
|
|
14
|
+
/** The keep-alive line of an NDJSON stream: a blank line. A reader skips it. It has no `seq`. */
|
|
15
|
+
export const KEEP_ALIVE_LINE = "\n";
|
|
16
|
+
/** The longest delay of a Node timer. A longer delay starts the timer at once. */
|
|
17
|
+
const MAX_TIMER_DELAY_MS = 2_147_483_647;
|
|
18
|
+
/**
|
|
19
|
+
* This function gives the `error` line of a thrown error: its code, its message and its retryable
|
|
20
|
+
* flag (deep dive TRN-7). A code that is not a string gives `HANDLER_ERROR`.
|
|
21
|
+
*/
|
|
22
|
+
export function defaultNdjsonError(error) {
|
|
23
|
+
const retryable = error?.retryable;
|
|
24
|
+
return {
|
|
25
|
+
code: errorCodeOf(error),
|
|
26
|
+
message: error instanceof Error ? error.message : String(error),
|
|
27
|
+
retryable: typeof retryable === "boolean" ? retryable : false,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* This function gives the keep-alive time in milliseconds, or 0 for no keep-alive. A value that is
|
|
32
|
+
* not a duration throws a `TypeError`.
|
|
33
|
+
*/
|
|
34
|
+
export function keepAliveMsOf(keepAlive) {
|
|
35
|
+
if (keepAlive === false)
|
|
36
|
+
return 0;
|
|
37
|
+
if (keepAlive === undefined)
|
|
38
|
+
return TIMER_DEFAULTS.httpServer.keepAliveMs;
|
|
39
|
+
const value = toMs(keepAlive, "keepAlive");
|
|
40
|
+
return value > 0 ? Math.min(value, MAX_TIMER_DELAY_MS) : 0;
|
|
41
|
+
}
|
|
42
|
+
/** One JSON line of the stream. */
|
|
43
|
+
function lineOf(frame) {
|
|
44
|
+
return `${JSON.stringify(frame)}\n`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* This function gives the next result of the iterator, or `undefined` when the stop signal
|
|
48
|
+
* aborts first. Thus a quiet iterator does not keep a stopped stream open. The listener goes
|
|
49
|
+
* away after each step, so a long stream adds no listener.
|
|
50
|
+
*/
|
|
51
|
+
function nextOrStop(iterator, stop) {
|
|
52
|
+
if (stop.aborted)
|
|
53
|
+
return Promise.resolve(undefined);
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
const onStop = () => resolve(undefined);
|
|
56
|
+
stop.addEventListener("abort", onStop, { once: true });
|
|
57
|
+
Promise.resolve(iterator.next()).then((result) => {
|
|
58
|
+
stop.removeEventListener("abort", onStop);
|
|
59
|
+
resolve(result);
|
|
60
|
+
}, (error) => {
|
|
61
|
+
stop.removeEventListener("abort", onStop);
|
|
62
|
+
reject(error);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* This function waits until the response can take more data: the `drain` event. A closed
|
|
68
|
+
* connection or the stop signal also ends the wait (deep dive TRN-3).
|
|
69
|
+
*/
|
|
70
|
+
function drained(res, stop) {
|
|
71
|
+
if (stop.aborted)
|
|
72
|
+
return Promise.resolve();
|
|
73
|
+
return new Promise((resolve) => {
|
|
74
|
+
const done = () => {
|
|
75
|
+
res.off("drain", done);
|
|
76
|
+
res.off("close", done);
|
|
77
|
+
stop.removeEventListener("abort", done);
|
|
78
|
+
resolve();
|
|
79
|
+
};
|
|
80
|
+
res.on("drain", done);
|
|
81
|
+
res.on("close", done);
|
|
82
|
+
stop.addEventListener("abort", done, { once: true });
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* This function writes a stream of items as an NDJSON response of wire protocol v1.
|
|
87
|
+
*
|
|
88
|
+
* - It writes the status 200, the headers `Content-Type: application/x-ndjson`,
|
|
89
|
+
* `Cache-Control: no-cache` and `Procedures-Protocol: 1`, and it sends them at once.
|
|
90
|
+
* - Each item gives the line `{"type":"item","seq":n,"payload":...}`. The `seq` counts from 1.
|
|
91
|
+
* - The end of the items gives `{"type":"done","seq":n}`. An error gives
|
|
92
|
+
* `{"type":"error","error":{"code":...,"message":...,"retryable":...}}`, without `seq`.
|
|
93
|
+
* - After each quiet time of `keepAlive`, it writes a keep-alive line: a blank line. Each line
|
|
94
|
+
* starts the time again. No keep-alive line comes after the last line.
|
|
95
|
+
* - When `write()` gives false, it waits for `drain` before it reads the next item.
|
|
96
|
+
* - A closed connection or an abort of `signal` stops it. The iterator gets `return()`. After an
|
|
97
|
+
* abort, a response that is still open gets the error line `ABORTED`.
|
|
98
|
+
*
|
|
99
|
+
* The promise resolves when the response ends. It does not reject: an error of the items becomes
|
|
100
|
+
* the error line.
|
|
101
|
+
*/
|
|
102
|
+
export async function writeNdjsonStream(res, items, options = {}) {
|
|
103
|
+
const interval = keepAliveMsOf(options.keepAlive);
|
|
104
|
+
const clock = options.clock ?? realClock;
|
|
105
|
+
const errorOf = options.errorOf ?? defaultNdjsonError;
|
|
106
|
+
const { signal } = options;
|
|
107
|
+
// One stop for the abort of the caller and for a closed connection
|
|
108
|
+
const stop = new AbortController();
|
|
109
|
+
const onAbort = () => stop.abort();
|
|
110
|
+
const onClose = () => {
|
|
111
|
+
if (!res.writableFinished)
|
|
112
|
+
stop.abort();
|
|
113
|
+
};
|
|
114
|
+
if (signal?.aborted || res.destroyed)
|
|
115
|
+
stop.abort();
|
|
116
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
117
|
+
res.on("close", onClose);
|
|
118
|
+
// The keep-alive timer. Each line starts the quiet time again, but a line does not start a new
|
|
119
|
+
// timer: when the timer comes due, it examines the time of the last line. Thus a fast stream
|
|
120
|
+
// has one timer for each quiet time, not one for each item.
|
|
121
|
+
let timer;
|
|
122
|
+
let lastLine = 0;
|
|
123
|
+
const stopTimer = () => {
|
|
124
|
+
timer?.();
|
|
125
|
+
timer = undefined;
|
|
126
|
+
};
|
|
127
|
+
const arm = (delay) => {
|
|
128
|
+
timer = clock.after(delay, keepAlive);
|
|
129
|
+
timer.unref?.();
|
|
130
|
+
};
|
|
131
|
+
/** This function records a line: the quiet time starts again. */
|
|
132
|
+
const startTimer = () => {
|
|
133
|
+
lastLine = clock.now();
|
|
134
|
+
if (timer === undefined && interval > 0 && !stop.signal.aborted && !res.writableEnded)
|
|
135
|
+
arm(interval);
|
|
136
|
+
};
|
|
137
|
+
function keepAlive() {
|
|
138
|
+
timer = undefined;
|
|
139
|
+
if (stop.signal.aborted || res.writableEnded || res.destroyed)
|
|
140
|
+
return;
|
|
141
|
+
const quiet = clock.now() - lastLine;
|
|
142
|
+
if (quiet < interval) {
|
|
143
|
+
// A line went out after the start of this timer: wait for the rest of the quiet time
|
|
144
|
+
arm(interval - quiet);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
// A full buffer waits for the reader. One more byte there does not reach the reader sooner.
|
|
148
|
+
if (!res.writableNeedDrain)
|
|
149
|
+
res.write(KEEP_ALIVE_LINE);
|
|
150
|
+
startTimer();
|
|
151
|
+
}
|
|
152
|
+
/** This function ends an open response after an abort: the server stopped the request. */
|
|
153
|
+
const endAborted = () => {
|
|
154
|
+
if (!res.writableEnded && !res.destroyed) {
|
|
155
|
+
res.end(lineOf({ type: "error", error: { code: "ABORTED", message: "The server stopped the request", retryable: true } }));
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const iterator = items[Symbol.asyncIterator]();
|
|
159
|
+
/** True when the iterator ended by itself: at its end, or with an error */
|
|
160
|
+
let ended = false;
|
|
161
|
+
let seq = 0;
|
|
162
|
+
try {
|
|
163
|
+
if (!res.headersSent && !res.destroyed) {
|
|
164
|
+
res.statusCode = 200;
|
|
165
|
+
res.setHeader(PROTOCOL_HEADER, String(PROTOCOL_VERSION));
|
|
166
|
+
res.setHeader("Content-Type", NDJSON_MEDIA_TYPE);
|
|
167
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
168
|
+
res.flushHeaders();
|
|
169
|
+
}
|
|
170
|
+
startTimer();
|
|
171
|
+
for (;;) {
|
|
172
|
+
let next;
|
|
173
|
+
try {
|
|
174
|
+
next = await nextOrStop(iterator, stop.signal);
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
ended = true;
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
if (next === undefined)
|
|
181
|
+
break;
|
|
182
|
+
if (next.done) {
|
|
183
|
+
ended = true;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
if (stop.signal.aborted)
|
|
187
|
+
break;
|
|
188
|
+
const room = res.write(lineOf({ type: "item", seq: ++seq, payload: next.value }));
|
|
189
|
+
startTimer();
|
|
190
|
+
if (!room)
|
|
191
|
+
await drained(res, stop.signal);
|
|
192
|
+
if (stop.signal.aborted)
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
stopTimer();
|
|
196
|
+
if (stop.signal.aborted) {
|
|
197
|
+
endAborted();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
res.end(lineOf({ type: "done", seq: ++seq }));
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
stopTimer();
|
|
204
|
+
if (stop.signal.aborted) {
|
|
205
|
+
endAborted();
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
let info;
|
|
209
|
+
try {
|
|
210
|
+
info = errorOf(error);
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
// A failed errorOf does not leave the response open
|
|
214
|
+
info = defaultNdjsonError(error);
|
|
215
|
+
}
|
|
216
|
+
res.end(lineOf({ type: "error", error: info }));
|
|
217
|
+
}
|
|
218
|
+
finally {
|
|
219
|
+
stopTimer();
|
|
220
|
+
signal?.removeEventListener("abort", onAbort);
|
|
221
|
+
res.off("close", onClose);
|
|
222
|
+
// A stream that stopped early: its producer stops too. A pending next() keeps the return()
|
|
223
|
+
// waiting, so the writer does not wait for it.
|
|
224
|
+
if (!ended)
|
|
225
|
+
void Promise.resolve().then(() => iterator.return?.()).catch(() => undefined);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=ndjson.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ndjson.js","sourceRoot":"","sources":["../../../../src/adapters/http/server/ndjson.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG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QAC9C,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAC1B,2FAA2F;QAC3F,+CAA+C;QAC/C,IAAI,CAAC,KAAK;YAAE,KAAK,OAAO,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IAC5F,CAAC;AACH,CAAC"}
|
|
@@ -19,6 +19,10 @@ export declare class HttpServerTransport implements ServerTransport {
|
|
|
19
19
|
private readonly bodyLimit;
|
|
20
20
|
/** The browser checks of each procedure call (see `server/browser-guard.ts`) */
|
|
21
21
|
private readonly guard;
|
|
22
|
+
/** The quiet time of an NDJSON stream before a keep-alive line, or false for none */
|
|
23
|
+
private readonly keepAlive;
|
|
24
|
+
/** The clock of the keep-alive timers (roadmap R35) */
|
|
25
|
+
private readonly clock;
|
|
22
26
|
constructor(server: RequestHandler, options?: HttpServerTransportOptions);
|
|
23
27
|
/**
|
|
24
28
|
* Setup Express routes.
|
|
@@ -36,20 +40,21 @@ export declare class HttpServerTransport implements ServerTransport {
|
|
|
36
40
|
* a string (deep dive TRN-10).
|
|
37
41
|
*/
|
|
38
42
|
private readBody;
|
|
39
|
-
/**
|
|
40
|
-
* Convert HTTP request to ServerRequest.
|
|
41
|
-
*/
|
|
42
43
|
/**
|
|
43
44
|
* Send a stream response.
|
|
44
45
|
*
|
|
45
46
|
* A client that accepts `application/x-ndjson` gets one JSON line for each item
|
|
46
47
|
* (`{"type":"item","seq":1,"payload":...}`), then `{"type":"done","seq":...}`, or
|
|
47
48
|
* `{"type":"error","error":...}`. The `seq` of the lines counts from 1, as in the `stream`
|
|
48
|
-
* frames of WebSocket.
|
|
49
|
+
* frames of WebSocket. A quiet stream gets a keep-alive line, a blank line, after each quiet
|
|
50
|
+
* time of `keepAlive`. `writeNdjsonStream` writes these lines.
|
|
49
51
|
* Another client (curl, a browser fetch without the header) gets the last item as plain JSON,
|
|
50
52
|
* the "sponge" mode, so a streaming procedure does not change its response format.
|
|
51
53
|
*/
|
|
52
54
|
private streamToHttp;
|
|
55
|
+
/**
|
|
56
|
+
* This method converts an HTTP request to a `ServerRequest`.
|
|
57
|
+
*/
|
|
53
58
|
private httpToServerRequest;
|
|
54
59
|
/**
|
|
55
60
|
* Convert ServerResponse to HTTP response.
|
|
@@ -64,5 +69,11 @@ export declare class HttpServerTransport implements ServerTransport {
|
|
|
64
69
|
*/
|
|
65
70
|
stop(): Promise<void>;
|
|
66
71
|
isRunning(): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* The port that the server of the transport listens on. With the option `port` 0, the system
|
|
74
|
+
* gives a free port, and this function returns that port. Before `start()` and after `stop()`,
|
|
75
|
+
* the function returns undefined.
|
|
76
|
+
*/
|
|
77
|
+
port(): number | undefined;
|
|
67
78
|
}
|
|
68
79
|
//# sourceMappingURL=transport.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../../../src/adapters/http/server/transport.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAE,eAAe,EAAiC,MAAM,0BAA0B,CAAC;AAE/F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAG/D,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../../../src/adapters/http/server/transport.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAE,eAAe,EAAiC,MAAM,0BAA0B,CAAC;AAE/F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAG/D,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAyD7D,qBAAa,mBAAoB,YAAW,eAAe;IACzD,QAAQ,CAAC,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