grpc-dc 0.42.0 → 0.44.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/package.json +3 -2
- package/plugin/gen/generators.js +25 -17
- package/src/endpoint.js +45 -8
- package/src/events_pb.js +108 -24
- package/src/http2.js +17 -1
- package/src/kafka_pb.js +1488 -197
- package/src/registry.js +49 -0
- package/src/server.js +35 -0
- package/src/stdio.js +80 -0
package/src/registry.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// @generated by grpc-dc codegen. DO NOT EDIT.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// Registry discovery service (grpcdc.registry.v1.Registry):
|
|
4
|
+
// ListServices answers from the host Server's live
|
|
5
|
+
// registrations plus descriptor-only names recorded here;
|
|
6
|
+
// GetDescriptor returns FileDescriptorSet bytes registered
|
|
7
|
+
// for a name. The service's own descriptor set is embedded
|
|
8
|
+
// (proto/registry.proto), so no codegen is needed to serve
|
|
9
|
+
// or call it: parse it with parseDescriptorSet and use a
|
|
10
|
+
// DynamicClient.
|
|
11
|
+
|
|
12
|
+
import { GenericMessage, createDynamicService, parseDescriptorSet } from "./dynamic.js";
|
|
13
|
+
import { StatusCode, StatusError } from "./codec.js";
|
|
14
|
+
|
|
15
|
+
export const REGISTRY_DESCRIPTOR_SET = Uint8Array.from(
|
|
16
|
+
atob("CrkDCg5yZWdpc3RyeS5wcm90bxISZ3JwY2RjLnJlZ2lzdHJ5LnYxIhUKE0xpc3RTZXJ2aWNlc1JlcXVlc3QiMgoUTGlzdFNlcnZpY2VzUmVzcG9uc2USGgoIc2VydmljZXMYASADKAlSCHNlcnZpY2VzIioKFEdldERlc2NyaXB0b3JSZXF1ZXN0EhIKBG5hbWUYASABKAlSBG5hbWUiPgoVR2V0RGVzY3JpcHRvclJlc3BvbnNlEiUKDmRlc2NyaXB0b3Jfc2V0GAEgASgMUg1kZXNjcmlwdG9yU2V0MtMBCghSZWdpc3RyeRJhCgxMaXN0U2VydmljZXMSJy5ncnBjZGMucmVnaXN0cnkudjEuTGlzdFNlcnZpY2VzUmVxdWVzdBooLmdycGNkYy5yZWdpc3RyeS52MS5MaXN0U2VydmljZXNSZXNwb25zZRJkCg1HZXREZXNjcmlwdG9yEiguZ3JwY2RjLnJlZ2lzdHJ5LnYxLkdldERlc2NyaXB0b3JSZXF1ZXN0GikuZ3JwY2RjLnJlZ2lzdHJ5LnYxLkdldERlc2NyaXB0b3JSZXNwb25zZWIGcHJvdG8z"),
|
|
17
|
+
(c) => c.charCodeAt(0),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Create the Registry service definition for `server`.
|
|
22
|
+
* `descriptors` is a Map<string, Uint8Array> of service
|
|
23
|
+
* name to FileDescriptorSet bytes. Serve the result with
|
|
24
|
+
* server.addService(...) like any generated service.
|
|
25
|
+
*/
|
|
26
|
+
export function createRegistryService(server, descriptors = new Map()) {
|
|
27
|
+
const model = parseDescriptorSet(REGISTRY_DESCRIPTOR_SET);
|
|
28
|
+
return createDynamicService(model, "grpcdc.registry.v1.Registry", {
|
|
29
|
+
async unary(ctx, method, req) {
|
|
30
|
+
const resp = new GenericMessage(method.output);
|
|
31
|
+
if (method.name === "ListServices") {
|
|
32
|
+
const names = new Set(server.registeredServices());
|
|
33
|
+
for (const name of descriptors.keys()) names.add(name);
|
|
34
|
+
for (const name of [...names].sort()) resp.add(1, name);
|
|
35
|
+
return resp;
|
|
36
|
+
}
|
|
37
|
+
if (method.name === "GetDescriptor") {
|
|
38
|
+
const name = req.getString(1);
|
|
39
|
+
const bytes = descriptors.get(name);
|
|
40
|
+
if (!bytes) {
|
|
41
|
+
throw new StatusError(StatusCode.NOT_FOUND, `no descriptor for ${name}`);
|
|
42
|
+
}
|
|
43
|
+
resp.set(1, bytes);
|
|
44
|
+
return resp;
|
|
45
|
+
}
|
|
46
|
+
throw new StatusError(StatusCode.UNIMPLEMENTED, "registry method");
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
package/src/server.js
CHANGED
|
@@ -39,6 +39,7 @@ export class Server {
|
|
|
39
39
|
#methods = new Map();
|
|
40
40
|
#authVerifier = null;
|
|
41
41
|
#authScopes = {};
|
|
42
|
+
#interceptors = [];
|
|
42
43
|
|
|
43
44
|
addService(definition) {
|
|
44
45
|
for (const [name, method] of Object.entries(definition.methods)) {
|
|
@@ -59,6 +60,34 @@ export class Server {
|
|
|
59
60
|
* serveRendezvous when options.auth is set; rendezvous-only
|
|
60
61
|
* by design (no protoc plugin option).
|
|
61
62
|
*/
|
|
63
|
+
/** Remove every method of `name` (runtime unregistration). */
|
|
64
|
+
removeService(name) {
|
|
65
|
+
const prefix = `/${name}/`;
|
|
66
|
+
for (const path of [...this.#methods.keys()]) {
|
|
67
|
+
if (path.startsWith(prefix)) this.#methods.delete(path);
|
|
68
|
+
}
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Names of registered services, sorted (discovery source). */
|
|
73
|
+
registeredServices() {
|
|
74
|
+
const names = new Set();
|
|
75
|
+
for (const { service } of this.#methods.values()) names.add(service);
|
|
76
|
+
return [...names].sort();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Install a server interceptor: `fn({path, metadata})` runs
|
|
81
|
+
* in install order before dispatch (after rendezvous auth
|
|
82
|
+
* when set). Throwing a StatusError ends the call with that
|
|
83
|
+
* status and the handler never runs. Return a claims object
|
|
84
|
+
* to expose it as ctx.claims when no auth claims are set.
|
|
85
|
+
*/
|
|
86
|
+
addInterceptor(fn) {
|
|
87
|
+
this.#interceptors.push(fn);
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
62
91
|
requireRendezvousAuth(verifier, scopes = {}) {
|
|
63
92
|
this.#authVerifier = verifier;
|
|
64
93
|
this.#authScopes = scopes ?? {};
|
|
@@ -108,6 +137,12 @@ export class Server {
|
|
|
108
137
|
}
|
|
109
138
|
}
|
|
110
139
|
}
|
|
140
|
+
for (const interceptor of this.#interceptors) {
|
|
141
|
+
const extra = await interceptor({ path, metadata });
|
|
142
|
+
if (claims === null && extra !== null && typeof extra === "object") {
|
|
143
|
+
claims = extra;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
111
146
|
const timeoutMs = parseTimeout(metadata["grpc-timeout"]);
|
|
112
147
|
const deadline = timeoutMs === null ? null : Date.now() + timeoutMs;
|
|
113
148
|
// Initial/trailing metadata the handler accumulates via
|
package/src/stdio.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// @generated by grpc-dc codegen. DO NOT EDIT.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// IPC transports (Node only): stdio and Unix domain socket
|
|
4
|
+
// channels for host/plugin use. One message is a 4-byte
|
|
5
|
+
// big-endian length prefix plus the frame bytes — the same
|
|
6
|
+
// framing as the Rust StreamDc and Go SidecarTransport IPC
|
|
7
|
+
// forms, so a plugin process in any language can speak
|
|
8
|
+
// grpc-dc to a host without a WebRTC stack. Like
|
|
9
|
+
// rendezvous_server.js this module is Node-only and is not
|
|
10
|
+
// re-exported from index.js; import it directly:
|
|
11
|
+
// import { stdioChannel, udsChannel } from "grpc-dc/stdio";
|
|
12
|
+
|
|
13
|
+
import net from "node:net";
|
|
14
|
+
import { Duplex } from "node:stream";
|
|
15
|
+
import { Channel } from "./channel.js";
|
|
16
|
+
|
|
17
|
+
/** DataChannel-shaped IPC endpoint over a Duplex stream. */
|
|
18
|
+
export class StreamDataChannel extends EventTarget {
|
|
19
|
+
#inbox = Buffer.alloc(0);
|
|
20
|
+
#stream;
|
|
21
|
+
|
|
22
|
+
constructor(stream) {
|
|
23
|
+
super();
|
|
24
|
+
this.#stream = stream;
|
|
25
|
+
stream.on("data", (chunk) => {
|
|
26
|
+
this.#inbox = Buffer.concat([this.#inbox, chunk]);
|
|
27
|
+
for (;;) {
|
|
28
|
+
if (this.#inbox.length < 4) return;
|
|
29
|
+
const len = this.#inbox.readUInt32BE(0);
|
|
30
|
+
if (this.#inbox.length < 4 + len) return;
|
|
31
|
+
const frame = this.#inbox.subarray(4, 4 + len);
|
|
32
|
+
this.#inbox = this.#inbox.subarray(4 + len);
|
|
33
|
+
this.dispatchEvent(new MessageEvent("message", { data: frame }));
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
stream.on("error", (err) => {
|
|
37
|
+
this.dispatchEvent(new ErrorEvent("error", { error: err }));
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
send(frameBytes) {
|
|
42
|
+
const frame = Buffer.isBuffer(frameBytes) ? frameBytes : Buffer.from(frameBytes);
|
|
43
|
+
const prefix = Buffer.alloc(4);
|
|
44
|
+
prefix.writeUInt32BE(frame.length, 0);
|
|
45
|
+
this.#stream.write(Buffer.concat([prefix, frame]));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
close() {
|
|
49
|
+
this.#stream.end();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Channel over this process's stdin/stdout (plugin side). */
|
|
54
|
+
export function stdioChannel() {
|
|
55
|
+
const stream = Duplex.from({ readable: process.stdin, writable: process.stdout });
|
|
56
|
+
return new Channel(new StreamDataChannel(stream));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Channel over a Unix domain socket at `path` (plugin side). */
|
|
60
|
+
export async function udsChannel(path) {
|
|
61
|
+
const socket = await new Promise((resolve, reject) => {
|
|
62
|
+
const s = net.createConnection(path);
|
|
63
|
+
s.once("connect", () => resolve(s));
|
|
64
|
+
s.once("error", reject);
|
|
65
|
+
});
|
|
66
|
+
return new Channel(new StreamDataChannel(socket));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Accept one Unix socket connection as a Channel (host side). */
|
|
70
|
+
export async function udsServerChannel(path) {
|
|
71
|
+
const socket = await new Promise((resolve, reject) => {
|
|
72
|
+
const server = net.createServer((s) => {
|
|
73
|
+
server.close();
|
|
74
|
+
resolve(s);
|
|
75
|
+
});
|
|
76
|
+
server.once("error", reject);
|
|
77
|
+
server.listen(path);
|
|
78
|
+
});
|
|
79
|
+
return new Channel(new StreamDataChannel(socket));
|
|
80
|
+
}
|