grpc-dc 0.40.0 → 0.41.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/index.d.ts CHANGED
@@ -115,6 +115,80 @@ export declare function createPeer(
115
115
  options?: { parity?: "odd" | "even" },
116
116
  ): { endpoint: Endpoint; server: Server; channel: Channel };
117
117
 
118
+ // HTTP/POST signaling (browser client; server half is the
119
+ // Node-only grpc-dc/signaling_server module, untyped here).
120
+ export declare const SIGNAL_PATH: string;
121
+ export declare function signalViaHttp(
122
+ url: string,
123
+ offerSdp: string,
124
+ fetchImpl?: typeof fetch,
125
+ ): Promise<string>;
126
+ export declare function signalSessionViaHttp(
127
+ url: string,
128
+ offerSdp: string,
129
+ fetchImpl?: typeof fetch,
130
+ ): Promise<{ sdp: string; session: string | null }>;
131
+ export declare function signalCandidateViaHttp(
132
+ url: string,
133
+ session: string,
134
+ candidate: string,
135
+ fetchImpl?: typeof fetch,
136
+ ): Promise<void>;
137
+ export declare function signalRestartViaHttp(
138
+ url: string,
139
+ session: string,
140
+ offerSdp: string,
141
+ fetchImpl?: typeof fetch,
142
+ ): Promise<string>;
143
+ export interface HttpSignalingOptions {
144
+ iceServers?: RTCIceServer[];
145
+ iceGatherTimeoutMs?: number;
146
+ connectTimeoutMs?: number;
147
+ label?: string;
148
+ trickle?: boolean;
149
+ fetchImpl?: typeof fetch;
150
+ RTCPeerConnectionImpl?: typeof RTCPeerConnection;
151
+ }
152
+ export declare function httpSignalingChannel(
153
+ url: string,
154
+ options?: HttpSignalingOptions,
155
+ ): Promise<Channel & {
156
+ peerConnection: RTCPeerConnection;
157
+ dataChannel: RTCDataChannel;
158
+ signalingSession: string | null;
159
+ closeSignaling(): void;
160
+ restartSignaling(): Promise<void>;
161
+ }>;
162
+ export interface RendezvousDescriptor {
163
+ host: string;
164
+ port: number;
165
+ iceUfrag: string;
166
+ icePwd: string;
167
+ fingerprint: string;
168
+ setup?: string;
169
+ }
170
+ export interface RendezvousOptions {
171
+ iceServers?: RTCIceServer[];
172
+ iceTransportPolicy?: RTCIceTransportPolicy;
173
+ timeoutMs?: number;
174
+ label?: string;
175
+ }
176
+ export declare function synthesizeRendezvousOffer(
177
+ descriptor: RendezvousDescriptor,
178
+ ): string;
179
+ export declare function fetchRendezvousDescriptor(
180
+ url: string,
181
+ options?: { fetchImpl?: typeof fetch },
182
+ ): Promise<RendezvousDescriptor>;
183
+ export declare function rendezvousChannel(
184
+ descriptor: RendezvousDescriptor,
185
+ options?: RendezvousOptions,
186
+ ): Promise<Channel & {
187
+ peerConnection: RTCPeerConnection;
188
+ dataChannel: RTCDataChannel;
189
+ closeRendezvous(): void;
190
+ }>;
191
+
118
192
  // Event bus (grpcdc.events.v1): message/stub types live in
119
193
  // grpc-dc's generated events_pb/events_grpc_dc modules; these
120
194
  // are the broker and helpers over them. TagValue/Event are
package/index.js CHANGED
@@ -34,7 +34,35 @@ export {
34
34
  export { Channel } from "./src/channel.js";
35
35
  export { Server } from "./src/server.js";
36
36
  export { memoryDataChannelPair } from "./src/loopback.js";
37
+ export {
38
+ DC_CHUNK_MAGIC,
39
+ MAX_DC_CHUNK_PAYLOAD,
40
+ DcChunkReassembler,
41
+ chunkedDataChannel,
42
+ splitDcMessage,
43
+ } from "./src/chunking.js";
37
44
  export { createGrpcDcBridge, createGrpcDcServerBridge } from "./src/wasm_bridge.js";
45
+ export {
46
+ SIGNAL_PATH,
47
+ httpSignalingChannel,
48
+ signalCandidateViaHttp,
49
+ signalRestartViaHttp,
50
+ signalSessionViaHttp,
51
+ signalViaHttp,
52
+ } from "./src/signaling.js";
53
+ export {
54
+ fetchRendezvousDescriptor,
55
+ rendezvousChannel,
56
+ synthesizeRendezvousOffer,
57
+ } from "./src/rendezvous.js";
58
+ // The rendezvous server (src/rendezvous_server.js) is Node-only
59
+ // (node:dgram via webrtc/js/rendezvous.js) and is not re-exported
60
+ // here. Import it directly:
61
+ // import { serveRendezvous } from "grpc-dc/rendezvous_server";
62
+ // The Node HTTP/POST signaling server (src/signaling_server.js)
63
+ // is likewise Node-only (node:http + the Node WebRTC stack) and
64
+ // is not re-exported here. Import it directly:
65
+ // import { serveHttpSignaling } from "grpc-dc/signaling_server";
38
66
  // Note: the Node WebRTC-Direct adapter (src/webrtc_direct.js)
39
67
  // is deliberately NOT re-exported here -- it imports Node-only
40
68
  // modules (node:dgram via webrtc/js/direct.js) and this index
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "grpc-dc",
3
- "version": "0.40.0",
3
+ "version": "0.41.0",
4
4
  "description": "gRPC in the browser over WebRTC DataChannels: dependency-free generated gRPC clients & servers for JavaScript \u2014 no grpc-web proxy, no external runtime",
5
5
  "keywords": [
6
6
  "grpc",
@@ -29,6 +29,10 @@
29
29
  },
30
30
  "./wire_gen.js": "./wire_gen.js",
31
31
  "./webrtc_direct": "./src/webrtc_direct.js",
32
+ "./signaling": "./src/signaling.js",
33
+ "./signaling_server": "./src/signaling_server.js",
34
+ "./rendezvous": "./src/rendezvous.js",
35
+ "./rendezvous_server": "./src/rendezvous_server.js",
32
36
  "./http2": "./src/http2.js",
33
37
  "./eventbus_gitlog": "./src/eventbus_gitlog.js"
34
38
  },
@@ -3398,8 +3398,19 @@ function swpCamel(s) {
3398
3398
  return parts[0] + parts.slice(1).map((p) => (p ? p[0].toUpperCase() + p.slice(1) : '')).join('');
3399
3399
  }
3400
3400
 
3401
+ const SWP_KEYWORDS = new Set([
3402
+ 'associatedtype', 'class', 'deinit', 'enum', 'extension', 'fileprivate',
3403
+ 'func', 'import', 'init', 'inout', 'internal', 'let', 'open', 'operator',
3404
+ 'private', 'precedencegroup', 'protocol', 'public', 'rethrows', 'static',
3405
+ 'struct', 'subscript', 'typealias', 'var', 'actor', 'async', 'await',
3406
+ 'break', 'case', 'catch', 'continue', 'default', 'defer', 'do', 'else',
3407
+ 'fallthrough', 'for', 'guard', 'if', 'in', 'return', 'throw', 'switch',
3408
+ 'where', 'while', 'false', 'nil', 'true', 'self', 'Self', 'super',
3409
+ ]);
3410
+
3401
3411
  function swpFieldName(f) {
3402
- return f.jsonName ? f.jsonName : swpCamel(f.name);
3412
+ const n = f.jsonName ? f.jsonName : swpCamel(f.name);
3413
+ return SWP_KEYWORDS.has(n) ? '`' + n + '`' : n;
3403
3414
  }
3404
3415
 
3405
3416
  function swpResolve(model, typeName) {
@@ -0,0 +1,143 @@
1
+ // @generated by grpc-dc codegen. DO NOT EDIT.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // DataChannel message chunking: browsers reject (and peers
4
+ // drop) single RTCDataChannel messages above the negotiated
5
+ // max-message-size (262144 in Chromium), so a grpc-dc frame
6
+ // larger than that could never cross a browser. This shim
7
+ // sits below the Endpoint, on the DC message layer: outgoing
8
+ // messages over MAX_DC_CHUNK_PAYLOAD are split into chunk
9
+ // messages with a 2-byte header [magic, flags]; incoming
10
+ // chunk messages are reassembled before the Endpoint sees
11
+ // them. Messages at or under the limit pass through byte-
12
+ // identical, and the magic byte (0xF0) is an unknown grpc-dc
13
+ // frame type, so a peer without the shim ignores chunks
14
+ // instead of misdecoding them (the documented forward-compat
15
+ // rule) — chunking is safe to enable on one side of a link
16
+ // whose other side only ever receives small messages, and
17
+ // required on both sides for large ones.
18
+ //
19
+ // The native WebRTC stacks (webrtc/rust, webrtc/go,
20
+ // webrtc/js) implement the same format behind an opt-in flag.
21
+
22
+ export const DC_CHUNK_MAGIC = 0xf0;
23
+ export const DC_CHUNK_FINAL = 0x01;
24
+ export const MAX_DC_CHUNK_PAYLOAD = 65536;
25
+
26
+ /** Split one DC message into wire messages (raw if small). */
27
+ export function splitDcMessage(bytes, maxPayload = MAX_DC_CHUNK_PAYLOAD) {
28
+ const data = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
29
+ if (data.length <= maxPayload) return [data];
30
+ const out = [];
31
+ for (let off = 0; off < data.length; off += maxPayload) {
32
+ const last = off + maxPayload >= data.length;
33
+ const chunk = new Uint8Array(2 + Math.min(maxPayload, data.length - off));
34
+ chunk[0] = DC_CHUNK_MAGIC;
35
+ chunk[1] = last ? DC_CHUNK_FINAL : 0;
36
+ chunk.set(data.subarray(off, off + Math.min(maxPayload, data.length - off)), 2);
37
+ out.push(chunk);
38
+ }
39
+ return out;
40
+ }
41
+
42
+ /** Stateful reassembler: push one wire message, get the
43
+ * complete logical message or null while chunks accumulate.
44
+ * Non-chunk messages pass through untouched. */
45
+ export class DcChunkReassembler {
46
+ #parts = null;
47
+
48
+ push(bytes) {
49
+ const data = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
50
+ if (this.#parts === null && (data.length < 2 || data[0] !== DC_CHUNK_MAGIC)) {
51
+ return data;
52
+ }
53
+ if (data.length < 2 || data[0] !== DC_CHUNK_MAGIC) {
54
+ // Raw message interleaved mid-reassembly (should not
55
+ // happen on an ordered channel): drop the partial.
56
+ this.#parts = null;
57
+ return data;
58
+ }
59
+ if (this.#parts === null) this.#parts = [];
60
+ this.#parts.push(data.subarray(2));
61
+ if ((data[1] & DC_CHUNK_FINAL) === 0) return null;
62
+ const total = this.#parts.reduce((n, p) => n + p.length, 0);
63
+ const out = new Uint8Array(total);
64
+ let off = 0;
65
+ for (const part of this.#parts) {
66
+ out.set(part, off);
67
+ off += part.length;
68
+ }
69
+ this.#parts = null;
70
+ return out;
71
+ }
72
+ }
73
+
74
+ /** Wrap a DataChannel-shaped object (send + message events)
75
+ * with chunk-split sends and reassembling receives. The
76
+ * Endpoint-facing interface is preserved. */
77
+ export function chunkedDataChannel(dc, { maxPayload = MAX_DC_CHUNK_PAYLOAD } = {}) {
78
+ const reassembler = new DcChunkReassembler();
79
+ const listeners = new Map();
80
+ const emit = (name, event) => {
81
+ for (const fn of listeners.get(name) ?? []) fn(event);
82
+ };
83
+ const onRawMessage = (event) => {
84
+ let data = event?.data ?? event;
85
+ const done = (bytes) => {
86
+ const whole = reassembler.push(bytes);
87
+ if (whole !== null) emit("message", { data: whole });
88
+ };
89
+ if (typeof Blob !== "undefined" && data instanceof Blob) {
90
+ data.arrayBuffer().then((b) => done(new Uint8Array(b)));
91
+ } else if (data instanceof ArrayBuffer) {
92
+ done(new Uint8Array(data));
93
+ } else if (ArrayBuffer.isView(data)) {
94
+ done(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
95
+ } else {
96
+ emit("message", event);
97
+ }
98
+ };
99
+ if (typeof dc.addEventListener === "function") dc.addEventListener("message", onRawMessage);
100
+ else dc.onmessage = onRawMessage;
101
+ for (const name of ["open", "close", "error", "bufferedamountlow"]) {
102
+ if (typeof dc.addEventListener === "function") {
103
+ dc.addEventListener(name, (e) => emit(name, e));
104
+ }
105
+ }
106
+ return {
107
+ get readyState() {
108
+ return dc.readyState;
109
+ },
110
+ get bufferedAmount() {
111
+ return dc.bufferedAmount ?? 0;
112
+ },
113
+ set bufferedAmountLowThreshold(v) {
114
+ try {
115
+ dc.bufferedAmountLowThreshold = v;
116
+ } catch {
117
+ /* structural channels may not expose it */
118
+ }
119
+ },
120
+ set binaryType(v) {
121
+ try {
122
+ dc.binaryType = v;
123
+ } catch {
124
+ /* ignore */
125
+ }
126
+ },
127
+ send(bytes) {
128
+ for (const chunk of splitDcMessage(bytes, maxPayload)) dc.send(chunk);
129
+ },
130
+ addEventListener(name, fn) {
131
+ if (!listeners.has(name)) listeners.set(name, []);
132
+ listeners.get(name).push(fn);
133
+ },
134
+ removeEventListener(name, fn) {
135
+ const list = listeners.get(name) ?? [];
136
+ const i = list.indexOf(fn);
137
+ if (i >= 0) list.splice(i, 1);
138
+ },
139
+ close() {
140
+ dc.close?.();
141
+ },
142
+ };
143
+ }
@@ -1,7 +1,7 @@
1
1
  // Generated code: use without restriction (see GENERATED-CODE-LICENSE.md).
2
2
  // @generated by protoc-gen-grpc-dc-js. DO NOT EDIT.
3
3
  // source: kafka.proto
4
- import { MetadataRequest, MetadataResponse, OpenProducerRequest, OpenProducerResponse, CloseProducerRequest, CloseProducerResponse, EndTransactionRequest, EndTransactionResponse, ProduceRequest, ProduceResponse, ConsumeRequest, ConsumeResponse, GroupSubscribeRequest, GroupSubscribeResponse, ListOffsetsRequest, ListOffsetsResponse, CommitOffsetRequest, CommitOffsetResponse, FetchOffsetRequest, FetchOffsetResponse, CreateTopicsRequest, CreateTopicsResponse, DeleteTopicsRequest, DeleteTopicsResponse } from "./kafka_pb.js";
4
+ import { MetadataRequest, MetadataResponse, OpenProducerRequest, OpenProducerResponse, CloseProducerRequest, CloseProducerResponse, EndTransactionRequest, EndTransactionResponse, ProduceRequest, ProduceResponse, ConsumeRequest, ConsumeResponse, GroupSubscribeRequest, GroupSubscribeResponse, ListOffsetsRequest, ListOffsetsResponse, CommitOffsetRequest, CommitOffsetResponse, FetchOffsetRequest, FetchOffsetResponse, CreateTopicsRequest, CreateTopicsResponse, DeleteTopicsRequest, DeleteTopicsResponse, DescribeConfigsRequest, DescribeConfigsResponse, AlterConfigsRequest, AlterConfigsResponse, CreatePartitionsRequest, CreatePartitionsResponse, DeleteRecordsRequest, DeleteRecordsResponse, ListGroupsRequest, ListGroupsResponse, DescribeGroupsRequest, DescribeGroupsResponse, DeleteGroupsRequest, DeleteGroupsResponse, DeleteGroupOffsetsRequest, DeleteGroupOffsetsResponse, DescribeAclsRequest, DescribeAclsResponse, CreateAclsRequest, CreateAclsResponse, DeleteAclsRequest, DeleteAclsResponse, ListTransactionsRequest, ListTransactionsResponse, DescribeTransactionsRequest, DescribeTransactionsResponse, OpenShareGroupRequest, OpenShareGroupResponse, SharePollRequest, SharePollResponse, ShareAckRequest, ShareAckResponse, CloseShareGroupRequest, CloseShareGroupResponse } from "./kafka_pb.js";
5
5
 
6
6
  export class KafkaClient {
7
7
  #channel;
@@ -42,6 +42,57 @@ export class KafkaClient {
42
42
  async deleteTopics(request, options) {
43
43
  return this.#channel.unary("/grpcdc.kafka.v1.Kafka/DeleteTopics", request, DeleteTopicsRequest, DeleteTopicsResponse, options);
44
44
  }
45
+ async describeConfigs(request, options) {
46
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/DescribeConfigs", request, DescribeConfigsRequest, DescribeConfigsResponse, options);
47
+ }
48
+ async alterConfigs(request, options) {
49
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/AlterConfigs", request, AlterConfigsRequest, AlterConfigsResponse, options);
50
+ }
51
+ async createPartitions(request, options) {
52
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/CreatePartitions", request, CreatePartitionsRequest, CreatePartitionsResponse, options);
53
+ }
54
+ async deleteRecords(request, options) {
55
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/DeleteRecords", request, DeleteRecordsRequest, DeleteRecordsResponse, options);
56
+ }
57
+ async listGroups(request, options) {
58
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/ListGroups", request, ListGroupsRequest, ListGroupsResponse, options);
59
+ }
60
+ async describeGroups(request, options) {
61
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/DescribeGroups", request, DescribeGroupsRequest, DescribeGroupsResponse, options);
62
+ }
63
+ async deleteGroups(request, options) {
64
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/DeleteGroups", request, DeleteGroupsRequest, DeleteGroupsResponse, options);
65
+ }
66
+ async deleteGroupOffsets(request, options) {
67
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/DeleteGroupOffsets", request, DeleteGroupOffsetsRequest, DeleteGroupOffsetsResponse, options);
68
+ }
69
+ async describeAcls(request, options) {
70
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/DescribeAcls", request, DescribeAclsRequest, DescribeAclsResponse, options);
71
+ }
72
+ async createAcls(request, options) {
73
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/CreateAcls", request, CreateAclsRequest, CreateAclsResponse, options);
74
+ }
75
+ async deleteAcls(request, options) {
76
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/DeleteAcls", request, DeleteAclsRequest, DeleteAclsResponse, options);
77
+ }
78
+ async listTransactions(request, options) {
79
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/ListTransactions", request, ListTransactionsRequest, ListTransactionsResponse, options);
80
+ }
81
+ async describeTransactions(request, options) {
82
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/DescribeTransactions", request, DescribeTransactionsRequest, DescribeTransactionsResponse, options);
83
+ }
84
+ async openShareGroup(request, options) {
85
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/OpenShareGroup", request, OpenShareGroupRequest, OpenShareGroupResponse, options);
86
+ }
87
+ async sharePoll(request, options) {
88
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/SharePoll", request, SharePollRequest, SharePollResponse, options);
89
+ }
90
+ async shareAck(request, options) {
91
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/ShareAck", request, ShareAckRequest, ShareAckResponse, options);
92
+ }
93
+ async closeShareGroup(request, options) {
94
+ return this.#channel.unary("/grpcdc.kafka.v1.Kafka/CloseShareGroup", request, CloseShareGroupRequest, CloseShareGroupResponse, options);
95
+ }
45
96
  }
46
97
 
47
98
  export function createKafkaService(handlers) {
@@ -60,6 +111,23 @@ export function createKafkaService(handlers) {
60
111
  FetchOffset: { path: "/grpcdc.kafka.v1.Kafka/FetchOffset", kind: "unary", requestType: FetchOffsetRequest, responseType: FetchOffsetResponse, handler: handlers.fetchOffset },
61
112
  CreateTopics: { path: "/grpcdc.kafka.v1.Kafka/CreateTopics", kind: "unary", requestType: CreateTopicsRequest, responseType: CreateTopicsResponse, handler: handlers.createTopics },
62
113
  DeleteTopics: { path: "/grpcdc.kafka.v1.Kafka/DeleteTopics", kind: "unary", requestType: DeleteTopicsRequest, responseType: DeleteTopicsResponse, handler: handlers.deleteTopics },
114
+ DescribeConfigs: { path: "/grpcdc.kafka.v1.Kafka/DescribeConfigs", kind: "unary", requestType: DescribeConfigsRequest, responseType: DescribeConfigsResponse, handler: handlers.describeConfigs },
115
+ AlterConfigs: { path: "/grpcdc.kafka.v1.Kafka/AlterConfigs", kind: "unary", requestType: AlterConfigsRequest, responseType: AlterConfigsResponse, handler: handlers.alterConfigs },
116
+ CreatePartitions: { path: "/grpcdc.kafka.v1.Kafka/CreatePartitions", kind: "unary", requestType: CreatePartitionsRequest, responseType: CreatePartitionsResponse, handler: handlers.createPartitions },
117
+ DeleteRecords: { path: "/grpcdc.kafka.v1.Kafka/DeleteRecords", kind: "unary", requestType: DeleteRecordsRequest, responseType: DeleteRecordsResponse, handler: handlers.deleteRecords },
118
+ ListGroups: { path: "/grpcdc.kafka.v1.Kafka/ListGroups", kind: "unary", requestType: ListGroupsRequest, responseType: ListGroupsResponse, handler: handlers.listGroups },
119
+ DescribeGroups: { path: "/grpcdc.kafka.v1.Kafka/DescribeGroups", kind: "unary", requestType: DescribeGroupsRequest, responseType: DescribeGroupsResponse, handler: handlers.describeGroups },
120
+ DeleteGroups: { path: "/grpcdc.kafka.v1.Kafka/DeleteGroups", kind: "unary", requestType: DeleteGroupsRequest, responseType: DeleteGroupsResponse, handler: handlers.deleteGroups },
121
+ DeleteGroupOffsets: { path: "/grpcdc.kafka.v1.Kafka/DeleteGroupOffsets", kind: "unary", requestType: DeleteGroupOffsetsRequest, responseType: DeleteGroupOffsetsResponse, handler: handlers.deleteGroupOffsets },
122
+ DescribeAcls: { path: "/grpcdc.kafka.v1.Kafka/DescribeAcls", kind: "unary", requestType: DescribeAclsRequest, responseType: DescribeAclsResponse, handler: handlers.describeAcls },
123
+ CreateAcls: { path: "/grpcdc.kafka.v1.Kafka/CreateAcls", kind: "unary", requestType: CreateAclsRequest, responseType: CreateAclsResponse, handler: handlers.createAcls },
124
+ DeleteAcls: { path: "/grpcdc.kafka.v1.Kafka/DeleteAcls", kind: "unary", requestType: DeleteAclsRequest, responseType: DeleteAclsResponse, handler: handlers.deleteAcls },
125
+ ListTransactions: { path: "/grpcdc.kafka.v1.Kafka/ListTransactions", kind: "unary", requestType: ListTransactionsRequest, responseType: ListTransactionsResponse, handler: handlers.listTransactions },
126
+ DescribeTransactions: { path: "/grpcdc.kafka.v1.Kafka/DescribeTransactions", kind: "unary", requestType: DescribeTransactionsRequest, responseType: DescribeTransactionsResponse, handler: handlers.describeTransactions },
127
+ OpenShareGroup: { path: "/grpcdc.kafka.v1.Kafka/OpenShareGroup", kind: "unary", requestType: OpenShareGroupRequest, responseType: OpenShareGroupResponse, handler: handlers.openShareGroup },
128
+ SharePoll: { path: "/grpcdc.kafka.v1.Kafka/SharePoll", kind: "unary", requestType: SharePollRequest, responseType: SharePollResponse, handler: handlers.sharePoll },
129
+ ShareAck: { path: "/grpcdc.kafka.v1.Kafka/ShareAck", kind: "unary", requestType: ShareAckRequest, responseType: ShareAckResponse, handler: handlers.shareAck },
130
+ CloseShareGroup: { path: "/grpcdc.kafka.v1.Kafka/CloseShareGroup", kind: "unary", requestType: CloseShareGroupRequest, responseType: CloseShareGroupResponse, handler: handlers.closeShareGroup },
63
131
  },
64
132
  };
65
133
  }
@@ -0,0 +1,177 @@
1
+ // @generated by grpc-dc codegen. DO NOT EDIT.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // In-process Kafka client via libkafka_native (native only).
4
+ //
5
+ // The collapsed form of the Kafka proxy: the Kafka protocol
6
+ // implementation runs in this process through the
7
+ // kafka-native N-API addon, instead of in a kafka_proxy
8
+ // sidecar reached over gRPC. Method surface mirrors the
9
+ // generated KafkaClient; requests and responses are the
10
+ // same generated kafka_pb.js messages.
11
+ //
12
+ // The addon is pure N-API C (kafka_native_addon.c next to
13
+ // this file); build it once per platform:
14
+ // cc -O2 -fPIC -shared -I<node include dir> \
15
+ // -I<kafka-native include dir> kafka_native_addon.c \
16
+ // -L<kafka-native lib dir> -lkafka_native \
17
+ // -Wl,-rpath,<kafka-native lib dir> \
18
+ // -o kafka_native_addon.node
19
+ // Addon location: options.addon, the
20
+ // GRPC_DC_KAFKA_NATIVE_ADDON environment variable, or
21
+ // kafka_native_addon.node next to this file.
22
+
23
+ import { createRequire } from "node:module";
24
+ import { dirname, join } from "node:path";
25
+ import { fileURLToPath } from "node:url";
26
+ import * as pb from "./kafka_pb.js";
27
+
28
+ const require = createRequire(import.meta.url);
29
+
30
+ const UNARY = {
31
+ Metadata: "MetadataResponse",
32
+ OpenProducer: "OpenProducerResponse",
33
+ CloseProducer: "CloseProducerResponse",
34
+ EndTransaction: "EndTransactionResponse",
35
+ Produce: "ProduceResponse",
36
+ ListOffsets: "ListOffsetsResponse",
37
+ CommitOffset: "CommitOffsetResponse",
38
+ FetchOffset: "FetchOffsetResponse",
39
+ CreateTopics: "CreateTopicsResponse",
40
+ DeleteTopics: "DeleteTopicsResponse",
41
+ DescribeConfigs: "DescribeConfigsResponse",
42
+ AlterConfigs: "AlterConfigsResponse",
43
+ CreatePartitions: "CreatePartitionsResponse",
44
+ DeleteRecords: "DeleteRecordsResponse",
45
+ ListGroups: "ListGroupsResponse",
46
+ DescribeGroups: "DescribeGroupsResponse",
47
+ DeleteGroups: "DeleteGroupsResponse",
48
+ DeleteGroupOffsets: "DeleteGroupOffsetsResponse",
49
+ DescribeAcls: "DescribeAclsResponse",
50
+ CreateAcls: "CreateAclsResponse",
51
+ DeleteAcls: "DeleteAclsResponse",
52
+ ListTransactions: "ListTransactionsResponse",
53
+ DescribeTransactions: "DescribeTransactionsResponse",
54
+ };
55
+
56
+ const STREAMS = {
57
+ Consume: "ConsumeResponse",
58
+ Subscribe: "GroupSubscribeResponse",
59
+ };
60
+
61
+ function loadAddon(options) {
62
+ const candidates = [];
63
+ if (options.addon) candidates.push(options.addon);
64
+ if (process.env.GRPC_DC_KAFKA_NATIVE_ADDON) {
65
+ candidates.push(process.env.GRPC_DC_KAFKA_NATIVE_ADDON);
66
+ }
67
+ candidates.push(
68
+ join(dirname(fileURLToPath(import.meta.url)),
69
+ "kafka_native_addon.node"));
70
+ let lastErr = null;
71
+ for (const path of candidates) {
72
+ try {
73
+ return require(path);
74
+ } catch (err) {
75
+ lastErr = err;
76
+ }
77
+ }
78
+ throw new Error(
79
+ `kafka-native: addon not loadable (${lastErr?.message}); ` +
80
+ "build kafka_native_addon.c (see header comment)");
81
+ }
82
+
83
+ export class KafkaNativeStream {
84
+ #addon;
85
+ #stream;
86
+ #respClass;
87
+ #done = false;
88
+
89
+ constructor(addon, stream, respClass) {
90
+ this.#addon = addon;
91
+ this.#stream = stream;
92
+ this.#respClass = respClass;
93
+ }
94
+
95
+ /** Next chunk, or null at end of stream. */
96
+ async next() {
97
+ if (this.#done) return null;
98
+ const bytes = await this.#addon.streamNext(this.#stream);
99
+ if (bytes === null) {
100
+ this.#done = true;
101
+ return null;
102
+ }
103
+ return this.#respClass.decode(new Uint8Array(bytes));
104
+ }
105
+
106
+ close() {
107
+ this.#done = true;
108
+ }
109
+
110
+ async *[Symbol.asyncIterator]() {
111
+ for (;;) {
112
+ const chunk = await this.next();
113
+ if (chunk === null) return;
114
+ yield chunk;
115
+ }
116
+ }
117
+ }
118
+
119
+ export class KafkaNativeClient {
120
+ #addon;
121
+ #handle;
122
+
123
+ /**
124
+ * broker: "host:port". options: { clientId, addon,
125
+ * saslMechanism, username, password, tlsCaCert,
126
+ * tlsServerName, tlsInsecureSkipVerify }.
127
+ */
128
+ constructor(broker, options = {}) {
129
+ this.#addon = loadAddon(options);
130
+ this.#handle = this.#addon.create(broker, options);
131
+ }
132
+
133
+ close() {
134
+ this.#handle = null;
135
+ }
136
+
137
+ async #call(method, request) {
138
+ if (!this.#handle) throw new Error("kafka-native: client closed");
139
+ const bytes = await this.#addon.call(
140
+ this.#handle, method, request.constructor.encode(request));
141
+ return pb[UNARY[method]].decode(new Uint8Array(bytes));
142
+ }
143
+
144
+ #stream(method, request) {
145
+ if (!this.#handle) throw new Error("kafka-native: client closed");
146
+ const stream = this.#addon.streamNew(
147
+ this.#handle, method, request.constructor.encode(request));
148
+ return new KafkaNativeStream(
149
+ this.#addon, stream, pb[STREAMS[method]]);
150
+ }
151
+
152
+ async metadata(request) { return this.#call("Metadata", request); }
153
+ async openProducer(request) { return this.#call("OpenProducer", request); }
154
+ async closeProducer(request) { return this.#call("CloseProducer", request); }
155
+ async endTransaction(request) { return this.#call("EndTransaction", request); }
156
+ async produce(request) { return this.#call("Produce", request); }
157
+ consume(request) { return this.#stream("Consume", request); }
158
+ subscribe(request) { return this.#stream("Subscribe", request); }
159
+ async listOffsets(request) { return this.#call("ListOffsets", request); }
160
+ async commitOffset(request) { return this.#call("CommitOffset", request); }
161
+ async fetchOffset(request) { return this.#call("FetchOffset", request); }
162
+ async createTopics(request) { return this.#call("CreateTopics", request); }
163
+ async deleteTopics(request) { return this.#call("DeleteTopics", request); }
164
+ async describeConfigs(request) { return this.#call("DescribeConfigs", request); }
165
+ async alterConfigs(request) { return this.#call("AlterConfigs", request); }
166
+ async createPartitions(request) { return this.#call("CreatePartitions", request); }
167
+ async deleteRecords(request) { return this.#call("DeleteRecords", request); }
168
+ async listGroups(request) { return this.#call("ListGroups", request); }
169
+ async describeGroups(request) { return this.#call("DescribeGroups", request); }
170
+ async deleteGroups(request) { return this.#call("DeleteGroups", request); }
171
+ async deleteGroupOffsets(request) { return this.#call("DeleteGroupOffsets", request); }
172
+ async describeAcls(request) { return this.#call("DescribeAcls", request); }
173
+ async createAcls(request) { return this.#call("CreateAcls", request); }
174
+ async deleteAcls(request) { return this.#call("DeleteAcls", request); }
175
+ async listTransactions(request) { return this.#call("ListTransactions", request); }
176
+ async describeTransactions(request) { return this.#call("DescribeTransactions", request); }
177
+ }