@colyseus/schema 3.0.0-alpha.7 → 3.0.0-alpha.8
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 +30 -24
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +30 -24
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +30 -24
- package/lib/encoder/Encoder.js +7 -3
- package/lib/encoder/Encoder.js.map +1 -1
- package/lib/encoding/encode.d.ts +2 -1
- package/lib/encoding/encode.js +23 -22
- package/lib/encoding/encode.js.map +1 -1
- package/package.json +1 -1
- package/src/encoder/Encoder.ts +9 -3
- package/src/encoding/encode.ts +24 -21
package/build/esm/index.mjs
CHANGED
|
@@ -559,26 +559,29 @@ try {
|
|
|
559
559
|
textEncoder = new TextEncoder();
|
|
560
560
|
}
|
|
561
561
|
catch (e) { }
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
562
|
+
const hasBufferByteLength = (typeof Buffer !== 'undefined' && Buffer.byteLength);
|
|
563
|
+
const utf8Length = (hasBufferByteLength)
|
|
564
|
+
? Buffer.byteLength // node
|
|
565
|
+
: function (str, _) {
|
|
566
|
+
var c = 0, length = 0;
|
|
567
|
+
for (var i = 0, l = str.length; i < l; i++) {
|
|
568
|
+
c = str.charCodeAt(i);
|
|
569
|
+
if (c < 0x80) {
|
|
570
|
+
length += 1;
|
|
571
|
+
}
|
|
572
|
+
else if (c < 0x800) {
|
|
573
|
+
length += 2;
|
|
574
|
+
}
|
|
575
|
+
else if (c < 0xd800 || c >= 0xe000) {
|
|
576
|
+
length += 3;
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
i++;
|
|
580
|
+
length += 4;
|
|
581
|
+
}
|
|
578
582
|
}
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
}
|
|
583
|
+
return length;
|
|
584
|
+
};
|
|
582
585
|
function utf8Write(view, str, it) {
|
|
583
586
|
var c = 0;
|
|
584
587
|
for (var i = 0, l = str.length; i < l; i++) {
|
|
@@ -673,8 +676,7 @@ function string$1(bytes, value, it) {
|
|
|
673
676
|
if (!value) {
|
|
674
677
|
value = "";
|
|
675
678
|
}
|
|
676
|
-
|
|
677
|
-
let length = Buffer.byteLength(value, "utf8");
|
|
679
|
+
let length = utf8Length(value, "utf8");
|
|
678
680
|
let size = 0;
|
|
679
681
|
// fixstr
|
|
680
682
|
if (length < 0x20) {
|
|
@@ -3431,13 +3433,17 @@ class Encoder {
|
|
|
3431
3433
|
}
|
|
3432
3434
|
}
|
|
3433
3435
|
if (it.offset > buffer.byteLength) {
|
|
3434
|
-
const newSize = getNextPowerOf2(
|
|
3436
|
+
const newSize = getNextPowerOf2(buffer.byteLength * 2);
|
|
3435
3437
|
console.warn("@colyseus/schema encode buffer overflow. Current buffer size: " + buffer.byteLength + ", encoding offset: " + it.offset + ", new size: " + newSize);
|
|
3436
3438
|
//
|
|
3437
3439
|
// resize buffer and re-encode (TODO: can we avoid re-encoding here?)
|
|
3438
3440
|
//
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
+
buffer = Buffer.allocUnsafeSlow(newSize);
|
|
3442
|
+
// assign resized buffer to local sharedBuffer
|
|
3443
|
+
if (buffer === this.sharedBuffer) {
|
|
3444
|
+
this.sharedBuffer = buffer;
|
|
3445
|
+
}
|
|
3446
|
+
return this.encode({ offset: initialOffset }, view, buffer);
|
|
3441
3447
|
}
|
|
3442
3448
|
else {
|
|
3443
3449
|
//
|