dredless 0.2.0 → 0.3.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 (80) hide show
  1. package/README.md +65 -48
  2. package/dist/browser/dredless.global.js +12047 -0
  3. package/dist/browser/dredless.global.js.map +7 -0
  4. package/docs/API_REFERENCE.md +2481 -2481
  5. package/docs/BROWSER_API.md +98 -98
  6. package/docs/NODE_API.md +359 -359
  7. package/package.json +40 -35
  8. package/src/browser-global.js +3 -0
  9. package/src/browser.d.ts +212 -212
  10. package/src/browser.js +133 -133
  11. package/src/client/core.js +1202 -1202
  12. package/src/client/domains/index.js +1818 -1818
  13. package/src/client/index.d.ts +2 -2
  14. package/src/client/index.js +1 -1
  15. package/src/compression/lz4.d.ts +1 -1
  16. package/src/compression/lz4.js +112 -112
  17. package/src/constants.js +74 -74
  18. package/src/crypto/chacha-native.d.ts +9 -9
  19. package/src/crypto/chacha-native.js +48 -48
  20. package/src/crypto/chacha.d.ts +25 -25
  21. package/src/crypto/chacha.js +274 -274
  22. package/src/events.js +39 -39
  23. package/src/index.d.ts +2277 -2277
  24. package/src/index.js +125 -125
  25. package/src/network/browser-session.js +291 -291
  26. package/src/network/connection.d.ts +1 -1
  27. package/src/network/connection.js +31 -31
  28. package/src/network/cookies.js +85 -85
  29. package/src/network/http.js +34 -34
  30. package/src/network/index.d.ts +4 -4
  31. package/src/network/index.js +4 -4
  32. package/src/network/servers.d.ts +7 -7
  33. package/src/network/servers.js +133 -133
  34. package/src/network/session.d.ts +7 -7
  35. package/src/network/session.js +320 -320
  36. package/src/network/ships.d.ts +12 -12
  37. package/src/network/ships.js +73 -73
  38. package/src/node.d.ts +2 -2
  39. package/src/node.js +8 -8
  40. package/src/protocol/codec/binary.d.ts +3 -3
  41. package/src/protocol/codec/binary.js +52 -52
  42. package/src/protocol/codec/msgpack.d.ts +3 -3
  43. package/src/protocol/codec/msgpack.js +555 -555
  44. package/src/protocol/inbound/frame.d.ts +1 -1
  45. package/src/protocol/inbound/frame.js +8 -8
  46. package/src/protocol/inbound/world-payload.d.ts +3 -3
  47. package/src/protocol/inbound/world-payload.js +21 -21
  48. package/src/protocol/index.d.ts +11 -11
  49. package/src/protocol/index.js +19 -19
  50. package/src/protocol/outbound/blueprint.d.ts +22 -22
  51. package/src/protocol/outbound/blueprint.js +36 -36
  52. package/src/protocol/outbound/commands.d.ts +3 -3
  53. package/src/protocol/outbound/commands.js +149 -149
  54. package/src/protocol/outbound/comms.d.ts +28 -28
  55. package/src/protocol/outbound/comms.js +40 -40
  56. package/src/protocol/outbound/inventory.d.ts +64 -64
  57. package/src/protocol/outbound/inventory.js +147 -147
  58. package/src/protocol/outbound/ship-management.d.ts +84 -84
  59. package/src/protocol/outbound/ship-management.js +221 -221
  60. package/src/protocol/outbound/sign.d.ts +18 -18
  61. package/src/protocol/outbound/sign.js +45 -45
  62. package/src/protocol/outbound/ui-config.d.ts +157 -157
  63. package/src/protocol/outbound/ui-config.js +578 -578
  64. package/src/runtime.js +36 -36
  65. package/src/state/generator-maze.d.ts +32 -32
  66. package/src/state/generator-maze.js +370 -370
  67. package/src/state/index.d.ts +3 -3
  68. package/src/state/index.js +3 -3
  69. package/src/state/items.js +20 -20
  70. package/src/state/model/decoder.js +1554 -1554
  71. package/src/state/model/index.d.ts +33 -33
  72. package/src/state/model/index.js +2 -2
  73. package/src/state/model/loader-config.js +1020 -1020
  74. package/src/state/model/reader.js +69 -69
  75. package/src/state/model/state.js +1257 -1257
  76. package/src/state/overworld.js +72 -72
  77. package/src/state/tilesets.js +53 -53
  78. package/src/state/world/index.d.ts +29 -29
  79. package/src/state/world/index.js +180 -180
  80. package/src/state/world/state.js +297 -297
@@ -1,2 +1,2 @@
1
- export {DredlessClient, WrenchMode, TurretMode} from "../index.js";
2
- export type {DredlessClientOptions, ClientSnapshot} from "../index.js";
1
+ export {DredlessClient, WrenchMode, TurretMode} from "../index.js";
2
+ export type {DredlessClientOptions, ClientSnapshot} from "../index.js";
@@ -1 +1 @@
1
- export {DredlessClient, WrenchMode, TurretMode} from "./core.js";
1
+ export {DredlessClient, WrenchMode, TurretMode} from "./core.js";
@@ -1 +1 @@
1
- export function decompressLz4Frame(bytes: Uint8Array | ArrayBuffer | number[]): Uint8Array;
1
+ export function decompressLz4Frame(bytes: Uint8Array | ArrayBuffer | number[]): Uint8Array;
@@ -1,112 +1,112 @@
1
- import {toUint8Array} from "../protocol/codec/binary.js";
2
-
3
- // Growable typed-array sink. LZ4 block headers carry the compressed size, not
4
- // the decompressed size, so the output buffer starts at a generous multiple and
5
- // doubles on demand rather than pushing into a plain array one byte at a time.
6
- function ensureCapacity(buffer, needed) {
7
- if (needed <= buffer.length) return buffer;
8
- let size = buffer.length || 64;
9
- while (size < needed) size *= 2;
10
- const grown = new Uint8Array(size);
11
- grown.set(buffer);
12
- return grown;
13
- }
14
-
15
- function decompressLz4Block(block) {
16
- let out = new Uint8Array(Math.max(64, block.length * 4));
17
- let len = 0;
18
- let pos = 0;
19
-
20
- while (pos < block.length) {
21
- const token = block[pos++];
22
-
23
- let literalLen = token >>> 4;
24
- if (literalLen === 15) {
25
- let b;
26
- do {
27
- if (pos >= block.length) throw new Error("truncated lz4 literal length");
28
- b = block[pos++];
29
- literalLen += b;
30
- } while (b === 255);
31
- }
32
-
33
- if (pos + literalLen > block.length) throw new Error("truncated lz4 literals");
34
- if (literalLen > 0) {
35
- out = ensureCapacity(out, len + literalLen);
36
- out.set(block.subarray(pos, pos + literalLen), len);
37
- len += literalLen;
38
- pos += literalLen;
39
- }
40
-
41
- if (pos >= block.length) break;
42
- if (pos + 2 > block.length) throw new Error("truncated lz4 offset");
43
- const offset = block[pos] | (block[pos + 1] << 8);
44
- pos += 2;
45
- if (offset === 0 || offset > len) throw new Error(`invalid lz4 offset ${offset}`);
46
-
47
- let matchLen = token & 0x0f;
48
- if (matchLen === 15) {
49
- let b;
50
- do {
51
- if (pos >= block.length) throw new Error("truncated lz4 match length");
52
- b = block[pos++];
53
- matchLen += b;
54
- } while (b === 255);
55
- }
56
- matchLen += 4;
57
-
58
- out = ensureCapacity(out, len + matchLen);
59
- // Matches may overlap the region being written, so copy byte by byte rather
60
- // than with copyWithin, which would read the pre-copy bytes.
61
- let from = len - offset;
62
- for (let i = 0; i < matchLen; i++) out[len++] = out[from++];
63
- }
64
-
65
- // Return a compact standalone array, matching the previous contract: callers
66
- // must not see a view over a larger backing buffer.
67
- return out.length === len ? out : out.slice(0, len);
68
- }
69
-
70
- function decompressLz4Frame(bytes) {
71
- const buf = toUint8Array(bytes);
72
- if (buf.length < 7) return buf;
73
- const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
74
- if (view.getUint32(0, true) !== 0x184d2204) return buf;
75
- const flg = buf[4];
76
- let pos = 6;
77
- if (flg & 0x08) pos += 8;
78
- if (flg & 0x01) pos += 4;
79
- pos += 1;
80
- const chunks = [];
81
- let total = 0;
82
- while (pos + 4 <= buf.length) {
83
- const rawSize = view.getUint32(pos, true);
84
- pos += 4;
85
- if (rawSize === 0) break;
86
- const uncompressed = (rawSize & 0x80000000) !== 0;
87
- const size = rawSize & 0x7fffffff;
88
- if (pos + size > buf.length) throw new Error("truncated lz4 block");
89
- const block = buf.subarray(pos, pos + size);
90
- const chunk = uncompressed ? block.slice() : decompressLz4Block(block);
91
- chunks.push(chunk);
92
- total += chunk.length;
93
- pos += size;
94
- if (flg & 0x10) pos += 4;
95
- }
96
- // Single-chunk frames are the common case and need no concatenation pass.
97
- if (chunks.length === 1) return chunks[0];
98
- if (chunks.length === 0) return new Uint8Array(0);
99
- const out = new Uint8Array(total);
100
- let offset = 0;
101
- for (const chunk of chunks) {
102
- out.set(chunk, offset);
103
- offset += chunk.length;
104
- }
105
- return out;
106
- }
107
-
108
-
109
- export {
110
- decompressLz4Block,
111
- decompressLz4Frame
112
- };
1
+ import {toUint8Array} from "../protocol/codec/binary.js";
2
+
3
+ // Growable typed-array sink. LZ4 block headers carry the compressed size, not
4
+ // the decompressed size, so the output buffer starts at a generous multiple and
5
+ // doubles on demand rather than pushing into a plain array one byte at a time.
6
+ function ensureCapacity(buffer, needed) {
7
+ if (needed <= buffer.length) return buffer;
8
+ let size = buffer.length || 64;
9
+ while (size < needed) size *= 2;
10
+ const grown = new Uint8Array(size);
11
+ grown.set(buffer);
12
+ return grown;
13
+ }
14
+
15
+ function decompressLz4Block(block) {
16
+ let out = new Uint8Array(Math.max(64, block.length * 4));
17
+ let len = 0;
18
+ let pos = 0;
19
+
20
+ while (pos < block.length) {
21
+ const token = block[pos++];
22
+
23
+ let literalLen = token >>> 4;
24
+ if (literalLen === 15) {
25
+ let b;
26
+ do {
27
+ if (pos >= block.length) throw new Error("truncated lz4 literal length");
28
+ b = block[pos++];
29
+ literalLen += b;
30
+ } while (b === 255);
31
+ }
32
+
33
+ if (pos + literalLen > block.length) throw new Error("truncated lz4 literals");
34
+ if (literalLen > 0) {
35
+ out = ensureCapacity(out, len + literalLen);
36
+ out.set(block.subarray(pos, pos + literalLen), len);
37
+ len += literalLen;
38
+ pos += literalLen;
39
+ }
40
+
41
+ if (pos >= block.length) break;
42
+ if (pos + 2 > block.length) throw new Error("truncated lz4 offset");
43
+ const offset = block[pos] | (block[pos + 1] << 8);
44
+ pos += 2;
45
+ if (offset === 0 || offset > len) throw new Error(`invalid lz4 offset ${offset}`);
46
+
47
+ let matchLen = token & 0x0f;
48
+ if (matchLen === 15) {
49
+ let b;
50
+ do {
51
+ if (pos >= block.length) throw new Error("truncated lz4 match length");
52
+ b = block[pos++];
53
+ matchLen += b;
54
+ } while (b === 255);
55
+ }
56
+ matchLen += 4;
57
+
58
+ out = ensureCapacity(out, len + matchLen);
59
+ // Matches may overlap the region being written, so copy byte by byte rather
60
+ // than with copyWithin, which would read the pre-copy bytes.
61
+ let from = len - offset;
62
+ for (let i = 0; i < matchLen; i++) out[len++] = out[from++];
63
+ }
64
+
65
+ // Return a compact standalone array, matching the previous contract: callers
66
+ // must not see a view over a larger backing buffer.
67
+ return out.length === len ? out : out.slice(0, len);
68
+ }
69
+
70
+ function decompressLz4Frame(bytes) {
71
+ const buf = toUint8Array(bytes);
72
+ if (buf.length < 7) return buf;
73
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
74
+ if (view.getUint32(0, true) !== 0x184d2204) return buf;
75
+ const flg = buf[4];
76
+ let pos = 6;
77
+ if (flg & 0x08) pos += 8;
78
+ if (flg & 0x01) pos += 4;
79
+ pos += 1;
80
+ const chunks = [];
81
+ let total = 0;
82
+ while (pos + 4 <= buf.length) {
83
+ const rawSize = view.getUint32(pos, true);
84
+ pos += 4;
85
+ if (rawSize === 0) break;
86
+ const uncompressed = (rawSize & 0x80000000) !== 0;
87
+ const size = rawSize & 0x7fffffff;
88
+ if (pos + size > buf.length) throw new Error("truncated lz4 block");
89
+ const block = buf.subarray(pos, pos + size);
90
+ const chunk = uncompressed ? block.slice() : decompressLz4Block(block);
91
+ chunks.push(chunk);
92
+ total += chunk.length;
93
+ pos += size;
94
+ if (flg & 0x10) pos += 4;
95
+ }
96
+ // Single-chunk frames are the common case and need no concatenation pass.
97
+ if (chunks.length === 1) return chunks[0];
98
+ if (chunks.length === 0) return new Uint8Array(0);
99
+ const out = new Uint8Array(total);
100
+ let offset = 0;
101
+ for (const chunk of chunks) {
102
+ out.set(chunk, offset);
103
+ offset += chunk.length;
104
+ }
105
+ return out;
106
+ }
107
+
108
+
109
+ export {
110
+ decompressLz4Block,
111
+ decompressLz4Frame
112
+ };
package/src/constants.js CHANGED
@@ -1,74 +1,74 @@
1
- export const DEFAULT_BASE_URL = "https://drednot.io";
2
- export const DEFAULT_GAME_VERSION = null;
3
- export const DEFAULT_NOTICE_VERSION = 17;
4
-
5
- export const encoder = new TextEncoder();
6
- export const decoder = new TextDecoder("utf-8");
7
-
8
- export const SHARED_CHACHA_MATERIAL = Uint8Array.from([
9
- 0x15, 0x85, 0x59, 0xc6, 0x26, 0xc4, 0x31, 0x32,
10
- 0xb8, 0xed, 0xb2, 0xfe, 0x5b, 0x87, 0x5e, 0x5e,
11
- 0x3d, 0xd9, 0xe3, 0xcc, 0xa3, 0x9b, 0xd6, 0x58,
12
- 0xe2, 0xdc, 0x0e, 0x8b, 0x96, 0xb7, 0x05, 0x88,
13
- 0x78, 0xd1, 0xd4, 0xa4, 0x19, 0x3c, 0xb9, 0x1e,
14
- 0xbe, 0x78, 0x18, 0x12
15
- ]);
16
-
17
- export const JOIN_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36";
18
- export const KEEPALIVE_INTERVAL_MS = 15_000;
19
- export const INITIAL_OUTFIT_MESSAGE = {
20
- type: 7,
21
- outfit: {
22
- color_body: 4695355,
23
- color_legs: 1393785,
24
- color_skin: 13212550,
25
- color_feet: 6242065,
26
- color_hair: 1118481,
27
- style_hair: 0,
28
- color_hat: 16716049,
29
- style_hat: 0,
30
- color_face_1: 8421504,
31
- style_face_1: 0,
32
- color_face_2: 8421504,
33
- style_face_2: 0
34
- }
35
- };
36
- export const KEEPALIVE_MESSAGE = {type: 2, msg: "/"};
37
-
38
- export const COMMAND_FIELDS = [
39
- "type", "n", "x", "y", "mx", "my", "vx", "vy",
40
- "jump", "jump_held", "drop", "act1", "act1_held", "exit", "act2", "act_alt", "act_alt_held",
41
- "wrench_mode", "turret_mode", "scr_w", "scr_h", "motion",
42
- "focus_ent", "config_ent", "tip_select", "inv_slot", "blur", "drag"
43
- ];
44
-
45
- export const COMMAND_DEFAULT_FORMATS = {
46
- type: "positive-fixint",
47
- n: "uint16",
48
- x: "float32",
49
- y: "float32",
50
- mx: "float32",
51
- my: "float32",
52
- vx: "float32",
53
- vy: "float32",
54
- jump: "bool",
55
- jump_held: "bool",
56
- drop: "bool",
57
- act1: "bool",
58
- act1_held: "bool",
59
- exit: "bool",
60
- act2: "bool",
61
- act_alt: "bool",
62
- act_alt_held: "bool",
63
- wrench_mode: "positive-fixint",
64
- turret_mode: "positive-fixint",
65
- scr_w: "uint16",
66
- scr_h: "uint16",
67
- motion: "float32",
68
- focus_ent: "nil",
69
- config_ent: "nil",
70
- tip_select: "nil",
71
- inv_slot: "positive-fixint",
72
- blur: "bool",
73
- drag: "nil"
74
- };
1
+ export const DEFAULT_BASE_URL = "https://drednot.io";
2
+ export const DEFAULT_GAME_VERSION = null;
3
+ export const DEFAULT_NOTICE_VERSION = 17;
4
+
5
+ export const encoder = new TextEncoder();
6
+ export const decoder = new TextDecoder("utf-8");
7
+
8
+ export const SHARED_CHACHA_MATERIAL = Uint8Array.from([
9
+ 0x15, 0x85, 0x59, 0xc6, 0x26, 0xc4, 0x31, 0x32,
10
+ 0xb8, 0xed, 0xb2, 0xfe, 0x5b, 0x87, 0x5e, 0x5e,
11
+ 0x3d, 0xd9, 0xe3, 0xcc, 0xa3, 0x9b, 0xd6, 0x58,
12
+ 0xe2, 0xdc, 0x0e, 0x8b, 0x96, 0xb7, 0x05, 0x88,
13
+ 0x78, 0xd1, 0xd4, 0xa4, 0x19, 0x3c, 0xb9, 0x1e,
14
+ 0xbe, 0x78, 0x18, 0x12
15
+ ]);
16
+
17
+ export const JOIN_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36";
18
+ export const KEEPALIVE_INTERVAL_MS = 15_000;
19
+ export const INITIAL_OUTFIT_MESSAGE = {
20
+ type: 7,
21
+ outfit: {
22
+ color_body: 4695355,
23
+ color_legs: 1393785,
24
+ color_skin: 13212550,
25
+ color_feet: 6242065,
26
+ color_hair: 1118481,
27
+ style_hair: 0,
28
+ color_hat: 16716049,
29
+ style_hat: 0,
30
+ color_face_1: 8421504,
31
+ style_face_1: 0,
32
+ color_face_2: 8421504,
33
+ style_face_2: 0
34
+ }
35
+ };
36
+ export const KEEPALIVE_MESSAGE = {type: 2, msg: "/"};
37
+
38
+ export const COMMAND_FIELDS = [
39
+ "type", "n", "x", "y", "mx", "my", "vx", "vy",
40
+ "jump", "jump_held", "drop", "act1", "act1_held", "exit", "act2", "act_alt", "act_alt_held",
41
+ "wrench_mode", "turret_mode", "scr_w", "scr_h", "motion",
42
+ "focus_ent", "config_ent", "tip_select", "inv_slot", "blur", "drag"
43
+ ];
44
+
45
+ export const COMMAND_DEFAULT_FORMATS = {
46
+ type: "positive-fixint",
47
+ n: "uint16",
48
+ x: "float32",
49
+ y: "float32",
50
+ mx: "float32",
51
+ my: "float32",
52
+ vx: "float32",
53
+ vy: "float32",
54
+ jump: "bool",
55
+ jump_held: "bool",
56
+ drop: "bool",
57
+ act1: "bool",
58
+ act1_held: "bool",
59
+ exit: "bool",
60
+ act2: "bool",
61
+ act_alt: "bool",
62
+ act_alt_held: "bool",
63
+ wrench_mode: "positive-fixint",
64
+ turret_mode: "positive-fixint",
65
+ scr_w: "uint16",
66
+ scr_h: "uint16",
67
+ motion: "float32",
68
+ focus_ent: "nil",
69
+ config_ent: "nil",
70
+ tip_select: "nil",
71
+ inv_slot: "positive-fixint",
72
+ blur: "bool",
73
+ drag: "nil"
74
+ };
@@ -1,9 +1,9 @@
1
- import type {ChaCha20Backend} from "./chacha.js";
2
-
3
- /**
4
- * Node-only. Installs OpenSSL's `chacha20` as the ChaCha20 backend when it is
5
- * available. Returns false when the cipher is not present in this build.
6
- */
7
- export function enableNativeChaCha20(): boolean;
8
-
9
- export const nativeChaCha20: ChaCha20Backend;
1
+ import type {ChaCha20Backend} from "./chacha.js";
2
+
3
+ /**
4
+ * Node-only. Installs OpenSSL's `chacha20` as the ChaCha20 backend when it is
5
+ * available. Returns false when the cipher is not present in this build.
6
+ */
7
+ export function enableNativeChaCha20(): boolean;
8
+
9
+ export const nativeChaCha20: ChaCha20Backend;
@@ -1,48 +1,48 @@
1
- // Node-only ChaCha20 backend.
2
- //
3
- // OpenSSL exposes a raw `chacha20` stream cipher whose keystream matches
4
- // src/crypto/chacha.js exactly, and it is roughly 27x faster on 64 KB payloads.
5
- // This module is imported only from src/node.js so that browser bundles never
6
- // encounter a `node:crypto` specifier.
7
- import {createCipheriv, getCiphers} from "node:crypto";
8
-
9
- import {installNativeChaCha20} from "./chacha.js";
10
-
11
- // OpenSSL takes a 16-byte IV: a 32-bit little-endian block counter followed by
12
- // the 12-byte nonce. The JS implementation starts its counter at zero.
13
- function buildIv(nonce) {
14
- const iv = new Uint8Array(16);
15
- iv.set(nonce, 4);
16
- return iv;
17
- }
18
-
19
- function nativeChaCha20(key, nonce, out, source) {
20
- try {
21
- const cipher = createCipheriv("chacha20", key, buildIv(nonce));
22
- const input = source ?? new Uint8Array(out.length);
23
- const head = cipher.update(input);
24
- const tail = cipher.final();
25
- if (head.length) out.set(head, 0);
26
- if (tail.length) out.set(tail, head.length);
27
- return out;
28
- } catch (_) {
29
- // Fall back to the portable implementation.
30
- return null;
31
- }
32
- }
33
-
34
- function enableNativeChaCha20() {
35
- try {
36
- if (!getCiphers().includes("chacha20")) return false;
37
- } catch (_) {
38
- return false;
39
- }
40
- installNativeChaCha20(nativeChaCha20);
41
- return true;
42
- }
43
-
44
-
45
- export {
46
- enableNativeChaCha20,
47
- nativeChaCha20
48
- };
1
+ // Node-only ChaCha20 backend.
2
+ //
3
+ // OpenSSL exposes a raw `chacha20` stream cipher whose keystream matches
4
+ // src/crypto/chacha.js exactly, and it is roughly 27x faster on 64 KB payloads.
5
+ // This module is imported only from src/node.js so that browser bundles never
6
+ // encounter a `node:crypto` specifier.
7
+ import {createCipheriv, getCiphers} from "node:crypto";
8
+
9
+ import {installNativeChaCha20} from "./chacha.js";
10
+
11
+ // OpenSSL takes a 16-byte IV: a 32-bit little-endian block counter followed by
12
+ // the 12-byte nonce. The JS implementation starts its counter at zero.
13
+ function buildIv(nonce) {
14
+ const iv = new Uint8Array(16);
15
+ iv.set(nonce, 4);
16
+ return iv;
17
+ }
18
+
19
+ function nativeChaCha20(key, nonce, out, source) {
20
+ try {
21
+ const cipher = createCipheriv("chacha20", key, buildIv(nonce));
22
+ const input = source ?? new Uint8Array(out.length);
23
+ const head = cipher.update(input);
24
+ const tail = cipher.final();
25
+ if (head.length) out.set(head, 0);
26
+ if (tail.length) out.set(tail, head.length);
27
+ return out;
28
+ } catch (_) {
29
+ // Fall back to the portable implementation.
30
+ return null;
31
+ }
32
+ }
33
+
34
+ function enableNativeChaCha20() {
35
+ try {
36
+ if (!getCiphers().includes("chacha20")) return false;
37
+ } catch (_) {
38
+ return false;
39
+ }
40
+ installNativeChaCha20(nativeChaCha20);
41
+ return true;
42
+ }
43
+
44
+
45
+ export {
46
+ enableNativeChaCha20,
47
+ nativeChaCha20
48
+ };
@@ -1,25 +1,25 @@
1
- export function decryptPayload(wireBytes: Uint8Array | ArrayBuffer | number[], worldId: number, seed: number): Uint8Array;
2
-
3
- export function chacha20(key: Uint8Array, nonce: Uint8Array, length: number): Uint8Array;
4
-
5
- /** Generates the keystream and XORs it with `data` in a single pass. */
6
- export function chacha20Xor(key: Uint8Array, nonce: Uint8Array, data: Uint8Array): Uint8Array;
7
-
8
- export function rotl32(x: number, n: number): number;
9
-
10
- export function rotr32(x: number, n: number): number;
11
-
12
- export function quarterRound(state: Uint32Array, a: number, b: number, c: number, d: number): void;
13
-
14
- export type ChaCha20Backend = (
15
- key: Uint8Array,
16
- nonce: Uint8Array,
17
- out: Uint8Array,
18
- source?: Uint8Array
19
- ) => Uint8Array | null;
20
-
21
- /**
22
- * Installs a faster backend for large payloads. Node's entry point wires this to
23
- * OpenSSL's `chacha20`; pass null to restore the portable implementation.
24
- */
25
- export function installNativeChaCha20(cipherFactory: ChaCha20Backend | null): void;
1
+ export function decryptPayload(wireBytes: Uint8Array | ArrayBuffer | number[], worldId: number, seed: number): Uint8Array;
2
+
3
+ export function chacha20(key: Uint8Array, nonce: Uint8Array, length: number): Uint8Array;
4
+
5
+ /** Generates the keystream and XORs it with `data` in a single pass. */
6
+ export function chacha20Xor(key: Uint8Array, nonce: Uint8Array, data: Uint8Array): Uint8Array;
7
+
8
+ export function rotl32(x: number, n: number): number;
9
+
10
+ export function rotr32(x: number, n: number): number;
11
+
12
+ export function quarterRound(state: Uint32Array, a: number, b: number, c: number, d: number): void;
13
+
14
+ export type ChaCha20Backend = (
15
+ key: Uint8Array,
16
+ nonce: Uint8Array,
17
+ out: Uint8Array,
18
+ source?: Uint8Array
19
+ ) => Uint8Array | null;
20
+
21
+ /**
22
+ * Installs a faster backend for large payloads. Node's entry point wires this to
23
+ * OpenSSL's `chacha20`; pass null to restore the portable implementation.
24
+ */
25
+ export function installNativeChaCha20(cipherFactory: ChaCha20Backend | null): void;