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