@lesomnus/grpc-dgram 0.0.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/LICENSE +202 -0
- package/README.md +245 -0
- package/dist/conn-DOmx4nbt.mjs +849 -0
- package/dist/conn-DTWG9vIx.d.mts +331 -0
- package/dist/desc-BMF2FBqk.d.mts +72 -0
- package/dist/index.d.mts +27 -0
- package/dist/index.mjs +1079 -0
- package/dist/protocol-K4Zy8MuQ.mjs +156 -0
- package/dist/server-xIr1mwqq.d.mts +112 -0
- package/dist/status-DZwMDWIn.mjs +63 -0
- package/dist/transport/connect.d.mts +6 -0
- package/dist/transport/connect.mjs +122 -0
- package/dist/transport/node-udp.d.mts +42 -0
- package/dist/transport/node-udp.mjs +144 -0
- package/dist/transport/port.d.mts +52 -0
- package/dist/transport/port.mjs +240 -0
- package/dist/transport/protobuf-es.d.mts +19 -0
- package/dist/transport/protobuf-es.mjs +27 -0
- package/dist/transport/webrtc.d.mts +56 -0
- package/dist/transport/webrtc.mjs +240 -0
- package/dist/transport/websocket.d.mts +59 -0
- package/dist/transport/websocket.mjs +324 -0
- package/dist/wasm/worker.d.mts +8 -0
- package/dist/wasm/worker.mjs +73 -0
- package/dist/wasm.d.mts +36 -0
- package/dist/wasm.mjs +156 -0
- package/dist/wire-BR8KiyRg.mjs +876 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 lesomnus
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# @lesomnus/grpc-dgram
|
|
2
|
+
|
|
3
|
+
TypeScript port of [gRPC-dgram](../): gRPC-style RPC over unreliable datagram
|
|
4
|
+
channels, implementing the **dRPC wire protocol v1.1** (`../docs/PROTOCOL.md`).
|
|
5
|
+
Wire-compatible with the Go implementation — the §5 golden byte vectors are
|
|
6
|
+
shared between the two test suites — so a TS client interoperates with a Go
|
|
7
|
+
server and vice versa.
|
|
8
|
+
|
|
9
|
+
- **Zero runtime dependencies.** The three wire messages (`Frame`, `Envelop`,
|
|
10
|
+
`Metadata`) are hand-encoded; user payloads go through pluggable
|
|
11
|
+
per-method marshallers (protobuf-es, JSON, anything that produces bytes).
|
|
12
|
+
A **protobuf-es binding** (`@lesomnus/grpc-dgram/transport/protobuf-es`, optional peer
|
|
13
|
+
dep) derives the whole method descriptor — path, streaming kind, codec —
|
|
14
|
+
from a generated `protoc-gen-es` service, so RPC types are never re-declared
|
|
15
|
+
by hand. The core itself stays dependency-free.
|
|
16
|
+
- **Both endpoints.** `Conn` (client) and `Server`, with the full unreliable-
|
|
17
|
+
mode machinery: seq windows and dedup, epoch/`peer_epoch` incarnation
|
|
18
|
+
isolation, control-frame retransmission, tombstones + aged watermark,
|
|
19
|
+
PING/probe liveness, and the §15 resource caps. On a reliable transport all
|
|
20
|
+
timers are off, sequencing is strict fail-loud (§10.6), and **per-stream
|
|
21
|
+
flow control** (§4.2.1) keeps one slow consumer from stalling the other
|
|
22
|
+
calls on the channel — the browser case where a blocked receive path would
|
|
23
|
+
otherwise wedge the whole event loop.
|
|
24
|
+
- **v1.1 surface.** Binary metadata (`-bin` keys carry arbitrary octets;
|
|
25
|
+
base64 at the TS API, raw bytes on the wire), rich status details on the
|
|
26
|
+
terminal frame, per-message compression (`gzip` via the platform's
|
|
27
|
+
`CompressionStream`, never expanding a payload), per-call recv/send size
|
|
28
|
+
caps, and the shape/modifier flag split (§7.1) — an unimplemented flag bit
|
|
29
|
+
fails the call instead of silently corrupting or gapping it.
|
|
30
|
+
- **WebSocket adapter** (`@lesomnus/grpc-dgram/transport/websocket`), the TS
|
|
31
|
+
twin of the Go `transport/gorilla` adapter: reliable mode, one Envelop per
|
|
32
|
+
message, and the §4.5 teardown duty carried by `onclose`/`onerror` plus a
|
|
33
|
+
keepalive — browser-safe (WhatWG `WebSocket`, `binaryType='arraybuffer'`).
|
|
34
|
+
`dialWebSocket(url)` is a `Conn` in one line; `new WebSocketTransport(ws)` is
|
|
35
|
+
the path for a socket you brought yourself.
|
|
36
|
+
- **Message-port adapter** (`@lesomnus/grpc-dgram/transport/port`), the TS twin
|
|
37
|
+
of the Go `transport/jsport` adapter: `PortTransport`/`PortGateway` over
|
|
38
|
+
anything with `postMessage` and a `message` event — a `MessagePort`, a
|
|
39
|
+
`Worker`, a worker's own `self` — plus `dialWorker(worker)`, which transfers
|
|
40
|
+
a port to a worker of yours and hands back a `Conn`, one independent peer per
|
|
41
|
+
call. Always reliable, since a port in one process loses nothing, and since
|
|
42
|
+
nothing dies that a port can report, teardown is spoken: an empty message is the
|
|
43
|
+
goodbye, and `close(cause)` carries what only the host knows (a wasm instance
|
|
44
|
+
that exited, a terminated worker).
|
|
45
|
+
- **A Go server in the browser** (`@lesomnus/grpc-dgram/wasm`): `open()` starts
|
|
46
|
+
a **Go `drpc.Server` compiled to `js/wasm`** — in a Worker this package
|
|
47
|
+
ships, so a computing handler does not freeze the page — and hands back
|
|
48
|
+
connections to it. It is the JS half of `jsport.Gateway.Serve`: the readiness
|
|
49
|
+
publish, the instantiation, one channel per `dial()`, and the §4.5 teardown a
|
|
50
|
+
dying instance cannot perform for itself. `../examples/browser-wasm` is it
|
|
51
|
+
running, and `test/wasm.test.ts` drives a real Go server through it.
|
|
52
|
+
- **WebRTC DataChannel adapter** (`@lesomnus/grpc-dgram/transport/webrtc`), the TS twin
|
|
53
|
+
of the Go `transport/pion` adapter: the protocol mode is derived from the
|
|
54
|
+
channel's own configuration — an ordered channel with no retransmit or
|
|
55
|
+
lifetime cap runs reliable, anything else unreliable. Client `Transport`
|
|
56
|
+
and a mixed-mode server `Gateway` (a reliable control channel and
|
|
57
|
+
unreliable telemetry channels on one peer connection serve side by side,
|
|
58
|
+
each peer in its channel's mode).
|
|
59
|
+
- **Node UDP adapter** (`@lesomnus/grpc-dgram/transport/node-udp`, Node only), the TS
|
|
60
|
+
twin of the Go `transport/udp` adapter — `UdpTransport`/`UdpGateway`, with
|
|
61
|
+
`dialUdp` (a connected socket and a `Conn` over it, in one line) and
|
|
62
|
+
`listenUdp`. A TS client over this adapter interoperates with a Go
|
|
63
|
+
`drpc.Server` on the wire; this is exercised by a cross-language conformance
|
|
64
|
+
test (`test/conformance.test.ts`) that drives a real Go server.
|
|
65
|
+
- **Connect-ES transport** (`@lesomnus/grpc-dgram/transport/connect`, optional peer dep
|
|
66
|
+
on `@connectrpc/connect`): use the standard `createClient(Service, transport)`
|
|
67
|
+
ergonomics while the traffic runs over drpc. `createDrpcTransport(conn)`
|
|
68
|
+
turns a dRPC `Conn` into a Connect `Transport` — the conformance suite drives
|
|
69
|
+
a **real Go `drpc.Server` through a Connect client** end to end.
|
|
70
|
+
|
|
71
|
+
## Usage
|
|
72
|
+
|
|
73
|
+
**Getting a connection.** Four cases, and the verb says which:
|
|
74
|
+
`new Conn(new XTransport(ch), opts)` when you already hold the channel;
|
|
75
|
+
`dialUdp` / `dialWebSocket` / `dialWorker` when the library should make it —
|
|
76
|
+
each hands back a `Conn`, and takes one options bag, since `ConnOptions` and an
|
|
77
|
+
adapter's own options share no key; `open(app)` when there is no peer yet,
|
|
78
|
+
which returns a `Sock` whose `dial()` is the connection; and, on the serving
|
|
79
|
+
side, a `Gateway` — `listenUdp` where the library opens the socket, then
|
|
80
|
+
`serve` when the gateway owns the whole endpoint, or `bind` + `servePeer` per
|
|
81
|
+
channel handed in from outside.
|
|
82
|
+
[Transports](../docs/transports.md#the-four-ways-in) spells out the whole rule.
|
|
83
|
+
|
|
84
|
+
**With protobuf-es (recommended).** Point the binding at a generated service —
|
|
85
|
+
paths, streaming kinds, and codecs are all derived from the `.proto`, so
|
|
86
|
+
there is nothing to keep in sync and a TS client interoperates with a Go
|
|
87
|
+
server addressing the same methods:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { fromService } from '@lesomnus/grpc-dgram/transport/protobuf-es'
|
|
91
|
+
import { create } from '@bufbuild/protobuf'
|
|
92
|
+
import { EchoService, EchoRequestSchema } from './echo_pb' // protoc-gen-es output
|
|
93
|
+
|
|
94
|
+
const Echo = fromService(EchoService) // { once, many, count, live }, fully typed
|
|
95
|
+
|
|
96
|
+
await conn.invoke(Echo.once, create(EchoRequestSchema, { text: 'hi' }))
|
|
97
|
+
server.register(Echo.once, (req) => create(EchoResponseSchema, { text: req.text }))
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**With a Connect client.** If you already use Connect-ES, keep its client
|
|
101
|
+
ergonomics and swap only the transport:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { createClient } from '@connectrpc/connect'
|
|
105
|
+
import { createDrpcTransport } from '@lesomnus/grpc-dgram/transport/connect'
|
|
106
|
+
import { EchoService } from './echo_pb'
|
|
107
|
+
|
|
108
|
+
const client = createClient(EchoService, createDrpcTransport(conn))
|
|
109
|
+
await client.once({ message: 'hi' }) // unary
|
|
110
|
+
for await (const m of client.many({ message: 'x' })) // server streaming
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**Without codegen.** Describe methods explicitly with any byte serializer
|
|
114
|
+
(the core is codec-agnostic — this is what the test suite uses):
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { unaryMethod, bidiMethod, type PayloadCodec } from '@lesomnus/grpc-dgram'
|
|
118
|
+
|
|
119
|
+
const json = <T>(): PayloadCodec<T> => ({
|
|
120
|
+
marshal: (v) => new TextEncoder().encode(JSON.stringify(v)),
|
|
121
|
+
unmarshal: (b) => JSON.parse(new TextDecoder().decode(b)),
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
const Once = unaryMethod<Req, Res>('/echo.Echo/Once', { request: json(), response: json() })
|
|
125
|
+
const Live = bidiMethod<Req, Res>('/echo.Echo/Live', { request: json(), response: json() })
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**Against a Go server compiled to wasm.** The page starts it and calls it; the
|
|
129
|
+
Worker, the `wasm_exec.js` load, the readiness handshake and the teardown are
|
|
130
|
+
`open()`'s (`src/wasm`, `../transport/jsport` on the Go side):
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
import { open } from '@lesomnus/grpc-dgram/wasm'
|
|
134
|
+
|
|
135
|
+
const sock = await open('/app.wasm')
|
|
136
|
+
const conn = sock.dial() // one connection; call it again for another peer (§6.4)
|
|
137
|
+
|
|
138
|
+
await conn.invoke(Once, { text: 'hi' })
|
|
139
|
+
sock.close() // fails every live call, then terminates the worker
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Client over an `RTCDataChannel`:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import { Conn } from '@lesomnus/grpc-dgram'
|
|
146
|
+
import { DataChannelTransport } from '@lesomnus/grpc-dgram/transport/webrtc'
|
|
147
|
+
|
|
148
|
+
const dc = pc.createDataChannel('rpc') // ordered, no caps → reliable mode, no timers
|
|
149
|
+
const conn = new Conn(new DataChannelTransport(dc)) // pump attaches itself
|
|
150
|
+
|
|
151
|
+
const res = await conn.invoke(Once, { text: 'hi' })
|
|
152
|
+
|
|
153
|
+
const stream = conn.newStream(Live, {})
|
|
154
|
+
await stream.send({ text: 'x' })
|
|
155
|
+
for await (const msg of stream) console.log(msg)
|
|
156
|
+
|
|
157
|
+
conn.close() // one close tears everything down, channel included
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Server behind a gateway (any number of peers, mixed reliability):
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
import { Server } from '@lesomnus/grpc-dgram'
|
|
164
|
+
import { DataChannelGateway } from '@lesomnus/grpc-dgram/transport/webrtc'
|
|
165
|
+
|
|
166
|
+
const gw = new DataChannelGateway()
|
|
167
|
+
const server = new Server(gw)
|
|
168
|
+
server.register(Once, (req) => ({ text: `echo:${req.text}` }))
|
|
169
|
+
server.register(Live, async (stream) => {
|
|
170
|
+
for await (const msg of stream) await stream.send(msg)
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
pc.ondatachannel = ({ channel }) => {
|
|
174
|
+
gw.bind(channel) // synchronously, so no early message is lost
|
|
175
|
+
void gw.servePeer(server, channel) // §4.5 teardown on every exit
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Handlers are plain functions per RPC type:
|
|
180
|
+
|
|
181
|
+
| Type | Signature |
|
|
182
|
+
|---|---|
|
|
183
|
+
| unary | `(req, ctx) => Res \| Promise<Res>` |
|
|
184
|
+
| server-streaming | `(req, stream, ctx) => Promise<void>` |
|
|
185
|
+
| client-streaming | `(stream, ctx) => Res \| Promise<Res>` — the return value is the response |
|
|
186
|
+
| bidi | `(stream, ctx) => Promise<void>` |
|
|
187
|
+
|
|
188
|
+
`ctx.signal` is an `AbortSignal` aborted when the call ends for any reason
|
|
189
|
+
(client abort, RESET, deadline, liveness expiry, `stop()`), with the
|
|
190
|
+
`StatusError` cause as its reason; `ctx.setHeader` / `ctx.sendHeader` /
|
|
191
|
+
`ctx.setTrailer` follow §11. Handler failures are `StatusError`s (anything
|
|
192
|
+
else maps to `UNKNOWN`).
|
|
193
|
+
|
|
194
|
+
## Translation notes (Go ↔ TS)
|
|
195
|
+
|
|
196
|
+
| Go | TS |
|
|
197
|
+
|---|---|
|
|
198
|
+
| `context.Context` cancellation/deadline | `CallOptions.signal` (`AbortSignal`) + `timeoutMs`; handler `ctx.signal` |
|
|
199
|
+
| `*status.Status` errors | `StatusError { code, desc }` |
|
|
200
|
+
| `metadata.MD` | `Metadata = Record<string, string[]>`; a `-bin` key holds base64 (raw octets on the wire) |
|
|
201
|
+
| `grpc.ClientConnInterface` / generated stubs | `conn.invoke(desc, req)` / `conn.newStream(desc)` |
|
|
202
|
+
| `RegisterService` + codegen | `server.register(desc, handler)` per method |
|
|
203
|
+
| `TransportInfo` / `ConnAttacher` discovery | same, structural (`reliable()`, `attachConn()`) |
|
|
204
|
+
| `drpc.ErrMessageTooLarge` | `MessageTooLargeError` (adapters throw it or set it as `cause`) |
|
|
205
|
+
| `NewPeerContext` / `NewReliableContext` | `FrameContext { peer, reliable, signal }` argument to `handle` |
|
|
206
|
+
| mutexes + atomics | none needed: state transitions are synchronous between `await` points |
|
|
207
|
+
|
|
208
|
+
Deliberately not ported (yet): client/server interceptors, the observability
|
|
209
|
+
surface (Go's `stats.Handler` + `ProtocolStats`), and `Envelop` batching
|
|
210
|
+
(`Coalescer` — deferred to M8 in Go; every envelop carries one frame, as the
|
|
211
|
+
shipped Go adapters do).
|
|
212
|
+
|
|
213
|
+
Receive-path note for browsers: an `RTCDataChannel` cannot pause delivery, so
|
|
214
|
+
adapter-level buffering is unavoidable — but since v1.1 the *protocol* paces
|
|
215
|
+
the sender instead of the receiver (§4.2.1 flow control), so a slow consumer
|
|
216
|
+
no longer needs the receive path to block at all, and never stalls the other
|
|
217
|
+
calls sharing the channel.
|
|
218
|
+
|
|
219
|
+
## Tests
|
|
220
|
+
|
|
221
|
+
`pnpm test` — 353 tests mirroring the Go suites: the §5 golden wire vectors
|
|
222
|
+
byte-for-byte (including the v1.1 vectors generated from the Go
|
|
223
|
+
implementation), e2e for all four RPC types, the §10 timeout system under
|
|
224
|
+
deterministic fake-timer loss (blackhole, lost terminals/acks/half-closes,
|
|
225
|
+
probes, liveness), the §6.5 restart walkthroughs, §15 caps and §4.2 drop
|
|
226
|
+
policies, §4.2.1 flow control (advertisement, parking, grants, `T_stall`,
|
|
227
|
+
overrun), compression, size caps and binary metadata, each adapter
|
|
228
|
+
(WebRTC/WebSocket/Port/UDP/protobuf-es/Connect) next to its source, `open()`
|
|
229
|
+
and the worker it ships against a fake `Go` (`src/wasm/`), and two
|
|
230
|
+
cross-language conformance tests driving a real Go `drpc.Server`: one over
|
|
231
|
+
loopback UDP, one a `js/wasm` build of the server loaded into the test process
|
|
232
|
+
and talked to across a `MessageChannel` — the second being where reliable mode
|
|
233
|
+
is genuinely reliable rather than asserted, so the §4.2.1 windows are exercised
|
|
234
|
+
between the two implementations.
|
|
235
|
+
|
|
236
|
+
**Layout.** Unit and per-adapter tests are **co-located** next to their source
|
|
237
|
+
(`src/wire.test.ts`, `src/transport/webrtc/index.test.ts`, …); cross-cutting
|
|
238
|
+
integration tests (e2e, timeout, restart, limits, conformance) stay in
|
|
239
|
+
`test/`. Shared test infrastructure — the echo fixtures and the generated
|
|
240
|
+
`protoc-gen-es` code — lives in `src/testing/` (not an entry, not published).
|
|
241
|
+
None of it reaches `dist/` (tsdown builds only the `exports` entries).
|
|
242
|
+
|
|
243
|
+
## Licence
|
|
244
|
+
|
|
245
|
+
[Apache 2.0](https://github.com/lesomnus/grpc-dgram/blob/main/LICENSE).
|