protobufjs 8.1.6-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.
- package/README.md +219 -565
- package/dist/light/protobuf.js +1986 -1483
- package/dist/light/protobuf.js.map +1 -1
- package/dist/light/protobuf.min.js +3 -3
- package/dist/light/protobuf.min.js.map +1 -1
- package/dist/minimal/protobuf.js +1122 -861
- package/dist/minimal/protobuf.js.map +1 -1
- package/dist/minimal/protobuf.min.js +3 -3
- package/dist/minimal/protobuf.min.js.map +1 -1
- package/dist/protobuf.js +2089 -1513
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/README.md +81 -0
- package/ext/descriptor/README.md +3 -70
- package/ext/descriptor/index.d.ts +1 -191
- package/ext/descriptor/index.js +1 -1161
- package/ext/descriptor.d.ts +309 -0
- package/ext/descriptor.js +1236 -0
- package/ext/textformat.d.ts +30 -0
- package/ext/textformat.js +1249 -0
- package/google/protobuf/compiler/plugin.json +126 -0
- package/google/protobuf/compiler/plugin.proto +47 -0
- package/google/protobuf/descriptor.json +2 -2
- package/google/protobuf/descriptor.proto +2 -1
- package/index.d.ts +590 -476
- package/package.json +23 -38
- package/src/converter.js +60 -24
- package/src/decoder.js +122 -49
- package/src/encoder.js +10 -2
- package/src/enum.js +4 -1
- package/src/field.js +10 -7
- package/src/mapfield.js +1 -0
- package/src/message.js +7 -6
- package/src/method.js +4 -3
- package/src/namespace.js +23 -12
- package/src/object.js +24 -19
- package/src/oneof.js +2 -0
- package/src/parse.js +114 -46
- package/src/reader.js +145 -30
- package/src/reader_buffer.js +24 -3
- package/src/root.js +7 -4
- package/src/service.js +12 -6
- package/src/tokenize.js +6 -1
- package/src/type.js +48 -25
- package/src/types.js +1 -1
- package/src/util/aspromise.d.ts +13 -0
- package/src/util/aspromise.js +52 -0
- package/src/util/base64.d.ts +32 -0
- package/src/util/base64.js +146 -0
- package/src/util/codegen.d.ts +31 -0
- package/src/util/codegen.js +113 -0
- package/src/util/eventemitter.d.ts +45 -0
- package/src/util/eventemitter.js +84 -0
- package/src/util/fetch.d.ts +56 -0
- package/src/util/fetch.js +112 -0
- package/src/util/float.d.ts +83 -0
- package/src/util/float.js +335 -0
- package/src/util/fs.js +11 -0
- package/src/util/inquire.d.ts +10 -0
- package/src/util/inquire.js +38 -0
- package/src/util/minimal.js +67 -12
- package/src/util/path.d.ts +22 -0
- package/src/util/path.js +72 -0
- package/src/util/patterns.js +8 -0
- package/src/util/pool.d.ts +32 -0
- package/src/util/pool.js +48 -0
- package/src/util/utf8.d.ts +24 -0
- package/src/util/utf8.js +104 -0
- package/src/util.js +30 -13
- package/src/verifier.js +7 -4
- package/src/wrappers.js +4 -3
- package/src/writer.js +27 -4
- package/src/writer_buffer.js +12 -0
- package/tsconfig.json +2 -2
- package/ext/descriptor/test.js +0 -54
package/src/root.js
CHANGED
|
@@ -55,14 +55,16 @@ 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
|
+
depth = util.checkDepth(depth);
|
|
61
63
|
if (!root)
|
|
62
64
|
root = new Root();
|
|
63
65
|
if (json.options)
|
|
64
66
|
root.setOptions(json.options);
|
|
65
|
-
return root.addJSON(json.nested).resolveAll();
|
|
67
|
+
return root.addJSON(json.nested, depth).resolveAll();
|
|
66
68
|
};
|
|
67
69
|
|
|
68
70
|
/**
|
|
@@ -130,8 +132,9 @@ Root.prototype.load = function load(filename, options, callback) {
|
|
|
130
132
|
var idx = filename.lastIndexOf("google/protobuf/");
|
|
131
133
|
if (idx > -1) {
|
|
132
134
|
var altname = filename.substring(idx);
|
|
133
|
-
if (altname
|
|
135
|
+
if (Object.prototype.hasOwnProperty.call(common, altname)) return altname;
|
|
134
136
|
}
|
|
137
|
+
if (Object.prototype.hasOwnProperty.call(common, filename)) return filename;
|
|
135
138
|
return null;
|
|
136
139
|
}
|
|
137
140
|
|
|
@@ -175,7 +178,7 @@ Root.prototype.load = function load(filename, options, callback) {
|
|
|
175
178
|
self.files.push(filename);
|
|
176
179
|
|
|
177
180
|
// Shortcut bundled definitions
|
|
178
|
-
if (filename
|
|
181
|
+
if (Object.prototype.hasOwnProperty.call(common, filename)) {
|
|
179
182
|
if (sync) {
|
|
180
183
|
process(filename, common[filename]);
|
|
181
184
|
} 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,19 @@ 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
|
+
depth = util.checkDepth(depth);
|
|
53
57
|
var service = new Service(name, json.options);
|
|
54
58
|
/* istanbul ignore else */
|
|
55
59
|
if (json.methods)
|
|
56
60
|
for (var names = Object.keys(json.methods), i = 0; i < names.length; ++i)
|
|
57
61
|
service.add(Method.fromJSON(names[i], json.methods[names[i]]));
|
|
58
62
|
if (json.nested)
|
|
59
|
-
service.addJSON(json.nested);
|
|
63
|
+
service.addJSON(json.nested, depth);
|
|
60
64
|
if (json.edition)
|
|
61
65
|
service._edition = json.edition;
|
|
62
66
|
service.comment = json.comment;
|
|
@@ -102,8 +106,9 @@ function clearCache(service) {
|
|
|
102
106
|
* @override
|
|
103
107
|
*/
|
|
104
108
|
Service.prototype.get = function get(name) {
|
|
105
|
-
return this.methods
|
|
106
|
-
|
|
109
|
+
return Object.prototype.hasOwnProperty.call(this.methods, name)
|
|
110
|
+
? this.methods[name]
|
|
111
|
+
: Namespace.prototype.get.call(this, name);
|
|
107
112
|
};
|
|
108
113
|
|
|
109
114
|
/**
|
|
@@ -138,12 +143,13 @@ Service.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive
|
|
|
138
143
|
* @override
|
|
139
144
|
*/
|
|
140
145
|
Service.prototype.add = function add(object) {
|
|
141
|
-
|
|
142
146
|
/* istanbul ignore if */
|
|
143
147
|
if (this.get(object.name))
|
|
144
148
|
throw Error("duplicate name '" + object.name + "' in " + this);
|
|
145
149
|
|
|
146
150
|
if (object instanceof Method) {
|
|
151
|
+
if (object.name === "__proto__")
|
|
152
|
+
return this;
|
|
147
153
|
this.methods[object.name] = object;
|
|
148
154
|
object.parent = this;
|
|
149
155
|
return clearCache(this);
|
|
@@ -179,7 +185,7 @@ Service.prototype.create = function create(rpcImpl, requestDelimited, responseDe
|
|
|
179
185
|
var rpcService = new rpc.Service(rpcImpl, requestDelimited, responseDelimited);
|
|
180
186
|
for (var i = 0, method; i < /* initializes */ this.methodsArray.length; ++i) {
|
|
181
187
|
var methodName = util.lcFirst((method = this._methodsArray[i]).resolve().name).replace(/[^$\w_]/g, "");
|
|
182
|
-
rpcService[methodName] = util.codegen(["r","c"],
|
|
188
|
+
rpcService[methodName] = util.codegen(["r","c"], reservedRe.test(methodName) ? methodName + "_" : methodName)("return this.rpcCall(m,q,s,r,c)")({
|
|
183
189
|
m: method,
|
|
184
190
|
q: method.resolvedRequestType.ctor,
|
|
185
191
|
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
|
-
|
|
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
|
/**
|
|
@@ -173,8 +174,10 @@ Object.defineProperties(Type.prototype, {
|
|
|
173
174
|
|
|
174
175
|
// Messages have non-enumerable default values on their prototype
|
|
175
176
|
var i = 0;
|
|
176
|
-
for (; i < /* initializes */ this.fieldsArray.length; ++i)
|
|
177
|
-
this._fieldsArray[i].resolve(); // ensures a proper value
|
|
177
|
+
for (var field; i < /* initializes */ this.fieldsArray.length; ++i) {
|
|
178
|
+
field = this._fieldsArray[i].resolve(); // ensures a proper value
|
|
179
|
+
ctor.prototype[field.name] = field.defaultValue;
|
|
180
|
+
}
|
|
178
181
|
|
|
179
182
|
// Messages have non-enumerable getters and setters for each virtual oneof field
|
|
180
183
|
var ctorProperties = {};
|
|
@@ -204,7 +207,7 @@ Type.generateConstructor = function generateConstructor(mtype) {
|
|
|
204
207
|
else if (field.repeated) gen
|
|
205
208
|
("this%s=[]", util.safeProp(field.name));
|
|
206
209
|
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
|
|
210
|
+
("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
211
|
("this[ks[i]]=p[ks[i]]");
|
|
209
212
|
/* eslint-enable no-unexpected-multiline */
|
|
210
213
|
};
|
|
@@ -232,9 +235,11 @@ function clearCache(type) {
|
|
|
232
235
|
* Creates a message type from a message type descriptor.
|
|
233
236
|
* @param {string} name Message name
|
|
234
237
|
* @param {IType} json Message type descriptor
|
|
238
|
+
* @param {number} [depth] Current nesting depth, defaults to `0`
|
|
235
239
|
* @returns {Type} Created message type
|
|
236
240
|
*/
|
|
237
|
-
Type.fromJSON = function fromJSON(name, json) {
|
|
241
|
+
Type.fromJSON = function fromJSON(name, json, depth) {
|
|
242
|
+
depth = util.checkDepth(depth);
|
|
238
243
|
var type = new Type(name, json.options);
|
|
239
244
|
type.extensions = json.extensions;
|
|
240
245
|
type.reserved = json.reserved;
|
|
@@ -261,7 +266,7 @@ Type.fromJSON = function fromJSON(name, json) {
|
|
|
261
266
|
? Enum.fromJSON
|
|
262
267
|
: nested.methods !== undefined
|
|
263
268
|
? Service.fromJSON
|
|
264
|
-
: Namespace.fromJSON )(names[i], nested)
|
|
269
|
+
: Namespace.fromJSON )(names[i], nested, depth + 1)
|
|
265
270
|
);
|
|
266
271
|
}
|
|
267
272
|
if (json.extensions && json.extensions.length)
|
|
@@ -337,10 +342,13 @@ Type.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(ed
|
|
|
337
342
|
* @override
|
|
338
343
|
*/
|
|
339
344
|
Type.prototype.get = function get(name) {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
345
|
+
if (Object.prototype.hasOwnProperty.call(this.fields, name))
|
|
346
|
+
return this.fields[name];
|
|
347
|
+
if (this.oneofs && Object.prototype.hasOwnProperty.call(this.oneofs, name))
|
|
348
|
+
return this.oneofs[name];
|
|
349
|
+
if (this.nested && Object.prototype.hasOwnProperty.call(this.nested, name))
|
|
350
|
+
return this.nested[name];
|
|
351
|
+
return null;
|
|
344
352
|
};
|
|
345
353
|
|
|
346
354
|
/**
|
|
@@ -351,7 +359,6 @@ Type.prototype.get = function get(name) {
|
|
|
351
359
|
* @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
360
|
*/
|
|
353
361
|
Type.prototype.add = function add(object) {
|
|
354
|
-
|
|
355
362
|
if (this.get(object.name))
|
|
356
363
|
throw Error("duplicate name '" + object.name + "' in " + this);
|
|
357
364
|
|
|
@@ -367,6 +374,8 @@ Type.prototype.add = function add(object) {
|
|
|
367
374
|
throw Error("id " + object.id + " is reserved in " + this);
|
|
368
375
|
if (this.isReservedName(object.name))
|
|
369
376
|
throw Error("name '" + object.name + "' is reserved in " + this);
|
|
377
|
+
if (object.name === "__proto__")
|
|
378
|
+
return this;
|
|
370
379
|
|
|
371
380
|
if (object.parent)
|
|
372
381
|
object.parent.remove(object);
|
|
@@ -376,6 +385,8 @@ Type.prototype.add = function add(object) {
|
|
|
376
385
|
return clearCache(this);
|
|
377
386
|
}
|
|
378
387
|
if (object instanceof OneOf) {
|
|
388
|
+
if (object.name === "__proto__")
|
|
389
|
+
return this;
|
|
379
390
|
if (!this.oneofs)
|
|
380
391
|
this.oneofs = {};
|
|
381
392
|
this.oneofs[object.name] = object;
|
|
@@ -486,15 +497,13 @@ Type.prototype.setup = function setup() {
|
|
|
486
497
|
// Inject custom wrappers for common types
|
|
487
498
|
var wrapper = wrappers[fullName];
|
|
488
499
|
if (wrapper) {
|
|
489
|
-
var
|
|
490
|
-
//
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
this.toObject = wrapper.toObject.bind(originalThis);
|
|
497
|
-
// }
|
|
500
|
+
var wrapperThis = Object.create(this);
|
|
501
|
+
// Reuse this type's runtime constructor in wrapper fromObject/toObject
|
|
502
|
+
wrapperThis._ctor = this.ctor;
|
|
503
|
+
wrapperThis.fromObject = this.fromObject;
|
|
504
|
+
this.fromObject = wrapper.fromObject.bind(wrapperThis);
|
|
505
|
+
wrapperThis.toObject = this.toObject;
|
|
506
|
+
this.toObject = wrapper.toObject.bind(wrapperThis);
|
|
498
507
|
}
|
|
499
508
|
|
|
500
509
|
return this;
|
|
@@ -528,8 +537,8 @@ Type.prototype.encodeDelimited = function encodeDelimited(message, writer) {
|
|
|
528
537
|
* @throws {Error} If the payload is not a reader or valid buffer
|
|
529
538
|
* @throws {util.ProtocolError<{}>} If required fields are missing
|
|
530
539
|
*/
|
|
531
|
-
Type.prototype.decode = function decode_setup(reader, length) {
|
|
532
|
-
return this.setup().decode(
|
|
540
|
+
Type.prototype.decode = function decode_setup(reader, length) { // eslint-disable-line no-unused-vars
|
|
541
|
+
return this.setup().decode.apply(this, arguments); // overrides this method
|
|
533
542
|
};
|
|
534
543
|
|
|
535
544
|
/**
|
|
@@ -550,8 +559,8 @@ Type.prototype.decodeDelimited = function decodeDelimited(reader) {
|
|
|
550
559
|
* @param {Object.<string,*>} message Plain object to verify
|
|
551
560
|
* @returns {null|string} `null` if valid, otherwise the reason why it is not
|
|
552
561
|
*/
|
|
553
|
-
Type.prototype.verify = function verify_setup(message) {
|
|
554
|
-
return this.setup().verify(
|
|
562
|
+
Type.prototype.verify = function verify_setup(message) { // eslint-disable-line no-unused-vars
|
|
563
|
+
return this.setup().verify.apply(this, arguments); // overrides this method
|
|
555
564
|
};
|
|
556
565
|
|
|
557
566
|
/**
|
|
@@ -559,8 +568,8 @@ Type.prototype.verify = function verify_setup(message) {
|
|
|
559
568
|
* @param {Object.<string,*>} object Plain object to convert
|
|
560
569
|
* @returns {Message<{}>} Message instance
|
|
561
570
|
*/
|
|
562
|
-
Type.prototype.fromObject = function fromObject(object) {
|
|
563
|
-
return this.setup().fromObject(
|
|
571
|
+
Type.prototype.fromObject = function fromObject(object) { // eslint-disable-line no-unused-vars
|
|
572
|
+
return this.setup().fromObject.apply(this, arguments);
|
|
564
573
|
};
|
|
565
574
|
|
|
566
575
|
/**
|
|
@@ -592,6 +601,18 @@ Type.prototype.toObject = function toObject(message, options) {
|
|
|
592
601
|
return this.setup().toObject(message, options);
|
|
593
602
|
};
|
|
594
603
|
|
|
604
|
+
/**
|
|
605
|
+
* Gets the type url for this type.
|
|
606
|
+
* @param {string} [prefix] Custom type url prefix, defaults to `"type.googleapis.com"`
|
|
607
|
+
* @returns {string} The type url
|
|
608
|
+
*/
|
|
609
|
+
Type.prototype.getTypeUrl = function getTypeUrl(prefix) {
|
|
610
|
+
if (prefix === undefined)
|
|
611
|
+
prefix = "type.googleapis.com";
|
|
612
|
+
var fullName = this.fullName;
|
|
613
|
+
return prefix + "/" + (fullName.charAt(0) === "." ? fullName.substring(1) : fullName);
|
|
614
|
+
};
|
|
615
|
+
|
|
595
616
|
/**
|
|
596
617
|
* Decorator function as returned by {@link Type.d} (TypeScript).
|
|
597
618
|
* @typedef TypeDecorator
|
|
@@ -599,6 +620,7 @@ Type.prototype.toObject = function toObject(message, options) {
|
|
|
599
620
|
* @param {Constructor<T>} target Target constructor
|
|
600
621
|
* @returns {undefined}
|
|
601
622
|
* @template T extends Message<T>
|
|
623
|
+
* @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
|
|
602
624
|
*/
|
|
603
625
|
|
|
604
626
|
/**
|
|
@@ -606,6 +628,7 @@ Type.prototype.toObject = function toObject(message, options) {
|
|
|
606
628
|
* @param {string} [typeName] Type name, defaults to the constructor's name
|
|
607
629
|
* @returns {TypeDecorator<T>} Decorator function
|
|
608
630
|
* @template T extends Message<T>
|
|
631
|
+
* @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
|
|
609
632
|
*/
|
|
610
633
|
Type.d = function decorateType(typeName) {
|
|
611
634
|
return function typeDecorator(target) {
|
package/src/types.js
CHANGED
|
@@ -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
|
+
}
|