protobufjs 8.6.5 → 8.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1212,7 +1212,7 @@ export interface IToJSONOptions {
1212
1212
  */
1213
1213
  export function parse(source: string, root: Root, options?: IParseOptions): IParserResult;
1214
1214
 
1215
- /** Wire format reader using `Uint8Array` if available, otherwise `Array`. */
1215
+ /** Wire format reader using `Uint8Array`. */
1216
1216
  export class Reader {
1217
1217
 
1218
1218
  /**
@@ -1230,6 +1230,9 @@ export class Reader {
1230
1230
  /** Read buffer length. */
1231
1231
  len: number;
1232
1232
 
1233
+ /** Cached DataView for packed reads. */
1234
+ view: (DataView|null);
1235
+
1233
1236
  /** Whether to discard unknown fields while decoding. */
1234
1237
  discardUnknown: boolean;
1235
1238
 
@@ -1333,6 +1336,97 @@ export class Reader {
1333
1336
  */
1334
1337
  double(): number;
1335
1338
 
1339
+ /**
1340
+ * Reads a packed repeated field of unsigned 32 bit varints.
1341
+ * @param [array] Array to read into; a new one is created if omitted
1342
+ * @returns Array read into
1343
+ */
1344
+ uint32s(array?: number[]): number[];
1345
+
1346
+ /**
1347
+ * Reads a packed repeated field of signed 32 bit varints.
1348
+ * @param [array] Array to read into; a new one is created if omitted
1349
+ * @returns Array read into
1350
+ */
1351
+ int32s(array?: number[]): number[];
1352
+
1353
+ /**
1354
+ * Reads a packed repeated field of zig-zag encoded signed 32 bit varints.
1355
+ * @param [array] Array to read into; a new one is created if omitted
1356
+ * @returns Array read into
1357
+ */
1358
+ sint32s(array?: number[]): number[];
1359
+
1360
+ /**
1361
+ * Reads a packed repeated field of booleans.
1362
+ * @param [array] Array to read into; a new one is created if omitted
1363
+ * @returns Array read into
1364
+ */
1365
+ bools(array?: boolean[]): boolean[];
1366
+
1367
+ /**
1368
+ * Reads a packed repeated field of unsigned 32 bit fixed values.
1369
+ * @param [array] Array to read into; a new one is created if omitted
1370
+ * @returns Array read into
1371
+ */
1372
+ fixed32s(array?: number[]): number[];
1373
+
1374
+ /**
1375
+ * Reads a packed repeated field of signed 32 bit fixed values.
1376
+ * @param [array] Array to read into; a new one is created if omitted
1377
+ * @returns Array read into
1378
+ */
1379
+ sfixed32s(array?: number[]): number[];
1380
+
1381
+ /**
1382
+ * Reads a packed repeated field of floats (32 bit).
1383
+ * @param [array] Array to read into; a new one is created if omitted
1384
+ * @returns Array read into
1385
+ */
1386
+ floats(array?: number[]): number[];
1387
+
1388
+ /**
1389
+ * Reads a packed repeated field of doubles (64 bit float).
1390
+ * @param [array] Array to read into; a new one is created if omitted
1391
+ * @returns Array read into
1392
+ */
1393
+ doubles(array?: number[]): number[];
1394
+
1395
+ /**
1396
+ * Reads a packed repeated field of unsigned 64 bit varints.
1397
+ * @param [array] Array to read into; a new one is created if omitted
1398
+ * @returns Array read into
1399
+ */
1400
+ uint64s(array?: (Long|number)[]): (Long|number)[];
1401
+
1402
+ /**
1403
+ * Reads a packed repeated field of signed 64 bit varints.
1404
+ * @param [array] Array to read into; a new one is created if omitted
1405
+ * @returns Array read into
1406
+ */
1407
+ int64s(array?: (Long|number)[]): (Long|number)[];
1408
+
1409
+ /**
1410
+ * Reads a packed repeated field of zig-zag encoded signed 64 bit varints.
1411
+ * @param [array] Array to read into; a new one is created if omitted
1412
+ * @returns Array read into
1413
+ */
1414
+ sint64s(array?: (Long|number)[]): (Long|number)[];
1415
+
1416
+ /**
1417
+ * Reads a packed repeated field of unsigned 64 bit fixed values.
1418
+ * @param [array] Array to read into; a new one is created if omitted
1419
+ * @returns Array read into
1420
+ */
1421
+ fixed64s(array?: (Long|number)[]): (Long|number)[];
1422
+
1423
+ /**
1424
+ * Reads a packed repeated field of signed 64 bit fixed values.
1425
+ * @param [array] Array to read into; a new one is created if omitted
1426
+ * @returns Array read into
1427
+ */
1428
+ sfixed64s(array?: (Long|number)[]): (Long|number)[];
1429
+
1336
1430
  /**
1337
1431
  * Reads a sequence of bytes preceeded by its length as a varint.
1338
1432
  * @returns Value read
@@ -2464,7 +2558,10 @@ export namespace util {
2464
2558
  */
2465
2559
  function newBuffer(sizeOrArray?: (number|number[])): (Uint8Array|Buffer);
2466
2560
 
2467
- /** Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`. */
2561
+ /**
2562
+ * Array implementation used in the browser.
2563
+ * @deprecated Use `Uint8Array` instead.
2564
+ */
2468
2565
  let Array: Constructor<Uint8Array>;
2469
2566
 
2470
2567
  /** Long.js's Long class if available. */
@@ -2626,7 +2723,7 @@ export namespace util {
2626
2723
  */
2627
2724
  function pool(alloc: PoolAllocator, slice: PoolSlicer, size?: number): PoolAllocator;
2628
2725
 
2629
- /** A minimal UTF8 implementation for number arrays. */
2726
+ /** A minimal UTF8 implementation. */
2630
2727
  namespace utf8 {
2631
2728
 
2632
2729
  /**
@@ -2803,23 +2900,29 @@ export interface IWrapper {
2803
2900
  toObject?: WrapperToObjectConverter;
2804
2901
  }
2805
2902
 
2806
- /** Wire format writer using `Uint8Array` if available, otherwise `Array`. */
2903
+ /** Wire format writer using `Uint8Array`. */
2807
2904
  export class Writer {
2808
2905
 
2809
2906
  /** Constructs a new writer instance. */
2810
2907
  constructor();
2811
2908
 
2812
- /** Current length. */
2813
- len: number;
2909
+ /** Write cursor into {@link Writer#buf}. */
2910
+ pos: number;
2814
2911
 
2815
- /** Operations head. */
2816
- head: object;
2912
+ /** Backing buffer. */
2913
+ buf: Uint8Array;
2817
2914
 
2818
- /** Operations tail */
2819
- tail: object;
2915
+ /** Cached DataView over {@link Writer#buf}. */
2916
+ view: (DataView|null);
2820
2917
 
2821
- /** Linked forked states. */
2822
- states: (object|null);
2918
+ /** Stack of forked length-prefix positions. */
2919
+ states: (number[]|null);
2920
+
2921
+ /**
2922
+ * Current write position.
2923
+ * @deprecated Use {@link Writer#pos} instead.
2924
+ */
2925
+ len: number;
2823
2926
 
2824
2927
  /**
2825
2928
  * Creates a new writer.
@@ -2951,9 +3054,99 @@ export class Writer {
2951
3054
  */
2952
3055
  string(value: string): Writer;
2953
3056
 
3057
+ /**
3058
+ * Writes an array of unsigned 32 bit values as a packed repeated field.
3059
+ * @param value Values to write
3060
+ * @returns `this`
3061
+ */
3062
+ uint32s(value: number[]): Writer;
3063
+
3064
+ /**
3065
+ * Writes an array of signed 32 bit values as a packed repeated field.
3066
+ * @param value Values to write
3067
+ * @returns `this`
3068
+ */
3069
+ int32s(value: number[]): Writer;
3070
+
3071
+ /**
3072
+ * Writes an array of 32 bit values as packed, zig-zag encoded varints.
3073
+ * @param value Values to write
3074
+ * @returns `this`
3075
+ */
3076
+ sint32s(value: number[]): Writer;
3077
+
3078
+ /**
3079
+ * Writes an array of unsigned 64 bit values as a packed repeated field.
3080
+ * @param value Values to write
3081
+ * @returns `this`
3082
+ */
3083
+ uint64s(value: (Long|number|string)[]): Writer;
3084
+
3085
+ /**
3086
+ * Writes an array of signed 64 bit values as a packed repeated field.
3087
+ * @param value Values to write
3088
+ * @returns `this`
3089
+ */
3090
+ int64s(value: (Long|number|string)[]): Writer;
3091
+
3092
+ /**
3093
+ * Writes an array of 64 bit values as packed, zig-zag encoded varints.
3094
+ * @param value Values to write
3095
+ * @returns `this`
3096
+ */
3097
+ sint64s(value: (Long|number|string)[]): Writer;
3098
+
3099
+ /**
3100
+ * Writes an array of boolish values as a packed repeated field.
3101
+ * @param value Values to write
3102
+ * @returns `this`
3103
+ */
3104
+ bools(value: boolean[]): Writer;
3105
+
3106
+ /**
3107
+ * Writes an array of unsigned 32 bit values as packed, fixed 32 bits.
3108
+ * @param value Values to write
3109
+ * @returns `this`
3110
+ */
3111
+ fixed32s(value: number[]): Writer;
3112
+
3113
+ /**
3114
+ * Writes an array of signed 32 bit values as packed, fixed 32 bits.
3115
+ * @param value Values to write
3116
+ * @returns `this`
3117
+ */
3118
+ sfixed32s(value: number[]): Writer;
3119
+
3120
+ /**
3121
+ * Writes an array of unsigned 64 bit values as packed, fixed 64 bits.
3122
+ * @param value Values to write
3123
+ * @returns `this`
3124
+ */
3125
+ fixed64s(value: (Long|number|string)[]): Writer;
3126
+
3127
+ /**
3128
+ * Writes an array of signed 64 bit values as packed, fixed 64 bits.
3129
+ * @param value Values to write
3130
+ * @returns `this`
3131
+ */
3132
+ sfixed64s(value: (Long|number|string)[]): Writer;
3133
+
3134
+ /**
3135
+ * Writes an array of floats (32 bit) as a packed repeated field.
3136
+ * @param value Values to write
3137
+ * @returns `this`
3138
+ */
3139
+ floats(value: number[]): Writer;
3140
+
3141
+ /**
3142
+ * Writes an array of doubles (64 bit float) as a packed repeated field.
3143
+ * @param value Values to write
3144
+ * @returns `this`
3145
+ */
3146
+ doubles(value: number[]): Writer;
3147
+
2954
3148
  /**
2955
3149
  * Forks this writer's state by pushing it to a stack.
2956
- * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
2957
3150
  * @returns `this`
2958
3151
  */
2959
3152
  fork(): Writer;
@@ -2965,21 +3158,23 @@ export class Writer {
2965
3158
  reset(): Writer;
2966
3159
 
2967
3160
  /**
2968
- * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
3161
+ * Resets to the last state and prepends the fork state's current write length as a varint.
2969
3162
  * @returns `this`
2970
3163
  */
2971
3164
  ldelim(): Writer;
2972
3165
 
2973
3166
  /**
2974
3167
  * Finishes the write operation.
3168
+ * Returns a buffer sized to the written data by default.
3169
+ * @param [shared=false] Whether to return a shared view instead of a unique copy
2975
3170
  * @returns Finished buffer
2976
3171
  */
2977
- finish(): Uint8Array;
3172
+ finish(shared?: boolean): Uint8Array;
2978
3173
 
2979
3174
  /**
2980
3175
  * Finishes the write operation, writing into the provided buffer.
2981
3176
  * The caller must ensure that `buf` has enough space starting at `offset`
2982
- * to hold {@link Writer#len} bytes.
3177
+ * to hold {@link Writer#pos} bytes.
2983
3178
  * @param buf Target buffer
2984
3179
  * @param [offset=0] Offset to start writing at
2985
3180
  * @returns The provided buffer
@@ -3000,13 +3195,6 @@ export class BufferWriter extends Writer {
3000
3195
  */
3001
3196
  static alloc(size: number): Buffer;
3002
3197
 
3003
- /**
3004
- * Writes raw bytes without a tag or length prefix.
3005
- * @param value Raw bytes
3006
- * @returns `this`
3007
- */
3008
- raw(value: Uint8Array): BufferWriter;
3009
-
3010
3198
  /**
3011
3199
  * Finishes the write operation.
3012
3200
  * @returns Finished buffer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "protobufjs",
3
- "version": "8.6.5",
3
+ "version": "8.7.0",
4
4
  "description": "Protocol Buffers for JavaScript & TypeScript.",
5
5
  "author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
6
6
  "license": "BSD-3-Clause",
@@ -22,7 +22,7 @@
22
22
  "fs": false
23
23
  },
24
24
  "scripts": {
25
- "bench": "node bench",
25
+ "bench": "npm --prefix bench run bench",
26
26
  "build": "npm run build:bundle && npm run build:types",
27
27
  "build:bundle": "gulp --gulpfile scripts/gulpfile.js",
28
28
  "build:tests": "node ./scripts/gentests.js",
package/src/decoder.js CHANGED
@@ -141,13 +141,9 @@ function decoder(mtype) {
141
141
  if (types.packed[type] !== undefined) {
142
142
  gen
143
143
  ("if(u===2){");
144
- if (!closed) gen
145
- ("if(!(%s&&%s.length))", ref, ref)
146
- ("%s=[]", ref);
147
- gen
148
- ("var c2=r.uint32()+r.pos");
149
144
  if (closed) {
150
145
  gen
146
+ ("var c2=r.uint32()+r.pos")
151
147
  ("while(r.pos<c2){")
152
148
  ("s=r.pos")
153
149
  ("v=r.%s()", type)
@@ -159,8 +155,9 @@ function decoder(mtype) {
159
155
  genPreserveUnknown(gen, "util.rawField(" + field.id + ",0,r.raw(s,r.pos))")
160
156
  ("}");
161
157
  } else gen
162
- ("while(r.pos<c2)")
163
- ("%s.push(r.%s())", ref, type);
158
+ ("if(!(%s&&%s.length))", ref, ref)
159
+ ("%s=[]", ref)
160
+ ("r.%ss(%s)", type, ref);
164
161
  gen
165
162
  ("continue")
166
163
  ("}");
package/src/encoder.js CHANGED
@@ -72,10 +72,7 @@ function encoder(mtype) {
72
72
  // Packed repeated
73
73
  if (field.packed && types.packed[type] !== undefined) { gen
74
74
 
75
- ("w.uint32(%i).fork()", (field.id << 3 | 2) >>> 0)
76
- ("for(var i=0;i<%s.length;++i)", ref)
77
- ("w.%s(%s[i])", type, ref)
78
- ("w.ldelim()");
75
+ ("w.uint32(%i).%ss(%s)", (field.id << 3 | 2) >>> 0, type, ref);
79
76
 
80
77
  // Non-packed
81
78
  } else { gen
@@ -26,7 +26,6 @@ exports.configure = configure;
26
26
  * @returns {undefined}
27
27
  */
28
28
  function configure() {
29
- exports.util._configure();
30
29
  exports.Writer._configure(exports.BufferWriter);
31
30
  exports.Reader._configure(exports.BufferReader);
32
31
  }
package/src/parse.js CHANGED
@@ -220,6 +220,9 @@ function parse(source, root, options) {
220
220
  }
221
221
 
222
222
  function parseId(token, acceptNegative, max) {
223
+ if (token === null) {
224
+ throw illegal(token, "end of input");
225
+ }
223
226
  switch (token) {
224
227
  case "max": case "MAX": case "Max":
225
228
  return max || maxFieldId;
@@ -252,7 +255,7 @@ function parse(source, root, options) {
252
255
  pkg = next();
253
256
 
254
257
  /* istanbul ignore if */
255
- if (!typeRefRe.test(pkg))
258
+ if (pkg === null || !typeRefRe.test(pkg))
256
259
  throw illegal(pkg, "name");
257
260
 
258
261
  ptr = ptr.define(pkg);
@@ -389,7 +392,7 @@ function parse(source, root, options) {
389
392
  throw Error("max depth exceeded");
390
393
 
391
394
  /* istanbul ignore if */
392
- if (!nameRe.test(token = next()))
395
+ if ((token = next()) === null || !nameRe.test(token))
393
396
  throw illegal(token, "type name");
394
397
 
395
398
  var type = new Type(token);
@@ -456,6 +459,9 @@ function parse(source, root, options) {
456
459
 
457
460
  function parseField(parent, rule, extend, depth) {
458
461
  var type = next();
462
+ if (type === null) {
463
+ throw illegal(type, "end of input");
464
+ }
459
465
  if (type === "group") {
460
466
  parseGroup(parent, rule, extend, depth);
461
467
  return;
@@ -467,8 +473,12 @@ function parse(source, root, options) {
467
473
  // package .subpackage field tokens: "package" ".subpackage" [TYPE NAME ENDS HERE] "field"
468
474
  // Keep reading tokens until we get a type name with no period at the end,
469
475
  // and the next token does not start with a period.
470
- while (type.endsWith(".") || peek().startsWith(".")) {
471
- type += next();
476
+ while (type.endsWith(".") || (peek() || "").startsWith(".")) {
477
+ var part = next();
478
+ if (part === null) {
479
+ throw illegal(part, "end of input");
480
+ }
481
+ type += part;
472
482
  }
473
483
 
474
484
  /* istanbul ignore if */
@@ -476,6 +486,9 @@ function parse(source, root, options) {
476
486
  throw illegal(type, "type");
477
487
 
478
488
  var name = next();
489
+ if (name === null) {
490
+ throw illegal(name, "end of input");
491
+ }
479
492
 
480
493
  /* istanbul ignore if */
481
494
 
@@ -528,7 +541,7 @@ function parse(source, root, options) {
528
541
  var name = next();
529
542
 
530
543
  /* istanbul ignore if */
531
- if (!nameRe.test(name))
544
+ if (name === null || !nameRe.test(name))
532
545
  throw illegal(name, "name");
533
546
 
534
547
  var fieldName = util.lcFirst(name);
@@ -626,7 +639,7 @@ function parse(source, root, options) {
626
639
  var name = next();
627
640
 
628
641
  /* istanbul ignore if */
629
- if (!nameRe.test(name))
642
+ if (name === null || !nameRe.test(name))
630
643
  throw illegal(name, "name");
631
644
 
632
645
  skip("=");
@@ -653,7 +666,7 @@ function parse(source, root, options) {
653
666
  function parseOneOf(parent, token, depth) {
654
667
 
655
668
  /* istanbul ignore if */
656
- if (!nameRe.test(token = next()))
669
+ if ((token = next()) === null || !nameRe.test(token))
657
670
  throw illegal(token, "name");
658
671
 
659
672
  var oneof = new OneOf(applyCase(token));
@@ -672,7 +685,7 @@ function parse(source, root, options) {
672
685
  function parseEnum(parent, token) {
673
686
 
674
687
  /* istanbul ignore if */
675
- if (!nameRe.test(token = next()))
688
+ if ((token = next()) === null || !nameRe.test(token))
676
689
  throw illegal(token, "name");
677
690
 
678
691
  var enm = new Enum(token);
@@ -745,6 +758,9 @@ function parse(source, root, options) {
745
758
  }
746
759
 
747
760
  while (token !== "=") {
761
+ if (token === null) {
762
+ throw illegal(token, "end of input");
763
+ }
748
764
  if (token === "(") {
749
765
  var parensValue = next();
750
766
  skip(")");
@@ -851,15 +867,12 @@ function parse(source, root, options) {
851
867
  // lift json_name onto Field
852
868
  if (name === "json_name" && parent instanceof Field) {
853
869
  parent.jsonName = value;
854
- return;
855
870
  }
856
871
  if (parent.setOption)
857
872
  parent.setOption(name, value);
858
873
  }
859
874
 
860
875
  function setParsedOption(parent, name, value, propName) {
861
- if (name === "json_name" && parent instanceof Field)
862
- return; // lifted onto Field#jsonName above
863
876
  if (parent.setParsedOption)
864
877
  parent.setParsedOption(name, value, propName);
865
878
  }
@@ -881,7 +894,7 @@ function parse(source, root, options) {
881
894
  throw Error("max depth exceeded");
882
895
 
883
896
  /* istanbul ignore if */
884
- if (!nameRe.test(token = next()))
897
+ if ((token = next()) === null || !nameRe.test(token))
885
898
  throw illegal(token, "service name");
886
899
 
887
900
  var service = new Service(token);
@@ -959,7 +972,7 @@ function parse(source, root, options) {
959
972
  function parseExtension(parent, token, depth) {
960
973
 
961
974
  /* istanbul ignore if */
962
- if (!typeRefRe.test(token = next()))
975
+ if ((token = next()) === null || !typeRefRe.test(token))
963
976
  throw illegal(token, "reference");
964
977
 
965
978
  var reference = token;