@ts-pf/message-client 0.2.4 → 0.5.1
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/README.md +30 -0
- package/package.json +9 -8
- package/skills/ts-pf-message-client/SKILL.md +7 -3
package/README.md
CHANGED
|
@@ -13,6 +13,8 @@ const client = createClient<typeof contract>(new PortLink({ port }), {
|
|
|
13
13
|
})
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
+
`WsLink` takes `{ url, WebSocket? }` (default `globalThis.WebSocket`) or an open `{ socket }`. The npm `ws` 8.x `WebSocket` class and its sockets fit with no cast; this package does not depend on `ws`.
|
|
17
|
+
|
|
16
18
|
Stdio is a subpath, not on the main index:
|
|
17
19
|
|
|
18
20
|
```ts
|
|
@@ -47,6 +49,34 @@ const client = createClient<typeof contract>(new DuplexLink({ duplex }))
|
|
|
47
49
|
|
|
48
50
|
User owns listen / upgrade / spawn / Worker bootstrap. Prod never depends on `@ts-pf/server`.
|
|
49
51
|
|
|
52
|
+
## Credentials and refused connections
|
|
53
|
+
|
|
54
|
+
`meta` is sent once, in the `hello` that opens the connection. The convention for a credential is `meta.authorization` with the same value as an HTTP `Authorization` header (browsers cannot set headers on a WebSocket upgrade). A server that refuses the connection throws a `PFError` from its context factory; every call on the link then rejects with that `code` / `message` / `data` (not a local failure, so `RetryPlugin` does not retry it by default). The credential is fixed for the connection: to rotate it, close the link and open a new one. Streams do not survive a reconnect; call the stream procedure again after the new link is ready. A server that ends a live connection with an application close code (4400–4499, e.g. `4401`) fails in-flight calls with a local `Connection closed`; the code is on the socket's `close` event, so pass your own `socket` to `WsLink` if you need it.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { createClient } from '@ts-pf/client'
|
|
58
|
+
import { WsLink } from '@ts-pf/message-client'
|
|
59
|
+
import { isPFError } from '@ts-pf/protocol'
|
|
60
|
+
|
|
61
|
+
declare const token: string
|
|
62
|
+
|
|
63
|
+
const link = new WsLink({
|
|
64
|
+
url: 'wss://example.com/rpc',
|
|
65
|
+
WebSocket,
|
|
66
|
+
meta: { authorization: `Bearer ${token}` }, // same value as an HTTP Authorization header
|
|
67
|
+
})
|
|
68
|
+
const client = createClient<typeof contract>(link)
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
await client.planet.find({ id: 1 })
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// a refusal code is the server's, not the procedure's: it is not in the typed error union
|
|
74
|
+
if (isPFError(error) && error.code === 'UNAUTHORIZED') {
|
|
75
|
+
// the server refused the hello; open a new link with a fresh credential
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
50
80
|
## Backpressure
|
|
51
81
|
|
|
52
82
|
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.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ts-pf/message-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Opt-in PortLink, WsLink, and StdioLink",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ts-pf",
|
|
@@ -54,16 +54,17 @@
|
|
|
54
54
|
"lint": "biome check src tests"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@ts-pf/client": "0.
|
|
58
|
-
"@ts-pf/message": "0.
|
|
59
|
-
"@ts-pf/protocol": "0.
|
|
57
|
+
"@ts-pf/client": "0.5.1",
|
|
58
|
+
"@ts-pf/message": "0.5.1",
|
|
59
|
+
"@ts-pf/protocol": "0.5.1"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@ts-pf/contract": "0.
|
|
63
|
-
"@ts-pf/message-server": "0.
|
|
64
|
-
"@ts-pf/server": "0.
|
|
65
|
-
"@ts-pf/stream": "0.
|
|
62
|
+
"@ts-pf/contract": "0.5.1",
|
|
63
|
+
"@ts-pf/message-server": "0.5.1",
|
|
64
|
+
"@ts-pf/server": "0.5.1",
|
|
65
|
+
"@ts-pf/stream": "0.5.1",
|
|
66
66
|
"@types/node": "^22.15.0",
|
|
67
|
+
"@types/ws": "^8.18.2",
|
|
67
68
|
"typebox": "^1.4.6",
|
|
68
69
|
"zod": "^4.0.0"
|
|
69
70
|
}
|
|
@@ -24,6 +24,8 @@ const client = createClient<typeof contract>(new PortLink({ port }), {
|
|
|
24
24
|
new WsLink({ url: 'wss://example.com/rpc', WebSocket }) // or { socket }
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
+
`WebSocket` defaults to `globalThis.WebSocket`; the npm `ws` 8.x class (`import { WebSocket } from 'ws'`, an app dependency) and its sockets fit with no cast.
|
|
28
|
+
|
|
27
29
|
```ts
|
|
28
30
|
import { spawn } from 'node:child_process'
|
|
29
31
|
import { StdioLink } from '@ts-pf/message-client/stdio'
|
|
@@ -51,14 +53,16 @@ const duplex: Duplex = {
|
|
|
51
53
|
onClose: (handler) => ipc.on('close', handler),
|
|
52
54
|
close: () => ipc.close(),
|
|
53
55
|
}
|
|
54
|
-
const link = new DuplexLink({ duplex, meta: {
|
|
56
|
+
const link = new DuplexLink({ duplex, meta: { authorization: 'Bearer abc' } })
|
|
55
57
|
const client = createClient<typeof contract>(link)
|
|
56
58
|
link.close()
|
|
57
59
|
```
|
|
58
60
|
|
|
59
61
|
User owns listen / upgrade / spawn / Worker bootstrap. Impls may have `close()`; do not add `close()` to `Link`.
|
|
60
62
|
|
|
61
|
-
|
|
63
|
+
`meta` rides on the opening `hello` only. Put a credential in `meta.authorization` (same value as an HTTP `Authorization` header). A server that refuses the connection sends a `hello-error`: every call on that link rejects with the server's `PFError` (`code` such as `UNAUTHORIZED`, not local, not retried by `RetryPlugin`'s default). To rotate the credential, open a new link; streams do not survive a reconnect, so resubscribe after it is ready. Never retry `BAD_REQUEST` `Unsupported protocol version`. A server close code (4400–4499) is not on the `PFError` (calls fail with local `Connection closed`); pass your own `socket` to `WsLink` and listen for `close` to read it.
|
|
64
|
+
|
|
65
|
+
Slow servers are handled by default (`LinkOptions.backpressure`, the same `{ highWaterMark?, drainTimeout?, onStall? }` as the server; 1 MiB / 30 s / one `console.warn` on `aborted` only, and a throwing `onStall` is swallowed): an `AsyncIterable` input is pumped one item at a time and parked while the duplex is over its mark; after `drainTimeout` the call fails locally (`isLocalFailure` from `@ts-pf/client` is true; `Peer did not drain within 30000 ms`), `cancel` goes out, and the input iterator's `return()` runs once. Output streams you read are bounded by the same mark (frame text length) — the link pauses reads (connection-wide) while your `for await` lags, where the pipe can pause. A server that gives up on a slow reader ends the stream with `INTERNAL` / `Peer did not drain within …` after the buffered items; that one is not local. A stall is always that `INTERNAL` with the fixed `Peer did not drain within <n> ms` format and no dedicated code, distinct from any error code an app declares. `drainTimeout: 0` waits forever; `highWaterMark: Infinity` disables the receive-queue bound and the WebSocket `bufferedAmount` mark (stdio's write mark is the `Writable`'s own); `onStall: false` silences; a bad value throws from the constructor.
|
|
62
66
|
|
|
63
67
|
## API
|
|
64
68
|
|
|
@@ -73,7 +77,7 @@ Slow servers are handled by default (`LinkOptions.backpressure`, the same `{ hig
|
|
|
73
77
|
|
|
74
78
|
## Don't
|
|
75
79
|
|
|
76
|
-
-
|
|
80
|
+
- Expect this package to bring `ws`: it has no `ws` dependency. On Node, the app installs `ws` and passes its `WebSocket` class (or an open socket).
|
|
77
81
|
- Reconstruct HTTP status from `error.code`.
|
|
78
82
|
- Copy the multiplexer for a new pipe — write a `Duplex` and use `DuplexLink`.
|
|
79
83
|
- Retry a `Peer did not drain` failure blindly — the server stopped reading; fix the consumer or raise `drainTimeout`.
|