@colyseus/schema 3.0.0-alpha.9 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/README.md +148 -62
  2. package/bin/schema-debug +94 -0
  3. package/build/cjs/index.js +2222 -1513
  4. package/build/cjs/index.js.map +1 -1
  5. package/build/esm/index.mjs +2223 -1516
  6. package/build/esm/index.mjs.map +1 -1
  7. package/build/umd/index.js +2225 -1516
  8. package/lib/Metadata.d.ts +21 -9
  9. package/lib/Metadata.js +169 -32
  10. package/lib/Metadata.js.map +1 -1
  11. package/lib/Reflection.d.ts +19 -4
  12. package/lib/Reflection.js +66 -31
  13. package/lib/Reflection.js.map +1 -1
  14. package/lib/Schema.d.ts +12 -5
  15. package/lib/Schema.js +57 -56
  16. package/lib/Schema.js.map +1 -1
  17. package/lib/annotations.d.ts +31 -34
  18. package/lib/annotations.js +110 -160
  19. package/lib/annotations.js.map +1 -1
  20. package/lib/bench_encode.d.ts +1 -0
  21. package/lib/bench_encode.js +130 -0
  22. package/lib/bench_encode.js.map +1 -0
  23. package/lib/codegen/api.js +1 -2
  24. package/lib/codegen/api.js.map +1 -1
  25. package/lib/codegen/languages/cpp.js +1 -2
  26. package/lib/codegen/languages/cpp.js.map +1 -1
  27. package/lib/codegen/languages/csharp.js +9 -46
  28. package/lib/codegen/languages/csharp.js.map +1 -1
  29. package/lib/codegen/languages/haxe.js +4 -2
  30. package/lib/codegen/languages/haxe.js.map +1 -1
  31. package/lib/codegen/languages/java.js +1 -2
  32. package/lib/codegen/languages/java.js.map +1 -1
  33. package/lib/codegen/languages/js.js +1 -2
  34. package/lib/codegen/languages/js.js.map +1 -1
  35. package/lib/codegen/languages/lua.js +23 -25
  36. package/lib/codegen/languages/lua.js.map +1 -1
  37. package/lib/codegen/languages/ts.js +1 -2
  38. package/lib/codegen/languages/ts.js.map +1 -1
  39. package/lib/codegen/parser.js +85 -3
  40. package/lib/codegen/parser.js.map +1 -1
  41. package/lib/codegen/types.js +6 -3
  42. package/lib/codegen/types.js.map +1 -1
  43. package/lib/debug.d.ts +1 -0
  44. package/lib/debug.js +51 -0
  45. package/lib/debug.js.map +1 -0
  46. package/lib/decoder/DecodeOperation.d.ts +3 -4
  47. package/lib/decoder/DecodeOperation.js +35 -17
  48. package/lib/decoder/DecodeOperation.js.map +1 -1
  49. package/lib/decoder/Decoder.d.ts +5 -6
  50. package/lib/decoder/Decoder.js +10 -10
  51. package/lib/decoder/Decoder.js.map +1 -1
  52. package/lib/decoder/ReferenceTracker.js +4 -2
  53. package/lib/decoder/ReferenceTracker.js.map +1 -1
  54. package/lib/decoder/strategy/RawChanges.js +1 -2
  55. package/lib/decoder/strategy/RawChanges.js.map +1 -1
  56. package/lib/decoder/strategy/StateCallbacks.d.ts +44 -11
  57. package/lib/decoder/strategy/StateCallbacks.js +74 -64
  58. package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
  59. package/lib/encoder/ChangeTree.d.ts +28 -20
  60. package/lib/encoder/ChangeTree.js +242 -188
  61. package/lib/encoder/ChangeTree.js.map +1 -1
  62. package/lib/encoder/EncodeOperation.d.ts +3 -6
  63. package/lib/encoder/EncodeOperation.js +51 -65
  64. package/lib/encoder/EncodeOperation.js.map +1 -1
  65. package/lib/encoder/Encoder.d.ts +8 -7
  66. package/lib/encoder/Encoder.js +128 -79
  67. package/lib/encoder/Encoder.js.map +1 -1
  68. package/lib/encoder/Root.d.ts +22 -0
  69. package/lib/encoder/Root.js +81 -0
  70. package/lib/encoder/Root.js.map +1 -0
  71. package/lib/encoder/StateView.d.ts +7 -7
  72. package/lib/encoder/StateView.js +72 -74
  73. package/lib/encoder/StateView.js.map +1 -1
  74. package/lib/encoding/assert.d.ts +7 -6
  75. package/lib/encoding/assert.js +13 -5
  76. package/lib/encoding/assert.js.map +1 -1
  77. package/lib/encoding/decode.d.ts +36 -19
  78. package/lib/encoding/decode.js +54 -84
  79. package/lib/encoding/decode.js.map +1 -1
  80. package/lib/encoding/encode.d.ts +36 -18
  81. package/lib/encoding/encode.js +61 -48
  82. package/lib/encoding/encode.js.map +1 -1
  83. package/lib/encoding/spec.d.ts +4 -5
  84. package/lib/encoding/spec.js +1 -2
  85. package/lib/encoding/spec.js.map +1 -1
  86. package/lib/index.d.ts +10 -9
  87. package/lib/index.js +24 -17
  88. package/lib/index.js.map +1 -1
  89. package/lib/types/HelperTypes.d.ts +34 -2
  90. package/lib/types/HelperTypes.js.map +1 -1
  91. package/lib/types/TypeContext.d.ts +29 -0
  92. package/lib/types/TypeContext.js +151 -0
  93. package/lib/types/TypeContext.js.map +1 -0
  94. package/lib/types/custom/ArraySchema.d.ts +2 -2
  95. package/lib/types/custom/ArraySchema.js +33 -22
  96. package/lib/types/custom/ArraySchema.js.map +1 -1
  97. package/lib/types/custom/CollectionSchema.d.ts +2 -2
  98. package/lib/types/custom/CollectionSchema.js +1 -0
  99. package/lib/types/custom/CollectionSchema.js.map +1 -1
  100. package/lib/types/custom/MapSchema.d.ts +18 -16
  101. package/lib/types/custom/MapSchema.js +12 -4
  102. package/lib/types/custom/MapSchema.js.map +1 -1
  103. package/lib/types/custom/SetSchema.d.ts +2 -2
  104. package/lib/types/custom/SetSchema.js +1 -0
  105. package/lib/types/custom/SetSchema.js.map +1 -1
  106. package/lib/types/registry.d.ts +8 -1
  107. package/lib/types/registry.js +23 -6
  108. package/lib/types/registry.js.map +1 -1
  109. package/lib/types/symbols.d.ts +8 -5
  110. package/lib/types/symbols.js +9 -6
  111. package/lib/types/symbols.js.map +1 -1
  112. package/lib/types/utils.js +1 -2
  113. package/lib/types/utils.js.map +1 -1
  114. package/lib/utils.js +9 -7
  115. package/lib/utils.js.map +1 -1
  116. package/package.json +19 -18
  117. package/src/Metadata.ts +190 -42
  118. package/src/Reflection.ts +76 -38
  119. package/src/Schema.ts +72 -70
  120. package/src/annotations.ts +156 -202
  121. package/src/bench_encode.ts +108 -0
  122. package/src/codegen/languages/csharp.ts +8 -47
  123. package/src/codegen/languages/haxe.ts +4 -0
  124. package/src/codegen/languages/lua.ts +19 -27
  125. package/src/codegen/parser.ts +107 -0
  126. package/src/codegen/types.ts +1 -0
  127. package/src/debug.ts +55 -0
  128. package/src/decoder/DecodeOperation.ts +43 -15
  129. package/src/decoder/Decoder.ts +12 -10
  130. package/src/decoder/ReferenceTracker.ts +5 -3
  131. package/src/decoder/strategy/StateCallbacks.ts +152 -81
  132. package/src/encoder/ChangeTree.ts +282 -209
  133. package/src/encoder/EncodeOperation.ts +78 -78
  134. package/src/encoder/Encoder.ts +152 -87
  135. package/src/encoder/Root.ts +93 -0
  136. package/src/encoder/StateView.ts +80 -88
  137. package/src/encoding/assert.ts +17 -8
  138. package/src/encoding/decode.ts +73 -93
  139. package/src/encoding/encode.ts +76 -45
  140. package/src/encoding/spec.ts +3 -5
  141. package/src/index.ts +12 -20
  142. package/src/types/HelperTypes.ts +54 -2
  143. package/src/types/TypeContext.ts +175 -0
  144. package/src/types/custom/ArraySchema.ts +49 -19
  145. package/src/types/custom/CollectionSchema.ts +1 -0
  146. package/src/types/custom/MapSchema.ts +30 -17
  147. package/src/types/custom/SetSchema.ts +1 -0
  148. package/src/types/registry.ts +22 -3
  149. package/src/types/symbols.ts +10 -7
  150. package/src/utils.ts +7 -3
@@ -22,8 +22,16 @@
22
22
  * SOFTWARE
23
23
  */
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.switchStructureCheck = exports.arrayCheck = exports.numberCheck = exports.number = exports.stringCheck = exports.string = exports.boolean = exports.readFloat64 = exports.readFloat32 = exports.uint64 = exports.int64 = exports.float64 = exports.float32 = exports.uint32 = exports.int32 = exports.uint16 = exports.int16 = exports.uint8 = exports.int8 = exports.utf8Read = void 0;
26
- const spec_1 = require("./spec");
25
+ exports.decode = void 0;
26
+ exports.stringCheck = stringCheck;
27
+ // force little endian to facilitate decoding on multiple implementations
28
+ const _isLittleEndian = true; // new Uint16Array(new Uint8Array([1, 0]).buffer)[0] === 1;
29
+ const _convoBuffer = new ArrayBuffer(8);
30
+ const _int32 = new Int32Array(_convoBuffer);
31
+ const _float32 = new Float32Array(_convoBuffer);
32
+ const _float64 = new Float64Array(_convoBuffer);
33
+ const _uint64 = new BigUint64Array(_convoBuffer);
34
+ const _int64 = new BigInt64Array(_convoBuffer);
27
35
  function utf8Read(bytes, it, length) {
28
36
  var string = '', chr = 0;
29
37
  for (var i = it.offset, end = it.offset + length; i < end; i++) {
@@ -64,81 +72,66 @@ function utf8Read(bytes, it, length) {
64
72
  it.offset += length;
65
73
  return string;
66
74
  }
67
- exports.utf8Read = utf8Read;
68
75
  function int8(bytes, it) {
69
76
  return uint8(bytes, it) << 24 >> 24;
70
77
  }
71
- exports.int8 = int8;
72
78
  ;
73
79
  function uint8(bytes, it) {
74
80
  return bytes[it.offset++];
75
81
  }
76
- exports.uint8 = uint8;
77
82
  ;
78
83
  function int16(bytes, it) {
79
84
  return uint16(bytes, it) << 16 >> 16;
80
85
  }
81
- exports.int16 = int16;
82
86
  ;
83
87
  function uint16(bytes, it) {
84
88
  return bytes[it.offset++] | bytes[it.offset++] << 8;
85
89
  }
86
- exports.uint16 = uint16;
87
90
  ;
88
91
  function int32(bytes, it) {
89
92
  return bytes[it.offset++] | bytes[it.offset++] << 8 | bytes[it.offset++] << 16 | bytes[it.offset++] << 24;
90
93
  }
91
- exports.int32 = int32;
92
94
  ;
93
95
  function uint32(bytes, it) {
94
96
  return int32(bytes, it) >>> 0;
95
97
  }
96
- exports.uint32 = uint32;
97
98
  ;
98
99
  function float32(bytes, it) {
99
- return readFloat32(bytes, it);
100
+ _int32[0] = int32(bytes, it);
101
+ return _float32[0];
100
102
  }
101
- exports.float32 = float32;
103
+ ;
102
104
  function float64(bytes, it) {
103
- return readFloat64(bytes, it);
105
+ _int32[_isLittleEndian ? 0 : 1] = int32(bytes, it);
106
+ _int32[_isLittleEndian ? 1 : 0] = int32(bytes, it);
107
+ return _float64[0];
104
108
  }
105
- exports.float64 = float64;
109
+ ;
106
110
  function int64(bytes, it) {
107
111
  const low = uint32(bytes, it);
108
112
  const high = int32(bytes, it) * Math.pow(2, 32);
109
113
  return high + low;
110
114
  }
111
- exports.int64 = int64;
112
115
  ;
113
116
  function uint64(bytes, it) {
114
117
  const low = uint32(bytes, it);
115
118
  const high = uint32(bytes, it) * Math.pow(2, 32);
116
119
  return high + low;
117
120
  }
118
- exports.uint64 = uint64;
119
121
  ;
120
- // force little endian to facilitate decoding on multiple implementations
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) {
122
+ function bigint64(bytes, it) {
126
123
  _int32[0] = int32(bytes, it);
127
- return _float32[0];
124
+ _int32[1] = int32(bytes, it);
125
+ return _int64[0];
128
126
  }
129
- exports.readFloat32 = readFloat32;
130
- ;
131
- function readFloat64(bytes, it) {
132
- _int32[_isLittleEndian ? 0 : 1] = int32(bytes, it);
133
- _int32[_isLittleEndian ? 1 : 0] = int32(bytes, it);
134
- return _float64[0];
127
+ function biguint64(bytes, it) {
128
+ _int32[0] = int32(bytes, it);
129
+ _int32[1] = int32(bytes, it);
130
+ return _uint64[0];
135
131
  }
136
- exports.readFloat64 = readFloat64;
137
- ;
138
132
  function boolean(bytes, it) {
139
133
  return uint8(bytes, it) > 0;
140
134
  }
141
- exports.boolean = boolean;
142
135
  ;
143
136
  function string(bytes, it) {
144
137
  const prefix = bytes[it.offset++];
@@ -158,20 +151,6 @@ function string(bytes, it) {
158
151
  }
159
152
  return utf8Read(bytes, it, length);
160
153
  }
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
154
  function number(bytes, it) {
176
155
  const prefix = bytes[it.offset++];
177
156
  if (prefix < 0x80) {
@@ -180,11 +159,11 @@ function number(bytes, it) {
180
159
  }
181
160
  else if (prefix === 0xca) {
182
161
  // float 32
183
- return readFloat32(bytes, it);
162
+ return float32(bytes, it);
184
163
  }
185
164
  else if (prefix === 0xcb) {
186
165
  // float 64
187
- return readFloat64(bytes, it);
166
+ return float64(bytes, it);
188
167
  }
189
168
  else if (prefix === 0xcc) {
190
169
  // uint 8
@@ -223,45 +202,36 @@ function number(bytes, it) {
223
202
  return (0xff - prefix + 1) * -1;
224
203
  }
225
204
  }
226
- exports.number = number;
227
205
  ;
228
- function numberCheck(bytes, it) {
206
+ function stringCheck(bytes, it) {
229
207
  const prefix = bytes[it.offset];
230
- // positive fixint - 0x00 - 0x7f
231
- // float 32 - 0xca
232
- // float 64 - 0xcb
233
- // uint 8 - 0xcc
234
- // uint 16 - 0xcd
235
- // uint 32 - 0xce
236
- // uint 64 - 0xcf
237
- // int 8 - 0xd0
238
- // int 16 - 0xd1
239
- // int 32 - 0xd2
240
- // int 64 - 0xd3
241
- return (prefix < 0x80 ||
242
- (prefix >= 0xca && prefix <= 0xd3));
243
- }
244
- exports.numberCheck = numberCheck;
245
- function arrayCheck(bytes, it) {
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
208
  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)));
209
+ // fixstr
210
+ (prefix < 0xc0 && prefix > 0xa0) ||
211
+ // str 8
212
+ prefix === 0xd9 ||
213
+ // str 16
214
+ prefix === 0xda ||
215
+ // str 32
216
+ prefix === 0xdb);
265
217
  }
266
- exports.switchStructureCheck = switchStructureCheck;
218
+ exports.decode = {
219
+ utf8Read,
220
+ int8,
221
+ uint8,
222
+ int16,
223
+ uint16,
224
+ int32,
225
+ uint32,
226
+ float32,
227
+ float64,
228
+ int64,
229
+ uint64,
230
+ bigint64,
231
+ biguint64,
232
+ boolean,
233
+ string,
234
+ number,
235
+ stringCheck,
236
+ };
267
237
  //# 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,IAAI,EAAE,CAAC;YAC3B,MAAM,IAAI,MAAM,CAAC,YAAY,CAC3B,CAAC,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;gBACpB,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,C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,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;;;AA2MH,kCAYC;AA5MD,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,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,I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**\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 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 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 stringCheck,\n};"]}
@@ -20,23 +20,41 @@
20
20
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  * SOFTWARE
22
22
  */
23
- /// <reference types="node" />
24
23
  import type { Iterator } from "./decode";
25
24
  export type BufferLike = number[] | ArrayBufferLike;
26
- export declare const utf8Length: typeof Buffer.byteLength;
27
- export declare function utf8Write(view: BufferLike, str: string, it: Iterator): void;
28
- export declare function int8(bytes: BufferLike, value: number, it: Iterator): void;
29
- export declare function uint8(bytes: BufferLike, value: number, it: Iterator): void;
30
- export declare function int16(bytes: BufferLike, value: number, it: Iterator): void;
31
- export declare function uint16(bytes: BufferLike, value: number, it: Iterator): void;
32
- export declare function int32(bytes: BufferLike, value: number, it: Iterator): void;
33
- export declare function uint32(bytes: BufferLike, value: number, it: Iterator): void;
34
- export declare function int64(bytes: BufferLike, value: number, it: Iterator): void;
35
- export declare function uint64(bytes: BufferLike, value: number, it: Iterator): void;
36
- export declare function float32(bytes: BufferLike, value: number, it: Iterator): void;
37
- export declare function float64(bytes: BufferLike, value: number, it: Iterator): void;
38
- export declare function writeFloat32(bytes: BufferLike, value: number, it: Iterator): void;
39
- export declare function writeFloat64(bytes: BufferLike, value: number, it: Iterator): void;
40
- export declare function boolean(bytes: BufferLike, value: number, it: Iterator): void;
41
- export declare function string(bytes: BufferLike, value: string, it: Iterator): number;
42
- export declare function number(bytes: BufferLike, value: number, it: Iterator): 1 | 2 | 3 | 5 | 9;
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 {};
@@ -22,7 +22,7 @@
22
22
  * SOFTWARE
23
23
  */
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.number = exports.string = exports.boolean = exports.writeFloat64 = exports.writeFloat32 = exports.float64 = exports.float32 = exports.uint64 = exports.int64 = exports.uint32 = exports.int32 = exports.uint16 = exports.int16 = exports.uint8 = exports.int8 = exports.utf8Write = exports.utf8Length = void 0;
25
+ exports.encode = void 0;
26
26
  /**
27
27
  * msgpack implementation highly based on notepack.io
28
28
  * https://github.com/darrachequesne/notepack
@@ -33,8 +33,15 @@ try {
33
33
  textEncoder = new TextEncoder();
34
34
  }
35
35
  catch (e) { }
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);
36
43
  const hasBufferByteLength = (typeof Buffer !== 'undefined' && Buffer.byteLength);
37
- exports.utf8Length = (hasBufferByteLength)
44
+ const utf8Length = (hasBufferByteLength)
38
45
  ? Buffer.byteLength // node
39
46
  : function (str, _) {
40
47
  var c = 0, length = 0;
@@ -64,46 +71,44 @@ function utf8Write(view, str, it) {
64
71
  view[it.offset++] = c;
65
72
  }
66
73
  else if (c < 0x800) {
67
- view[it.offset++] = 0xc0 | (c >> 6);
68
- view[it.offset++] = 0x80 | (c & 0x3f);
74
+ view[it.offset] = 0xc0 | (c >> 6);
75
+ view[it.offset + 1] = 0x80 | (c & 0x3f);
76
+ it.offset += 2;
69
77
  }
70
78
  else if (c < 0xd800 || c >= 0xe000) {
71
- view[it.offset++] = 0xe0 | (c >> 12);
72
- view[it.offset++] = 0x80 | (c >> 6 & 0x3f);
73
- view[it.offset++] = 0x80 | (c & 0x3f);
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;
74
83
  }
75
84
  else {
76
85
  i++;
77
86
  c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
78
- view[it.offset++] = 0xf0 | (c >> 18);
79
- view[it.offset++] = 0x80 | (c >> 12 & 0x3f);
80
- view[it.offset++] = 0x80 | (c >> 6 & 0x3f);
81
- view[it.offset++] = 0x80 | (c & 0x3f);
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;
82
92
  }
83
93
  }
84
94
  }
85
- exports.utf8Write = utf8Write;
86
95
  function int8(bytes, value, it) {
87
96
  bytes[it.offset++] = value & 255;
88
97
  }
89
- exports.int8 = int8;
90
98
  ;
91
99
  function uint8(bytes, value, it) {
92
100
  bytes[it.offset++] = value & 255;
93
101
  }
94
- exports.uint8 = uint8;
95
102
  ;
96
103
  function int16(bytes, value, it) {
97
104
  bytes[it.offset++] = value & 255;
98
105
  bytes[it.offset++] = (value >> 8) & 255;
99
106
  }
100
- exports.int16 = int16;
101
107
  ;
102
108
  function uint16(bytes, value, it) {
103
109
  bytes[it.offset++] = value & 255;
104
110
  bytes[it.offset++] = (value >> 8) & 255;
105
111
  }
106
- exports.uint16 = uint16;
107
112
  ;
108
113
  function int32(bytes, value, it) {
109
114
  bytes[it.offset++] = value & 255;
@@ -111,7 +116,6 @@ function int32(bytes, value, it) {
111
116
  bytes[it.offset++] = (value >> 16) & 255;
112
117
  bytes[it.offset++] = (value >> 24) & 255;
113
118
  }
114
- exports.int32 = int32;
115
119
  ;
116
120
  function uint32(bytes, value, it) {
117
121
  const b4 = value >> 24;
@@ -123,7 +127,6 @@ function uint32(bytes, value, it) {
123
127
  bytes[it.offset++] = b3 & 255;
124
128
  bytes[it.offset++] = b4 & 255;
125
129
  }
126
- exports.uint32 = uint32;
127
130
  ;
128
131
  function int64(bytes, value, it) {
129
132
  const high = Math.floor(value / Math.pow(2, 32));
@@ -131,7 +134,6 @@ function int64(bytes, value, it) {
131
134
  uint32(bytes, low, it);
132
135
  uint32(bytes, high, it);
133
136
  }
134
- exports.int64 = int64;
135
137
  ;
136
138
  function uint64(bytes, value, it) {
137
139
  const high = (value / Math.pow(2, 32)) >> 0;
@@ -139,45 +141,36 @@ function uint64(bytes, value, it) {
139
141
  uint32(bytes, low, it);
140
142
  uint32(bytes, high, it);
141
143
  }
142
- exports.uint64 = uint64;
143
144
  ;
144
- function float32(bytes, value, it) {
145
- writeFloat32(bytes, value, it);
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);
146
149
  }
147
- exports.float32 = float32;
148
- function float64(bytes, value, it) {
149
- writeFloat64(bytes, value, it);
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);
150
154
  }
151
- exports.float64 = float64;
152
- // force little endian to facilitate decoding on multiple implementations
153
- const _isLittleEndian = true; // new Uint16Array(new Uint8Array([1, 0]).buffer)[0] === 1;
154
- const _int32 = new Int32Array(2);
155
- const _float32 = new Float32Array(_int32.buffer);
156
- const _float64 = new Float64Array(_int32.buffer);
157
- function writeFloat32(bytes, value, it) {
155
+ function float32(bytes, value, it) {
158
156
  _float32[0] = value;
159
157
  int32(bytes, _int32[0], it);
160
158
  }
161
- exports.writeFloat32 = writeFloat32;
162
- ;
163
- function writeFloat64(bytes, value, it) {
159
+ function float64(bytes, value, it) {
164
160
  _float64[0] = value;
165
161
  int32(bytes, _int32[_isLittleEndian ? 0 : 1], it);
166
162
  int32(bytes, _int32[_isLittleEndian ? 1 : 0], it);
167
163
  }
168
- exports.writeFloat64 = writeFloat64;
169
- ;
170
164
  function boolean(bytes, value, it) {
171
165
  bytes[it.offset++] = value ? 1 : 0; // uint8
172
166
  }
173
- exports.boolean = boolean;
174
167
  ;
175
168
  function string(bytes, value, it) {
176
169
  // encode `null` strings as empty.
177
170
  if (!value) {
178
171
  value = "";
179
172
  }
180
- let length = (0, exports.utf8Length)(value, "utf8");
173
+ let length = utf8Length(value, "utf8");
181
174
  let size = 0;
182
175
  // fixstr
183
176
  if (length < 0x20) {
@@ -208,7 +201,6 @@ function string(bytes, value, it) {
208
201
  utf8Write(bytes, value, it);
209
202
  return size + length;
210
203
  }
211
- exports.string = string;
212
204
  function number(bytes, value, it) {
213
205
  if (isNaN(value)) {
214
206
  return number(bytes, 0, it);
@@ -217,15 +209,18 @@ function number(bytes, value, it) {
217
209
  return number(bytes, (value > 0) ? Number.MAX_SAFE_INTEGER : -Number.MAX_SAFE_INTEGER, it);
218
210
  }
219
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
+ }
220
221
  bytes[it.offset++] = 0xcb;
221
- writeFloat64(bytes, value, it);
222
+ float64(bytes, value, it);
222
223
  return 9;
223
- // TODO: encode float 32?
224
- // is it possible to differentiate between float32 / float64 here?
225
- // // float 32
226
- // bytes.push(0xca);
227
- // writeFloat32(bytes, value);
228
- // return 5;
229
224
  }
230
225
  if (value >= 0) {
231
226
  // positive fixnum
@@ -286,5 +281,23 @@ function number(bytes, value, it) {
286
281
  return 9;
287
282
  }
288
283
  }
289
- exports.number = number;
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
+ };
290
303
  //# sourceMappingURL=encode.js.map