protobufjs 8.6.6 → 8.7.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.
@@ -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
- ? util.Buffer
163
- ? util._Buffer_allocUnsafe(sizeOrArray)
164
- : new util.Array(sizeOrArray)
165
- : util.Buffer
166
- ? util._Buffer_from(sizeOrArray)
167
- : typeof Uint8Array === "undefined"
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. `Uint8Array` if supported, otherwise `Array`.
188
+ * Array implementation used in the browser.
196
189
  * @type {Constructor<Uint8Array>}
190
+ * @deprecated Use `Uint8Array` instead.
197
191
  */
198
- util.Array = typeof Uint8Array !== "undefined" ? Uint8Array /* istanbul ignore next */ : 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 for number arrays.
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 = new TextDecoder("utf-8", { ignoreBOM: true });
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 utf8_read_ascii(buffer, start, end) {
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 utf8_read_strict_ascii(buffer, start, end) {
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 + utf8_read_strict(buffer, i, end);
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 + utf8_read_strict(buffer, i, end);
151
+ return str + utf8_read_decoder(strictDecoder, buffer, i, end);
150
152
  str += String.fromCharCode(c1);
151
153
  }
152
154