@colyseus/schema 3.0.0-alpha.4 → 3.0.0-alpha.41
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 +148 -62
- package/bin/schema-debug +94 -0
- package/build/cjs/index.js +2201 -1507
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +2198 -1506
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +2208 -1514
- package/lib/Metadata.d.ts +21 -9
- package/lib/Metadata.js +169 -32
- package/lib/Metadata.js.map +1 -1
- package/lib/Reflection.d.ts +19 -4
- package/lib/Reflection.js +66 -32
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.d.ts +4 -4
- package/lib/Schema.js +44 -50
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +31 -34
- package/lib/annotations.js +110 -160
- package/lib/annotations.js.map +1 -1
- package/lib/bench_encode.d.ts +1 -0
- package/lib/bench_encode.js +130 -0
- package/lib/bench_encode.js.map +1 -0
- package/lib/codegen/api.js +1 -2
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/languages/cpp.js +1 -2
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +2 -46
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +1 -2
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +1 -2
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +1 -2
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +1 -2
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +1 -2
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.js +85 -3
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.js +6 -3
- package/lib/codegen/types.js.map +1 -1
- package/lib/debug.d.ts +1 -0
- package/lib/debug.js +51 -0
- package/lib/debug.js.map +1 -0
- package/lib/decoder/DecodeOperation.d.ts +3 -4
- package/lib/decoder/DecodeOperation.js +37 -19
- package/lib/decoder/DecodeOperation.js.map +1 -1
- package/lib/decoder/Decoder.d.ts +6 -7
- package/lib/decoder/Decoder.js +14 -14
- package/lib/decoder/Decoder.js.map +1 -1
- package/lib/decoder/ReferenceTracker.js +3 -2
- package/lib/decoder/ReferenceTracker.js.map +1 -1
- package/lib/decoder/strategy/RawChanges.js +1 -2
- package/lib/decoder/strategy/RawChanges.js.map +1 -1
- package/lib/decoder/strategy/StateCallbacks.d.ts +44 -11
- package/lib/decoder/strategy/StateCallbacks.js +75 -65
- package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
- package/lib/encoder/ChangeTree.d.ts +27 -21
- package/lib/encoder/ChangeTree.js +246 -186
- package/lib/encoder/ChangeTree.js.map +1 -1
- package/lib/encoder/EncodeOperation.d.ts +3 -6
- package/lib/encoder/EncodeOperation.js +51 -65
- package/lib/encoder/EncodeOperation.js.map +1 -1
- package/lib/encoder/Encoder.d.ts +9 -8
- package/lib/encoder/Encoder.js +168 -91
- package/lib/encoder/Encoder.js.map +1 -1
- package/lib/encoder/Root.d.ts +22 -0
- package/lib/encoder/Root.js +81 -0
- package/lib/encoder/Root.js.map +1 -0
- package/lib/encoder/StateView.d.ts +7 -7
- package/lib/encoder/StateView.js +70 -74
- package/lib/encoder/StateView.js.map +1 -1
- package/lib/encoding/assert.d.ts +7 -6
- package/lib/encoding/assert.js +13 -5
- package/lib/encoding/assert.js.map +1 -1
- package/lib/encoding/decode.d.ts +35 -20
- package/lib/encoding/decode.js +43 -87
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.d.ts +36 -17
- package/lib/encoding/encode.js +82 -68
- package/lib/encoding/encode.js.map +1 -1
- package/lib/encoding/spec.d.ts +4 -5
- package/lib/encoding/spec.js +1 -2
- package/lib/encoding/spec.js.map +1 -1
- package/lib/index.d.ts +10 -9
- package/lib/index.js +24 -17
- package/lib/index.js.map +1 -1
- package/lib/types/HelperTypes.d.ts +34 -2
- package/lib/types/HelperTypes.js.map +1 -1
- package/lib/types/TypeContext.d.ts +23 -0
- package/lib/types/TypeContext.js +111 -0
- package/lib/types/TypeContext.js.map +1 -0
- package/lib/types/custom/ArraySchema.d.ts +2 -2
- package/lib/types/custom/ArraySchema.js +33 -22
- package/lib/types/custom/ArraySchema.js.map +1 -1
- package/lib/types/custom/CollectionSchema.js +1 -0
- package/lib/types/custom/CollectionSchema.js.map +1 -1
- package/lib/types/custom/MapSchema.d.ts +3 -1
- package/lib/types/custom/MapSchema.js +12 -4
- package/lib/types/custom/MapSchema.js.map +1 -1
- package/lib/types/custom/SetSchema.js +1 -0
- package/lib/types/custom/SetSchema.js.map +1 -1
- package/lib/types/registry.d.ts +8 -1
- package/lib/types/registry.js +23 -6
- package/lib/types/registry.js.map +1 -1
- package/lib/types/symbols.d.ts +8 -5
- package/lib/types/symbols.js +9 -6
- package/lib/types/symbols.js.map +1 -1
- package/lib/types/utils.js +1 -2
- package/lib/types/utils.js.map +1 -1
- package/lib/utils.js +9 -7
- package/lib/utils.js.map +1 -1
- package/package.json +7 -6
- package/src/Metadata.ts +190 -42
- package/src/Reflection.ts +77 -39
- package/src/Schema.ts +59 -64
- package/src/annotations.ts +156 -202
- package/src/bench_encode.ts +108 -0
- package/src/codegen/languages/csharp.ts +1 -47
- package/src/codegen/parser.ts +107 -0
- package/src/codegen/types.ts +1 -0
- package/src/debug.ts +55 -0
- package/src/decoder/DecodeOperation.ts +46 -18
- package/src/decoder/Decoder.ts +17 -15
- package/src/decoder/ReferenceTracker.ts +3 -2
- package/src/decoder/strategy/StateCallbacks.ts +153 -82
- package/src/encoder/ChangeTree.ts +286 -202
- package/src/encoder/EncodeOperation.ts +78 -78
- package/src/encoder/Encoder.ts +202 -97
- package/src/encoder/Root.ts +93 -0
- package/src/encoder/StateView.ts +76 -88
- package/src/encoding/assert.ts +17 -8
- package/src/encoding/decode.ts +62 -97
- package/src/encoding/encode.ts +99 -65
- package/src/encoding/spec.ts +3 -5
- package/src/index.ts +12 -20
- package/src/types/HelperTypes.ts +54 -2
- package/src/types/TypeContext.ts +133 -0
- package/src/types/custom/ArraySchema.ts +49 -19
- package/src/types/custom/CollectionSchema.ts +1 -0
- package/src/types/custom/MapSchema.ts +18 -5
- package/src/types/custom/SetSchema.ts +1 -0
- package/src/types/registry.ts +22 -3
- package/src/types/symbols.ts +10 -7
- package/src/utils.ts +7 -3
package/lib/encoding/decode.js
CHANGED
|
@@ -22,8 +22,15 @@
|
|
|
22
22
|
* SOFTWARE
|
|
23
23
|
*/
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.
|
|
26
|
-
|
|
25
|
+
exports.decode = void 0;
|
|
26
|
+
// force little endian to facilitate decoding on multiple implementations
|
|
27
|
+
const _isLittleEndian = true; // new Uint16Array(new Uint8Array([1, 0]).buffer)[0] === 1;
|
|
28
|
+
const _convoBuffer = new ArrayBuffer(8);
|
|
29
|
+
const _int32 = new Int32Array(_convoBuffer);
|
|
30
|
+
const _float32 = new Float32Array(_convoBuffer);
|
|
31
|
+
const _float64 = new Float64Array(_convoBuffer);
|
|
32
|
+
const _uint64 = new BigUint64Array(_convoBuffer);
|
|
33
|
+
const _int64 = new BigInt64Array(_convoBuffer);
|
|
27
34
|
function utf8Read(bytes, it, length) {
|
|
28
35
|
var string = '', chr = 0;
|
|
29
36
|
for (var i = it.offset, end = it.offset + length; i < end; i++) {
|
|
@@ -64,81 +71,66 @@ function utf8Read(bytes, it, length) {
|
|
|
64
71
|
it.offset += length;
|
|
65
72
|
return string;
|
|
66
73
|
}
|
|
67
|
-
exports.utf8Read = utf8Read;
|
|
68
74
|
function int8(bytes, it) {
|
|
69
75
|
return uint8(bytes, it) << 24 >> 24;
|
|
70
76
|
}
|
|
71
|
-
exports.int8 = int8;
|
|
72
77
|
;
|
|
73
78
|
function uint8(bytes, it) {
|
|
74
79
|
return bytes[it.offset++];
|
|
75
80
|
}
|
|
76
|
-
exports.uint8 = uint8;
|
|
77
81
|
;
|
|
78
82
|
function int16(bytes, it) {
|
|
79
83
|
return uint16(bytes, it) << 16 >> 16;
|
|
80
84
|
}
|
|
81
|
-
exports.int16 = int16;
|
|
82
85
|
;
|
|
83
86
|
function uint16(bytes, it) {
|
|
84
87
|
return bytes[it.offset++] | bytes[it.offset++] << 8;
|
|
85
88
|
}
|
|
86
|
-
exports.uint16 = uint16;
|
|
87
89
|
;
|
|
88
90
|
function int32(bytes, it) {
|
|
89
91
|
return bytes[it.offset++] | bytes[it.offset++] << 8 | bytes[it.offset++] << 16 | bytes[it.offset++] << 24;
|
|
90
92
|
}
|
|
91
|
-
exports.int32 = int32;
|
|
92
93
|
;
|
|
93
94
|
function uint32(bytes, it) {
|
|
94
95
|
return int32(bytes, it) >>> 0;
|
|
95
96
|
}
|
|
96
|
-
exports.uint32 = uint32;
|
|
97
97
|
;
|
|
98
98
|
function float32(bytes, it) {
|
|
99
|
-
|
|
99
|
+
_int32[0] = int32(bytes, it);
|
|
100
|
+
return _float32[0];
|
|
100
101
|
}
|
|
101
|
-
|
|
102
|
+
;
|
|
102
103
|
function float64(bytes, it) {
|
|
103
|
-
|
|
104
|
+
_int32[_isLittleEndian ? 0 : 1] = int32(bytes, it);
|
|
105
|
+
_int32[_isLittleEndian ? 1 : 0] = int32(bytes, it);
|
|
106
|
+
return _float64[0];
|
|
104
107
|
}
|
|
105
|
-
|
|
108
|
+
;
|
|
106
109
|
function int64(bytes, it) {
|
|
107
110
|
const low = uint32(bytes, it);
|
|
108
111
|
const high = int32(bytes, it) * Math.pow(2, 32);
|
|
109
112
|
return high + low;
|
|
110
113
|
}
|
|
111
|
-
exports.int64 = int64;
|
|
112
114
|
;
|
|
113
115
|
function uint64(bytes, it) {
|
|
114
116
|
const low = uint32(bytes, it);
|
|
115
117
|
const high = uint32(bytes, it) * Math.pow(2, 32);
|
|
116
118
|
return high + low;
|
|
117
119
|
}
|
|
118
|
-
exports.uint64 = uint64;
|
|
119
120
|
;
|
|
120
|
-
|
|
121
|
-
const _isLittleEndian = true; // new Uint16Array(new Uint8Array([1, 0]).buffer)[0] === 1;
|
|
122
|
-
const _int32 = new Int32Array(2);
|
|
123
|
-
const _float32 = new Float32Array(_int32.buffer);
|
|
124
|
-
const _float64 = new Float64Array(_int32.buffer);
|
|
125
|
-
function readFloat32(bytes, it) {
|
|
121
|
+
function bigint64(bytes, it) {
|
|
126
122
|
_int32[0] = int32(bytes, it);
|
|
127
|
-
|
|
123
|
+
_int32[1] = int32(bytes, it);
|
|
124
|
+
return _int64[0];
|
|
128
125
|
}
|
|
129
|
-
|
|
130
|
-
;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
_int32[_isLittleEndian ? 1 : 0] = int32(bytes, it);
|
|
134
|
-
return _float64[0];
|
|
126
|
+
function biguint64(bytes, it) {
|
|
127
|
+
_int32[0] = int32(bytes, it);
|
|
128
|
+
_int32[1] = int32(bytes, it);
|
|
129
|
+
return _uint64[0];
|
|
135
130
|
}
|
|
136
|
-
exports.readFloat64 = readFloat64;
|
|
137
|
-
;
|
|
138
131
|
function boolean(bytes, it) {
|
|
139
132
|
return uint8(bytes, it) > 0;
|
|
140
133
|
}
|
|
141
|
-
exports.boolean = boolean;
|
|
142
134
|
;
|
|
143
135
|
function string(bytes, it) {
|
|
144
136
|
const prefix = bytes[it.offset++];
|
|
@@ -158,20 +150,6 @@ function string(bytes, it) {
|
|
|
158
150
|
}
|
|
159
151
|
return utf8Read(bytes, it, length);
|
|
160
152
|
}
|
|
161
|
-
exports.string = string;
|
|
162
|
-
function stringCheck(bytes, it) {
|
|
163
|
-
const prefix = bytes[it.offset];
|
|
164
|
-
return (
|
|
165
|
-
// fixstr
|
|
166
|
-
(prefix < 0xc0 && prefix > 0xa0) ||
|
|
167
|
-
// str 8
|
|
168
|
-
prefix === 0xd9 ||
|
|
169
|
-
// str 16
|
|
170
|
-
prefix === 0xda ||
|
|
171
|
-
// str 32
|
|
172
|
-
prefix === 0xdb);
|
|
173
|
-
}
|
|
174
|
-
exports.stringCheck = stringCheck;
|
|
175
153
|
function number(bytes, it) {
|
|
176
154
|
const prefix = bytes[it.offset++];
|
|
177
155
|
if (prefix < 0x80) {
|
|
@@ -180,11 +158,11 @@ function number(bytes, it) {
|
|
|
180
158
|
}
|
|
181
159
|
else if (prefix === 0xca) {
|
|
182
160
|
// float 32
|
|
183
|
-
return
|
|
161
|
+
return float32(bytes, it);
|
|
184
162
|
}
|
|
185
163
|
else if (prefix === 0xcb) {
|
|
186
164
|
// float 64
|
|
187
|
-
return
|
|
165
|
+
return float64(bytes, it);
|
|
188
166
|
}
|
|
189
167
|
else if (prefix === 0xcc) {
|
|
190
168
|
// uint 8
|
|
@@ -223,45 +201,23 @@ function number(bytes, it) {
|
|
|
223
201
|
return (0xff - prefix + 1) * -1;
|
|
224
202
|
}
|
|
225
203
|
}
|
|
226
|
-
exports.number = number;
|
|
227
204
|
;
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
return bytes[it.offset] < 0xa0;
|
|
247
|
-
// const prefix = bytes[it.offset] ;
|
|
248
|
-
// if (prefix < 0xa0) {
|
|
249
|
-
// return prefix;
|
|
250
|
-
// // array
|
|
251
|
-
// } else if (prefix === 0xdc) {
|
|
252
|
-
// it.offset += 2;
|
|
253
|
-
// } else if (0xdd) {
|
|
254
|
-
// it.offset += 4;
|
|
255
|
-
// }
|
|
256
|
-
// return prefix;
|
|
257
|
-
}
|
|
258
|
-
exports.arrayCheck = arrayCheck;
|
|
259
|
-
function switchStructureCheck(bytes, it) {
|
|
260
|
-
return (
|
|
261
|
-
// previous byte should be `SWITCH_TO_STRUCTURE`
|
|
262
|
-
bytes[it.offset - 1] === spec_1.SWITCH_TO_STRUCTURE &&
|
|
263
|
-
// next byte should be a number
|
|
264
|
-
(bytes[it.offset] < 0x80 || (bytes[it.offset] >= 0xca && bytes[it.offset] <= 0xd3)));
|
|
265
|
-
}
|
|
266
|
-
exports.switchStructureCheck = switchStructureCheck;
|
|
205
|
+
exports.decode = {
|
|
206
|
+
utf8Read,
|
|
207
|
+
int8,
|
|
208
|
+
uint8,
|
|
209
|
+
int16,
|
|
210
|
+
uint16,
|
|
211
|
+
int32,
|
|
212
|
+
uint32,
|
|
213
|
+
float32,
|
|
214
|
+
float64,
|
|
215
|
+
int64,
|
|
216
|
+
uint64,
|
|
217
|
+
bigint64,
|
|
218
|
+
biguint64,
|
|
219
|
+
boolean,
|
|
220
|
+
string,
|
|
221
|
+
number,
|
|
222
|
+
};
|
|
267
223
|
//# sourceMappingURL=decode.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decode.js","sourceRoot":"","sources":["../../src/encoding/decode.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;;;AAEH,iCAA6C;AAU7C,SAAgB,QAAQ,CAAC,KAAiB,EAAE,EAAY,EAAE,MAAc;IACtE,IAAI,MAAM,GAAG,EAAE,EAAE,GAAG,GAAG,CAAC,CAAC;IACzB,KAAK,IAAI,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,GAAG,GAAG,EAAE,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;QAC/D,IAAI,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;YAC3B,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;YACpC,SAAS;QACX,CAAC;QACD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,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yBAAyB;IACzB,yBAAyB;IACzB,yBAAyB;IACzB,yBAAyB;IACzB,yBAAyB;IACzB,yBAAyB;IACzB,OAAO,CACL,MAAM,GAAG,IAAI;QACb,CAAC,MAAM,IAAI,IAAI,IAAI,MAAM,IAAI,IAAI,CAAC,CACnC,CAAC;AACJ,CAAC;AAjBD,kCAiBC;AAED,SAAgB,UAAU,CAAE,KAAiB,EAAE,EAAY;IACzD,OAAO,KAAK,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC;IAE/B,oCAAoC;IAEpC,uBAAuB;IACvB,mBAAmB;IAEnB,WAAW;IACX,gCAAgC;IAChC,oBAAoB;IAEpB,qBAAqB;IACrB,oBAAoB;IACpB,IAAI;IAEJ,iBAAiB;AACnB,CAAC;AAjBD,gCAiBC;AAED,SAAgB,oBAAoB,CAAC,KAAiB,EAAE,EAAY;IAClE,OAAO;IACH,gDAAgD;IAChD,KAAK,CAAC,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,0BAAmB;QAC5C,+BAA+B;QAC/B,CAAC,KAAK,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,CACtF,CAAC;AACJ,CAAC;AAPD,oDAOC","sourcesContent":["/**\n * Copyright (c) 2018 Endel Dreyer\n * Copyright (c) 2014 Ion Drive Software Ltd.\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE\n */\n\nimport { SWITCH_TO_STRUCTURE } from \"./spec\";\nimport type { BufferLike } from \"./encode\";\n\n/**\n * msgpack implementation highly based on notepack.io\n * https://github.com/darrachequesne/notepack\n */\n\nexport interface Iterator { offset: number; }\n\nexport function utf8Read(bytes: BufferLike, it: Iterator, length: number) {\n var string = '', chr = 0;\n for (var i = it.offset, end = it.offset + length; i < end; i++) {\n var byte = bytes[i];\n if ((byte & 0x80) === 0x00) {\n string += String.fromCharCode(byte);\n continue;\n }\n if ((byte & 0xe0) === 0xc0) {\n string += String.fromCharCode(\n ((byte & 0x1f) << 6) |\n (bytes[++i] & 0x3f)\n );\n continue;\n }\n if ((byte & 0xf0) === 0xe0) {\n string += String.fromCharCode(\n ((byte & 0x0f) << 12) |\n ((bytes[++i] & 0x3f) << 6) |\n ((bytes[++i] & 0x3f) << 0)\n );\n continue;\n }\n if ((byte & 0xf8) === 0xf0) {\n chr = ((byte & 0x07) << 18) |\n ((bytes[++i] & 0x3f) << 12) |\n ((bytes[++i] & 0x3f) << 6) |\n ((bytes[++i] & 0x3f) << 0);\n if (chr >= 0x010000) { // surrogate pair\n chr -= 0x010000;\n string += String.fromCharCode((chr >>> 10) + 0xD800, (chr & 0x3FF) + 0xDC00);\n } else {\n string += String.fromCharCode(chr);\n }\n continue;\n }\n\n console.error('Invalid byte ' + byte.toString(16));\n // (do not throw error to avoid server/client from crashing due to hack attemps)\n // throw new Error('Invalid byte ' + byte.toString(16));\n }\n it.offset += length;\n return string;\n}\n\nexport function int8 (bytes: BufferLike, it: Iterator) {\n return uint8(bytes, it) << 24 >> 24;\n};\n\nexport function uint8 (bytes: BufferLike, it: Iterator) {\n return bytes[it.offset++];\n};\n\nexport function int16 (bytes: BufferLike, it: Iterator) {\n return uint16(bytes, it) << 16 >> 16;\n};\n\nexport function uint16 (bytes: BufferLike, it: Iterator) {\n return bytes[it.offset++] | bytes[it.offset++] << 8;\n};\n\nexport function int32 (bytes: BufferLike, it: Iterator) {\n return bytes[it.offset++] | bytes[it.offset++] << 8 | bytes[it.offset++] << 16 | bytes[it.offset++] << 24;\n};\n\nexport function uint32 (bytes: BufferLike, it: Iterator) {\n return int32(bytes, it) >>> 0;\n};\n\nexport function float32(bytes: BufferLike, it: Iterator) {\n return readFloat32(bytes, it);\n}\n\nexport function float64(bytes: BufferLike, it: Iterator) {\n return readFloat64(bytes, it);\n}\n\nexport function int64(bytes: BufferLike, it: Iterator) {\n const low = uint32(bytes, it);\n const high = int32(bytes, it) * Math.pow(2, 32);\n return high + low;\n};\n\nexport function uint64(bytes: BufferLike, it: Iterator) {\n const low = uint32(bytes, it);\n const high = uint32(bytes, it) * Math.pow(2, 32);\n return high + low;\n};\n\n// force little endian to facilitate decoding on multiple implementations\nconst _isLittleEndian = true; // new Uint16Array(new Uint8Array([1, 0]).buffer)[0] === 1;\nconst _int32 = new Int32Array(2);\nconst _float32 = new Float32Array(_int32.buffer);\nconst _float64 = new Float64Array(_int32.buffer);\n\nexport function readFloat32 (bytes: BufferLike, it: Iterator) {\n _int32[0] = int32(bytes, it);\n return _float32[0];\n};\n\nexport function readFloat64 (bytes: BufferLike, it: Iterator) {\n _int32[_isLittleEndian ? 0 : 1] = int32(bytes, it);\n _int32[_isLittleEndian ? 1 : 0] = int32(bytes, it);\n return _float64[0];\n};\n\nexport function boolean (bytes: BufferLike, it: Iterator) {\n return uint8(bytes, it) > 0;\n};\n\nexport function string (bytes: BufferLike, it: Iterator) {\n const prefix = bytes[it.offset++];\n let length: number;\n\n if (prefix < 0xc0) {\n // fixstr\n length = prefix & 0x1f;\n\n } else if (prefix === 0xd9) {\n length = uint8(bytes, it);\n\n } else if (prefix === 0xda) {\n length = uint16(bytes, it);\n\n } else if (prefix === 0xdb) {\n length = uint32(bytes, it);\n }\n\n return utf8Read(bytes, it, length);\n}\n\nexport function stringCheck(bytes: BufferLike, it: Iterator) {\n const prefix = bytes[it.offset];\n return (\n // fixstr\n (prefix < 0xc0 && prefix > 0xa0) ||\n // str 8\n prefix === 0xd9 ||\n // str 16\n prefix === 0xda ||\n // str 32\n prefix === 0xdb\n );\n}\n\nexport function number (bytes: BufferLike, it: Iterator) {\n const prefix = bytes[it.offset++];\n\n if (prefix < 0x80) {\n // positive fixint\n return prefix;\n\n } else if (prefix === 0xca) {\n // float 32\n return readFloat32(bytes, it);\n\n } else if (prefix === 0xcb) {\n // float 64\n return readFloat64(bytes, it);\n\n } else if (prefix === 0xcc) {\n // uint 8\n return uint8(bytes, it);\n\n } else if (prefix === 0xcd) {\n // uint 16\n return uint16(bytes, it);\n\n } else if (prefix === 0xce) {\n // uint 32\n return uint32(bytes, it);\n\n } else if (prefix === 0xcf) {\n // uint 64\n return uint64(bytes, it);\n\n } else if (prefix === 0xd0) {\n // int 8\n return int8(bytes, it);\n\n } else if (prefix === 0xd1) {\n // int 16\n return int16(bytes, it);\n\n } else if (prefix === 0xd2) {\n // int 32\n return int32(bytes, it);\n\n } else if (prefix === 0xd3) {\n // int 64\n return int64(bytes, it);\n\n } else if (prefix > 0xdf) {\n // negative fixint\n return (0xff - prefix + 1) * -1\n }\n};\n\nexport function numberCheck (bytes: BufferLike, it: Iterator) {\n const prefix = bytes[it.offset];\n // positive fixint - 0x00 - 0x7f\n // float 32 - 0xca\n // float 64 - 0xcb\n // uint 8 - 0xcc\n // uint 16 - 0xcd\n // uint 32 - 0xce\n // uint 64 - 0xcf\n // int 8 - 0xd0\n // int 16 - 0xd1\n // int 32 - 0xd2\n // int 64 - 0xd3\n return (\n prefix < 0x80 ||\n (prefix >= 0xca && prefix <= 0xd3)\n );\n}\n\nexport function arrayCheck (bytes: BufferLike, it: Iterator) {\n return bytes[it.offset] < 0xa0;\n\n // const prefix = bytes[it.offset] ;\n\n // if (prefix < 0xa0) {\n // return prefix;\n\n // // array\n // } else if (prefix === 0xdc) {\n // it.offset += 2;\n\n // } else if (0xdd) {\n // it.offset += 4;\n // }\n\n // return prefix;\n}\n\nexport function switchStructureCheck(bytes: BufferLike, it: Iterator) {\n return (\n // previous byte should be `SWITCH_TO_STRUCTURE`\n bytes[it.offset - 1] === SWITCH_TO_STRUCTURE &&\n // next byte should be a number\n (bytes[it.offset] < 0x80 || (bytes[it.offset] >= 0xca && bytes[it.offset] <= 0xd3))\n );\n}"]}
|
|
1
|
+
{"version":3,"file":"decode.js","sourceRoot":"","sources":["../../src/encoding/decode.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;;;AAWH,yEAAyE;AACzE,MAAM,eAAe,GAAG,IAAI,CAAC,CAAE,2DAA2D;AAC1F,MAAM,YAAY,GAAG,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;AAExC,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,YAAY,CAAC,CAAC;AAC5C,MAAM,QAAQ,GAAG,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC;AAChD,MAAM,QAAQ,GAAG,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC;AAChD,MAAM,OAAO,GAAG,IAAI,cAAc,CAAC,YAAY,CAAC,CAAC;AACjD,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,YAAY,CAAC,CAAC;AAE/C,SAAS,QAAQ,CAAC,KAAiB,EAAE,EAAY,EAAE,MAAc;IAC/D,IAAI,MAAM,GAAG,EAAE,EAAE,GAAG,GAAG,CAAC,CAAC;IACzB,KAAK,IAAI,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,GAAG,GAAG,EAAE,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V,OAAO,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAE3B,CAAC;SAAM,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QAC3B,UAAU;QACV,OAAO,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAE3B,CAAC;SAAM,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QAC3B,QAAQ;QACR,OAAO,IAAI,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAEzB,CAAC;SAAM,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QAC3B,SAAS;QACT,OAAO,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAE1B,CAAC;SAAM,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QAC3B,SAAS;QACT,OAAO,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAE1B,CAAC;SAAM,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QAC3B,SAAS;QACT,OAAO,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAE1B,CAAC;SAAM,IAAI,MAAM,GAAG,IAAI,EAAE,CAAC;QACzB,kBAAkB;QAClB,OAAO,CAAC,IAAI,GAAG,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;IACjC,CAAC;AACH,CAAC;AAAA,CAAC;AAEW,QAAA,MAAM,GAAG;IAClB,QAAQ;IACR,IAAI;IACJ,KAAK;IACL,KAAK;IACL,MAAM;IACN,KAAK;IACL,MAAM;IACN,OAAO;IACP,OAAO;IACP,KAAK;IACL,MAAM;IACN,QAAQ;IACR,SAAS;IACT,OAAO;IACP,MAAM;IACN,MAAM;CACT,CAAA","sourcesContent":["/**\n * Copyright (c) 2018 Endel Dreyer\n * Copyright (c) 2014 Ion Drive Software Ltd.\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE\n */\n\nimport type { BufferLike } from \"./encode\";\n\n/**\n * msgpack implementation highly based on notepack.io\n * https://github.com/darrachequesne/notepack\n */\n\nexport interface Iterator { offset: number; }\n\n// force little endian to facilitate decoding on multiple implementations\nconst _isLittleEndian = true; // new Uint16Array(new Uint8Array([1, 0]).buffer)[0] === 1;\nconst _convoBuffer = new ArrayBuffer(8);\n\nconst _int32 = new Int32Array(_convoBuffer);\nconst _float32 = new Float32Array(_convoBuffer);\nconst _float64 = new Float64Array(_convoBuffer);\nconst _uint64 = new BigUint64Array(_convoBuffer);\nconst _int64 = new BigInt64Array(_convoBuffer);\n\nfunction utf8Read(bytes: BufferLike, it: Iterator, length: number) {\n var string = '', chr = 0;\n for (var i = it.offset, end = it.offset + length; i < end; i++) {\n var byte = bytes[i];\n if ((byte & 0x80) === 0x00) {\n string += String.fromCharCode(byte);\n continue;\n }\n if ((byte & 0xe0) === 0xc0) {\n string += String.fromCharCode(\n ((byte & 0x1f) << 6) |\n (bytes[++i] & 0x3f)\n );\n continue;\n }\n if ((byte & 0xf0) === 0xe0) {\n string += String.fromCharCode(\n ((byte & 0x0f) << 12) |\n ((bytes[++i] & 0x3f) << 6) |\n ((bytes[++i] & 0x3f) << 0)\n );\n continue;\n }\n if ((byte & 0xf8) === 0xf0) {\n chr = ((byte & 0x07) << 18) |\n ((bytes[++i] & 0x3f) << 12) |\n ((bytes[++i] & 0x3f) << 6) |\n ((bytes[++i] & 0x3f) << 0);\n if (chr >= 0x010000) { // surrogate pair\n chr -= 0x010000;\n string += String.fromCharCode((chr >>> 10) + 0xD800, (chr & 0x3FF) + 0xDC00);\n } else {\n string += String.fromCharCode(chr);\n }\n continue;\n }\n\n console.error('Invalid byte ' + byte.toString(16));\n // (do not throw error to avoid server/client from crashing due to hack attemps)\n // throw new Error('Invalid byte ' + byte.toString(16));\n }\n it.offset += length;\n return string;\n}\n\nfunction int8 (bytes: BufferLike, it: Iterator) {\n return uint8(bytes, it) << 24 >> 24;\n};\n\nfunction uint8 (bytes: BufferLike, it: Iterator) {\n return bytes[it.offset++];\n};\n\nfunction int16 (bytes: BufferLike, it: Iterator) {\n return uint16(bytes, it) << 16 >> 16;\n};\n\nfunction uint16 (bytes: BufferLike, it: Iterator) {\n return bytes[it.offset++] | bytes[it.offset++] << 8;\n};\n\nfunction int32 (bytes: BufferLike, it: Iterator) {\n return bytes[it.offset++] | bytes[it.offset++] << 8 | bytes[it.offset++] << 16 | bytes[it.offset++] << 24;\n};\n\nfunction uint32 (bytes: BufferLike, it: Iterator) {\n return int32(bytes, it) >>> 0;\n};\n\nfunction float32 (bytes: BufferLike, it: Iterator) {\n _int32[0] = int32(bytes, it);\n return _float32[0];\n};\n\nfunction float64 (bytes: BufferLike, it: Iterator) {\n _int32[_isLittleEndian ? 0 : 1] = int32(bytes, it);\n _int32[_isLittleEndian ? 1 : 0] = int32(bytes, it);\n return _float64[0];\n};\n\nfunction int64(bytes: BufferLike, it: Iterator) {\n const low = uint32(bytes, it);\n const high = int32(bytes, it) * Math.pow(2, 32);\n return high + low;\n};\n\nfunction uint64(bytes: BufferLike, it: Iterator) {\n const low = uint32(bytes, it);\n const high = uint32(bytes, it) * Math.pow(2, 32);\n return high + low;\n};\n\nfunction bigint64(bytes: BufferLike, it: Iterator) {\n _int32[0] = int32(bytes, it);\n _int32[1] = int32(bytes, it);\n return _int64[0];\n}\n\nfunction biguint64(bytes: BufferLike, it: Iterator) {\n _int32[0] = int32(bytes, it);\n _int32[1] = int32(bytes, it);\n return _uint64[0];\n}\n\nfunction boolean (bytes: BufferLike, it: Iterator) {\n return uint8(bytes, it) > 0;\n};\n\nfunction string (bytes: BufferLike, it: Iterator) {\n const prefix = bytes[it.offset++];\n let length: number;\n\n if (prefix < 0xc0) {\n // fixstr\n length = prefix & 0x1f;\n\n } else if (prefix === 0xd9) {\n length = uint8(bytes, it);\n\n } else if (prefix === 0xda) {\n length = uint16(bytes, it);\n\n } else if (prefix === 0xdb) {\n length = uint32(bytes, it);\n }\n\n return utf8Read(bytes, it, length);\n}\n\nfunction number (bytes: BufferLike, it: Iterator) {\n const prefix = bytes[it.offset++];\n\n if (prefix < 0x80) {\n // positive fixint\n return prefix;\n\n } else if (prefix === 0xca) {\n // float 32\n return float32(bytes, it);\n\n } else if (prefix === 0xcb) {\n // float 64\n return float64(bytes, it);\n\n } else if (prefix === 0xcc) {\n // uint 8\n return uint8(bytes, it);\n\n } else if (prefix === 0xcd) {\n // uint 16\n return uint16(bytes, it);\n\n } else if (prefix === 0xce) {\n // uint 32\n return uint32(bytes, it);\n\n } else if (prefix === 0xcf) {\n // uint 64\n return uint64(bytes, it);\n\n } else if (prefix === 0xd0) {\n // int 8\n return int8(bytes, it);\n\n } else if (prefix === 0xd1) {\n // int 16\n return int16(bytes, it);\n\n } else if (prefix === 0xd2) {\n // int 32\n return int32(bytes, it);\n\n } else if (prefix === 0xd3) {\n // int 64\n return int64(bytes, it);\n\n } else if (prefix > 0xdf) {\n // negative fixint\n return (0xff - prefix + 1) * -1\n }\n};\n\nexport const decode = {\n utf8Read,\n int8,\n uint8,\n int16,\n uint16,\n int32,\n uint32,\n float32,\n float64,\n int64,\n uint64,\n bigint64,\n biguint64,\n boolean,\n string,\n number,\n}"]}
|
package/lib/encoding/encode.d.ts
CHANGED
|
@@ -22,20 +22,39 @@
|
|
|
22
22
|
*/
|
|
23
23
|
import type { Iterator } from "./decode";
|
|
24
24
|
export type BufferLike = number[] | ArrayBufferLike;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
export declare
|
|
25
|
+
declare function utf8Write(view: BufferLike, str: string, it: Iterator): void;
|
|
26
|
+
declare function int8(bytes: BufferLike, value: number, it: Iterator): void;
|
|
27
|
+
declare function uint8(bytes: BufferLike, value: number, it: Iterator): void;
|
|
28
|
+
declare function int16(bytes: BufferLike, value: number, it: Iterator): void;
|
|
29
|
+
declare function uint16(bytes: BufferLike, value: number, it: Iterator): void;
|
|
30
|
+
declare function int32(bytes: BufferLike, value: number, it: Iterator): void;
|
|
31
|
+
declare function uint32(bytes: BufferLike, value: number, it: Iterator): void;
|
|
32
|
+
declare function int64(bytes: BufferLike, value: number, it: Iterator): void;
|
|
33
|
+
declare function uint64(bytes: BufferLike, value: number, it: Iterator): void;
|
|
34
|
+
declare function bigint64(bytes: BufferLike, value: bigint, it: Iterator): void;
|
|
35
|
+
declare function biguint64(bytes: BufferLike, value: bigint, it: Iterator): void;
|
|
36
|
+
declare function float32(bytes: BufferLike, value: number, it: Iterator): void;
|
|
37
|
+
declare function float64(bytes: BufferLike, value: number, it: Iterator): void;
|
|
38
|
+
declare function boolean(bytes: BufferLike, value: number, it: Iterator): void;
|
|
39
|
+
declare function string(bytes: BufferLike, value: string, it: Iterator): number;
|
|
40
|
+
declare function number(bytes: BufferLike, value: number, it: Iterator): 1 | 2 | 3 | 5 | 9;
|
|
41
|
+
export declare const encode: {
|
|
42
|
+
int8: typeof int8;
|
|
43
|
+
uint8: typeof uint8;
|
|
44
|
+
int16: typeof int16;
|
|
45
|
+
uint16: typeof uint16;
|
|
46
|
+
int32: typeof int32;
|
|
47
|
+
uint32: typeof uint32;
|
|
48
|
+
int64: typeof int64;
|
|
49
|
+
uint64: typeof uint64;
|
|
50
|
+
bigint64: typeof bigint64;
|
|
51
|
+
biguint64: typeof biguint64;
|
|
52
|
+
float32: typeof float32;
|
|
53
|
+
float64: typeof float64;
|
|
54
|
+
boolean: typeof boolean;
|
|
55
|
+
string: typeof string;
|
|
56
|
+
number: typeof number;
|
|
57
|
+
utf8Write: typeof utf8Write;
|
|
58
|
+
utf8Length: (str: string, _?: any) => number;
|
|
59
|
+
};
|
|
60
|
+
export {};
|
package/lib/encoding/encode.js
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* SOFTWARE
|
|
23
23
|
*/
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.
|
|
25
|
+
exports.encode = void 0;
|
|
26
26
|
/**
|
|
27
27
|
* msgpack implementation highly based on notepack.io
|
|
28
28
|
* https://github.com/darrachequesne/notepack
|
|
@@ -33,27 +33,36 @@ try {
|
|
|
33
33
|
textEncoder = new TextEncoder();
|
|
34
34
|
}
|
|
35
35
|
catch (e) { }
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
36
|
+
// force little endian to facilitate decoding on multiple implementations
|
|
37
|
+
const _isLittleEndian = true; // new Uint16Array(new Uint8Array([1, 0]).buffer)[0] === 1;
|
|
38
|
+
const _convoBuffer = new ArrayBuffer(8);
|
|
39
|
+
const _int32 = new Int32Array(_convoBuffer);
|
|
40
|
+
const _float32 = new Float32Array(_convoBuffer);
|
|
41
|
+
const _float64 = new Float64Array(_convoBuffer);
|
|
42
|
+
const _int64 = new BigInt64Array(_convoBuffer);
|
|
43
|
+
const hasBufferByteLength = (typeof Buffer !== 'undefined' && Buffer.byteLength);
|
|
44
|
+
const utf8Length = (hasBufferByteLength)
|
|
45
|
+
? Buffer.byteLength // node
|
|
46
|
+
: function (str, _) {
|
|
47
|
+
var c = 0, length = 0;
|
|
48
|
+
for (var i = 0, l = str.length; i < l; i++) {
|
|
49
|
+
c = str.charCodeAt(i);
|
|
50
|
+
if (c < 0x80) {
|
|
51
|
+
length += 1;
|
|
52
|
+
}
|
|
53
|
+
else if (c < 0x800) {
|
|
54
|
+
length += 2;
|
|
55
|
+
}
|
|
56
|
+
else if (c < 0xd800 || c >= 0xe000) {
|
|
57
|
+
length += 3;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
i++;
|
|
61
|
+
length += 4;
|
|
62
|
+
}
|
|
52
63
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
exports.utf8Length = utf8Length;
|
|
64
|
+
return length;
|
|
65
|
+
};
|
|
57
66
|
function utf8Write(view, str, it) {
|
|
58
67
|
var c = 0;
|
|
59
68
|
for (var i = 0, l = str.length; i < l; i++) {
|
|
@@ -62,46 +71,44 @@ function utf8Write(view, str, it) {
|
|
|
62
71
|
view[it.offset++] = c;
|
|
63
72
|
}
|
|
64
73
|
else if (c < 0x800) {
|
|
65
|
-
view[it.offset
|
|
66
|
-
view[it.offset
|
|
74
|
+
view[it.offset] = 0xc0 | (c >> 6);
|
|
75
|
+
view[it.offset + 1] = 0x80 | (c & 0x3f);
|
|
76
|
+
it.offset += 2;
|
|
67
77
|
}
|
|
68
78
|
else if (c < 0xd800 || c >= 0xe000) {
|
|
69
|
-
view[it.offset
|
|
70
|
-
view[it.offset
|
|
71
|
-
view[it.offset
|
|
79
|
+
view[it.offset] = 0xe0 | (c >> 12);
|
|
80
|
+
view[it.offset + 1] = 0x80 | (c >> 6 & 0x3f);
|
|
81
|
+
view[it.offset + 2] = 0x80 | (c & 0x3f);
|
|
82
|
+
it.offset += 3;
|
|
72
83
|
}
|
|
73
84
|
else {
|
|
74
85
|
i++;
|
|
75
86
|
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
|
|
76
|
-
view[it.offset
|
|
77
|
-
view[it.offset
|
|
78
|
-
view[it.offset
|
|
79
|
-
view[it.offset
|
|
87
|
+
view[it.offset] = 0xf0 | (c >> 18);
|
|
88
|
+
view[it.offset + 1] = 0x80 | (c >> 12 & 0x3f);
|
|
89
|
+
view[it.offset + 2] = 0x80 | (c >> 6 & 0x3f);
|
|
90
|
+
view[it.offset + 3] = 0x80 | (c & 0x3f);
|
|
91
|
+
it.offset += 4;
|
|
80
92
|
}
|
|
81
93
|
}
|
|
82
94
|
}
|
|
83
|
-
exports.utf8Write = utf8Write;
|
|
84
95
|
function int8(bytes, value, it) {
|
|
85
96
|
bytes[it.offset++] = value & 255;
|
|
86
97
|
}
|
|
87
|
-
exports.int8 = int8;
|
|
88
98
|
;
|
|
89
99
|
function uint8(bytes, value, it) {
|
|
90
100
|
bytes[it.offset++] = value & 255;
|
|
91
101
|
}
|
|
92
|
-
exports.uint8 = uint8;
|
|
93
102
|
;
|
|
94
103
|
function int16(bytes, value, it) {
|
|
95
104
|
bytes[it.offset++] = value & 255;
|
|
96
105
|
bytes[it.offset++] = (value >> 8) & 255;
|
|
97
106
|
}
|
|
98
|
-
exports.int16 = int16;
|
|
99
107
|
;
|
|
100
108
|
function uint16(bytes, value, it) {
|
|
101
109
|
bytes[it.offset++] = value & 255;
|
|
102
110
|
bytes[it.offset++] = (value >> 8) & 255;
|
|
103
111
|
}
|
|
104
|
-
exports.uint16 = uint16;
|
|
105
112
|
;
|
|
106
113
|
function int32(bytes, value, it) {
|
|
107
114
|
bytes[it.offset++] = value & 255;
|
|
@@ -109,7 +116,6 @@ function int32(bytes, value, it) {
|
|
|
109
116
|
bytes[it.offset++] = (value >> 16) & 255;
|
|
110
117
|
bytes[it.offset++] = (value >> 24) & 255;
|
|
111
118
|
}
|
|
112
|
-
exports.int32 = int32;
|
|
113
119
|
;
|
|
114
120
|
function uint32(bytes, value, it) {
|
|
115
121
|
const b4 = value >> 24;
|
|
@@ -121,7 +127,6 @@ function uint32(bytes, value, it) {
|
|
|
121
127
|
bytes[it.offset++] = b3 & 255;
|
|
122
128
|
bytes[it.offset++] = b4 & 255;
|
|
123
129
|
}
|
|
124
|
-
exports.uint32 = uint32;
|
|
125
130
|
;
|
|
126
131
|
function int64(bytes, value, it) {
|
|
127
132
|
const high = Math.floor(value / Math.pow(2, 32));
|
|
@@ -129,7 +134,6 @@ function int64(bytes, value, it) {
|
|
|
129
134
|
uint32(bytes, low, it);
|
|
130
135
|
uint32(bytes, high, it);
|
|
131
136
|
}
|
|
132
|
-
exports.int64 = int64;
|
|
133
137
|
;
|
|
134
138
|
function uint64(bytes, value, it) {
|
|
135
139
|
const high = (value / Math.pow(2, 32)) >> 0;
|
|
@@ -137,46 +141,36 @@ function uint64(bytes, value, it) {
|
|
|
137
141
|
uint32(bytes, low, it);
|
|
138
142
|
uint32(bytes, high, it);
|
|
139
143
|
}
|
|
140
|
-
exports.uint64 = uint64;
|
|
141
144
|
;
|
|
142
|
-
function
|
|
143
|
-
|
|
145
|
+
function bigint64(bytes, value, it) {
|
|
146
|
+
_int64[0] = BigInt.asIntN(64, value);
|
|
147
|
+
int32(bytes, _int32[0], it);
|
|
148
|
+
int32(bytes, _int32[1], it);
|
|
144
149
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
150
|
+
function biguint64(bytes, value, it) {
|
|
151
|
+
_int64[0] = BigInt.asIntN(64, value);
|
|
152
|
+
int32(bytes, _int32[0], it);
|
|
153
|
+
int32(bytes, _int32[1], it);
|
|
148
154
|
}
|
|
149
|
-
|
|
150
|
-
// force little endian to facilitate decoding on multiple implementations
|
|
151
|
-
const _isLittleEndian = true; // new Uint16Array(new Uint8Array([1, 0]).buffer)[0] === 1;
|
|
152
|
-
const _int32 = new Int32Array(2);
|
|
153
|
-
const _float32 = new Float32Array(_int32.buffer);
|
|
154
|
-
const _float64 = new Float64Array(_int32.buffer);
|
|
155
|
-
function writeFloat32(bytes, value, it) {
|
|
155
|
+
function float32(bytes, value, it) {
|
|
156
156
|
_float32[0] = value;
|
|
157
157
|
int32(bytes, _int32[0], it);
|
|
158
158
|
}
|
|
159
|
-
|
|
160
|
-
;
|
|
161
|
-
function writeFloat64(bytes, value, it) {
|
|
159
|
+
function float64(bytes, value, it) {
|
|
162
160
|
_float64[0] = value;
|
|
163
161
|
int32(bytes, _int32[_isLittleEndian ? 0 : 1], it);
|
|
164
162
|
int32(bytes, _int32[_isLittleEndian ? 1 : 0], it);
|
|
165
163
|
}
|
|
166
|
-
exports.writeFloat64 = writeFloat64;
|
|
167
|
-
;
|
|
168
164
|
function boolean(bytes, value, it) {
|
|
169
165
|
bytes[it.offset++] = value ? 1 : 0; // uint8
|
|
170
166
|
}
|
|
171
|
-
exports.boolean = boolean;
|
|
172
167
|
;
|
|
173
168
|
function string(bytes, value, it) {
|
|
174
169
|
// encode `null` strings as empty.
|
|
175
170
|
if (!value) {
|
|
176
171
|
value = "";
|
|
177
172
|
}
|
|
178
|
-
|
|
179
|
-
let length = Buffer.byteLength(value, "utf8");
|
|
173
|
+
let length = utf8Length(value, "utf8");
|
|
180
174
|
let size = 0;
|
|
181
175
|
// fixstr
|
|
182
176
|
if (length < 0x20) {
|
|
@@ -207,7 +201,6 @@ function string(bytes, value, it) {
|
|
|
207
201
|
utf8Write(bytes, value, it);
|
|
208
202
|
return size + length;
|
|
209
203
|
}
|
|
210
|
-
exports.string = string;
|
|
211
204
|
function number(bytes, value, it) {
|
|
212
205
|
if (isNaN(value)) {
|
|
213
206
|
return number(bytes, 0, it);
|
|
@@ -216,15 +209,18 @@ function number(bytes, value, it) {
|
|
|
216
209
|
return number(bytes, (value > 0) ? Number.MAX_SAFE_INTEGER : -Number.MAX_SAFE_INTEGER, it);
|
|
217
210
|
}
|
|
218
211
|
else if (value !== (value | 0)) {
|
|
212
|
+
if (Math.abs(value) <= 3.4028235e+38) { // range check
|
|
213
|
+
_float32[0] = value;
|
|
214
|
+
if (Math.abs(Math.abs(_float32[0]) - Math.abs(value)) < 1e-4) { // precision check; adjust 1e-n (n = precision) to in-/decrease acceptable precision loss
|
|
215
|
+
// now we know value is in range for f32 and has acceptable precision for f32
|
|
216
|
+
bytes[it.offset++] = 0xca;
|
|
217
|
+
float32(bytes, value, it);
|
|
218
|
+
return 5;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
219
221
|
bytes[it.offset++] = 0xcb;
|
|
220
|
-
|
|
222
|
+
float64(bytes, value, it);
|
|
221
223
|
return 9;
|
|
222
|
-
// TODO: encode float 32?
|
|
223
|
-
// is it possible to differentiate between float32 / float64 here?
|
|
224
|
-
// // float 32
|
|
225
|
-
// bytes.push(0xca);
|
|
226
|
-
// writeFloat32(bytes, value);
|
|
227
|
-
// return 5;
|
|
228
224
|
}
|
|
229
225
|
if (value >= 0) {
|
|
230
226
|
// positive fixnum
|
|
@@ -285,5 +281,23 @@ function number(bytes, value, it) {
|
|
|
285
281
|
return 9;
|
|
286
282
|
}
|
|
287
283
|
}
|
|
288
|
-
exports.
|
|
284
|
+
exports.encode = {
|
|
285
|
+
int8,
|
|
286
|
+
uint8,
|
|
287
|
+
int16,
|
|
288
|
+
uint16,
|
|
289
|
+
int32,
|
|
290
|
+
uint32,
|
|
291
|
+
int64,
|
|
292
|
+
uint64,
|
|
293
|
+
bigint64,
|
|
294
|
+
biguint64,
|
|
295
|
+
float32,
|
|
296
|
+
float64,
|
|
297
|
+
boolean,
|
|
298
|
+
string,
|
|
299
|
+
number,
|
|
300
|
+
utf8Write,
|
|
301
|
+
utf8Length,
|
|
302
|
+
};
|
|
289
303
|
//# sourceMappingURL=encode.js.map
|