vortez-io 1.0.0

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 (52) hide show
  1. package/.gitignore +15 -0
  2. package/LICENSE +201 -0
  3. package/PROTOCOL.md +245 -0
  4. package/README.md +103 -0
  5. package/build/Frame.d.ts +118 -0
  6. package/build/Frame.js +241 -0
  7. package/build/Frame.js.map +1 -0
  8. package/build/Isomorphic.d.ts +7 -0
  9. package/build/Isomorphic.js +2 -0
  10. package/build/Isomorphic.js.map +1 -0
  11. package/build/RequestManager.d.ts +74 -0
  12. package/build/RequestManager.js +61 -0
  13. package/build/RequestManager.js.map +1 -0
  14. package/build/VIO.d.ts +82 -0
  15. package/build/VIO.js +219 -0
  16. package/build/VIO.js.map +1 -0
  17. package/build/VIOError.d.ts +17 -0
  18. package/build/VIOError.js +25 -0
  19. package/build/VIOError.js.map +1 -0
  20. package/build/VIOStrict.d.ts +62 -0
  21. package/build/VIOStrict.js +103 -0
  22. package/build/VIOStrict.js.map +1 -0
  23. package/build/index.d.ts +4 -0
  24. package/build/index.js +5 -0
  25. package/build/index.js.map +1 -0
  26. package/build/support/LineParser.d.ts +25 -0
  27. package/build/support/LineParser.js +50 -0
  28. package/build/support/LineParser.js.map +1 -0
  29. package/build/support/UTF8Codec.d.ts +34 -0
  30. package/build/support/UTF8Codec.js +155 -0
  31. package/build/support/UTF8Codec.js.map +1 -0
  32. package/build/support/UUID.d.ts +62 -0
  33. package/build/support/UUID.js +109 -0
  34. package/build/support/UUID.js.map +1 -0
  35. package/build/support/Varint.d.ts +26 -0
  36. package/build/support/Varint.js +61 -0
  37. package/build/support/Varint.js.map +1 -0
  38. package/docs/PROTOCOL-PROPOSE.md +252 -0
  39. package/package.json +35 -0
  40. package/scripts/build.sh +14 -0
  41. package/src/Frame.ts +312 -0
  42. package/src/Isomorphic.ts +7 -0
  43. package/src/RequestManager.ts +114 -0
  44. package/src/VIO.ts +194 -0
  45. package/src/VIOError.ts +22 -0
  46. package/src/VIOStrict.ts +123 -0
  47. package/src/index.ts +4 -0
  48. package/src/support/LineParser.ts +55 -0
  49. package/src/support/UTF8Codec.ts +166 -0
  50. package/src/support/UUID.ts +125 -0
  51. package/src/support/Varint.ts +62 -0
  52. package/tsconfig.json +17 -0
@@ -0,0 +1,155 @@
1
+ /**
2
+ * @module vortez-io
3
+ * @author NetFeez <netfeez.dev@netfeez.dev>
4
+ * @license Apache-2.0
5
+ * @description UTF-8 encoder/decoder optimized for:
6
+ * - predictable memory usage (pre-allocation)
7
+ * - safe decoding (malformed/truncated input)
8
+ * - zero external dependencies
9
+ *
10
+ * ⚠️ Fully standards-compliant behavior for replacement (U+FFFD)
11
+ */
12
+ export class UTF8Codec {
13
+ static REPLACEMENT_CHAR = '\uFFFD';
14
+ /**
15
+ * Encodes a string into a UTF-8 byte array.
16
+ * @param text The string to encode.
17
+ * @returns A Uint8Array containing the UTF-8 encoded bytes of the input string.
18
+ */
19
+ static encode(text) {
20
+ const len = text.length;
21
+ let byteLength = 0;
22
+ for (let i = 0; i < len; i++) {
23
+ const code = text.charCodeAt(i);
24
+ if (code < 0x80) {
25
+ byteLength += 1;
26
+ }
27
+ else if (code < 0x800) {
28
+ byteLength += 2;
29
+ }
30
+ else if (code >= 0xd800 && code <= 0xdbff) {
31
+ const next = i + 1 < len ? text.charCodeAt(i + 1) : 0;
32
+ if (next >= 0xdc00 && next <= 0xdfff) {
33
+ byteLength += 4;
34
+ i++;
35
+ }
36
+ else {
37
+ byteLength += 3;
38
+ }
39
+ }
40
+ else {
41
+ byteLength += 3;
42
+ }
43
+ }
44
+ const out = new Uint8Array(byteLength);
45
+ let offset = 0;
46
+ for (let i = 0; i < len; i++) {
47
+ const code = text.charCodeAt(i);
48
+ if (code < 0x80) {
49
+ out[offset++] = code;
50
+ continue;
51
+ }
52
+ if (code < 0x800) {
53
+ out[offset++] = 0xc0 | (code >> 6);
54
+ out[offset++] = 0x80 | (code & 0x3f);
55
+ continue;
56
+ }
57
+ if (code >= 0xd800 && code <= 0xdbff) {
58
+ const next = i + 1 < len ? text.charCodeAt(i + 1) : 0;
59
+ if (next >= 0xdc00 && next <= 0xdfff) {
60
+ const cp = 0x10000 + ((code & 0x3ff) << 10) + (next & 0x3ff);
61
+ out[offset++] = 0xf0 | (cp >> 18);
62
+ out[offset++] = 0x80 | ((cp >> 12) & 0x3f);
63
+ out[offset++] = 0x80 | ((cp >> 6) & 0x3f);
64
+ out[offset++] = 0x80 | (cp & 0x3f);
65
+ i++;
66
+ continue;
67
+ }
68
+ offset = this.writeReplacement(out, offset);
69
+ continue;
70
+ }
71
+ out[offset++] = 0xe0 | (code >> 12);
72
+ out[offset++] = 0x80 | ((code >> 6) & 0x3f);
73
+ out[offset++] = 0x80 | (code & 0x3f);
74
+ }
75
+ return out;
76
+ }
77
+ /**
78
+ * Decodes a UTF-8 byte array into a string. Malformed sequences are replaced with the Unicode replacement character (U+FFFD).
79
+ * @param bytes The Uint8Array containing the UTF-8 encoded bytes to decode.
80
+ * @returns The decoded string, with any malformed sequences replaced by U+FFFD.
81
+ */
82
+ static decode(bytes) {
83
+ let out = '';
84
+ let i = 0;
85
+ const len = bytes.length;
86
+ while (i < len) {
87
+ const b1 = bytes[i++];
88
+ if (b1 < 0x80) {
89
+ out += String.fromCharCode(b1);
90
+ continue;
91
+ }
92
+ if ((b1 & 0xe0) === 0xc0) {
93
+ if (i >= len) {
94
+ out += this.REPLACEMENT_CHAR;
95
+ break;
96
+ }
97
+ const b2 = bytes[i++];
98
+ if ((b2 & 0xc0) !== 0x80) {
99
+ out += this.REPLACEMENT_CHAR;
100
+ continue;
101
+ }
102
+ const code = ((b1 & 0x1f) << 6) | (b2 & 0x3f);
103
+ out += String.fromCharCode(code);
104
+ continue;
105
+ }
106
+ if ((b1 & 0xf0) === 0xe0) {
107
+ if (i + 1 >= len) {
108
+ out += this.REPLACEMENT_CHAR;
109
+ break;
110
+ }
111
+ const b2 = bytes[i++];
112
+ const b3 = bytes[i++];
113
+ if ((b2 & 0xc0) !== 0x80 || (b3 & 0xc0) !== 0x80) {
114
+ out += this.REPLACEMENT_CHAR;
115
+ continue;
116
+ }
117
+ const code = ((b1 & 0x0f) << 12) | ((b2 & 0x3f) << 6) | (b3 & 0x3f);
118
+ out += String.fromCharCode(code);
119
+ continue;
120
+ }
121
+ if ((b1 & 0xf8) === 0xf0) {
122
+ if (i + 2 >= len) {
123
+ out += this.REPLACEMENT_CHAR;
124
+ break;
125
+ }
126
+ const b2 = bytes[i++];
127
+ const b3 = bytes[i++];
128
+ const b4 = bytes[i++];
129
+ if ((b2 & 0xc0) !== 0x80 || (b3 & 0xc0) !== 0x80 || (b4 & 0xc0) !== 0x80) {
130
+ out += this.REPLACEMENT_CHAR;
131
+ continue;
132
+ }
133
+ const cp = ((b1 & 0x07) << 18) | ((b2 & 0x3f) << 12) | ((b3 & 0x3f) << 6) | (b4 & 0x3f);
134
+ out += String.fromCodePoint(cp);
135
+ continue;
136
+ }
137
+ out += this.REPLACEMENT_CHAR;
138
+ }
139
+ return out;
140
+ }
141
+ /**
142
+ * Writes the UTF-8 encoding of the replacement character (U+FFFD) to the output buffer at the specified offset.
143
+ * @param out The output buffer to write to.
144
+ * @param offset The offset in the buffer where the replacement character should be written.
145
+ * @returns The new offset after writing the replacement character.
146
+ */
147
+ static writeReplacement(out, offset) {
148
+ out[offset++] = 0xef;
149
+ out[offset++] = 0xbf;
150
+ out[offset++] = 0xbd;
151
+ return offset;
152
+ }
153
+ }
154
+ export default UTF8Codec;
155
+ //# sourceMappingURL=UTF8Codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UTF8Codec.js","sourceRoot":"","sources":["../../src/support/UTF8Codec.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,OAAO,SAAS;IACV,MAAM,CAAU,gBAAgB,GAAG,QAAQ,CAAC;IACpD;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,IAAY;QAC7B,MAAM,GAAG,GAAG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AAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;gBAEtB,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;oBACvE,GAAG,IAAI,IAAI,CAAC,gBAAgB,CAAC;oBAC7B,SAAS;gBACb,CAAC;gBAED,MAAM,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC;gBAExF,GAAG,IAAI,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;gBAChC,SAAS;YACb,CAAC;YACD,GAAG,IAAI,IAAI,CAAC,gBAAgB,CAAC;QACjC,CAAC;QAED,OAAO,GAAG,CAAC;IACf,CAAC;IACD;;;;;OAKG;IACO,MAAM,CAAC,gBAAgB,CAAC,GAAe,EAAE,MAAc;QAC7D,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC;QACrB,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC;QACrB,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC;QACrB,OAAO,MAAM,CAAC;IAClB,CAAC;;AAGL,eAAe,SAAS,CAAC"}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * @module vortez-io
3
+ * @author NetFeez <netfeez.dev@gemail.com>
4
+ * @license Apache-2.0
5
+ * @description Isomorphic UUID v4 generator.
6
+ *
7
+ * ⚠️ IMPORTANT: READ THIS BEFORE USING.
8
+ *
9
+ * IS NO LONGER CRYPTOGRAPHICALLY SECURE.
10
+ * This module provides a fallback implementation of UUID v4 generation using a Linear Congruential Generator (LCG) algorithm
11
+ * The generated UUIDs are suitable for non-cryptographic use cases, such as generating unique identifiers for events or requests within the Vortez IO protocol, but should not be used in security-sensitive contexts.
12
+ */
13
+ export declare class UUID {
14
+ private static seed;
15
+ private static readonly HEX_TABLE;
16
+ /**
17
+ * Initialize the seed for the LCG fallback using a combination of current time and random value.
18
+ * This ensures that the generated UUIDs are not the same across different runs of the application, even in fallback mode.
19
+ * @returns A 32-bit unsigned integer to be used as the initial seed for the LCG.
20
+ */
21
+ private static initSeed;
22
+ /**
23
+ * Generate the next 32-bit unsigned integer using a Linear Congruential Generator (LCG) algorithm.
24
+ * This method is used as a fallback for generating random values when crypto APIs are not available.
25
+ * The LCG parameters are chosen to provide a reasonable period and distribution for non-cryptographic use cases.
26
+ * @returns A 32-bit unsigned integer generated by the LCG algorithm.
27
+ */
28
+ private static next32;
29
+ /**
30
+ * Generate a UUID v4 string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
31
+ * This method uses the createRaw() method to generate a 16-byte Uint8Array and then converts it to the standard UUID string format.
32
+ * @returns A string representation of the generated UUID.
33
+ */
34
+ static create(): string;
35
+ /**
36
+ * Generate a 16-byte Uint8Array containing a UUID v4.
37
+ * This method uses the LCG fallback to generate random values when crypto APIs are not available.
38
+ * @returns A Uint8Array containing the 16 bytes of the UUID.
39
+ */
40
+ static createRaw(): Uint8Array;
41
+ /**
42
+ * Convert a 16-byte Uint8Array to a UUID string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
43
+ * @param bin A Uint8Array containing the 16 bytes of the UUID.
44
+ * @returns A string representation of the UUID.
45
+ */
46
+ static binToUuid(bin: Uint8Array): string;
47
+ /**
48
+ * Convert a UUID string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx to a 16-byte Uint8Array.
49
+ * @param uuid A string representation of the UUID.
50
+ * @returns A Uint8Array containing the 16 bytes of the UUID.
51
+ */
52
+ static uuidToBin(uuid: string): Uint8Array;
53
+ /**
54
+ * Validate whether a given string is a valid UUID v4 according to the standard format and version/type bits.
55
+ * This method checks that the string matches the expected pattern for UUID v4, including the correct placement of hyphens and the specific bits that indicate version 4 and the variant.
56
+ * @param uuid The string to validate as a UUID v4.
57
+ * @returns True if the input string is a valid UUID v4, false otherwise.
58
+ */
59
+ static isValid(uuid: string): boolean;
60
+ }
61
+ export declare namespace UUID { }
62
+ export default UUID;
@@ -0,0 +1,109 @@
1
+ /**
2
+ * @module vortez-io
3
+ * @author NetFeez <netfeez.dev@gemail.com>
4
+ * @license Apache-2.0
5
+ * @description Isomorphic UUID v4 generator.
6
+ *
7
+ * ⚠️ IMPORTANT: READ THIS BEFORE USING.
8
+ *
9
+ * IS NO LONGER CRYPTOGRAPHICALLY SECURE.
10
+ * This module provides a fallback implementation of UUID v4 generation using a Linear Congruential Generator (LCG) algorithm
11
+ * The generated UUIDs are suitable for non-cryptographic use cases, such as generating unique identifiers for events or requests within the Vortez IO protocol, but should not be used in security-sensitive contexts.
12
+ */
13
+ export class UUID {
14
+ static seed = UUID.initSeed();
15
+ static HEX_TABLE = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
16
+ /**
17
+ * Initialize the seed for the LCG fallback using a combination of current time and random value.
18
+ * This ensures that the generated UUIDs are not the same across different runs of the application, even in fallback mode.
19
+ * @returns A 32-bit unsigned integer to be used as the initial seed for the LCG.
20
+ */
21
+ static initSeed() {
22
+ return (Date.now() ^ Math.floor(Math.random() * 0xffffffff)) >>> 0;
23
+ }
24
+ /**
25
+ * Generate the next 32-bit unsigned integer using a Linear Congruential Generator (LCG) algorithm.
26
+ * This method is used as a fallback for generating random values when crypto APIs are not available.
27
+ * The LCG parameters are chosen to provide a reasonable period and distribution for non-cryptographic use cases.
28
+ * @returns A 32-bit unsigned integer generated by the LCG algorithm.
29
+ */
30
+ static next32() {
31
+ UUID.seed = (1664525 * UUID.seed + 1013904223) >>> 0;
32
+ return UUID.seed;
33
+ }
34
+ /**
35
+ * Generate a UUID v4 string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
36
+ * This method uses the createRaw() method to generate a 16-byte Uint8Array and then converts it to the standard UUID string format.
37
+ * @returns A string representation of the generated UUID.
38
+ */
39
+ static create() {
40
+ return this.binToUuid(this.createRaw());
41
+ }
42
+ /**
43
+ * Generate a 16-byte Uint8Array containing a UUID v4.
44
+ * This method uses the LCG fallback to generate random values when crypto APIs are not available.
45
+ * @returns A Uint8Array containing the 16 bytes of the UUID.
46
+ */
47
+ static createRaw() {
48
+ const bytes = new Uint8Array(16);
49
+ for (let i = 0; i < 16; i += 4) {
50
+ const n = UUID.next32();
51
+ bytes[i] = (n >>> 24) & 0xff;
52
+ bytes[i + 1] = (n >>> 16) & 0xff;
53
+ bytes[i + 2] = (n >>> 8) & 0xff;
54
+ bytes[i + 3] = n & 0xff;
55
+ }
56
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
57
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
58
+ return bytes;
59
+ }
60
+ /**
61
+ * Convert a 16-byte Uint8Array to a UUID string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
62
+ * @param bin A Uint8Array containing the 16 bytes of the UUID.
63
+ * @returns A string representation of the UUID.
64
+ */
65
+ static binToUuid(bin) {
66
+ const hex = UUID.HEX_TABLE;
67
+ return (hex[bin[0]] + hex[bin[1]] + hex[bin[2]] + hex[bin[3]] + "-" +
68
+ hex[bin[4]] + hex[bin[5]] + "-" +
69
+ hex[bin[6]] + hex[bin[7]] + "-" +
70
+ hex[bin[8]] + hex[bin[9]] + "-" +
71
+ hex[bin[10]] + hex[bin[11]] + hex[bin[12]] +
72
+ hex[bin[13]] + hex[bin[14]] + hex[bin[15]]);
73
+ }
74
+ /**
75
+ * Convert a UUID string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx to a 16-byte Uint8Array.
76
+ * @param uuid A string representation of the UUID.
77
+ * @returns A Uint8Array containing the 16 bytes of the UUID.
78
+ */
79
+ static uuidToBin(uuid) {
80
+ const hex = UUID.HEX_TABLE;
81
+ const bin = new Uint8Array(16);
82
+ const parts = uuid.split('-');
83
+ if (parts.length !== 5)
84
+ throw new Error(`Invalid UUID format: ${uuid}`);
85
+ const hexStr = parts.join('');
86
+ if (hexStr.length !== 32)
87
+ throw new Error(`Invalid UUID format: ${uuid}`);
88
+ for (let i = 0; i < 16; i++) {
89
+ const byteHex = hexStr.substr(i * 2, 2);
90
+ const byteValue = parseInt(byteHex, 16);
91
+ if (isNaN(byteValue))
92
+ throw new Error(`Invalid UUID format: ${uuid}`);
93
+ bin[i] = byteValue;
94
+ }
95
+ return bin;
96
+ }
97
+ /**
98
+ * Validate whether a given string is a valid UUID v4 according to the standard format and version/type bits.
99
+ * This method checks that the string matches the expected pattern for UUID v4, including the correct placement of hyphens and the specific bits that indicate version 4 and the variant.
100
+ * @param uuid The string to validate as a UUID v4.
101
+ * @returns True if the input string is a valid UUID v4, false otherwise.
102
+ */
103
+ static isValid(uuid) {
104
+ const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
105
+ return uuidRegex.test(uuid);
106
+ }
107
+ }
108
+ export default UUID;
109
+ //# sourceMappingURL=UUID.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UUID.js","sourceRoot":"","sources":["../../src/support/UUID.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,IAAI;IACL,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;IAE9B,MAAM,CAAU,SAAS,GAAG,KAAK,CAAC,IAAI,CAC1C,EAAE,MAAM,EAAE,GAAG,EAAE,EACf,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC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AG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;QAE/B,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC9B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,EAAE,CAAC,CAAC;QAExE,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC9B,IAAI,MAAM,CAAC,MAAM,KAAK,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,EAAE,CAAC,CAAC;QAE1E,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC;YAC1B,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;YACxC,MAAM,SAAS,GAAG,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;YACxC,IAAI,KAAK,CAAC,SAAS,CAAC;gBAAE,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,EAAE,CAAC,CAAC;YACtE,GAAG,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;QACvB,CAAC;QACD,OAAO,GAAG,CAAC;IACf,CAAC;IACD;;;;;OAKG;IACI,MAAM,CAAC,OAAO,CAAC,IAAY;QAC9B,MAAM,SAAS,GAAG,wEAAwE,CAAC;QAC3F,OAAO,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC;;AAIL,eAAe,IAAI,CAAC"}
@@ -0,0 +1,26 @@
1
+ export declare class Varint {
2
+ /**
3
+ * Reads a varint-encoded integer from the given Uint8Array starting at the specified offset.
4
+ * Varints are a compact way to encode integers, where smaller numbers take fewer bytes.
5
+ * Each byte uses the lower 7 bits to store data and the highest bit as a continuation flag (1 if more bytes follow, 0 if this is the last byte).
6
+ * @param view The Uint8Array to read from.
7
+ * @param offset The starting index in the array to read the varint from.
8
+ * @returns An object containing the decoded integer value and the new offset after reading the varint.
9
+ */
10
+ static readVarint(view: Uint8Array, offset: number): Varint.Result;
11
+ /**
12
+ * Encodes a number into a varint format (base-128 encoding) and returns it as a Uint8Array.
13
+ * Varints are a compact way to encode integers, where smaller numbers take fewer bytes.
14
+ * Each byte uses the lower 7 bits to store data and the highest bit as a continuation flag (1 if more bytes follow, 0 if this is the last byte).
15
+ * @param value The number to encode as a varint. Must be a non-negative integer.
16
+ * @returns A Uint8Array containing the varint-encoded bytes of the input number.
17
+ */
18
+ static writeVarint(value: number): Uint8Array;
19
+ }
20
+ export declare namespace Varint {
21
+ interface Result {
22
+ value: number;
23
+ offset: number;
24
+ }
25
+ }
26
+ export default Varint;
@@ -0,0 +1,61 @@
1
+ /**
2
+ * @module vortez-io
3
+ * @author NetFeez <netfeez.dev@gemail.com>
4
+ * @license Apache-2.0
5
+ * @description Provides encoding and decoding functionality for variable-length integers (varints) used in the Vortez IO protocol.
6
+ * Varints are a compact way to encode integers, where smaller numbers take fewer bytes. Each byte uses the lower 7 bits to store data and the highest bit as a continuation flag (1 if more bytes follow, 0 if this is the last byte).
7
+ * This module includes methods for reading varints from a Uint8Array and writing numbers as varints into a Uint8Array, with error handling for invalid input and buffer overflows.
8
+ */
9
+ import VIOError from "../VIOError.js";
10
+ export class Varint {
11
+ /**
12
+ * Reads a varint-encoded integer from the given Uint8Array starting at the specified offset.
13
+ * Varints are a compact way to encode integers, where smaller numbers take fewer bytes.
14
+ * Each byte uses the lower 7 bits to store data and the highest bit as a continuation flag (1 if more bytes follow, 0 if this is the last byte).
15
+ * @param view The Uint8Array to read from.
16
+ * @param offset The starting index in the array to read the varint from.
17
+ * @returns An object containing the decoded integer value and the new offset after reading the varint.
18
+ */
19
+ static readVarint(view, offset) {
20
+ let value = 0;
21
+ let shift = 0;
22
+ let pos = offset;
23
+ while (true) {
24
+ if (pos >= view.length)
25
+ throw new VIOError(VIOError.Code.BUFFER_TOO_SHORT, 'Varint truncated');
26
+ const byte = view[pos++];
27
+ value |= (byte & 0x7F) << shift;
28
+ if ((byte & 0x80) === 0)
29
+ break;
30
+ shift += 7;
31
+ if (shift > 35)
32
+ throw new VIOError(VIOError.Code.INVALID_DATA, 'Varint too large');
33
+ }
34
+ return { value, offset: pos };
35
+ }
36
+ /**
37
+ * Encodes a number into a varint format (base-128 encoding) and returns it as a Uint8Array.
38
+ * Varints are a compact way to encode integers, where smaller numbers take fewer bytes.
39
+ * Each byte uses the lower 7 bits to store data and the highest bit as a continuation flag (1 if more bytes follow, 0 if this is the last byte).
40
+ * @param value The number to encode as a varint. Must be a non-negative integer.
41
+ * @returns A Uint8Array containing the varint-encoded bytes of the input number.
42
+ */
43
+ static writeVarint(value) {
44
+ if (value < 0)
45
+ throw new VIOError(VIOError.Code.INVALID_DATA, 'Varint cannot be negative');
46
+ const bytes = [];
47
+ while (true) {
48
+ let byte = value & 0x7F;
49
+ value >>>= 7;
50
+ if (value !== 0) {
51
+ byte |= 0x80;
52
+ }
53
+ bytes.push(byte);
54
+ if (value === 0)
55
+ break;
56
+ }
57
+ return new Uint8Array(bytes);
58
+ }
59
+ }
60
+ export default Varint;
61
+ //# sourceMappingURL=Varint.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Varint.js","sourceRoot":"","sources":["../../src/support/Varint.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAEtC,MAAM,OAAO,MAAM;IACf;;;;;;;OAOG;IACI,MAAM,CAAC,UAAU,CAAC,IAAgB,EAAE,MAAc;QACrD,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,IAAI,GAAG,GAAG,MAAM,CAAC;QAEjB,OAAO,IAAI,EAAE,CAAC;YACV,IAAI,GAAG,IAAI,IAAI,CAAC,MAAM;gBAAE,MAAM,IAAI,QAAQ,CAAC,QAAQ,CA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