grpc-dc 0.0.0-stage → 0.31.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 +33 -2
- package/index.d.ts +193 -0
- package/index.js +51 -0
- package/package.json +41 -4
- package/src/channel.js +228 -0
- package/src/codec.js +220 -0
- package/src/dynamic.js +426 -0
- package/src/endpoint.js +269 -0
- package/src/loopback.js +60 -0
- package/src/server.js +126 -0
- package/src/wasm_bridge.js +241 -0
- package/src/webrtc_direct.js +119 -0
- package/wire_gen.js +50 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// @generated by grpc-dc codegen. DO NOT EDIT.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// WebRTC-Direct (libp2p-style) client->server helpers for Node:
|
|
4
|
+
// no ICE, no SDP exchange. The listener is identified by the
|
|
5
|
+
// certhash in its multiaddr; dialers pin the listener
|
|
6
|
+
// certificate to it. The TURN variant is the symmetric-NAT path
|
|
7
|
+
// (the whole DTLS session rides a TURN allocation). This is the
|
|
8
|
+
// generated-runtime adapter over the hand-written stack in
|
|
9
|
+
// webrtc/js/direct.js -- the same shape as the Rust
|
|
10
|
+
// `grpc_dc::webrtc_direct` module and the Go adapter
|
|
11
|
+
// `go/webrtc_direct.go`. Browsers cannot bind UDP: in the
|
|
12
|
+
// browser, use createPeer with a native RTCPeerConnection's
|
|
13
|
+
// relay-only datachannel instead.
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
DirectListener,
|
|
17
|
+
directDialMultiaddr,
|
|
18
|
+
directDialTurnMultiaddr,
|
|
19
|
+
} from "../../webrtc/js/direct.js";
|
|
20
|
+
import { Endpoint } from "./endpoint.js";
|
|
21
|
+
|
|
22
|
+
export { DirectListener };
|
|
23
|
+
|
|
24
|
+
/** Bind a WebRTC-Direct listener (multiaddr() is its identity). */
|
|
25
|
+
export async function directListener(options = {}) {
|
|
26
|
+
return DirectListener.bind(options);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function waitForChannel(endpoint, timeoutMs = 30000) {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const timer = setTimeout(
|
|
32
|
+
() => reject(new Error("webrtc-direct: no channel opened")),
|
|
33
|
+
timeoutMs,
|
|
34
|
+
);
|
|
35
|
+
endpoint.onchannel = (channel) => {
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
resolve(channel);
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Accept one direct connection and its "grpc" channel; serve
|
|
44
|
+
* the returned Endpoint with `new Server().serve(endpoint)` or
|
|
45
|
+
* register services on it. The server side answers with even
|
|
46
|
+
* stream ids.
|
|
47
|
+
*/
|
|
48
|
+
export async function acceptDirectEndpoint(listener) {
|
|
49
|
+
const endpoint = await listener.accept();
|
|
50
|
+
const channel = await waitForChannel(endpoint);
|
|
51
|
+
const grpcEndpoint = new Endpoint(channel, { parity: "even" });
|
|
52
|
+
// The underlying stack endpoint, for close()/transport
|
|
53
|
+
// control; the generated Endpoint wraps only the channel.
|
|
54
|
+
grpcEndpoint.directEndpoint = endpoint;
|
|
55
|
+
return grpcEndpoint;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Dial a Direct listener by multiaddr and open the "grpc"
|
|
60
|
+
* channel; build a generated client with
|
|
61
|
+
* `new Channel(await directClientEndpoint(ma))`. The dialer
|
|
62
|
+
* allocates odd stream ids.
|
|
63
|
+
*/
|
|
64
|
+
export async function directClientEndpoint(multiaddr, options = {}) {
|
|
65
|
+
const endpoint = await directDialMultiaddr(multiaddr, options);
|
|
66
|
+
const channel = endpoint.openChannel({ label: "grpc" });
|
|
67
|
+
const grpcEndpoint = new Endpoint(channel, { parity: "odd" });
|
|
68
|
+
grpcEndpoint.directEndpoint = endpoint;
|
|
69
|
+
return grpcEndpoint;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Dial a Direct listener by multiaddr and return the raw stack
|
|
74
|
+
* endpoint (no channel opened yet), plus the TURN variant. Use
|
|
75
|
+
* these to open extra application channels next to the "grpc"
|
|
76
|
+
* one -- including lossy or unordered ones, via
|
|
77
|
+
* `endpoint.openChannel({ label, ordered: false,
|
|
78
|
+
* reliability: { kind: "retransmits", count: n } })` (or
|
|
79
|
+
* `{ kind: "lifetime", ms }`). The "grpc" channel itself must
|
|
80
|
+
* stay reliable and ordered: grpc-dc framing assumes in-order
|
|
81
|
+
* reliable delivery, so keep directClientEndpoint's defaults
|
|
82
|
+
* for gRPC traffic and use lossy channels only for application
|
|
83
|
+
* data that tolerates drops and reordering. Incoming
|
|
84
|
+
* application channels surface as `endpoint.onchannel`.
|
|
85
|
+
*/
|
|
86
|
+
export async function directClientStackEndpoint(multiaddr, options = {}) {
|
|
87
|
+
return directDialMultiaddr(multiaddr, options);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function directClientTurnStackEndpoint(multiaddr, turn, options = {}) {
|
|
91
|
+
return directDialTurnMultiaddr(multiaddr, { ...options, turn });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Open an extra application channel on a stack endpoint from
|
|
96
|
+
* directClientStackEndpoint / acceptDirectEndpoint's
|
|
97
|
+
* directEndpoint. `params` is the stack ChannelParams shape:
|
|
98
|
+
* { label, ordered?, reliability? } -- see
|
|
99
|
+
* directClientStackEndpoint for lossy/unordered options.
|
|
100
|
+
*/
|
|
101
|
+
export function openDirectChannel(stackEndpoint, params = {}) {
|
|
102
|
+
return stackEndpoint.openChannel(params);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Dial by multiaddr through a TURN allocation (symmetric-NAT
|
|
107
|
+
* path). `turn` is {address, username, password, transport?,
|
|
108
|
+
* insecureSkipVerify?, requestIpv6Relay?}.
|
|
109
|
+
*/
|
|
110
|
+
export async function directClientTurnEndpoint(multiaddr, turn, options = {}) {
|
|
111
|
+
const endpoint = await directDialTurnMultiaddr(multiaddr, {
|
|
112
|
+
...options,
|
|
113
|
+
turn,
|
|
114
|
+
});
|
|
115
|
+
const channel = endpoint.openChannel({ label: "grpc" });
|
|
116
|
+
const grpcEndpoint = new Endpoint(channel, { parity: "odd" });
|
|
117
|
+
grpcEndpoint.directEndpoint = endpoint;
|
|
118
|
+
return grpcEndpoint;
|
|
119
|
+
}
|
package/wire_gen.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// @generated by grpc-dc codegen. DO NOT EDIT.
|
|
3
|
+
|
|
4
|
+
export const FrameType = {
|
|
5
|
+
HEADERS: 1, // call headers, JSON {path, metadata}
|
|
6
|
+
DATA: 2, // one serialized protobuf message
|
|
7
|
+
TRAILERS: 3, // terminal status, JSON {code, message}
|
|
8
|
+
HALF_CLOSE: 4, // client finished sending
|
|
9
|
+
RST: 5, // abort stream, JSON {code, message}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const StatusCode = {
|
|
13
|
+
OK: 0,
|
|
14
|
+
CANCELLED: 1,
|
|
15
|
+
UNKNOWN: 2,
|
|
16
|
+
INVALID_ARGUMENT: 3,
|
|
17
|
+
DEADLINE_EXCEEDED: 4,
|
|
18
|
+
NOT_FOUND: 5,
|
|
19
|
+
ALREADY_EXISTS: 6,
|
|
20
|
+
PERMISSION_DENIED: 7,
|
|
21
|
+
RESOURCE_EXHAUSTED: 8,
|
|
22
|
+
FAILED_PRECONDITION: 9,
|
|
23
|
+
ABORTED: 10,
|
|
24
|
+
OUT_OF_RANGE: 11,
|
|
25
|
+
UNIMPLEMENTED: 12,
|
|
26
|
+
INTERNAL: 13,
|
|
27
|
+
UNAVAILABLE: 14,
|
|
28
|
+
DATA_LOSS: 15,
|
|
29
|
+
UNAUTHENTICATED: 16,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const StatusName = {
|
|
33
|
+
0: "OK",
|
|
34
|
+
1: "CANCELLED",
|
|
35
|
+
2: "UNKNOWN",
|
|
36
|
+
3: "INVALID-ARGUMENT",
|
|
37
|
+
4: "DEADLINE-EXCEEDED",
|
|
38
|
+
5: "NOT-FOUND",
|
|
39
|
+
6: "ALREADY-EXISTS",
|
|
40
|
+
7: "PERMISSION-DENIED",
|
|
41
|
+
8: "RESOURCE-EXHAUSTED",
|
|
42
|
+
9: "FAILED-PRECONDITION",
|
|
43
|
+
10: "ABORTED",
|
|
44
|
+
11: "OUT-OF-RANGE",
|
|
45
|
+
12: "UNIMPLEMENTED",
|
|
46
|
+
13: "INTERNAL",
|
|
47
|
+
14: "UNAVAILABLE",
|
|
48
|
+
15: "DATA-LOSS",
|
|
49
|
+
16: "UNAUTHENTICATED",
|
|
50
|
+
};
|