@colyseus/schema 3.0.0-alpha.1 → 3.0.0-alpha.11
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/build/cjs/index.js +113 -69
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +113 -69
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +113 -69
- package/lib/Reflection.d.ts +1 -1
- package/lib/Reflection.js +1 -2
- package/lib/Reflection.js.map +1 -1
- package/lib/decoder/DecodeOperation.js +2 -2
- package/lib/decoder/DecodeOperation.js.map +1 -1
- package/lib/decoder/Decoder.d.ts +1 -1
- package/lib/decoder/Decoder.js +4 -4
- package/lib/decoder/Decoder.js.map +1 -1
- package/lib/decoder/strategy/StateCallbacks.js +1 -1
- package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
- package/lib/encoder/ChangeTree.d.ts +0 -2
- package/lib/encoder/ChangeTree.js +2 -6
- package/lib/encoder/ChangeTree.js.map +1 -1
- package/lib/encoder/EncodeOperation.js +12 -0
- package/lib/encoder/EncodeOperation.js.map +1 -1
- package/lib/encoder/Encoder.d.ts +5 -4
- package/lib/encoder/Encoder.js +62 -48
- package/lib/encoder/Encoder.js.map +1 -1
- package/lib/encoder/StateView.d.ts +2 -0
- package/lib/encoder/StateView.js +4 -1
- package/lib/encoder/StateView.js.map +1 -1
- package/lib/encoding/decode.d.ts +21 -19
- package/lib/encoding/decode.js +6 -6
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.d.ts +3 -2
- package/lib/encoding/encode.js +24 -22
- package/lib/encoding/encode.js.map +1 -1
- package/package.json +1 -1
- package/src/Reflection.ts +1 -2
- package/src/decoder/DecodeOperation.ts +3 -3
- package/src/decoder/Decoder.ts +5 -5
- package/src/decoder/strategy/StateCallbacks.ts +1 -1
- package/src/encoder/ChangeTree.ts +2 -6
- package/src/encoder/EncodeOperation.ts +13 -0
- package/src/encoder/Encoder.ts +69 -53
- package/src/encoder/StateView.ts +4 -0
- package/src/encoding/decode.ts +24 -25
- package/src/encoding/encode.ts +26 -23
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encode.js","sourceRoot":"","sources":["../../src/encoding/encode.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;;;AAOH;;;GAGG;AAEH,IAAI,WAAwB,CAAC;AAC7B,aAAa;AACb,IAAI,CAAC;IAAC,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AAAC,CAAC;AAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;AAEtD,SAAS,UAAU,CAAC,GAAG;IACrB,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC;IACtB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3C,CAAC,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QACtB,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC;YACb,MAAM,IAAI,CAAC,CAAC;QACd,CAAC;aACI,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC;YACnB,MAAM,IAAI,CAAC,CAAC;QACd,CAAC;aACI,IAAI,CAAC,GAAG,MAAM,IAAI,CAAC,IAAI,MAAM,EAAE,CAAC;YACnC,MAAM,IAAI,CAAC,CAAC;QACd,CAAC;aACI,CAAC;YACJ,CAAC,EAAE,CAAC;YACJ,MAAM,IAAI,CAAC,CAAC;QACd,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAgB,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE;IACrC,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC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H,CAAC;AAxFD,wBAwFC","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 { TextEncoder } from \"util\";\nimport type { Iterator } from \"./decode\";\n\nexport type BufferLike = number[] | ArrayBufferLike;\n\n/**\n * msgpack implementation highly based on notepack.io\n * https://github.com/darrachequesne/notepack\n */\n\nlet textEncoder: TextEncoder;\n// @ts-ignore\ntry { textEncoder = new TextEncoder(); } catch (e) { }\n\nfunction utf8Length(str) {\n var c = 0, length = 0;\n for (var i = 0, l = str.length; i < l; i++) {\n c = str.charCodeAt(i);\n if (c < 0x80) {\n length += 1;\n }\n else if (c < 0x800) {\n length += 2;\n }\n else if (c < 0xd800 || c >= 0xe000) {\n length += 3;\n }\n else {\n i++;\n length += 4;\n }\n }\n return length;\n}\n\nexport function utf8Write(view, str, it) {\n var c = 0;\n for (var i = 0, l = str.length; i < l; i++) {\n c = str.charCodeAt(i);\n if (c < 0x80) {\n view[it.offset++] = c;\n }\n else if (c < 0x800) {\n view[it.offset++] = 0xc0 | (c >> 6);\n view[it.offset++] = 0x80 | (c & 0x3f);\n }\n else if (c < 0xd800 || c >= 0xe000) {\n view[it.offset++] = 0xe0 | (c >> 12);\n view[it.offset++] = 0x80 | (c >> 6 & 0x3f);\n view[it.offset++] = 0x80 | (c & 0x3f);\n }\n else {\n i++;\n c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));\n view[it.offset++] = 0xf0 | (c >> 18);\n view[it.offset++] = 0x80 | (c >> 12 & 0x3f);\n view[it.offset++] = 0x80 | (c >> 6 & 0x3f);\n view[it.offset++] = 0x80 | (c & 0x3f);\n }\n }\n}\n\nexport function int8(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value & 255;\n};\n\nexport function uint8(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value & 255;\n};\n\nexport function int16(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value & 255;\n bytes[it.offset++] = (value >> 8) & 255;\n};\n\nexport function uint16(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value & 255;\n bytes[it.offset++] = (value >> 8) & 255;\n};\n\nexport function int32(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value & 255;\n bytes[it.offset++] = (value >> 8) & 255;\n bytes[it.offset++] = (value >> 16) & 255;\n bytes[it.offset++] = (value >> 24) & 255;\n};\n\nexport function uint32(bytes: BufferLike, value: number, it: Iterator) {\n const b4 = value >> 24;\n const b3 = value >> 16;\n const b2 = value >> 8;\n const b1 = value;\n bytes[it.offset++] = b1 & 255;\n bytes[it.offset++] = b2 & 255;\n bytes[it.offset++] = b3 & 255;\n bytes[it.offset++] = b4 & 255;\n};\n\nexport function int64(bytes: BufferLike, value: number, it: Iterator) {\n const high = Math.floor(value / Math.pow(2, 32));\n const low = value >>> 0;\n uint32(bytes, low, it);\n uint32(bytes, high, it);\n};\n\nexport function uint64(bytes: BufferLike, value: number, it: Iterator) {\n const high = (value / Math.pow(2, 32)) >> 0;\n const low = value >>> 0;\n uint32(bytes, low, it);\n uint32(bytes, high, it);\n};\n\nexport function float32(bytes: BufferLike, value: number, it: Iterator) {\n writeFloat32(bytes, value, it);\n}\n\nexport function float64(bytes: BufferLike, value: number, it: Iterator) {\n writeFloat64(bytes, value, it);\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 writeFloat32(bytes: BufferLike, value: number, it: Iterator) {\n _float32[0] = value;\n int32(bytes, _int32[0], it);\n};\n\nexport function writeFloat64(bytes: BufferLike, value: number, it: Iterator) {\n _float64[0] = value;\n int32(bytes, _int32[_isLittleEndian ? 0 : 1], it);\n int32(bytes, _int32[_isLittleEndian ? 1 : 0], it);\n};\n\nexport function boolean(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value ? 1 : 0; // uint8\n};\n\nexport function string(bytes: Buffer, value: string, it: Iterator) {\n // encode `null` strings as empty.\n if (!value) { value = \"\"; }\n\n // let length = utf8Length(value);\n let length = Buffer.byteLength(value, \"utf8\");\n let size = 0;\n\n // fixstr\n if (length < 0x20) {\n bytes[it.offset++] = length | 0xa0;\n size = 1;\n }\n // str 8\n else if (length < 0x100) {\n bytes[it.offset++] = 0xd9;\n bytes[it.offset++] = length % 255;\n size = 2;\n }\n // str 16\n else if (length < 0x10000) {\n bytes[it.offset++] = 0xda;\n uint16(bytes, length, it);\n size = 3;\n }\n // str 32\n else if (length < 0x100000000) {\n bytes[it.offset++] = 0xdb;\n uint32(bytes, length, it);\n size = 5;\n } else {\n throw new Error('String too long');\n }\n\n utf8Write(bytes, value, it);\n\n return size + length;\n}\n\nexport function number(bytes: BufferLike, value: number, it: Iterator) {\n if (isNaN(value)) {\n return number(bytes, 0, it);\n\n } else if (!isFinite(value)) {\n return number(bytes, (value > 0) ? Number.MAX_SAFE_INTEGER : -Number.MAX_SAFE_INTEGER, it);\n\n } else if (value !== (value|0)) {\n bytes[it.offset++] = 0xcb;\n writeFloat64(bytes, value, it);\n return 9;\n\n // TODO: encode float 32?\n // is it possible to differentiate between float32 / float64 here?\n\n // // float 32\n // bytes.push(0xca);\n // writeFloat32(bytes, value);\n // return 5;\n }\n\n if (value >= 0) {\n // positive fixnum\n if (value < 0x80) {\n bytes[it.offset++] = value & 255; // uint8\n return 1;\n }\n\n // uint 8\n if (value < 0x100) {\n bytes[it.offset++] = 0xcc;\n bytes[it.offset++] = value & 255; // uint8\n return 2;\n }\n\n // uint 16\n if (value < 0x10000) {\n bytes[it.offset++] = 0xcd;\n uint16(bytes, value, it);\n return 3;\n }\n\n // uint 32\n if (value < 0x100000000) {\n bytes[it.offset++] = 0xce;\n uint32(bytes, value, it);\n return 5;\n }\n\n // uint 64\n bytes[it.offset++] = 0xcf;\n uint64(bytes, value, it);\n return 9;\n\n } else {\n\n // negative fixnum\n if (value >= -0x20) {\n bytes[it.offset++] = 0xe0 | (value + 0x20);\n return 1;\n }\n\n // int 8\n if (value >= -0x80) {\n bytes[it.offset++] = 0xd0;\n int8(bytes, value, it);\n return 2;\n }\n\n // int 16\n if (value >= -0x8000) {\n bytes[it.offset++] = 0xd1;\n int16(bytes, value, it);\n return 3;\n }\n\n // int 32\n if (value >= -0x80000000) {\n bytes[it.offset++] = 0xd2;\n int32(bytes, value, it);\n return 5;\n }\n\n // int 64\n bytes[it.offset++] = 0xd3;\n int64(bytes, value, it);\n return 9;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"encode.js","sourceRoot":"","sources":["../../src/encoding/encode.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;;;AAOH;;;GAGG;AAEH,IAAI,WAAwB,CAAC;AAC7B,aAAa;AACb,IAAI,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er\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 { TextEncoder } from \"util\";\nimport type { Iterator } from \"./decode\";\n\nexport type BufferLike = number[] | ArrayBufferLike;\n\n/**\n * msgpack implementation highly based on notepack.io\n * https://github.com/darrachequesne/notepack\n */\n\nlet textEncoder: TextEncoder;\n// @ts-ignore\ntry { textEncoder = new TextEncoder(); } catch (e) { }\n\nconst hasBufferByteLength = (typeof Buffer !== 'undefined' && Buffer.byteLength);\n\nexport const utf8Length = (hasBufferByteLength)\n ? Buffer.byteLength // node\n : function (str: string, _?: any) {\n var c = 0, length = 0;\n for (var i = 0, l = str.length; i < l; i++) {\n c = str.charCodeAt(i);\n if (c < 0x80) {\n length += 1;\n }\n else if (c < 0x800) {\n length += 2;\n }\n else if (c < 0xd800 || c >= 0xe000) {\n length += 3;\n }\n else {\n i++;\n length += 4;\n }\n }\n return length;\n }\n\nexport function utf8Write(view: BufferLike, str: string, it: Iterator) {\n var c = 0;\n for (var i = 0, l = str.length; i < l; i++) {\n c = str.charCodeAt(i);\n if (c < 0x80) {\n view[it.offset++] = c;\n }\n else if (c < 0x800) {\n view[it.offset++] = 0xc0 | (c >> 6);\n view[it.offset++] = 0x80 | (c & 0x3f);\n }\n else if (c < 0xd800 || c >= 0xe000) {\n view[it.offset++] = 0xe0 | (c >> 12);\n view[it.offset++] = 0x80 | (c >> 6 & 0x3f);\n view[it.offset++] = 0x80 | (c & 0x3f);\n }\n else {\n i++;\n c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));\n view[it.offset++] = 0xf0 | (c >> 18);\n view[it.offset++] = 0x80 | (c >> 12 & 0x3f);\n view[it.offset++] = 0x80 | (c >> 6 & 0x3f);\n view[it.offset++] = 0x80 | (c & 0x3f);\n }\n }\n}\n\nexport function int8(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value & 255;\n};\n\nexport function uint8(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value & 255;\n};\n\nexport function int16(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value & 255;\n bytes[it.offset++] = (value >> 8) & 255;\n};\n\nexport function uint16(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value & 255;\n bytes[it.offset++] = (value >> 8) & 255;\n};\n\nexport function int32(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value & 255;\n bytes[it.offset++] = (value >> 8) & 255;\n bytes[it.offset++] = (value >> 16) & 255;\n bytes[it.offset++] = (value >> 24) & 255;\n};\n\nexport function uint32(bytes: BufferLike, value: number, it: Iterator) {\n const b4 = value >> 24;\n const b3 = value >> 16;\n const b2 = value >> 8;\n const b1 = value;\n bytes[it.offset++] = b1 & 255;\n bytes[it.offset++] = b2 & 255;\n bytes[it.offset++] = b3 & 255;\n bytes[it.offset++] = b4 & 255;\n};\n\nexport function int64(bytes: BufferLike, value: number, it: Iterator) {\n const high = Math.floor(value / Math.pow(2, 32));\n const low = value >>> 0;\n uint32(bytes, low, it);\n uint32(bytes, high, it);\n};\n\nexport function uint64(bytes: BufferLike, value: number, it: Iterator) {\n const high = (value / Math.pow(2, 32)) >> 0;\n const low = value >>> 0;\n uint32(bytes, low, it);\n uint32(bytes, high, it);\n};\n\nexport function float32(bytes: BufferLike, value: number, it: Iterator) {\n writeFloat32(bytes, value, it);\n}\n\nexport function float64(bytes: BufferLike, value: number, it: Iterator) {\n writeFloat64(bytes, value, it);\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 writeFloat32(bytes: BufferLike, value: number, it: Iterator) {\n _float32[0] = value;\n int32(bytes, _int32[0], it);\n};\n\nexport function writeFloat64(bytes: BufferLike, value: number, it: Iterator) {\n _float64[0] = value;\n int32(bytes, _int32[_isLittleEndian ? 0 : 1], it);\n int32(bytes, _int32[_isLittleEndian ? 1 : 0], it);\n};\n\nexport function boolean(bytes: BufferLike, value: number, it: Iterator) {\n bytes[it.offset++] = value ? 1 : 0; // uint8\n};\n\nexport function string(bytes: BufferLike, value: string, it: Iterator) {\n // encode `null` strings as empty.\n if (!value) { value = \"\"; }\n\n let length = utf8Length(value, \"utf8\");\n let size = 0;\n\n // fixstr\n if (length < 0x20) {\n bytes[it.offset++] = length | 0xa0;\n size = 1;\n }\n // str 8\n else if (length < 0x100) {\n bytes[it.offset++] = 0xd9;\n bytes[it.offset++] = length % 255;\n size = 2;\n }\n // str 16\n else if (length < 0x10000) {\n bytes[it.offset++] = 0xda;\n uint16(bytes, length, it);\n size = 3;\n }\n // str 32\n else if (length < 0x100000000) {\n bytes[it.offset++] = 0xdb;\n uint32(bytes, length, it);\n size = 5;\n } else {\n throw new Error('String too long');\n }\n\n utf8Write(bytes, value, it);\n\n return size + length;\n}\n\nexport function number(bytes: BufferLike, value: number, it: Iterator) {\n if (isNaN(value)) {\n return number(bytes, 0, it);\n\n } else if (!isFinite(value)) {\n return number(bytes, (value > 0) ? Number.MAX_SAFE_INTEGER : -Number.MAX_SAFE_INTEGER, it);\n\n } else if (value !== (value|0)) {\n bytes[it.offset++] = 0xcb;\n writeFloat64(bytes, value, it);\n return 9;\n\n // TODO: encode float 32?\n // is it possible to differentiate between float32 / float64 here?\n\n // // float 32\n // bytes.push(0xca);\n // writeFloat32(bytes, value);\n // return 5;\n }\n\n if (value >= 0) {\n // positive fixnum\n if (value < 0x80) {\n bytes[it.offset++] = value & 255; // uint8\n return 1;\n }\n\n // uint 8\n if (value < 0x100) {\n bytes[it.offset++] = 0xcc;\n bytes[it.offset++] = value & 255; // uint8\n return 2;\n }\n\n // uint 16\n if (value < 0x10000) {\n bytes[it.offset++] = 0xcd;\n uint16(bytes, value, it);\n return 3;\n }\n\n // uint 32\n if (value < 0x100000000) {\n bytes[it.offset++] = 0xce;\n uint32(bytes, value, it);\n return 5;\n }\n\n // uint 64\n bytes[it.offset++] = 0xcf;\n uint64(bytes, value, it);\n return 9;\n\n } else {\n\n // negative fixnum\n if (value >= -0x20) {\n bytes[it.offset++] = 0xe0 | (value + 0x20);\n return 1;\n }\n\n // int 8\n if (value >= -0x80) {\n bytes[it.offset++] = 0xd0;\n int8(bytes, value, it);\n return 2;\n }\n\n // int 16\n if (value >= -0x8000) {\n bytes[it.offset++] = 0xd1;\n int16(bytes, value, it);\n return 3;\n }\n\n // int 32\n if (value >= -0x80000000) {\n bytes[it.offset++] = 0xd2;\n int32(bytes, value, it);\n return 5;\n }\n\n // int 64\n bytes[it.offset++] = 0xd3;\n int64(bytes, value, it);\n return 9;\n }\n}\n"]}
|
package/package.json
CHANGED
package/src/Reflection.ts
CHANGED
|
@@ -24,7 +24,7 @@ export class ReflectionType extends Schema {
|
|
|
24
24
|
export class Reflection extends Schema {
|
|
25
25
|
@type([ ReflectionType ]) types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();
|
|
26
26
|
|
|
27
|
-
static encode
|
|
27
|
+
static encode(instance: Schema, context?: TypeContext, it: Iterator = { offset: 0 }) {
|
|
28
28
|
if (!context) {
|
|
29
29
|
context = new TypeContext(instance.constructor as typeof Schema);
|
|
30
30
|
}
|
|
@@ -96,7 +96,6 @@ export class Reflection extends Schema {
|
|
|
96
96
|
buildType(type, klass[Symbol.metadata]);
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
const it = { offset: 0 };
|
|
100
99
|
const buf = encoder.encodeAll(it);
|
|
101
100
|
return Buffer.from(buf, 0, it.offset);
|
|
102
101
|
}
|
|
@@ -4,7 +4,7 @@ import { Schema } from "../Schema";
|
|
|
4
4
|
import type { Ref } from "../encoder/ChangeTree";
|
|
5
5
|
import type { Decoder } from "./Decoder";
|
|
6
6
|
import * as decode from "../encoding/decode";
|
|
7
|
-
import { $
|
|
7
|
+
import { $childType, $deleteByIndex, $getByIndex } from "../types/symbols";
|
|
8
8
|
|
|
9
9
|
import type { MapSchema } from "../types/custom/MapSchema";
|
|
10
10
|
import type { ArraySchema } from "../types/custom/ArraySchema";
|
|
@@ -43,7 +43,7 @@ export function decodeValue(
|
|
|
43
43
|
it: decode.Iterator,
|
|
44
44
|
allChanges: DataChange[],
|
|
45
45
|
) {
|
|
46
|
-
const $root = decoder
|
|
46
|
+
const $root = decoder.root;
|
|
47
47
|
const previousValue = ref[$getByIndex](index);
|
|
48
48
|
|
|
49
49
|
let value: any;
|
|
@@ -318,7 +318,7 @@ export const decodeArray: DecodeOperation = function (
|
|
|
318
318
|
} else if (operation === OPERATION.DELETE_BY_REFID) {
|
|
319
319
|
// TODO: refactor here, try to follow same flow as below
|
|
320
320
|
const refId = decode.number(bytes, it);
|
|
321
|
-
const previousValue = decoder
|
|
321
|
+
const previousValue = decoder.root.refs.get(refId);
|
|
322
322
|
const index = ref.findIndex((value) => value === previousValue);
|
|
323
323
|
ref[$deleteByIndex](index);
|
|
324
324
|
allChanges.push({
|
package/src/decoder/Decoder.ts
CHANGED
|
@@ -14,7 +14,7 @@ export class Decoder<T extends Schema = any> {
|
|
|
14
14
|
context: TypeContext;
|
|
15
15
|
|
|
16
16
|
state: T;
|
|
17
|
-
|
|
17
|
+
root: ReferenceTracker;
|
|
18
18
|
|
|
19
19
|
currentRefId: number = 0;
|
|
20
20
|
|
|
@@ -32,8 +32,8 @@ export class Decoder<T extends Schema = any> {
|
|
|
32
32
|
|
|
33
33
|
protected setRoot(root: T) {
|
|
34
34
|
this.state = root;
|
|
35
|
-
this
|
|
36
|
-
this
|
|
35
|
+
this.root = new ReferenceTracker();
|
|
36
|
+
this.root.addRef(0, root);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
decode(
|
|
@@ -43,7 +43,7 @@ export class Decoder<T extends Schema = any> {
|
|
|
43
43
|
) {
|
|
44
44
|
const allChanges: DataChange[] = [];
|
|
45
45
|
|
|
46
|
-
const $root = this
|
|
46
|
+
const $root = this.root;
|
|
47
47
|
const totalBytes = bytes.byteLength;
|
|
48
48
|
|
|
49
49
|
let decoder: DecodeOperation = ref['constructor'][$decoder];
|
|
@@ -146,7 +146,7 @@ export class Decoder<T extends Schema = any> {
|
|
|
146
146
|
});
|
|
147
147
|
|
|
148
148
|
if (needRemoveRef) {
|
|
149
|
-
this
|
|
149
|
+
this.root.removeRef(this.root.refIds.get(value));
|
|
150
150
|
}
|
|
151
151
|
});
|
|
152
152
|
}
|
|
@@ -39,8 +39,6 @@ export class Root {
|
|
|
39
39
|
changes = new Map<ChangeTree, Map<number, OPERATION>>();
|
|
40
40
|
filteredChanges = new Map<ChangeTree, Map<number, OPERATION>>();
|
|
41
41
|
|
|
42
|
-
views: StateView[] = [];
|
|
43
|
-
|
|
44
42
|
getNextUniqueId() {
|
|
45
43
|
return this.nextUniqueId++;
|
|
46
44
|
}
|
|
@@ -172,13 +170,12 @@ export class ChangeTree<T extends Ref=any> {
|
|
|
172
170
|
|
|
173
171
|
if (!this.isFiltered) {
|
|
174
172
|
this.root.changes.set(this, this.changes);
|
|
173
|
+
this.root.allChanges.set(this, this.allChanges);
|
|
175
174
|
}
|
|
176
175
|
|
|
177
176
|
if (this.isFiltered || this.isPartiallyFiltered) {
|
|
178
177
|
this.root.filteredChanges.set(this, this.filteredChanges);
|
|
179
178
|
this.root.allFilteredChanges.set(this, this.filteredChanges);
|
|
180
|
-
} else {
|
|
181
|
-
this.root.allChanges.set(this, this.allChanges);
|
|
182
179
|
}
|
|
183
180
|
|
|
184
181
|
this.ensureRefId();
|
|
@@ -287,7 +284,6 @@ export class ChangeTree<T extends Ref=any> {
|
|
|
287
284
|
|
|
288
285
|
private _shiftAllChangeIndexes(shiftIndex: number, startIndex: number = 0, allChangeSet: Map<number, OPERATION>) {
|
|
289
286
|
Array.from(allChangeSet.entries()).forEach(([index, op]) => {
|
|
290
|
-
// console.log('shiftAllChangeIndexes', index >= startIndex, { index, op, shiftIndex, startIndex })
|
|
291
287
|
if (index >= startIndex) {
|
|
292
288
|
allChangeSet.delete(index);
|
|
293
289
|
allChangeSet.set(index + shiftIndex, op);
|
|
@@ -454,7 +450,7 @@ export class ChangeTree<T extends Ref=any> {
|
|
|
454
450
|
|
|
455
451
|
protected checkIsFiltered(parent: Ref, parentIndex: number) {
|
|
456
452
|
// Detect if current structure has "filters" declared
|
|
457
|
-
this.isPartiallyFiltered = this.ref['constructor']?.[Symbol.metadata]?.[-2];
|
|
453
|
+
this.isPartiallyFiltered = (this.ref['constructor']?.[Symbol.metadata]?.[-2] !== undefined);
|
|
458
454
|
|
|
459
455
|
// TODO: support "partially filtered", where the instance is visible, but only a field is not.
|
|
460
456
|
|
|
@@ -172,6 +172,19 @@ export const encodeKeyValueOperation: EncodeOperation = function (
|
|
|
172
172
|
const type = changeTree.getType(field);
|
|
173
173
|
const value = changeTree.getValue(field);
|
|
174
174
|
|
|
175
|
+
// try { throw new Error(); } catch (e) {
|
|
176
|
+
// // only print if not coming from Reflection.ts
|
|
177
|
+
// if (!e.stack.includes("src/Reflection.ts")) {
|
|
178
|
+
// console.log("encodeKeyValueOperation -> ", {
|
|
179
|
+
// ref: changeTree.ref.constructor.name,
|
|
180
|
+
// field,
|
|
181
|
+
// operation: OPERATION[operation],
|
|
182
|
+
// value: value?.toJSON(),
|
|
183
|
+
// items: ref.toJSON(),
|
|
184
|
+
// });
|
|
185
|
+
// }
|
|
186
|
+
// }
|
|
187
|
+
|
|
175
188
|
// TODO: inline this function call small performance gain
|
|
176
189
|
encodeValue(encoder, bytes, ref, type, value, field, operation, it);
|
|
177
190
|
}
|
package/src/encoder/Encoder.ts
CHANGED
|
@@ -8,8 +8,7 @@ import type { Iterator } from "../encoding/decode";
|
|
|
8
8
|
import { OPERATION, SWITCH_TO_STRUCTURE, TYPE_ID } from '../encoding/spec';
|
|
9
9
|
import { Root } from "./ChangeTree";
|
|
10
10
|
import { getNextPowerOf2 } from "../utils";
|
|
11
|
-
import { StateView } from "./StateView";
|
|
12
|
-
import { Metadata } from "../Metadata";
|
|
11
|
+
import type { StateView } from "./StateView";
|
|
13
12
|
|
|
14
13
|
export class Encoder<T extends Schema = any> {
|
|
15
14
|
static BUFFER_SIZE = 8 * 1024;// 8KB
|
|
@@ -18,7 +17,7 @@ export class Encoder<T extends Schema = any> {
|
|
|
18
17
|
context: TypeContext;
|
|
19
18
|
state: T;
|
|
20
19
|
|
|
21
|
-
|
|
20
|
+
root: Root;
|
|
22
21
|
|
|
23
22
|
constructor(root: T) {
|
|
24
23
|
this.setRoot(root);
|
|
@@ -36,20 +35,20 @@ export class Encoder<T extends Schema = any> {
|
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
protected setRoot(state: T) {
|
|
39
|
-
this
|
|
38
|
+
this.root = new Root();
|
|
40
39
|
this.state = state;
|
|
41
|
-
state[$changes].setRoot(this
|
|
40
|
+
state[$changes].setRoot(this.root);
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
encode(
|
|
45
44
|
it: Iterator = { offset: 0 },
|
|
46
45
|
view?: StateView,
|
|
47
|
-
|
|
48
|
-
changeTrees = this
|
|
46
|
+
buffer = this.sharedBuffer,
|
|
47
|
+
changeTrees = this.root.changes
|
|
49
48
|
): Buffer {
|
|
50
49
|
const initialOffset = it.offset; // cache current offset in case we need to resize the buffer
|
|
51
50
|
|
|
52
|
-
const isEncodeAll = this
|
|
51
|
+
const isEncodeAll = this.root.allChanges === changeTrees;
|
|
53
52
|
const hasView = (view !== undefined);
|
|
54
53
|
const rootChangeTree = this.state[$changes];
|
|
55
54
|
|
|
@@ -62,6 +61,13 @@ export class Encoder<T extends Schema = any> {
|
|
|
62
61
|
const encoder = ctor[$encoder];
|
|
63
62
|
const filter = ctor[$filter];
|
|
64
63
|
|
|
64
|
+
// try { throw new Error(); } catch (e) {
|
|
65
|
+
// // only print if not coming from Reflection.ts
|
|
66
|
+
// if (!e.stack.includes("src/Reflection.ts")) {
|
|
67
|
+
// console.log("ChangeTree:", { ref: ref.constructor.name, });
|
|
68
|
+
// }
|
|
69
|
+
// }
|
|
70
|
+
|
|
65
71
|
if (hasView) {
|
|
66
72
|
if (!view.items.has(changeTree)) {
|
|
67
73
|
view.invisible.add(changeTree);
|
|
@@ -74,8 +80,8 @@ export class Encoder<T extends Schema = any> {
|
|
|
74
80
|
|
|
75
81
|
// skip root `refId` if it's the first change tree
|
|
76
82
|
if (it.offset !== initialOffset || changeTree !== rootChangeTree) {
|
|
77
|
-
|
|
78
|
-
encode.number(
|
|
83
|
+
buffer[it.offset++] = SWITCH_TO_STRUCTURE & 255;
|
|
84
|
+
encode.number(buffer, changeTree.refId, it);
|
|
79
85
|
}
|
|
80
86
|
|
|
81
87
|
const changesIterator = changes.entries();
|
|
@@ -96,25 +102,36 @@ export class Encoder<T extends Schema = any> {
|
|
|
96
102
|
continue;
|
|
97
103
|
}
|
|
98
104
|
|
|
99
|
-
//
|
|
100
|
-
//
|
|
101
|
-
//
|
|
102
|
-
//
|
|
103
|
-
//
|
|
104
|
-
|
|
105
|
-
|
|
105
|
+
// try { throw new Error(); } catch (e) {
|
|
106
|
+
// // only print if not coming from Reflection.ts
|
|
107
|
+
// if (!e.stack.includes("src/Reflection.ts")) {
|
|
108
|
+
// // console.log("WILL ENCODE", {
|
|
109
|
+
// // ref: changeTree.ref.constructor.name,
|
|
110
|
+
// // fieldIndex,
|
|
111
|
+
// // operation: OPERATION[operation],
|
|
112
|
+
// // });
|
|
113
|
+
// }
|
|
114
|
+
// }
|
|
115
|
+
|
|
116
|
+
encoder(this, buffer, changeTree, fieldIndex, operation, it, isEncodeAll, hasView);
|
|
106
117
|
}
|
|
107
118
|
}
|
|
108
119
|
|
|
109
|
-
if (it.offset >
|
|
110
|
-
const newSize = getNextPowerOf2(
|
|
111
|
-
console.warn("@colyseus/schema encode buffer overflow. Current buffer size: " +
|
|
120
|
+
if (it.offset > buffer.byteLength) {
|
|
121
|
+
const newSize = getNextPowerOf2(buffer.byteLength * 2);
|
|
122
|
+
console.warn("@colyseus/schema encode buffer overflow. Current buffer size: " + buffer.byteLength + ", encoding offset: " + it.offset + ", new size: " + newSize);
|
|
112
123
|
|
|
113
124
|
//
|
|
114
125
|
// resize buffer and re-encode (TODO: can we avoid re-encoding here?)
|
|
115
126
|
//
|
|
116
|
-
|
|
117
|
-
|
|
127
|
+
buffer = Buffer.allocUnsafeSlow(newSize);
|
|
128
|
+
|
|
129
|
+
// assign resized buffer to local sharedBuffer
|
|
130
|
+
if (buffer === this.sharedBuffer) {
|
|
131
|
+
this.sharedBuffer = buffer;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return this.encode({ offset: initialOffset }, view, buffer);
|
|
118
135
|
|
|
119
136
|
} else {
|
|
120
137
|
//
|
|
@@ -127,53 +144,52 @@ export class Encoder<T extends Schema = any> {
|
|
|
127
144
|
this.onEndEncode(changeTrees);
|
|
128
145
|
}
|
|
129
146
|
|
|
130
|
-
|
|
131
|
-
return bytes.slice(0, it.offset);
|
|
147
|
+
return buffer.subarray(0, it.offset);
|
|
132
148
|
}
|
|
133
149
|
}
|
|
134
150
|
|
|
135
|
-
encodeAll(it: Iterator = { offset: 0 }) {
|
|
136
|
-
// console.log(`encodeAll(), this
|
|
151
|
+
encodeAll(it: Iterator = { offset: 0 }, buffer: Buffer = this.sharedBuffer) {
|
|
152
|
+
// console.log(`encodeAll(), this.root.allChanges (${this.root.allChanges.size})`);
|
|
137
153
|
|
|
138
|
-
// Array.from(this
|
|
139
|
-
// console.log("->", item[0].
|
|
154
|
+
// Array.from(this.root.allChanges.entries()).map((item) => {
|
|
155
|
+
// console.log("->", { ref: item[0].ref.constructor.name, refId: item[0].refId, changes: item[1].size });
|
|
140
156
|
// });
|
|
141
157
|
|
|
142
|
-
return this.encode(it, undefined,
|
|
158
|
+
return this.encode(it, undefined, buffer, this.root.allChanges);
|
|
143
159
|
}
|
|
144
160
|
|
|
145
161
|
encodeAllView(view: StateView, sharedOffset: number, it: Iterator, bytes = this.sharedBuffer) {
|
|
146
162
|
const viewOffset = it.offset;
|
|
147
163
|
|
|
148
|
-
// console.log(`encodeAllView(), this
|
|
164
|
+
// console.log(`encodeAllView(), this.root.allFilteredChanges (${this.root.allFilteredChanges.size})`);
|
|
149
165
|
// this.debugAllFilteredChanges();
|
|
150
166
|
|
|
151
167
|
// try to encode "filtered" changes
|
|
152
|
-
this.encode(it, view, bytes, this
|
|
168
|
+
this.encode(it, view, bytes, this.root.allFilteredChanges);
|
|
153
169
|
|
|
154
170
|
return Buffer.concat([
|
|
155
|
-
bytes.
|
|
156
|
-
bytes.
|
|
171
|
+
bytes.subarray(0, sharedOffset),
|
|
172
|
+
bytes.subarray(viewOffset, it.offset)
|
|
157
173
|
]);
|
|
158
174
|
}
|
|
159
175
|
|
|
160
176
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
177
|
+
debugAllFilteredChanges() {
|
|
178
|
+
Array.from(this.root.allFilteredChanges.entries()).map((item) => {
|
|
179
|
+
console.log("->", { refId: item[0].refId, changes: item[1].size }, item[0].ref.toJSON());
|
|
180
|
+
if (Array.isArray(item[0].ref.toJSON())) {
|
|
181
|
+
item[1].forEach((op, key) => {
|
|
182
|
+
console.log(" ->", { key, op: OPERATION[op] });
|
|
183
|
+
})
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}
|
|
171
187
|
|
|
172
188
|
encodeView(view: StateView, sharedOffset: number, it: Iterator, bytes = this.sharedBuffer) {
|
|
173
189
|
const viewOffset = it.offset;
|
|
174
190
|
|
|
175
191
|
// try to encode "filtered" changes
|
|
176
|
-
this.encode(it, view, bytes, this
|
|
192
|
+
this.encode(it, view, bytes, this.root.filteredChanges);
|
|
177
193
|
|
|
178
194
|
// encode visibility changes (add/remove for this view)
|
|
179
195
|
const viewChangesIterator = view.changes.entries();
|
|
@@ -209,12 +225,12 @@ export class Encoder<T extends Schema = any> {
|
|
|
209
225
|
view.changes.clear();
|
|
210
226
|
|
|
211
227
|
return Buffer.concat([
|
|
212
|
-
bytes.
|
|
213
|
-
bytes.
|
|
228
|
+
bytes.subarray(0, sharedOffset),
|
|
229
|
+
bytes.subarray(viewOffset, it.offset)
|
|
214
230
|
]);
|
|
215
231
|
}
|
|
216
232
|
|
|
217
|
-
onEndEncode(changeTrees = this
|
|
233
|
+
onEndEncode(changeTrees = this.root.changes) {
|
|
218
234
|
const changeTreesIterator = changeTrees.entries();
|
|
219
235
|
for (const [changeTree, _] of changeTreesIterator) {
|
|
220
236
|
changeTree.endEncode();
|
|
@@ -223,14 +239,14 @@ export class Encoder<T extends Schema = any> {
|
|
|
223
239
|
|
|
224
240
|
discardChanges() {
|
|
225
241
|
// discard shared changes
|
|
226
|
-
if (this
|
|
227
|
-
this.onEndEncode(this
|
|
228
|
-
this
|
|
242
|
+
if (this.root.changes.size > 0) {
|
|
243
|
+
this.onEndEncode(this.root.changes);
|
|
244
|
+
this.root.changes.clear();
|
|
229
245
|
}
|
|
230
246
|
// discard filtered changes
|
|
231
|
-
if (this
|
|
232
|
-
this.onEndEncode(this
|
|
233
|
-
this
|
|
247
|
+
if (this.root.filteredChanges.size > 0) {
|
|
248
|
+
this.onEndEncode(this.root.filteredChanges);
|
|
249
|
+
this.root.filteredChanges.clear();
|
|
234
250
|
}
|
|
235
251
|
}
|
|
236
252
|
|
package/src/encoder/StateView.ts
CHANGED
|
@@ -3,6 +3,10 @@ import { $changes } from "../types/symbols";
|
|
|
3
3
|
import { DEFAULT_VIEW_TAG } from "../annotations";
|
|
4
4
|
import { OPERATION } from "../encoding/spec";
|
|
5
5
|
import { Metadata } from "../Metadata";
|
|
6
|
+
import type { Schema } from "../Schema";
|
|
7
|
+
|
|
8
|
+
export function createView(root: Schema) {
|
|
9
|
+
}
|
|
6
10
|
|
|
7
11
|
export class StateView {
|
|
8
12
|
/**
|
package/src/encoding/decode.ts
CHANGED
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import { SWITCH_TO_STRUCTURE } from "./spec";
|
|
25
|
+
import type { BufferLike } from "./encode";
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
28
|
* msgpack implementation highly based on notepack.io
|
|
@@ -30,9 +31,9 @@ import { SWITCH_TO_STRUCTURE } from "./spec";
|
|
|
30
31
|
|
|
31
32
|
export interface Iterator { offset: number; }
|
|
32
33
|
|
|
33
|
-
function utf8Read(bytes,
|
|
34
|
+
export function utf8Read(bytes: BufferLike, it: Iterator, length: number) {
|
|
34
35
|
var string = '', chr = 0;
|
|
35
|
-
for (var i = offset, end = offset + length; i < end; i++) {
|
|
36
|
+
for (var i = it.offset, end = it.offset + length; i < end; i++) {
|
|
36
37
|
var byte = bytes[i];
|
|
37
38
|
if ((byte & 0x80) === 0x00) {
|
|
38
39
|
string += String.fromCharCode(byte);
|
|
@@ -71,48 +72,49 @@ function utf8Read(bytes, offset, length) {
|
|
|
71
72
|
// (do not throw error to avoid server/client from crashing due to hack attemps)
|
|
72
73
|
// throw new Error('Invalid byte ' + byte.toString(16));
|
|
73
74
|
}
|
|
75
|
+
it.offset += length;
|
|
74
76
|
return string;
|
|
75
77
|
}
|
|
76
78
|
|
|
77
|
-
export function int8 (bytes:
|
|
79
|
+
export function int8 (bytes: BufferLike, it: Iterator) {
|
|
78
80
|
return uint8(bytes, it) << 24 >> 24;
|
|
79
81
|
};
|
|
80
82
|
|
|
81
|
-
export function uint8 (bytes:
|
|
83
|
+
export function uint8 (bytes: BufferLike, it: Iterator) {
|
|
82
84
|
return bytes[it.offset++];
|
|
83
85
|
};
|
|
84
86
|
|
|
85
|
-
export function int16 (bytes:
|
|
87
|
+
export function int16 (bytes: BufferLike, it: Iterator) {
|
|
86
88
|
return uint16(bytes, it) << 16 >> 16;
|
|
87
89
|
};
|
|
88
90
|
|
|
89
|
-
export function uint16 (bytes:
|
|
91
|
+
export function uint16 (bytes: BufferLike, it: Iterator) {
|
|
90
92
|
return bytes[it.offset++] | bytes[it.offset++] << 8;
|
|
91
93
|
};
|
|
92
94
|
|
|
93
|
-
export function int32 (bytes:
|
|
95
|
+
export function int32 (bytes: BufferLike, it: Iterator) {
|
|
94
96
|
return bytes[it.offset++] | bytes[it.offset++] << 8 | bytes[it.offset++] << 16 | bytes[it.offset++] << 24;
|
|
95
97
|
};
|
|
96
98
|
|
|
97
|
-
export function uint32 (bytes:
|
|
99
|
+
export function uint32 (bytes: BufferLike, it: Iterator) {
|
|
98
100
|
return int32(bytes, it) >>> 0;
|
|
99
101
|
};
|
|
100
102
|
|
|
101
|
-
export function float32(bytes:
|
|
103
|
+
export function float32(bytes: BufferLike, it: Iterator) {
|
|
102
104
|
return readFloat32(bytes, it);
|
|
103
105
|
}
|
|
104
106
|
|
|
105
|
-
export function float64(bytes:
|
|
107
|
+
export function float64(bytes: BufferLike, it: Iterator) {
|
|
106
108
|
return readFloat64(bytes, it);
|
|
107
109
|
}
|
|
108
110
|
|
|
109
|
-
export function int64(bytes:
|
|
111
|
+
export function int64(bytes: BufferLike, it: Iterator) {
|
|
110
112
|
const low = uint32(bytes, it);
|
|
111
113
|
const high = int32(bytes, it) * Math.pow(2, 32);
|
|
112
114
|
return high + low;
|
|
113
115
|
};
|
|
114
116
|
|
|
115
|
-
export function uint64(bytes:
|
|
117
|
+
export function uint64(bytes: BufferLike, it: Iterator) {
|
|
116
118
|
const low = uint32(bytes, it);
|
|
117
119
|
const high = uint32(bytes, it) * Math.pow(2, 32);
|
|
118
120
|
return high + low;
|
|
@@ -124,22 +126,22 @@ const _int32 = new Int32Array(2);
|
|
|
124
126
|
const _float32 = new Float32Array(_int32.buffer);
|
|
125
127
|
const _float64 = new Float64Array(_int32.buffer);
|
|
126
128
|
|
|
127
|
-
export function readFloat32 (bytes:
|
|
129
|
+
export function readFloat32 (bytes: BufferLike, it: Iterator) {
|
|
128
130
|
_int32[0] = int32(bytes, it);
|
|
129
131
|
return _float32[0];
|
|
130
132
|
};
|
|
131
133
|
|
|
132
|
-
export function readFloat64 (bytes:
|
|
134
|
+
export function readFloat64 (bytes: BufferLike, it: Iterator) {
|
|
133
135
|
_int32[_isLittleEndian ? 0 : 1] = int32(bytes, it);
|
|
134
136
|
_int32[_isLittleEndian ? 1 : 0] = int32(bytes, it);
|
|
135
137
|
return _float64[0];
|
|
136
138
|
};
|
|
137
139
|
|
|
138
|
-
export function boolean (bytes:
|
|
140
|
+
export function boolean (bytes: BufferLike, it: Iterator) {
|
|
139
141
|
return uint8(bytes, it) > 0;
|
|
140
142
|
};
|
|
141
143
|
|
|
142
|
-
export function string (bytes, it: Iterator) {
|
|
144
|
+
export function string (bytes: BufferLike, it: Iterator) {
|
|
143
145
|
const prefix = bytes[it.offset++];
|
|
144
146
|
let length: number;
|
|
145
147
|
|
|
@@ -157,13 +159,10 @@ export function string (bytes, it: Iterator) {
|
|
|
157
159
|
length = uint32(bytes, it);
|
|
158
160
|
}
|
|
159
161
|
|
|
160
|
-
|
|
161
|
-
it.offset += length;
|
|
162
|
-
|
|
163
|
-
return value;
|
|
162
|
+
return utf8Read(bytes, it, length);
|
|
164
163
|
}
|
|
165
164
|
|
|
166
|
-
export function stringCheck(bytes, it: Iterator) {
|
|
165
|
+
export function stringCheck(bytes: BufferLike, it: Iterator) {
|
|
167
166
|
const prefix = bytes[it.offset];
|
|
168
167
|
return (
|
|
169
168
|
// fixstr
|
|
@@ -177,7 +176,7 @@ export function stringCheck(bytes, it: Iterator) {
|
|
|
177
176
|
);
|
|
178
177
|
}
|
|
179
178
|
|
|
180
|
-
export function number (bytes, it: Iterator) {
|
|
179
|
+
export function number (bytes: BufferLike, it: Iterator) {
|
|
181
180
|
const prefix = bytes[it.offset++];
|
|
182
181
|
|
|
183
182
|
if (prefix < 0x80) {
|
|
@@ -230,7 +229,7 @@ export function number (bytes, it: Iterator) {
|
|
|
230
229
|
}
|
|
231
230
|
};
|
|
232
231
|
|
|
233
|
-
export function numberCheck (bytes, it: Iterator) {
|
|
232
|
+
export function numberCheck (bytes: BufferLike, it: Iterator) {
|
|
234
233
|
const prefix = bytes[it.offset];
|
|
235
234
|
// positive fixint - 0x00 - 0x7f
|
|
236
235
|
// float 32 - 0xca
|
|
@@ -249,7 +248,7 @@ export function numberCheck (bytes, it: Iterator) {
|
|
|
249
248
|
);
|
|
250
249
|
}
|
|
251
250
|
|
|
252
|
-
export function arrayCheck (bytes, it: Iterator) {
|
|
251
|
+
export function arrayCheck (bytes: BufferLike, it: Iterator) {
|
|
253
252
|
return bytes[it.offset] < 0xa0;
|
|
254
253
|
|
|
255
254
|
// const prefix = bytes[it.offset] ;
|
|
@@ -268,7 +267,7 @@ export function arrayCheck (bytes, it: Iterator) {
|
|
|
268
267
|
// return prefix;
|
|
269
268
|
}
|
|
270
269
|
|
|
271
|
-
export function switchStructureCheck(bytes, it: Iterator) {
|
|
270
|
+
export function switchStructureCheck(bytes: BufferLike, it: Iterator) {
|
|
272
271
|
return (
|
|
273
272
|
// previous byte should be `SWITCH_TO_STRUCTURE`
|
|
274
273
|
bytes[it.offset - 1] === SWITCH_TO_STRUCTURE &&
|