@colyseus/schema 3.0.0-alpha.7 → 3.0.0-alpha.9

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
  //
@@ -3449,30 +3455,29 @@ class Encoder {
3449
3455
  //
3450
3456
  this.onEndEncode(changeTrees);
3451
3457
  }
3452
- // return bytes;
3453
- return buffer.slice(0, it.offset);
3458
+ return buffer.subarray(0, it.offset);
3454
3459
  }
3455
3460
  }
3456
3461
  encodeAll(it = { offset: 0 }, buffer = this.sharedBuffer) {
3457
- // console.log(`encodeAll(), this.$root.allChanges (${this.$root.allChanges.size})`);
3458
- // Array.from(this.$root.allChanges.entries()).map((item) => {
3462
+ // console.log(`encodeAll(), this.root.allChanges (${this.root.allChanges.size})`);
3463
+ // Array.from(this.root.allChanges.entries()).map((item) => {
3459
3464
  // console.log("->", item[0].refId, item[0].ref.toJSON());
3460
3465
  // });
3461
3466
  return this.encode(it, undefined, buffer, this.root.allChanges);
3462
3467
  }
3463
3468
  encodeAllView(view, sharedOffset, it, bytes = this.sharedBuffer) {
3464
3469
  const viewOffset = it.offset;
3465
- // console.log(`encodeAllView(), this.$root.allFilteredChanges (${this.$root.allFilteredChanges.size})`);
3470
+ // console.log(`encodeAllView(), this.root.allFilteredChanges (${this.root.allFilteredChanges.size})`);
3466
3471
  // this.debugAllFilteredChanges();
3467
3472
  // try to encode "filtered" changes
3468
3473
  this.encode(it, view, bytes, this.root.allFilteredChanges);
3469
3474
  return Buffer.concat([
3470
- bytes.slice(0, sharedOffset),
3471
- bytes.slice(viewOffset, it.offset)
3475
+ bytes.subarray(0, sharedOffset),
3476
+ bytes.subarray(viewOffset, it.offset)
3472
3477
  ]);
3473
3478
  }
3474
3479
  // debugAllFilteredChanges() {
3475
- // Array.from(this.$root.allFilteredChanges.entries()).map((item) => {
3480
+ // Array.from(this.root.allFilteredChanges.entries()).map((item) => {
3476
3481
  // console.log("->", { refId: item[0].refId }, item[0].ref.toJSON());
3477
3482
  // if (Array.isArray(item[0].ref.toJSON())) {
3478
3483
  // item[1].forEach((op, key) => {
@@ -3512,8 +3517,8 @@ class Encoder {
3512
3517
  // clear "view" changes after encoding
3513
3518
  view.changes.clear();
3514
3519
  return Buffer.concat([
3515
- bytes.slice(0, sharedOffset),
3516
- bytes.slice(viewOffset, it.offset)
3520
+ bytes.subarray(0, sharedOffset),
3521
+ bytes.subarray(viewOffset, it.offset)
3517
3522
  ]);
3518
3523
  }
3519
3524
  onEndEncode(changeTrees = this.root.changes) {