@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.
Files changed (186) hide show
  1. package/README.md +123 -2
  2. package/cell.yml +12 -0
  3. package/esm/node/call/async-queue.d.ts +31 -0
  4. package/esm/node/call/async-queue.js +80 -0
  5. package/esm/node/call/async-queue.js.map +1 -0
  6. package/esm/node/call/grpc-status.d.ts +33 -0
  7. package/esm/node/call/grpc-status.js +48 -0
  8. package/esm/node/call/grpc-status.js.map +1 -0
  9. package/esm/node/call/message-framer.d.ts +24 -0
  10. package/esm/node/call/message-framer.js +105 -0
  11. package/esm/node/call/message-framer.js.map +1 -0
  12. package/esm/node/call/metadata.d.ts +15 -0
  13. package/esm/node/call/metadata.js +81 -0
  14. package/esm/node/call/metadata.js.map +1 -0
  15. package/esm/node/client/grpc-channel.d.ts +64 -0
  16. package/esm/node/client/grpc-channel.js +285 -0
  17. package/esm/node/client/grpc-channel.js.map +1 -0
  18. package/esm/node/codec/proto-reader.d.ts +26 -0
  19. package/esm/node/codec/proto-reader.js +153 -0
  20. package/esm/node/codec/proto-reader.js.map +1 -0
  21. package/esm/node/codec/proto-writer.d.ts +29 -0
  22. package/esm/node/codec/proto-writer.js +150 -0
  23. package/esm/node/codec/proto-writer.js.map +1 -0
  24. package/esm/node/index.d.ts +15 -0
  25. package/esm/node/index.js +16 -0
  26. package/esm/node/index.js.map +1 -0
  27. package/esm/node/module.d.ts +3 -0
  28. package/esm/node/module.js +4 -0
  29. package/esm/node/module.js.map +1 -0
  30. package/esm/node/proto/proto-ast.d.ts +46 -0
  31. package/esm/node/proto/proto-ast.js +2 -0
  32. package/esm/node/proto/proto-ast.js.map +1 -0
  33. package/esm/node/proto/proto-parser.d.ts +45 -0
  34. package/esm/node/proto/proto-parser.js +451 -0
  35. package/esm/node/proto/proto-parser.js.map +1 -0
  36. package/esm/node/proto/proto-types.d.ts +76 -0
  37. package/esm/node/proto/proto-types.js +444 -0
  38. package/esm/node/proto/proto-types.js.map +1 -0
  39. package/esm/node/proto/proto.d.ts +57 -0
  40. package/esm/node/proto/proto.js +174 -0
  41. package/esm/node/proto/proto.js.map +1 -0
  42. package/esm/node/proto/well-known.d.ts +5 -0
  43. package/esm/node/proto/well-known.js +41 -0
  44. package/esm/node/proto/well-known.js.map +1 -0
  45. package/esm/node/server/grpc-server-lifecycle.d.ts +21 -0
  46. package/esm/node/server/grpc-server-lifecycle.js +65 -0
  47. package/esm/node/server/grpc-server-lifecycle.js.map +1 -0
  48. package/esm/node/server/grpc-server.d.ts +60 -0
  49. package/esm/node/server/grpc-server.js +312 -0
  50. package/esm/node/server/grpc-server.js.map +1 -0
  51. package/esm/node/server/grpc-service.d.ts +24 -0
  52. package/esm/node/server/grpc-service.js +38 -0
  53. package/esm/node/server/grpc-service.js.map +1 -0
  54. package/esm/node/test/proto-loader.d.ts +2 -0
  55. package/esm/node/test/proto-loader.js +13 -0
  56. package/esm/node/test/proto-loader.js.map +1 -0
  57. package/esm/node/test/test-protos.d.ts +28 -0
  58. package/esm/node/test/test-protos.js +145 -0
  59. package/esm/node/test/test-protos.js.map +1 -0
  60. package/esm/package.json +3 -0
  61. package/lib/node/call/async-queue.d.ts +32 -0
  62. package/lib/node/call/async-queue.d.ts.map +1 -0
  63. package/lib/node/call/async-queue.js +84 -0
  64. package/lib/node/call/async-queue.js.map +1 -0
  65. package/lib/node/call/grpc-status.d.ts +34 -0
  66. package/lib/node/call/grpc-status.d.ts.map +1 -0
  67. package/lib/node/call/grpc-status.js +52 -0
  68. package/lib/node/call/grpc-status.js.map +1 -0
  69. package/lib/node/call/message-framer.d.ts +25 -0
  70. package/lib/node/call/message-framer.d.ts.map +1 -0
  71. package/lib/node/call/message-framer.js +109 -0
  72. package/lib/node/call/message-framer.js.map +1 -0
  73. package/lib/node/call/metadata.d.ts +16 -0
  74. package/lib/node/call/metadata.d.ts.map +1 -0
  75. package/lib/node/call/metadata.js +84 -0
  76. package/lib/node/call/metadata.js.map +1 -0
  77. package/lib/node/client/grpc-channel.d.ts +65 -0
  78. package/lib/node/client/grpc-channel.d.ts.map +1 -0
  79. package/lib/node/client/grpc-channel.js +289 -0
  80. package/lib/node/client/grpc-channel.js.map +1 -0
  81. package/lib/node/codec/proto-reader.d.ts +27 -0
  82. package/lib/node/codec/proto-reader.d.ts.map +1 -0
  83. package/lib/node/codec/proto-reader.js +157 -0
  84. package/lib/node/codec/proto-reader.js.map +1 -0
  85. package/lib/node/codec/proto-writer.d.ts +30 -0
  86. package/lib/node/codec/proto-writer.d.ts.map +1 -0
  87. package/lib/node/codec/proto-writer.js +154 -0
  88. package/lib/node/codec/proto-writer.js.map +1 -0
  89. package/lib/node/grpc.spec.d.ts +2 -0
  90. package/lib/node/grpc.spec.d.ts.map +1 -0
  91. package/lib/node/grpc.spec.js +148 -0
  92. package/lib/node/grpc.spec.js.map +1 -0
  93. package/lib/node/index.d.ts +16 -0
  94. package/lib/node/index.d.ts.map +1 -0
  95. package/lib/node/index.js +32 -0
  96. package/lib/node/index.js.map +1 -0
  97. package/lib/node/interop.spec.d.ts +2 -0
  98. package/lib/node/interop.spec.d.ts.map +1 -0
  99. package/lib/node/interop.spec.js +194 -0
  100. package/lib/node/interop.spec.js.map +1 -0
  101. package/lib/node/module.d.ts +4 -0
  102. package/lib/node/module.d.ts.map +1 -0
  103. package/lib/node/module.js +6 -0
  104. package/lib/node/module.js.map +1 -0
  105. package/lib/node/proto/proto-ast.d.ts +47 -0
  106. package/lib/node/proto/proto-ast.d.ts.map +1 -0
  107. package/lib/node/proto/proto-ast.js +3 -0
  108. package/lib/node/proto/proto-ast.js.map +1 -0
  109. package/lib/node/proto/proto-parser.d.ts +46 -0
  110. package/lib/node/proto/proto-parser.d.ts.map +1 -0
  111. package/lib/node/proto/proto-parser.js +455 -0
  112. package/lib/node/proto/proto-parser.js.map +1 -0
  113. package/lib/node/proto/proto-parser.spec.d.ts +2 -0
  114. package/lib/node/proto/proto-parser.spec.d.ts.map +1 -0
  115. package/lib/node/proto/proto-parser.spec.js +95 -0
  116. package/lib/node/proto/proto-parser.spec.js.map +1 -0
  117. package/lib/node/proto/proto-types.d.ts +77 -0
  118. package/lib/node/proto/proto-types.d.ts.map +1 -0
  119. package/lib/node/proto/proto-types.js +449 -0
  120. package/lib/node/proto/proto-types.js.map +1 -0
  121. package/lib/node/proto/proto-types.spec.d.ts +2 -0
  122. package/lib/node/proto/proto-types.spec.d.ts.map +1 -0
  123. package/lib/node/proto/proto-types.spec.js +120 -0
  124. package/lib/node/proto/proto-types.spec.js.map +1 -0
  125. package/lib/node/proto/proto.d.ts +58 -0
  126. package/lib/node/proto/proto.d.ts.map +1 -0
  127. package/lib/node/proto/proto.js +178 -0
  128. package/lib/node/proto/proto.js.map +1 -0
  129. package/lib/node/proto/well-known.d.ts +6 -0
  130. package/lib/node/proto/well-known.d.ts.map +1 -0
  131. package/lib/node/proto/well-known.js +44 -0
  132. package/lib/node/proto/well-known.js.map +1 -0
  133. package/lib/node/server/grpc-server-lifecycle.d.ts +22 -0
  134. package/lib/node/server/grpc-server-lifecycle.d.ts.map +1 -0
  135. package/lib/node/server/grpc-server-lifecycle.js +68 -0
  136. package/lib/node/server/grpc-server-lifecycle.js.map +1 -0
  137. package/lib/node/server/grpc-server-lifecycle.spec.d.ts +2 -0
  138. package/lib/node/server/grpc-server-lifecycle.spec.d.ts.map +1 -0
  139. package/lib/node/server/grpc-server-lifecycle.spec.js +100 -0
  140. package/lib/node/server/grpc-server-lifecycle.spec.js.map +1 -0
  141. package/lib/node/server/grpc-server.d.ts +61 -0
  142. package/lib/node/server/grpc-server.d.ts.map +1 -0
  143. package/lib/node/server/grpc-server.js +316 -0
  144. package/lib/node/server/grpc-server.js.map +1 -0
  145. package/lib/node/server/grpc-service.d.ts +25 -0
  146. package/lib/node/server/grpc-service.d.ts.map +1 -0
  147. package/lib/node/server/grpc-service.js +42 -0
  148. package/lib/node/server/grpc-service.js.map +1 -0
  149. package/lib/node/test/proto-loader.d.ts +3 -0
  150. package/lib/node/test/proto-loader.d.ts.map +1 -0
  151. package/lib/node/test/proto-loader.js +16 -0
  152. package/lib/node/test/proto-loader.js.map +1 -0
  153. package/lib/node/test/test-protos.d.ts +29 -0
  154. package/lib/node/test/test-protos.d.ts.map +1 -0
  155. package/lib/node/test/test-protos.js +150 -0
  156. package/lib/node/test/test-protos.js.map +1 -0
  157. package/lib/package.spec.d.ts +2 -0
  158. package/lib/package.spec.d.ts.map +1 -0
  159. package/lib/package.spec.js +7 -0
  160. package/lib/package.spec.js.map +1 -0
  161. package/package.json +69 -4
  162. package/src/node/call/async-queue.ts +79 -0
  163. package/src/node/call/grpc-status.ts +49 -0
  164. package/src/node/call/message-framer.ts +109 -0
  165. package/src/node/call/metadata.ts +85 -0
  166. package/src/node/client/grpc-channel.ts +327 -0
  167. package/src/node/codec/proto-reader.ts +172 -0
  168. package/src/node/codec/proto-writer.ts +170 -0
  169. package/src/node/grpc.spec.ts +160 -0
  170. package/src/node/index.ts +15 -0
  171. package/src/node/interop.spec.ts +174 -0
  172. package/src/node/module.ts +4 -0
  173. package/src/node/proto/proto-ast.ts +48 -0
  174. package/src/node/proto/proto-parser.spec.ts +104 -0
  175. package/src/node/proto/proto-parser.ts +477 -0
  176. package/src/node/proto/proto-types.spec.ts +128 -0
  177. package/src/node/proto/proto-types.ts +497 -0
  178. package/src/node/proto/proto.ts +218 -0
  179. package/src/node/proto/well-known.ts +40 -0
  180. package/src/node/server/grpc-server-lifecycle.spec.ts +88 -0
  181. package/src/node/server/grpc-server-lifecycle.ts +56 -0
  182. package/src/node/server/grpc-server.ts +368 -0
  183. package/src/node/server/grpc-service.ts +40 -0
  184. package/src/node/test/proto-loader.ts +13 -0
  185. package/src/node/test/test-protos.ts +159 -0
  186. package/src/package.spec.ts +5 -0
@@ -0,0 +1,29 @@
1
+ import type { ServerCall } from '../server/grpc-server';
2
+ /** The .proto of the package's tests: every field kind, and a service with every kind of method. */
3
+ export declare const TEST_PROTO = "\nsyntax = \"proto3\";\npackage cell.test;\n\nimport \"google/protobuf/empty.proto\";\nimport \"google/protobuf/timestamp.proto\";\n\n// Colors.\nenum Color {\n COLOR_UNSPECIFIED = 0;\n RED = 1;\n GREEN = 2 [deprecated = true];\n}\n\nmessage Scalars {\n double d = 1;\n float f = 2;\n int32 i32 = 3;\n int64 i64 = 4;\n uint32 u32 = 5;\n uint64 u64 = 6;\n sint32 s32 = 7;\n sint64 s64 = 8;\n fixed32 fx32 = 9;\n fixed64 fx64 = 10;\n sfixed32 sfx32 = 11;\n sfixed64 sfx64 = 12;\n bool flag = 13;\n string text = 14;\n bytes data = 15;\n Color color = 16;\n}\n\n/* Everything else. */\nmessage Everything {\n option deprecated = false;\n Scalars scalars = 1;\n repeated int32 numbers = 2;\n repeated string names = 3;\n repeated Scalars items = 4;\n map<string, int32> counts = 5;\n map<int64, Scalars> by_id = 6;\n oneof choice {\n string label = 7;\n int32 code = 8;\n Scalars nested = 9;\n }\n optional int32 maybe = 10;\n google.protobuf.Timestamp at = 11;\n repeated Color colors = 12;\n message Inner { string value = 1; }\n Inner inner = 13;\n string snake_case_name = 14;\n reserved 100 to 110;\n}\n\nmessage HelloRequest {\n string name = 1;\n int32 count = 2;\n bytes payload = 3;\n}\n\nmessage HelloReply {\n string message = 1;\n bytes payload = 2;\n}\n\nservice Greeter {\n option deprecated = false;\n rpc SayHello (HelloRequest) returns (HelloReply);\n rpc SayHellos (HelloRequest) returns (stream HelloReply);\n rpc CollectHellos (stream HelloRequest) returns (HelloReply);\n rpc Chat (stream HelloRequest) returns (stream HelloReply) {\n option deprecated = false;\n }\n rpc Echo (Everything) returns (Everything);\n rpc Nothing (google.protobuf.Empty) returns (google.protobuf.Empty);\n}\n";
4
+ interface HelloRequest {
5
+ name: string;
6
+ count: number;
7
+ payload: Uint8Array;
8
+ }
9
+ /** An implementation of cell.test.Greeter whose answers depend on the request, for the tests. */
10
+ export declare class TestGreeter {
11
+ sayHello(request: HelloRequest, call: ServerCall): Promise<unknown> | {
12
+ message: string;
13
+ payload: Uint8Array<ArrayBufferLike>;
14
+ };
15
+ sayHellos(request: HelloRequest, call: ServerCall): AsyncGenerator<{
16
+ message: string;
17
+ }, void, unknown>;
18
+ collectHellos(requests: AsyncIterable<HelloRequest>): Promise<{
19
+ message: string;
20
+ }>;
21
+ chat(requests: AsyncIterable<HelloRequest>): AsyncGenerator<{
22
+ message: string;
23
+ }, void, unknown>;
24
+ echo(message: unknown): unknown;
25
+ nothing(): {};
26
+ }
27
+ export declare function collect<T>(iterable: AsyncIterable<T>): Promise<T[]>;
28
+ export {};
29
+ //# sourceMappingURL=test-protos.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-protos.d.ts","sourceRoot":"","sources":["../../../src/node/test/test-protos.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAExD,oGAAoG;AACpG,eAAO,MAAM,UAAU,yuDA8EtB,CAAC;AAEF,UAAU,YAAY;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,UAAU,CAAC;CACvB;AAED,iGAAiG;AACjG,qBAAa,WAAW;IACpB,QAAQ,CAAC,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,UAAU;;;MAsB/C;IAEM,SAAS,CAAC,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,UAAU;;sBAWvD;IAEK,aAAa,CAAC,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC;;OAMxD;IAEM,IAAI,CAAC,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC;;sBAIhD;IAED,IAAI,CAAC,OAAO,EAAE,OAAO,WAEpB;IAED,OAAO,OAEN;CACJ;AAED,wBAAsB,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAMzE"}
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TestGreeter = exports.TEST_PROTO = void 0;
4
+ exports.collect = collect;
5
+ const grpc_status_1 = require("../call/grpc-status");
6
+ /** The .proto of the package's tests: every field kind, and a service with every kind of method. */
7
+ exports.TEST_PROTO = `
8
+ syntax = "proto3";
9
+ package cell.test;
10
+
11
+ import "google/protobuf/empty.proto";
12
+ import "google/protobuf/timestamp.proto";
13
+
14
+ // Colors.
15
+ enum Color {
16
+ COLOR_UNSPECIFIED = 0;
17
+ RED = 1;
18
+ GREEN = 2 [deprecated = true];
19
+ }
20
+
21
+ message Scalars {
22
+ double d = 1;
23
+ float f = 2;
24
+ int32 i32 = 3;
25
+ int64 i64 = 4;
26
+ uint32 u32 = 5;
27
+ uint64 u64 = 6;
28
+ sint32 s32 = 7;
29
+ sint64 s64 = 8;
30
+ fixed32 fx32 = 9;
31
+ fixed64 fx64 = 10;
32
+ sfixed32 sfx32 = 11;
33
+ sfixed64 sfx64 = 12;
34
+ bool flag = 13;
35
+ string text = 14;
36
+ bytes data = 15;
37
+ Color color = 16;
38
+ }
39
+
40
+ /* Everything else. */
41
+ message Everything {
42
+ option deprecated = false;
43
+ Scalars scalars = 1;
44
+ repeated int32 numbers = 2;
45
+ repeated string names = 3;
46
+ repeated Scalars items = 4;
47
+ map<string, int32> counts = 5;
48
+ map<int64, Scalars> by_id = 6;
49
+ oneof choice {
50
+ string label = 7;
51
+ int32 code = 8;
52
+ Scalars nested = 9;
53
+ }
54
+ optional int32 maybe = 10;
55
+ google.protobuf.Timestamp at = 11;
56
+ repeated Color colors = 12;
57
+ message Inner { string value = 1; }
58
+ Inner inner = 13;
59
+ string snake_case_name = 14;
60
+ reserved 100 to 110;
61
+ }
62
+
63
+ message HelloRequest {
64
+ string name = 1;
65
+ int32 count = 2;
66
+ bytes payload = 3;
67
+ }
68
+
69
+ message HelloReply {
70
+ string message = 1;
71
+ bytes payload = 2;
72
+ }
73
+
74
+ service Greeter {
75
+ option deprecated = false;
76
+ rpc SayHello (HelloRequest) returns (HelloReply);
77
+ rpc SayHellos (HelloRequest) returns (stream HelloReply);
78
+ rpc CollectHellos (stream HelloRequest) returns (HelloReply);
79
+ rpc Chat (stream HelloRequest) returns (stream HelloReply) {
80
+ option deprecated = false;
81
+ }
82
+ rpc Echo (Everything) returns (Everything);
83
+ rpc Nothing (google.protobuf.Empty) returns (google.protobuf.Empty);
84
+ }
85
+ `;
86
+ /** An implementation of cell.test.Greeter whose answers depend on the request, for the tests. */
87
+ class TestGreeter {
88
+ sayHello(request, call) {
89
+ if (request.name === 'fail') {
90
+ throw new grpc_status_1.GrpcError(grpc_status_1.GrpcStatus.INVALID_ARGUMENT, 'name must not be "fail" — ünïcode', { 'x-reason': 'bad name' });
91
+ }
92
+ if (request.name === 'crash') {
93
+ throw new Error('boom');
94
+ }
95
+ if (request.name === 'slow') {
96
+ return new Promise((resolve, reject) => {
97
+ const timer = setTimeout(() => resolve({ message: 'late' }), 2000);
98
+ call.signal.addEventListener('abort', () => {
99
+ clearTimeout(timer);
100
+ reject(call.signal.reason);
101
+ });
102
+ });
103
+ }
104
+ call.setHeader('x-greeting', 'hi');
105
+ call.setTrailer('x-done', 'yes');
106
+ const user = call.metadata['x-user'];
107
+ const token = call.metadata['x-token-bin'];
108
+ const suffix = (user ? ` (${user})` : '') + (token instanceof Uint8Array ? ` [${[...token].join(',')}]` : '');
109
+ return { message: `Hello ${request.name}${suffix}`, payload: request.payload };
110
+ }
111
+ async *sayHellos(request, call) {
112
+ for (let i = 0; i < request.count; i++) {
113
+ if (call.signal.aborted) {
114
+ return;
115
+ }
116
+ if (request.name === 'fail' && i === 2) {
117
+ throw new grpc_status_1.GrpcError(grpc_status_1.GrpcStatus.ABORTED, 'stopped at 2');
118
+ }
119
+ yield { message: `Hello ${request.name} ${i}` };
120
+ await new Promise(r => setTimeout(r, 1));
121
+ }
122
+ }
123
+ async collectHellos(requests) {
124
+ const names = [];
125
+ for await (const request of requests) {
126
+ names.push(request.name);
127
+ }
128
+ return { message: `Hello ${names.join(', ')}` };
129
+ }
130
+ async *chat(requests) {
131
+ for await (const request of requests) {
132
+ yield { message: `> ${request.name}` };
133
+ }
134
+ }
135
+ echo(message) {
136
+ return message;
137
+ }
138
+ nothing() {
139
+ return {};
140
+ }
141
+ }
142
+ exports.TestGreeter = TestGreeter;
143
+ async function collect(iterable) {
144
+ const items = [];
145
+ for await (const item of iterable) {
146
+ items.push(item);
147
+ }
148
+ return items;
149
+ }
150
+ //# sourceMappingURL=test-protos.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-protos.js","sourceRoot":"","sources":["../../../src/node/test/test-protos.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=package.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package.spec.d.ts","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const bun_test_1 = require("bun:test");
4
+ (0, bun_test_1.describe)('grpc package', () => {
5
+ (0, bun_test_1.it)('support code coverage statistics', () => { });
6
+ });
7
+ //# sourceMappingURL=package.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package.spec.js","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":";;AAAA,uCAAwC;AAExC,IAAA,mBAAQ,EAAC,cAAc,EAAE,GAAG,EAAE;IAC1B,IAAA,aAAE,EAAC,kCAAkC,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;AACrD,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
2
  "name": "@celljs/grpc",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "4.3.0",
4
+ "description": "gRPC servers and clients for Cell over node:http2, with a built-in .proto parser and protobuf codec",
5
+ "keywords": [
6
+ "cell-component"
7
+ ],
8
+ "homepage": "https://github.com/cellbang/cell",
9
+ "bugs": {
10
+ "url": "https://github.com/cellbang/cell/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/cellbang/cell.git"
15
+ },
16
+ "license": "MIT",
17
+ "engines": {
18
+ "node": ">=18.0.0"
19
+ },
20
+ "main": "lib/node/index.js",
21
+ "typings": "lib/node/index.d.ts",
22
+ "files": [
23
+ "lib",
24
+ "esm",
25
+ "src",
26
+ "cell.yml"
27
+ ],
28
+ "scripts": {
29
+ "build": "tsc -b && bun ../../scripts/build-esm.ts",
30
+ "clean": "rm -rf lib esm *.tsbuildinfo",
31
+ "test": "bun test --preload reflect-metadata ./lib",
32
+ "watch": "tsc -b -w --preserveWatchOutput"
33
+ },
34
+ "dependencies": {
35
+ "@celljs/core": "4.3.0"
36
+ },
37
+ "devDependencies": {
38
+ "@grpc/grpc-js": "^1.14.7",
39
+ "@grpc/proto-loader": "^0.8.1"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "exports": {
45
+ ".": {
46
+ "import": "./esm/node/index.js",
47
+ "default": "./lib/node/index.js"
48
+ },
49
+ "./package.json": "./package.json",
50
+ "./lib/node": {
51
+ "import": "./esm/node/index.js",
52
+ "default": "./lib/node/index.js"
53
+ },
54
+ "./lib/*.js": {
55
+ "import": "./esm/*.js",
56
+ "default": "./lib/*.js"
57
+ },
58
+ "./lib/*": {
59
+ "import": "./esm/*.js",
60
+ "default": "./lib/*.js"
61
+ },
62
+ "./*": "./*"
63
+ },
64
+ "cell": {
65
+ "modules": {
66
+ "backend": [
67
+ "lib/node/module"
68
+ ]
69
+ }
70
+ }
71
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * The messages of a stream as an async iterable. `pause` is called when `highWaterMark` messages are
3
+ * waiting, `resume` once the consumer has taken them all; `onReturn` when the consumer stops early.
4
+ */
5
+ export class AsyncQueue<T> implements AsyncIterable<T> {
6
+ protected readonly items: T[] = [];
7
+ protected waiter?: { resolve: (result: IteratorResult<T>) => void; reject: (error: unknown) => void };
8
+ protected ended = false;
9
+ protected error: unknown;
10
+ protected paused = false;
11
+
12
+ constructor(protected readonly options: { highWaterMark?: number; pause?: () => void; resume?: () => void; onReturn?: () => void } = {}) {}
13
+
14
+ push(item: T): void {
15
+ if (this.ended) {
16
+ return;
17
+ }
18
+ if (this.waiter) {
19
+ const { resolve } = this.waiter;
20
+ this.waiter = undefined;
21
+ resolve({ value: item, done: false });
22
+ return;
23
+ }
24
+ this.items.push(item);
25
+ if (!this.paused && this.items.length >= (this.options.highWaterMark ?? 16)) {
26
+ this.paused = true;
27
+ this.options.pause?.();
28
+ }
29
+ }
30
+
31
+ /** Ends the iteration, with `error` thrown to the consumer once the waiting messages are taken. */
32
+ end(error?: unknown): void {
33
+ if (this.ended) {
34
+ return;
35
+ }
36
+ this.ended = true;
37
+ this.error = error;
38
+ if (this.waiter) {
39
+ const { resolve, reject } = this.waiter;
40
+ this.waiter = undefined;
41
+ if (error) {
42
+ reject(error);
43
+ } else {
44
+ resolve({ value: undefined, done: true });
45
+ }
46
+ }
47
+ }
48
+
49
+ next(): Promise<IteratorResult<T>> {
50
+ if (this.items.length > 0) {
51
+ const value = this.items.shift() as T;
52
+ if (this.paused && this.items.length === 0) {
53
+ this.paused = false;
54
+ this.options.resume?.();
55
+ }
56
+ return Promise.resolve({ value, done: false });
57
+ }
58
+ if (this.ended) {
59
+ return this.error ? Promise.reject(this.error) : Promise.resolve({ value: undefined, done: true });
60
+ }
61
+ return new Promise((resolve, reject) => {
62
+ this.waiter = { resolve, reject };
63
+ });
64
+ }
65
+
66
+ [Symbol.asyncIterator](): AsyncIterator<T> {
67
+ return {
68
+ next: () => this.next(),
69
+ return: async () => {
70
+ this.items.length = 0;
71
+ if (!this.ended) {
72
+ this.ended = true;
73
+ this.options.onReturn?.();
74
+ }
75
+ return { value: undefined, done: true };
76
+ },
77
+ };
78
+ }
79
+ }
@@ -0,0 +1,49 @@
1
+ import type { Metadata } from './metadata';
2
+
3
+ /** gRPC status codes. */
4
+ export enum GrpcStatus {
5
+ OK = 0,
6
+ CANCELLED = 1,
7
+ UNKNOWN = 2,
8
+ INVALID_ARGUMENT = 3,
9
+ DEADLINE_EXCEEDED = 4,
10
+ NOT_FOUND = 5,
11
+ ALREADY_EXISTS = 6,
12
+ PERMISSION_DENIED = 7,
13
+ RESOURCE_EXHAUSTED = 8,
14
+ FAILED_PRECONDITION = 9,
15
+ ABORTED = 10,
16
+ OUT_OF_RANGE = 11,
17
+ UNIMPLEMENTED = 12,
18
+ INTERNAL = 13,
19
+ UNAVAILABLE = 14,
20
+ DATA_LOSS = 15,
21
+ UNAUTHENTICATED = 16,
22
+ }
23
+
24
+ /**
25
+ * A call that ended with a status other than OK. Throw it from a service method to answer with that
26
+ * status; clients reject with it. Errors of other types become `UNKNOWN` with the error's message.
27
+ */
28
+ export class GrpcError extends Error {
29
+ constructor(
30
+ readonly code: GrpcStatus,
31
+ readonly details = '',
32
+ readonly metadata: Metadata = {},
33
+ ) {
34
+ super(details ? `${GrpcStatus[code] ?? code}: ${details}` : (GrpcStatus[code] ?? String(code)));
35
+ this.name = 'GrpcError';
36
+ }
37
+
38
+ /** The status of any error: a `GrpcError`, or one with a numeric gRPC `code` as @grpc/grpc-js throws them. */
39
+ static from(error: unknown): GrpcError {
40
+ if (error instanceof GrpcError) {
41
+ return error;
42
+ }
43
+ const e = error as { code?: unknown; details?: unknown; message?: unknown };
44
+ if (typeof e?.code === 'number' && GrpcStatus[e.code] !== undefined) {
45
+ return new GrpcError(e.code, String(e.details ?? e.message ?? ''));
46
+ }
47
+ return new GrpcError(GrpcStatus.UNKNOWN, error instanceof Error ? error.message : String(error));
48
+ }
49
+ }
@@ -0,0 +1,109 @@
1
+ import { gunzipSync, inflateSync } from 'node:zlib';
2
+ import { GrpcError, GrpcStatus } from './grpc-status';
3
+
4
+ /** Default limit of a received message: 4 MiB, as in other gRPC implementations. */
5
+ export const DEFAULT_MAX_RECEIVE_MESSAGE_SIZE = 4 * 1024 * 1024;
6
+
7
+ /** The encodings messages may be compressed with, for `grpc-accept-encoding`. */
8
+ export const ACCEPT_ENCODING = 'identity,deflate,gzip';
9
+
10
+ export namespace MessageFramer {
11
+ /** A message prefixed with gRPC's 5-byte header (uncompressed flag, big-endian length). */
12
+ export function frame(message: Uint8Array): Uint8Array {
13
+ const framed = new Uint8Array(message.length + 5);
14
+ new DataView(framed.buffer).setUint32(1, message.length);
15
+ framed.set(message, 5);
16
+ return framed;
17
+ }
18
+ }
19
+
20
+ /** Splits the DATA of an HTTP/2 stream into gRPC messages, decompressing them when flagged. */
21
+ export class MessageDeframer {
22
+ protected chunks: Uint8Array[] = [];
23
+ protected buffered = 0;
24
+ protected expected = -1;
25
+ protected compressed = false;
26
+
27
+ constructor(
28
+ protected readonly encoding: string,
29
+ protected readonly maxMessageSize = DEFAULT_MAX_RECEIVE_MESSAGE_SIZE,
30
+ ) {
31
+ if (!['identity', 'gzip', 'deflate'].includes(encoding)) {
32
+ throw new GrpcError(GrpcStatus.UNIMPLEMENTED, `Unsupported message encoding '${encoding}' (supported: ${ACCEPT_ENCODING})`);
33
+ }
34
+ }
35
+
36
+ /** The complete messages once `chunk` is added. */
37
+ push(chunk: Uint8Array): Uint8Array[] {
38
+ this.chunks.push(chunk);
39
+ this.buffered += chunk.length;
40
+ const messages: Uint8Array[] = [];
41
+ for (;;) {
42
+ if (this.expected < 0) {
43
+ if (this.buffered < 5) {
44
+ break;
45
+ }
46
+ const head = this.take(5);
47
+ this.compressed = head[0] === 1;
48
+ this.expected = new DataView(head.buffer, head.byteOffset, 5).getUint32(1);
49
+ if (this.expected > this.maxMessageSize) {
50
+ throw new GrpcError(GrpcStatus.RESOURCE_EXHAUSTED, `Received message larger than max (${this.expected} vs. ${this.maxMessageSize})`);
51
+ }
52
+ }
53
+ if (this.buffered < this.expected) {
54
+ break;
55
+ }
56
+ const message = this.take(this.expected);
57
+ this.expected = -1;
58
+ messages.push(this.compressed ? this.decompress(message) : message);
59
+ }
60
+ return messages;
61
+ }
62
+
63
+ /** Whether a message was cut off by the end of the stream. */
64
+ get pending(): boolean {
65
+ return this.buffered > 0 || this.expected >= 0;
66
+ }
67
+
68
+ protected decompress(message: Uint8Array): Uint8Array {
69
+ if (this.encoding === 'identity') {
70
+ throw new GrpcError(GrpcStatus.INTERNAL, 'Received a compressed message without grpc-encoding');
71
+ }
72
+ try {
73
+ const options = { maxOutputLength: this.maxMessageSize };
74
+ return this.encoding === 'gzip' ? gunzipSync(message, options) : inflateSync(message, options);
75
+ } catch (error) {
76
+ if ((error as { code?: string }).code === 'ERR_BUFFER_TOO_LARGE') {
77
+ throw new GrpcError(GrpcStatus.RESOURCE_EXHAUSTED, `Received message larger than max (${this.maxMessageSize}) once decompressed`);
78
+ }
79
+ throw new GrpcError(GrpcStatus.INTERNAL, `Could not decompress a message: ${(error as Error).message}`);
80
+ }
81
+ }
82
+
83
+ protected take(count: number): Uint8Array {
84
+ this.buffered -= count;
85
+ const first = this.chunks[0];
86
+ if (first && first.length >= count) {
87
+ if (first.length === count) {
88
+ this.chunks.shift();
89
+ } else {
90
+ this.chunks[0] = first.subarray(count);
91
+ }
92
+ return first.subarray(0, count);
93
+ }
94
+ const result = new Uint8Array(count);
95
+ let offset = 0;
96
+ while (offset < count) {
97
+ const chunk = this.chunks[0];
98
+ const n = Math.min(chunk.length, count - offset);
99
+ result.set(chunk.subarray(0, n), offset);
100
+ offset += n;
101
+ if (n === chunk.length) {
102
+ this.chunks.shift();
103
+ } else {
104
+ this.chunks[0] = chunk.subarray(n);
105
+ }
106
+ }
107
+ return result;
108
+ }
109
+ }
@@ -0,0 +1,85 @@
1
+ import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'node:http2';
2
+
3
+ /** A metadata value: a string, or bytes for keys ending in `-bin`. */
4
+ export type MetadataValue = string | Uint8Array;
5
+
6
+ /** gRPC metadata: HTTP/2 headers and trailers with lowercase keys. */
7
+ export type Metadata = Record<string, MetadataValue | MetadataValue[]>;
8
+
9
+ /** Headers that carry the protocol, not metadata. */
10
+ const RESERVED = new Set(['content-type', 'te', 'grpc-timeout', 'grpc-encoding', 'grpc-accept-encoding', 'grpc-status', 'grpc-message', 'content-length']);
11
+ const KEY = /^[0-9a-z_.-]+$/;
12
+ /** Milliseconds per unit of `grpc-timeout`. */
13
+ const TIMEOUT_UNITS: Record<string, number> = { H: 3600000, M: 60000, S: 1000, m: 1, u: 0.001, n: 0.000001 };
14
+
15
+ export namespace MetadataUtil {
16
+ export function toHeaders(metadata: Metadata | undefined, headers: OutgoingHttpHeaders = {}): OutgoingHttpHeaders {
17
+ if (!metadata) {
18
+ return headers;
19
+ }
20
+ for (const [name, value] of Object.entries(metadata)) {
21
+ const key = name.toLowerCase();
22
+ if (!KEY.test(key) || key.startsWith('grpc-') || RESERVED.has(key)) {
23
+ throw new TypeError(`'${name}' is not a valid metadata key: use lowercase letters, digits, '_', '.' and '-', not reserved names.`);
24
+ }
25
+ const binary = key.endsWith('-bin');
26
+ const encode = (v: MetadataValue) => {
27
+ if (binary) {
28
+ return Buffer.from(typeof v === 'string' ? Buffer.from(v) : v).toString('base64');
29
+ }
30
+ if (typeof v !== 'string') {
31
+ throw new TypeError(`The metadata value of '${key}' is binary; binary keys end in '-bin'.`);
32
+ }
33
+ return v;
34
+ };
35
+ headers[key] = Array.isArray(value) ? value.map(encode) : encode(value);
36
+ }
37
+ return headers;
38
+ }
39
+
40
+ export function fromHeaders(headers: IncomingHttpHeaders): Metadata {
41
+ const metadata: Metadata = {};
42
+ for (const [key, value] of Object.entries(headers)) {
43
+ if (value === undefined || key.startsWith(':') || RESERVED.has(key)) {
44
+ continue;
45
+ }
46
+ if (key.endsWith('-bin')) {
47
+ // Several binary values may arrive joined by commas.
48
+ const values = (Array.isArray(value) ? value : String(value).split(',')).map(v => new Uint8Array(Buffer.from(v.trim(), 'base64')));
49
+ metadata[key] = values.length === 1 ? values[0] : values;
50
+ } else {
51
+ metadata[key] = Array.isArray(value) ? value : String(value);
52
+ }
53
+ }
54
+ return metadata;
55
+ }
56
+
57
+ /** `grpc-message` is percent-encoded UTF-8. */
58
+ export function encodeMessage(message: string): string {
59
+ return message.replace(/[^\x20-\x24\x26-\x7e]+/g, s => encodeURIComponent(s));
60
+ }
61
+
62
+ export function decodeMessage(value: unknown): string {
63
+ const text = Array.isArray(value) ? value.join(',') : String(value ?? '');
64
+ try {
65
+ return decodeURIComponent(text);
66
+ } catch {
67
+ return text;
68
+ }
69
+ }
70
+
71
+ /** Parses `grpc-timeout` (`100m`, `5S`...) to milliseconds. */
72
+ export function parseTimeout(value: unknown): number | undefined {
73
+ const match = /^(\d{1,8})([HMSmun])$/.exec(String(value ?? ''));
74
+ if (!match) {
75
+ return undefined;
76
+ }
77
+ const unit = TIMEOUT_UNITS[match[2]];
78
+ return Number(match[1]) * unit;
79
+ }
80
+
81
+ export function formatTimeout(milliseconds: number): string {
82
+ const ms = Math.max(0, Math.ceil(milliseconds));
83
+ return ms < 1e8 ? `${ms}m` : `${Math.min(99999999, Math.ceil(ms / 1000))}S`;
84
+ }
85
+ }