protobufjs 8.6.5 → 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.
@@ -24,6 +24,8 @@ function BufferWriter() {
24
24
  Writer.call(this);
25
25
  }
26
26
 
27
+ var writeStringBuffer;
28
+
27
29
  BufferWriter._configure = function () {
28
30
  /**
29
31
  * Allocates a buffer of the specified size.
@@ -31,19 +33,14 @@ BufferWriter._configure = function () {
31
33
  * @param {number} size Buffer size
32
34
  * @returns {Buffer} Buffer
33
35
  */
34
- BufferWriter.alloc = util._Buffer_allocUnsafe;
36
+ BufferWriter.alloc = util.Buffer && util.Buffer.allocUnsafe;
35
37
 
36
- BufferWriter.writeBytesBuffer = util.Buffer && util.Buffer.prototype instanceof Uint8Array && util.Buffer.prototype.set.name === "set"
37
- ? function writeBytesBuffer_set(val, buf, pos) {
38
- buf.set(val, pos); // faster than copy (requires node >= 4 where Buffers extend Uint8Array and set is properly inherited)
39
- // also works for plain array values
38
+ writeStringBuffer = util.Buffer && util.Buffer.prototype.utf8Write
39
+ ? function writeStringBuffer_utf8Write(val, buf, pos) {
40
+ return buf.utf8Write(val, pos);
40
41
  }
41
- /* istanbul ignore next */
42
- : function writeBytesBuffer_copy(val, buf, pos) {
43
- if (val.copy) // Buffer values
44
- val.copy(buf, pos, 0, val.length);
45
- else for (var i = 0; i < val.length;) // plain array values
46
- buf[pos++] = val[i++];
42
+ : function writeStringBuffer_write(val, buf, pos) {
43
+ return buf.write(val, pos);
47
44
  };
48
45
  };
49
46
 
@@ -53,48 +50,42 @@ BufferWriter._configure = function () {
53
50
  */
54
51
  BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
55
52
  if (util.isString(value))
56
- value = util._Buffer_from(value, "base64");
53
+ value = util.Buffer.from(value, "base64");
57
54
  var len = value.length >>> 0;
58
55
  this.uint32(len);
59
- if (len)
60
- this._push(BufferWriter.writeBytesBuffer, len, value);
56
+ if (len) {
57
+ this._reserve(len);
58
+ this.buf.set(value, this.pos);
59
+ this.pos += len;
60
+ }
61
61
  return this;
62
62
  };
63
63
 
64
- /**
65
- * Writes raw bytes without a tag or length prefix.
66
- * @name BufferWriter#raw
67
- * @function
68
- * @param {Uint8Array} value Raw bytes
69
- * @returns {BufferWriter} `this`
70
- */
71
- BufferWriter.prototype.raw = function write_raw_buffer(value) {
72
- var len = value.length >>> 0;
73
- return len ? this._push(BufferWriter.writeBytesBuffer, len, value) : this;
74
- };
75
-
76
- function writeStringBufferAscii(val, buf, pos) {
77
- for (var i = 0; i < val.length;)
78
- buf[pos++] = val.charCodeAt(i++);
79
- }
80
-
81
- function writeStringBuffer(val, buf, pos) {
82
- if (val.length < 40) // plain js is faster for short strings (probably due to redundant assertions)
83
- util.utf8.write(val, buf, pos);
84
- else if (buf.utf8Write)
85
- buf.utf8Write(val, pos);
86
- else
87
- buf.write(val, pos);
88
- }
89
-
90
64
  /**
91
65
  * @override
92
66
  */
93
67
  BufferWriter.prototype.string = function write_string_buffer(value) {
68
+ var n = value.length;
69
+ if (!n) {
70
+ this._reserve(1);
71
+ this.buf[this.pos++] = 0;
72
+ return this;
73
+ }
74
+ if (n < 0x80) {
75
+ this._reserve(n * 3 + 5); // worst case
76
+ var pos = this.pos,
77
+ buf = this.buf;
78
+ return this._delim(pos,
79
+ n < 40
80
+ ? util.utf8.write(value, buf, pos + 1)
81
+ : writeStringBuffer(value, buf, pos + 1)
82
+ );
83
+ }
94
84
  var len = util.Buffer.byteLength(value);
95
85
  this.uint32(len);
96
- if (len)
97
- this._push(len === value.length && len < 40 ? writeStringBufferAscii : writeStringBuffer, len, value);
86
+ this._reserve(len);
87
+ writeStringBuffer(value, this.buf, this.pos);
88
+ this.pos += len;
98
89
  return this;
99
90
  };
100
91