@colyseus/schema 3.0.0-alpha.6 → 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.
@@ -559,26 +559,29 @@ try {
559
559
  textEncoder = new TextEncoder();
560
560
  }
561
561
  catch (e) { }
562
- function utf8Length(str) {
563
- var c = 0, length = 0;
564
- for (var i = 0, l = str.length; i < l; i++) {
565
- c = str.charCodeAt(i);
566
- if (c < 0x80) {
567
- length += 1;
568
- }
569
- else if (c < 0x800) {
570
- length += 2;
571
- }
572
- else if (c < 0xd800 || c >= 0xe000) {
573
- length += 3;
574
- }
575
- else {
576
- i++;
577
- length += 4;
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
- return length;
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
- // let length = utf8Length(value);
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(this.sharedBuffer.byteLength * 2);
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
- this.sharedBuffer = Buffer.allocUnsafeSlow(newSize);
3440
- return this.encode({ offset: initialOffset }, view);
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
  //
@@ -3453,12 +3459,12 @@ class Encoder {
3453
3459
  return buffer.slice(0, it.offset);
3454
3460
  }
3455
3461
  }
3456
- encodeAll(it = { offset: 0 }) {
3462
+ encodeAll(it = { offset: 0 }, buffer = this.sharedBuffer) {
3457
3463
  // console.log(`encodeAll(), this.$root.allChanges (${this.$root.allChanges.size})`);
3458
3464
  // Array.from(this.$root.allChanges.entries()).map((item) => {
3459
3465
  // console.log("->", item[0].refId, item[0].ref.toJSON());
3460
3466
  // });
3461
- return this.encode(it, undefined, this.sharedBuffer, this.root.allChanges);
3467
+ return this.encode(it, undefined, buffer, this.root.allChanges);
3462
3468
  }
3463
3469
  encodeAllView(view, sharedOffset, it, bytes = this.sharedBuffer) {
3464
3470
  const viewOffset = it.offset;