@ts-pf/message-client 0.1.4 → 0.2.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/LICENSE +21 -0
- package/README.md +48 -2
- package/dist/call.d.ts +59 -0
- package/dist/call.d.ts.map +1 -0
- package/dist/call.js +269 -0
- package/dist/call.js.map +1 -0
- package/dist/duplex.d.ts +30 -0
- package/dist/duplex.d.ts.map +1 -0
- package/dist/duplex.js +108 -0
- package/dist/duplex.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/port.d.ts +3 -2
- package/dist/port.d.ts.map +1 -1
- package/dist/port.js +9 -18
- package/dist/port.js.map +1 -1
- package/dist/shared.d.ts +19 -9
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +33 -323
- package/dist/shared.js.map +1 -1
- package/dist/stdio.d.ts +3 -2
- package/dist/stdio.d.ts.map +1 -1
- package/dist/stdio.js +10 -18
- package/dist/stdio.js.map +1 -1
- package/dist/ws.d.ts +8 -2
- package/dist/ws.d.ts.map +1 -1
- package/dist/ws.js +33 -60
- package/dist/ws.js.map +1 -1
- package/package.json +12 -7
- package/skills/ts-pf-message-client/SKILL.md +32 -3
- package/dist/is-async-iterable.d.ts +0 -2
- package/dist/is-async-iterable.d.ts.map +0 -1
- package/dist/is-async-iterable.js +0 -12
- package/dist/is-async-iterable.js.map +0 -1
- package/dist/push-queue.d.ts +0 -7
- package/dist/push-queue.d.ts.map +0 -1
- package/dist/push-queue.js +0 -74
- package/dist/push-queue.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ts-pf contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @ts-pf/message-client
|
|
2
2
|
|
|
3
|
-
Opt-in `PortLink` / `WsLink` / `StdioLink` for the same `createClient(link)` as `@ts-pf/client-http` `FetchLink`.
|
|
3
|
+
Opt-in `PortLink` / `WsLink` / `StdioLink` / `DuplexLink` for the same `createClient(link)` as `@ts-pf/client-http` `FetchLink`.
|
|
4
4
|
|
|
5
5
|
Agent skill: [`skills/ts-pf-message-client/`](skills/ts-pf-message-client/). Sync with `npx skills experimental_sync -y`.
|
|
6
6
|
|
|
@@ -16,12 +16,58 @@ const client = createClient<typeof contract>(new PortLink({ port }), {
|
|
|
16
16
|
Stdio is a subpath, not on the main index:
|
|
17
17
|
|
|
18
18
|
```ts
|
|
19
|
+
import { spawn } from 'node:child_process'
|
|
19
20
|
import { StdioLink } from '@ts-pf/message-client/stdio'
|
|
20
21
|
|
|
21
22
|
const child = spawn(cmd, { stdio: ['pipe', 'pipe', 'inherit'] })
|
|
22
23
|
new StdioLink({ input: child.stdout, output: child.stdin })
|
|
23
24
|
```
|
|
24
25
|
|
|
26
|
+
Bring your own duplex. Any pipe that moves JSON text (Electron IPC, a Chrome runtime port, WebTransport, a Unix socket) is a `Duplex` from `@ts-pf/message` plus `DuplexLink`; `PortLink` / `WsLink` / `StdioLink` are exactly that over their built-in adapters. The link owns the handshake, ids, multiplexing, and cancel. Return a promise from `send` while your pipe is above its write mark (it must settle on close) and implement `pause` / `resume` if it can stop receiving; a duplex that does neither gets no slow-peer protection on that pipe:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { createClient } from '@ts-pf/client'
|
|
30
|
+
import type { Duplex } from '@ts-pf/message'
|
|
31
|
+
import { DuplexLink } from '@ts-pf/message-client'
|
|
32
|
+
|
|
33
|
+
declare const ipc: {
|
|
34
|
+
send(text: string): void
|
|
35
|
+
on(event: 'message' | 'close', handler: (text: string) => void): () => void
|
|
36
|
+
close(): void
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const duplex: Duplex = {
|
|
40
|
+
send: (text) => ipc.send(text),
|
|
41
|
+
onMessage: (handler) => ipc.on('message', handler),
|
|
42
|
+
onClose: (handler) => ipc.on('close', handler),
|
|
43
|
+
close: () => ipc.close(),
|
|
44
|
+
}
|
|
45
|
+
const client = createClient<typeof contract>(new DuplexLink({ duplex }))
|
|
46
|
+
```
|
|
47
|
+
|
|
25
48
|
User owns listen / upgrade / spawn / Worker bootstrap. Prod never depends on `@ts-pf/server`.
|
|
26
49
|
|
|
27
|
-
|
|
50
|
+
## Backpressure
|
|
51
|
+
|
|
52
|
+
On by default, mirroring the server. An `AsyncIterable` input is pumped one item at a time and parked while the duplex is above its write high-water mark (WebSocket `bufferedAmount` over 1 MiB, stdio `write()` refused). If the server still has not drained after `drainTimeout` (30 s) the call fails locally (`isLocalFailure` from `@ts-pf/client` is true; message `Peer did not drain within 30000 ms`), a `cancel` is sent if the socket accepts it, and the input iterator's `return()` runs once. An output stream you read is bounded by the same mark (measured as frame text length): while its queue is over it the link pauses reading the duplex (connection-wide) until you catch up, where the pipe can pause (stdio, npm `ws`); a server that times out on you ends the stream with `INTERNAL` / `Peer did not drain within …` after the items already buffered — that one came from the server, so `isLocalFailure` is false.
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { createClient } from '@ts-pf/client'
|
|
56
|
+
import { WsLink } from '@ts-pf/message-client'
|
|
57
|
+
|
|
58
|
+
const link = new WsLink({
|
|
59
|
+
url: 'wss://example.com/rpc',
|
|
60
|
+
WebSocket,
|
|
61
|
+
backpressure: { highWaterMark: 1024 * 1024, drainTimeout: 30_000, onStall: false }, // defaults shown; onStall default is one console.warn on 'aborted'
|
|
62
|
+
})
|
|
63
|
+
const client = createClient<typeof contract>(link)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
A bad value is a `TypeError` naming the option, thrown by the constructor. `BackpressureOptions` / `StallInfo` are re-exported from `@ts-pf/message`.
|
|
67
|
+
|
|
68
|
+
Wire spec: [`packages/protocol/PROTOCOL.md`](../protocol/PROTOCOL.md) (Message transports, "Backpressure").
|
|
69
|
+
|
|
70
|
+
## API
|
|
71
|
+
|
|
72
|
+
- `DuplexLink`, `PortLink`, `WsLink`, `LinkOptions`, `BackpressureOptions`, `StallInfo`, `WebSocketLike`
|
|
73
|
+
- `StdioLink` from `./stdio` only
|
package/dist/call.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type Backpressure, type MessageFrame, type SendResult } from '@ts-pf/message';
|
|
2
|
+
import { type PFError } from '@ts-pf/protocol';
|
|
3
|
+
/** What one call needs from its connection; `DuplexLink` provides it. */
|
|
4
|
+
export type ClientCallHost = {
|
|
5
|
+
send(frame: MessageFrame): SendResult;
|
|
6
|
+
/** The id reached a terminal state: forget it. */
|
|
7
|
+
settle(id: string): void;
|
|
8
|
+
backpressure: Backpressure;
|
|
9
|
+
/** A call's output queue crossed / drained below its mark: pause / resume reading the duplex (connection-wide). */
|
|
10
|
+
pauseReads(): void;
|
|
11
|
+
resumeReads(): void;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* One in-flight id on the client side: the call promise until the first
|
|
15
|
+
* inbound frame, then (for an output stream) the iterator the promise
|
|
16
|
+
* resolved to, plus the `in-item` pump of a streamed input and the abort
|
|
17
|
+
* listener. Settles exactly once. Frame routing stays in `DuplexLink`.
|
|
18
|
+
*
|
|
19
|
+
* Backpressure: the output queue is bounded by `highWaterMark` and pauses the
|
|
20
|
+
* duplex through the host; the input pump awaits the duplex's drain between
|
|
21
|
+
* items and fails the call locally when the server does not drain in
|
|
22
|
+
* `drainTimeout`.
|
|
23
|
+
*/
|
|
24
|
+
export declare class ClientCall {
|
|
25
|
+
readonly id: string;
|
|
26
|
+
private readonly host;
|
|
27
|
+
private readonly resolve;
|
|
28
|
+
private readonly reject;
|
|
29
|
+
private mode;
|
|
30
|
+
private sendingInput;
|
|
31
|
+
private output;
|
|
32
|
+
private inputIterator;
|
|
33
|
+
private unsubAbort;
|
|
34
|
+
private drain;
|
|
35
|
+
constructor(id: string, host: ClientCallHost, resolve: (value: unknown) => void, reject: (error: PFError) => void);
|
|
36
|
+
/** Send `call`, arm `signal`, and start pumping `in-item` / `in-done` for an input stream. */
|
|
37
|
+
open(path: string[], input: unknown, signal?: AbortSignal): void;
|
|
38
|
+
/** An inbound frame carrying this id; `size` is its text length. */
|
|
39
|
+
onFrame(frame: MessageFrame, size: number): void;
|
|
40
|
+
/**
|
|
41
|
+
* Settle with `error`: reject the call promise before the first frame,
|
|
42
|
+
* fail the output iterator after it. `sendCancel` tells the server to stop
|
|
43
|
+
* (abort, input pump failure); a peer close or server `result` does not.
|
|
44
|
+
*/
|
|
45
|
+
fail(error: PFError, sendCancel: boolean): void;
|
|
46
|
+
private createOutput;
|
|
47
|
+
private onStreamFrame;
|
|
48
|
+
/** One-shot terminal transition; `action` runs after the id is forgotten. */
|
|
49
|
+
private finish;
|
|
50
|
+
/** The iterable the call resolves to; `break` / `return()` / `throw()` send `cancel`. */
|
|
51
|
+
private wrapOutput;
|
|
52
|
+
/**
|
|
53
|
+
* Pull the input iterator one item at a time and send `in-item`, pausing at
|
|
54
|
+
* the duplex's drain promise. A server that does not drain within
|
|
55
|
+
* `drainTimeout` fails the call locally and gets a `cancel`.
|
|
56
|
+
*/
|
|
57
|
+
private pumpInput;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=call.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"call.d.ts","sourceRoot":"","sources":["../src/call.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EAMjB,KAAK,YAAY,EAEjB,KAAK,UAAU,EAChB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAA2B,KAAK,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAGvE,yEAAyE;AACzE,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,CAAC,KAAK,EAAE,YAAY,GAAG,UAAU,CAAA;IACrC,kDAAkD;IAClD,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,YAAY,EAAE,YAAY,CAAA;IAC1B,mHAAmH;IACnH,UAAU,IAAI,IAAI,CAAA;IAClB,WAAW,IAAI,IAAI,CAAA;CACpB,CAAA;AA+BD;;;;;;;;;;GAUG;AACH,qBAAa,UAAU;IASnB,QAAQ,CAAC,EAAE,EAAE,MAAM;IACnB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM;IAXzB,OAAO,CAAC,IAAI,CAAwB;IACpC,OAAO,CAAC,YAAY,CAAQ;IAC5B,OAAO,CAAC,MAAM,CAAgC;IAC9C,OAAO,CAAC,aAAa,CAAoC;IACzD,OAAO,CAAC,UAAU,CAA0B;IAC5C,OAAO,CAAC,KAAK,CAAyB;gBAG3B,EAAE,EAAE,MAAM,EACF,IAAI,EAAE,cAAc,EACpB,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,EACjC,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI;IAGnD,8FAA8F;IAC9F,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,IAAI;IAuChE,oEAAoE;IACpE,OAAO,CAAC,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAqChD;;;;OAIG;IACH,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,GAAG,IAAI;IAc/C,OAAO,CAAC,YAAY;IAYpB,OAAO,CAAC,aAAa;IAiBrB,6EAA6E;IAC7E,OAAO,CAAC,MAAM;IAgBd,yFAAyF;IACzF,OAAO,CAAC,UAAU;IA6BlB;;;;OAIG;YACW,SAAS;CAiDxB"}
|
package/dist/call.js
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { createPushQueue, DrainWaiter, errorFromEnvelope, } from '@ts-pf/message';
|
|
2
|
+
import { isPFError, localFailure } from '@ts-pf/protocol';
|
|
3
|
+
import { abortedFailure, isItemStream } from './shared.js';
|
|
4
|
+
function asCallError(error) {
|
|
5
|
+
if (isPFError(error)) {
|
|
6
|
+
return error;
|
|
7
|
+
}
|
|
8
|
+
return error instanceof Error
|
|
9
|
+
? localFailure(error.message, error)
|
|
10
|
+
: localFailure('Input stream failed', error);
|
|
11
|
+
}
|
|
12
|
+
/** A frame of ours that could not go out, as the error the caller sees. */
|
|
13
|
+
function sendFailure(reason) {
|
|
14
|
+
if (reason === 'oversize') {
|
|
15
|
+
return errorFromEnvelope({
|
|
16
|
+
code: 'PAYLOAD_TOO_LARGE',
|
|
17
|
+
message: 'Frame too large',
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
if (reason === 'stringify') {
|
|
21
|
+
return errorFromEnvelope({
|
|
22
|
+
code: 'INTERNAL',
|
|
23
|
+
message: 'Internal server error',
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return localFailure('Connection closed');
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* One in-flight id on the client side: the call promise until the first
|
|
30
|
+
* inbound frame, then (for an output stream) the iterator the promise
|
|
31
|
+
* resolved to, plus the `in-item` pump of a streamed input and the abort
|
|
32
|
+
* listener. Settles exactly once. Frame routing stays in `DuplexLink`.
|
|
33
|
+
*
|
|
34
|
+
* Backpressure: the output queue is bounded by `highWaterMark` and pauses the
|
|
35
|
+
* duplex through the host; the input pump awaits the duplex's drain between
|
|
36
|
+
* items and fails the call locally when the server does not drain in
|
|
37
|
+
* `drainTimeout`.
|
|
38
|
+
*/
|
|
39
|
+
export class ClientCall {
|
|
40
|
+
id;
|
|
41
|
+
host;
|
|
42
|
+
resolve;
|
|
43
|
+
reject;
|
|
44
|
+
mode = 'pending-first';
|
|
45
|
+
sendingInput = false;
|
|
46
|
+
output;
|
|
47
|
+
inputIterator;
|
|
48
|
+
unsubAbort;
|
|
49
|
+
drain;
|
|
50
|
+
constructor(id, host, resolve, reject) {
|
|
51
|
+
this.id = id;
|
|
52
|
+
this.host = host;
|
|
53
|
+
this.resolve = resolve;
|
|
54
|
+
this.reject = reject;
|
|
55
|
+
}
|
|
56
|
+
/** Send `call`, arm `signal`, and start pumping `in-item` / `in-done` for an input stream. */
|
|
57
|
+
open(path, input, signal) {
|
|
58
|
+
const streaming = isItemStream(input);
|
|
59
|
+
const frame = { type: 'call', id: this.id, path };
|
|
60
|
+
if (streaming) {
|
|
61
|
+
frame.stream = true;
|
|
62
|
+
}
|
|
63
|
+
else if (input !== undefined) {
|
|
64
|
+
frame.input = input;
|
|
65
|
+
}
|
|
66
|
+
const sent = this.host.send(frame);
|
|
67
|
+
if (!sent.ok) {
|
|
68
|
+
this.fail(sendFailure(sent.reason), false);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (signal !== undefined) {
|
|
72
|
+
const onAbort = () => {
|
|
73
|
+
this.fail(abortedFailure(signal), true);
|
|
74
|
+
};
|
|
75
|
+
signal.addEventListener('abort', onAbort);
|
|
76
|
+
this.unsubAbort = () => {
|
|
77
|
+
signal.removeEventListener('abort', onAbort);
|
|
78
|
+
};
|
|
79
|
+
if (signal.aborted) {
|
|
80
|
+
onAbort();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (streaming) {
|
|
85
|
+
this.sendingInput = true;
|
|
86
|
+
this.inputIterator = input[Symbol.asyncIterator]();
|
|
87
|
+
this.drain = new DrainWaiter(this.host.backpressure, {
|
|
88
|
+
path,
|
|
89
|
+
id: this.id,
|
|
90
|
+
});
|
|
91
|
+
void this.pumpInput(this.drain);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** An inbound frame carrying this id; `size` is its text length. */
|
|
95
|
+
onFrame(frame, size) {
|
|
96
|
+
if (this.mode === 'done') {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (this.mode === 'streaming-out') {
|
|
100
|
+
this.onStreamFrame(frame, size);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (frame.type === 'result') {
|
|
104
|
+
if (frame.ok) {
|
|
105
|
+
this.finish(() => {
|
|
106
|
+
this.resolve(frame.output);
|
|
107
|
+
});
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
this.fail(errorFromEnvelope(frame.error), false);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (frame.type === 'item') {
|
|
114
|
+
this.mode = 'streaming-out';
|
|
115
|
+
const queue = this.createOutput();
|
|
116
|
+
this.output = queue;
|
|
117
|
+
queue.push(frame.output, size);
|
|
118
|
+
this.resolve(this.wrapOutput(queue));
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (frame.type === 'done') {
|
|
122
|
+
const queue = this.createOutput();
|
|
123
|
+
this.output = queue;
|
|
124
|
+
queue.end();
|
|
125
|
+
this.resolve(this.wrapOutput(queue));
|
|
126
|
+
this.finish(() => { });
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
this.fail(localFailure('Invalid response'), false);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Settle with `error`: reject the call promise before the first frame,
|
|
133
|
+
* fail the output iterator after it. `sendCancel` tells the server to stop
|
|
134
|
+
* (abort, input pump failure); a peer close or server `result` does not.
|
|
135
|
+
*/
|
|
136
|
+
fail(error, sendCancel) {
|
|
137
|
+
const mode = this.mode;
|
|
138
|
+
this.finish(() => {
|
|
139
|
+
if (sendCancel) {
|
|
140
|
+
this.host.send({ type: 'cancel', id: this.id });
|
|
141
|
+
}
|
|
142
|
+
if (mode === 'pending-first') {
|
|
143
|
+
this.reject(error);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
this.output?.fail(error);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
createOutput() {
|
|
150
|
+
return createPushQueue({
|
|
151
|
+
highWaterMark: this.host.backpressure.highWaterMark,
|
|
152
|
+
onPause: () => {
|
|
153
|
+
this.host.pauseReads();
|
|
154
|
+
},
|
|
155
|
+
onResume: () => {
|
|
156
|
+
this.host.resumeReads();
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
onStreamFrame(frame, size) {
|
|
161
|
+
if (frame.type === 'item') {
|
|
162
|
+
this.output?.push(frame.output, size);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (frame.type === 'done') {
|
|
166
|
+
this.finish(() => {
|
|
167
|
+
this.output?.end();
|
|
168
|
+
});
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (frame.type === 'result' && !frame.ok) {
|
|
172
|
+
this.fail(errorFromEnvelope(frame.error), false);
|
|
173
|
+
}
|
|
174
|
+
// result ok:true, extra done, item after done, unknown: ignored
|
|
175
|
+
}
|
|
176
|
+
/** One-shot terminal transition; `action` runs after the id is forgotten. */
|
|
177
|
+
finish(action) {
|
|
178
|
+
if (this.mode === 'done') {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
this.mode = 'done';
|
|
182
|
+
this.sendingInput = false;
|
|
183
|
+
this.host.settle(this.id);
|
|
184
|
+
this.unsubAbort?.();
|
|
185
|
+
this.drain?.cancel();
|
|
186
|
+
void Promise.resolve(this.inputIterator?.return?.()).then(() => { }, () => { });
|
|
187
|
+
action();
|
|
188
|
+
}
|
|
189
|
+
/** The iterable the call resolves to; `break` / `return()` / `throw()` send `cancel`. */
|
|
190
|
+
wrapOutput(queue) {
|
|
191
|
+
return {
|
|
192
|
+
[Symbol.asyncIterator]: () => {
|
|
193
|
+
const inner = queue[Symbol.asyncIterator]();
|
|
194
|
+
return {
|
|
195
|
+
next: () => inner.next(),
|
|
196
|
+
return: async () => {
|
|
197
|
+
if (this.mode !== 'done') {
|
|
198
|
+
this.finish(() => {
|
|
199
|
+
this.host.send({ type: 'cancel', id: this.id });
|
|
200
|
+
this.output?.end();
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return { done: true, value: undefined };
|
|
204
|
+
},
|
|
205
|
+
throw: async (error) => {
|
|
206
|
+
if (this.mode !== 'done') {
|
|
207
|
+
this.finish(() => {
|
|
208
|
+
this.host.send({ type: 'cancel', id: this.id });
|
|
209
|
+
this.output?.fail(error);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
throw error;
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Pull the input iterator one item at a time and send `in-item`, pausing at
|
|
220
|
+
* the duplex's drain promise. A server that does not drain within
|
|
221
|
+
* `drainTimeout` fails the call locally and gets a `cancel`.
|
|
222
|
+
*/
|
|
223
|
+
async pumpInput(drain) {
|
|
224
|
+
const iterator = this.inputIterator;
|
|
225
|
+
if (iterator === undefined) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
while (this.sendingInput) {
|
|
230
|
+
const nextValue = await iterator.next();
|
|
231
|
+
if (!this.sendingInput || this.mode === 'done') {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (nextValue.done) {
|
|
235
|
+
this.sendingInput = false;
|
|
236
|
+
this.host.send({ type: 'in-done', id: this.id });
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const item = { type: 'in-item', id: this.id };
|
|
240
|
+
if (nextValue.value !== undefined) {
|
|
241
|
+
item.input = nextValue.value;
|
|
242
|
+
}
|
|
243
|
+
const sent = this.host.send(item);
|
|
244
|
+
if (!sent.ok) {
|
|
245
|
+
this.fail(sendFailure(sent.reason), sent.reason !== 'closed');
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (sent.drained === undefined) {
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
const outcome = await drain.wait(sent.drained);
|
|
252
|
+
if (outcome === 'timeout') {
|
|
253
|
+
this.fail(localFailure(`Peer did not drain within ${this.host.backpressure.drainTimeout} ms`), true);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (outcome === 'cancelled') {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
if (this.mode === 'done') {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
this.fail(asCallError(error), true);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
//# sourceMappingURL=call.js.map
|
package/dist/call.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"call.js","sourceRoot":"","sources":["../src/call.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL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CAAA;gBAC9C,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;oBAC1B,IAAI,CAAC,IAAI,CACP,YAAY,CACV,6BAA6B,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,YAAY,KAAK,CACtE,EACD,IAAI,CACL,CAAA;oBACD,OAAM;gBACR,CAAC;gBACD,IAAI,OAAO,KAAK,WAAW,EAAE,CAAC;oBAC5B,OAAM;gBACR,CAAC;YACH,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;gBACzB,OAAM;YACR,CAAC;YACD,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,CAAA;QACrC,CAAC;IACH,CAAC;CACF"}
|
package/dist/duplex.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Link } from '@ts-pf/client';
|
|
2
|
+
import { type Duplex } from '@ts-pf/message';
|
|
3
|
+
import { type LinkOptions } from './shared.js';
|
|
4
|
+
/**
|
|
5
|
+
* A `Link` over any `Duplex` from `@ts-pf/message`: write one for your pipe
|
|
6
|
+
* (Electron IPC, a Chrome runtime port, WebTransport, a Unix socket) and
|
|
7
|
+
* hand it here. `PortLink` / `WsLink` / `StdioLink` are this plus their
|
|
8
|
+
* duplex adapter. The link owns the handshake, id allocation, multiplexing,
|
|
9
|
+
* cancel, and slow-peer protection (`LinkOptions.backpressure`); the duplex
|
|
10
|
+
* only moves JSON text. `close()` closes the duplex. Reads pause
|
|
11
|
+
* (connection-wide) while any output stream's queue is over its high-water
|
|
12
|
+
* mark and the duplex can pause.
|
|
13
|
+
*/
|
|
14
|
+
export declare class DuplexLink implements Link {
|
|
15
|
+
private readonly inflight;
|
|
16
|
+
private readonly host;
|
|
17
|
+
private readonly session;
|
|
18
|
+
private nextId;
|
|
19
|
+
private closed;
|
|
20
|
+
constructor(opts: {
|
|
21
|
+
duplex: Duplex;
|
|
22
|
+
} & LinkOptions);
|
|
23
|
+
call(path: string[], input: unknown, signal?: AbortSignal): ReturnType<Link['call']>;
|
|
24
|
+
close(): void;
|
|
25
|
+
private callInner;
|
|
26
|
+
/** `PROTOCOL.md` "Multiplex": frames route by id; a handshake frame after `ready` closes. */
|
|
27
|
+
private onFrame;
|
|
28
|
+
private onClose;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=duplex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"duplex.d.ts","sourceRoot":"","sources":["../src/duplex.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,eAAe,CAAA;AACzC,OAAO,EACL,KAAK,MAAM,EAIZ,MAAM,gBAAgB,CAAA;AAGvB,OAAO,EAAkC,KAAK,WAAW,EAAE,MAAM,aAAa,CAAA;AAE9E;;;;;;;;;GASG;AACH,qBAAa,UAAW,YAAW,IAAI;IACrC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgC;IACzD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAgB;IACrC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAgB;IACxC,OAAO,CAAC,MAAM,CAAI;IAClB,OAAO,CAAC,MAAM,CAAQ;gBAEV,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,WAAW;IA8ClD,IAAI,CACF,IAAI,EAAE,MAAM,EAAE,EACd,KAAK,EAAE,OAAO,EACd,MAAM,CAAC,EAAE,WAAW,GACnB,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAI3B,KAAK,IAAI,IAAI;YAIC,SAAS;IAqBvB,6FAA6F;IAC7F,OAAO,CAAC,OAAO;IAYf,OAAO,CAAC,OAAO;CAUhB"}
|
package/dist/duplex.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { MessageSession, resolveBackpressure, } from '@ts-pf/message';
|
|
2
|
+
import { localFailure } from '@ts-pf/protocol';
|
|
3
|
+
import { ClientCall } from './call.js';
|
|
4
|
+
import { abortedFailure, awaitWithAbort } from './shared.js';
|
|
5
|
+
/**
|
|
6
|
+
* A `Link` over any `Duplex` from `@ts-pf/message`: write one for your pipe
|
|
7
|
+
* (Electron IPC, a Chrome runtime port, WebTransport, a Unix socket) and
|
|
8
|
+
* hand it here. `PortLink` / `WsLink` / `StdioLink` are this plus their
|
|
9
|
+
* duplex adapter. The link owns the handshake, id allocation, multiplexing,
|
|
10
|
+
* cancel, and slow-peer protection (`LinkOptions.backpressure`); the duplex
|
|
11
|
+
* only moves JSON text. `close()` closes the duplex. Reads pause
|
|
12
|
+
* (connection-wide) while any output stream's queue is over its high-water
|
|
13
|
+
* mark and the duplex can pause.
|
|
14
|
+
*/
|
|
15
|
+
export class DuplexLink {
|
|
16
|
+
inflight = new Map();
|
|
17
|
+
host;
|
|
18
|
+
session;
|
|
19
|
+
nextId = 1;
|
|
20
|
+
closed = false;
|
|
21
|
+
constructor(opts) {
|
|
22
|
+
const backpressure = resolveBackpressure(opts.backpressure);
|
|
23
|
+
let pausedQueues = 0;
|
|
24
|
+
this.host = {
|
|
25
|
+
send: (frame) => this.session.send(frame),
|
|
26
|
+
settle: (id) => {
|
|
27
|
+
this.inflight.delete(id);
|
|
28
|
+
},
|
|
29
|
+
backpressure,
|
|
30
|
+
pauseReads: () => {
|
|
31
|
+
pausedQueues += 1;
|
|
32
|
+
if (pausedQueues === 1) {
|
|
33
|
+
opts.duplex.pause?.();
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
resumeReads: () => {
|
|
37
|
+
pausedQueues -= 1;
|
|
38
|
+
if (pausedQueues === 0) {
|
|
39
|
+
opts.duplex.resume?.();
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
this.session = new MessageSession({
|
|
44
|
+
duplex: opts.duplex,
|
|
45
|
+
role: 'client',
|
|
46
|
+
...(opts.maxFrameBytes !== undefined
|
|
47
|
+
? { maxFrameBytes: opts.maxFrameBytes }
|
|
48
|
+
: {}),
|
|
49
|
+
...(opts.helloTimeoutMs !== undefined
|
|
50
|
+
? { helloTimeoutMs: opts.helloTimeoutMs }
|
|
51
|
+
: {}),
|
|
52
|
+
...(opts.meta !== undefined ? { helloMeta: opts.meta } : {}),
|
|
53
|
+
onFrame: (frame, size) => {
|
|
54
|
+
this.onFrame(frame, size);
|
|
55
|
+
},
|
|
56
|
+
// decode failures never settle a call, in-flight or not
|
|
57
|
+
onInvalidFrame: () => { },
|
|
58
|
+
onClose: (reason) => {
|
|
59
|
+
this.onClose(reason);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
void this.session.ready.catch(() => {
|
|
63
|
+
// handshake / close rejects ready; call() re-awaits the same promise
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
call(path, input, signal) {
|
|
67
|
+
return this.callInner(path, input, signal);
|
|
68
|
+
}
|
|
69
|
+
close() {
|
|
70
|
+
this.session.close();
|
|
71
|
+
}
|
|
72
|
+
async callInner(path, input, signal) {
|
|
73
|
+
await awaitWithAbort(this.session.ready, signal);
|
|
74
|
+
if (this.closed) {
|
|
75
|
+
throw localFailure('Connection closed');
|
|
76
|
+
}
|
|
77
|
+
if (signal?.aborted) {
|
|
78
|
+
throw abortedFailure(signal);
|
|
79
|
+
}
|
|
80
|
+
const id = String(this.nextId);
|
|
81
|
+
this.nextId += 1;
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
const call = new ClientCall(id, this.host, resolve, reject);
|
|
84
|
+
this.inflight.set(id, call);
|
|
85
|
+
call.open(path, input, signal);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/** `PROTOCOL.md` "Multiplex": frames route by id; a handshake frame after `ready` closes. */
|
|
89
|
+
onFrame(frame, size) {
|
|
90
|
+
if (frame.type === 'hello' ||
|
|
91
|
+
frame.type === 'hello-ok' ||
|
|
92
|
+
frame.type === 'hello-error') {
|
|
93
|
+
this.session.close();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
this.inflight.get(frame.id)?.onFrame(frame, size);
|
|
97
|
+
}
|
|
98
|
+
onClose(reason) {
|
|
99
|
+
this.closed = true;
|
|
100
|
+
const error = reason === undefined
|
|
101
|
+
? localFailure('Connection closed')
|
|
102
|
+
: localFailure('Connection closed', reason);
|
|
103
|
+
for (const call of [...this.inflight.values()]) {
|
|
104
|
+
call.fail(error, false);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=duplex.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"duplex.js","sourceRoot":"","sources":["../src/duplex.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,cAAc,EACd,mBAAmB,GACpB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,UAAU,EAAuB,MAAM,WAAW,CAAA;AAC3D,OAAO,EAAE,cAAc,EAAE,cAAc,EAAoB,MAAM,aAAa,CAAA;AAE9E;;;;;;;;;GASG;AACH,MAAM,OAAO,UAAU;IACJ,QAAQ,GAAG,IAAI,GAAG,EAAsB,CAAA;IACxC,IAAI,CAAgB;IACpB,OAAO,CAAgB;IAChC,MAAM,GAAG,CAAC,CAAA;IACV,MAAM,GAAG,KAAK,CAAA;IAEtB,YAAY,IAAsC;QAChD,MAAM,YAAY,GAAG,mBAAmB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;QAC3D,IAAI,YAAY,GAAG,CAAC,CAAA;QACpB,IAAI,CAAC,IAAI,GAAG;YACV,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC;YACzC,MAAM,EAAE,CAAC,EAAE,EAAE,EAAE;gBACb,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;YAC1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C,IAAI,KAAK,UAAU;YACzB,KAAK,CAAC,IAAI,KAAK,aAAa,EAC5B,CAAC;YACD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAA;YACpB,OAAM;QACR,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IACnD,CAAC;IAEO,OAAO,CAAC,MAAgB;QAC9B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAA;QAClB,MAAM,KAAK,GACT,MAAM,KAAK,SAAS;YAClB,CAAC,CAAC,YAAY,CAAC,mBAAmB,CAAC;YACnC,CAAC,CAAC,YAAY,CAAC,mBAAmB,EAAE,MAAM,CAAC,CAAA;QAC/C,KAAK,MAAM,IAAI,IAAI,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC;YAC/C,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;QACzB,CAAC;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
export { DuplexLink } from './duplex.js';
|
|
1
2
|
export { PortLink } from './port.js';
|
|
2
|
-
export type { LinkOptions } from './shared.js';
|
|
3
|
+
export type { BackpressureOptions, LinkOptions, StallInfo, } from './shared.js';
|
|
3
4
|
export type { WebSocketLike } from './ws.js';
|
|
4
5
|
export { WsLink } from './ws.js';
|
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,YAAY,EACV,mBAAmB,EACnB,WAAW,EACX,SAAS,GACV,MAAM,aAAa,CAAA;AACpB,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAOpC,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA"}
|
package/dist/port.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { Link } from '@ts-pf/client';
|
|
2
|
-
import {
|
|
2
|
+
import type { LinkOptions } from './shared.js';
|
|
3
|
+
/** `DuplexLink` over `createPortDuplex`; `close()` also closes the port. */
|
|
3
4
|
export declare class PortLink implements Link {
|
|
4
|
-
private readonly
|
|
5
|
+
private readonly link;
|
|
5
6
|
private readonly port;
|
|
6
7
|
private closed;
|
|
7
8
|
constructor(opts: {
|
package/dist/port.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"port.d.ts","sourceRoot":"","sources":["../src/port.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,eAAe,CAAA;
|
|
1
|
+
{"version":3,"file":"port.d.ts","sourceRoot":"","sources":["../src/port.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,eAAe,CAAA;AAGzC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAE9C,4EAA4E;AAC5E,qBAAa,QAAS,YAAW,IAAI;IACnC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAY;IACjC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAa;IAClC,OAAO,CAAC,MAAM,CAAQ;gBAEV,IAAI,EAAE;QAAE,IAAI,EAAE,WAAW,CAAA;KAAE,GAAG,WAAW;IAOrD,IAAI,CACF,IAAI,EAAE,MAAM,EAAE,EACd,KAAK,EAAE,OAAO,EACd,MAAM,CAAC,EAAE,WAAW,GACnB,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAI3B,KAAK,IAAI,IAAI;CAYd"}
|