grpc-dc 0.31.0 → 0.32.6

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.
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // c protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "c";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // cpp protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "cpp";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // csharp protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "csharp";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // dart protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "dart";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // go protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "go";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // java protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "java";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // js protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "js";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // kotlin-native protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "kotlin-native";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // kotlin protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "kotlin";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // lua protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "lua";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // php protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "php";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // python protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "python";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // ruby protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "ruby";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // rust protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "rust";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // scala protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "scala";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // swift protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "swift";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // wasm-cpp protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "wasm-cpp";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // wasm-go protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "wasm-go";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // wasm-kotlin protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "wasm-kotlin";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // wasm-scala protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "wasm-scala";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // wasm-swift protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "wasm-swift";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // wasm-zig protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "wasm-zig";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // zig protoc plugin (Node, dependency-free) — generated by
3
+ // grpc-dc release generator. npm bin entry; pins the language
4
+ // and runs the published plugin runtime beside it.
5
+ process.env.GRPC_DC_LANG = "zig";
6
+ await import("./protoc-gen-grpc-dc.mjs");
@@ -0,0 +1,151 @@
1
+ #!/usr/bin/env node
2
+ // protoc plugin for grpc-over-datachannels — Node edition.
3
+ //
4
+ // Speaks the protoc plugin protocol on stdin/stdout and emits
5
+ // exactly what the private Racket plugins emit, by driving the
6
+ // same generated-JS emitters the browser app uses (./gen/
7
+ // generators.js; byte-identical output is parity-tested against
8
+ // the Racket plugins). No Racket required, no dependencies.
9
+ //
10
+ // Install under one name per language on PATH, e.g.:
11
+ // ln -s "$PWD/protoc-gen-grpc-dc.mjs" /usr/local/bin/protoc-gen-grpc-dc-rust
12
+ // protoc --plugin=protoc-gen-grpc-dc-rust=protoc-gen-grpc-dc-rust \
13
+ // --grpc-dc-rust_out=out -I . echo.proto
14
+ // The language comes from the binary name (protoc-gen-grpc-dc-
15
+ // <lang>); override with GRPC_DC_LANG or `--lang <lang>`.
16
+ // Languages: see GENERATOR_LANGUAGES in ./gen/generators.js.
17
+
18
+ import { readFileSync } from "node:fs";
19
+ import { basename } from "node:path";
20
+ import { generateFiles } from "./gen/generators.js";
21
+
22
+ // ---------------------------------------------------------------------------
23
+ // Minimal protobuf envelope codec (CodeGeneratorRequest /
24
+ // CodeGeneratorResponse). FileDescriptorProto entries pass
25
+ // through as raw bytes, so no descriptor schema is needed here.
26
+ // ---------------------------------------------------------------------------
27
+
28
+ function readVarint(buf, pos) {
29
+ let result = 0n;
30
+ let shift = 0n;
31
+ for (;;) {
32
+ const b = buf[pos++];
33
+ result |= BigInt(b & 0x7f) << shift;
34
+ if ((b & 0x80) === 0) return [result, pos];
35
+ shift += 7n;
36
+ }
37
+ }
38
+
39
+ function* fields(buf) {
40
+ let pos = 0;
41
+ while (pos < buf.length) {
42
+ const [tag, p1] = readVarint(buf, pos);
43
+ pos = p1;
44
+ const num = Number(tag >> 3n);
45
+ const wire = Number(tag & 7n);
46
+ if (wire === 0) {
47
+ const [, p2] = readVarint(buf, pos);
48
+ pos = p2;
49
+ } else if (wire === 2) {
50
+ const [len, p2] = readVarint(buf, pos);
51
+ const n = Number(len);
52
+ yield [num, buf.subarray(p2, p2 + n)];
53
+ pos = p2 + n;
54
+ } else if (wire === 5) {
55
+ pos += 4;
56
+ } else if (wire === 1) {
57
+ pos += 8;
58
+ } else {
59
+ throw new Error("unsupported wire type " + wire);
60
+ }
61
+ }
62
+ }
63
+
64
+ function varintBytes(value) {
65
+ let v = BigInt(value);
66
+ const out = [];
67
+ do {
68
+ let b = Number(v & 0x7fn);
69
+ v >>= 7n;
70
+ if (v) b |= 0x80;
71
+ out.push(b);
72
+ } while (v);
73
+ return out;
74
+ }
75
+
76
+ const bytesField = (num, payload) => [
77
+ ...varintBytes((BigInt(num) << 3n) | 2n),
78
+ ...varintBytes(payload.length),
79
+ ...payload,
80
+ ];
81
+ const varintField = (num, value) => [
82
+ ...varintBytes((BigInt(num) << 3n) | 0n),
83
+ ...varintBytes(value),
84
+ ];
85
+
86
+ function parseRequest(buf) {
87
+ const filesToGenerate = [];
88
+ const protoFiles = [];
89
+ let parameter = "";
90
+ const dec = new TextDecoder();
91
+ for (const [num, payload] of fields(buf)) {
92
+ if (num === 1) filesToGenerate.push(dec.decode(payload));
93
+ else if (num === 2) parameter = dec.decode(payload);
94
+ else if (num === 15) protoFiles.push(payload);
95
+ }
96
+ // Re-wrap the raw FileDescriptorProtos as a FileDescriptorSet.
97
+ const set = [];
98
+ for (const raw of protoFiles) set.push(...bytesField(1, raw));
99
+ return { filesToGenerate, parameter, setBytes: new Uint8Array(set) };
100
+ }
101
+
102
+ function encodeResponse(files) {
103
+ const enc = new TextEncoder();
104
+ const out = [...varintField(2, 1)]; // FEATURE_PROTO3_OPTIONAL
105
+ for (const f of files) {
106
+ const content = typeof f.content === "string" ? enc.encode(f.content) : f.content;
107
+ const entry = [...bytesField(1, enc.encode(f.name)), ...bytesField(15, content)];
108
+ out.push(...bytesField(15, new Uint8Array(entry)));
109
+ }
110
+ return Buffer.from(out);
111
+ }
112
+
113
+ function encodeError(message) {
114
+ const enc = new TextEncoder();
115
+ return Buffer.from(bytesField(1, enc.encode(message)));
116
+ }
117
+
118
+ // ---------------------------------------------------------------------------
119
+ // Language selection + main.
120
+ // ---------------------------------------------------------------------------
121
+
122
+ function selectedLang() {
123
+ const args = process.argv.slice(2);
124
+ const ai = args.indexOf("--lang");
125
+ if (ai >= 0 && args[ai + 1]) return args[ai + 1];
126
+ if (process.env.GRPC_DC_LANG) return process.env.GRPC_DC_LANG;
127
+ const base = basename(process.argv[1] || "");
128
+ const prefix = "protoc-gen-grpc-dc-";
129
+ if (base.startsWith(prefix)) return base.slice(prefix.length);
130
+ return null;
131
+ }
132
+
133
+ function main() {
134
+ const lang = selectedLang();
135
+ if (!lang) {
136
+ process.stderr.write(
137
+ "protoc-gen-grpc-dc: no language selected (name the binary " +
138
+ "protoc-gen-grpc-dc-<lang>, or set GRPC_DC_LANG, or pass --lang)\n",
139
+ );
140
+ process.exit(1);
141
+ }
142
+ const request = parseRequest(readFileSync(0));
143
+ try {
144
+ const files = generateFiles(lang, request.setBytes, request.filesToGenerate, request.parameter);
145
+ process.stdout.write(encodeResponse(files));
146
+ } catch (e) {
147
+ process.stdout.write(encodeError(String((e && e.message) || e)));
148
+ }
149
+ }
150
+
151
+ main();
package/src/codec.js CHANGED
@@ -18,6 +18,10 @@ export class StatusError extends Error {
18
18
  super(`${StatusName[code] ?? `CODE_${code}`}: ${message}`);
19
19
  this.name = "StatusError";
20
20
  this.code = code;
21
+ // The raw status message for the wire: Error.message carries
22
+ // a "NAME: " display prefix that must not leak into status
23
+ // frames (peers would see it doubled).
24
+ this.statusMessage = message;
21
25
  }
22
26
  static ok() {
23
27
  return new StatusError(StatusCode.OK, "");
@@ -106,7 +110,7 @@ export function parseTimeout(text) {
106
110
  }
107
111
 
108
112
  export function statusFrame(frameType, streamId, status) {
109
- const body = JSON.stringify({ code: status.code, message: status.message ?? "" });
113
+ const body = JSON.stringify({ code: status.code, message: status.statusMessage ?? status.message ?? "" });
110
114
  return encodeFrame(frameType, streamId, textEncoder.encode(body));
111
115
  }
112
116
 
package/src/http2.js ADDED
@@ -0,0 +1,119 @@
1
+ // @generated by grpc-dc codegen. DO NOT EDIT.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // HTTP/2 client transport: call official gRPC servers directly.
4
+ //
5
+ // Http2DataChannel is a DataChannel-like adapter over a
6
+ // node:http2 client session: Endpoint/Channel above it speak
7
+ // grpc-dc frames; each frame is translated to HTTP/2 streams
8
+ // and back. The session layer (HPACK, flow control) is Node
9
+ // stdlib; TLS comes from node:http2 connect options (ALPN h2)
10
+ // — only the dial glue is generated. Node only: browsers keep
11
+ // using WebRTC DataChannels.
12
+
13
+ import http2 from "node:http2";
14
+ import { decodeFrame, encodeFrame, FrameType } from "./codec.js";
15
+ import { parseHeaders } from "./codec.js";
16
+ import { Channel } from "./channel.js";
17
+
18
+ export class Http2DataChannel {
19
+ #listeners = new Map();
20
+ #streams = new Map();
21
+ #buffers = new Map();
22
+
23
+ constructor(authority, { secure = false, ...connectOptions } = {}) {
24
+ this.readyState = "open";
25
+ this.bufferedAmount = 0;
26
+ const url = `${secure ? "https" : "http"}://${authority}`;
27
+ this.session = http2.connect(url, connectOptions);
28
+ this.session.on("error", () => {});
29
+ }
30
+
31
+ addEventListener(name, fn) {
32
+ if (!this.#listeners.has(name)) this.#listeners.set(name, []);
33
+ this.#listeners.get(name).push(fn);
34
+ }
35
+
36
+ #emit(bytes) {
37
+ const data = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
38
+ for (const fn of this.#listeners.get("message") ?? []) fn({ data });
39
+ }
40
+
41
+ #emitFrame(type, streamId, payloadText) {
42
+ this.#emit(encodeFrame(type, streamId, new TextEncoder().encode(payloadText)));
43
+ }
44
+
45
+ send(frameBytes) {
46
+ const frame = decodeFrame(frameBytes);
47
+ const id = frame.streamId;
48
+ if (frame.type === FrameType.HEADERS) {
49
+ const head = parseHeaders(frame.payload);
50
+ const metadata = {};
51
+ for (const [k, v] of Object.entries(head.metadata ?? {})) metadata[k.toLowerCase()] = v;
52
+ const stream = this.session.request({
53
+ ":path": head.path,
54
+ ":method": "POST",
55
+ "content-type": "application/grpc",
56
+ te: "trailers",
57
+ ...metadata,
58
+ });
59
+ this.#streams.set(id, stream);
60
+ this.#buffers.set(id, Buffer.alloc(0));
61
+ stream.on("response", (headers, flags) => {
62
+ const meta = {};
63
+ for (const [k, v] of Object.entries(headers)) {
64
+ if (!k.startsWith(":") && k !== "content-type") meta[k] = v;
65
+ }
66
+ const trailersOnly =
67
+ (flags & http2.constants.NGHTTP2_FLAG_END_STREAM) !== 0 && headers["grpc-status"] !== undefined;
68
+ if (trailersOnly) {
69
+ this.#emitFrame(FrameType.TRAILERS, id, JSON.stringify({
70
+ code: Number(headers["grpc-status"]),
71
+ message: decodeURIComponent(headers["grpc-message"] ?? ""),
72
+ }));
73
+ } else {
74
+ this.#emitFrame(FrameType.HEADERS, id, JSON.stringify({ metadata: meta }));
75
+ }
76
+ });
77
+ stream.on("trailers", (headers) => {
78
+ this.#emitFrame(FrameType.TRAILERS, id, JSON.stringify({
79
+ code: Number(headers["grpc-status"] ?? 2),
80
+ message: decodeURIComponent(headers["grpc-message"] ?? ""),
81
+ }));
82
+ });
83
+ stream.on("data", (chunk) => {
84
+ let buf = Buffer.concat([this.#buffers.get(id), chunk]);
85
+ while (buf.length >= 5) {
86
+ const len = buf.readUInt32BE(1);
87
+ if (buf.length < 5 + len) break;
88
+ this.#emit(encodeFrame(FrameType.DATA, id, new Uint8Array(buf.subarray(5, 5 + len))));
89
+ buf = buf.subarray(5 + len);
90
+ }
91
+ this.#buffers.set(id, buf);
92
+ });
93
+ stream.on("error", () => {
94
+ this.#emitFrame(FrameType.RST, id, JSON.stringify({ code: 8, message: "h2 stream error" }));
95
+ });
96
+ } else if (frame.type === FrameType.DATA) {
97
+ const stream = this.#streams.get(id);
98
+ if (stream) {
99
+ const lpm = Buffer.alloc(5 + frame.payload.length);
100
+ lpm.writeUInt32BE(frame.payload.length, 1);
101
+ Buffer.from(frame.payload).copy(lpm, 5);
102
+ stream.write(lpm);
103
+ }
104
+ } else if (frame.type === FrameType.HALF_CLOSE) {
105
+ this.#streams.get(id)?.end();
106
+ } else if (frame.type === FrameType.RST) {
107
+ this.#streams.get(id)?.close(http2.constants.NGHTTP2_CANCEL);
108
+ }
109
+ }
110
+
111
+ close() {
112
+ this.session.close();
113
+ }
114
+ }
115
+
116
+ /** Channel over HTTP/2 to an official gRPC server. */
117
+ export function http2Channel(authority, options) {
118
+ return new Channel(new Http2DataChannel(authority, options));
119
+ }