@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.
@@ -563,26 +563,29 @@ try {
563
563
  textEncoder = new TextEncoder();
564
564
  }
565
565
  catch (e) { }
566
- function utf8Length(str) {
567
- var c = 0, length = 0;
568
- for (var i = 0, l = str.length; i < l; i++) {
569
- c = str.charCodeAt(i);
570
- if (c < 0x80) {
571
- length += 1;
572
- }
573
- else if (c < 0x800) {
574
- length += 2;
575
- }
576
- else if (c < 0xd800 || c >= 0xe000) {
577
- length += 3;
578
- }
579
- else {
580
- i++;
581
- length += 4;
566
+ const hasBufferByteLength = (typeof Buffer !== 'undefined' && Buffer.byteLength);
567
+ const utf8Length = (hasBufferByteLength)
568
+ ? Buffer.byteLength // node
569
+ : function (str, _) {
570
+ var c = 0, length = 0;
571
+ for (var i = 0, l = str.length; i < l; i++) {
572
+ c = str.charCodeAt(i);
573
+ if (c < 0x80) {
574
+ length += 1;
575
+ }
576
+ else if (c < 0x800) {
577
+ length += 2;
578
+ }
579
+ else if (c < 0xd800 || c >= 0xe000) {
580
+ length += 3;
581
+ }
582
+ else {
583
+ i++;
584
+ length += 4;
585
+ }
582
586
  }
583
- }
584
- return length;
585
- }
587
+ return length;
588
+ };
586
589
  function utf8Write(view, str, it) {
587
590
  var c = 0;
588
591
  for (var i = 0, l = str.length; i < l; i++) {
@@ -677,8 +680,7 @@ function string$1(bytes, value, it) {
677
680
  if (!value) {
678
681
  value = "";
679
682
  }
680
- // let length = utf8Length(value);
681
- let length = Buffer.byteLength(value, "utf8");
683
+ let length = utf8Length(value, "utf8");
682
684
  let size = 0;
683
685
  // fixstr
684
686
  if (length < 0x20) {
@@ -3435,13 +3437,17 @@ class Encoder {
3435
3437
  }
3436
3438
  }
3437
3439
  if (it.offset > buffer.byteLength) {
3438
- const newSize = getNextPowerOf2(this.sharedBuffer.byteLength * 2);
3440
+ const newSize = getNextPowerOf2(buffer.byteLength * 2);
3439
3441
  console.warn("@colyseus/schema encode buffer overflow. Current buffer size: " + buffer.byteLength + ", encoding offset: " + it.offset + ", new size: " + newSize);
3440
3442
  //
3441
3443
  // resize buffer and re-encode (TODO: can we avoid re-encoding here?)
3442
3444
  //
3443
- this.sharedBuffer = Buffer.allocUnsafeSlow(newSize);
3444
- return this.encode({ offset: initialOffset }, view);
3445
+ buffer = Buffer.allocUnsafeSlow(newSize);
3446
+ // assign resized buffer to local sharedBuffer
3447
+ if (buffer === this.sharedBuffer) {
3448
+ this.sharedBuffer = buffer;
3449
+ }
3450
+ return this.encode({ offset: initialOffset }, view, buffer);
3445
3451
  }
3446
3452
  else {
3447
3453
  //