@celljs/grpc 0.0.0-stage → 4.3.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 +123 -2
- package/cell.yml +12 -0
- package/esm/node/call/async-queue.d.ts +31 -0
- package/esm/node/call/async-queue.js +80 -0
- package/esm/node/call/async-queue.js.map +1 -0
- package/esm/node/call/grpc-status.d.ts +33 -0
- package/esm/node/call/grpc-status.js +48 -0
- package/esm/node/call/grpc-status.js.map +1 -0
- package/esm/node/call/message-framer.d.ts +24 -0
- package/esm/node/call/message-framer.js +105 -0
- package/esm/node/call/message-framer.js.map +1 -0
- package/esm/node/call/metadata.d.ts +15 -0
- package/esm/node/call/metadata.js +81 -0
- package/esm/node/call/metadata.js.map +1 -0
- package/esm/node/client/grpc-channel.d.ts +64 -0
- package/esm/node/client/grpc-channel.js +285 -0
- package/esm/node/client/grpc-channel.js.map +1 -0
- package/esm/node/codec/proto-reader.d.ts +26 -0
- package/esm/node/codec/proto-reader.js +153 -0
- package/esm/node/codec/proto-reader.js.map +1 -0
- package/esm/node/codec/proto-writer.d.ts +29 -0
- package/esm/node/codec/proto-writer.js +150 -0
- package/esm/node/codec/proto-writer.js.map +1 -0
- package/esm/node/index.d.ts +15 -0
- package/esm/node/index.js +16 -0
- package/esm/node/index.js.map +1 -0
- package/esm/node/module.d.ts +3 -0
- package/esm/node/module.js +4 -0
- package/esm/node/module.js.map +1 -0
- package/esm/node/proto/proto-ast.d.ts +46 -0
- package/esm/node/proto/proto-ast.js +2 -0
- package/esm/node/proto/proto-ast.js.map +1 -0
- package/esm/node/proto/proto-parser.d.ts +45 -0
- package/esm/node/proto/proto-parser.js +451 -0
- package/esm/node/proto/proto-parser.js.map +1 -0
- package/esm/node/proto/proto-types.d.ts +76 -0
- package/esm/node/proto/proto-types.js +444 -0
- package/esm/node/proto/proto-types.js.map +1 -0
- package/esm/node/proto/proto.d.ts +57 -0
- package/esm/node/proto/proto.js +174 -0
- package/esm/node/proto/proto.js.map +1 -0
- package/esm/node/proto/well-known.d.ts +5 -0
- package/esm/node/proto/well-known.js +41 -0
- package/esm/node/proto/well-known.js.map +1 -0
- package/esm/node/server/grpc-server-lifecycle.d.ts +21 -0
- package/esm/node/server/grpc-server-lifecycle.js +65 -0
- package/esm/node/server/grpc-server-lifecycle.js.map +1 -0
- package/esm/node/server/grpc-server.d.ts +60 -0
- package/esm/node/server/grpc-server.js +312 -0
- package/esm/node/server/grpc-server.js.map +1 -0
- package/esm/node/server/grpc-service.d.ts +24 -0
- package/esm/node/server/grpc-service.js +38 -0
- package/esm/node/server/grpc-service.js.map +1 -0
- package/esm/node/test/proto-loader.d.ts +2 -0
- package/esm/node/test/proto-loader.js +13 -0
- package/esm/node/test/proto-loader.js.map +1 -0
- package/esm/node/test/test-protos.d.ts +28 -0
- package/esm/node/test/test-protos.js +145 -0
- package/esm/node/test/test-protos.js.map +1 -0
- package/esm/package.json +3 -0
- package/lib/node/call/async-queue.d.ts +32 -0
- package/lib/node/call/async-queue.d.ts.map +1 -0
- package/lib/node/call/async-queue.js +84 -0
- package/lib/node/call/async-queue.js.map +1 -0
- package/lib/node/call/grpc-status.d.ts +34 -0
- package/lib/node/call/grpc-status.d.ts.map +1 -0
- package/lib/node/call/grpc-status.js +52 -0
- package/lib/node/call/grpc-status.js.map +1 -0
- package/lib/node/call/message-framer.d.ts +25 -0
- package/lib/node/call/message-framer.d.ts.map +1 -0
- package/lib/node/call/message-framer.js +109 -0
- package/lib/node/call/message-framer.js.map +1 -0
- package/lib/node/call/metadata.d.ts +16 -0
- package/lib/node/call/metadata.d.ts.map +1 -0
- package/lib/node/call/metadata.js +84 -0
- package/lib/node/call/metadata.js.map +1 -0
- package/lib/node/client/grpc-channel.d.ts +65 -0
- package/lib/node/client/grpc-channel.d.ts.map +1 -0
- package/lib/node/client/grpc-channel.js +289 -0
- package/lib/node/client/grpc-channel.js.map +1 -0
- package/lib/node/codec/proto-reader.d.ts +27 -0
- package/lib/node/codec/proto-reader.d.ts.map +1 -0
- package/lib/node/codec/proto-reader.js +157 -0
- package/lib/node/codec/proto-reader.js.map +1 -0
- package/lib/node/codec/proto-writer.d.ts +30 -0
- package/lib/node/codec/proto-writer.d.ts.map +1 -0
- package/lib/node/codec/proto-writer.js +154 -0
- package/lib/node/codec/proto-writer.js.map +1 -0
- package/lib/node/grpc.spec.d.ts +2 -0
- package/lib/node/grpc.spec.d.ts.map +1 -0
- package/lib/node/grpc.spec.js +148 -0
- package/lib/node/grpc.spec.js.map +1 -0
- package/lib/node/index.d.ts +16 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +32 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/interop.spec.d.ts +2 -0
- package/lib/node/interop.spec.d.ts.map +1 -0
- package/lib/node/interop.spec.js +194 -0
- package/lib/node/interop.spec.js.map +1 -0
- package/lib/node/module.d.ts +4 -0
- package/lib/node/module.d.ts.map +1 -0
- package/lib/node/module.js +6 -0
- package/lib/node/module.js.map +1 -0
- package/lib/node/proto/proto-ast.d.ts +47 -0
- package/lib/node/proto/proto-ast.d.ts.map +1 -0
- package/lib/node/proto/proto-ast.js +3 -0
- package/lib/node/proto/proto-ast.js.map +1 -0
- package/lib/node/proto/proto-parser.d.ts +46 -0
- package/lib/node/proto/proto-parser.d.ts.map +1 -0
- package/lib/node/proto/proto-parser.js +455 -0
- package/lib/node/proto/proto-parser.js.map +1 -0
- package/lib/node/proto/proto-parser.spec.d.ts +2 -0
- package/lib/node/proto/proto-parser.spec.d.ts.map +1 -0
- package/lib/node/proto/proto-parser.spec.js +95 -0
- package/lib/node/proto/proto-parser.spec.js.map +1 -0
- package/lib/node/proto/proto-types.d.ts +77 -0
- package/lib/node/proto/proto-types.d.ts.map +1 -0
- package/lib/node/proto/proto-types.js +449 -0
- package/lib/node/proto/proto-types.js.map +1 -0
- package/lib/node/proto/proto-types.spec.d.ts +2 -0
- package/lib/node/proto/proto-types.spec.d.ts.map +1 -0
- package/lib/node/proto/proto-types.spec.js +120 -0
- package/lib/node/proto/proto-types.spec.js.map +1 -0
- package/lib/node/proto/proto.d.ts +58 -0
- package/lib/node/proto/proto.d.ts.map +1 -0
- package/lib/node/proto/proto.js +178 -0
- package/lib/node/proto/proto.js.map +1 -0
- package/lib/node/proto/well-known.d.ts +6 -0
- package/lib/node/proto/well-known.d.ts.map +1 -0
- package/lib/node/proto/well-known.js +44 -0
- package/lib/node/proto/well-known.js.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.d.ts +22 -0
- package/lib/node/server/grpc-server-lifecycle.d.ts.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.js +68 -0
- package/lib/node/server/grpc-server-lifecycle.js.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.spec.d.ts +2 -0
- package/lib/node/server/grpc-server-lifecycle.spec.d.ts.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.spec.js +100 -0
- package/lib/node/server/grpc-server-lifecycle.spec.js.map +1 -0
- package/lib/node/server/grpc-server.d.ts +61 -0
- package/lib/node/server/grpc-server.d.ts.map +1 -0
- package/lib/node/server/grpc-server.js +316 -0
- package/lib/node/server/grpc-server.js.map +1 -0
- package/lib/node/server/grpc-service.d.ts +25 -0
- package/lib/node/server/grpc-service.d.ts.map +1 -0
- package/lib/node/server/grpc-service.js +42 -0
- package/lib/node/server/grpc-service.js.map +1 -0
- package/lib/node/test/proto-loader.d.ts +3 -0
- package/lib/node/test/proto-loader.d.ts.map +1 -0
- package/lib/node/test/proto-loader.js +16 -0
- package/lib/node/test/proto-loader.js.map +1 -0
- package/lib/node/test/test-protos.d.ts +29 -0
- package/lib/node/test/test-protos.d.ts.map +1 -0
- package/lib/node/test/test-protos.js +150 -0
- package/lib/node/test/test-protos.js.map +1 -0
- package/lib/package.spec.d.ts +2 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +7 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +69 -4
- package/src/node/call/async-queue.ts +79 -0
- package/src/node/call/grpc-status.ts +49 -0
- package/src/node/call/message-framer.ts +109 -0
- package/src/node/call/metadata.ts +85 -0
- package/src/node/client/grpc-channel.ts +327 -0
- package/src/node/codec/proto-reader.ts +172 -0
- package/src/node/codec/proto-writer.ts +170 -0
- package/src/node/grpc.spec.ts +160 -0
- package/src/node/index.ts +15 -0
- package/src/node/interop.spec.ts +174 -0
- package/src/node/module.ts +4 -0
- package/src/node/proto/proto-ast.ts +48 -0
- package/src/node/proto/proto-parser.spec.ts +104 -0
- package/src/node/proto/proto-parser.ts +477 -0
- package/src/node/proto/proto-types.spec.ts +128 -0
- package/src/node/proto/proto-types.ts +497 -0
- package/src/node/proto/proto.ts +218 -0
- package/src/node/proto/well-known.ts +40 -0
- package/src/node/server/grpc-server-lifecycle.spec.ts +88 -0
- package/src/node/server/grpc-server-lifecycle.ts +56 -0
- package/src/node/server/grpc-server.ts +368 -0
- package/src/node/server/grpc-service.ts +40 -0
- package/src/node/test/proto-loader.ts +13 -0
- package/src/node/test/test-protos.ts +159 -0
- package/src/package.spec.ts +5 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { GrpcError, GrpcStatus } from './call/grpc-status';
|
|
3
|
+
import { GrpcChannel } from './client/grpc-channel';
|
|
4
|
+
import { Proto } from './proto/proto';
|
|
5
|
+
import { GrpcServer } from './server/grpc-server';
|
|
6
|
+
import { collect, TEST_PROTO, TestGreeter } from './test/test-protos';
|
|
7
|
+
|
|
8
|
+
const Greeter = Proto.parse(TEST_PROTO).service('Greeter');
|
|
9
|
+
|
|
10
|
+
/** The error a call fails with. (Not `expect(call).rejects`: under Bun 1.4 it breaks HTTP/2 streams of the same process.) */
|
|
11
|
+
async function rejection(promise: Promise<unknown> | AsyncIterable<unknown>): Promise<GrpcError> {
|
|
12
|
+
try {
|
|
13
|
+
await (Symbol.asyncIterator in promise ? collect(promise as AsyncIterable<unknown>) : promise);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
return error as GrpcError;
|
|
16
|
+
}
|
|
17
|
+
throw new Error('The call did not fail');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('GrpcServer and GrpcChannel', () => {
|
|
21
|
+
const errors: unknown[] = [];
|
|
22
|
+
let server: GrpcServer;
|
|
23
|
+
let channel: GrpcChannel;
|
|
24
|
+
let greeter: any;
|
|
25
|
+
|
|
26
|
+
beforeAll(async () => {
|
|
27
|
+
server = new GrpcServer({ onError: error => errors.push(error) }).addService(Greeter, new TestGreeter());
|
|
28
|
+
const port = await server.listen(0);
|
|
29
|
+
channel = new GrpcChannel(`127.0.0.1:${port}`, { metadata: { 'x-user': 'ann' } });
|
|
30
|
+
greeter = channel.createClient(Greeter);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterAll(async () => {
|
|
34
|
+
channel.close();
|
|
35
|
+
await server.close(true);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('makes unary calls with metadata, binary metadata included', async () => {
|
|
39
|
+
const payload = new Uint8Array([1, 2, 3]);
|
|
40
|
+
const reply = await greeter.sayHello({ name: 'Cell', payload }, { metadata: { 'x-token-bin': new Uint8Array([7, 8]) } });
|
|
41
|
+
expect(reply).toEqual({ message: 'Hello Cell (ann) [7,8]', payload });
|
|
42
|
+
expect(await greeter.nothing({})).toEqual({});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('streams responses, requests, and both', async () => {
|
|
46
|
+
expect(await collect(greeter.sayHellos({ name: 'a', count: 3 }))).toEqual(
|
|
47
|
+
[{ message: 'Hello a 0' }, { message: 'Hello a 1' }, { message: 'Hello a 2' }].map(m => ({ ...m, payload: new Uint8Array(0) })),
|
|
48
|
+
);
|
|
49
|
+
expect(await greeter.collectHellos([{ name: 'a' }, { name: 'b' }])).toMatchObject({ message: 'Hello a, b' });
|
|
50
|
+
async function* names() {
|
|
51
|
+
for (const name of ['x', 'y', 'z']) {
|
|
52
|
+
await new Promise(r => setTimeout(r, 1));
|
|
53
|
+
yield { name };
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
expect((await collect(greeter.chat(names()))).map((r: any) => r.message)).toEqual(['> x', '> y', '> z']);
|
|
57
|
+
expect(await greeter.collectHellos([])).toMatchObject({ message: 'Hello ' });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('answers the status, message and metadata of a GrpcError', async () => {
|
|
61
|
+
const error = await rejection(greeter.sayHello({ name: 'fail' }));
|
|
62
|
+
expect(error).toBeInstanceOf(GrpcError);
|
|
63
|
+
expect(error.code).toBe(GrpcStatus.INVALID_ARGUMENT);
|
|
64
|
+
expect(error.details).toBe('name must not be "fail" — ünïcode');
|
|
65
|
+
expect(error.metadata['x-reason']).toBe('bad name');
|
|
66
|
+
expect(error.message).toBe('INVALID_ARGUMENT: name must not be "fail" — ünïcode');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('answers UNKNOWN for other errors and reports them', async () => {
|
|
70
|
+
const error = await rejection(greeter.sayHello({ name: 'crash' }));
|
|
71
|
+
expect([error.code, error.details]).toEqual([GrpcStatus.UNKNOWN, 'boom']);
|
|
72
|
+
expect(errors.map(e => (e as Error).message)).toContain('boom');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('ends a response stream with the error after the messages sent', async () => {
|
|
76
|
+
const received: unknown[] = [];
|
|
77
|
+
let error: GrpcError | undefined;
|
|
78
|
+
try {
|
|
79
|
+
for await (const reply of greeter.sayHellos({ name: 'fail', count: 5 })) {
|
|
80
|
+
received.push(reply);
|
|
81
|
+
}
|
|
82
|
+
} catch (e) {
|
|
83
|
+
error = e as GrpcError;
|
|
84
|
+
}
|
|
85
|
+
expect(received.length).toBe(2);
|
|
86
|
+
expect(error?.code).toBe(GrpcStatus.ABORTED);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('answers UNIMPLEMENTED for methods without implementation', async () => {
|
|
90
|
+
const partial = new GrpcServer().addService(Greeter, { sayHello: () => ({ message: 'only this' }) });
|
|
91
|
+
const port = await partial.listen(0);
|
|
92
|
+
const client: any = new GrpcChannel(`http://127.0.0.1:${port}`).createClient(Greeter);
|
|
93
|
+
try {
|
|
94
|
+
expect((await client.sayHello({})).message).toBe('only this');
|
|
95
|
+
const error = await rejection(client.echo({}));
|
|
96
|
+
expect([error.code, error.details]).toEqual([GrpcStatus.UNIMPLEMENTED, 'The server does not implement /cell.test.Greeter/Echo']);
|
|
97
|
+
} finally {
|
|
98
|
+
await partial.close(true);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('fails with DEADLINE_EXCEEDED after the timeout, and the server method sees the cancellation', async () => {
|
|
103
|
+
const started = Date.now();
|
|
104
|
+
const error = await rejection(greeter.sayHello({ name: 'slow' }, { timeout: 50 }));
|
|
105
|
+
expect(error.code).toBe(GrpcStatus.DEADLINE_EXCEEDED);
|
|
106
|
+
expect(Date.now() - started).toBeLessThan(1000);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('cancels calls by signal and by leaving a response stream', async () => {
|
|
110
|
+
const controller = new AbortController();
|
|
111
|
+
const call = greeter.sayHello({ name: 'slow' }, { signal: controller.signal });
|
|
112
|
+
setTimeout(() => controller.abort(), 20);
|
|
113
|
+
expect((await rejection(call)).code).toBe(GrpcStatus.CANCELLED);
|
|
114
|
+
const received: unknown[] = [];
|
|
115
|
+
for await (const reply of greeter.sayHellos({ name: 'b', count: 1000 })) {
|
|
116
|
+
received.push(reply);
|
|
117
|
+
if (received.length === 3) {
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
expect(received.length).toBe(3);
|
|
122
|
+
// The channel is still usable.
|
|
123
|
+
expect((await greeter.sayHello({ name: 'again' })).message).toBe('Hello again (ann)');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('fails with UNAVAILABLE when nothing listens', async () => {
|
|
127
|
+
const free = new GrpcServer();
|
|
128
|
+
const port = await free.listen(0);
|
|
129
|
+
await free.close();
|
|
130
|
+
const client: any = new GrpcChannel(`127.0.0.1:${port}`).createClient(Greeter);
|
|
131
|
+
expect((await rejection(client.sayHello({ name: 'x' }))).code).toBe(GrpcStatus.UNAVAILABLE);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('rejects messages over the size limit with RESOURCE_EXHAUSTED', async () => {
|
|
135
|
+
const small = new GrpcServer({ maxReceiveMessageSize: 1000 }).addService(Greeter, new TestGreeter());
|
|
136
|
+
const port = await small.listen(0);
|
|
137
|
+
const client: any = new GrpcChannel(`127.0.0.1:${port}`, { maxReceiveMessageSize: 1000 }).createClient(Greeter);
|
|
138
|
+
try {
|
|
139
|
+
expect((await rejection(client.sayHello({ payload: new Uint8Array(2000) }))).code).toBe(GrpcStatus.RESOURCE_EXHAUSTED);
|
|
140
|
+
// The response of the other server is over the client's limit.
|
|
141
|
+
const big: any = new GrpcChannel(channel.address, { maxReceiveMessageSize: 1000 }).createClient(Greeter);
|
|
142
|
+
expect((await rejection(big.sayHello({ payload: new Uint8Array(2000) }))).code).toBe(GrpcStatus.RESOURCE_EXHAUSTED);
|
|
143
|
+
} finally {
|
|
144
|
+
await small.close(true);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('carries large messages and many concurrent calls on one connection', async () => {
|
|
149
|
+
const payload = new Uint8Array(3 * 1024 * 1024).map((_, i) => i % 251);
|
|
150
|
+
const reply = await greeter.sayHello({ name: 'big', payload });
|
|
151
|
+
expect(reply.payload.length).toBe(payload.length);
|
|
152
|
+
expect(Buffer.compare(Buffer.from(reply.payload), Buffer.from(payload))).toBe(0);
|
|
153
|
+
const replies = await Promise.all(Array.from({ length: 200 }, (_, i) => greeter.sayHello({ name: String(i) })));
|
|
154
|
+
expect(replies.map(r => r.message)).toEqual(Array.from({ length: 200 }, (_, i) => `Hello ${i} (ann)`));
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('rejects requests it cannot encode without calling the server', async () => {
|
|
158
|
+
expect((await rejection(greeter.sayHello({ count: 'many' }))).message).toBe('cell.test.HelloRequest.count: "many" is not a number');
|
|
159
|
+
});
|
|
160
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export * from './call/async-queue';
|
|
2
|
+
export * from './call/grpc-status';
|
|
3
|
+
export * from './call/message-framer';
|
|
4
|
+
export * from './call/metadata';
|
|
5
|
+
export * from './client/grpc-channel';
|
|
6
|
+
export * from './codec/proto-reader';
|
|
7
|
+
export * from './codec/proto-writer';
|
|
8
|
+
export * from './proto/proto';
|
|
9
|
+
export * from './proto/proto-ast';
|
|
10
|
+
export * from './proto/proto-parser';
|
|
11
|
+
export * from './proto/proto-types';
|
|
12
|
+
export * from './proto/well-known';
|
|
13
|
+
export * from './server/grpc-server';
|
|
14
|
+
export * from './server/grpc-server-lifecycle';
|
|
15
|
+
export * from './server/grpc-service';
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
|
|
2
|
+
import * as grpc from '@grpc/grpc-js';
|
|
3
|
+
import { type GrpcError, GrpcStatus } from './call/grpc-status';
|
|
4
|
+
import { GrpcChannel } from './client/grpc-channel';
|
|
5
|
+
import { Proto } from './proto/proto';
|
|
6
|
+
import { GrpcServer } from './server/grpc-server';
|
|
7
|
+
import { loadTestProto } from './test/proto-loader';
|
|
8
|
+
import { collect, TEST_PROTO, TestGreeter } from './test/test-protos';
|
|
9
|
+
|
|
10
|
+
/** Interoperability with @grpc/grpc-js, the reference Node.js implementation, in both directions. */
|
|
11
|
+
|
|
12
|
+
const Greeter = Proto.parse(TEST_PROTO).service('Greeter');
|
|
13
|
+
const GreeterClient = (grpc.loadPackageDefinition(loadTestProto()) as any).cell.test.Greeter as grpc.ServiceClientConstructor;
|
|
14
|
+
const GZIP = { 'grpc.default_compression_algorithm': 2, 'grpc.default_compression_level': 2 };
|
|
15
|
+
|
|
16
|
+
function unary(client: any, method: string, request: unknown, metadata = new grpc.Metadata()) {
|
|
17
|
+
return new Promise<{ error?: grpc.ServiceError; response?: any; headers?: grpc.Metadata; trailers?: grpc.Metadata }>(resolve => {
|
|
18
|
+
let headers: grpc.Metadata | undefined;
|
|
19
|
+
const call = client[method](request, metadata, (error: grpc.ServiceError | null, response: unknown) => {
|
|
20
|
+
call.on('status', (status: grpc.StatusObject) => resolve({ error: error ?? undefined, response, headers, trailers: status.metadata }));
|
|
21
|
+
});
|
|
22
|
+
call.on('metadata', (md: grpc.Metadata) => {
|
|
23
|
+
headers = md;
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('@grpc/grpc-js client → GrpcServer', () => {
|
|
29
|
+
let server: GrpcServer;
|
|
30
|
+
let client: any;
|
|
31
|
+
|
|
32
|
+
beforeAll(async () => {
|
|
33
|
+
server = new GrpcServer({ onError: () => undefined }).addService(Greeter, new TestGreeter());
|
|
34
|
+
const port = await server.listen(0);
|
|
35
|
+
client = new GreeterClient(`127.0.0.1:${port}`, grpc.credentials.createInsecure(), GZIP);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
afterAll(async () => {
|
|
39
|
+
client.close();
|
|
40
|
+
await server.close(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('unary calls with gzip-compressed requests, metadata, response headers and trailers', async () => {
|
|
44
|
+
const metadata = new grpc.Metadata();
|
|
45
|
+
metadata.set('x-user', 'bob');
|
|
46
|
+
metadata.set('x-token-bin', Buffer.from([1, 2]));
|
|
47
|
+
const { error, response, headers, trailers } = await unary(client, 'sayHello', { name: 'grpc-js', payload: Buffer.alloc(5000, 7) }, metadata);
|
|
48
|
+
expect(error).toBeUndefined();
|
|
49
|
+
expect(response.message).toBe('Hello grpc-js (bob) [1,2]');
|
|
50
|
+
expect(response.payload.length).toBe(5000);
|
|
51
|
+
expect(headers?.get('x-greeting')).toEqual(['hi']);
|
|
52
|
+
expect(trailers?.get('x-done')).toEqual(['yes']);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('error statuses with their message and metadata', async () => {
|
|
56
|
+
const { error } = await unary(client, 'sayHello', { name: 'fail' });
|
|
57
|
+
expect(error?.code).toBe(grpc.status.INVALID_ARGUMENT);
|
|
58
|
+
expect(error?.details).toBe('name must not be "fail" — ünïcode');
|
|
59
|
+
expect(error?.metadata.get('x-reason')).toEqual(['bad name']);
|
|
60
|
+
expect((await unary(client, 'echo', { numbers: [1] })).response.numbers).toEqual([1]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('streams in both directions', async () => {
|
|
64
|
+
const replies = await collect<any>(client.sayHellos({ name: 'js', count: 3 }));
|
|
65
|
+
expect(replies.map(r => r.message)).toEqual(['Hello js 0', 'Hello js 1', 'Hello js 2']);
|
|
66
|
+
|
|
67
|
+
const collected = await new Promise<any>((resolve, reject) => {
|
|
68
|
+
const call = client.collectHellos((error: Error | null, response: unknown) => (error ? reject(error) : resolve(response)));
|
|
69
|
+
for (const name of ['a', 'b', 'c']) {
|
|
70
|
+
call.write({ name });
|
|
71
|
+
}
|
|
72
|
+
call.end();
|
|
73
|
+
});
|
|
74
|
+
expect(collected.message).toBe('Hello a, b, c');
|
|
75
|
+
|
|
76
|
+
const chat = client.chat();
|
|
77
|
+
const answers: string[] = [];
|
|
78
|
+
const done = new Promise<void>(resolve => {
|
|
79
|
+
chat.on('data', (reply: any) => answers.push(reply.message));
|
|
80
|
+
chat.on('end', resolve);
|
|
81
|
+
});
|
|
82
|
+
chat.write({ name: 'one' });
|
|
83
|
+
chat.write({ name: 'two' });
|
|
84
|
+
chat.end();
|
|
85
|
+
await done;
|
|
86
|
+
expect(answers).toEqual(['> one', '> two']);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('deadlines', async () => {
|
|
90
|
+
const error = await new Promise<grpc.ServiceError>(resolve => {
|
|
91
|
+
client.sayHello({ name: 'slow' }, { deadline: Date.now() + 50 }, (e: grpc.ServiceError) => resolve(e));
|
|
92
|
+
});
|
|
93
|
+
expect(error.code).toBe(grpc.status.DEADLINE_EXCEEDED);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe('GrpcChannel → @grpc/grpc-js server', () => {
|
|
98
|
+
let server: grpc.Server;
|
|
99
|
+
let channel: GrpcChannel;
|
|
100
|
+
let greeter: any;
|
|
101
|
+
|
|
102
|
+
beforeAll(async () => {
|
|
103
|
+
server = new grpc.Server(GZIP);
|
|
104
|
+
server.addService(GreeterClient.service, {
|
|
105
|
+
sayHello(call: any, callback: grpc.sendUnaryData<unknown>) {
|
|
106
|
+
if (call.request.name === 'fail') {
|
|
107
|
+
const metadata = new grpc.Metadata();
|
|
108
|
+
metadata.set('x-reason', 'from grpc-js');
|
|
109
|
+
callback({ code: grpc.status.NOT_FOUND, details: 'no such name', metadata });
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const user = call.metadata.get('x-user')[0] ?? '';
|
|
113
|
+
callback(null, { message: `Hi ${call.request.name} ${user}`, payload: call.request.payload });
|
|
114
|
+
},
|
|
115
|
+
sayHellos(call: any) {
|
|
116
|
+
for (let i = 0; i < call.request.count; i++) {
|
|
117
|
+
call.write({ message: `Hi ${i}` });
|
|
118
|
+
}
|
|
119
|
+
call.end();
|
|
120
|
+
},
|
|
121
|
+
collectHellos(call: any, callback: grpc.sendUnaryData<unknown>) {
|
|
122
|
+
const names: string[] = [];
|
|
123
|
+
call.on('data', (r: any) => names.push(r.name));
|
|
124
|
+
call.on('end', () => callback(null, { message: names.join('+') }));
|
|
125
|
+
},
|
|
126
|
+
chat(call: any) {
|
|
127
|
+
call.on('data', (r: any) => call.write({ message: r.name.toUpperCase() }));
|
|
128
|
+
call.on('end', () => call.end());
|
|
129
|
+
},
|
|
130
|
+
echo(call: any, callback: grpc.sendUnaryData<unknown>) {
|
|
131
|
+
callback(null, call.request);
|
|
132
|
+
},
|
|
133
|
+
nothing(_call: unknown, callback: grpc.sendUnaryData<unknown>) {
|
|
134
|
+
callback(null, {});
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
const port = await new Promise<number>((resolve, reject) => {
|
|
138
|
+
server.bindAsync('127.0.0.1:0', grpc.ServerCredentials.createInsecure(), (error, p) => (error ? reject(error) : resolve(p)));
|
|
139
|
+
});
|
|
140
|
+
channel = new GrpcChannel(`127.0.0.1:${port}`, { metadata: { 'x-user': 'cell' } });
|
|
141
|
+
greeter = channel.createClient(Greeter);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
afterAll(() => {
|
|
145
|
+
channel.close();
|
|
146
|
+
server.forceShutdown();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('unary calls, reading gzip-compressed responses', async () => {
|
|
150
|
+
const payload = new Uint8Array(5000).fill(9);
|
|
151
|
+
const reply = await greeter.sayHello({ name: 'there', payload });
|
|
152
|
+
expect(reply.message).toBe('Hi there cell');
|
|
153
|
+
expect(Buffer.compare(Buffer.from(reply.payload), Buffer.from(payload))).toBe(0);
|
|
154
|
+
const echo = await greeter.echo({ scalars: { i64: '-5', color: 'RED' }, counts: { a: 1 }, label: 'x' });
|
|
155
|
+
expect([echo.scalars.i64, echo.scalars.color, echo.counts, echo.choice, echo.label]).toEqual(['-5', 'RED', { a: 1 }, 'label', 'x']);
|
|
156
|
+
expect(await greeter.nothing({})).toEqual({});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('error statuses', async () => {
|
|
160
|
+
let error: GrpcError | undefined;
|
|
161
|
+
try {
|
|
162
|
+
await greeter.sayHello({ name: 'fail' });
|
|
163
|
+
} catch (e) {
|
|
164
|
+
error = e as GrpcError;
|
|
165
|
+
}
|
|
166
|
+
expect([error?.code, error?.details, error?.metadata['x-reason']]).toEqual([GrpcStatus.NOT_FOUND, 'no such name', 'from grpc-js']);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('streams in both directions', async () => {
|
|
170
|
+
expect((await collect<any>(greeter.sayHellos({ count: 3 }))).map(r => r.message)).toEqual(['Hi 0', 'Hi 1', 'Hi 2']);
|
|
171
|
+
expect((await greeter.collectHellos([{ name: 'a' }, { name: 'b' }])).message).toBe('a+b');
|
|
172
|
+
expect((await collect<any>(greeter.chat([{ name: 'x' }, { name: 'y' }]))).map(r => r.message)).toEqual(['X', 'Y']);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** What {@link ProtoParser} reads from one .proto file, before type references are resolved. */
|
|
2
|
+
export interface ProtoFileAst {
|
|
3
|
+
syntax: 'proto2' | 'proto3';
|
|
4
|
+
package: string;
|
|
5
|
+
imports: string[];
|
|
6
|
+
messages: MessageAst[];
|
|
7
|
+
enums: EnumAst[];
|
|
8
|
+
services: ServiceAst[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface MessageAst {
|
|
12
|
+
name: string;
|
|
13
|
+
fields: FieldAst[];
|
|
14
|
+
oneofs: string[];
|
|
15
|
+
messages: MessageAst[];
|
|
16
|
+
enums: EnumAst[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface FieldAst {
|
|
20
|
+
name: string;
|
|
21
|
+
number: number;
|
|
22
|
+
/** A scalar type name (`int32`, `string`...) or a message / enum reference as written. */
|
|
23
|
+
type: string;
|
|
24
|
+
rule?: 'repeated' | 'optional' | 'required';
|
|
25
|
+
/** Key type of a `map<K, V>` field; `type` is then the value type. */
|
|
26
|
+
keyType?: string;
|
|
27
|
+
oneof?: string;
|
|
28
|
+
/** `[packed = ...]`, when given. */
|
|
29
|
+
packed?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface EnumAst {
|
|
33
|
+
name: string;
|
|
34
|
+
values: { name: string; number: number }[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ServiceAst {
|
|
38
|
+
name: string;
|
|
39
|
+
methods: MethodAst[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface MethodAst {
|
|
43
|
+
name: string;
|
|
44
|
+
requestType: string;
|
|
45
|
+
requestStream: boolean;
|
|
46
|
+
responseType: string;
|
|
47
|
+
responseStream: boolean;
|
|
48
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { TEST_PROTO } from '../test/test-protos';
|
|
3
|
+
import { Proto } from './proto';
|
|
4
|
+
import type { MessageAst } from './proto-ast';
|
|
5
|
+
import { ProtoParser } from './proto-parser';
|
|
6
|
+
|
|
7
|
+
describe('ProtoParser', () => {
|
|
8
|
+
it('reads messages, nested types, oneofs, maps, enums and services', () => {
|
|
9
|
+
const file = ProtoParser.parse(TEST_PROTO);
|
|
10
|
+
expect(file.syntax).toBe('proto3');
|
|
11
|
+
expect(file.package).toBe('cell.test');
|
|
12
|
+
expect(file.imports).toEqual(['google/protobuf/empty.proto', 'google/protobuf/timestamp.proto']);
|
|
13
|
+
expect(file.enums[0]).toEqual({
|
|
14
|
+
name: 'Color',
|
|
15
|
+
values: [
|
|
16
|
+
{ name: 'COLOR_UNSPECIFIED', number: 0 },
|
|
17
|
+
{ name: 'RED', number: 1 },
|
|
18
|
+
{ name: 'GREEN', number: 2 },
|
|
19
|
+
],
|
|
20
|
+
});
|
|
21
|
+
const everything = file.messages.find(m => m.name === 'Everything') as MessageAst;
|
|
22
|
+
expect(everything.oneofs).toEqual(['choice']);
|
|
23
|
+
expect(everything.messages.map(m => m.name)).toEqual(['Inner']);
|
|
24
|
+
expect(everything.fields.find(f => f.name === 'by_id')).toEqual({ name: 'by_id', number: 6, type: 'Scalars', keyType: 'int64' });
|
|
25
|
+
expect(everything.fields.find(f => f.name === 'code')).toEqual({ name: 'code', number: 8, type: 'int32', oneof: 'choice' });
|
|
26
|
+
expect(everything.fields.find(f => f.name === 'maybe')?.rule).toBe('optional');
|
|
27
|
+
expect(file.services[0].methods.map(m => [m.name, m.requestStream, m.responseStream])).toEqual([
|
|
28
|
+
['SayHello', false, false],
|
|
29
|
+
['SayHellos', false, true],
|
|
30
|
+
['CollectHellos', true, false],
|
|
31
|
+
['Chat', true, true],
|
|
32
|
+
['Echo', false, false],
|
|
33
|
+
['Nothing', false, false],
|
|
34
|
+
]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('skips aggregate options, extensions and extend blocks, and reads proto2 labels and packed', () => {
|
|
38
|
+
const file = ProtoParser.parse(`
|
|
39
|
+
syntax = 'proto2';
|
|
40
|
+
package a;
|
|
41
|
+
option (my.opt) = { a: 1 b: { c: "x;" } };
|
|
42
|
+
message M {
|
|
43
|
+
extensions 100 to max;
|
|
44
|
+
required int32 id = 1 [default = -1];
|
|
45
|
+
repeated int32 values = 2 [packed = true, (custom).x = "y"];
|
|
46
|
+
optional string name = 3 [default = "a" "b"];
|
|
47
|
+
}
|
|
48
|
+
extend M { optional int32 extra = 100; }
|
|
49
|
+
service S {
|
|
50
|
+
rpc Get (M) returns (M) { option (google.api.http) = { get: "/v1/{id}" }; };
|
|
51
|
+
}
|
|
52
|
+
`);
|
|
53
|
+
expect(file.syntax).toBe('proto2');
|
|
54
|
+
expect(file.messages[0].fields).toEqual([
|
|
55
|
+
{ name: 'id', number: 1, type: 'int32', rule: 'required' },
|
|
56
|
+
{ name: 'values', number: 2, type: 'int32', rule: 'repeated', packed: true },
|
|
57
|
+
{ name: 'name', number: 3, type: 'string', rule: 'optional' },
|
|
58
|
+
]);
|
|
59
|
+
expect(file.services[0].methods[0]).toEqual({ name: 'Get', requestType: 'M', requestStream: false, responseType: 'M', responseStream: false });
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('reports the line and column of syntax errors', () => {
|
|
63
|
+
expect(() => ProtoParser.parse('syntax = "proto3";\nmessage M {\n string name = ;\n}', 'm.proto')).toThrow(
|
|
64
|
+
"m.proto:3:17: expected an integer but found ';'",
|
|
65
|
+
);
|
|
66
|
+
expect(() => ProtoParser.parse('message M { string name = 1;', 'm.proto')).toThrow('unexpected end of file');
|
|
67
|
+
expect(() => ProtoParser.parse('edition = "2023";')).toThrow('editions are not supported');
|
|
68
|
+
expect(() => ProtoParser.parse('message M { repeated group G = 1 {} }')).toThrow('groups are not supported');
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('Proto', () => {
|
|
73
|
+
it('resolves type references by protobuf scoping and finds services by full or short name', () => {
|
|
74
|
+
const proto = Proto.parse(TEST_PROTO);
|
|
75
|
+
const greeter = proto.service('Greeter');
|
|
76
|
+
expect(proto.service('cell.test.Greeter')).toBe(greeter);
|
|
77
|
+
expect(greeter.fullName).toBe('cell.test.Greeter');
|
|
78
|
+
expect(greeter.methods.map(m => [m.jsName, m.path])).toContainEqual(['sayHello', '/cell.test.Greeter/SayHello']);
|
|
79
|
+
expect(greeter.methods.find(m => m.name === 'Nothing')?.requestType.fullName).toBe('google.protobuf.Empty');
|
|
80
|
+
const everything = proto.message('Everything');
|
|
81
|
+
expect(everything.fields.find(f => f.name === 'inner')?.messageType).toBe(proto.message('cell.test.Everything.Inner'));
|
|
82
|
+
expect(everything.fields.find(f => f.name === 'snakeCaseName')?.protoName).toBe('snake_case_name');
|
|
83
|
+
expect(proto.enum('Color').values).toEqual({ COLOR_UNSPECIFIED: 0, RED: 1, GREEN: 2 });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('combines several files, resolving types across them', () => {
|
|
87
|
+
const proto = Proto.parse(
|
|
88
|
+
'syntax = "proto3"; package app; import "common.proto"; service S { rpc Get (common.Id) returns (.common.Item); }',
|
|
89
|
+
'syntax = "proto3"; package common; message Id { int64 id = 1; } message Item { Id id = 1; string name = 2; }',
|
|
90
|
+
);
|
|
91
|
+
const method = proto.service('S').methods[0];
|
|
92
|
+
expect([method.requestType.fullName, method.responseType.fullName]).toEqual(['common.Id', 'common.Item']);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('explains unknown types, missing names and ambiguous ones', () => {
|
|
96
|
+
expect(() => Proto.parse('syntax = "proto3"; message M { other.Thing t = 1; }')).toThrow(
|
|
97
|
+
"Unknown type other.Thing in M. If it is defined in an imported .proto file, pass that file's source to Proto.parse() too.",
|
|
98
|
+
);
|
|
99
|
+
const proto = Proto.parse('syntax = "proto3"; package a; message M {} service S {}', 'syntax = "proto3"; package b; message M {}');
|
|
100
|
+
expect(() => proto.message('M')).toThrow("The message name 'M' is ambiguous: a.M, b.M. Use the full name.");
|
|
101
|
+
expect(() => proto.service('Missing')).toThrow("No service 'Missing' in the .proto sources. Services: a.S.");
|
|
102
|
+
expect(() => Proto.parse('syntax = "proto3"; message M {} message M {}')).toThrow('The type M is defined twice');
|
|
103
|
+
});
|
|
104
|
+
});
|