@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,477 @@
1
+ import type { EnumAst, FieldAst, MessageAst, MethodAst, ProtoFileAst, ServiceAst } from './proto-ast';
2
+
3
+ interface Token {
4
+ kind: 'ident' | 'number' | 'string' | 'symbol';
5
+ value: string;
6
+ line: number;
7
+ column: number;
8
+ }
9
+
10
+ const IDENT = /[A-Za-z_][\w.]*|\.[A-Za-z_][\w.]*/y;
11
+ const NUMBER = /[-+]?(?:0[xX][0-9a-fA-F]+|(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?)/y;
12
+ const ESCAPES: Record<string, string> = { n: '\n', r: '\r', t: '\t', v: '\v', b: '\b', f: '\f', a: '\x07', '\\': '\\', "'": "'", '"': '"', '?': '?' };
13
+ const MAX_FIELD_NUMBER = 536870911;
14
+
15
+ /**
16
+ * Parses the text of a .proto file (proto2 or proto3) into a {@link ProtoFileAst}: messages (nested,
17
+ * `oneof`, `map<K, V>`), enums and services. Options, `reserved`, `extensions` and `extend` blocks are
18
+ * read and skipped; groups and editions are rejected.
19
+ */
20
+ export class ProtoParser {
21
+ protected readonly tokens: Token[];
22
+ protected pos = 0;
23
+
24
+ constructor(
25
+ protected readonly source: string,
26
+ protected readonly fileName = '<source>',
27
+ ) {
28
+ this.tokens = this.tokenize();
29
+ }
30
+
31
+ static parse(source: string, fileName?: string): ProtoFileAst {
32
+ return new ProtoParser(source, fileName).parseFile();
33
+ }
34
+
35
+ parseFile(): ProtoFileAst {
36
+ const file: ProtoFileAst = { syntax: 'proto2', package: '', imports: [], messages: [], enums: [], services: [] };
37
+ while (this.pos < this.tokens.length) {
38
+ if (this.accept(';')) {
39
+ continue;
40
+ }
41
+ const keyword = this.ident();
42
+ switch (keyword) {
43
+ case 'syntax': {
44
+ this.expect('=');
45
+ const syntax = this.string();
46
+ if (syntax !== 'proto2' && syntax !== 'proto3') {
47
+ this.fail(`unsupported syntax "${syntax}"`, this.tokens[this.pos - 1]);
48
+ }
49
+ file.syntax = syntax;
50
+ this.expect(';');
51
+ break;
52
+ }
53
+ case 'edition':
54
+ this.fail('protobuf editions are not supported, use syntax = "proto3"', this.tokens[this.pos - 1]);
55
+ break;
56
+ case 'package':
57
+ file.package = this.ident();
58
+ this.expect(';');
59
+ break;
60
+ case 'import':
61
+ this.accept('public') || this.accept('weak');
62
+ file.imports.push(this.string());
63
+ this.expect(';');
64
+ break;
65
+ case 'option':
66
+ this.skipStatement();
67
+ break;
68
+ case 'message':
69
+ file.messages.push(this.parseMessage());
70
+ break;
71
+ case 'enum':
72
+ file.enums.push(this.parseEnum());
73
+ break;
74
+ case 'service':
75
+ file.services.push(this.parseService());
76
+ break;
77
+ case 'extend':
78
+ this.ident();
79
+ this.skipBlock();
80
+ break;
81
+ default:
82
+ this.fail(`unexpected '${keyword}'`, this.tokens[this.pos - 1]);
83
+ }
84
+ }
85
+ return file;
86
+ }
87
+
88
+ protected parseMessage(): MessageAst {
89
+ const message: MessageAst = { name: this.ident(), fields: [], oneofs: [], messages: [], enums: [] };
90
+ this.expect('{');
91
+ while (!this.accept('}')) {
92
+ if (this.accept(';')) {
93
+ continue;
94
+ }
95
+ const token = this.peek();
96
+ switch (token.value) {
97
+ case 'message':
98
+ this.pos++;
99
+ message.messages.push(this.parseMessage());
100
+ break;
101
+ case 'enum':
102
+ this.pos++;
103
+ message.enums.push(this.parseEnum());
104
+ break;
105
+ case 'option':
106
+ case 'reserved':
107
+ case 'extensions':
108
+ this.pos++;
109
+ this.skipStatement();
110
+ break;
111
+ case 'extend':
112
+ this.pos++;
113
+ this.ident();
114
+ this.skipBlock();
115
+ break;
116
+ case 'oneof': {
117
+ this.pos++;
118
+ const oneof = this.ident();
119
+ message.oneofs.push(oneof);
120
+ this.expect('{');
121
+ while (!this.accept('}')) {
122
+ if (this.accept(';')) {
123
+ continue;
124
+ }
125
+ if (this.accept('option')) {
126
+ this.skipStatement();
127
+ continue;
128
+ }
129
+ message.fields.push({ ...this.parseField(), oneof });
130
+ }
131
+ break;
132
+ }
133
+ default:
134
+ message.fields.push(this.parseField());
135
+ }
136
+ }
137
+ return message;
138
+ }
139
+
140
+ protected parseField(): FieldAst {
141
+ let rule: FieldAst['rule'];
142
+ const first = this.peek();
143
+ if ((first.value === 'repeated' || first.value === 'optional' || first.value === 'required') && this.peek(1).kind === 'ident') {
144
+ rule = first.value;
145
+ this.pos++;
146
+ }
147
+ const typeToken = this.peek();
148
+ let type = this.ident();
149
+ let keyType: string | undefined;
150
+ if (type === 'map' && this.accept('<')) {
151
+ keyType = this.ident();
152
+ this.expect(',');
153
+ type = this.ident();
154
+ this.expect('>');
155
+ } else if (type === 'group') {
156
+ this.fail('proto2 groups are not supported', typeToken);
157
+ }
158
+ const name = this.ident();
159
+ this.expect('=');
160
+ const numberToken = this.peek();
161
+ const number = this.integer();
162
+ if (number < 1 || number > MAX_FIELD_NUMBER) {
163
+ this.fail(`field number ${number} is out of range`, numberToken);
164
+ }
165
+ const field: FieldAst = { name, number, type };
166
+ if (rule) {
167
+ field.rule = rule;
168
+ }
169
+ if (keyType) {
170
+ field.keyType = keyType;
171
+ }
172
+ if (this.accept('[')) {
173
+ do {
174
+ const option = this.optionName();
175
+ this.expect('=');
176
+ const value = this.optionValue();
177
+ if (option === 'packed') {
178
+ field.packed = value === 'true';
179
+ }
180
+ } while (this.accept(','));
181
+ this.expect(']');
182
+ }
183
+ this.expect(';');
184
+ return field;
185
+ }
186
+
187
+ protected parseEnum(): EnumAst {
188
+ const result: EnumAst = { name: this.ident(), values: [] };
189
+ this.expect('{');
190
+ while (!this.accept('}')) {
191
+ if (this.accept(';')) {
192
+ continue;
193
+ }
194
+ if (this.accept('option') || this.accept('reserved')) {
195
+ this.skipStatement();
196
+ continue;
197
+ }
198
+ const name = this.ident();
199
+ this.expect('=');
200
+ result.values.push({ name, number: this.integer() });
201
+ if (this.accept('[')) {
202
+ this.skipUntil(']');
203
+ }
204
+ this.expect(';');
205
+ }
206
+ return result;
207
+ }
208
+
209
+ protected parseService(): ServiceAst {
210
+ const service: ServiceAst = { name: this.ident(), methods: [] };
211
+ this.expect('{');
212
+ while (!this.accept('}')) {
213
+ if (this.accept(';')) {
214
+ continue;
215
+ }
216
+ if (this.accept('option')) {
217
+ this.skipStatement();
218
+ continue;
219
+ }
220
+ const keyword = this.peek();
221
+ if (this.ident() !== 'rpc') {
222
+ this.fail(`expected 'rpc' but found '${keyword.value}'`, keyword);
223
+ }
224
+ service.methods.push(this.parseMethod());
225
+ }
226
+ return service;
227
+ }
228
+
229
+ protected parseMethod(): MethodAst {
230
+ const name = this.ident();
231
+ const [requestStream, requestType] = this.methodType();
232
+ const returns = this.peek();
233
+ if (this.ident() !== 'returns') {
234
+ this.fail(`expected 'returns' but found '${returns.value}'`, returns);
235
+ }
236
+ const [responseStream, responseType] = this.methodType();
237
+ if (this.accept('{')) {
238
+ this.pos--;
239
+ this.skipBlock();
240
+ } else {
241
+ this.expect(';');
242
+ }
243
+ return { name, requestType, requestStream, responseType, responseStream };
244
+ }
245
+
246
+ protected methodType(): [boolean, string] {
247
+ this.expect('(');
248
+ const stream = this.peek().value === 'stream' && this.peek(1).value !== ')';
249
+ if (stream) {
250
+ this.pos++;
251
+ }
252
+ const type = this.ident();
253
+ this.expect(')');
254
+ return [stream, type];
255
+ }
256
+
257
+ protected optionName(): string {
258
+ let name = '';
259
+ while (this.peek().value !== '=') {
260
+ name += this.next().value;
261
+ }
262
+ return name;
263
+ }
264
+
265
+ protected optionValue(): string {
266
+ const token = this.peek();
267
+ if (token.value === '{') {
268
+ this.skipBlock();
269
+ return '';
270
+ }
271
+ this.pos++;
272
+ if (token.kind === 'string') {
273
+ let value = token.value;
274
+ while (this.peek().kind === 'string') {
275
+ value += this.next().value;
276
+ }
277
+ return value;
278
+ }
279
+ if (token.value === '-' || token.value === '+') {
280
+ return token.value + this.next().value;
281
+ }
282
+ return token.value;
283
+ }
284
+
285
+ /** Skips the rest of an `option`, `reserved` or `extensions` statement, aggregate values included. */
286
+ protected skipStatement(): void {
287
+ let depth = 0;
288
+ for (;;) {
289
+ const token = this.next();
290
+ if (token.kind !== 'symbol') {
291
+ continue;
292
+ }
293
+ if (token.value === '{') {
294
+ depth++;
295
+ } else if (token.value === '}') {
296
+ depth--;
297
+ } else if (token.value === ';' && depth === 0) {
298
+ return;
299
+ }
300
+ }
301
+ }
302
+
303
+ /** Skips a `{ ... }` block, nested blocks included. */
304
+ protected skipBlock(): void {
305
+ this.expect('{');
306
+ let depth = 1;
307
+ while (depth > 0) {
308
+ const token = this.next();
309
+ if (token.kind === 'symbol') {
310
+ depth += token.value === '{' ? 1 : token.value === '}' ? -1 : 0;
311
+ }
312
+ }
313
+ }
314
+
315
+ protected skipUntil(symbol: string): void {
316
+ while (this.next().value !== symbol) {
317
+ // skip
318
+ }
319
+ }
320
+
321
+ protected ident(): string {
322
+ const token = this.next();
323
+ if (token.kind !== 'ident') {
324
+ this.fail(`expected a name but found '${token.value}'`, token);
325
+ }
326
+ return token.value;
327
+ }
328
+
329
+ protected string(): string {
330
+ const token = this.next();
331
+ if (token.kind !== 'string') {
332
+ this.fail(`expected a string but found '${token.value}'`, token);
333
+ }
334
+ let value = token.value;
335
+ while (this.peek().kind === 'string') {
336
+ value += this.next().value;
337
+ }
338
+ return value;
339
+ }
340
+
341
+ protected integer(): number {
342
+ const token = this.next();
343
+ const value =
344
+ token.kind === 'number' ? Number(token.value.replace(/^([-+]?)0([0-7]+)$/, (_, sign, octal) => sign + Number.parseInt(octal, 8))) : Number.NaN;
345
+ if (!Number.isInteger(value)) {
346
+ this.fail(`expected an integer but found '${token.value}'`, token);
347
+ }
348
+ return value;
349
+ }
350
+
351
+ protected accept(value: string): boolean {
352
+ const token = this.tokens[this.pos];
353
+ if (token && token.value === value && token.kind !== 'string') {
354
+ this.pos++;
355
+ return true;
356
+ }
357
+ return false;
358
+ }
359
+
360
+ protected expect(symbol: string): void {
361
+ const token = this.next();
362
+ if (token.value !== symbol || token.kind === 'string') {
363
+ this.fail(`expected '${symbol}' but found '${token.value}'`, token);
364
+ }
365
+ }
366
+
367
+ protected peek(offset = 0): Token {
368
+ return this.tokens[this.pos + offset] ?? this.endToken();
369
+ }
370
+
371
+ protected next(): Token {
372
+ const token = this.tokens[this.pos++];
373
+ if (!token) {
374
+ this.fail('unexpected end of file', this.endToken());
375
+ }
376
+ return token;
377
+ }
378
+
379
+ protected endToken(): Token {
380
+ const lines = this.source.split('\n');
381
+ return { kind: 'symbol', value: '<end of file>', line: lines.length, column: lines[lines.length - 1].length + 1 };
382
+ }
383
+
384
+ protected fail(message: string, token: Token): never {
385
+ throw new SyntaxError(`${this.fileName}:${token.line}:${token.column}: ${message}`);
386
+ }
387
+
388
+ protected tokenize(): Token[] {
389
+ const { source } = this;
390
+ const tokens: Token[] = [];
391
+ let line = 1;
392
+ let lineStart = 0;
393
+ let i = 0;
394
+ while (i < source.length) {
395
+ const c = source[i];
396
+ if (c === '\n') {
397
+ line++;
398
+ lineStart = ++i;
399
+ continue;
400
+ }
401
+ if (c === ' ' || c === '\t' || c === '\r' || c === '\f' || c === '\v') {
402
+ i++;
403
+ continue;
404
+ }
405
+ const column = i - lineStart + 1;
406
+ if (c === '/' && source[i + 1] === '/') {
407
+ while (i < source.length && source[i] !== '\n') {
408
+ i++;
409
+ }
410
+ continue;
411
+ }
412
+ if (c === '/' && source[i + 1] === '*') {
413
+ const end = source.indexOf('*/', i + 2);
414
+ if (end < 0) {
415
+ this.fail('unterminated comment', { kind: 'symbol', value: '/*', line, column });
416
+ }
417
+ for (let j = i; j < end; j++) {
418
+ if (source[j] === '\n') {
419
+ line++;
420
+ lineStart = j + 1;
421
+ }
422
+ }
423
+ i = end + 2;
424
+ continue;
425
+ }
426
+ if (c === '"' || c === "'") {
427
+ let value = '';
428
+ let j = i + 1;
429
+ while (source[j] !== c) {
430
+ if (j >= source.length || source[j] === '\n') {
431
+ this.fail('unterminated string', { kind: 'symbol', value: c, line, column });
432
+ }
433
+ if (source[j] === '\\') {
434
+ const e = source[j + 1];
435
+ if (e === 'x' || e === 'X') {
436
+ const hex = /^[0-9a-fA-F]{1,2}/.exec(source.slice(j + 2, j + 4))?.[0];
437
+ if (!hex) {
438
+ this.fail('invalid \\x escape', { kind: 'symbol', value: '\\x', line, column });
439
+ }
440
+ value += String.fromCharCode(Number.parseInt(hex, 16));
441
+ j += 2 + hex.length;
442
+ } else if (e >= '0' && e <= '7') {
443
+ const octal = /^[0-7]{1,3}/.exec(source.slice(j + 1, j + 4))?.[0] ?? e;
444
+ value += String.fromCharCode(Number.parseInt(octal, 8));
445
+ j += 1 + octal.length;
446
+ } else {
447
+ value += ESCAPES[e] ?? e;
448
+ j += 2;
449
+ }
450
+ } else {
451
+ value += source[j++];
452
+ }
453
+ }
454
+ tokens.push({ kind: 'string', value, line, column });
455
+ i = j + 1;
456
+ continue;
457
+ }
458
+ IDENT.lastIndex = i;
459
+ const ident = IDENT.exec(source);
460
+ if (ident) {
461
+ tokens.push({ kind: 'ident', value: ident[0], line, column });
462
+ i += ident[0].length;
463
+ continue;
464
+ }
465
+ NUMBER.lastIndex = i;
466
+ const number = NUMBER.exec(source);
467
+ if (number) {
468
+ tokens.push({ kind: 'number', value: number[0], line, column });
469
+ i += number[0].length;
470
+ continue;
471
+ }
472
+ tokens.push({ kind: 'symbol', value: c, line, column });
473
+ i++;
474
+ }
475
+ return tokens;
476
+ }
477
+ }
@@ -0,0 +1,128 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import { ProtoWriter } from '../codec/proto-writer';
3
+ import { loadTestProto } from '../test/proto-loader';
4
+ import { TEST_PROTO } from '../test/test-protos';
5
+ import { Proto } from './proto';
6
+
7
+ const proto = Proto.parse(TEST_PROTO);
8
+ const Everything = proto.message('Everything');
9
+ const Scalars = proto.message('Scalars');
10
+ const reference = loadTestProto();
11
+ const echo = (reference['cell.test.Greeter'] as any).Echo;
12
+
13
+ const SCALARS = {
14
+ d: 1.5,
15
+ f: -2.25,
16
+ i32: -42,
17
+ i64: '-9007199254740993',
18
+ u32: 4294967295,
19
+ u64: '18446744073709551615',
20
+ s32: -2147483648,
21
+ s64: '-9223372036854775808',
22
+ fx32: 4000000000,
23
+ fx64: '18446744073709551614',
24
+ sfx32: -7,
25
+ sfx64: '-9223372036854775807',
26
+ flag: true,
27
+ text: 'héllo 🌍',
28
+ data: new Uint8Array([0, 1, 255]),
29
+ color: 'RED',
30
+ };
31
+
32
+ const EVERYTHING = {
33
+ scalars: SCALARS,
34
+ numbers: [1, -1, 300, 2147483647],
35
+ names: ['a', '', 'c'],
36
+ items: [{ ...SCALARS, text: 'x' }, { i32: 1 }],
37
+ counts: { a: 1, b: 0 },
38
+ byId: { '-5': { text: 'minus five' }, 12345678901234: { flag: true } },
39
+ nested: { text: 'in oneof' },
40
+ maybe: 0,
41
+ at: { seconds: '1700000000', nanos: 5 },
42
+ colors: ['RED', 'GREEN', 'COLOR_UNSPECIFIED'],
43
+ inner: { value: 'inner' },
44
+ snakeCaseName: 'snake',
45
+ };
46
+
47
+ describe('MessageType', () => {
48
+ it('round-trips every field kind', () => {
49
+ const decoded = Everything.decode(Everything.encode(EVERYTHING));
50
+ expect(decoded.scalars).toEqual(SCALARS);
51
+ expect(decoded.numbers).toEqual(EVERYTHING.numbers);
52
+ expect(decoded.items[1]).toEqual({ ...Scalars.create(), i32: 1 });
53
+ expect(decoded.counts).toEqual({ a: 1, b: 0 });
54
+ expect(Object.keys(decoded.byId).sort()).toEqual(['-5', '12345678901234']);
55
+ expect(decoded.byId['-5'].text).toBe('minus five');
56
+ expect(decoded.choice).toBe('nested');
57
+ expect(decoded.nested.text).toBe('in oneof');
58
+ expect(decoded.label).toBeUndefined();
59
+ expect(decoded.maybe).toBe(0);
60
+ expect(decoded.at).toEqual({ seconds: '1700000000', nanos: 5 });
61
+ expect(decoded.colors).toEqual(EVERYTHING.colors);
62
+ expect(decoded.snakeCaseName).toBe('snake');
63
+ });
64
+
65
+ it('decodes what protobuf.js encodes, and the other way round', () => {
66
+ const ours = Everything.decode(echo.requestSerialize(EVERYTHING));
67
+ expect(ours.scalars).toEqual(SCALARS);
68
+ expect(ours.byId['12345678901234'].flag).toBe(true);
69
+ expect(ours.nested.text).toBe('in oneof');
70
+ expect(ours.colors).toEqual(EVERYTHING.colors);
71
+
72
+ const theirs = echo.requestDeserialize(Buffer.from(Everything.encode(EVERYTHING)));
73
+ expect({ ...theirs.scalars, data: [...theirs.scalars.data] }).toEqual({ ...SCALARS, data: [0, 1, 255] });
74
+ expect(theirs.numbers).toEqual(EVERYTHING.numbers);
75
+ expect(theirs.names).toEqual(EVERYTHING.names);
76
+ expect(theirs.counts).toEqual({ a: 1, b: 0 });
77
+ // protobuf.js decodes 64-bit map keys to hash strings; compare the bytes instead.
78
+ const byId = { byId: { '-5': { text: 'a' }, 7: { text: 'b' } } };
79
+ expect(Buffer.from(Everything.encode(byId)).toString('hex')).toBe(Buffer.from(echo.requestSerialize(byId)).toString('hex'));
80
+ expect(theirs.choice).toBe('nested');
81
+ expect(theirs.maybe).toBe(0);
82
+ expect(theirs.at).toEqual({ seconds: '1700000000', nanos: 5 });
83
+ expect(theirs.colors).toEqual(EVERYTHING.colors);
84
+ expect(theirs.inner).toEqual({ value: 'inner' });
85
+ expect(theirs.snakeCaseName).toBe('snake');
86
+ });
87
+
88
+ it('fills defaults and leaves fields with explicit presence out', () => {
89
+ const empty = Everything.decode(new Uint8Array(0));
90
+ expect(empty).toEqual({ numbers: [], names: [], items: [], counts: {}, byId: {}, colors: [], snakeCaseName: '' });
91
+ const scalars = Scalars.decode(new Uint8Array(0));
92
+ expect(scalars.i64).toBe('0');
93
+ expect(scalars.color).toBe('COLOR_UNSPECIFIED');
94
+ expect(scalars.data).toEqual(new Uint8Array(0));
95
+ expect(Scalars.encode(scalars).length).toBe(0);
96
+ });
97
+
98
+ it('accepts proto field names, bigints, numbers for 64-bit fields, enum numbers and base64 bytes', () => {
99
+ const decoded = Everything.decode(Everything.encode({ snake_case_name: 'x', scalars: { i64: BigInt(-3), u64: 7, color: 2, data: 'AAH/' } }));
100
+ expect(decoded.snakeCaseName).toBe('x');
101
+ expect(decoded.scalars.i64).toBe('-3');
102
+ expect(decoded.scalars.u64).toBe('7');
103
+ expect(decoded.scalars.color).toBe('GREEN');
104
+ expect([...decoded.scalars.data]).toEqual([0, 1, 255]);
105
+ });
106
+
107
+ it('writes only the member a oneof names', () => {
108
+ const decoded = Everything.decode(Everything.encode({ choice: 'code', label: 'ignored', code: 3 }));
109
+ expect([decoded.choice, decoded.code, decoded.label]).toEqual(['code', 3, undefined]);
110
+ });
111
+
112
+ it('names the field of invalid values', () => {
113
+ expect(() => Everything.encode({ scalars: { i64: 1.5 } })).toThrow('cell.test.Everything.scalars: cell.test.Scalars.i64: 1.5 is not an integer');
114
+ expect(() => Everything.encode({ colors: ['BLUE'] })).toThrow("cell.test.Everything.colors: 'BLUE' is not a value of enum cell.test.Color");
115
+ expect(() => Everything.encode({ numbers: 3 } as any)).toThrow('cell.test.Everything.numbers: expected an array');
116
+ expect(() => Scalars.decode(new Uint8Array([0x0a, 0x05, 0x01]))).toThrow(RangeError);
117
+ });
118
+
119
+ it('moves long length-delimited values to make room for their length', () => {
120
+ for (const length of [127, 128, 16383, 16384, 70000]) {
121
+ const text = 'x'.repeat(length);
122
+ expect(Everything.decode(Everything.encode({ items: [{ text }] })).items[0].text).toBe(text);
123
+ }
124
+ const writer = new ProtoWriter(1);
125
+ writer.string('é'.repeat(100));
126
+ expect(writer.finish().length).toBe(202);
127
+ });
128
+ });