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.
- package/README.md +225 -570
- package/dist/light/protobuf.js +2041 -1482
- 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 +1167 -863
- 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 +2173 -1527
- 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 -190
- package/ext/descriptor/index.js +1 -1161
- package/ext/descriptor.d.ts +309 -0
- package/ext/descriptor.js +1241 -0
- package/ext/textformat.d.ts +24 -0
- package/ext/textformat.js +1227 -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 +585 -476
- package/package.json +22 -40
- package/src/converter.js +63 -27
- package/src/decoder.js +126 -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 +31 -12
- package/src/object.js +24 -19
- package/src/oneof.js +2 -0
- package/src/parse.js +128 -46
- package/src/reader.js +145 -30
- package/src/reader_buffer.js +24 -3
- package/src/root.js +10 -4
- package/src/service.js +15 -6
- package/src/tokenize.js +6 -1
- package/src/type.js +57 -27
- 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 +74 -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 +130 -0
- package/src/util.js +18 -13
- package/src/verifier.js +7 -4
- package/src/wrappers.js +4 -3
- package/src/writer.js +33 -5
- package/src/writer_buffer.js +18 -1
- package/tsconfig.json +2 -2
- package/ext/descriptor/test.js +0 -54
package/src/util/pool.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = pool;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* An allocator as used by {@link util.pool}.
|
|
6
|
+
* @typedef PoolAllocator
|
|
7
|
+
* @type {function}
|
|
8
|
+
* @param {number} size Buffer size
|
|
9
|
+
* @returns {Uint8Array} Buffer
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A slicer as used by {@link util.pool}.
|
|
14
|
+
* @typedef PoolSlicer
|
|
15
|
+
* @type {function}
|
|
16
|
+
* @param {number} start Start offset
|
|
17
|
+
* @param {number} end End offset
|
|
18
|
+
* @returns {Uint8Array} Buffer slice
|
|
19
|
+
* @this {Uint8Array}
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A general purpose buffer pool.
|
|
24
|
+
* @memberof util
|
|
25
|
+
* @function
|
|
26
|
+
* @param {PoolAllocator} alloc Allocator
|
|
27
|
+
* @param {PoolSlicer} slice Slicer
|
|
28
|
+
* @param {number} [size=8192] Slab size
|
|
29
|
+
* @returns {PoolAllocator} Pooled allocator
|
|
30
|
+
*/
|
|
31
|
+
function pool(alloc, slice, size) {
|
|
32
|
+
var SIZE = size || 8192;
|
|
33
|
+
var MAX = SIZE >>> 1;
|
|
34
|
+
var slab = null;
|
|
35
|
+
var offset = SIZE;
|
|
36
|
+
return function pool_alloc(size) {
|
|
37
|
+
if (size < 1 || size > MAX)
|
|
38
|
+
return alloc(size);
|
|
39
|
+
if (offset + size > SIZE) {
|
|
40
|
+
slab = alloc(SIZE);
|
|
41
|
+
offset = 0;
|
|
42
|
+
}
|
|
43
|
+
var buf = slice.call(slab, offset, offset += size);
|
|
44
|
+
if (offset & 7) // align to 32 bit
|
|
45
|
+
offset = (offset | 7) + 1;
|
|
46
|
+
return buf;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the UTF8 byte length of a string.
|
|
3
|
+
* @param {string} string String
|
|
4
|
+
* @returns {number} Byte length
|
|
5
|
+
*/
|
|
6
|
+
export function length(string: string): number;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reads UTF8 bytes as a string.
|
|
10
|
+
* @param {Uint8Array} buffer Source buffer
|
|
11
|
+
* @param {number} start Source start
|
|
12
|
+
* @param {number} end Source end
|
|
13
|
+
* @returns {string} String read
|
|
14
|
+
*/
|
|
15
|
+
export function read(buffer: Uint8Array, start: number, end: number): string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Writes a string as UTF8 bytes.
|
|
19
|
+
* @param {string} string Source string
|
|
20
|
+
* @param {Uint8Array} buffer Destination buffer
|
|
21
|
+
* @param {number} offset Destination offset
|
|
22
|
+
* @returns {number} Bytes written
|
|
23
|
+
*/
|
|
24
|
+
export function write(string: string, buffer: Uint8Array, offset: number): number;
|
package/src/util/utf8.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A minimal UTF8 implementation for number arrays.
|
|
5
|
+
* @memberof util
|
|
6
|
+
* @namespace
|
|
7
|
+
*/
|
|
8
|
+
var utf8 = exports,
|
|
9
|
+
replacementChar = "\ufffd";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Calculates the UTF8 byte length of a string.
|
|
13
|
+
* @param {string} string String
|
|
14
|
+
* @returns {number} Byte length
|
|
15
|
+
*/
|
|
16
|
+
utf8.length = function utf8_length(string) {
|
|
17
|
+
var len = 0,
|
|
18
|
+
c = 0;
|
|
19
|
+
for (var i = 0; i < string.length; ++i) {
|
|
20
|
+
c = string.charCodeAt(i);
|
|
21
|
+
if (c < 128)
|
|
22
|
+
len += 1;
|
|
23
|
+
else if (c < 2048)
|
|
24
|
+
len += 2;
|
|
25
|
+
else if ((c & 0xFC00) === 0xD800 && (string.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
|
|
26
|
+
++i;
|
|
27
|
+
len += 4;
|
|
28
|
+
} else
|
|
29
|
+
len += 3;
|
|
30
|
+
}
|
|
31
|
+
return len;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
function utf8_read_js(buffer, start, end, str) {
|
|
35
|
+
for (var i = start; i < end;) {
|
|
36
|
+
var t = buffer[i++];
|
|
37
|
+
if (t <= 0x7F) {
|
|
38
|
+
str += String.fromCharCode(t);
|
|
39
|
+
} else if (t >= 0xC0 && t < 0xE0) {
|
|
40
|
+
var c2 = (t & 0x1F) << 6 | buffer[i++] & 0x3F;
|
|
41
|
+
str += c2 >= 0x80 ? String.fromCharCode(c2) : replacementChar;
|
|
42
|
+
} else if (t >= 0xE0 && t < 0xF0) {
|
|
43
|
+
var c3 = (t & 0xF) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F;
|
|
44
|
+
str += c3 >= 0x800 ? String.fromCharCode(c3) : replacementChar;
|
|
45
|
+
} else if (t >= 0xF0) {
|
|
46
|
+
var t2 = (t & 7) << 18 | (buffer[i++] & 0x3F) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F;
|
|
47
|
+
if (t2 < 0x10000 || t2 > 0x10FFFF)
|
|
48
|
+
str += replacementChar;
|
|
49
|
+
else {
|
|
50
|
+
t2 -= 0x10000;
|
|
51
|
+
str += String.fromCharCode(0xD800 + (t2 >> 10));
|
|
52
|
+
str += String.fromCharCode(0xDC00 + (t2 & 0x3FF));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return str;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Reads UTF8 bytes as a string.
|
|
61
|
+
* @param {Uint8Array} buffer Source buffer
|
|
62
|
+
* @param {number} start Source start
|
|
63
|
+
* @param {number} end Source end
|
|
64
|
+
* @returns {string} String read
|
|
65
|
+
*/
|
|
66
|
+
utf8.read = function utf8_read_ascii(buffer, start, end) {
|
|
67
|
+
if (end - start < 1)
|
|
68
|
+
return "";
|
|
69
|
+
|
|
70
|
+
var str = "",
|
|
71
|
+
i = start,
|
|
72
|
+
c1, c2, c3, c4, c5, c6, c7, c8;
|
|
73
|
+
|
|
74
|
+
for (; i + 7 < end; i += 8) {
|
|
75
|
+
c1 = buffer[i];
|
|
76
|
+
c2 = buffer[i + 1];
|
|
77
|
+
c3 = buffer[i + 2];
|
|
78
|
+
c4 = buffer[i + 3];
|
|
79
|
+
c5 = buffer[i + 4];
|
|
80
|
+
c6 = buffer[i + 5];
|
|
81
|
+
c7 = buffer[i + 6];
|
|
82
|
+
c8 = buffer[i + 7];
|
|
83
|
+
if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) & 0x80)
|
|
84
|
+
return utf8_read_js(buffer, i, end, str);
|
|
85
|
+
str += String.fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
for (; i < end; ++i) {
|
|
89
|
+
c1 = buffer[i];
|
|
90
|
+
if (c1 & 0x80)
|
|
91
|
+
return utf8_read_js(buffer, i, end, str);
|
|
92
|
+
str += String.fromCharCode(c1);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return str;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Writes a string as UTF8 bytes.
|
|
100
|
+
* @param {string} string Source string
|
|
101
|
+
* @param {Uint8Array} buffer Destination buffer
|
|
102
|
+
* @param {number} offset Destination offset
|
|
103
|
+
* @returns {number} Bytes written
|
|
104
|
+
*/
|
|
105
|
+
utf8.write = function utf8_write(string, buffer, offset) {
|
|
106
|
+
var start = offset,
|
|
107
|
+
c1, // character 1
|
|
108
|
+
c2; // character 2
|
|
109
|
+
for (var i = 0; i < string.length; ++i) {
|
|
110
|
+
c1 = string.charCodeAt(i);
|
|
111
|
+
if (c1 < 128) {
|
|
112
|
+
buffer[offset++] = c1;
|
|
113
|
+
} else if (c1 < 2048) {
|
|
114
|
+
buffer[offset++] = c1 >> 6 | 192;
|
|
115
|
+
buffer[offset++] = c1 & 63 | 128;
|
|
116
|
+
} else if ((c1 & 0xFC00) === 0xD800 && ((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
|
|
117
|
+
c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
|
|
118
|
+
++i;
|
|
119
|
+
buffer[offset++] = c1 >> 18 | 240;
|
|
120
|
+
buffer[offset++] = c1 >> 12 & 63 | 128;
|
|
121
|
+
buffer[offset++] = c1 >> 6 & 63 | 128;
|
|
122
|
+
buffer[offset++] = c1 & 63 | 128;
|
|
123
|
+
} else {
|
|
124
|
+
buffer[offset++] = c1 >> 12 | 224;
|
|
125
|
+
buffer[offset++] = c1 >> 6 & 63 | 128;
|
|
126
|
+
buffer[offset++] = c1 & 63 | 128;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return offset - start;
|
|
130
|
+
};
|
package/src/util.js
CHANGED
|
@@ -11,15 +11,19 @@ var roots = require("./roots");
|
|
|
11
11
|
var Type, // cyclic
|
|
12
12
|
Enum;
|
|
13
13
|
|
|
14
|
-
util.codegen
|
|
15
|
-
util.fetch
|
|
16
|
-
util.path
|
|
14
|
+
util.codegen = require("./util/codegen");
|
|
15
|
+
util.fetch = require("./util/fetch");
|
|
16
|
+
util.path = require("./util/path");
|
|
17
|
+
util.patterns = require("./util/patterns");
|
|
18
|
+
|
|
19
|
+
var reservedRe = util.patterns.reservedRe,
|
|
20
|
+
unsafePropertyRe = util.patterns.unsafePropertyRe;
|
|
17
21
|
|
|
18
22
|
/**
|
|
19
23
|
* Node's fs module if available.
|
|
20
24
|
* @type {Object.<string,*>}
|
|
21
25
|
*/
|
|
22
|
-
util.fs =
|
|
26
|
+
util.fs = require("./util/fs");
|
|
23
27
|
|
|
24
28
|
/**
|
|
25
29
|
* Converts an object's values to an array.
|
|
@@ -55,16 +59,13 @@ util.toObject = function toObject(array) {
|
|
|
55
59
|
return object;
|
|
56
60
|
};
|
|
57
61
|
|
|
58
|
-
var safePropBackslashRe = /\\/g,
|
|
59
|
-
safePropQuoteRe = /"/g;
|
|
60
|
-
|
|
61
62
|
/**
|
|
62
63
|
* Tests whether the specified name is a reserved word in JS.
|
|
63
64
|
* @param {string} name Name to test
|
|
64
65
|
* @returns {boolean} `true` if reserved, otherwise `false`
|
|
65
66
|
*/
|
|
66
67
|
util.isReserved = function isReserved(name) {
|
|
67
|
-
return
|
|
68
|
+
return reservedRe.test(name);
|
|
68
69
|
};
|
|
69
70
|
|
|
70
71
|
/**
|
|
@@ -73,8 +74,8 @@ util.isReserved = function isReserved(name) {
|
|
|
73
74
|
* @returns {string} Safe accessor
|
|
74
75
|
*/
|
|
75
76
|
util.safeProp = function safeProp(prop) {
|
|
76
|
-
if (!/^[$\w_]+$/.test(prop) ||
|
|
77
|
-
return "[
|
|
77
|
+
if (!/^[$\w_]+$/.test(prop) || reservedRe.test(prop))
|
|
78
|
+
return "[" + JSON.stringify(prop) + "]";
|
|
78
79
|
return "." + prop;
|
|
79
80
|
};
|
|
80
81
|
|
|
@@ -117,6 +118,7 @@ util.compareFieldsById = function compareFieldsById(a, b) {
|
|
|
117
118
|
* @returns {Type} Reflected type
|
|
118
119
|
* @template T extends Message<T>
|
|
119
120
|
* @property {Root} root Decorators root
|
|
121
|
+
* @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
|
|
120
122
|
*/
|
|
121
123
|
util.decorateType = function decorateType(ctor, typeName) {
|
|
122
124
|
|
|
@@ -148,6 +150,7 @@ var decorateEnumIndex = 0;
|
|
|
148
150
|
* Decorator helper for enums (TypeScript).
|
|
149
151
|
* @param {Object} object Enum object
|
|
150
152
|
* @returns {Enum} Reflected enum
|
|
153
|
+
* @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
|
|
151
154
|
*/
|
|
152
155
|
util.decorateEnum = function decorateEnum(object) {
|
|
153
156
|
|
|
@@ -177,9 +180,8 @@ util.decorateEnum = function decorateEnum(object) {
|
|
|
177
180
|
util.setProperty = function setProperty(dst, path, value, ifNotSet) {
|
|
178
181
|
function setProp(dst, path, value) {
|
|
179
182
|
var part = path.shift();
|
|
180
|
-
if (part
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
+
if (unsafePropertyRe.test(part))
|
|
184
|
+
return dst;
|
|
183
185
|
if (path.length > 0) {
|
|
184
186
|
dst[part] = setProp(dst[part] || {}, path, value);
|
|
185
187
|
} else {
|
|
@@ -199,6 +201,8 @@ util.setProperty = function setProperty(dst, path, value, ifNotSet) {
|
|
|
199
201
|
throw TypeError("path must be specified");
|
|
200
202
|
|
|
201
203
|
path = path.split(".");
|
|
204
|
+
if (path.length > util.recursionLimit)
|
|
205
|
+
throw Error("max depth exceeded");
|
|
202
206
|
return setProp(dst, path, value);
|
|
203
207
|
};
|
|
204
208
|
|
|
@@ -207,6 +211,7 @@ util.setProperty = function setProperty(dst, path, value, ifNotSet) {
|
|
|
207
211
|
* @name util.decorateRoot
|
|
208
212
|
* @type {Root}
|
|
209
213
|
* @readonly
|
|
214
|
+
* @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
|
|
210
215
|
*/
|
|
211
216
|
Object.defineProperty(util, "decorateRoot", {
|
|
212
217
|
get: function() {
|
package/src/verifier.js
CHANGED
|
@@ -32,7 +32,7 @@ function genVerifyValue(gen, field, fieldIndex, ref) {
|
|
|
32
32
|
} else {
|
|
33
33
|
gen
|
|
34
34
|
("{")
|
|
35
|
-
("var e=types[%i].verify(%s);", fieldIndex, ref)
|
|
35
|
+
("var e=types[%i].verify(%s,q+1);", fieldIndex, ref)
|
|
36
36
|
("if(e)")
|
|
37
37
|
("return%j+e", field.name + ".")
|
|
38
38
|
("}");
|
|
@@ -122,9 +122,12 @@ function genVerifyKey(gen, field, ref) {
|
|
|
122
122
|
function verifier(mtype) {
|
|
123
123
|
/* eslint-disable no-unexpected-multiline */
|
|
124
124
|
|
|
125
|
-
var gen = util.codegen(["m"], mtype.name + "$verify")
|
|
125
|
+
var gen = util.codegen(["m", "q"], mtype.name + "$verify")
|
|
126
126
|
("if(typeof m!==\"object\"||m===null)")
|
|
127
|
-
("return%j", "object expected")
|
|
127
|
+
("return%j", "object expected")
|
|
128
|
+
("if(q===undefined)q=0")
|
|
129
|
+
("if(q>util.recursionLimit)")
|
|
130
|
+
("return%j", "max depth exceeded");
|
|
128
131
|
var oneofs = mtype.oneofsArray,
|
|
129
132
|
seenFirstField = {};
|
|
130
133
|
if (oneofs.length) gen
|
|
@@ -174,4 +177,4 @@ function verifier(mtype) {
|
|
|
174
177
|
return gen
|
|
175
178
|
("return null");
|
|
176
179
|
/* eslint-enable no-unexpected-multiline */
|
|
177
|
-
}
|
|
180
|
+
}
|
package/src/wrappers.js
CHANGED
|
@@ -38,7 +38,7 @@ var Message = require("./message");
|
|
|
38
38
|
// Custom wrapper for Any
|
|
39
39
|
wrappers[".google.protobuf.Any"] = {
|
|
40
40
|
|
|
41
|
-
fromObject: function(object) {
|
|
41
|
+
fromObject: function(object, depth) {
|
|
42
42
|
|
|
43
43
|
// unwrap value type if mapped
|
|
44
44
|
if (object && object["@type"]) {
|
|
@@ -54,14 +54,15 @@ wrappers[".google.protobuf.Any"] = {
|
|
|
54
54
|
if (type_url.indexOf("/") === -1) {
|
|
55
55
|
type_url = "/" + type_url;
|
|
56
56
|
}
|
|
57
|
+
var nextDepth = depth === undefined ? 1 : depth + 1;
|
|
57
58
|
return this.create({
|
|
58
59
|
type_url: type_url,
|
|
59
|
-
value: type.encode(type.fromObject(object)).finish()
|
|
60
|
+
value: type.encode(type.fromObject(object, nextDepth)).finish()
|
|
60
61
|
});
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
return this.fromObject(object);
|
|
65
|
+
return this.fromObject(object, depth);
|
|
65
66
|
},
|
|
66
67
|
|
|
67
68
|
toObject: function(message, options) {
|
package/src/writer.js
CHANGED
|
@@ -173,6 +173,11 @@ function writeByte(val, buf, pos) {
|
|
|
173
173
|
buf[pos] = val & 255;
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
function writeStringAscii(val, buf, pos) {
|
|
177
|
+
for (var i = 0; i < val.length;)
|
|
178
|
+
buf[pos++] = val.charCodeAt(i++);
|
|
179
|
+
}
|
|
180
|
+
|
|
176
181
|
function writeVarint32(val, buf, pos) {
|
|
177
182
|
while (val > 127) {
|
|
178
183
|
buf[pos++] = val & 127 | 128;
|
|
@@ -383,6 +388,16 @@ Writer.prototype.bytes = function write_bytes(value) {
|
|
|
383
388
|
return this.uint32(len)._push(writeBytes, len, value);
|
|
384
389
|
};
|
|
385
390
|
|
|
391
|
+
/**
|
|
392
|
+
* Writes raw bytes without a tag or length prefix.
|
|
393
|
+
* @param {Uint8Array} value Raw bytes
|
|
394
|
+
* @returns {Writer} `this`
|
|
395
|
+
*/
|
|
396
|
+
Writer.prototype.raw = function write_raw(value) {
|
|
397
|
+
var len = value.length >>> 0;
|
|
398
|
+
return len ? this._push(writeBytes, len, value) : this;
|
|
399
|
+
};
|
|
400
|
+
|
|
386
401
|
/**
|
|
387
402
|
* Writes a string.
|
|
388
403
|
* @param {string} value Value to write
|
|
@@ -391,7 +406,7 @@ Writer.prototype.bytes = function write_bytes(value) {
|
|
|
391
406
|
Writer.prototype.string = function write_string(value) {
|
|
392
407
|
var len = utf8.length(value);
|
|
393
408
|
return len
|
|
394
|
-
? this.uint32(len)._push(utf8.write, len, value)
|
|
409
|
+
? this.uint32(len)._push(len === value.length ? writeStringAscii : utf8.write, len, value)
|
|
395
410
|
: this._push(writeByte, 1, 0);
|
|
396
411
|
};
|
|
397
412
|
|
|
@@ -446,15 +461,28 @@ Writer.prototype.ldelim = function ldelim() {
|
|
|
446
461
|
* @returns {Uint8Array} Finished buffer
|
|
447
462
|
*/
|
|
448
463
|
Writer.prototype.finish = function finish() {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
464
|
+
return this.finishInto(this.constructor.alloc(this.len), 0);
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Finishes the write operation, writing into the provided buffer.
|
|
469
|
+
* The caller must ensure that `buf` has enough space starting at `offset`
|
|
470
|
+
* to hold {@link Writer#len} bytes.
|
|
471
|
+
* @param {T} buf Target buffer
|
|
472
|
+
* @param {number} [offset=0] Offset to start writing at
|
|
473
|
+
* @returns {T} The provided buffer
|
|
474
|
+
* @template T extends Uint8Array
|
|
475
|
+
*/
|
|
476
|
+
Writer.prototype.finishInto = function finishInto(buf, offset) {
|
|
477
|
+
if (offset === undefined)
|
|
478
|
+
offset = 0;
|
|
479
|
+
var head = this.head.next,
|
|
480
|
+
pos = offset;
|
|
452
481
|
while (head) {
|
|
453
482
|
head.fn(head.val, buf, pos);
|
|
454
483
|
pos += head.len;
|
|
455
484
|
head = head.next;
|
|
456
485
|
}
|
|
457
|
-
// this.head = this.tail = null;
|
|
458
486
|
return buf;
|
|
459
487
|
};
|
|
460
488
|
|
package/src/writer_buffer.js
CHANGED
|
@@ -54,6 +54,23 @@ BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
|
|
|
54
54
|
return this;
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Writes raw bytes without a tag or length prefix.
|
|
59
|
+
* @name BufferWriter#raw
|
|
60
|
+
* @function
|
|
61
|
+
* @param {Uint8Array} value Raw bytes
|
|
62
|
+
* @returns {BufferWriter} `this`
|
|
63
|
+
*/
|
|
64
|
+
BufferWriter.prototype.raw = function write_raw_buffer(value) {
|
|
65
|
+
var len = value.length >>> 0;
|
|
66
|
+
return len ? this._push(BufferWriter.writeBytesBuffer, len, value) : this;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
function writeStringBufferAscii(val, buf, pos) {
|
|
70
|
+
for (var i = 0; i < val.length;)
|
|
71
|
+
buf[pos++] = val.charCodeAt(i++);
|
|
72
|
+
}
|
|
73
|
+
|
|
57
74
|
function writeStringBuffer(val, buf, pos) {
|
|
58
75
|
if (val.length < 40) // plain js is faster for short strings (probably due to redundant assertions)
|
|
59
76
|
util.utf8.write(val, buf, pos);
|
|
@@ -70,7 +87,7 @@ BufferWriter.prototype.string = function write_string_buffer(value) {
|
|
|
70
87
|
var len = util.Buffer.byteLength(value);
|
|
71
88
|
this.uint32(len);
|
|
72
89
|
if (len)
|
|
73
|
-
this._push(writeStringBuffer, len, value);
|
|
90
|
+
this._push(len === value.length && len < 40 ? writeStringBufferAscii : writeStringBuffer, len, value);
|
|
74
91
|
return this;
|
|
75
92
|
};
|
|
76
93
|
|
package/tsconfig.json
CHANGED
package/ext/descriptor/test.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/*eslint-disable no-console*/
|
|
2
|
-
"use strict";
|
|
3
|
-
var protobuf = require("../../"),
|
|
4
|
-
descriptor = require(".");
|
|
5
|
-
|
|
6
|
-
/* var proto = {
|
|
7
|
-
nested: {
|
|
8
|
-
Message: {
|
|
9
|
-
fields: {
|
|
10
|
-
foo: {
|
|
11
|
-
type: "string",
|
|
12
|
-
id: 1
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
|
-
nested: {
|
|
16
|
-
SubMessage: {
|
|
17
|
-
fields: {}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
Enum: {
|
|
22
|
-
values: {
|
|
23
|
-
ONE: 1,
|
|
24
|
-
TWO: 2
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}; */
|
|
29
|
-
|
|
30
|
-
// var root = protobuf.Root.fromJSON(proto).resolveAll();
|
|
31
|
-
var root = protobuf.loadSync("tests/data/google/protobuf/descriptor.proto").resolveAll();
|
|
32
|
-
|
|
33
|
-
// console.log("Original proto", JSON.stringify(root, null, 2));
|
|
34
|
-
|
|
35
|
-
var msg = root.toDescriptor();
|
|
36
|
-
|
|
37
|
-
// console.log("\nDescriptor", JSON.stringify(msg.toObject(), null, 2));
|
|
38
|
-
|
|
39
|
-
var buf = descriptor.FileDescriptorSet.encode(msg).finish();
|
|
40
|
-
var root2 = protobuf.Root.fromDescriptor(buf, "proto2").resolveAll();
|
|
41
|
-
|
|
42
|
-
// console.log("\nDecoded proto", JSON.stringify(root2, null, 2));
|
|
43
|
-
|
|
44
|
-
var diff = require("deep-diff").diff(root.toJSON(), root2.toJSON());
|
|
45
|
-
if (diff) {
|
|
46
|
-
diff.forEach(function(diff) {
|
|
47
|
-
console.log(diff.kind + " @ " + diff.path.join("."));
|
|
48
|
-
console.log("lhs:", typeof diff.lhs, diff.lhs);
|
|
49
|
-
console.log("rhs:", typeof diff.rhs, diff.rhs);
|
|
50
|
-
console.log();
|
|
51
|
-
});
|
|
52
|
-
process.exitCode = 1;
|
|
53
|
-
} else
|
|
54
|
-
console.log("no differences");
|