protons 7.7.0 → 8.0.1

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 (60) hide show
  1. package/README.md +5 -5
  2. package/dist/bin/protons.js +1 -1
  3. package/dist/src/fields/array-field.d.ts +18 -0
  4. package/dist/src/fields/array-field.d.ts.map +1 -0
  5. package/dist/src/fields/array-field.js +83 -0
  6. package/dist/src/fields/array-field.js.map +1 -0
  7. package/dist/src/fields/enum-field.d.ts +9 -0
  8. package/dist/src/fields/enum-field.d.ts.map +1 -0
  9. package/dist/src/fields/enum-field.js +21 -0
  10. package/dist/src/fields/enum-field.js.map +1 -0
  11. package/dist/src/fields/field.d.ts +45 -0
  12. package/dist/src/fields/field.d.ts.map +1 -0
  13. package/dist/src/fields/field.js +147 -0
  14. package/dist/src/fields/field.js.map +1 -0
  15. package/dist/src/fields/map-field.d.ts +22 -0
  16. package/dist/src/fields/map-field.d.ts.map +1 -0
  17. package/dist/src/fields/map-field.js +83 -0
  18. package/dist/src/fields/map-field.js.map +1 -0
  19. package/dist/src/fields/message-field.d.ts +9 -0
  20. package/dist/src/fields/message-field.d.ts.map +1 -0
  21. package/dist/src/fields/message-field.js +23 -0
  22. package/dist/src/fields/message-field.js.map +1 -0
  23. package/dist/src/index.d.ts +190 -16
  24. package/dist/src/index.d.ts.map +1 -1
  25. package/dist/src/index.js +5 -963
  26. package/dist/src/index.js.map +1 -1
  27. package/dist/src/types/enum.d.ts +21 -0
  28. package/dist/src/types/enum.d.ts.map +1 -0
  29. package/dist/src/types/enum.js +87 -0
  30. package/dist/src/types/enum.js.map +1 -0
  31. package/dist/src/types/index.d.ts +20 -0
  32. package/dist/src/types/index.d.ts.map +1 -0
  33. package/dist/src/types/index.js +2 -0
  34. package/dist/src/types/index.js.map +1 -0
  35. package/dist/src/types/message.d.ts +49 -0
  36. package/dist/src/types/message.d.ts.map +1 -0
  37. package/dist/src/types/message.js +478 -0
  38. package/dist/src/types/message.js.map +1 -0
  39. package/dist/src/types/module.d.ts +30 -0
  40. package/dist/src/types/module.d.ts.map +1 -0
  41. package/dist/src/types/module.js +184 -0
  42. package/dist/src/types/module.js.map +1 -0
  43. package/dist/src/types/primitive.d.ts +13 -0
  44. package/dist/src/types/primitive.d.ts.map +1 -0
  45. package/dist/src/types/primitive.js +174 -0
  46. package/dist/src/types/primitive.js.map +1 -0
  47. package/dist/typedoc-urls.json +2 -4
  48. package/package.json +103 -16
  49. package/src/fields/array-field.ts +109 -0
  50. package/src/fields/enum-field.ts +30 -0
  51. package/src/fields/field.ts +201 -0
  52. package/src/fields/map-field.ts +107 -0
  53. package/src/fields/message-field.ts +29 -0
  54. package/src/index.ts +6 -1202
  55. package/src/types/enum.ts +112 -0
  56. package/src/types/index.ts +21 -0
  57. package/src/types/message.ts +558 -0
  58. package/src/types/module.ts +234 -0
  59. package/src/types/primitive.ts +215 -0
  60. package/LICENSE +0 -4
package/dist/src/index.js CHANGED
@@ -1,4 +1,3 @@
1
- /* eslint-disable max-depth */
2
1
  /**
3
2
  * @packageDocumentation
4
3
  *
@@ -32,7 +31,7 @@
32
31
  * In your code import the generated classes and use them to transform to/from bytes:
33
32
  *
34
33
  * ```js
35
- * import { Foo } from './foo.js'
34
+ * import { Foo } from './foo.ts'
36
35
  *
37
36
  * const foo = {
38
37
  * message: 'hello world'
@@ -47,7 +46,7 @@
47
46
  *
48
47
  * ## Differences from protobuf.js
49
48
  *
50
- * This module uses the internal reader/writer from `protobuf.js` as it is highly optimised and there's no point reinventing the wheel.
49
+ * This module uses the internal reader/writer from `protobuf.js` as it is highly optimized and there's no point reinventing the wheel.
51
50
  *
52
51
  * It does have one or two differences:
53
52
  *
@@ -195,815 +194,11 @@ import fs from 'fs/promises';
195
194
  import path from 'path';
196
195
  import { promisify } from 'util';
197
196
  import { main as pbjs } from 'protobufjs-cli/pbjs.js';
198
- import { NoMessagesFoundError, ParseError } from 'protons-runtime';
199
- export var CODEC_TYPES;
200
- (function (CODEC_TYPES) {
201
- CODEC_TYPES[CODEC_TYPES["VARINT"] = 0] = "VARINT";
202
- CODEC_TYPES[CODEC_TYPES["BIT64"] = 1] = "BIT64";
203
- CODEC_TYPES[CODEC_TYPES["LENGTH_DELIMITED"] = 2] = "LENGTH_DELIMITED";
204
- CODEC_TYPES[CODEC_TYPES["START_GROUP"] = 3] = "START_GROUP";
205
- CODEC_TYPES[CODEC_TYPES["END_GROUP"] = 4] = "END_GROUP";
206
- CODEC_TYPES[CODEC_TYPES["BIT32"] = 5] = "BIT32";
207
- })(CODEC_TYPES || (CODEC_TYPES = {}));
197
+ import { Module } from "./types/module.js";
208
198
  function pathWithExtension(input, extension, outputDir) {
209
199
  const output = outputDir ?? path.dirname(input);
210
200
  return path.join(output, path.basename(input).split('.').slice(0, -1).join('.') + extension);
211
201
  }
212
- /**
213
- * This will be removed in a future release
214
- *
215
- * @deprecated
216
- */
217
- export class CodeError extends Error {
218
- code;
219
- constructor(message, code, options) {
220
- super(message, options);
221
- this.code = code;
222
- }
223
- }
224
- const types = {
225
- bool: 'boolean',
226
- bytes: 'Uint8Array',
227
- double: 'number',
228
- fixed32: 'number',
229
- fixed64: 'bigint',
230
- float: 'number',
231
- int32: 'number',
232
- int64: 'bigint',
233
- sfixed32: 'number',
234
- sfixed64: 'bigint',
235
- sint32: 'number',
236
- sint64: 'bigint',
237
- string: 'string',
238
- uint32: 'number',
239
- uint64: 'bigint'
240
- };
241
- const jsTypeOverrides = {
242
- JS_NUMBER: 'number',
243
- JS_STRING: 'string'
244
- };
245
- const encoderGenerators = {
246
- bool: (val) => `w.bool(${val})`,
247
- bytes: (val) => `w.bytes(${val})`,
248
- double: (val) => `w.double(${val})`,
249
- fixed32: (val) => `w.fixed32(${val})`,
250
- fixed64: (val, jsTypeOverride) => {
251
- if (jsTypeOverride === 'number') {
252
- return `w.fixed64Number(${val})`;
253
- }
254
- if (jsTypeOverride === 'string') {
255
- return `w.fixed64String(${val})`;
256
- }
257
- return `w.fixed64(${val})`;
258
- },
259
- float: (val) => `w.float(${val})`,
260
- int32: (val) => `w.int32(${val})`,
261
- int64: (val, jsTypeOverride) => {
262
- if (jsTypeOverride === 'number') {
263
- return `w.int64Number(${val})`;
264
- }
265
- if (jsTypeOverride === 'string') {
266
- return `w.int64String(${val})`;
267
- }
268
- return `w.int64(${val})`;
269
- },
270
- sfixed32: (val) => `w.sfixed32(${val})`,
271
- sfixed64: (val, jsTypeOverride) => {
272
- if (jsTypeOverride === 'number') {
273
- return `w.sfixed64Number(${val})`;
274
- }
275
- if (jsTypeOverride === 'string') {
276
- return `w.sfixed64String(${val})`;
277
- }
278
- return `w.sfixed64(${val})`;
279
- },
280
- sint32: (val) => `w.sint32(${val})`,
281
- sint64: (val, jsTypeOverride) => {
282
- if (jsTypeOverride === 'number') {
283
- return `w.sint64Number(${val})`;
284
- }
285
- if (jsTypeOverride === 'string') {
286
- return `w.sint64String(${val})`;
287
- }
288
- return `w.sint64(${val})`;
289
- },
290
- string: (val) => `w.string(${val})`,
291
- uint32: (val) => `w.uint32(${val})`,
292
- uint64: (val, jsTypeOverride) => {
293
- if (jsTypeOverride === 'number') {
294
- return `w.uint64Number(${val})`;
295
- }
296
- if (jsTypeOverride === 'string') {
297
- return `w.uint64String(${val})`;
298
- }
299
- return `w.uint64(${val})`;
300
- }
301
- };
302
- const decoderGenerators = {
303
- bool: () => 'reader.bool()',
304
- bytes: () => 'reader.bytes()',
305
- double: () => 'reader.double()',
306
- fixed32: () => 'reader.fixed32()',
307
- fixed64: (jsTypeOverride) => {
308
- if (jsTypeOverride === 'number') {
309
- return 'reader.fixed64Number()';
310
- }
311
- if (jsTypeOverride === 'string') {
312
- return 'reader.fixed64String()';
313
- }
314
- return 'reader.fixed64()';
315
- },
316
- float: () => 'reader.float()',
317
- int32: () => 'reader.int32()',
318
- int64: (jsTypeOverride) => {
319
- if (jsTypeOverride === 'number') {
320
- return 'reader.int64Number()';
321
- }
322
- if (jsTypeOverride === 'string') {
323
- return 'reader.int64String()';
324
- }
325
- return 'reader.int64()';
326
- },
327
- sfixed32: () => 'reader.sfixed32()',
328
- sfixed64: (jsTypeOverride) => {
329
- if (jsTypeOverride === 'number') {
330
- return 'reader.sfixed64Number()';
331
- }
332
- if (jsTypeOverride === 'string') {
333
- return 'reader.sfixed64String()';
334
- }
335
- return 'reader.sfixed64()';
336
- },
337
- sint32: () => 'reader.sint32()',
338
- sint64: (jsTypeOverride) => {
339
- if (jsTypeOverride === 'number') {
340
- return 'reader.sint64Number()';
341
- }
342
- if (jsTypeOverride === 'string') {
343
- return 'reader.sint64String()';
344
- }
345
- return 'reader.sint64()';
346
- },
347
- string: () => 'reader.string()',
348
- uint32: () => 'reader.uint32()',
349
- uint64: (jsTypeOverride) => {
350
- if (jsTypeOverride === 'number') {
351
- return 'reader.uint64Number()';
352
- }
353
- if (jsTypeOverride === 'string') {
354
- return 'reader.uint64String()';
355
- }
356
- return 'reader.uint64()';
357
- }
358
- };
359
- const defaultValueGenerators = {
360
- bool: () => 'false',
361
- bytes: () => 'uint8ArrayAlloc(0)',
362
- double: () => '0',
363
- fixed32: () => '0',
364
- fixed64: () => '0n',
365
- float: () => '0',
366
- int32: () => '0',
367
- int64: () => '0n',
368
- sfixed32: () => '0',
369
- sfixed64: () => '0n',
370
- sint32: () => '0',
371
- sint64: () => '0n',
372
- string: () => "''",
373
- uint32: () => '0',
374
- uint64: () => '0n'
375
- };
376
- const defaultValueGeneratorsJsTypeOverrides = {
377
- number: () => '0',
378
- string: () => "''"
379
- };
380
- const defaultValueTestGenerators = {
381
- bool: (field) => `(${field} != null && ${field} !== false)`,
382
- bytes: (field) => `(${field} != null && ${field}.byteLength > 0)`,
383
- double: (field) => `(${field} != null && ${field} !== 0)`,
384
- fixed32: (field) => `(${field} != null && ${field} !== 0)`,
385
- fixed64: (field) => `(${field} != null && ${field} !== 0n)`,
386
- float: (field) => `(${field} != null && ${field} !== 0)`,
387
- int32: (field) => `(${field} != null && ${field} !== 0)`,
388
- int64: (field) => `(${field} != null && ${field} !== 0n)`,
389
- sfixed32: (field) => `(${field} != null && ${field} !== 0)`,
390
- sfixed64: (field) => `(${field} != null && ${field} !== 0n)`,
391
- sint32: (field) => `(${field} != null && ${field} !== 0)`,
392
- sint64: (field) => `(${field} != null && ${field} !== 0n)`,
393
- string: (field) => `(${field} != null && ${field} !== '')`,
394
- uint32: (field) => `(${field} != null && ${field} !== 0)`,
395
- uint64: (field) => `(${field} != null && ${field} !== 0n)`
396
- };
397
- const defaultValueTestGeneratorsJsTypeOverrides = {
398
- number: (field) => `(${field} != null && ${field} !== 0)`,
399
- string: (field) => `(${field} != null && ${field} !== '')`
400
- };
401
- function findJsTypeOverride(defaultType, fieldDef) {
402
- if (fieldDef.options?.jstype != null && jsTypeOverrides[fieldDef.options?.jstype] != null) {
403
- if (!['int64', 'uint64', 'sint64', 'fixed64', 'sfixed64'].includes(defaultType)) {
404
- throw new Error(`jstype is only allowed on int64, uint64, sint64, fixed64 or sfixed64 fields - got "${defaultType}"`);
405
- }
406
- return jsTypeOverrides[fieldDef.options?.jstype];
407
- }
408
- }
409
- function findJsTypeName(typeName, classDef, moduleDef, fieldDef) {
410
- const override = findJsTypeOverride(typeName, fieldDef);
411
- if (override != null) {
412
- return override;
413
- }
414
- if (types[typeName] != null) {
415
- return types[typeName];
416
- }
417
- if (isEnumDef(classDef)) {
418
- throw new Error('Could not find type in enum');
419
- }
420
- if (classDef.nested?.[typeName] != null) {
421
- return `${classDef.fullName}.${typeName}`;
422
- }
423
- if (classDef.parent != null) {
424
- return findJsTypeName(typeName, classDef.parent, moduleDef, fieldDef);
425
- }
426
- if (moduleDef.globals[typeName] != null) {
427
- return typeName;
428
- }
429
- throw new Error(`Could not resolve type name "${typeName}"`);
430
- }
431
- function findDef(typeName, classDef, moduleDef) {
432
- if (isEnumDef(classDef)) {
433
- throw new Error('Could not find type in enum');
434
- }
435
- if (classDef.nested?.[typeName] != null) {
436
- return classDef.nested?.[typeName];
437
- }
438
- if (classDef.parent != null) {
439
- return findDef(typeName, classDef.parent, moduleDef);
440
- }
441
- if (moduleDef.globals[typeName] != null) {
442
- return moduleDef.globals[typeName];
443
- }
444
- throw new Error(`Could not resolve type name "${typeName}"`);
445
- }
446
- function createDefaultObject(fields, messageDef, moduleDef) {
447
- const output = Object.entries(fields)
448
- .map(([name, fieldDef]) => {
449
- if (fieldDef.map) {
450
- return `${name}: new Map<${types[fieldDef.keyType ?? 'string']}, ${types[fieldDef.valueType]}>()`;
451
- }
452
- if (fieldDef.repeated) {
453
- return `${name}: []`;
454
- }
455
- if (fieldDef.optional) {
456
- return '';
457
- }
458
- const type = fieldDef.type;
459
- let defaultValue;
460
- let defaultValueGenerator = defaultValueGenerators[type];
461
- if (defaultValueGenerator != null) {
462
- const jsTypeOverride = findJsTypeOverride(type, fieldDef);
463
- if (jsTypeOverride != null && defaultValueGeneratorsJsTypeOverrides[jsTypeOverride] != null) {
464
- defaultValueGenerator = defaultValueGeneratorsJsTypeOverrides[jsTypeOverride];
465
- }
466
- if (type === 'bytes') {
467
- moduleDef.addImport('uint8arrays/alloc', 'alloc', 'uint8ArrayAlloc');
468
- }
469
- defaultValue = defaultValueGenerator();
470
- }
471
- else {
472
- const def = findDef(fieldDef.type, messageDef, moduleDef);
473
- if (isEnumDef(def)) {
474
- // select lowest-value enum - should be 0 but it's not guaranteed
475
- const val = Object.entries(def.values)
476
- .sort((a, b) => {
477
- if (a[1] < b[1]) {
478
- return 1;
479
- }
480
- if (a[1] > b[1]) {
481
- return -1;
482
- }
483
- return 0;
484
- })
485
- .pop();
486
- if (val == null) {
487
- throw new Error(`Could not find default enum value for ${def.fullName}`);
488
- }
489
- defaultValue = `${def.name}.${val[0]}`;
490
- }
491
- else {
492
- defaultValue = 'undefined';
493
- }
494
- }
495
- return `${name}: ${defaultValue}`;
496
- })
497
- .filter(Boolean)
498
- .join(',\n ');
499
- if (output !== '') {
500
- return `
501
- ${output}
502
- `;
503
- }
504
- return '';
505
- }
506
- const encoders = {
507
- bool: 'bool',
508
- bytes: 'bytes',
509
- double: 'double',
510
- fixed32: 'fixed32',
511
- fixed64: 'fixed64',
512
- float: 'float',
513
- int32: 'int32',
514
- int64: 'int64',
515
- sfixed32: 'sfixed32',
516
- sfixed64: 'sfixed64',
517
- sint32: 'sint32',
518
- sint64: 'sint64',
519
- string: 'string',
520
- uint32: 'uint32',
521
- uint64: 'uint64'
522
- };
523
- const codecTypes = {
524
- bool: CODEC_TYPES.VARINT,
525
- bytes: CODEC_TYPES.LENGTH_DELIMITED,
526
- double: CODEC_TYPES.BIT64,
527
- enum: CODEC_TYPES.VARINT,
528
- fixed32: CODEC_TYPES.BIT32,
529
- fixed64: CODEC_TYPES.BIT64,
530
- float: CODEC_TYPES.BIT32,
531
- int32: CODEC_TYPES.VARINT,
532
- int64: CODEC_TYPES.VARINT,
533
- message: CODEC_TYPES.LENGTH_DELIMITED,
534
- sfixed32: CODEC_TYPES.BIT32,
535
- sfixed64: CODEC_TYPES.BIT64,
536
- sint32: CODEC_TYPES.VARINT,
537
- sint64: CODEC_TYPES.VARINT,
538
- string: CODEC_TYPES.LENGTH_DELIMITED,
539
- uint32: CODEC_TYPES.VARINT,
540
- uint64: CODEC_TYPES.VARINT
541
- };
542
- function isEnumDef(obj) {
543
- return obj.values != null;
544
- }
545
- function defineFields(fields, messageDef, moduleDef) {
546
- return Object.entries(fields).map(([fieldName, fieldDef]) => {
547
- if (fieldDef.map) {
548
- return `${fieldName}: Map<${findJsTypeName(fieldDef.keyType ?? 'string', messageDef, moduleDef, fieldDef)}, ${findJsTypeName(fieldDef.valueType, messageDef, moduleDef, fieldDef)}>`;
549
- }
550
- return `${fieldName}${fieldDef.optional ? '?' : ''}: ${findJsTypeName(fieldDef.type, messageDef, moduleDef, fieldDef)}${fieldDef.repeated ? '[]' : ''}`;
551
- });
552
- }
553
- function compileMessage(messageDef, moduleDef, flags) {
554
- if (isEnumDef(messageDef)) {
555
- moduleDef.addImport('protons-runtime', 'enumeration');
556
- // check that the enum def values start from 0
557
- if (Object.values(messageDef.values)[0] !== 0) {
558
- const message = `enum ${messageDef.name} does not contain a value that maps to zero as it's first element, this is required in proto3 - see https://protobuf.dev/programming-guides/proto3/#enum`;
559
- if (flags?.strict === true) {
560
- throw new ParseError(message);
561
- }
562
- else {
563
- // eslint-disable-next-line no-console
564
- console.info(`[WARN] ${message}`);
565
- }
566
- }
567
- return `
568
- export enum ${messageDef.name} {
569
- ${Object.keys(messageDef.values).map(name => {
570
- return `${name} = '${name}'`;
571
- }).join(',\n ').trim()}
572
- }
573
-
574
- enum __${messageDef.name}Values {
575
- ${Object.entries(messageDef.values).map(([name, value]) => {
576
- return `${name} = ${value}`;
577
- }).join(',\n ').trim()}
578
- }
579
-
580
- export namespace ${messageDef.name} {
581
- export const codec = (): Codec<${messageDef.name}> => {
582
- return enumeration<${messageDef.name}>(__${messageDef.name}Values)
583
- }
584
- }
585
- `.trimStart();
586
- }
587
- let nested = '';
588
- if (messageDef.nested != null) {
589
- nested = '\n';
590
- nested += Object.values(messageDef.nested)
591
- .map(def => compileMessage(def, moduleDef, flags).trim())
592
- .join('\n\n')
593
- .split('\n')
594
- .map(line => line.trim() === '' ? '' : ` ${line}`)
595
- .join('\n');
596
- }
597
- const fields = messageDef.fields ?? {};
598
- // import relevant modules
599
- moduleDef.addImport('protons-runtime', 'encodeMessage');
600
- moduleDef.addImport('protons-runtime', 'decodeMessage');
601
- moduleDef.addImport('protons-runtime', 'message');
602
- moduleDef.addTypeImport('protons-runtime', 'Codec');
603
- moduleDef.addTypeImport('protons-runtime', 'DecodeOptions');
604
- moduleDef.addTypeImport('uint8arraylist', 'Uint8ArrayList');
605
- const interfaceFields = defineFields(fields, messageDef, moduleDef)
606
- .join('\n ')
607
- .trim();
608
- let interfaceDef = '';
609
- let interfaceCodecDef = '';
610
- if (interfaceFields === '') {
611
- interfaceDef = `
612
- export interface ${messageDef.name} {}`;
613
- }
614
- else {
615
- interfaceDef = `
616
- export interface ${messageDef.name} {
617
- ${defineFields(fields, messageDef, moduleDef)
618
- .join('\n ')
619
- .trim()}
620
- }`;
621
- }
622
- const encodeFields = Object.entries(fields)
623
- .map(([name, fieldDef]) => {
624
- let codec = encoders[fieldDef.type];
625
- let type = fieldDef.map ? 'message' : fieldDef.type;
626
- let typeName = '';
627
- if (codec == null) {
628
- if (fieldDef.enum) {
629
- moduleDef.addImport('protons-runtime', 'enumeration');
630
- type = 'enum';
631
- }
632
- else {
633
- moduleDef.addImport('protons-runtime', 'message');
634
- type = 'message';
635
- }
636
- typeName = findJsTypeName(fieldDef.type, messageDef, moduleDef, fieldDef);
637
- codec = `${typeName}.codec()`;
638
- }
639
- let valueTest = `obj.${name} != null`;
640
- if (fieldDef.map) {
641
- valueTest = `obj.${name} != null && obj.${name}.size !== 0`;
642
- }
643
- else if (!fieldDef.optional && !fieldDef.repeated && !fieldDef.proto2Required) {
644
- let defaultValueTestGenerator = defaultValueTestGenerators[type];
645
- // proto3 singular fields should only be written out if they are not the default value
646
- if (defaultValueTestGenerator != null) {
647
- const jsTypeOverride = findJsTypeOverride(type, fieldDef);
648
- if (jsTypeOverride != null && defaultValueTestGeneratorsJsTypeOverrides[jsTypeOverride] != null) {
649
- defaultValueTestGenerator = defaultValueTestGeneratorsJsTypeOverrides[jsTypeOverride];
650
- }
651
- valueTest = `${defaultValueTestGenerator(`obj.${name}`)}`;
652
- }
653
- else if (type === 'enum') {
654
- // handle enums
655
- const def = findDef(fieldDef.type, messageDef, moduleDef);
656
- if (!isEnumDef(def)) {
657
- throw new Error(`${fieldDef.type} was not enum def`);
658
- }
659
- valueTest = `obj.${name} != null`;
660
- // singular enums default to 0, but enums can be defined without a 0
661
- // value which is against the proto3 spec but is tolerated
662
- if (Object.values(def.values)[0] === 0) {
663
- valueTest += ` && __${fieldDef.type}Values[obj.${name}] !== 0`;
664
- }
665
- }
666
- }
667
- function createWriteField(valueVar) {
668
- const id = (fieldDef.id << 3) | codecTypes[type];
669
- if (fieldDef.enum) {
670
- const def = findDef(fieldDef.type, messageDef, moduleDef);
671
- if (!isEnumDef(def)) {
672
- throw new Error(`${fieldDef.type} was not enum def`);
673
- }
674
- }
675
- let writeField = () => {
676
- const encoderGenerator = encoderGenerators[type];
677
- const jsTypeOverride = findJsTypeOverride(type, fieldDef);
678
- return `w.uint32(${id})
679
- ${encoderGenerator == null ? `${codec}.encode(${valueVar}, w)` : encoderGenerator(valueVar, jsTypeOverride)}`;
680
- };
681
- if (type === 'message') {
682
- // message fields are only written if they have values. But if a message
683
- // is part of a repeated field, and consists of only default values it
684
- // won't be written, so write a zero-length buffer if that's the case
685
- writeField = () => `w.uint32(${id})
686
- ${typeName}.codec().encode(${valueVar}, w)`;
687
- }
688
- return writeField;
689
- }
690
- let writeField = createWriteField(`obj.${name}`);
691
- if (fieldDef.repeated) {
692
- if (fieldDef.map) {
693
- writeField = () => `
694
- for (const [key, value] of obj.${name}.entries()) {
695
- ${createWriteField('{ key, value }')()
696
- .split('\n')
697
- .map(s => {
698
- const trimmed = s.trim();
699
- return trimmed === '' ? trimmed : ` ${s}`;
700
- })
701
- .join('\n')}
702
- }
703
- `.trim();
704
- }
705
- else {
706
- writeField = () => `
707
- for (const value of obj.${name}) {
708
- ${createWriteField('value')()
709
- .split('\n')
710
- .map(s => {
711
- const trimmed = s.trim();
712
- return trimmed === '' ? trimmed : ` ${s}`;
713
- })
714
- .join('\n')}
715
- }
716
- `.trim();
717
- }
718
- }
719
- return `
720
- if (${valueTest}) {
721
- ${writeField()}
722
- }`;
723
- }).join('\n');
724
- const enforceOneOfEncoding = createOneOfEncoding(messageDef);
725
- const enforceOneOfDecoding = createOneOfDecoding(messageDef);
726
- const decodeFields = Object.entries(fields)
727
- .map(([fieldName, fieldDef]) => {
728
- function createReadField(fieldName, fieldDef) {
729
- let codec = encoders[fieldDef.type];
730
- let type = fieldDef.type;
731
- if (codec == null) {
732
- if (fieldDef.enum) {
733
- moduleDef.addImport('protons-runtime', 'enumeration');
734
- type = 'enum';
735
- }
736
- else {
737
- moduleDef.addImport('protons-runtime', 'message');
738
- type = 'message';
739
- }
740
- const typeName = findJsTypeName(fieldDef.type, messageDef, moduleDef, fieldDef);
741
- codec = `${typeName}.codec()`;
742
- }
743
- // override setting type on js object
744
- const jsTypeOverride = findJsTypeOverride(fieldDef.type, fieldDef);
745
- let fieldOpts = '';
746
- if (fieldDef.message) {
747
- let suffix = '';
748
- if (fieldDef.repeated) {
749
- suffix = '$';
750
- }
751
- fieldOpts = `, {
752
- limits: opts.limits?.${fieldName}${suffix}
753
- }`;
754
- }
755
- if (fieldDef.map) {
756
- fieldOpts = `, {
757
- limits: {
758
- value: opts.limits?.${fieldName}$value
759
- }
760
- }`;
761
- // do not pass limit opts to map value types that are enums or
762
- // primitives - only support messages
763
- if (types[fieldDef.valueType] != null) {
764
- // primmitive type
765
- fieldOpts = '';
766
- }
767
- else {
768
- const valueType = findDef(fieldDef.valueType, messageDef, moduleDef);
769
- if (isEnumDef(valueType)) {
770
- // enum type
771
- fieldOpts = '';
772
- }
773
- }
774
- }
775
- const parseValue = `${decoderGenerators[type] == null
776
- ? `${codec}.decode(reader${type === 'message'
777
- ? `, reader.uint32()${fieldOpts}`
778
- : ''})`
779
- : decoderGenerators[type](jsTypeOverride)}`;
780
- if (fieldDef.map) {
781
- moduleDef.addImport('protons-runtime', 'MaxSizeError');
782
- let limit = `
783
- if (opts.limits?.${fieldName} != null && obj.${fieldName}.size === opts.limits.${fieldName}) {
784
- throw new MaxSizeError('Decode error - map field "${fieldName}" had too many elements')
785
- }
786
- `;
787
- if (fieldDef.lengthLimit != null) {
788
- limit += `
789
- if (obj.${fieldName}.size === ${fieldDef.lengthLimit}) {
790
- throw new MaxSizeError('Decode error - map field "${fieldName}" had too many elements')
791
- }
792
- `;
793
- }
794
- return `case ${fieldDef.id}: {${limit}
795
- const entry = ${parseValue}
796
- obj.${fieldName}.set(entry.key, entry.value)
797
- break
798
- }`;
799
- }
800
- else if (fieldDef.repeated) {
801
- moduleDef.addImport('protons-runtime', 'MaxLengthError');
802
- let limit = `
803
- if (opts.limits?.${fieldName} != null && obj.${fieldName}.length === opts.limits.${fieldName}) {
804
- throw new MaxLengthError('Decode error - map field "${fieldName}" had too many elements')
805
- }
806
- `;
807
- if (fieldDef.lengthLimit != null) {
808
- limit += `
809
- if (obj.${fieldName}.length === ${fieldDef.lengthLimit}) {
810
- throw new MaxLengthError('Decode error - repeated field "${fieldName}" had too many elements')
811
- }
812
- `;
813
- }
814
- return `case ${fieldDef.id}: {${limit}
815
- obj.${fieldName}.push(${parseValue})
816
- break
817
- }`;
818
- }
819
- return `case ${fieldDef.id}: {
820
- obj.${fieldName} = ${parseValue}
821
- break
822
- }`;
823
- }
824
- return createReadField(fieldName, fieldDef);
825
- })
826
- .join('\n ');
827
- interfaceCodecDef = `
828
- let _codec: Codec<${messageDef.name}>
829
-
830
- export const codec = (): Codec<${messageDef.name}> => {
831
- if (_codec == null) {
832
- _codec = message<${messageDef.name}>((obj, w, opts = {}) => {
833
- if (opts.lengthDelimited !== false) {
834
- w.fork()
835
- }
836
- ${enforceOneOfEncoding}${encodeFields === '' ? '' : `${encodeFields}\n`}
837
- if (opts.lengthDelimited !== false) {
838
- w.ldelim()
839
- }
840
- }, (reader, length, opts = {}) => {
841
- const obj: any = {${createDefaultObject(fields, messageDef, moduleDef)}}
842
-
843
- const end = length == null ? reader.len : reader.pos + length
844
-
845
- while (reader.pos < end) {
846
- const tag = reader.uint32()
847
-
848
- switch (tag >>> 3) {${decodeFields === '' ? '' : `\n ${decodeFields}`}
849
- default: {
850
- reader.skipType(tag & 7)
851
- break
852
- }
853
- }
854
- }
855
- ${enforceOneOfDecoding === '' ? '' : `${enforceOneOfDecoding}\n`}
856
- return obj
857
- })
858
- }
859
-
860
- return _codec
861
- }
862
-
863
- export const encode = (obj: Partial<${messageDef.name}>): Uint8Array => {
864
- return encodeMessage(obj, ${messageDef.name}.codec())
865
- }
866
-
867
- export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<${messageDef.name}>): ${messageDef.name} => {
868
- return decodeMessage(buf, ${messageDef.name}.codec(), opts)
869
- }`;
870
- return `
871
- ${interfaceDef}
872
-
873
- export namespace ${messageDef.name} {
874
- ${`${nested}${nested !== '' && interfaceCodecDef !== '' ? '\n' : ''}${interfaceCodecDef}`.trim()}
875
- }
876
- `.trimStart();
877
- }
878
- class ModuleDef {
879
- imports;
880
- types;
881
- compiled;
882
- globals;
883
- constructor() {
884
- this.imports = new Map();
885
- this.types = new Set();
886
- this.compiled = [];
887
- this.globals = {};
888
- }
889
- addImport(module, symbol, alias) {
890
- const defs = this._findDefs(module);
891
- for (const def of defs) {
892
- // check if we already have a definition for this symbol
893
- if (def.symbol === symbol) {
894
- if (alias !== def.alias) {
895
- throw new Error(`Type symbol ${symbol} imported from ${module} with alias ${def.alias} does not match alias ${alias}`);
896
- }
897
- // if it was a type before it's not now
898
- def.type = false;
899
- return;
900
- }
901
- }
902
- defs.push({
903
- symbol,
904
- alias,
905
- type: false
906
- });
907
- }
908
- addTypeImport(module, symbol, alias) {
909
- const defs = this._findDefs(module);
910
- for (const def of defs) {
911
- // check if we already have a definition for this symbol
912
- if (def.symbol === symbol) {
913
- if (alias !== def.alias) {
914
- throw new Error(`Type symbol ${symbol} imported from ${module} with alias ${def.alias} does not match alias ${alias}`);
915
- }
916
- return;
917
- }
918
- }
919
- defs.push({
920
- symbol,
921
- alias,
922
- type: true
923
- });
924
- }
925
- _findDefs(module) {
926
- let defs = this.imports.get(module);
927
- if (defs == null) {
928
- defs = [];
929
- this.imports.set(module, defs);
930
- }
931
- return defs;
932
- }
933
- }
934
- function defineModule(def, flags) {
935
- const moduleDef = new ModuleDef();
936
- const defs = def.nested;
937
- if (defs == null) {
938
- throw new NoMessagesFoundError('No top-level messages found in protobuf');
939
- }
940
- function defineMessage(defs, parent, flags) {
941
- for (const className of Object.keys(defs)) {
942
- const classDef = defs[className];
943
- classDef.name = className;
944
- classDef.parent = parent;
945
- classDef.fullName = parent == null ? className : `${parent.fullName}.${className}`;
946
- if (classDef.nested != null) {
947
- defineMessage(classDef.nested, classDef);
948
- }
949
- if (classDef.fields != null) {
950
- for (const name of Object.keys(classDef.fields)) {
951
- const fieldDef = classDef.fields[name];
952
- fieldDef.repeated = fieldDef.rule === 'repeated';
953
- fieldDef.optional = !fieldDef.repeated && fieldDef.options?.proto3_optional === true;
954
- fieldDef.map = fieldDef.keyType != null;
955
- fieldDef.lengthLimit = fieldDef.options?.['(protons.options).limit'];
956
- fieldDef.proto2Required = false;
957
- if (fieldDef.rule === 'required') {
958
- const message = `field "${name}" is required, this is not allowed in proto3. Please convert your proto2 definitions to proto3 - see https://github.com/ipfs/protons/wiki/Required-fields-and-protobuf-3`;
959
- if (flags?.strict === true) {
960
- throw new ParseError(message);
961
- }
962
- else {
963
- fieldDef.proto2Required = true;
964
- // eslint-disable-next-line no-console
965
- console.info(`[WARN] ${message}`);
966
- }
967
- }
968
- }
969
- }
970
- if (parent == null) {
971
- moduleDef.globals[className] = classDef;
972
- }
973
- }
974
- }
975
- function updateTypes(defs, parent) {
976
- for (const className of Object.keys(defs)) {
977
- const classDef = defs[className];
978
- if (classDef.nested != null) {
979
- updateTypes(classDef.nested, classDef);
980
- }
981
- if (classDef.fields != null) {
982
- for (const name of Object.keys(classDef.fields)) {
983
- const fieldDef = classDef.fields[name];
984
- if (types[fieldDef.type] == null) {
985
- const def = findDef(fieldDef.type, classDef, moduleDef);
986
- fieldDef.enum = isEnumDef(def);
987
- fieldDef.message = !fieldDef.enum;
988
- if (fieldDef.message && !fieldDef.repeated) {
989
- // the default type for a message is unset so they are always optional
990
- // https://developers.google.com/protocol-buffers/docs/proto3#default
991
- fieldDef.optional = true;
992
- }
993
- }
994
- }
995
- }
996
- }
997
- }
998
- defineMessage(defs, undefined, flags);
999
- // set enum/message fields now all messages have been defined
1000
- updateTypes(defs);
1001
- for (const className of Object.keys(defs)) {
1002
- const classDef = defs[className];
1003
- moduleDef.compiled.push(compileMessage(classDef, moduleDef, flags));
1004
- }
1005
- return moduleDef;
1006
- }
1007
202
  export async function generate(source, flags) {
1008
203
  // convert .protobuf to .json
1009
204
  const json = await promisify(pbjs)([
@@ -1015,162 +210,9 @@ export async function generate(source, flags) {
1015
210
  throw new Error(`Could not convert ${source} to intermediate JSON format`);
1016
211
  }
1017
212
  const def = JSON.parse(json);
1018
- for (const [className, classDef] of Object.entries(def.nested ?? {})) {
1019
- for (const [fieldName, fieldDef] of Object.entries(classDef.fields ?? {})) {
1020
- if (fieldDef.keyType == null) {
1021
- continue;
1022
- }
1023
- // https://developers.google.com/protocol-buffers/docs/proto3#backwards_compatibility
1024
- const mapEntryType = `${className}$${fieldName}Entry`;
1025
- classDef.nested = classDef.nested ?? {};
1026
- classDef.nested[mapEntryType] = {
1027
- fields: {
1028
- key: {
1029
- type: fieldDef.keyType,
1030
- id: 1
1031
- },
1032
- value: {
1033
- type: fieldDef.type,
1034
- id: 2
1035
- }
1036
- }
1037
- };
1038
- fieldDef.valueType = fieldDef.type;
1039
- fieldDef.type = mapEntryType;
1040
- fieldDef.rule = 'repeated';
1041
- }
1042
- configureOnOfs(classDef);
1043
- }
1044
- const moduleDef = defineModule(def, flags);
1045
- const ignores = [
1046
- '/* eslint-disable import/export */',
1047
- '/* eslint-disable complexity */',
1048
- '/* eslint-disable @typescript-eslint/no-namespace */',
1049
- '/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */',
1050
- '/* eslint-disable @typescript-eslint/no-empty-interface */',
1051
- '/* eslint-disable import/consistent-type-specifier-style */',
1052
- '/* eslint-disable @typescript-eslint/no-unused-vars */'
1053
- ];
1054
- const imports = [];
1055
- const importedModules = Array.from([...moduleDef.imports.entries()])
1056
- .sort((a, b) => {
1057
- return a[0].localeCompare(b[0]);
1058
- })
1059
- .sort((a, b) => {
1060
- const aAllTypes = a[1].reduce((acc, curr) => {
1061
- return acc && curr.type;
1062
- }, true);
1063
- const bAllTypes = b[1].reduce((acc, curr) => {
1064
- return acc && curr.type;
1065
- }, true);
1066
- if (aAllTypes && !bAllTypes) {
1067
- return 1;
1068
- }
1069
- if (!aAllTypes && bAllTypes) {
1070
- return -1;
1071
- }
1072
- return 0;
1073
- });
1074
- // add imports
1075
- for (const imp of importedModules) {
1076
- const symbols = imp[1]
1077
- .filter(imp => !imp.type)
1078
- .sort((a, b) => {
1079
- return a.symbol.localeCompare(b.symbol);
1080
- }).map(imp => {
1081
- return `${imp.symbol}${imp.alias != null ? ` as ${imp.alias}` : ''}`;
1082
- }).join(', ');
1083
- if (symbols.length > 0) {
1084
- imports.push(`import { ${symbols} } from '${imp[0]}'`);
1085
- }
1086
- }
1087
- // add type imports
1088
- for (const imp of importedModules) {
1089
- const symbols = imp[1]
1090
- .filter(imp => imp.type)
1091
- .sort((a, b) => {
1092
- return a.symbol.localeCompare(b.symbol);
1093
- }).map(imp => {
1094
- return `${imp.symbol}${imp.alias != null ? ` as ${imp.alias}` : ''}`;
1095
- }).join(', ');
1096
- if (symbols.length > 0) {
1097
- imports.push(`import type { ${symbols} } from '${imp[0]}'`);
1098
- }
1099
- }
1100
- const lines = [
1101
- ...ignores,
1102
- '',
1103
- ...imports,
1104
- '',
1105
- ...moduleDef.compiled
1106
- ];
1107
- const content = lines.join('\n').trim();
213
+ const module = new Module(def, flags);
214
+ const content = module.compile();
1108
215
  const outputPath = pathWithExtension(source, '.ts', flags.output);
1109
216
  await fs.writeFile(outputPath, content + '\n');
1110
217
  }
1111
- function configureOnOfs(classDef) {
1112
- const oneOfs = classDef.oneofs ?? {};
1113
- for (const [, { oneof: fields }] of Object.entries(oneOfs)) {
1114
- fields.forEach(field => {
1115
- classDef.fields[field].options ??= {};
1116
- classDef.fields[field].options.proto3_optional = true;
1117
- classDef.fields[field].oneof = fields;
1118
- });
1119
- }
1120
- const oneofs = classDef.oneofs ?? {};
1121
- // reverse order of oneofs as spec says last field overwrites all others
1122
- classDef.oneofs = Object.values(oneofs).map(({ oneof }) => oneof.reverse());
1123
- for (const nestedDef of Object.values(classDef.nested ?? {})) {
1124
- configureOnOfs(nestedDef);
1125
- }
1126
- }
1127
- function createOneOfEncoding(messageDef) {
1128
- if (messageDef.oneofs == null) {
1129
- return '';
1130
- }
1131
- let oneofs = messageDef.oneofs.map(fields => {
1132
- if (fields.length < 2) {
1133
- return '';
1134
- }
1135
- let oneof = '';
1136
- for (const name of fields) {
1137
- oneof += `
1138
- if (obj.${name} != null) {
1139
- ${fields
1140
- .filter(field => field !== name)
1141
- .map(name => ` obj.${name} = undefined`).join('\n')}
1142
- }
1143
- `;
1144
- }
1145
- return oneof.trimEnd();
1146
- }).join('\n').trimEnd();
1147
- if (oneofs !== '') {
1148
- oneofs = `
1149
- obj = { ...obj }
1150
- ${oneofs}
1151
- `;
1152
- }
1153
- return oneofs;
1154
- }
1155
- function createOneOfDecoding(messageDef) {
1156
- if (messageDef.oneofs == null) {
1157
- return '';
1158
- }
1159
- return messageDef.oneofs.map(fields => {
1160
- if (fields.length < 2) {
1161
- return '';
1162
- }
1163
- let oneof = '';
1164
- for (const name of fields) {
1165
- oneof += `
1166
- if (obj.${name} != null) {
1167
- ${fields
1168
- .filter(field => field !== name)
1169
- .map(name => ` delete obj.${name}`).join('\n')}
1170
- }
1171
- `;
1172
- }
1173
- return oneof.trimEnd();
1174
- }).join('\n').trimEnd();
1175
- }
1176
218
  //# sourceMappingURL=index.js.map