@netifyjs/netify 1.0.0-alpha.4 → 1.0.0-alpha.5

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.
Files changed (72) hide show
  1. package/README.md +6 -6
  2. package/package.json +1 -1
  3. package/src/bootstrap/client/index.ts +1 -1
  4. package/src/bootstrap/dispatcher/index.ts +7 -0
  5. package/src/bootstrap/reconnect/index.ts +14 -4
  6. package/src/bootstrap/server/index.ts +10 -4
  7. package/src/buffer/index.ts +229 -88
  8. package/src/buffer/quantizer/index.ts +16 -3
  9. package/src/channel/context/index.ts +18 -11
  10. package/src/channel/handler/index.ts +1 -1
  11. package/src/channel/index.ts +30 -7
  12. package/src/channel/outbound/index.ts +28 -12
  13. package/src/channel/pipeline/index.ts +45 -5
  14. package/src/channel/queue/index.ts +3 -2
  15. package/src/channel/resolved/index.ts +13 -3
  16. package/src/channel/server/index.ts +19 -10
  17. package/src/cluster/index.ts +73 -32
  18. package/src/codec/binary/index.ts +19 -0
  19. package/src/codec/decoder/index.ts +223 -83
  20. package/src/codec/delimiter/index.ts +95 -37
  21. package/src/codec/encoder/index.ts +30 -5
  22. package/src/codec/fixed/index.ts +5 -1
  23. package/src/codec/http/aggregator/index.ts +45 -16
  24. package/src/codec/http/decoder/object/index.ts +156 -40
  25. package/src/codec/http/decoder/request/index.ts +6 -2
  26. package/src/codec/http/encoder/object/index.ts +2 -7
  27. package/src/codec/http/encoder/request/index.ts +5 -0
  28. package/src/codec/http/headers/index.ts +45 -2
  29. package/src/codec/http/message/full/request/index.ts +1 -1
  30. package/src/codec/http/message/full/response/index.ts +1 -1
  31. package/src/codec/http/message/request/index.ts +14 -1
  32. package/src/codec/http/message/response/index.ts +1 -1
  33. package/src/codec/http/version/index.ts +3 -0
  34. package/src/codec/length/index.ts +66 -44
  35. package/src/codec/message/decoder/index.ts +18 -1
  36. package/src/codec/message/encoder/index.ts +85 -8
  37. package/src/codec/prepender/index.ts +4 -1
  38. package/src/codec/string/decoder/index.ts +6 -1
  39. package/src/codec/websocket/decoder/index.ts +78 -3
  40. package/src/codec/websocket/encoder/index.ts +90 -6
  41. package/src/codec/websocket/frame/base/index.ts +19 -10
  42. package/src/codec/websocket/frame/text/index.ts +2 -2
  43. package/src/codec/websocket/protocol/index.ts +12 -0
  44. package/src/datagram/bootstrap/index.ts +1 -1
  45. package/src/datagram/transport/index.ts +30 -1
  46. package/src/handler/flush/index.ts +25 -5
  47. package/src/handler/idle/index.ts +120 -26
  48. package/src/handler/logging/index.ts +13 -1
  49. package/src/handler/metrics/index.ts +49 -16
  50. package/src/handler/offload/index.ts +7 -0
  51. package/src/handler/timeout/read/index.ts +1 -1
  52. package/src/handler/timeout/write/index.ts +104 -23
  53. package/src/handler/traffic/index.ts +251 -38
  54. package/src/index.ts +1 -0
  55. package/src/reliable/cipher/index.ts +49 -5
  56. package/src/reliable/cookie/index.ts +64 -2
  57. package/src/reliable/handler/index.ts +11 -15
  58. package/src/reliable/index.ts +7 -1
  59. package/src/reliable/lane/index.ts +277 -58
  60. package/src/reliable/peer/index.ts +125 -38
  61. package/src/serve/bootstrap/index.ts +82 -135
  62. package/src/serve/channel/index.ts +84 -2
  63. package/src/serve/gate/index.ts +215 -0
  64. package/src/serve/index.ts +6 -2
  65. package/src/serve/outbox/index.ts +63 -2
  66. package/src/serve/transport/index.ts +33 -3
  67. package/src/serve/writer/index.ts +4 -3
  68. package/src/shard/gateway/index.ts +102 -29
  69. package/src/shard/link/index.ts +15 -0
  70. package/src/worker/group/index.ts +231 -13
  71. package/src/worker/pool/index.ts +100 -19
  72. package/src/worker/service/index.ts +9 -1
@@ -47,6 +47,11 @@ export class ByteBuf {
47
47
  */
48
48
  public static readonly defaultMaxCapacity: number = 64 * 1024 * 1024
49
49
 
50
+ /**
51
+ * Longest shared-array search done by hand
52
+ */
53
+ private static readonly scanAt: number = 64
54
+
50
55
  /**
51
56
  * One encoder for every buffer, they're stateless
52
57
  */
@@ -55,7 +60,12 @@ export class ByteBuf {
55
60
  /**
56
61
  * One decoder for every buffer, only used for whole strings
57
62
  */
58
- private static readonly decoder: TextDecoder = new TextDecoder()
63
+ private static readonly decoder: TextDecoder = new TextDecoder('utf-8', {
64
+ /**
65
+ * Keep a leading BOM
66
+ */
67
+ ignoreBOM: true,
68
+ })
59
69
 
60
70
  /**
61
71
  * Underlying bytes, swapped for a bigger array when we grow
@@ -389,7 +399,7 @@ export class ByteBuf {
389
399
 
390
400
  /**
391
401
  * Quick check before a read so decoders can wait for more data
392
- * @param size bytes you need, 1 if left out
402
+ * @param size bytes you need
393
403
  */
394
404
  public isReadable(size = 1): boolean {
395
405
  return this.readableBytes >= size
@@ -508,11 +518,7 @@ export class ByteBuf {
508
518
  * @param endian byte order, big by default
509
519
  */
510
520
  public getUint16(index: number, endian: Endian = 'big'): number {
511
- const start = this.checkIndex(index, 2)
512
-
513
- const first = this.storage[start] ?? 0
514
- const second = this.storage[start + 1] ?? 0
515
- return endian === 'big' ? (first << 8) | second : (second << 8) | first
521
+ return this.loadUint16(this.checkIndex(index, 2), endian)
516
522
  }
517
523
 
518
524
  /**
@@ -521,14 +527,7 @@ export class ByteBuf {
521
527
  * @param endian big or little
522
528
  */
523
529
  public getUint24(index: number, endian: Endian = 'big'): number {
524
- const start = this.checkIndex(index, 3)
525
-
526
- const first = this.storage[start] ?? 0
527
- const middle = this.storage[start + 1] ?? 0
528
- const last = this.storage[start + 2] ?? 0
529
- return endian === 'big'
530
- ? (first << 16) | (middle << 8) | last
531
- : (last << 16) | (middle << 8) | first
530
+ return this.loadUint24(this.checkIndex(index, 3), endian)
532
531
  }
533
532
 
534
533
  /**
@@ -546,20 +545,7 @@ export class ByteBuf {
546
545
  * @param endian big or little
547
546
  */
548
547
  public getUint32(index: number, endian: Endian = 'big'): number {
549
- const start = this.checkIndex(index, 4)
550
-
551
- const bytes = this.storage
552
- const value =
553
- endian === 'big'
554
- ? ((bytes[start] ?? 0) << 24) |
555
- ((bytes[start + 1] ?? 0) << 16) |
556
- ((bytes[start + 2] ?? 0) << 8) |
557
- (bytes[start + 3] ?? 0)
558
- : ((bytes[start + 3] ?? 0) << 24) |
559
- ((bytes[start + 2] ?? 0) << 16) |
560
- ((bytes[start + 1] ?? 0) << 8) |
561
- (bytes[start] ?? 0)
562
- return value >>> 0
548
+ return this.loadUint32(this.checkIndex(index, 4), endian)
563
549
  }
564
550
 
565
551
  /**
@@ -638,12 +624,7 @@ export class ByteBuf {
638
624
  * @param endian big or little
639
625
  */
640
626
  public setUint16(index: number, value: number, endian: Endian = 'big'): this {
641
- const start = this.checkIndex(index, 2)
642
-
643
- const high = (value >>> 8) & 0xff
644
- const low = value & 0xff
645
- this.storage[start] = endian === 'big' ? high : low
646
- this.storage[start + 1] = endian === 'big' ? low : high
627
+ this.storeUint16(this.checkIndex(index, 2), value, endian)
647
628
  return this
648
629
  }
649
630
 
@@ -654,14 +635,7 @@ export class ByteBuf {
654
635
  * @param endian big by default
655
636
  */
656
637
  public setUint24(index: number, value: number, endian: Endian = 'big'): this {
657
- const start = this.checkIndex(index, 3)
658
-
659
- const high = (value >>> 16) & 0xff
660
- const middle = (value >>> 8) & 0xff
661
- const low = value & 0xff
662
- this.storage[start] = endian === 'big' ? high : low
663
- this.storage[start + 1] = middle
664
- this.storage[start + 2] = endian === 'big' ? low : high
638
+ this.storeUint24(this.checkIndex(index, 3), value, endian)
665
639
  return this
666
640
  }
667
641
 
@@ -682,18 +656,7 @@ export class ByteBuf {
682
656
  * @param endian big by default
683
657
  */
684
658
  public setUint32(index: number, value: number, endian: Endian = 'big'): this {
685
- const start = this.checkIndex(index, 4)
686
-
687
- const bytes = this.storage
688
- const first = (value >>> 24) & 0xff
689
- const second = (value >>> 16) & 0xff
690
- const third = (value >>> 8) & 0xff
691
- const fourth = value & 0xff
692
- const big = endian === 'big'
693
- bytes[start] = big ? first : fourth
694
- bytes[start + 1] = big ? second : third
695
- bytes[start + 2] = big ? third : second
696
- bytes[start + 3] = big ? fourth : first
659
+ this.storeUint32(this.checkIndex(index, 4), value, endian)
697
660
  return this
698
661
  }
699
662
 
@@ -757,14 +720,14 @@ export class ByteBuf {
757
720
  * Takes one byte and moves on, throws if nothing's readable
758
721
  */
759
722
  public readInt8(): number {
760
- return this.getInt8(this.advanceReader(1))
723
+ return ((this.storage[this.base + this.advanceReader(1)] ?? 0) << 24) >> 24
761
724
  }
762
725
 
763
726
  /**
764
727
  * Takes one unsigned byte and moves on
765
728
  */
766
729
  public readUint8(): number {
767
- return this.getUint8(this.advanceReader(1))
730
+ return this.storage[this.base + this.advanceReader(1)] ?? 0
768
731
  }
769
732
 
770
733
  /**
@@ -772,7 +735,9 @@ export class ByteBuf {
772
735
  * @param endian big by default
773
736
  */
774
737
  public readInt16(endian: Endian = 'big'): number {
775
- return this.getInt16(this.advanceReader(2), endian)
738
+ const start = this.base + this.advanceReader(2)
739
+
740
+ return (this.loadUint16(start, endian) << 16) >> 16
776
741
  }
777
742
 
778
743
  /**
@@ -780,7 +745,7 @@ export class ByteBuf {
780
745
  * @param endian big or little
781
746
  */
782
747
  public readUint16(endian: Endian = 'big'): number {
783
- return this.getUint16(this.advanceReader(2), endian)
748
+ return this.loadUint16(this.base + this.advanceReader(2), endian)
784
749
  }
785
750
 
786
751
  /**
@@ -788,7 +753,7 @@ export class ByteBuf {
788
753
  * @param endian big by default
789
754
  */
790
755
  public readUint24(endian: Endian = 'big'): number {
791
- return this.getUint24(this.advanceReader(3), endian)
756
+ return this.loadUint24(this.base + this.advanceReader(3), endian)
792
757
  }
793
758
 
794
759
  /**
@@ -796,7 +761,7 @@ export class ByteBuf {
796
761
  * @param endian big or little
797
762
  */
798
763
  public readInt32(endian: Endian = 'big'): number {
799
- return this.getInt32(this.advanceReader(4), endian)
764
+ return this.loadUint32(this.base + this.advanceReader(4), endian) | 0
800
765
  }
801
766
 
802
767
  /**
@@ -804,7 +769,7 @@ export class ByteBuf {
804
769
  * @param endian big by default
805
770
  */
806
771
  public readUint32(endian: Endian = 'big'): number {
807
- return this.getUint32(this.advanceReader(4), endian)
772
+ return this.loadUint32(this.base + this.advanceReader(4), endian)
808
773
  }
809
774
 
810
775
  /**
@@ -812,7 +777,9 @@ export class ByteBuf {
812
777
  * @param endian big or little
813
778
  */
814
779
  public readInt64(endian: Endian = 'big'): bigint {
815
- return this.getInt64(this.advanceReader(8), endian)
780
+ const start = this.base + this.advanceReader(8)
781
+
782
+ return this.data.getBigInt64(start, endian === 'little')
816
783
  }
817
784
 
818
785
  /**
@@ -820,7 +787,9 @@ export class ByteBuf {
820
787
  * @param endian big by default
821
788
  */
822
789
  public readUint64(endian: Endian = 'big'): bigint {
823
- return this.getUint64(this.advanceReader(8), endian)
790
+ const start = this.base + this.advanceReader(8)
791
+
792
+ return this.data.getBigUint64(start, endian === 'little')
824
793
  }
825
794
 
826
795
  /**
@@ -828,7 +797,9 @@ export class ByteBuf {
828
797
  * @param endian big or little
829
798
  */
830
799
  public readFloat32(endian: Endian = 'big'): number {
831
- return this.getFloat32(this.advanceReader(4), endian)
800
+ const start = this.base + this.advanceReader(4)
801
+
802
+ return this.data.getFloat32(start, endian === 'little')
832
803
  }
833
804
 
834
805
  /**
@@ -836,7 +807,9 @@ export class ByteBuf {
836
807
  * @param endian big by default
837
808
  */
838
809
  public readFloat64(endian: Endian = 'big'): number {
839
- return this.getFloat64(this.advanceReader(8), endian)
810
+ const start = this.base + this.advanceReader(8)
811
+
812
+ return this.data.getFloat64(start, endian === 'little')
840
813
  }
841
814
 
842
815
  /**
@@ -933,6 +906,10 @@ export class ByteBuf {
933
906
  for (let count = 0; count < 8; count++) {
934
907
  if (index >= end) throw new RangeError('Index is out of range')
935
908
  const byte = storage[index++] ?? 0
909
+ if (count === 7 && byte > 0x0f && byte < 0x80) {
910
+ throw new RangeError('Varint is too large')
911
+ }
912
+
936
913
  value += (byte & 0x7f) * scale
937
914
  if (byte < 0x80) {
938
915
  this.readerPosition = index - base
@@ -1001,7 +978,10 @@ export class ByteBuf {
1001
978
  * @param value low 8 bits are kept
1002
979
  */
1003
980
  public writeInt8(value: number): this {
1004
- return this.setInt8(this.advanceWriter(1), value)
981
+ const start = this.advanceWriter(1)
982
+
983
+ this.storage[this.base + start] = value & 0xff
984
+ return this
1005
985
  }
1006
986
 
1007
987
  /**
@@ -1009,7 +989,10 @@ export class ByteBuf {
1009
989
  * @param value 0 to 255, anything else is masked
1010
990
  */
1011
991
  public writeUint8(value: number): this {
1012
- return this.setUint8(this.advanceWriter(1), value)
992
+ const start = this.advanceWriter(1)
993
+
994
+ this.storage[this.base + start] = value & 0xff
995
+ return this
1013
996
  }
1014
997
 
1015
998
  /**
@@ -1018,7 +1001,10 @@ export class ByteBuf {
1018
1001
  * @param endian big by default
1019
1002
  */
1020
1003
  public writeInt16(value: number, endian: Endian = 'big'): this {
1021
- return this.setInt16(this.advanceWriter(2), value, endian)
1004
+ const start = this.advanceWriter(2)
1005
+
1006
+ this.storeUint16(this.base + start, value, endian)
1007
+ return this
1022
1008
  }
1023
1009
 
1024
1010
  /**
@@ -1027,7 +1013,10 @@ export class ByteBuf {
1027
1013
  * @param endian big or little
1028
1014
  */
1029
1015
  public writeUint16(value: number, endian: Endian = 'big'): this {
1030
- return this.setUint16(this.advanceWriter(2), value, endian)
1016
+ const start = this.advanceWriter(2)
1017
+
1018
+ this.storeUint16(this.base + start, value, endian)
1019
+ return this
1031
1020
  }
1032
1021
 
1033
1022
  /**
@@ -1036,7 +1025,10 @@ export class ByteBuf {
1036
1025
  * @param endian big by default
1037
1026
  */
1038
1027
  public writeUint24(value: number, endian: Endian = 'big'): this {
1039
- return this.setUint24(this.advanceWriter(3), value, endian)
1028
+ const start = this.advanceWriter(3)
1029
+
1030
+ this.storeUint24(this.base + start, value, endian)
1031
+ return this
1040
1032
  }
1041
1033
 
1042
1034
  /**
@@ -1045,7 +1037,10 @@ export class ByteBuf {
1045
1037
  * @param endian big or little
1046
1038
  */
1047
1039
  public writeInt32(value: number, endian: Endian = 'big'): this {
1048
- return this.setInt32(this.advanceWriter(4), value, endian)
1040
+ const start = this.advanceWriter(4)
1041
+
1042
+ this.storeUint32(this.base + start, value, endian)
1043
+ return this
1049
1044
  }
1050
1045
 
1051
1046
  /**
@@ -1054,7 +1049,10 @@ export class ByteBuf {
1054
1049
  * @param endian big by default
1055
1050
  */
1056
1051
  public writeUint32(value: number, endian: Endian = 'big'): this {
1057
- return this.setUint32(this.advanceWriter(4), value, endian)
1052
+ const start = this.advanceWriter(4)
1053
+
1054
+ this.storeUint32(this.base + start, value, endian)
1055
+ return this
1058
1056
  }
1059
1057
 
1060
1058
  /**
@@ -1063,7 +1061,10 @@ export class ByteBuf {
1063
1061
  * @param endian big or little
1064
1062
  */
1065
1063
  public writeInt64(value: bigint, endian: Endian = 'big'): this {
1066
- return this.setInt64(this.advanceWriter(8), value, endian)
1064
+ const start = this.advanceWriter(8)
1065
+
1066
+ this.data.setBigInt64(this.base + start, value, endian === 'little')
1067
+ return this
1067
1068
  }
1068
1069
 
1069
1070
  /**
@@ -1072,7 +1073,10 @@ export class ByteBuf {
1072
1073
  * @param endian big by default
1073
1074
  */
1074
1075
  public writeUint64(value: bigint, endian: Endian = 'big'): this {
1075
- return this.setUint64(this.advanceWriter(8), value, endian)
1076
+ const start = this.advanceWriter(8)
1077
+
1078
+ this.data.setBigUint64(this.base + start, value, endian === 'little')
1079
+ return this
1076
1080
  }
1077
1081
 
1078
1082
  /**
@@ -1081,7 +1085,10 @@ export class ByteBuf {
1081
1085
  * @param endian big or little
1082
1086
  */
1083
1087
  public writeFloat32(value: number, endian: Endian = 'big'): this {
1084
- return this.setFloat32(this.advanceWriter(4), value, endian)
1088
+ const start = this.advanceWriter(4)
1089
+
1090
+ this.data.setFloat32(this.base + start, value, endian === 'little')
1091
+ return this
1085
1092
  }
1086
1093
 
1087
1094
  /**
@@ -1090,7 +1097,10 @@ export class ByteBuf {
1090
1097
  * @param endian big by default
1091
1098
  */
1092
1099
  public writeFloat64(value: number, endian: Endian = 'big'): this {
1093
- return this.setFloat64(this.advanceWriter(8), value, endian)
1100
+ const start = this.advanceWriter(8)
1101
+
1102
+ this.data.setFloat64(this.base + start, value, endian === 'little')
1103
+ return this
1094
1104
  }
1095
1105
 
1096
1106
  /**
@@ -1122,15 +1132,27 @@ export class ByteBuf {
1122
1132
  if (index !== undefined) {
1123
1133
  const size = length ?? 0
1124
1134
  const from = source.checkIndex(index, size)
1125
- const to = this.base + this.advanceWriter(size)
1126
- ByteBuf.copyBytes(source.storage, from, this.storage, to, size)
1135
+ const to = this.advanceWriter(size)
1136
+ ByteBuf.copyBytes(
1137
+ source.storage,
1138
+ from,
1139
+ this.storage,
1140
+ this.base + to,
1141
+ size,
1142
+ )
1127
1143
  return this
1128
1144
  }
1129
1145
 
1130
1146
  const size = source.readableBytes
1131
1147
  const from = source.base + source.advanceReader(size)
1132
- const to = this.base + this.advanceWriter(size)
1133
- ByteBuf.copyBytes(source.storage, from, this.storage, to, size)
1148
+ const to = this.advanceWriter(size)
1149
+ ByteBuf.copyBytes(
1150
+ source.storage,
1151
+ from,
1152
+ this.storage,
1153
+ this.base + to,
1154
+ size,
1155
+ )
1134
1156
  return this
1135
1157
  }
1136
1158
 
@@ -1177,7 +1199,7 @@ export class ByteBuf {
1177
1199
  public writeString(value: string): number {
1178
1200
  const length = value.length
1179
1201
  if (length <= 1024) {
1180
- const start = this.base + this.advanceWriter(length)
1202
+ const start = this.advanceWriter(length) + this.base
1181
1203
  const storage = this.storage
1182
1204
  let index = 0
1183
1205
  for (; index < length; index++) {
@@ -1220,15 +1242,15 @@ export class ByteBuf {
1220
1242
 
1221
1243
  const base = this.base
1222
1244
  const needle = value
1223
- const end = base + Math.min(to, this.writerPosition) - needle.length
1245
+ const end = base + Math.min(to, this.writerPosition) - needle.length + 1
1224
1246
  const first = needle[0]
1225
1247
  if (first === undefined) return Math.max(from, 0)
1226
1248
 
1227
- let index = this.storage.indexOf(first, base + Math.max(from, 0))
1228
- while (index !== -1 && index <= end) {
1249
+ let index = this.seek(first, base + Math.max(from, 0), end)
1250
+ while (index !== -1) {
1229
1251
  if (this.matches(index, needle)) return index - base
1230
1252
 
1231
- index = this.storage.indexOf(first, index + 1)
1253
+ index = this.seek(first, index + 1, end)
1232
1254
  }
1233
1255
 
1234
1256
  return -1
@@ -1296,6 +1318,99 @@ export class ByteBuf {
1296
1318
  return this.view
1297
1319
  }
1298
1320
 
1321
+ /**
1322
+ * Reads two raw bytes
1323
+ * @param start where they sit in storage
1324
+ * @param endian byte order
1325
+ */
1326
+ private loadUint16(start: number, endian: Endian): number {
1327
+ const first = this.storage[start] ?? 0
1328
+ const second = this.storage[start + 1] ?? 0
1329
+
1330
+ return endian === 'big' ? (first << 8) | second : (second << 8) | first
1331
+ }
1332
+
1333
+ /**
1334
+ * Reads three raw bytes
1335
+ * @param start where they sit in storage
1336
+ * @param endian byte order
1337
+ */
1338
+ private loadUint24(start: number, endian: Endian): number {
1339
+ const first = this.storage[start] ?? 0
1340
+ const middle = this.storage[start + 1] ?? 0
1341
+ const last = this.storage[start + 2] ?? 0
1342
+
1343
+ return endian === 'big'
1344
+ ? (first << 16) | (middle << 8) | last
1345
+ : (last << 16) | (middle << 8) | first
1346
+ }
1347
+
1348
+ /**
1349
+ * Reads four raw bytes as unsigned
1350
+ * @param start where they sit in storage
1351
+ * @param endian byte order
1352
+ */
1353
+ private loadUint32(start: number, endian: Endian): number {
1354
+ const bytes = this.storage
1355
+ const value =
1356
+ endian === 'big'
1357
+ ? ((bytes[start] ?? 0) << 24) |
1358
+ ((bytes[start + 1] ?? 0) << 16) |
1359
+ ((bytes[start + 2] ?? 0) << 8) |
1360
+ (bytes[start + 3] ?? 0)
1361
+ : ((bytes[start + 3] ?? 0) << 24) |
1362
+ ((bytes[start + 2] ?? 0) << 16) |
1363
+ ((bytes[start + 1] ?? 0) << 8) |
1364
+ (bytes[start] ?? 0)
1365
+
1366
+ return value >>> 0
1367
+ }
1368
+
1369
+ /**
1370
+ * Writes two raw bytes
1371
+ * @param start where they go in storage
1372
+ * @param value only the low 16 bits count
1373
+ * @param endian byte order
1374
+ */
1375
+ private storeUint16(start: number, value: number, endian: Endian): void {
1376
+ const high = (value >>> 8) & 0xff
1377
+ const low = value & 0xff
1378
+
1379
+ this.storage[start] = endian === 'big' ? high : low
1380
+ this.storage[start + 1] = endian === 'big' ? low : high
1381
+ }
1382
+
1383
+ /**
1384
+ * Writes three raw bytes
1385
+ * @param start where they go in storage
1386
+ * @param value only the low 24 bits count
1387
+ * @param endian byte order
1388
+ */
1389
+ private storeUint24(start: number, value: number, endian: Endian): void {
1390
+ const high = (value >>> 16) & 0xff
1391
+ const low = value & 0xff
1392
+
1393
+ this.storage[start] = endian === 'big' ? high : low
1394
+ this.storage[start + 1] = (value >>> 8) & 0xff
1395
+ this.storage[start + 2] = endian === 'big' ? low : high
1396
+ }
1397
+
1398
+ /**
1399
+ * Writes four raw bytes
1400
+ * @param start where they go in storage
1401
+ * @param value only the low 32 bits count
1402
+ * @param endian byte order
1403
+ */
1404
+ private storeUint32(start: number, value: number, endian: Endian): void {
1405
+ const bytes = this.storage
1406
+ const big = endian === 'big'
1407
+
1408
+ bytes[start] = (big ? value >>> 24 : value) & 0xff
1409
+ bytes[start + 1] = (big ? value >>> 16 : value >>> 8) & 0xff
1410
+ bytes[start + 2] = (big ? value >>> 8 : value >>> 16) & 0xff
1411
+ bytes[start + 3] = (big ? value : value >>> 24) & 0xff
1412
+ }
1413
+
1299
1414
  /**
1300
1415
  * Throws if the range runs outside the array, otherwise returns index
1301
1416
  * @param index absolute offset
@@ -1349,10 +1464,36 @@ export class ByteBuf {
1349
1464
  */
1350
1465
  private indexOfByte(value: number, from: number, to: number): number {
1351
1466
  const base = this.base
1352
- const index = this.storage.indexOf(value, base + Math.max(from, 0))
1353
1467
  const end = base + Math.min(to, this.writerPosition)
1468
+ const index = this.seek(value, base + Math.max(from, 0), end)
1354
1469
 
1355
- return index !== -1 && index < end ? index - base : -1
1470
+ return index === -1 ? -1 : index - base
1471
+ }
1472
+
1473
+ /**
1474
+ * First index of a byte, or -1
1475
+ * @param value byte to look for
1476
+ * @param start first place to check
1477
+ * @param end stops before this
1478
+ */
1479
+ private seek(value: number, start: number, end: number): number {
1480
+ const storage = this.storage
1481
+ const size = end - start
1482
+ if (storage.length - end <= size) {
1483
+ const index = storage.indexOf(value, start)
1484
+ return index < end ? index : -1
1485
+ }
1486
+
1487
+ if (size > ByteBuf.scanAt) {
1488
+ const index = storage.subarray(start, end).indexOf(value)
1489
+ return index === -1 ? -1 : start + index
1490
+ }
1491
+
1492
+ for (let index = start; index < end; index++) {
1493
+ if (storage[index] === value) return index
1494
+ }
1495
+
1496
+ return -1
1356
1497
  }
1357
1498
 
1358
1499
  /**
@@ -28,6 +28,11 @@ export class Quantizer {
28
28
  */
29
29
  private readonly min: number
30
30
 
31
+ /**
32
+ * Top of the range
33
+ */
34
+ private readonly max: number
35
+
31
36
  /**
32
37
  * Top step, 2 to the bits minus 1
33
38
  */
@@ -52,6 +57,7 @@ export class Quantizer {
52
57
  if (reason !== null) throw new RangeError(reason)
53
58
 
54
59
  this.min = options.min
60
+ this.max = options.max
55
61
  this.bits = options.bits
56
62
  this.steps = 2 ** options.bits - 1
57
63
  this.step = (options.max - options.min) / this.steps
@@ -63,6 +69,10 @@ export class Quantizer {
63
69
  */
64
70
  private static refusal(options: QuantizerOptions): string | null {
65
71
  if (!(options.max > options.min)) return 'Range is empty'
72
+ if (!Number.isFinite(options.max - options.min)) {
73
+ return 'Range is not finite'
74
+ }
75
+
66
76
  if (
67
77
  !Number.isInteger(options.bits) ||
68
78
  options.bits < 1 ||
@@ -82,20 +92,23 @@ export class Quantizer {
82
92
  }
83
93
 
84
94
  /**
85
- * Turns a float into its integer step
95
+ * Float to integer step
86
96
  * @param value the float, clamped to the range
87
97
  */
88
98
  public encode(value: number): number {
89
99
  const index = Math.round((value - this.min) / this.step)
100
+ if (index !== index) throw new RangeError('Value is not a number')
90
101
 
91
102
  return index < 0 ? 0 : index > this.steps ? this.steps : index
92
103
  }
93
104
 
94
105
  /**
95
- * Turns a step back into a float
106
+ * Step back to a float
96
107
  * @param index what encode gave
97
108
  */
98
109
  public decode(index: number): number {
99
- return this.min + index * this.step
110
+ const value = this.min + index * this.step
111
+
112
+ return value < this.min ? this.min : value > this.max ? this.max : value
100
113
  }
101
114
  }
@@ -10,7 +10,7 @@ import { OutboundHandlerAdapter } from '../handler/outbound/index.ts'
10
10
  import type { Pipeline } from '../pipeline/index.ts'
11
11
  import type { WriteTracker } from '../pipeline/tracker/index.ts'
12
12
  import { ByteBuf } from '../../buffer/index.ts'
13
- import { pending, protect, resolved } from '../resolved/index.ts'
13
+ import { adopt, pending, protect, resolved } from '../resolved/index.ts'
14
14
  import type { Channel } from '../index.ts'
15
15
 
16
16
  /**
@@ -295,15 +295,20 @@ export class HandlerContext {
295
295
  */
296
296
  public write(message: unknown): Promise<void> {
297
297
  if (message instanceof ByteBuf) message.detach()
298
- this.pipeline.tracker.forwarded(this)
299
- if (this.cachedAt !== this.pipeline.version) this.refresh()
298
+ const pipeline = this.pipeline
299
+ pipeline.tracker.forwarded(this)
300
+ if (this.cachedAt !== pipeline.version) this.refresh()
300
301
  const target = this.writer
302
+ if (target === null) {
303
+ return protect(Promise.reject(new Error('Write is not handled')))
304
+ }
301
305
 
302
- return protect(
303
- target === null
304
- ? Promise.reject(new Error('Write is not handled'))
305
- : target.send(message),
306
- )
306
+ if (!pipeline.syncing) return protect(target.send(message))
307
+
308
+ pipeline.syncing = false
309
+ const result = target.send(message)
310
+ pipeline.syncing = true
311
+ return protect(result)
307
312
  }
308
313
 
309
314
  /**
@@ -327,7 +332,7 @@ export class HandlerContext {
327
332
  }
328
333
 
329
334
  /**
330
- * Fire-and-forget write, lets the pipeline skip tracking the promise
335
+ * Writes without waiting
331
336
  * @param message whatever the next outbound handler expects
332
337
  */
333
338
  public writeSync(message: unknown): void {
@@ -346,7 +351,7 @@ export class HandlerContext {
346
351
  ? target.sendAtHead(message)
347
352
  : target.sendTracked(pipeline.tracker, message)
348
353
  pipeline.syncing = previous
349
- if (result !== resolved) void protect(result)
354
+ if (result !== resolved) pipeline.watch(result)
350
355
  }
351
356
 
352
357
  /**
@@ -466,7 +471,9 @@ export class HandlerContext {
466
471
  }
467
472
 
468
473
  if (result === undefined || result === resolved) return resolved
469
- return result.catch((error: unknown): Promise<void> => this.fail(error))
474
+
475
+ const waiting = result instanceof Promise ? result : adopt(result)
476
+ return waiting.catch((error: unknown): Promise<void> => this.fail(error))
470
477
  }
471
478
 
472
479
  /**