protobufjs 8.1.5-experimental → 8.2.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 (76) hide show
  1. package/README.md +219 -565
  2. package/dist/light/protobuf.js +1986 -1483
  3. package/dist/light/protobuf.js.map +1 -1
  4. package/dist/light/protobuf.min.js +3 -3
  5. package/dist/light/protobuf.min.js.map +1 -1
  6. package/dist/minimal/protobuf.js +1122 -861
  7. package/dist/minimal/protobuf.js.map +1 -1
  8. package/dist/minimal/protobuf.min.js +3 -3
  9. package/dist/minimal/protobuf.min.js.map +1 -1
  10. package/dist/protobuf.js +2089 -1513
  11. package/dist/protobuf.js.map +1 -1
  12. package/dist/protobuf.min.js +3 -3
  13. package/dist/protobuf.min.js.map +1 -1
  14. package/ext/README.md +81 -0
  15. package/ext/descriptor/README.md +3 -70
  16. package/ext/descriptor/index.d.ts +1 -191
  17. package/ext/descriptor/index.js +1 -1156
  18. package/ext/descriptor.d.ts +309 -0
  19. package/ext/descriptor.js +1236 -0
  20. package/ext/textformat.d.ts +30 -0
  21. package/ext/textformat.js +1249 -0
  22. package/google/protobuf/compiler/plugin.json +126 -0
  23. package/google/protobuf/compiler/plugin.proto +47 -0
  24. package/google/protobuf/descriptor.json +114 -10
  25. package/google/protobuf/descriptor.proto +35 -9
  26. package/index.d.ts +590 -476
  27. package/package.json +23 -38
  28. package/src/converter.js +60 -24
  29. package/src/decoder.js +122 -49
  30. package/src/encoder.js +10 -2
  31. package/src/enum.js +4 -1
  32. package/src/field.js +10 -7
  33. package/src/mapfield.js +1 -0
  34. package/src/message.js +7 -6
  35. package/src/method.js +4 -3
  36. package/src/namespace.js +23 -12
  37. package/src/object.js +24 -19
  38. package/src/oneof.js +2 -0
  39. package/src/parse.js +114 -46
  40. package/src/reader.js +145 -30
  41. package/src/reader_buffer.js +24 -3
  42. package/src/root.js +7 -4
  43. package/src/service.js +12 -6
  44. package/src/tokenize.js +6 -1
  45. package/src/type.js +48 -25
  46. package/src/types.js +1 -1
  47. package/src/util/aspromise.d.ts +13 -0
  48. package/src/util/aspromise.js +52 -0
  49. package/src/util/base64.d.ts +32 -0
  50. package/src/util/base64.js +146 -0
  51. package/src/util/codegen.d.ts +31 -0
  52. package/src/util/codegen.js +113 -0
  53. package/src/util/eventemitter.d.ts +45 -0
  54. package/src/util/eventemitter.js +84 -0
  55. package/src/util/fetch.d.ts +56 -0
  56. package/src/util/fetch.js +112 -0
  57. package/src/util/float.d.ts +83 -0
  58. package/src/util/float.js +335 -0
  59. package/src/util/fs.js +11 -0
  60. package/src/util/inquire.d.ts +10 -0
  61. package/src/util/inquire.js +38 -0
  62. package/src/util/minimal.js +67 -12
  63. package/src/util/path.d.ts +22 -0
  64. package/src/util/path.js +72 -0
  65. package/src/util/patterns.js +8 -0
  66. package/src/util/pool.d.ts +32 -0
  67. package/src/util/pool.js +48 -0
  68. package/src/util/utf8.d.ts +24 -0
  69. package/src/util/utf8.js +104 -0
  70. package/src/util.js +30 -13
  71. package/src/verifier.js +7 -4
  72. package/src/wrappers.js +4 -3
  73. package/src/writer.js +27 -4
  74. package/src/writer_buffer.js +12 -0
  75. package/tsconfig.json +2 -2
  76. package/ext/descriptor/test.js +0 -54
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ module.exports = pool;
3
+
4
+ /**
5
+ * An allocator as used by {@link util.pool}.
6
+ * @typedef PoolAllocator
7
+ * @type {function}
8
+ * @param {number} size Buffer size
9
+ * @returns {Uint8Array} Buffer
10
+ */
11
+
12
+ /**
13
+ * A slicer as used by {@link util.pool}.
14
+ * @typedef PoolSlicer
15
+ * @type {function}
16
+ * @param {number} start Start offset
17
+ * @param {number} end End offset
18
+ * @returns {Uint8Array} Buffer slice
19
+ * @this {Uint8Array}
20
+ */
21
+
22
+ /**
23
+ * A general purpose buffer pool.
24
+ * @memberof util
25
+ * @function
26
+ * @param {PoolAllocator} alloc Allocator
27
+ * @param {PoolSlicer} slice Slicer
28
+ * @param {number} [size=8192] Slab size
29
+ * @returns {PoolAllocator} Pooled allocator
30
+ */
31
+ function pool(alloc, slice, size) {
32
+ var SIZE = size || 8192;
33
+ var MAX = SIZE >>> 1;
34
+ var slab = null;
35
+ var offset = SIZE;
36
+ return function pool_alloc(size) {
37
+ if (size < 1 || size > MAX)
38
+ return alloc(size);
39
+ if (offset + size > SIZE) {
40
+ slab = alloc(SIZE);
41
+ offset = 0;
42
+ }
43
+ var buf = slice.call(slab, offset, offset += size);
44
+ if (offset & 7) // align to 32 bit
45
+ offset = (offset | 7) + 1;
46
+ return buf;
47
+ };
48
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Calculates the UTF8 byte length of a string.
3
+ * @param {string} string String
4
+ * @returns {number} Byte length
5
+ */
6
+ export function length(string: string): number;
7
+
8
+ /**
9
+ * Reads UTF8 bytes as a string.
10
+ * @param {Uint8Array} buffer Source buffer
11
+ * @param {number} start Source start
12
+ * @param {number} end Source end
13
+ * @returns {string} String read
14
+ */
15
+ export function read(buffer: Uint8Array, start: number, end: number): string;
16
+
17
+ /**
18
+ * Writes a string as UTF8 bytes.
19
+ * @param {string} string Source string
20
+ * @param {Uint8Array} buffer Destination buffer
21
+ * @param {number} offset Destination offset
22
+ * @returns {number} Bytes written
23
+ */
24
+ export function write(string: string, buffer: Uint8Array, offset: number): number;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * A minimal UTF8 implementation for number arrays.
5
+ * @memberof util
6
+ * @namespace
7
+ */
8
+ var utf8 = exports,
9
+ replacementChar = "\ufffd";
10
+
11
+ /**
12
+ * Calculates the UTF8 byte length of a string.
13
+ * @param {string} string String
14
+ * @returns {number} Byte length
15
+ */
16
+ utf8.length = function utf8_length(string) {
17
+ var len = 0,
18
+ c = 0;
19
+ for (var i = 0; i < string.length; ++i) {
20
+ c = string.charCodeAt(i);
21
+ if (c < 128)
22
+ len += 1;
23
+ else if (c < 2048)
24
+ len += 2;
25
+ else if ((c & 0xFC00) === 0xD800 && (string.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
26
+ ++i;
27
+ len += 4;
28
+ } else
29
+ len += 3;
30
+ }
31
+ return len;
32
+ };
33
+
34
+ /**
35
+ * Reads UTF8 bytes as a string.
36
+ * @param {Uint8Array} buffer Source buffer
37
+ * @param {number} start Source start
38
+ * @param {number} end Source end
39
+ * @returns {string} String read
40
+ */
41
+ utf8.read = function utf8_read(buffer, start, end) {
42
+ if (end - start < 1) {
43
+ return "";
44
+ }
45
+
46
+ var str = "";
47
+ for (var i = start; i < end;) {
48
+ var t = buffer[i++];
49
+ if (t <= 0x7F) {
50
+ str += String.fromCharCode(t);
51
+ } else if (t >= 0xC0 && t < 0xE0) {
52
+ var c2 = (t & 0x1F) << 6 | buffer[i++] & 0x3F;
53
+ str += c2 >= 0x80 ? String.fromCharCode(c2) : replacementChar;
54
+ } else if (t >= 0xE0 && t < 0xF0) {
55
+ var c3 = (t & 0xF) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F;
56
+ str += c3 >= 0x800 ? String.fromCharCode(c3) : replacementChar;
57
+ } else if (t >= 0xF0) {
58
+ var t2 = (t & 7) << 18 | (buffer[i++] & 0x3F) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F;
59
+ if (t2 < 0x10000 || t2 > 0x10FFFF)
60
+ str += replacementChar;
61
+ else {
62
+ t2 -= 0x10000;
63
+ str += String.fromCharCode(0xD800 + (t2 >> 10));
64
+ str += String.fromCharCode(0xDC00 + (t2 & 0x3FF));
65
+ }
66
+ }
67
+ }
68
+
69
+ return str;
70
+ };
71
+
72
+ /**
73
+ * Writes a string as UTF8 bytes.
74
+ * @param {string} string Source string
75
+ * @param {Uint8Array} buffer Destination buffer
76
+ * @param {number} offset Destination offset
77
+ * @returns {number} Bytes written
78
+ */
79
+ utf8.write = function utf8_write(string, buffer, offset) {
80
+ var start = offset,
81
+ c1, // character 1
82
+ c2; // character 2
83
+ for (var i = 0; i < string.length; ++i) {
84
+ c1 = string.charCodeAt(i);
85
+ if (c1 < 128) {
86
+ buffer[offset++] = c1;
87
+ } else if (c1 < 2048) {
88
+ buffer[offset++] = c1 >> 6 | 192;
89
+ buffer[offset++] = c1 & 63 | 128;
90
+ } else if ((c1 & 0xFC00) === 0xD800 && ((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
91
+ c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
92
+ ++i;
93
+ buffer[offset++] = c1 >> 18 | 240;
94
+ buffer[offset++] = c1 >> 12 & 63 | 128;
95
+ buffer[offset++] = c1 >> 6 & 63 | 128;
96
+ buffer[offset++] = c1 & 63 | 128;
97
+ } else {
98
+ buffer[offset++] = c1 >> 12 | 224;
99
+ buffer[offset++] = c1 >> 6 & 63 | 128;
100
+ buffer[offset++] = c1 & 63 | 128;
101
+ }
102
+ }
103
+ return offset - start;
104
+ };
package/src/util.js CHANGED
@@ -11,15 +11,33 @@ var roots = require("./roots");
11
11
  var Type, // cyclic
12
12
  Enum;
13
13
 
14
- util.codegen = require("@protobufjs/codegen");
15
- util.fetch = require("@protobufjs/fetch");
16
- util.path = require("@protobufjs/path");
14
+ util.codegen = require("./util/codegen");
15
+ util.fetch = require("./util/fetch");
16
+ util.path = require("./util/path");
17
+ util.patterns = require("./util/patterns");
18
+
19
+ var reservedRe = util.patterns.reservedRe,
20
+ unsafePropertyRe = util.patterns.unsafePropertyRe;
17
21
 
18
22
  /**
19
23
  * Node's fs module if available.
20
24
  * @type {Object.<string,*>}
21
25
  */
22
- util.fs = util.inquire("fs");
26
+ util.fs = require("./util/fs");
27
+
28
+ /**
29
+ * Checks a recursion depth.
30
+ * @param {number|undefined} depth Depth of recursion
31
+ * @returns {number} Depth of recursion
32
+ * @throws {Error} If depth exceeds util.recursionLimit
33
+ */
34
+ util.checkDepth = function checkDepth(depth) {
35
+ if (depth === undefined)
36
+ depth = 0;
37
+ if (depth > util.recursionLimit)
38
+ throw Error("max depth exceeded");
39
+ return depth;
40
+ };
23
41
 
24
42
  /**
25
43
  * Converts an object's values to an array.
@@ -55,16 +73,13 @@ util.toObject = function toObject(array) {
55
73
  return object;
56
74
  };
57
75
 
58
- var safePropBackslashRe = /\\/g,
59
- safePropQuoteRe = /"/g;
60
-
61
76
  /**
62
77
  * Tests whether the specified name is a reserved word in JS.
63
78
  * @param {string} name Name to test
64
79
  * @returns {boolean} `true` if reserved, otherwise `false`
65
80
  */
66
81
  util.isReserved = function isReserved(name) {
67
- return /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/.test(name);
82
+ return reservedRe.test(name);
68
83
  };
69
84
 
70
85
  /**
@@ -73,8 +88,8 @@ util.isReserved = function isReserved(name) {
73
88
  * @returns {string} Safe accessor
74
89
  */
75
90
  util.safeProp = function safeProp(prop) {
76
- if (!/^[$\w_]+$/.test(prop) || util.isReserved(prop))
77
- return "[\"" + prop.replace(safePropBackslashRe, "\\\\").replace(safePropQuoteRe, "\\\"") + "\"]";
91
+ if (!/^[$\w_]+$/.test(prop) || reservedRe.test(prop))
92
+ return "[" + JSON.stringify(prop) + "]";
78
93
  return "." + prop;
79
94
  };
80
95
 
@@ -117,6 +132,7 @@ util.compareFieldsById = function compareFieldsById(a, b) {
117
132
  * @returns {Type} Reflected type
118
133
  * @template T extends Message<T>
119
134
  * @property {Root} root Decorators root
135
+ * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
120
136
  */
121
137
  util.decorateType = function decorateType(ctor, typeName) {
122
138
 
@@ -148,6 +164,7 @@ var decorateEnumIndex = 0;
148
164
  * Decorator helper for enums (TypeScript).
149
165
  * @param {Object} object Enum object
150
166
  * @returns {Enum} Reflected enum
167
+ * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
151
168
  */
152
169
  util.decorateEnum = function decorateEnum(object) {
153
170
 
@@ -177,9 +194,8 @@ util.decorateEnum = function decorateEnum(object) {
177
194
  util.setProperty = function setProperty(dst, path, value, ifNotSet) {
178
195
  function setProp(dst, path, value) {
179
196
  var part = path.shift();
180
- if (part === "__proto__" || part === "prototype") {
181
- return dst;
182
- }
197
+ if (unsafePropertyRe.test(part))
198
+ return dst;
183
199
  if (path.length > 0) {
184
200
  dst[part] = setProp(dst[part] || {}, path, value);
185
201
  } else {
@@ -207,6 +223,7 @@ util.setProperty = function setProperty(dst, path, value, ifNotSet) {
207
223
  * @name util.decorateRoot
208
224
  * @type {Root}
209
225
  * @readonly
226
+ * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
210
227
  */
211
228
  Object.defineProperty(util, "decorateRoot", {
212
229
  get: function() {
package/src/verifier.js CHANGED
@@ -32,7 +32,7 @@ function genVerifyValue(gen, field, fieldIndex, ref) {
32
32
  } else {
33
33
  gen
34
34
  ("{")
35
- ("var e=types[%i].verify(%s);", fieldIndex, ref)
35
+ ("var e=types[%i].verify(%s,q+1);", fieldIndex, ref)
36
36
  ("if(e)")
37
37
  ("return%j+e", field.name + ".")
38
38
  ("}");
@@ -122,9 +122,12 @@ function genVerifyKey(gen, field, ref) {
122
122
  function verifier(mtype) {
123
123
  /* eslint-disable no-unexpected-multiline */
124
124
 
125
- var gen = util.codegen(["m"], mtype.name + "$verify")
125
+ var gen = util.codegen(["m", "q"], mtype.name + "$verify")
126
126
  ("if(typeof m!==\"object\"||m===null)")
127
- ("return%j", "object expected");
127
+ ("return%j", "object expected")
128
+ ("if(q===undefined)q=0")
129
+ ("if(q>util.recursionLimit)")
130
+ ("return%j", "max depth exceeded");
128
131
  var oneofs = mtype.oneofsArray,
129
132
  seenFirstField = {};
130
133
  if (oneofs.length) gen
@@ -174,4 +177,4 @@ function verifier(mtype) {
174
177
  return gen
175
178
  ("return null");
176
179
  /* eslint-enable no-unexpected-multiline */
177
- }
180
+ }
package/src/wrappers.js CHANGED
@@ -38,7 +38,7 @@ var Message = require("./message");
38
38
  // Custom wrapper for Any
39
39
  wrappers[".google.protobuf.Any"] = {
40
40
 
41
- fromObject: function(object) {
41
+ fromObject: function(object, depth) {
42
42
 
43
43
  // unwrap value type if mapped
44
44
  if (object && object["@type"]) {
@@ -54,14 +54,15 @@ wrappers[".google.protobuf.Any"] = {
54
54
  if (type_url.indexOf("/") === -1) {
55
55
  type_url = "/" + type_url;
56
56
  }
57
+ var nextDepth = depth === undefined ? 1 : depth + 1;
57
58
  return this.create({
58
59
  type_url: type_url,
59
- value: type.encode(type.fromObject(object)).finish()
60
+ value: type.encode(type.fromObject(object, nextDepth)).finish()
60
61
  });
61
62
  }
62
63
  }
63
64
 
64
- return this.fromObject(object);
65
+ return this.fromObject(object, depth);
65
66
  },
66
67
 
67
68
  toObject: function(message, options) {
package/src/writer.js CHANGED
@@ -383,6 +383,16 @@ Writer.prototype.bytes = function write_bytes(value) {
383
383
  return this.uint32(len)._push(writeBytes, len, value);
384
384
  };
385
385
 
386
+ /**
387
+ * Writes raw bytes without a tag or length prefix.
388
+ * @param {Uint8Array} value Raw bytes
389
+ * @returns {Writer} `this`
390
+ */
391
+ Writer.prototype.raw = function write_raw(value) {
392
+ var len = value.length >>> 0;
393
+ return len ? this._push(writeBytes, len, value) : this;
394
+ };
395
+
386
396
  /**
387
397
  * Writes a string.
388
398
  * @param {string} value Value to write
@@ -446,15 +456,28 @@ Writer.prototype.ldelim = function ldelim() {
446
456
  * @returns {Uint8Array} Finished buffer
447
457
  */
448
458
  Writer.prototype.finish = function finish() {
449
- var head = this.head.next, // skip noop
450
- buf = this.constructor.alloc(this.len),
451
- pos = 0;
459
+ return this.finishInto(this.constructor.alloc(this.len), 0);
460
+ };
461
+
462
+ /**
463
+ * Finishes the write operation, writing into the provided buffer.
464
+ * The caller must ensure that `buf` has enough space starting at `offset`
465
+ * to hold {@link Writer#len} bytes.
466
+ * @param {T} buf Target buffer
467
+ * @param {number} [offset=0] Offset to start writing at
468
+ * @returns {T} The provided buffer
469
+ * @template T extends Uint8Array
470
+ */
471
+ Writer.prototype.finishInto = function finishInto(buf, offset) {
472
+ if (offset === undefined)
473
+ offset = 0;
474
+ var head = this.head.next,
475
+ pos = offset;
452
476
  while (head) {
453
477
  head.fn(head.val, buf, pos);
454
478
  pos += head.len;
455
479
  head = head.next;
456
480
  }
457
- // this.head = this.tail = null;
458
481
  return buf;
459
482
  };
460
483
 
@@ -54,6 +54,18 @@ BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
54
54
  return this;
55
55
  };
56
56
 
57
+ /**
58
+ * Writes raw bytes without a tag or length prefix.
59
+ * @name BufferWriter#raw
60
+ * @function
61
+ * @param {Uint8Array} value Raw bytes
62
+ * @returns {BufferWriter} `this`
63
+ */
64
+ BufferWriter.prototype.raw = function write_raw_buffer(value) {
65
+ var len = value.length >>> 0;
66
+ return len ? this._push(BufferWriter.writeBytesBuffer, len, value) : this;
67
+ };
68
+
57
69
  function writeStringBuffer(val, buf, pos) {
58
70
  if (val.length < 40) // plain js is faster for short strings (probably due to redundant assertions)
59
71
  util.utf8.write(val, buf, pos);
package/tsconfig.json CHANGED
@@ -3,6 +3,6 @@
3
3
  "target": "ES5",
4
4
  "experimentalDecorators": true,
5
5
  "emitDecoratorMetadata": true,
6
- "esModuleInterop": true,
6
+ "esModuleInterop": true
7
7
  }
8
- }
8
+ }
@@ -1,54 +0,0 @@
1
- /*eslint-disable no-console*/
2
- "use strict";
3
- var protobuf = require("../../"),
4
- descriptor = require(".");
5
-
6
- /* var proto = {
7
- nested: {
8
- Message: {
9
- fields: {
10
- foo: {
11
- type: "string",
12
- id: 1
13
- }
14
- },
15
- nested: {
16
- SubMessage: {
17
- fields: {}
18
- }
19
- }
20
- },
21
- Enum: {
22
- values: {
23
- ONE: 1,
24
- TWO: 2
25
- }
26
- }
27
- }
28
- }; */
29
-
30
- // var root = protobuf.Root.fromJSON(proto).resolveAll();
31
- var root = protobuf.loadSync("tests/data/google/protobuf/descriptor.proto").resolveAll();
32
-
33
- // console.log("Original proto", JSON.stringify(root, null, 2));
34
-
35
- var msg = root.toDescriptor();
36
-
37
- // console.log("\nDescriptor", JSON.stringify(msg.toObject(), null, 2));
38
-
39
- var buf = descriptor.FileDescriptorSet.encode(msg).finish();
40
- var root2 = protobuf.Root.fromDescriptor(buf, "proto2").resolveAll();
41
-
42
- // console.log("\nDecoded proto", JSON.stringify(root2, null, 2));
43
-
44
- var diff = require("deep-diff").diff(root.toJSON(), root2.toJSON());
45
- if (diff) {
46
- diff.forEach(function(diff) {
47
- console.log(diff.kind + " @ " + diff.path.join("."));
48
- console.log("lhs:", typeof diff.lhs, diff.lhs);
49
- console.log("rhs:", typeof diff.rhs, diff.rhs);
50
- console.log();
51
- });
52
- process.exitCode = 1;
53
- } else
54
- console.log("no differences");