@johnhenry/wsh 0.50.0 → 0.52.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/README.md +148 -19
- package/bin/wsh-objects-types.mjs +98 -0
- package/package.json +7 -2
- package/spec/gateway-fail.vectors.json +14 -0
- package/spec/objects.md +212 -0
- package/spec/wsh-v1.yaml +12 -9
- package/src/duplex.mjs +2 -1
- package/src/gateway-client.mjs +6 -2
- package/src/gateway-errors.mjs +12 -5
- package/src/index.d.ts +11 -4
- package/src/objects-core.mjs +270 -41
- package/src/objects-mcp.mjs +130 -0
- package/src/objects-schema.mjs +143 -0
- package/src/objects-type-text.mjs +95 -0
- package/src/objects-types.d.ts +38 -0
- package/src/objects-types.mjs +292 -0
- package/src/objects.d.ts +127 -7
- package/src/objects.mjs +4 -1
- package/src/page-host.d.ts +26 -5
- package/src/page-host.mjs +53 -16
- package/src/webmcp.mjs +17 -4
package/spec/objects.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# The `wsh-objects` protocol (remote objects)
|
|
2
|
+
|
|
3
|
+
Status: describes what `@johnhenry/wsh/objects` (`src/objects-core.mjs`, `src/objects.mjs`) serves and speaks as of 0.51.0, and what the Rust client
|
|
4
|
+
(`wsh_client::objects`, `wsh objects`) speaks. It adds nothing to the wire: this file only writes down what the code does. The JavaScript
|
|
5
|
+
implementation is the reference; where this text and `src/objects-core.mjs` differ, the code is right and this file is a bug.
|
|
6
|
+
|
|
7
|
+
A host names some **roots** (objects: a page's `window`, a worker's `self`, a Node object). A peer reaches into them **by path** and only
|
|
8
|
+
**cloneable values** cross: plain data, or a name for what is not plain data. There is no distributed object graph. What may be read, called,
|
|
9
|
+
set or listened to is decided by the host's allow-lists (paths with `*` and `**`), by `authorize`, and by the key's capabilities
|
|
10
|
+
(`objects:ROOT[:OP]`, `spec/capabilities.md`); the protocol only carries the request and the refusal.
|
|
11
|
+
|
|
12
|
+
## 1. The channel
|
|
13
|
+
|
|
14
|
+
An rpc channel of protocol `wsh-objects` (`rpc-protocol:wsh-objects` in `ServerHello.features`), opened as any rpc channel is: `Open { kind: rpc,
|
|
15
|
+
command: "wsh-objects" }`. On a sealed session or behind a relay the host states `rpc-virtual` and the channel rides `SessionData` (inside the
|
|
16
|
+
seal) with `GatewayCredit` backpressure; otherwise it is a data stream. Messages are a CBOR sequence (RFC 8742) of JSON-RPC 2.0 messages
|
|
17
|
+
(`{ jsonrpc: "2.0", id, method, params }`, `{ jsonrpc, id, result }`, `{ jsonrpc, id, error: { code, message, data? } }`, notifications without
|
|
18
|
+
`id`), bounded by `rpc-max-message:<n>` (default 1 MiB). `$/cancel { id }` cancels a request; `$/progress` is not used by this protocol.
|
|
19
|
+
|
|
20
|
+
A path is an array of segments in requests (`["app","cart","items"]`), and text with dots for people (`window.app.cart.items`). The first segment of
|
|
21
|
+
the text form is the root.
|
|
22
|
+
|
|
23
|
+
## 2. Requests (peer to host)
|
|
24
|
+
|
|
25
|
+
Every `params` is an object. A request names where it starts: `root: "window"` (a root the host listed) or `handle: "<id>"` (section 5), never both.
|
|
26
|
+
|
|
27
|
+
| method | params | result |
|
|
28
|
+
|---|---|---|
|
|
29
|
+
| `roots` | `{ features?: [string…] }` | `{ roots: [name…], features: [string…] }` |
|
|
30
|
+
| `resolve` | `{ root \| handle, ops: [op…] }` | a **wire value** (section 3) |
|
|
31
|
+
| `set` | `{ root \| handle, path: [seg…], value }` | `{ ok: true }` |
|
|
32
|
+
| `describe` | `{ root \| handle, path?: [seg…] }` | `{ type, keys: [{ key, type, own }…], truncated? }` |
|
|
33
|
+
| `snapshot` | `{ root \| handle, path?: [seg…], depth?, maxBytes? }` | a wire value; `truncated: true` when a subtree deeper than `depth` was cut to `null` |
|
|
34
|
+
| `subscribe` | `{ id, root \| handle, path?: [seg…], type, fields?: [name…] }` | `{ ok: true }` |
|
|
35
|
+
| `unsubscribe` | `{ id }` | `{ ok: true, dropped }` |
|
|
36
|
+
| `release` | `{ handle }` | `{ ok: true }` |
|
|
37
|
+
| `releaseCallback` | `{ id }` | `{ ok: true }` |
|
|
38
|
+
| `callbackResult` | `{ rid, value? \| error?: true }` | `{ ok }` (peer to host, section 6) |
|
|
39
|
+
| `types` | `{ root }` | `{ version, root, ops, allow, eventFields, types, handles }`: plain data |
|
|
40
|
+
|
|
41
|
+
`roots` lists only the roots the key may use (`objects:ROOT[:OP]`). `features` is what the host supports: `events`, `callbacks`, `callback-returns` (only when an
|
|
42
|
+
allow-list entry lets a callback return a value), `types` and (when a root opted in) `handles` and `handle-events`. A host that says nothing (before 0.48.0) supports
|
|
43
|
+
none of them; a host before 0.51.0 does not say `types` or `handle-events`; a peer must not use a feature the host did not state. A peer says what it can do in the
|
|
44
|
+
optional `features` of its `roots` request: `callback-returns` means it answers a callback that asks for a return value (section 6). A host that predates the field
|
|
45
|
+
ignores it.
|
|
46
|
+
|
|
47
|
+
**`types`** answers what this key may do on one root: `ops` (the operations among `get`, `call`, `set`, `on` the key holds on it), `allow` (the host's allow-list
|
|
48
|
+
entries for the root, by operation, only for those operations; entries are written with the root first, `window.app.cart.add`), `eventFields` (the extra event
|
|
49
|
+
fields of the types the key may listen to), `types` (the host's type map, `{ path: { value?, args?, result?, data? } }`: host-authored type text, never to be
|
|
50
|
+
evaluated), `handles` (whether the root opted in). Every string in it is the host's text: untrusted.
|
|
51
|
+
|
|
52
|
+
**`resolve`** walks a whole chain in one request. `ops` is up to 32 operations, each an object with exactly one of:
|
|
53
|
+
|
|
54
|
+
- `{ get: key }` read a member;
|
|
55
|
+
- `{ call: key, args?: [value…], callbacks?: [[index, id]…] }` call the method `key` of the current value (`this` is the current value);
|
|
56
|
+
- `{ construct: key, args?, callbacks? }` `new` the constructor `key`.
|
|
57
|
+
|
|
58
|
+
The chain continues on the result of each step, so `window.app.getCart().items` is `get app, call getCart, get items`. The path an operation is
|
|
59
|
+
judged by is the keys so far, with `()` after a called one (`window.app.getCart().items`): that is what an allow-list entry names. The last operation
|
|
60
|
+
is the one the allow-list must allow (`get`, or `call`/`construct` from the `call` list); the ones before it need only be on the way to something
|
|
61
|
+
the host allows. At most 16 arguments (`maxArgs`). `callbacks` are section 6.
|
|
62
|
+
|
|
63
|
+
`set` assigns the last segment of `path` on the object the rest of it reaches. `describe` lists the names the allow-lists could walk to from the path
|
|
64
|
+
(`type` is `null`, `boolean`, `number`, `string`, `array`, `object`, `function`, `accessor`…). `snapshot` is a bounded deep copy: `depth` (whole
|
|
65
|
+
number, at most the host's `maxDepth`, 8) and `maxBytes` bound it.
|
|
66
|
+
|
|
67
|
+
**`subscribe`** listens to an event source named by an `allow.on` entry `path@type`: the object at `path` must have `addEventListener` /
|
|
68
|
+
`removeEventListener` or `on` / `off`. `id` is a whole number chosen by the peer, at least 1, not yet used on the channel. `type` is a name of
|
|
69
|
+
letters, digits and `_ $ : . -` (up to 128). `fields` (at most 16) are extra event properties the host lists for the type (`eventFields`); a field it
|
|
70
|
+
does not offer is `not-allowed`. A subscription through a handle (`handle` in place of `root`, only if `features` has `handle-events`) is judged as a path subscription
|
|
71
|
+
for the path that produced the handle extended by `path` (its own `allow.on` entry naming the type, the key's `objects:ROOT:on`, `authorize`) and ends with the
|
|
72
|
+
handle: `release`, the session ending and a revoke end it. A host before 0.51.0 takes a `root` only.
|
|
73
|
+
|
|
74
|
+
## 3. Wire values
|
|
75
|
+
|
|
76
|
+
The result of `resolve`, `snapshot` is one of:
|
|
77
|
+
|
|
78
|
+
| shape | meaning |
|
|
79
|
+
|---|---|
|
|
80
|
+
| `{ v: <plain> }` | plain data, plus `truncated: true` on a `snapshot` that cut something |
|
|
81
|
+
| `{ u: <kind>, type?, text? }` | a **named non-value**: something that cannot be cloned |
|
|
82
|
+
| `{ h: <id>, kind, type?, preview?: { keys: [name…] } }` | an opaque **handle** (section 5), only on a root that opted in and only if `features` has `handles` |
|
|
83
|
+
|
|
84
|
+
**Plain data** is `null`, booleans, numbers (integers as CBOR integers, others as floats), text, **bytes** (a CBOR byte string: an `ArrayBuffer`,
|
|
85
|
+
a typed array or a `DataView` on the host), arrays and plain objects with text keys. Inside an object or array the rule is JSON's: a member that
|
|
86
|
+
cannot be cloned (a function, a node, a class instance, a symbol, an accessor property, `undefined`, a cycle) is left out of an object and is `null`
|
|
87
|
+
in an array; the keys `__proto__`, `constructor` and `prototype` are never copied. An integer of 2^63 or more is not plain data (a non-value
|
|
88
|
+
`number`). A value is at most `maxDepth` (8) deep and `maxValueBytes` (256 KiB, and under the channel's message bound less 4096) long, else the
|
|
89
|
+
request is refused with `limit`.
|
|
90
|
+
|
|
91
|
+
**Named non-values**, `u` is one of:
|
|
92
|
+
|
|
93
|
+
| `u` | what it names | `type` / `text` |
|
|
94
|
+
|---|---|---|
|
|
95
|
+
| `undefined` | `undefined` | |
|
|
96
|
+
| `function` | a function | `type`: its name, when printable |
|
|
97
|
+
| `symbol` | a symbol | |
|
|
98
|
+
| `bigint` | a bigint | `text`: its digits |
|
|
99
|
+
| `number` | an integer too large for the wire | `text`: its rendering |
|
|
100
|
+
| `Date` | a date | `text`: ISO 8601 (or `Invalid Date`) |
|
|
101
|
+
| `RegExp` | a regular expression | `text`: its source and flags |
|
|
102
|
+
| `Error` | an error or `DOMException` | `type`: class name, `text`: its message (up to 200 characters) |
|
|
103
|
+
| `object` | any other object: a class instance, a node, a Map | `type`: its class name |
|
|
104
|
+
|
|
105
|
+
`type` is up to 64 printable ASCII characters. **Everything in `type`, `text`, names, previews and strings is the page's own text: untrusted.** A
|
|
106
|
+
peer prints it escaped and never interprets it.
|
|
107
|
+
|
|
108
|
+
**Values sent to the host** (`args`, `set`'s `value`) are plain data only: `null`, booleans, numbers, text, bytes, arrays, plain objects. The host
|
|
109
|
+
refuses any other kind (`bad-request`), a key `__proto__`, `constructor` or `prototype` (`always-refused`), values deeper than 8 or longer than
|
|
110
|
+
`maxValueBytes`.
|
|
111
|
+
|
|
112
|
+
## 4. Always-refused names and the rules that hold for every allow-list
|
|
113
|
+
|
|
114
|
+
Whatever an allow-list says, these are refused (reason `always-refused`): the path segments `__proto__`, `prototype`, `constructor`,
|
|
115
|
+
`__defineGetter__`, `__defineSetter__`, `__lookupGetter__`, `__lookupSetter__`, `eval`, `Function`, `import`, `execScript`; `call`, `apply` and
|
|
116
|
+
`bind` of a function; `setTimeout`, `setInterval` and `setImmediate` called with a string. A segment is a string of 1 to 256 characters with no
|
|
117
|
+
`.`. The names in `NEEDS_LISTING` (`document.cookie`, `localStorage`, `sessionStorage`, `indexedDB`, `navigator.credentials`, `fetch`,
|
|
118
|
+
`XMLHttpRequest`, `postMessage`, `open`; and on a root with handles the members that climb back to the window or document) are refused (reason
|
|
119
|
+
`needs-listing`) until an allow-list entry **names** them: a wildcard does not.
|
|
120
|
+
|
|
121
|
+
## 5. Handles
|
|
122
|
+
|
|
123
|
+
On a root that the host says opts in (`features` has `handles`), a function or an object that is not plain data comes back as a handle
|
|
124
|
+
`{ h, kind: "function" | "object", type?, preview? }` instead of a named non-value. A handle is **path-bound**: it stands at the path that produced
|
|
125
|
+
it, and every operation through it (`resolve`, `set`, `describe`, `snapshot` with `handle` in place of `root`) is judged by the allow-lists for that
|
|
126
|
+
path extended by the member named; a handle never widens what the path grants. `preview.keys` (up to 8 names) is told only where a `get` of the
|
|
127
|
+
path would be allowed. The id is 22 characters of base64url from the host's CSPRNG, valid on this channel only (another channel or key finds
|
|
128
|
+
nothing: `bad-request` "no such handle"). The table holds 64 handles per channel (`maxHandles`); more is `limit` and **nothing is evicted**. A
|
|
129
|
+
handle is given back by `release` and all of them when the channel ends. A handle is not an argument: it means something only on its own channel.
|
|
130
|
+
|
|
131
|
+
## 6. Callbacks
|
|
132
|
+
|
|
133
|
+
A function of the peer may be passed to a `call` or `construct` whose allow-list entry ends in `(callback)`: its place in `args` is `null` and
|
|
134
|
+
`callbacks` lists `[argumentIndex, id]` (`id` a whole number from 1, the same function the same id). The host's stub is **one-way**: calling it
|
|
135
|
+
copies its arguments as plain data, queues a `callback` notification and returns `undefined` at once. The host sends
|
|
136
|
+
`callback { id, seq, args: [value…], dropped? }`; calls beyond `maxCallbacksPerSecond` (20), larger than `maxCallbackBytes` (16 KiB), or when the
|
|
137
|
+
queue is full are dropped and counted (`dropped`, told with the next delivered call). There are at most 16 callbacks per channel
|
|
138
|
+
(`maxCallbacks`). `releaseCallback` kills a stub. `features` has `callbacks`.
|
|
139
|
+
|
|
140
|
+
**Return values (optional, off unless the host says so).** The allow-list entry is `(callback)` for a one-way stub, `(callback:returns)` when every function it takes
|
|
141
|
+
may return a value, `(callback:returns=I,J)` for the functions at those argument positions only (0 to 99). Such a stub returns a promise to the page's code, never
|
|
142
|
+
blocks, and the host settles it at the latest `callbackReturnTimeoutMs` (default 2000, at most 30000) after the call: with the value, or with `undefined` in every
|
|
143
|
+
other case (timeout, an error in the peer's function, a value that is not plain data or is over `maxCallbackResultBytes` (16384), a call dropped by the rate or size
|
|
144
|
+
bound, a full table of waiting calls (`maxPendingCallbackReturns`, default 8, at most 64: such a call is not sent), a released callback, the end of the channel).
|
|
145
|
+
A host asks only a peer that listed `callback-returns` in its `roots` request, and says it supports this in `features` (`callback-returns`). The `callback`
|
|
146
|
+
notification then has one more field, `rid` (a whole number from 1, per channel): `callback { id, seq, args, dropped?, rid? }`. The peer answers once with the request
|
|
147
|
+
`callbackResult { rid, value }` (`value` plain data, left out for `undefined`) or `callbackResult { rid, error: true }` (an error, a function, a handle, anything that
|
|
148
|
+
is not plain data: the text of an error is never sent), result `{ ok: true }`, or `{ ok: false }` for a `rid` nobody waits for (late, unknown or already answered);
|
|
149
|
+
the host counts such an answer against `maxRequestsPerSecond` and ignores it. A peer that does not answer costs the page the timeout, once per call; a host never has
|
|
150
|
+
more than `maxPendingCallbackReturns` calls waiting. In the `types` answer the entry is told as `(callback)`.
|
|
151
|
+
|
|
152
|
+
## 7. Notifications (host to peer)
|
|
153
|
+
|
|
154
|
+
- `event { id, seq, data, dropped? }`: `id` is the subscription's, `seq` counts the delivered events of that subscription from 1. `data` is plain: for
|
|
155
|
+
an EventTarget source `{ type, timeStamp, isTrusted, ...fields }`, for an emitter `{ type, args: [value…] }`. `dropped` (when present) is the
|
|
156
|
+
number of events the host dropped (rate `maxEventsPerSecond` 20 per subscription, size `maxEventBytes` 16 KiB, queue `maxQueuedEvents` 100 per
|
|
157
|
+
channel, a slow reader) since the last one it delivered for that subscription. At most 16 subscriptions per channel.
|
|
158
|
+
- `callback { id, seq, args, dropped? }`: section 6.
|
|
159
|
+
- `closing { reason }`: the host is about to end the channel (the page left, Stop was pressed, a key was revoked).
|
|
160
|
+
|
|
161
|
+
A peer treats a notification for an id it does not know as noise.
|
|
162
|
+
|
|
163
|
+
## 8. Errors and limits
|
|
164
|
+
|
|
165
|
+
An error response is `{ code, message, data: { reason, ... } }`. `message` is a sentence for a person and may carry page-controlled text.
|
|
166
|
+
|
|
167
|
+
| `code` | meaning | `reason` (in `data`) |
|
|
168
|
+
|---|---|---|
|
|
169
|
+
| `-32003` | refused | `always-refused`, `not-allowed` (allow-list, or an event type or field the host does not offer), `needs-listing`, `authorize` (the host's `authorize` said no), `not-permitted` (the key's `objects:` capability does not allow the operation on the root), `gone` (the page left) |
|
|
170
|
+
| `-32002` | a bound | `limit`, with `limit` (the name: `maxOps`, `maxArgs`, `maxDepth`, `maxValueBytes`, `maxRequestsPerSecond`, `maxHandles`, `maxCallbacks`, `maxSubscriptions`, `callTimeoutMs`) and `max` |
|
|
171
|
+
| `-32602` | invalid | `bad-request`, `no-such-root` |
|
|
172
|
+
| `-32000` | failed | `bad-path` (reading a member of `null` or `undefined`), `not-a-function`, `threw` (the called code threw), `refused-by-object` (a read-only or frozen assignment), `not-an-event-source`, `cancelled`, `closed` |
|
|
173
|
+
| `-32601`, `-32600`, `-32603`, `-32001` | the rpc layer's own: method not found, invalid request, internal error, cancelled | |
|
|
174
|
+
|
|
175
|
+
`-32002` is also the rpc layer's `stream-limit` (more than 64 requests in flight). The bounds, per attached channel: 32 operations per `resolve`,
|
|
176
|
+
16 arguments, depth 8, 256 KiB per value, 200 keys per `describe`, 50 requests per second, 30 s for a call that returns a promise, and the counts
|
|
177
|
+
of sections 5 to 7.
|
|
178
|
+
|
|
179
|
+
## 9. Keys and the host's switches
|
|
180
|
+
|
|
181
|
+
The caller's key may carry `capabilities="objects:ROOT[:OP]"` (`spec/capabilities.md`): a key that names any `objects:` entry is limited to what they
|
|
182
|
+
name (the operation is `get`, `call` (also `construct`), `set` or `on`; `get` also covers `describe`, `snapshot` and walking), may open the
|
|
183
|
+
channel without `rpc:wsh-objects`, and is told only of the roots it can use. A key with no `objects:` entry is limited by `rpc:wsh-objects` alone.
|
|
184
|
+
The key's capability is asked again for every operation (an expiry holds at call time).
|
|
185
|
+
|
|
186
|
+
## 10. What a peer must do
|
|
187
|
+
|
|
188
|
+
- Open the channel only if the host states `rpc-protocol:wsh-objects`; on a sealed session also `rpc-virtual`.
|
|
189
|
+
- Ask `roots` first, and use only `features` the host stated; refuse locally, with a name, what it did not state, rather than send what a host
|
|
190
|
+
that does not know it would drop (a function it does not know is `null`).
|
|
191
|
+
- Treat every byte of text from the host as untrusted data; never interpret it, never let it reach a terminal unescaped.
|
|
192
|
+
- Bound what it accepts: the channel's message bound applies in both directions, and a subscriber that cannot keep up must drop and count, not stop
|
|
193
|
+
reading (stopping reading stops the host's credit, which is the host's backstop, but also every answer behind it).
|
|
194
|
+
|
|
195
|
+
## 11. What the released peers do
|
|
196
|
+
|
|
197
|
+
- JavaScript (`@johnhenry/wsh` 0.46.0 to 0.51.0): the reference described above. 0.46.0 and 0.47.0 hosts have no `features` in `roots`, no events, handles or
|
|
198
|
+
callbacks; a peer sees an empty `features`. 0.50.0 and earlier hosts do not state `types` or `handle-events`.
|
|
199
|
+
- Rust client (`wsh_client::objects`, `wsh objects`): `rust-v0.18.0` speaks values, events by path and handles. `rust-v0.19.0` also speaks callbacks, events
|
|
200
|
+
through a handle and `types`, each only to a host that states it (`callbacks`, `handle-events`, `types`); against an older host they are refused locally, by name, and
|
|
201
|
+
nothing is sent. Rust released before the client (`rust-v0.17.1` and earlier) has no objects client at all.
|
|
202
|
+
- `callback-returns` (callback return values, JS hosts only): a 0.51.0 client, and the Rust client, never list `callback-returns` in their `roots` request, so a host that
|
|
203
|
+
states it never asks them for an answer: the page gets `undefined` at once and the callback is delivered one-way. A 0.51.0 host ignores the `features` of a `roots` request.
|
|
204
|
+
- Rust `wsh-server`: serves no `wsh-objects` channel and no page host, and refuses a key line with an `objects:` capability (an unknown capability
|
|
205
|
+
fails closed). **This is a decision, not a gap to close.** `wsh-objects` exists to reach a *live object graph* by path (a page's `window`, a worker's `self`, a
|
|
206
|
+
Node object), under allow-lists written over that graph's names; a Rust server has no such graph to expose, and anything it could name (its configuration, its
|
|
207
|
+
sessions, its counters) would be a fixed set of paths written in Rust, which is what a typed rpc channel is for. The Rust server already offers one
|
|
208
|
+
(`wsh-host`, `wsh mcp --channel wsh-host`), with its methods, bounds and `rpc:` capability checked in Rust rather than reconstructed as path rules over a
|
|
209
|
+
dynamic graph. A second, path-addressed way to read the same few fields would add a surface (allow-list syntax, `needs-listing` names, handles, callbacks that
|
|
210
|
+
call back into the server's own tasks) with nothing to protect it that the typed channel does not already have. So the server does not serve `wsh-objects`, and
|
|
211
|
+
a key line that asks for `objects:` capabilities stays refused rather than accepted and ignored. What would change this: a Rust host that embeds a scripting
|
|
212
|
+
runtime or another real object graph (none is planned).
|
package/spec/wsh-v1.yaml
CHANGED
|
@@ -1273,15 +1273,18 @@ messages:
|
|
|
1273
1273
|
description: >
|
|
1274
1274
|
Machine-readable failure reason. Known values:
|
|
1275
1275
|
1=CONNECTION_REFUSED, 2=HOST_UNREACHABLE, 3=DNS_FAILED,
|
|
1276
|
-
4=POLICY_DENIED, 5=GATEWAY_DISABLED, 6=CLOSED, 7=QUEUE_FULL
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1276
|
+
4=POLICY_DENIED, 5=GATEWAY_DISABLED, 6=CLOSED, 7=QUEUE_FULL,
|
|
1277
|
+
8=TIMEOUT. A code is never reused for something else.
|
|
1278
|
+
COMPATIBILITY: Node gateways older than the release that
|
|
1279
|
+
introduced 8 (the @johnhenry/wsh/server gateway and the page and
|
|
1280
|
+
memory hosts) sent 5 for a timeout, and a released peer still
|
|
1281
|
+
does. A reader therefore treats 5 as "the gateway is disabled,
|
|
1282
|
+
or a timeout from an older Node gateway" and says so; it never
|
|
1283
|
+
assumes a 5 is only one of the two. Senders: the Rust wsh-server
|
|
1284
|
+
sends 5 for a disabled gateway and 7 for a full queue; the Node
|
|
1285
|
+
gateway sends 6, 7 and 8. The table is exported as data from
|
|
1286
|
+
@johnhenry/wsh (GATEWAY_FAIL_TABLE) and as vectors in
|
|
1287
|
+
spec/gateway-fail.vectors.json.
|
|
1285
1288
|
message:
|
|
1286
1289
|
type: string
|
|
1287
1290
|
required: true
|
package/src/duplex.mjs
CHANGED
|
@@ -124,7 +124,8 @@ export function createDuplexSocket({ send, close, maxMessageBytes = DUPLEX_DEFAU
|
|
|
124
124
|
if (!bytes) { finish(1003, 'a message that is not bytes'); return; }
|
|
125
125
|
if (bytes.byteLength > maxMessageBytes) { finish(1009, 'a message larger than maxMessageBytes'); return; }
|
|
126
126
|
if (incoming.reduce((n, b) => n + b.byteLength, 0) + bytes.byteLength > maxBufferedBytes) { finish(1013, 'more arrived than the receiver takes'); return; }
|
|
127
|
-
|
|
127
|
+
// A copy that owns its memory: `Buffer.prototype.slice` (what the `ws` package hands over) returns a view of the same pool, whose `.buffer` is not these bytes.
|
|
128
|
+
incoming.push(new Uint8Array(bytes).buffer);
|
|
128
129
|
queueMicrotask(flush); // in order, after `open`, once something listens
|
|
129
130
|
},
|
|
130
131
|
/** The duplex ended on its own (the peer left, the channel closed): the socket closes, `close` is not called back. */
|
package/src/gateway-client.mjs
CHANGED
|
@@ -15,16 +15,20 @@ import {
|
|
|
15
15
|
} from './messages.mjs';
|
|
16
16
|
import { GatewayChannel, GATEWAY_CREDIT_FEATURE } from './gateway-channel.mjs';
|
|
17
17
|
import { UdpFlow, decodeDatagram } from './gateway-datagram.mjs';
|
|
18
|
+
import { GATEWAY_FAIL_FIVE_NOTE } from './gateway-errors.mjs';
|
|
18
19
|
|
|
19
20
|
const ID_BASE = 0x40000000;
|
|
20
21
|
const ID_LIMIT = 0xffffffff;
|
|
21
22
|
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
22
23
|
const CLIENT_GRACE_MS = 5_000;
|
|
23
24
|
|
|
24
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* An Error from a GatewayFail / ListenFail: `.code` is the GatewayFail code (4 = refused by policy, 8 = timed out). A `5` is "the gateway is
|
|
27
|
+
* disabled, or a timeout from an older Node gateway" and the message says both (the host's own words come first).
|
|
28
|
+
*/
|
|
25
29
|
export class GatewayError extends Error {
|
|
26
30
|
constructor(message, code) {
|
|
27
|
-
super(message);
|
|
31
|
+
super(code === 5 ? `${message} (${GATEWAY_FAIL_FIVE_NOTE})` : message);
|
|
28
32
|
this.name = 'GatewayError';
|
|
29
33
|
this.code = code;
|
|
30
34
|
}
|
package/src/gateway-errors.mjs
CHANGED
|
@@ -5,26 +5,33 @@
|
|
|
5
5
|
* decodes them (it keeps a copy by hand: it can import this one instead, the names and numbers are the same).
|
|
6
6
|
*
|
|
7
7
|
* The table is a contract: a code is never reused for something else and a number is never renumbered. A new code is added at the end.
|
|
8
|
+
* One exception was made, once (#223): the Node gateway used to send `5` for a timeout while the spec and the Rust `wsh-server` use `5` for
|
|
9
|
+
* "gateway disabled". `5` is now `GATEWAY_DISABLED` everywhere and `TIMEOUT` is `8`. A reader keeps accepting a `5` as "disabled, or a
|
|
10
|
+
* timeout from an older Node gateway", because a released Node peer still sends it.
|
|
8
11
|
*/
|
|
9
12
|
|
|
10
13
|
/** Wire codes of `GatewayFail`, by name. */
|
|
11
14
|
export const GATEWAY_FAIL = Object.freeze({
|
|
12
|
-
CONNECTION_REFUSED: 1, HOST_UNREACHABLE: 2, DNS_FAILED: 3, POLICY_DENIED: 4,
|
|
15
|
+
CONNECTION_REFUSED: 1, HOST_UNREACHABLE: 2, DNS_FAILED: 3, POLICY_DENIED: 4, GATEWAY_DISABLED: 5, CLOSED: 6, QUEUE_FULL: 7, TIMEOUT: 8,
|
|
13
16
|
});
|
|
14
17
|
|
|
18
|
+
/** What a reader says about a `5`: it is what the Rust server sends when its gateway is off, and what a Node gateway older than the one that sends `TIMEOUT` (8) sent for a timeout. */
|
|
19
|
+
export const GATEWAY_FAIL_FIVE_NOTE = 'code 5: the gateway is disabled, or a timeout from an older Node gateway';
|
|
20
|
+
|
|
15
21
|
/**
|
|
16
|
-
* The same codes with their meaning. `alsoSentAs` lists a second name a different implementation gives the same number:
|
|
17
|
-
*
|
|
18
|
-
*
|
|
22
|
+
* The same codes with their meaning. `alsoSentAs` lists a second name a different (or older) implementation gives the same number: a Node
|
|
23
|
+
* gateway older than the release that introduced `TIMEOUT` = 8 sent `5` for a timeout. A client that reads `5` therefore says "disabled,
|
|
24
|
+
* or a timeout from an older Node gateway" rather than only one of the two.
|
|
19
25
|
*/
|
|
20
26
|
export const GATEWAY_FAIL_TABLE = Object.freeze([
|
|
21
27
|
Object.freeze({ name: 'CONNECTION_REFUSED', code: 1, meaning: 'The destination actively refused the connection (nothing listens there).' }),
|
|
22
28
|
Object.freeze({ name: 'HOST_UNREACHABLE', code: 2, meaning: 'The destination could not be reached on the network (no route, connect failed, socket error).' }),
|
|
23
29
|
Object.freeze({ name: 'DNS_FAILED', code: 3, meaning: 'The name did not resolve, or is not a valid name.' }),
|
|
24
30
|
Object.freeze({ name: 'POLICY_DENIED', code: 4, meaning: 'The host refused by policy: the key lacks the capability, the address is not allowed, the endpoint or target does not exist or is not for this caller, or the request was malformed or reused an id.' }),
|
|
25
|
-
Object.freeze({ name: '
|
|
31
|
+
Object.freeze({ name: 'GATEWAY_DISABLED', code: 5, alsoSentAs: Object.freeze(['TIMEOUT']), meaning: 'The host has its gateway disabled (the Rust wsh-server sends this). A Node gateway older than the one that sends TIMEOUT (8) sent a 5 for a timeout, so a 5 can also be a timeout from an older Node gateway.' }),
|
|
26
32
|
Object.freeze({ name: 'CLOSED', code: 6, meaning: 'The gateway connection was closed before the operation finished.' }),
|
|
27
33
|
Object.freeze({ name: 'QUEUE_FULL', code: 7, meaning: 'The host holds too many gateway connections or too much unsent data, and takes no more for now.' }),
|
|
34
|
+
Object.freeze({ name: 'TIMEOUT', code: 8, meaning: 'The host did not finish in time (a connect or a name lookup timed out).' }),
|
|
28
35
|
]);
|
|
29
36
|
|
|
30
37
|
/**
|
package/src/index.d.ts
CHANGED
|
@@ -2248,14 +2248,21 @@ export interface WshUdpFlow extends WshGatewayDuplex {
|
|
|
2248
2248
|
/** Feature string for UDP over WebTransport datagrams. */
|
|
2249
2249
|
export const GATEWAY_DATAGRAM_FEATURE: 'gateway-datagram';
|
|
2250
2250
|
|
|
2251
|
-
/**
|
|
2251
|
+
/**
|
|
2252
|
+
* A GatewayFail / ListenFail as an Error: `code` 4 = refused by the host's policy, 7 = too many connections, 1 = refused by the target,
|
|
2253
|
+
* 8 = timed out. A `5` is "the gateway is disabled, or a timeout from an older Node gateway", and the message says so.
|
|
2254
|
+
*/
|
|
2252
2255
|
export class GatewayError extends Error { code: number }
|
|
2253
2256
|
|
|
2254
|
-
/**
|
|
2257
|
+
/**
|
|
2258
|
+
* The codes of `GatewayFail` by name (the one table the Node gateway, the page host and netway share). Frozen. `5` is `GATEWAY_DISABLED` (the
|
|
2259
|
+
* spec, the Rust server); `TIMEOUT` is `8`. A Node gateway older than the one that sends `8` sent `5` for a timeout, so keep reading a `5` as
|
|
2260
|
+
* "disabled, or a timeout from an older Node gateway".
|
|
2261
|
+
*/
|
|
2255
2262
|
export const GATEWAY_FAIL: Readonly<{
|
|
2256
|
-
CONNECTION_REFUSED: 1; HOST_UNREACHABLE: 2; DNS_FAILED: 3; POLICY_DENIED: 4;
|
|
2263
|
+
CONNECTION_REFUSED: 1; HOST_UNREACHABLE: 2; DNS_FAILED: 3; POLICY_DENIED: 4; GATEWAY_DISABLED: 5; CLOSED: 6; QUEUE_FULL: 7; TIMEOUT: 8;
|
|
2257
2264
|
}>;
|
|
2258
|
-
/** The same codes with their meaning; `alsoSentAs` is a second name another implementation gives the number (
|
|
2265
|
+
/** The same codes with their meaning; `alsoSentAs` is a second name another (or an older) implementation gives the number (`5` is also what an older Node gateway sent for a timeout). */
|
|
2259
2266
|
export const GATEWAY_FAIL_TABLE: ReadonlyArray<Readonly<{ name: keyof typeof GATEWAY_FAIL; code: number; meaning: string; alsoSentAs?: readonly string[] }>>;
|
|
2260
2267
|
/** Codes a client gives its own `GatewayError` when it never asked the host (not on the wire): `UNSUPPORTED` is `0`, a feature the host did not advertise. */
|
|
2261
2268
|
export const GATEWAY_FAIL_LOCAL: Readonly<{ UNSUPPORTED: 0 }>;
|