protobufjs 8.1.6-experimental → 8.2.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 (76) hide show
  1. package/README.md +225 -570
  2. package/dist/light/protobuf.js +2041 -1482
  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 +1167 -863
  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 +2173 -1527
  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 -190
  17. package/ext/descriptor/index.js +1 -1161
  18. package/ext/descriptor.d.ts +309 -0
  19. package/ext/descriptor.js +1241 -0
  20. package/ext/textformat.d.ts +24 -0
  21. package/ext/textformat.js +1227 -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 +2 -2
  25. package/google/protobuf/descriptor.proto +2 -1
  26. package/index.d.ts +585 -476
  27. package/package.json +22 -40
  28. package/src/converter.js +63 -27
  29. package/src/decoder.js +126 -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 +31 -12
  37. package/src/object.js +24 -19
  38. package/src/oneof.js +2 -0
  39. package/src/parse.js +128 -46
  40. package/src/reader.js +145 -30
  41. package/src/reader_buffer.js +24 -3
  42. package/src/root.js +10 -4
  43. package/src/service.js +15 -6
  44. package/src/tokenize.js +6 -1
  45. package/src/type.js +57 -27
  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 +74 -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 +130 -0
  70. package/src/util.js +18 -13
  71. package/src/verifier.js +7 -4
  72. package/src/wrappers.js +4 -3
  73. package/src/writer.js +33 -5
  74. package/src/writer_buffer.js +18 -1
  75. package/tsconfig.json +2 -2
  76. package/ext/descriptor/test.js +0 -54
@@ -30,15 +30,36 @@ BufferReader._configure = function () {
30
30
  BufferReader.prototype._slice = util.Buffer.prototype.slice;
31
31
  };
32
32
 
33
+ /**
34
+ * Returns raw bytes from the backing buffer without advancing the reader.
35
+ * @name BufferReader#raw
36
+ * @function
37
+ * @param {number} start Start offset
38
+ * @param {number} end End offset
39
+ * @returns {Buffer} Raw bytes
40
+ */
41
+ BufferReader.prototype.raw = function read_raw_buffer(start, end) {
42
+ if (start === end)
43
+ return util.Buffer.alloc(0);
44
+ return this._slice.call(this.buf, start, end);
45
+ };
33
46
 
34
47
  /**
35
48
  * @override
36
49
  */
37
50
  BufferReader.prototype.string = function read_string_buffer() {
38
- var len = this.uint32(); // modifies pos
51
+ var len = this.uint32(), // modifies pos
52
+ start = this.pos,
53
+ end = this.pos + len;
54
+
55
+ /* istanbul ignore if */
56
+ if (end > this.len)
57
+ throw RangeError("index out of range: " + this.pos + " + " + len + " > " + this.len);
58
+
59
+ this.pos = end;
39
60
  return this.buf.utf8Slice
40
- ? this.buf.utf8Slice(this.pos, this.pos = Math.min(this.pos + len, this.len))
41
- : this.buf.toString("utf-8", this.pos, this.pos = Math.min(this.pos + len, this.len));
61
+ ? this.buf.utf8Slice(start, end)
62
+ : this.buf.toString("utf-8", start, end);
42
63
  };
43
64
 
44
65
  /**
package/src/root.js CHANGED
@@ -55,14 +55,19 @@ function Root(options) {
55
55
  * Loads a namespace descriptor into a root namespace.
56
56
  * @param {INamespace} json Namespace descriptor
57
57
  * @param {Root} [root] Root namespace, defaults to create a new one if omitted
58
+ * @param {number} [depth] Current nesting depth, defaults to `0`
58
59
  * @returns {Root} Root namespace
59
60
  */
60
- Root.fromJSON = function fromJSON(json, root) {
61
+ Root.fromJSON = function fromJSON(json, root, depth) {
62
+ if (depth === undefined)
63
+ depth = 0;
64
+ if (depth > util.recursionLimit)
65
+ throw Error("max depth exceeded");
61
66
  if (!root)
62
67
  root = new Root();
63
68
  if (json.options)
64
69
  root.setOptions(json.options);
65
- return root.addJSON(json.nested).resolveAll();
70
+ return root.addJSON(json.nested, depth).resolveAll();
66
71
  };
67
72
 
68
73
  /**
@@ -130,8 +135,9 @@ Root.prototype.load = function load(filename, options, callback) {
130
135
  var idx = filename.lastIndexOf("google/protobuf/");
131
136
  if (idx > -1) {
132
137
  var altname = filename.substring(idx);
133
- if (altname in common) return altname;
138
+ if (Object.prototype.hasOwnProperty.call(common, altname)) return altname;
134
139
  }
140
+ if (Object.prototype.hasOwnProperty.call(common, filename)) return filename;
135
141
  return null;
136
142
  }
137
143
 
@@ -175,7 +181,7 @@ Root.prototype.load = function load(filename, options, callback) {
175
181
  self.files.push(filename);
176
182
 
177
183
  // Shortcut bundled definitions
178
- if (filename in common) {
184
+ if (Object.prototype.hasOwnProperty.call(common, filename)) {
179
185
  if (sync) {
180
186
  process(filename, common[filename]);
181
187
  } else {
package/src/service.js CHANGED
@@ -9,6 +9,8 @@ var Method = require("./method"),
9
9
  util = require("./util"),
10
10
  rpc = require("./rpc");
11
11
 
12
+ var reservedRe = util.patterns.reservedRe;
13
+
12
14
  /**
13
15
  * Constructs a new service instance.
14
16
  * @classdesc Reflected service.
@@ -46,17 +48,22 @@ function Service(name, options) {
46
48
  * Constructs a service from a service descriptor.
47
49
  * @param {string} name Service name
48
50
  * @param {IService} json Service descriptor
51
+ * @param {number} [depth] Current nesting depth, defaults to `0`
49
52
  * @returns {Service} Created service
50
53
  * @throws {TypeError} If arguments are invalid
51
54
  */
52
- Service.fromJSON = function fromJSON(name, json) {
55
+ Service.fromJSON = function fromJSON(name, json, depth) {
56
+ if (depth === undefined)
57
+ depth = 0;
58
+ if (depth > util.recursionLimit)
59
+ throw Error("max depth exceeded");
53
60
  var service = new Service(name, json.options);
54
61
  /* istanbul ignore else */
55
62
  if (json.methods)
56
63
  for (var names = Object.keys(json.methods), i = 0; i < names.length; ++i)
57
64
  service.add(Method.fromJSON(names[i], json.methods[names[i]]));
58
65
  if (json.nested)
59
- service.addJSON(json.nested);
66
+ service.addJSON(json.nested, depth);
60
67
  if (json.edition)
61
68
  service._edition = json.edition;
62
69
  service.comment = json.comment;
@@ -102,8 +109,9 @@ function clearCache(service) {
102
109
  * @override
103
110
  */
104
111
  Service.prototype.get = function get(name) {
105
- return this.methods[name]
106
- || Namespace.prototype.get.call(this, name);
112
+ return Object.prototype.hasOwnProperty.call(this.methods, name)
113
+ ? this.methods[name]
114
+ : Namespace.prototype.get.call(this, name);
107
115
  };
108
116
 
109
117
  /**
@@ -138,12 +146,13 @@ Service.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive
138
146
  * @override
139
147
  */
140
148
  Service.prototype.add = function add(object) {
141
-
142
149
  /* istanbul ignore if */
143
150
  if (this.get(object.name))
144
151
  throw Error("duplicate name '" + object.name + "' in " + this);
145
152
 
146
153
  if (object instanceof Method) {
154
+ if (object.name === "__proto__")
155
+ return this;
147
156
  this.methods[object.name] = object;
148
157
  object.parent = this;
149
158
  return clearCache(this);
@@ -179,7 +188,7 @@ Service.prototype.create = function create(rpcImpl, requestDelimited, responseDe
179
188
  var rpcService = new rpc.Service(rpcImpl, requestDelimited, responseDelimited);
180
189
  for (var i = 0, method; i < /* initializes */ this.methodsArray.length; ++i) {
181
190
  var methodName = util.lcFirst((method = this._methodsArray[i]).resolve().name).replace(/[^$\w_]/g, "");
182
- rpcService[methodName] = util.codegen(["r","c"], util.isReserved(methodName) ? methodName + "_" : methodName)("return this.rpcCall(m,q,s,r,c)")({
191
+ rpcService[methodName] = util.codegen(["r","c"], reservedRe.test(methodName) ? methodName + "_" : methodName)("return this.rpcCall(m,q,s,r,c)")({
183
192
  m: method,
184
193
  q: method.resolvedRequestType.ctor,
185
194
  s: method.resolvedResponseType.ctor
package/src/tokenize.js CHANGED
@@ -225,6 +225,7 @@ function tokenize(source, alternateCommentMode) {
225
225
  curr,
226
226
  start,
227
227
  isDoc,
228
+ nextLineIsComment,
228
229
  isLeadingComment = offset === 0;
229
230
  do {
230
231
  if (offset === length)
@@ -278,7 +279,11 @@ function tokenize(source, alternateCommentMode) {
278
279
  // Trailing comment cannot not be multi-line
279
280
  break;
280
281
  }
281
- } while (isDoubleSlashCommentLine(offset));
282
+ nextLineIsComment = isDoubleSlashCommentLine(offset);
283
+ if (nextLineIsComment) {
284
+ line++;
285
+ }
286
+ } while (nextLineIsComment);
282
287
  } else {
283
288
  offset = Math.min(length, findEndOfLine(offset) + 1);
284
289
  }
package/src/type.js CHANGED
@@ -29,6 +29,7 @@ var Enum = require("./enum"),
29
29
  * @param {Object.<string,*>} [options] Declared options
30
30
  */
31
31
  function Type(name, options) {
32
+ name = name.replace(/\W/g, "");
32
33
  Namespace.call(this, name, options);
33
34
 
34
35
  /**
@@ -170,11 +171,15 @@ Object.defineProperties(Type.prototype, {
170
171
  util.merge(ctor, Message, true);
171
172
 
172
173
  this._ctor = ctor;
174
+ delete this.decode;
175
+ delete this.fromObject;
173
176
 
174
177
  // Messages have non-enumerable default values on their prototype
175
178
  var i = 0;
176
- for (; i < /* initializes */ this.fieldsArray.length; ++i)
177
- this._fieldsArray[i].resolve(); // ensures a proper value
179
+ for (var field; i < /* initializes */ this.fieldsArray.length; ++i) {
180
+ field = this._fieldsArray[i].resolve(); // ensures a proper value
181
+ ctor.prototype[field.name] = field.defaultValue;
182
+ }
178
183
 
179
184
  // Messages have non-enumerable getters and setters for each virtual oneof field
180
185
  var ctorProperties = {};
@@ -204,7 +209,7 @@ Type.generateConstructor = function generateConstructor(mtype) {
204
209
  else if (field.repeated) gen
205
210
  ("this%s=[]", util.safeProp(field.name));
206
211
  return gen
207
- ("if(p)for(var ks=Object.keys(p),i=0;i<ks.length;++i)if(p[ks[i]]!=null)") // omit undefined or null
212
+ ("if(p)for(var ks=Object.keys(p),i=0;i<ks.length;++i)if(p[ks[i]]!=null&&ks[i]!==\"__proto__\")") // omit undefined or null
208
213
  ("this[ks[i]]=p[ks[i]]");
209
214
  /* eslint-enable no-unexpected-multiline */
210
215
  };
@@ -232,9 +237,14 @@ function clearCache(type) {
232
237
  * Creates a message type from a message type descriptor.
233
238
  * @param {string} name Message name
234
239
  * @param {IType} json Message type descriptor
240
+ * @param {number} [depth] Current nesting depth, defaults to `0`
235
241
  * @returns {Type} Created message type
236
242
  */
237
- Type.fromJSON = function fromJSON(name, json) {
243
+ Type.fromJSON = function fromJSON(name, json, depth) {
244
+ if (depth === undefined)
245
+ depth = 0;
246
+ if (depth > util.nestingLimit)
247
+ throw Error("max depth exceeded");
238
248
  var type = new Type(name, json.options);
239
249
  type.extensions = json.extensions;
240
250
  type.reserved = json.reserved;
@@ -261,7 +271,7 @@ Type.fromJSON = function fromJSON(name, json) {
261
271
  ? Enum.fromJSON
262
272
  : nested.methods !== undefined
263
273
  ? Service.fromJSON
264
- : Namespace.fromJSON )(names[i], nested)
274
+ : Namespace.fromJSON )(names[i], nested, depth + 1)
265
275
  );
266
276
  }
267
277
  if (json.extensions && json.extensions.length)
@@ -337,10 +347,13 @@ Type.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(ed
337
347
  * @override
338
348
  */
339
349
  Type.prototype.get = function get(name) {
340
- return this.fields[name]
341
- || this.oneofs && this.oneofs[name]
342
- || this.nested && this.nested[name]
343
- || null;
350
+ if (Object.prototype.hasOwnProperty.call(this.fields, name))
351
+ return this.fields[name];
352
+ if (this.oneofs && Object.prototype.hasOwnProperty.call(this.oneofs, name))
353
+ return this.oneofs[name];
354
+ if (this.nested && Object.prototype.hasOwnProperty.call(this.nested, name))
355
+ return this.nested[name];
356
+ return null;
344
357
  };
345
358
 
346
359
  /**
@@ -351,7 +364,6 @@ Type.prototype.get = function get(name) {
351
364
  * @throws {Error} If there is already a nested object with this name or, if a field, when there is already a field with this id
352
365
  */
353
366
  Type.prototype.add = function add(object) {
354
-
355
367
  if (this.get(object.name))
356
368
  throw Error("duplicate name '" + object.name + "' in " + this);
357
369
 
@@ -367,6 +379,8 @@ Type.prototype.add = function add(object) {
367
379
  throw Error("id " + object.id + " is reserved in " + this);
368
380
  if (this.isReservedName(object.name))
369
381
  throw Error("name '" + object.name + "' is reserved in " + this);
382
+ if (object.name === "__proto__")
383
+ return this;
370
384
 
371
385
  if (object.parent)
372
386
  object.parent.remove(object);
@@ -376,6 +390,8 @@ Type.prototype.add = function add(object) {
376
390
  return clearCache(this);
377
391
  }
378
392
  if (object instanceof OneOf) {
393
+ if (object.name === "__proto__")
394
+ return this;
379
395
  if (!this.oneofs)
380
396
  this.oneofs = {};
381
397
  this.oneofs[object.name] = object;
@@ -468,7 +484,8 @@ Type.prototype.setup = function setup() {
468
484
  this.decode = decoder(this)({
469
485
  Reader : Reader,
470
486
  types : types,
471
- util : util
487
+ util : util,
488
+ C : this.ctor
472
489
  });
473
490
  this.verify = verifier(this)({
474
491
  types : types,
@@ -476,7 +493,8 @@ Type.prototype.setup = function setup() {
476
493
  });
477
494
  this.fromObject = converter.fromObject(this)({
478
495
  types : types,
479
- util : util
496
+ util : util,
497
+ C : this.ctor
480
498
  });
481
499
  this.toObject = converter.toObject(this)({
482
500
  types : types,
@@ -486,15 +504,13 @@ Type.prototype.setup = function setup() {
486
504
  // Inject custom wrappers for common types
487
505
  var wrapper = wrappers[fullName];
488
506
  if (wrapper) {
489
- var originalThis = Object.create(this);
490
- // if (wrapper.fromObject) {
491
- originalThis.fromObject = this.fromObject;
492
- this.fromObject = wrapper.fromObject.bind(originalThis);
493
- // }
494
- // if (wrapper.toObject) {
495
- originalThis.toObject = this.toObject;
496
- this.toObject = wrapper.toObject.bind(originalThis);
497
- // }
507
+ var wrapperThis = Object.create(this);
508
+ // Reuse this type's runtime constructor in wrapper fromObject/toObject
509
+ wrapperThis._ctor = this.ctor;
510
+ wrapperThis.fromObject = this.fromObject;
511
+ this.fromObject = wrapper.fromObject.bind(wrapperThis);
512
+ wrapperThis.toObject = this.toObject;
513
+ this.toObject = wrapper.toObject.bind(wrapperThis);
498
514
  }
499
515
 
500
516
  return this;
@@ -528,8 +544,8 @@ Type.prototype.encodeDelimited = function encodeDelimited(message, writer) {
528
544
  * @throws {Error} If the payload is not a reader or valid buffer
529
545
  * @throws {util.ProtocolError<{}>} If required fields are missing
530
546
  */
531
- Type.prototype.decode = function decode_setup(reader, length) {
532
- return this.setup().decode(reader, length); // overrides this method
547
+ Type.prototype.decode = function decode_setup(reader, length) { // eslint-disable-line no-unused-vars
548
+ return this.setup().decode.apply(this, arguments); // overrides this method
533
549
  };
534
550
 
535
551
  /**
@@ -550,8 +566,8 @@ Type.prototype.decodeDelimited = function decodeDelimited(reader) {
550
566
  * @param {Object.<string,*>} message Plain object to verify
551
567
  * @returns {null|string} `null` if valid, otherwise the reason why it is not
552
568
  */
553
- Type.prototype.verify = function verify_setup(message) {
554
- return this.setup().verify(message); // overrides this method
569
+ Type.prototype.verify = function verify_setup(message) { // eslint-disable-line no-unused-vars
570
+ return this.setup().verify.apply(this, arguments); // overrides this method
555
571
  };
556
572
 
557
573
  /**
@@ -559,8 +575,8 @@ Type.prototype.verify = function verify_setup(message) {
559
575
  * @param {Object.<string,*>} object Plain object to convert
560
576
  * @returns {Message<{}>} Message instance
561
577
  */
562
- Type.prototype.fromObject = function fromObject(object) {
563
- return this.setup().fromObject(object);
578
+ Type.prototype.fromObject = function fromObject(object) { // eslint-disable-line no-unused-vars
579
+ return this.setup().fromObject.apply(this, arguments);
564
580
  };
565
581
 
566
582
  /**
@@ -592,6 +608,18 @@ Type.prototype.toObject = function toObject(message, options) {
592
608
  return this.setup().toObject(message, options);
593
609
  };
594
610
 
611
+ /**
612
+ * Gets the type url for this type.
613
+ * @param {string} [prefix] Custom type url prefix, defaults to `"type.googleapis.com"`
614
+ * @returns {string} The type url
615
+ */
616
+ Type.prototype.getTypeUrl = function getTypeUrl(prefix) {
617
+ if (prefix === undefined)
618
+ prefix = "type.googleapis.com";
619
+ var fullName = this.fullName;
620
+ return prefix + "/" + (fullName.charAt(0) === "." ? fullName.substring(1) : fullName);
621
+ };
622
+
595
623
  /**
596
624
  * Decorator function as returned by {@link Type.d} (TypeScript).
597
625
  * @typedef TypeDecorator
@@ -599,6 +627,7 @@ Type.prototype.toObject = function toObject(message, options) {
599
627
  * @param {Constructor<T>} target Target constructor
600
628
  * @returns {undefined}
601
629
  * @template T extends Message<T>
630
+ * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
602
631
  */
603
632
 
604
633
  /**
@@ -606,6 +635,7 @@ Type.prototype.toObject = function toObject(message, options) {
606
635
  * @param {string} [typeName] Type name, defaults to the constructor's name
607
636
  * @returns {TypeDecorator<T>} Decorator function
608
637
  * @template T extends Message<T>
638
+ * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
609
639
  */
610
640
  Type.d = function decorateType(typeName) {
611
641
  return function typeDecorator(target) {
package/src/types.js CHANGED
@@ -27,7 +27,7 @@ var s = [
27
27
  ];
28
28
 
29
29
  function bake(values, offset) {
30
- var i = 0, o = {};
30
+ var i = 0, o = Object.create(null);
31
31
  offset |= 0;
32
32
  while (i < values.length) o[s[i + offset]] = values[i++];
33
33
  return o;
@@ -0,0 +1,13 @@
1
+ export = asPromise;
2
+
3
+ type asPromiseCallback = (error: Error | null, ...params: any[]) => {};
4
+
5
+ /**
6
+ * Returns a promise from a node-style callback function.
7
+ * @memberof util
8
+ * @param {asPromiseCallback} fn Function to call
9
+ * @param {*} ctx Function context
10
+ * @param {...*} params Function arguments
11
+ * @returns {Promise<*>} Promisified function
12
+ */
13
+ declare function asPromise(fn: asPromiseCallback, ctx: any, ...params: any[]): Promise<any>;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ module.exports = asPromise;
3
+
4
+ /**
5
+ * Callback as used by {@link util.asPromise}.
6
+ * @typedef asPromiseCallback
7
+ * @type {function}
8
+ * @param {Error|null} error Error, if any
9
+ * @param {...*} params Additional arguments
10
+ * @returns {undefined}
11
+ */
12
+
13
+ /**
14
+ * Returns a promise from a node-style callback function.
15
+ * @memberof util
16
+ * @param {asPromiseCallback} fn Function to call
17
+ * @param {*} ctx Function context
18
+ * @param {...*} params Function arguments
19
+ * @returns {Promise<*>} Promisified function
20
+ */
21
+ function asPromise(fn, ctx/*, varargs */) {
22
+ var params = new Array(arguments.length - 1),
23
+ offset = 0,
24
+ index = 2,
25
+ pending = true;
26
+ while (index < arguments.length)
27
+ params[offset++] = arguments[index++];
28
+ return new Promise(function executor(resolve, reject) {
29
+ params[offset] = function callback(err/*, varargs */) {
30
+ if (pending) {
31
+ pending = false;
32
+ if (err)
33
+ reject(err);
34
+ else {
35
+ var params = new Array(arguments.length - 1),
36
+ offset = 0;
37
+ while (offset < params.length)
38
+ params[offset++] = arguments[offset];
39
+ resolve.apply(null, params);
40
+ }
41
+ }
42
+ };
43
+ try {
44
+ fn.apply(ctx || null, params);
45
+ } catch (err) {
46
+ if (pending) {
47
+ pending = false;
48
+ reject(err);
49
+ }
50
+ }
51
+ });
52
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Calculates the byte length of a base64 encoded string.
3
+ * @param {string} string Base64 encoded string
4
+ * @returns {number} Byte length
5
+ */
6
+ export function length(string: string): number;
7
+
8
+ /**
9
+ * Encodes a buffer to a base64 encoded string.
10
+ * @param {Uint8Array} buffer Source buffer
11
+ * @param {number} start Source start
12
+ * @param {number} end Source end
13
+ * @returns {string} Base64 encoded string
14
+ */
15
+ export function encode(buffer: Uint8Array, start: number, end: number): string;
16
+
17
+ /**
18
+ * Decodes a base64 encoded string to a buffer.
19
+ * @param {string} string Source string
20
+ * @param {Uint8Array} buffer Destination buffer
21
+ * @param {number} offset Destination offset
22
+ * @returns {number} Number of bytes written
23
+ * @throws {Error} If encoding is invalid
24
+ */
25
+ export function decode(string: string, buffer: Uint8Array, offset: number): number;
26
+
27
+ /**
28
+ * Tests if the specified string appears to be base64 encoded.
29
+ * @param {string} string String to test
30
+ * @returns {boolean} `true` if it appears to be base64 encoded, otherwise false
31
+ */
32
+ export function test(string: string): boolean;
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * A minimal base64 implementation for number arrays.
5
+ * @memberof util
6
+ * @namespace
7
+ */
8
+ var base64 = exports;
9
+
10
+ /**
11
+ * Calculates the byte length of a base64 encoded string.
12
+ * @param {string} string Base64 encoded string
13
+ * @returns {number} Byte length
14
+ */
15
+ base64.length = function length(string) {
16
+ var p = string.length;
17
+ if (!p)
18
+ return 0;
19
+ while (p > 0 && string.charAt(p - 1) === "=")
20
+ --p;
21
+ return Math.floor(p * 3 / 4);
22
+ };
23
+
24
+ // Base64 encoding table
25
+ var b64 = new Array(64);
26
+
27
+ // Base64 decoding table
28
+ var s64 = new Array(123);
29
+
30
+ // 65..90, 97..122, 48..57, 43, 47
31
+ for (var i = 0; i < 64;)
32
+ s64[b64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i - 59 | 43] = i++;
33
+
34
+ s64[45] = 62; // - -> +
35
+ s64[95] = 63; // _ -> /
36
+
37
+ /**
38
+ * Encodes a buffer to a base64 encoded string.
39
+ * @param {Uint8Array} buffer Source buffer
40
+ * @param {number} start Source start
41
+ * @param {number} end Source end
42
+ * @returns {string} Base64 encoded string
43
+ */
44
+ base64.encode = function encode(buffer, start, end) {
45
+ var parts = null,
46
+ chunk = [];
47
+ var i = 0, // output index
48
+ j = 0, // goto index
49
+ t; // temporary
50
+ while (start < end) {
51
+ var b = buffer[start++];
52
+ switch (j) {
53
+ case 0:
54
+ chunk[i++] = b64[b >> 2];
55
+ t = (b & 3) << 4;
56
+ j = 1;
57
+ break;
58
+ case 1:
59
+ chunk[i++] = b64[t | b >> 4];
60
+ t = (b & 15) << 2;
61
+ j = 2;
62
+ break;
63
+ case 2:
64
+ chunk[i++] = b64[t | b >> 6];
65
+ chunk[i++] = b64[b & 63];
66
+ j = 0;
67
+ break;
68
+ }
69
+ if (i > 8191) {
70
+ (parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
71
+ i = 0;
72
+ }
73
+ }
74
+ if (j) {
75
+ chunk[i++] = b64[t];
76
+ chunk[i++] = 61;
77
+ if (j === 1)
78
+ chunk[i++] = 61;
79
+ }
80
+ if (parts) {
81
+ if (i)
82
+ parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
83
+ return parts.join("");
84
+ }
85
+ return String.fromCharCode.apply(String, chunk.slice(0, i));
86
+ };
87
+
88
+ var invalidEncoding = "invalid encoding";
89
+
90
+ /**
91
+ * Decodes a base64 encoded string to a buffer.
92
+ * @param {string} string Source string
93
+ * @param {Uint8Array} buffer Destination buffer
94
+ * @param {number} offset Destination offset
95
+ * @returns {number} Number of bytes written
96
+ * @throws {Error} If encoding is invalid
97
+ */
98
+ base64.decode = function decode(string, buffer, offset) {
99
+ var start = offset;
100
+ var j = 0, // goto index
101
+ t; // temporary
102
+ for (var i = 0; i < string.length;) {
103
+ var c = string.charCodeAt(i++);
104
+ if (c === 61 && j > 1)
105
+ break;
106
+ if ((c = s64[c]) === undefined)
107
+ throw Error(invalidEncoding);
108
+ switch (j) {
109
+ case 0:
110
+ t = c;
111
+ j = 1;
112
+ break;
113
+ case 1:
114
+ buffer[offset++] = t << 2 | (c & 48) >> 4;
115
+ t = c;
116
+ j = 2;
117
+ break;
118
+ case 2:
119
+ buffer[offset++] = (t & 15) << 4 | (c & 60) >> 2;
120
+ t = c;
121
+ j = 3;
122
+ break;
123
+ case 3:
124
+ buffer[offset++] = (t & 3) << 6 | c;
125
+ j = 0;
126
+ break;
127
+ }
128
+ }
129
+ if (j === 1)
130
+ throw Error(invalidEncoding);
131
+ return offset - start;
132
+ };
133
+
134
+ var base64Re = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,
135
+ base64UrlRe = /[-_]/,
136
+ base64UrlNoPaddingRe = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}(?:==)?|[A-Za-z0-9_-]{3}=?)?$/;
137
+
138
+ /**
139
+ * Tests if the specified string appears to be base64 encoded.
140
+ * @param {string} string String to test
141
+ * @returns {boolean} `true` if probably base64 encoded, otherwise false
142
+ */
143
+ base64.test = function test(string) {
144
+ return base64Re.test(string)
145
+ || base64UrlRe.test(string) && base64UrlNoPaddingRe.test(string);
146
+ };
@@ -0,0 +1,31 @@
1
+ export = codegen;
2
+
3
+ /**
4
+ * Appends code to the function's body.
5
+ * @param [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
6
+ * @param [formatParams] Format parameters
7
+ * @returns Itself or the generated function if finished
8
+ * @throws {Error} If format parameter counts do not match
9
+ */
10
+ type Codegen = (formatStringOrScope?: (string|{ [k: string]: any }), ...formatParams: any[]) => (Codegen|Function);
11
+
12
+ /**
13
+ * Begins generating a function.
14
+ * @param functionParams Function parameter names
15
+ * @param [functionName] Function name if not anonymous
16
+ * @returns Appender that appends code to the function's body
17
+ */
18
+ declare function codegen(functionParams: string[], functionName?: string): Codegen;
19
+
20
+ /**
21
+ * Begins generating a function.
22
+ * @param [functionName] Function name if not anonymous
23
+ * @returns Appender that appends code to the function's body
24
+ */
25
+ declare function codegen(functionName?: string): Codegen;
26
+
27
+ declare namespace codegen {
28
+
29
+ /** When set to `true`, codegen will log generated code to console. Useful for debugging. */
30
+ let verbose: boolean;
31
+ }