protobufjs 8.6.6 → 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/README.md +52 -53
- package/dist/light/protobuf.js +731 -332
- 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 +717 -320
- 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 +754 -345
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/index.d.ts +211 -23
- package/package.json +2 -2
- package/src/decoder.js +4 -7
- package/src/encoder.js +1 -4
- package/src/index-minimal.js +0 -1
- package/src/parse.js +23 -13
- package/src/reader.js +251 -21
- package/src/reader_buffer.js +1 -3
- package/src/type.js +9 -1
- package/src/util/minimal.js +12 -40
- package/src/util/utf8.js +19 -17
- package/src/writer.js +399 -194
- package/src/writer_buffer.js +33 -42
package/src/reader.js
CHANGED
|
@@ -15,7 +15,7 @@ function indexOutOfRange(reader, writeLength) {
|
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Constructs a new reader instance using the specified buffer.
|
|
18
|
-
* @classdesc Wire format reader using `Uint8Array
|
|
18
|
+
* @classdesc Wire format reader using `Uint8Array`.
|
|
19
19
|
* @constructor
|
|
20
20
|
* @param {Uint8Array} buffer Buffer to read from
|
|
21
21
|
*/
|
|
@@ -39,6 +39,12 @@ function Reader(buffer) {
|
|
|
39
39
|
*/
|
|
40
40
|
this.len = buffer.length;
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Cached DataView for packed reads.
|
|
44
|
+
* @type {DataView|null}
|
|
45
|
+
*/
|
|
46
|
+
this.view = null;
|
|
47
|
+
|
|
42
48
|
/**
|
|
43
49
|
* Whether to discard unknown fields while decoding.
|
|
44
50
|
* @type {boolean}
|
|
@@ -46,18 +52,14 @@ function Reader(buffer) {
|
|
|
46
52
|
this.discardUnknown = Reader.discardUnknown;
|
|
47
53
|
}
|
|
48
54
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (Array.isArray(buffer))
|
|
58
|
-
return new Reader(buffer);
|
|
59
|
-
throw Error("illegal buffer");
|
|
60
|
-
};
|
|
55
|
+
function create_array(buffer) {
|
|
56
|
+
// TODO: Remove plain array reader support in the next major release.
|
|
57
|
+
if (Array.isArray(buffer))
|
|
58
|
+
buffer = new Uint8Array(buffer);
|
|
59
|
+
if (buffer instanceof Uint8Array)
|
|
60
|
+
return new Reader(buffer);
|
|
61
|
+
throw Error("illegal buffer");
|
|
62
|
+
}
|
|
61
63
|
|
|
62
64
|
var create = function create() {
|
|
63
65
|
return util.Buffer
|
|
@@ -82,8 +84,6 @@ var create = function create() {
|
|
|
82
84
|
*/
|
|
83
85
|
Reader.create = create();
|
|
84
86
|
|
|
85
|
-
Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore next */ util.Array.prototype.slice;
|
|
86
|
-
|
|
87
87
|
/**
|
|
88
88
|
* Returns raw bytes from the backing buffer without advancing the reader.
|
|
89
89
|
* @param {number} start Start offset
|
|
@@ -91,12 +91,7 @@ Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore ne
|
|
|
91
91
|
* @returns {Uint8Array} Raw bytes
|
|
92
92
|
*/
|
|
93
93
|
Reader.prototype.raw = function read_raw(start, end) {
|
|
94
|
-
|
|
95
|
-
return this.buf.slice(start, end);
|
|
96
|
-
|
|
97
|
-
if (start === end) // fix for IE 10/Win8 and others' subarray returning array of size 1
|
|
98
|
-
return new this.buf.constructor(0);
|
|
99
|
-
return this._slice.call(this.buf, start, end);
|
|
94
|
+
return this.buf.subarray(start, end);
|
|
100
95
|
};
|
|
101
96
|
|
|
102
97
|
/**
|
|
@@ -393,6 +388,241 @@ Reader.prototype.double = function read_double() {
|
|
|
393
388
|
return value;
|
|
394
389
|
};
|
|
395
390
|
|
|
391
|
+
/**
|
|
392
|
+
* Reads a packed repeated field of unsigned 32 bit varints.
|
|
393
|
+
* @param {number[]} [array] Array to read into; a new one is created if omitted
|
|
394
|
+
* @returns {number[]} Array read into
|
|
395
|
+
*/
|
|
396
|
+
Reader.prototype.uint32s = function read_uint32s(array) {
|
|
397
|
+
if (array === undefined) array = [];
|
|
398
|
+
var end = this.uint32() + this.pos;
|
|
399
|
+
while (this.pos < end)
|
|
400
|
+
array.push(this.uint32());
|
|
401
|
+
return array;
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Reads a packed repeated field of signed 32 bit varints.
|
|
406
|
+
* @param {number[]} [array] Array to read into; a new one is created if omitted
|
|
407
|
+
* @returns {number[]} Array read into
|
|
408
|
+
*/
|
|
409
|
+
Reader.prototype.int32s = function read_int32s(array) {
|
|
410
|
+
if (array === undefined) array = [];
|
|
411
|
+
var end = this.uint32() + this.pos;
|
|
412
|
+
while (this.pos < end)
|
|
413
|
+
array.push(this.int32());
|
|
414
|
+
return array;
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Reads a packed repeated field of zig-zag encoded signed 32 bit varints.
|
|
419
|
+
* @param {number[]} [array] Array to read into; a new one is created if omitted
|
|
420
|
+
* @returns {number[]} Array read into
|
|
421
|
+
*/
|
|
422
|
+
Reader.prototype.sint32s = function read_sint32s(array) {
|
|
423
|
+
if (array === undefined) array = [];
|
|
424
|
+
var end = this.uint32() + this.pos;
|
|
425
|
+
while (this.pos < end)
|
|
426
|
+
array.push(this.sint32());
|
|
427
|
+
return array;
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Reads a packed repeated field of booleans.
|
|
432
|
+
* @param {boolean[]} [array] Array to read into; a new one is created if omitted
|
|
433
|
+
* @returns {boolean[]} Array read into
|
|
434
|
+
*/
|
|
435
|
+
Reader.prototype.bools = function read_bools(array) {
|
|
436
|
+
if (array === undefined) array = [];
|
|
437
|
+
var end = this.uint32() + this.pos;
|
|
438
|
+
while (this.pos < end)
|
|
439
|
+
array.push(this.bool());
|
|
440
|
+
return array;
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
// The view allocation only pays off when amortized over enough reads
|
|
444
|
+
var VIEW_THRESHOLD_FLOAT = 8,
|
|
445
|
+
VIEW_THRESHOLD_INT = 128;
|
|
446
|
+
|
|
447
|
+
function getLazyView(reader, count, threshold) {
|
|
448
|
+
var view = reader.view;
|
|
449
|
+
if (view || count < threshold)
|
|
450
|
+
return view;
|
|
451
|
+
var buf = reader.buf;
|
|
452
|
+
return reader.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Reads a packed repeated field of unsigned 32 bit fixed values.
|
|
457
|
+
* @param {number[]} [array] Array to read into; a new one is created if omitted
|
|
458
|
+
* @returns {number[]} Array read into
|
|
459
|
+
*/
|
|
460
|
+
Reader.prototype.fixed32s = function read_fixed32s(array) {
|
|
461
|
+
if (array === undefined) array = [];
|
|
462
|
+
var len = this.uint32(), end = this.pos + len;
|
|
463
|
+
/* istanbul ignore if */
|
|
464
|
+
if (end > this.len) throw indexOutOfRange(this, len);
|
|
465
|
+
var count = len >>> 2, i = array.length, pos = this.pos;
|
|
466
|
+
array.length = i + count;
|
|
467
|
+
var dv = getLazyView(this, count, VIEW_THRESHOLD_INT);
|
|
468
|
+
if (dv)
|
|
469
|
+
for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getUint32(pos, true);
|
|
470
|
+
else {
|
|
471
|
+
var buf = this.buf;
|
|
472
|
+
for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4);
|
|
473
|
+
}
|
|
474
|
+
this.pos = pos;
|
|
475
|
+
if (pos !== end) throw indexOutOfRange(this, 4);
|
|
476
|
+
return array;
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Reads a packed repeated field of signed 32 bit fixed values.
|
|
481
|
+
* @param {number[]} [array] Array to read into; a new one is created if omitted
|
|
482
|
+
* @returns {number[]} Array read into
|
|
483
|
+
*/
|
|
484
|
+
Reader.prototype.sfixed32s = function read_sfixed32s(array) {
|
|
485
|
+
if (array === undefined) array = [];
|
|
486
|
+
var len = this.uint32(), end = this.pos + len;
|
|
487
|
+
/* istanbul ignore if */
|
|
488
|
+
if (end > this.len) throw indexOutOfRange(this, len);
|
|
489
|
+
var count = len >>> 2, i = array.length, pos = this.pos;
|
|
490
|
+
array.length = i + count;
|
|
491
|
+
var dv = getLazyView(this, count, VIEW_THRESHOLD_INT);
|
|
492
|
+
if (dv)
|
|
493
|
+
for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getInt32(pos, true);
|
|
494
|
+
else {
|
|
495
|
+
var buf = this.buf;
|
|
496
|
+
for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4) | 0;
|
|
497
|
+
}
|
|
498
|
+
this.pos = pos;
|
|
499
|
+
if (pos !== end) throw indexOutOfRange(this, 4);
|
|
500
|
+
return array;
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Reads a packed repeated field of floats (32 bit).
|
|
505
|
+
* @param {number[]} [array] Array to read into; a new one is created if omitted
|
|
506
|
+
* @returns {number[]} Array read into
|
|
507
|
+
*/
|
|
508
|
+
Reader.prototype.floats = function read_floats(array) {
|
|
509
|
+
if (array === undefined) array = [];
|
|
510
|
+
var len = this.uint32(), end = this.pos + len;
|
|
511
|
+
/* istanbul ignore if */
|
|
512
|
+
if (end > this.len) throw indexOutOfRange(this, len);
|
|
513
|
+
var count = len >>> 2, i = array.length, pos = this.pos;
|
|
514
|
+
array.length = i + count;
|
|
515
|
+
var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT);
|
|
516
|
+
if (dv)
|
|
517
|
+
for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getFloat32(pos, true);
|
|
518
|
+
else {
|
|
519
|
+
var buf = this.buf;
|
|
520
|
+
for (var j = 0; j < count; ++j, pos += 4) array[i++] = util.float.readFloatLE(buf, pos);
|
|
521
|
+
}
|
|
522
|
+
this.pos = pos;
|
|
523
|
+
if (pos !== end) throw indexOutOfRange(this, 4);
|
|
524
|
+
return array;
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Reads a packed repeated field of doubles (64 bit float).
|
|
529
|
+
* @param {number[]} [array] Array to read into; a new one is created if omitted
|
|
530
|
+
* @returns {number[]} Array read into
|
|
531
|
+
*/
|
|
532
|
+
Reader.prototype.doubles = function read_doubles(array) {
|
|
533
|
+
if (array === undefined) array = [];
|
|
534
|
+
var len = this.uint32(), end = this.pos + len;
|
|
535
|
+
/* istanbul ignore if */
|
|
536
|
+
if (end > this.len) throw indexOutOfRange(this, len);
|
|
537
|
+
var count = len >>> 3, i = array.length, pos = this.pos;
|
|
538
|
+
array.length = i + count;
|
|
539
|
+
var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT);
|
|
540
|
+
if (dv)
|
|
541
|
+
for (var k = 0; k < count; ++k, pos += 8) array[i++] = dv.getFloat64(pos, true);
|
|
542
|
+
else {
|
|
543
|
+
var buf = this.buf;
|
|
544
|
+
for (var j = 0; j < count; ++j, pos += 8) array[i++] = util.float.readDoubleLE(buf, pos);
|
|
545
|
+
}
|
|
546
|
+
this.pos = pos;
|
|
547
|
+
if (pos !== end) throw indexOutOfRange(this, 8);
|
|
548
|
+
return array;
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Reads a packed repeated field of unsigned 64 bit varints.
|
|
553
|
+
* @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
|
|
554
|
+
* @returns {Array.<Long|number>} Array read into
|
|
555
|
+
*/
|
|
556
|
+
Reader.prototype.uint64s = function read_uint64s(array) {
|
|
557
|
+
if (array === undefined) array = [];
|
|
558
|
+
var end = this.uint32() + this.pos;
|
|
559
|
+
while (this.pos < end)
|
|
560
|
+
array.push(this.uint64());
|
|
561
|
+
return array;
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Reads a packed repeated field of signed 64 bit varints.
|
|
566
|
+
* @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
|
|
567
|
+
* @returns {Array.<Long|number>} Array read into
|
|
568
|
+
*/
|
|
569
|
+
Reader.prototype.int64s = function read_int64s(array) {
|
|
570
|
+
if (array === undefined) array = [];
|
|
571
|
+
var end = this.uint32() + this.pos;
|
|
572
|
+
while (this.pos < end)
|
|
573
|
+
array.push(this.int64());
|
|
574
|
+
return array;
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Reads a packed repeated field of zig-zag encoded signed 64 bit varints.
|
|
579
|
+
* @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
|
|
580
|
+
* @returns {Array.<Long|number>} Array read into
|
|
581
|
+
*/
|
|
582
|
+
Reader.prototype.sint64s = function read_sint64s(array) {
|
|
583
|
+
if (array === undefined) array = [];
|
|
584
|
+
var end = this.uint32() + this.pos;
|
|
585
|
+
while (this.pos < end)
|
|
586
|
+
array.push(this.sint64());
|
|
587
|
+
return array;
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Reads a packed repeated field of unsigned 64 bit fixed values.
|
|
592
|
+
* @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
|
|
593
|
+
* @returns {Array.<Long|number>} Array read into
|
|
594
|
+
*/
|
|
595
|
+
Reader.prototype.fixed64s = function read_fixed64s(array) {
|
|
596
|
+
if (array === undefined) array = [];
|
|
597
|
+
var len = this.uint32(), end = this.pos + len, i = array.length;
|
|
598
|
+
/* istanbul ignore if */
|
|
599
|
+
if (end > this.len) throw indexOutOfRange(this, len);
|
|
600
|
+
var count = len >>> 3;
|
|
601
|
+
array.length = i + count; // 8 bytes per value, count is known
|
|
602
|
+
for (var j = 0; j < count; ++j)
|
|
603
|
+
array[i++] = this.fixed64();
|
|
604
|
+
if (this.pos !== end) throw indexOutOfRange(this, 8);
|
|
605
|
+
return array;
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Reads a packed repeated field of signed 64 bit fixed values.
|
|
610
|
+
* @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
|
|
611
|
+
* @returns {Array.<Long|number>} Array read into
|
|
612
|
+
*/
|
|
613
|
+
Reader.prototype.sfixed64s = function read_sfixed64s(array) {
|
|
614
|
+
if (array === undefined) array = [];
|
|
615
|
+
var len = this.uint32(), end = this.pos + len, i = array.length;
|
|
616
|
+
/* istanbul ignore if */
|
|
617
|
+
if (end > this.len) throw indexOutOfRange(this, len);
|
|
618
|
+
var count = len >>> 3;
|
|
619
|
+
array.length = i + count; // 8 bytes per value, count is known
|
|
620
|
+
for (var j = 0; j < count; ++j)
|
|
621
|
+
array[i++] = this.sfixed64();
|
|
622
|
+
if (this.pos !== end) throw indexOutOfRange(this, 8);
|
|
623
|
+
return array;
|
|
624
|
+
};
|
|
625
|
+
|
|
396
626
|
/**
|
|
397
627
|
* Reads a sequence of bytes preceeded by its length as a varint.
|
|
398
628
|
* @returns {Uint8Array} Value read
|
package/src/reader_buffer.js
CHANGED
|
@@ -28,7 +28,7 @@ function BufferReader(buffer) {
|
|
|
28
28
|
* Read buffer.
|
|
29
29
|
* @name BufferReader#buf
|
|
30
30
|
* @type {Buffer}
|
|
31
|
-
|
|
31
|
+
*/
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
BufferReader._configure = function () {
|
|
@@ -46,8 +46,6 @@ BufferReader._configure = function () {
|
|
|
46
46
|
* @returns {Buffer} Raw bytes
|
|
47
47
|
*/
|
|
48
48
|
BufferReader.prototype.raw = function read_raw_buffer(start, end) {
|
|
49
|
-
if (start === end)
|
|
50
|
-
return util.Buffer.alloc(0);
|
|
51
49
|
return this._slice.call(this.buf, start, end);
|
|
52
50
|
};
|
|
53
51
|
|
package/src/type.js
CHANGED
|
@@ -494,6 +494,14 @@ Type.prototype.setup = function setup() {
|
|
|
494
494
|
// Sets up everything at once so that the prototype chain does not have to be re-evaluated
|
|
495
495
|
// multiple times (V8, soft-deopt prototype-check).
|
|
496
496
|
|
|
497
|
+
// Resolve feature defaults incl. field presence before generating codecs
|
|
498
|
+
var root = this.root;
|
|
499
|
+
if (root && root._needsRecursiveFeatureResolution) {
|
|
500
|
+
var edition = root._edition || this._edition;
|
|
501
|
+
if (edition)
|
|
502
|
+
root._resolveFeaturesRecursive(edition);
|
|
503
|
+
}
|
|
504
|
+
|
|
497
505
|
var fullName = this.fullName,
|
|
498
506
|
types = [];
|
|
499
507
|
for (var i = 0; i < /* initializes */ this.fieldsArray.length; ++i)
|
|
@@ -557,7 +565,7 @@ Type.prototype.encode = function encode_setup(message, writer) { // eslint-disab
|
|
|
557
565
|
* @returns {Writer} writer
|
|
558
566
|
*/
|
|
559
567
|
Type.prototype.encodeDelimited = function encodeDelimited(message, writer) {
|
|
560
|
-
return this.encode(message, writer
|
|
568
|
+
return this.encode(message, (writer || Writer.create()).fork()).ldelim();
|
|
561
569
|
};
|
|
562
570
|
|
|
563
571
|
/**
|
package/src/util/minimal.js
CHANGED
|
@@ -137,36 +137,29 @@ util.isSet = function isSet(obj, prop) {
|
|
|
137
137
|
util.Buffer = (function() {
|
|
138
138
|
try {
|
|
139
139
|
var Buffer = util.global.Buffer;
|
|
140
|
-
// refuse to use non-node buffers if not explicitly assigned (perf reasons)
|
|
141
|
-
return Buffer.prototype.utf8Write ? Buffer : /* istanbul ignore next */ null;
|
|
140
|
+
// refuse to use non-node buffers if not explicitly assigned (perf reasons)
|
|
141
|
+
return Buffer.prototype.utf8Write || util.isNode ? Buffer : /* istanbul ignore next */ null;
|
|
142
142
|
} catch (e) {
|
|
143
143
|
/* istanbul ignore next */
|
|
144
144
|
return null;
|
|
145
145
|
}
|
|
146
146
|
})();
|
|
147
147
|
|
|
148
|
-
// Internal alias of or polyfull for Buffer.from.
|
|
149
|
-
util._Buffer_from = null;
|
|
150
|
-
|
|
151
|
-
// Internal alias of or polyfill for Buffer.allocUnsafe.
|
|
152
|
-
util._Buffer_allocUnsafe = null;
|
|
153
|
-
|
|
154
148
|
/**
|
|
155
149
|
* Creates a new buffer of whatever type supported by the environment.
|
|
156
150
|
* @param {number|number[]} [sizeOrArray=0] Buffer size or number array
|
|
157
151
|
* @returns {Uint8Array|Buffer} Buffer
|
|
158
152
|
*/
|
|
159
153
|
util.newBuffer = function newBuffer(sizeOrArray) {
|
|
154
|
+
var Buffer = util.Buffer;
|
|
160
155
|
/* istanbul ignore next */
|
|
161
156
|
return typeof sizeOrArray === "number"
|
|
162
|
-
?
|
|
163
|
-
?
|
|
164
|
-
: new
|
|
165
|
-
:
|
|
166
|
-
?
|
|
167
|
-
:
|
|
168
|
-
? sizeOrArray
|
|
169
|
-
: new Uint8Array(sizeOrArray);
|
|
157
|
+
? Buffer
|
|
158
|
+
? Buffer.allocUnsafe(sizeOrArray)
|
|
159
|
+
: new Uint8Array(sizeOrArray)
|
|
160
|
+
: Buffer
|
|
161
|
+
? Buffer.from(sizeOrArray)
|
|
162
|
+
: new Uint8Array(sizeOrArray);
|
|
170
163
|
};
|
|
171
164
|
|
|
172
165
|
/**
|
|
@@ -192,10 +185,11 @@ util.rawField = function rawField(id, wireType, data) {
|
|
|
192
185
|
};
|
|
193
186
|
|
|
194
187
|
/**
|
|
195
|
-
* Array implementation used in the browser.
|
|
188
|
+
* Array implementation used in the browser.
|
|
196
189
|
* @type {Constructor<Uint8Array>}
|
|
190
|
+
* @deprecated Use `Uint8Array` instead.
|
|
197
191
|
*/
|
|
198
|
-
util.Array =
|
|
192
|
+
util.Array = Uint8Array;
|
|
199
193
|
|
|
200
194
|
/**
|
|
201
195
|
* Any compatible Long instance.
|
|
@@ -515,25 +509,3 @@ util.toJSONOptions = {
|
|
|
515
509
|
bytes: String,
|
|
516
510
|
json: true
|
|
517
511
|
};
|
|
518
|
-
|
|
519
|
-
// Sets up buffer utility according to the environment (called in index-minimal)
|
|
520
|
-
util._configure = function() {
|
|
521
|
-
var Buffer = util.Buffer;
|
|
522
|
-
/* istanbul ignore if */
|
|
523
|
-
if (!Buffer) {
|
|
524
|
-
util._Buffer_from = util._Buffer_allocUnsafe = null;
|
|
525
|
-
return;
|
|
526
|
-
}
|
|
527
|
-
// because node 4.x buffers are incompatible & immutable
|
|
528
|
-
// see: https://github.com/dcodeIO/protobuf.js/pull/665
|
|
529
|
-
util._Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from ||
|
|
530
|
-
/* istanbul ignore next */
|
|
531
|
-
function Buffer_from(value, encoding) {
|
|
532
|
-
return new Buffer(value, encoding);
|
|
533
|
-
};
|
|
534
|
-
util._Buffer_allocUnsafe = Buffer.allocUnsafe ||
|
|
535
|
-
/* istanbul ignore next */
|
|
536
|
-
function Buffer_allocUnsafe(size) {
|
|
537
|
-
return new Buffer(size);
|
|
538
|
-
};
|
|
539
|
-
};
|
package/src/util/utf8.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* A minimal UTF8 implementation
|
|
4
|
+
* A minimal UTF8 implementation.
|
|
5
5
|
* @memberof util
|
|
6
6
|
* @namespace
|
|
7
7
|
*/
|
|
8
8
|
var utf8 = exports,
|
|
9
9
|
replacementChar = "\ufffd",
|
|
10
|
+
looseDecoder = new TextDecoder("utf-8", { ignoreBOM: true }),
|
|
10
11
|
strictDecoder;
|
|
12
|
+
var TEXT_DECODER_MIN_LENGTH = 64;
|
|
11
13
|
|
|
12
14
|
try {
|
|
13
15
|
strictDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
14
16
|
} catch (err) {
|
|
15
17
|
// "fatal" option is not supported on Node.js compiled without ICU
|
|
16
|
-
strictDecoder =
|
|
18
|
+
strictDecoder = looseDecoder;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
/**
|
|
@@ -64,6 +66,13 @@ function utf8_read_js(buffer, start, end, str) {
|
|
|
64
66
|
return str;
|
|
65
67
|
}
|
|
66
68
|
|
|
69
|
+
function utf8_read_decoder(decoder, buffer, start, end) {
|
|
70
|
+
var source = start === 0 && end === buffer.length
|
|
71
|
+
? buffer
|
|
72
|
+
: buffer.subarray(start, end);
|
|
73
|
+
return decoder.decode(source);
|
|
74
|
+
}
|
|
75
|
+
|
|
67
76
|
/**
|
|
68
77
|
* Reads UTF8 bytes as a string.
|
|
69
78
|
* @param {Uint8Array} buffer Source buffer
|
|
@@ -71,9 +80,11 @@ function utf8_read_js(buffer, start, end, str) {
|
|
|
71
80
|
* @param {number} end Source end
|
|
72
81
|
* @returns {string} String read
|
|
73
82
|
*/
|
|
74
|
-
utf8.read = function
|
|
83
|
+
utf8.read = function utf8_read_loose(buffer, start, end) {
|
|
75
84
|
if (end - start < 1)
|
|
76
85
|
return "";
|
|
86
|
+
if (end - start >= TEXT_DECODER_MIN_LENGTH)
|
|
87
|
+
return utf8_read_decoder(looseDecoder, buffer, start, end);
|
|
77
88
|
|
|
78
89
|
var str = "",
|
|
79
90
|
i = start,
|
|
@@ -103,17 +114,6 @@ utf8.read = function utf8_read_ascii(buffer, start, end) {
|
|
|
103
114
|
return str;
|
|
104
115
|
};
|
|
105
116
|
|
|
106
|
-
function utf8_read_strict(buffer, start, end) {
|
|
107
|
-
var source = start === 0 && end === buffer.length
|
|
108
|
-
? buffer
|
|
109
|
-
: buffer.subarray
|
|
110
|
-
? buffer.subarray(start, end)
|
|
111
|
-
: buffer.slice(start, end);
|
|
112
|
-
if (Array.isArray(source))
|
|
113
|
-
source = Uint8Array.from(source);
|
|
114
|
-
return strictDecoder.decode(source);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
117
|
/**
|
|
118
118
|
* Reads UTF8 bytes as a string, rejecting invalid UTF8.
|
|
119
119
|
* @param {Uint8Array} buffer Source buffer
|
|
@@ -121,9 +121,11 @@ function utf8_read_strict(buffer, start, end) {
|
|
|
121
121
|
* @param {number} end Source end
|
|
122
122
|
* @returns {string} String read
|
|
123
123
|
*/
|
|
124
|
-
utf8.readStrict = function
|
|
124
|
+
utf8.readStrict = function utf8_read_strict(buffer, start, end) {
|
|
125
125
|
if (end - start < 1)
|
|
126
126
|
return "";
|
|
127
|
+
if (end - start >= TEXT_DECODER_MIN_LENGTH)
|
|
128
|
+
return utf8_read_decoder(strictDecoder, buffer, start, end);
|
|
127
129
|
|
|
128
130
|
var str = "",
|
|
129
131
|
i = start,
|
|
@@ -139,14 +141,14 @@ utf8.readStrict = function utf8_read_strict_ascii(buffer, start, end) {
|
|
|
139
141
|
c7 = buffer[i + 6];
|
|
140
142
|
c8 = buffer[i + 7];
|
|
141
143
|
if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) & 0x80)
|
|
142
|
-
return str +
|
|
144
|
+
return str + utf8_read_decoder(strictDecoder, buffer, i, end);
|
|
143
145
|
str += String.fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8);
|
|
144
146
|
}
|
|
145
147
|
|
|
146
148
|
for (; i < end; ++i) {
|
|
147
149
|
c1 = buffer[i];
|
|
148
150
|
if (c1 & 0x80)
|
|
149
|
-
return str +
|
|
151
|
+
return str + utf8_read_decoder(strictDecoder, buffer, i, end);
|
|
150
152
|
str += String.fromCharCode(c1);
|
|
151
153
|
}
|
|
152
154
|
|