@cubingcenter/scrambler 0.7.0 → 0.8.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.
- package/README.md +15 -4
- package/dist/core/api.d.ts.map +1 -1
- package/dist/core/api.js +10 -2
- package/dist/puzzles/cube2/solver/browser-loader.d.ts +4 -7
- package/dist/puzzles/cube2/solver/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/cube2/solver/browser-loader.js +42 -14
- package/dist/puzzles/cube2/solver/generated-assets.d.ts.map +1 -1
- package/dist/puzzles/cube2/solver/generated-assets.js +8 -3
- package/dist/puzzles/cube3/solver/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/browser-loader.js +42 -20
- package/dist/puzzles/cube3/solver/generated-assets.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/generated-assets.js +9 -3
- package/dist/puzzles/cube3/solver/phase1.d.ts +19 -0
- package/dist/puzzles/cube3/solver/phase1.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/phase1.js +29 -6
- package/dist/puzzles/cube3/solver/phase2.d.ts +28 -0
- package/dist/puzzles/cube3/solver/phase2.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/phase2.js +56 -15
- package/dist/puzzles/cube3/solver/tables.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/tables.js +14 -12
- package/dist/puzzles/cube4/browser-worker-pool.d.ts +82 -0
- package/dist/puzzles/cube4/browser-worker-pool.d.ts.map +1 -0
- package/dist/puzzles/cube4/browser-worker-pool.js +205 -0
- package/dist/puzzles/cube4/browser-worker.d.ts +2 -0
- package/dist/puzzles/cube4/browser-worker.d.ts.map +1 -0
- package/dist/puzzles/cube4/browser-worker.js +31 -0
- package/dist/puzzles/cube4/index.d.ts +7 -0
- package/dist/puzzles/cube4/index.d.ts.map +1 -1
- package/dist/puzzles/cube4/index.js +4 -0
- package/dist/puzzles/cube4/moves.d.ts.map +1 -1
- package/dist/puzzles/cube4/moves.js +122 -30
- package/dist/puzzles/cube4/reduction/browser-loader.d.ts +26 -1
- package/dist/puzzles/cube4/reduction/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/browser-loader.js +212 -48
- package/dist/puzzles/cube4/reduction/center2.d.ts +24 -0
- package/dist/puzzles/cube4/reduction/center2.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/center2.js +99 -1
- package/dist/puzzles/cube4/reduction/finish3.d.ts +53 -0
- package/dist/puzzles/cube4/reduction/finish3.d.ts.map +1 -0
- package/dist/puzzles/cube4/reduction/finish3.js +393 -0
- package/dist/puzzles/cube4/reduction/generated/hc-manifest.json +6 -0
- package/dist/puzzles/cube4/reduction/hc-pdb.d.ts +5 -3
- package/dist/puzzles/cube4/reduction/hc-pdb.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/hc-pdb.js +30 -12
- package/dist/puzzles/cube4/reduction/node-loader.d.ts +10 -1
- package/dist/puzzles/cube4/reduction/node-loader.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/node-loader.js +170 -36
- package/dist/puzzles/cube4/reduction/pair-transversal.d.ts +28 -0
- package/dist/puzzles/cube4/reduction/pair-transversal.d.ts.map +1 -0
- package/dist/puzzles/cube4/reduction/pair-transversal.js +107 -0
- package/dist/puzzles/cube4/reduction/phase3-search.d.ts +33 -30
- package/dist/puzzles/cube4/reduction/phase3-search.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/phase3-search.js +211 -104
- package/dist/puzzles/cube4/reduction/rotation-fix.d.ts +28 -0
- package/dist/puzzles/cube4/reduction/rotation-fix.d.ts.map +1 -0
- package/dist/puzzles/cube4/reduction/rotation-fix.js +212 -0
- package/dist/puzzles/cube4/reduction/solve.d.ts +72 -13
- package/dist/puzzles/cube4/reduction/solve.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/solve.js +374 -92
- package/dist/puzzles/cube4/scramble-cache.d.ts +93 -0
- package/dist/puzzles/cube4/scramble-cache.d.ts.map +1 -0
- package/dist/puzzles/cube4/scramble-cache.js +150 -0
- package/dist/puzzles/cube4/scrambler.d.ts +112 -18
- package/dist/puzzles/cube4/scrambler.d.ts.map +1 -1
- package/dist/puzzles/cube4/scrambler.js +271 -83
- package/dist/puzzles/cube4/worker-pool.d.ts +139 -0
- package/dist/puzzles/cube4/worker-pool.d.ts.map +1 -0
- package/dist/puzzles/cube4/worker-pool.js +405 -0
- package/dist/puzzles/cube4/worker-protocol.d.ts +58 -0
- package/dist/puzzles/cube4/worker-protocol.d.ts.map +1 -0
- package/dist/puzzles/cube4/worker-protocol.js +58 -0
- package/dist/puzzles/cube4/worker.d.ts +26 -0
- package/dist/puzzles/cube4/worker.d.ts.map +1 -0
- package/dist/puzzles/cube4/worker.js +49 -0
- package/dist/puzzles/pyraminx/solver/browser-loader.d.ts +4 -8
- package/dist/puzzles/pyraminx/solver/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/pyraminx/solver/browser-loader.js +44 -15
- package/dist/puzzles/pyraminx/solver/generated-assets.d.ts.map +1 -1
- package/dist/puzzles/pyraminx/solver/generated-assets.js +8 -3
- package/dist/puzzles/shared/compressed-fetch.d.ts +37 -1
- package/dist/puzzles/shared/compressed-fetch.d.ts.map +1 -1
- package/dist/puzzles/shared/compressed-fetch.js +226 -31
- package/dist/puzzles/shared/load-once.d.ts +16 -0
- package/dist/puzzles/shared/load-once.d.ts.map +1 -0
- package/dist/puzzles/shared/load-once.js +11 -0
- package/dist/puzzles/skewb/solver/browser-loader.d.ts +4 -11
- package/dist/puzzles/skewb/solver/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/skewb/solver/browser-loader.js +44 -16
- package/dist/puzzles/skewb/solver/generated-assets.d.ts.map +1 -1
- package/dist/puzzles/skewb/solver/generated-assets.js +8 -3
- package/dist/puzzles/sq1/solver/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/sq1/solver/browser-loader.js +58 -26
- package/dist-cjs/core/api.js +10 -2
- package/dist-cjs/puzzles/cube2/solver/browser-loader.js +42 -14
- package/dist-cjs/puzzles/cube2/solver/generated-assets.js +8 -3
- package/dist-cjs/puzzles/cube3/solver/browser-loader.js +42 -20
- package/dist-cjs/puzzles/cube3/solver/generated-assets.js +9 -3
- package/dist-cjs/puzzles/cube3/solver/phase1.js +29 -6
- package/dist-cjs/puzzles/cube3/solver/phase2.js +56 -15
- package/dist-cjs/puzzles/cube3/solver/tables.js +14 -12
- package/dist-cjs/puzzles/cube4/browser-worker-pool.js +211 -0
- package/dist-cjs/puzzles/cube4/index.js +21 -1
- package/dist-cjs/puzzles/cube4/moves.js +122 -30
- package/dist-cjs/puzzles/cube4/reduction/browser-loader.js +212 -48
- package/dist-cjs/puzzles/cube4/reduction/center2.js +102 -2
- package/dist-cjs/puzzles/cube4/reduction/finish3.js +398 -0
- package/dist-cjs/puzzles/cube4/reduction/generated/hc-manifest.json +6 -0
- package/dist-cjs/puzzles/cube4/reduction/hc-pdb.js +31 -12
- package/dist-cjs/puzzles/cube4/reduction/node-loader.js +171 -36
- package/dist-cjs/puzzles/cube4/reduction/pair-transversal.js +112 -0
- package/dist-cjs/puzzles/cube4/reduction/phase3-search.js +211 -104
- package/dist-cjs/puzzles/cube4/reduction/rotation-fix.js +216 -0
- package/dist-cjs/puzzles/cube4/reduction/solve.js +374 -88
- package/dist-cjs/puzzles/cube4/scramble-cache.js +156 -0
- package/dist-cjs/puzzles/cube4/scrambler.js +271 -80
- package/dist-cjs/puzzles/cube4/worker-pool.js +447 -0
- package/dist-cjs/puzzles/cube4/worker-protocol.js +62 -0
- package/dist-cjs/puzzles/pyraminx/solver/browser-loader.js +44 -15
- package/dist-cjs/puzzles/pyraminx/solver/generated-assets.js +8 -3
- package/dist-cjs/puzzles/shared/compressed-fetch.js +229 -31
- package/dist-cjs/puzzles/shared/load-once.js +14 -0
- package/dist-cjs/puzzles/skewb/solver/browser-loader.js +44 -16
- package/dist-cjs/puzzles/skewb/solver/generated-assets.js +8 -3
- package/dist-cjs/puzzles/sq1/solver/browser-loader.js +58 -26
- package/docs/development.md +19 -7
- package/package.json +4 -3
|
@@ -1,22 +1,42 @@
|
|
|
1
1
|
import { PYRAMINX_SOLVER_TABLE_METADATA } from "./table-metadata.js";
|
|
2
2
|
import { fetchTableBuffer as fetchCompressedTableBuffer } from "../../shared/compressed-fetch.js";
|
|
3
|
+
const TABLES_DIR = "puzzles/pyraminx/solver/generated";
|
|
4
|
+
const CACHE_NAME = "cubingcenter-scrambler-tables-v1";
|
|
3
5
|
let cachedBrowserTables;
|
|
6
|
+
let cachedBrowserTablesPromise;
|
|
4
7
|
export function getCachedBrowserTables() {
|
|
5
8
|
return cachedBrowserTables;
|
|
6
9
|
}
|
|
7
10
|
export function clearCachedBrowserTables() {
|
|
8
11
|
cachedBrowserTables = undefined;
|
|
12
|
+
cachedBrowserTablesPromise = undefined;
|
|
9
13
|
}
|
|
10
14
|
export async function loadBrowserTables(baseUrl) {
|
|
11
15
|
if (cachedBrowserTables)
|
|
12
16
|
return cachedBrowserTables;
|
|
17
|
+
if (cachedBrowserTablesPromise) {
|
|
18
|
+
// One shared in-flight promise: concurrent calls trigger exactly one
|
|
19
|
+
// fetch pass (dedup).
|
|
20
|
+
return cachedBrowserTablesPromise;
|
|
21
|
+
}
|
|
22
|
+
const promise = fetchTables(baseUrl);
|
|
23
|
+
cachedBrowserTablesPromise = promise;
|
|
24
|
+
promise.then(() => { }, () => {
|
|
25
|
+
// A failed load must not poison the dedup slot: purge the rejected
|
|
26
|
+
// promise so a later call retries from scratch.
|
|
27
|
+
if (cachedBrowserTablesPromise === promise)
|
|
28
|
+
cachedBrowserTablesPromise = undefined;
|
|
29
|
+
});
|
|
30
|
+
return promise;
|
|
31
|
+
}
|
|
32
|
+
async function fetchTables(baseUrl) {
|
|
13
33
|
const resolved = resolveBaseUrl(baseUrl);
|
|
14
34
|
const manifest = await fetchManifest(resolved);
|
|
15
35
|
const [centersTable, permutationTable, orientationTable, policyTable] = await Promise.all([
|
|
16
|
-
fetchUint8Table(resolved, manifest.files.centersTable, PYRAMINX_SOLVER_TABLE_METADATA.centersTable
|
|
17
|
-
fetchUint16Table(resolved, manifest.files.permutationTable, PYRAMINX_SOLVER_TABLE_METADATA.permutationTable
|
|
18
|
-
fetchUint8Table(resolved, manifest.files.orientationTable, PYRAMINX_SOLVER_TABLE_METADATA.orientationTable
|
|
19
|
-
fetchUint8Table(resolved, manifest.files.policyTable, PYRAMINX_SOLVER_TABLE_METADATA.policyTable
|
|
36
|
+
fetchUint8Table(resolved, manifest.files.centersTable, PYRAMINX_SOLVER_TABLE_METADATA.centersTable),
|
|
37
|
+
fetchUint16Table(resolved, manifest.files.permutationTable, PYRAMINX_SOLVER_TABLE_METADATA.permutationTable),
|
|
38
|
+
fetchUint8Table(resolved, manifest.files.orientationTable, PYRAMINX_SOLVER_TABLE_METADATA.orientationTable),
|
|
39
|
+
fetchUint8Table(resolved, manifest.files.policyTable, PYRAMINX_SOLVER_TABLE_METADATA.policyTable)
|
|
20
40
|
]);
|
|
21
41
|
cachedBrowserTables = { centersTable, permutationTable, orientationTable, policyTable };
|
|
22
42
|
return cachedBrowserTables;
|
|
@@ -43,19 +63,28 @@ async function fetchManifest(baseUrl) {
|
|
|
43
63
|
throw new Error(`Unsupported pyraminx solver table manifest version: ${manifest.version}`);
|
|
44
64
|
return manifest;
|
|
45
65
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Fetch + verify with the SAME checks as node-loader.ts: byte length from
|
|
68
|
+
* the manifest entries, FNV-1a checksum against the table-metadata
|
|
69
|
+
* constants (the values node asserts against).
|
|
70
|
+
*/
|
|
71
|
+
async function fetchUint8Table(baseUrl, entry, meta) {
|
|
72
|
+
const buffer = await fetchCompressedTableBuffer(baseUrl, TABLES_DIR, entry.file, {
|
|
73
|
+
cacheName: CACHE_NAME,
|
|
74
|
+
compressed: entry.compressed !== false,
|
|
75
|
+
expectedBytes: entry.entries,
|
|
76
|
+
expectedFnv1a: { value: meta.checksum, elementSize: 1 }
|
|
77
|
+
});
|
|
52
78
|
return new Uint8Array(buffer);
|
|
53
79
|
}
|
|
54
|
-
async function fetchUint16Table(baseUrl, entry,
|
|
55
|
-
const buffer = await fetchCompressedTableBuffer(baseUrl, TABLES_DIR, entry.file, {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
80
|
+
async function fetchUint16Table(baseUrl, entry, meta) {
|
|
81
|
+
const buffer = await fetchCompressedTableBuffer(baseUrl, TABLES_DIR, entry.file, {
|
|
82
|
+
cacheName: CACHE_NAME,
|
|
83
|
+
compressed: entry.compressed !== false,
|
|
84
|
+
expectedBytes: entry.entries * 2,
|
|
85
|
+
expectedFnv1a: { value: meta.checksum, elementSize: 2 }
|
|
86
|
+
});
|
|
87
|
+
const values = new Uint16Array(entry.entries);
|
|
59
88
|
const view = new DataView(buffer);
|
|
60
89
|
for (let i = 0; i < values.length; i++)
|
|
61
90
|
values[i] = view.getUint16(i * 2, true);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generated-assets.d.ts","sourceRoot":"","sources":["../../../../src/puzzles/pyraminx/solver/generated-assets.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"generated-assets.d.ts","sourceRoot":"","sources":["../../../../src/puzzles/pyraminx/solver/generated-assets.ts"],"names":[],"mappings":"AAmBA,wBAAsB,iCAAiC,IAAI,OAAO,CAAC;IACjE,YAAY,EAAE,UAAU,CAAC;IACzB,gBAAgB,EAAE,WAAW,CAAC;IAC9B,gBAAgB,EAAE,UAAU,CAAC;IAC7B,WAAW,EAAE,UAAU,CAAC;CACzB,CAAC,CAWD"}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { getCachedBrowserTables } from "./browser-loader.js";
|
|
2
|
-
|
|
2
|
+
import { loadOnce } from "../../shared/load-once.js";
|
|
3
|
+
// One shared in-flight promise for the node fs load (same pattern as
|
|
4
|
+
// cube3/solver/generated-assets.ts): the previous `cached ??= await …` raced —
|
|
5
|
+
// concurrent callers interleaving on the await both ran `loadNodeTables()`
|
|
6
|
+
// (two filesystem reads, last assignment wins). On rejection the slot is
|
|
7
|
+
// purged so the next call can retry.
|
|
8
|
+
const nodeTablesSlot = { promise: undefined };
|
|
3
9
|
function isBrowser() {
|
|
4
10
|
return typeof self !== "undefined";
|
|
5
11
|
}
|
|
@@ -12,8 +18,7 @@ export async function loadGeneratedPyraminxSolverTables() {
|
|
|
12
18
|
"In browser environments, call `initializeScrambler(baseUrl)` " +
|
|
13
19
|
"before generating scrambles.");
|
|
14
20
|
}
|
|
15
|
-
|
|
16
|
-
return cachedTables;
|
|
21
|
+
return loadOnce(nodeTablesSlot, loadNodeTables);
|
|
17
22
|
}
|
|
18
23
|
async function loadNodeTables() {
|
|
19
24
|
const loader = await import(
|
|
@@ -1,5 +1,19 @@
|
|
|
1
|
+
/** FNV-1a expectations from the legacy (non-sha256) manifests. */
|
|
2
|
+
export interface ExpectedFnv1a {
|
|
3
|
+
/** FNV-1a 32-bit checksum (`>>> 0`) over the decoded numeric sequence. */
|
|
4
|
+
value: number;
|
|
5
|
+
/**
|
|
6
|
+
* Wire size of each hashed element: 1 = byte, 2 = uint16 LE, 4 = int32 LE.
|
|
7
|
+
* Mirrors how each node loader decodes values before hashing (`4` also
|
|
8
|
+
* covers the 3x3 int32 move tables: their hashed uint16 values are the
|
|
9
|
+
* same numbers, and XOR/`Math.imul` are sign-insensitive there).
|
|
10
|
+
*/
|
|
11
|
+
elementSize: 1 | 2 | 4;
|
|
12
|
+
/** Hash 1-byte elements as signed int8 (Square-1's int8 tables). */
|
|
13
|
+
signed?: boolean;
|
|
14
|
+
}
|
|
1
15
|
export interface FetchTableBufferOptions {
|
|
2
|
-
/** Cache API cache name to store responses in (used by
|
|
16
|
+
/** Cache API cache name to store responses in (used by all table loaders). */
|
|
3
17
|
cacheName?: string;
|
|
4
18
|
/**
|
|
5
19
|
* Whether the manifest advertises precompressed variants for this file.
|
|
@@ -7,6 +21,28 @@ export interface FetchTableBufferOptions {
|
|
|
7
21
|
* set to false to skip .br/.gz attempts entirely.
|
|
8
22
|
*/
|
|
9
23
|
compressed?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* sha256 hex digest of the UNCOMPRESSED file (from its manifest entry).
|
|
26
|
+
* Verified after decompression; a mismatch rejects with the file URL.
|
|
27
|
+
*/
|
|
28
|
+
expectedSha256?: string;
|
|
29
|
+
/** Exact byte length of the uncompressed file (from its manifest entry). */
|
|
30
|
+
expectedBytes?: number;
|
|
31
|
+
/**
|
|
32
|
+
* FNV-1a checksum from legacy manifests (cube2/cube3/pyraminx/skewb/sq1).
|
|
33
|
+
* Verified after the length check; a mismatch rejects with the file URL.
|
|
34
|
+
*/
|
|
35
|
+
expectedFnv1a?: ExpectedFnv1a;
|
|
10
36
|
}
|
|
11
37
|
export declare function fetchTableBuffer(baseUrl: string, dir: string, file: string, options?: FetchTableBufferOptions): Promise<ArrayBuffer>;
|
|
38
|
+
/**
|
|
39
|
+
* FNV-1a (32-bit) over the elements decoded from a wire buffer: the browser
|
|
40
|
+
* twin of the node loaders' `checksum(values)` (imul multiply, `>>> 0`
|
|
41
|
+
* result). `elementSize` mirrors each loader's decoding of the manifest
|
|
42
|
+
* checksum — see `ExpectedFnv1a`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function fnv1a32(buffer: ArrayBuffer, elementSize: 1 | 2 | 4, signed?: boolean): number;
|
|
45
|
+
/** sha256 hex of a buffer; WebCrypto when available, portable JS fallback otherwise. */
|
|
46
|
+
export declare function sha256Hex(buffer: ArrayBuffer): Promise<string>;
|
|
47
|
+
export declare function sha256HexJs(buffer: ArrayBuffer): string;
|
|
12
48
|
//# sourceMappingURL=compressed-fetch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compressed-fetch.d.ts","sourceRoot":"","sources":["../../../src/puzzles/shared/compressed-fetch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"compressed-fetch.d.ts","sourceRoot":"","sources":["../../../src/puzzles/shared/compressed-fetch.ts"],"names":[],"mappings":"AAeA,kEAAkE;AAClE,MAAM,WAAW,aAAa;IAC5B,0EAA0E;IAC1E,KAAK,EAAE,MAAM,CAAC;IACd;;;;;OAKG;IACH,WAAW,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACvB,oEAAoE;IACpE,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,uBAAuB;IACtC,8EAA8E;IAC9E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,4EAA4E;IAC5E,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC;CAC/B;AAkBD,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,MAAM,EACf,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,uBAA4B,GACpC,OAAO,CAAC,WAAW,CAAC,CAyDtB;AAmCD;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,MAAM,UAAQ,GAAG,MAAM,CAgB3F;AAED,wFAAwF;AACxF,wBAAsB,SAAS,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAYpE;AAcD,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAmEvD"}
|
|
@@ -4,6 +4,14 @@
|
|
|
4
4
|
//
|
|
5
5
|
// Node never uses this module: node-loader.ts reads the .bin files directly
|
|
6
6
|
// from the filesystem.
|
|
7
|
+
//
|
|
8
|
+
// Integrity: callers can pass the manifest's expectations — sha256 + byte
|
|
9
|
+
// length (4x4 manifests) or the FNV-1a32 checksums the older manifests ship
|
|
10
|
+
// — and every check runs on the FINAL (decompressed) bytes, i.e. exactly the
|
|
11
|
+
// .bin payload: identical checks to the node loaders. A payload served by
|
|
12
|
+
// the Cache API that fails verification is purged (`cache.delete`) and
|
|
13
|
+
// re-downloaded exactly once, so a corrupt cache entry heals instead of
|
|
14
|
+
// failing every load forever.
|
|
7
15
|
// DecompressionStream only guarantees "gzip"/"deflate"; "br" support varies,
|
|
8
16
|
// so probe once and never attempt formats the runtime cannot decompress.
|
|
9
17
|
const SUPPORTED_FORMATS = (() => {
|
|
@@ -23,46 +31,233 @@ const SUPPORTED_FORMATS = (() => {
|
|
|
23
31
|
})();
|
|
24
32
|
export async function fetchTableBuffer(baseUrl, dir, file, options = {}) {
|
|
25
33
|
const urlFor = (name) => `${baseUrl}/${dir}/${name}`;
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
const canonicalUrl = urlFor(file);
|
|
35
|
+
const cache = options.cacheName === undefined ? undefined : makeCacheChannel(options.cacheName);
|
|
36
|
+
// Cache API URLs that served the current download attempt; when the
|
|
37
|
+
// payload fails verification they are purged before the single retry.
|
|
38
|
+
let servedFromCache = [];
|
|
39
|
+
const get = async (url) => {
|
|
40
|
+
if (!cache)
|
|
41
|
+
return fetch(url);
|
|
42
|
+
const { response, fromCache } = await cache.fetch(url);
|
|
43
|
+
if (fromCache)
|
|
44
|
+
servedFromCache.push(url);
|
|
45
|
+
return response;
|
|
46
|
+
};
|
|
47
|
+
const download = async () => {
|
|
48
|
+
servedFromCache = [];
|
|
49
|
+
if (options.compressed !== false && SUPPORTED_FORMATS.length > 0) {
|
|
50
|
+
for (const [suffix, format] of SUPPORTED_FORMATS) {
|
|
51
|
+
try {
|
|
52
|
+
const response = await get(urlFor(`${file}.${suffix}`));
|
|
53
|
+
if (response.ok && response.body) {
|
|
54
|
+
const stream = response.body.pipeThrough(new DecompressionStream(format));
|
|
55
|
+
return await new Response(stream).arrayBuffer();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
// try the next candidate
|
|
34
60
|
}
|
|
35
61
|
}
|
|
36
|
-
catch {
|
|
37
|
-
// try the next candidate
|
|
38
|
-
}
|
|
39
62
|
}
|
|
63
|
+
const response = await get(canonicalUrl);
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
throw new Error(`Failed to load table ${canonicalUrl}: ${response.status} ${response.statusText}` +
|
|
66
|
+
(options.expectedBytes !== undefined ? ` (expected ${options.expectedBytes} bytes)` : ""));
|
|
67
|
+
}
|
|
68
|
+
return response.arrayBuffer();
|
|
69
|
+
};
|
|
70
|
+
let buffer = await download();
|
|
71
|
+
try {
|
|
72
|
+
await verifyTableBuffer(buffer, canonicalUrl, options);
|
|
40
73
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
74
|
+
catch (error) {
|
|
75
|
+
// A corrupt Cache API entry fails every load until it is purged: delete
|
|
76
|
+
// the cached copies that served this attempt and re-download exactly
|
|
77
|
+
// ONCE. A second verification failure (or a payload that never came
|
|
78
|
+
// from the cache) propagates unchanged.
|
|
79
|
+
if (!cache || servedFromCache.length === 0)
|
|
80
|
+
throw error;
|
|
81
|
+
await cache.purge(servedFromCache);
|
|
82
|
+
buffer = await download();
|
|
83
|
+
await verifyTableBuffer(buffer, canonicalUrl, options);
|
|
44
84
|
}
|
|
45
|
-
return
|
|
85
|
+
return buffer;
|
|
46
86
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Verify the downloaded payload against the manifest: byte length first
|
|
89
|
+
* (clearer truncation messages), then sha256 / FNV-1a. Runs on the
|
|
90
|
+
* decompressed bytes, so .br/.gz variants and the raw file are checked
|
|
91
|
+
* identically — and identically to the node loaders.
|
|
92
|
+
*/
|
|
93
|
+
async function verifyTableBuffer(buffer, url, options) {
|
|
94
|
+
if (options.expectedBytes !== undefined && buffer.byteLength !== options.expectedBytes) {
|
|
95
|
+
throw new Error(`Table size mismatch: ${url} (expected ${options.expectedBytes} bytes, got ${buffer.byteLength})`);
|
|
96
|
+
}
|
|
97
|
+
if (options.expectedSha256 !== undefined) {
|
|
98
|
+
const actual = await sha256Hex(buffer);
|
|
99
|
+
if (actual !== options.expectedSha256) {
|
|
100
|
+
throw new Error(`Table checksum mismatch: ${url} (expected sha256 ${options.expectedSha256}, got ${actual}, ${buffer.byteLength} bytes)`);
|
|
51
101
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
102
|
+
}
|
|
103
|
+
if (options.expectedFnv1a !== undefined) {
|
|
104
|
+
const spec = options.expectedFnv1a;
|
|
105
|
+
const actual = fnv1a32(buffer, spec.elementSize, spec.signed ?? false);
|
|
106
|
+
if (actual !== spec.value) {
|
|
107
|
+
throw new Error(`Table checksum mismatch: ${url} (expected fnv1a ${spec.value}, got ${actual}, ${buffer.byteLength} bytes)`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* FNV-1a (32-bit) over the elements decoded from a wire buffer: the browser
|
|
113
|
+
* twin of the node loaders' `checksum(values)` (imul multiply, `>>> 0`
|
|
114
|
+
* result). `elementSize` mirrors each loader's decoding of the manifest
|
|
115
|
+
* checksum — see `ExpectedFnv1a`.
|
|
116
|
+
*/
|
|
117
|
+
export function fnv1a32(buffer, elementSize, signed = false) {
|
|
118
|
+
if (buffer.byteLength % elementSize !== 0) {
|
|
119
|
+
throw new Error(`FNV-1a input length ${buffer.byteLength} is not a multiple of ${elementSize}-byte elements`);
|
|
120
|
+
}
|
|
121
|
+
let hash = 0x811c9dc5;
|
|
122
|
+
const view = new DataView(buffer);
|
|
123
|
+
const count = buffer.byteLength / elementSize;
|
|
124
|
+
for (let index = 0; index < count; index += 1) {
|
|
125
|
+
let value;
|
|
126
|
+
if (elementSize === 1)
|
|
127
|
+
value = signed ? view.getInt8(index) : view.getUint8(index);
|
|
128
|
+
else if (elementSize === 2)
|
|
129
|
+
value = view.getUint16(index * 2, true);
|
|
130
|
+
else
|
|
131
|
+
value = view.getInt32(index * 4, true);
|
|
132
|
+
hash ^= value;
|
|
133
|
+
hash = Math.imul(hash, 0x01000193);
|
|
134
|
+
}
|
|
135
|
+
return hash >>> 0;
|
|
136
|
+
}
|
|
137
|
+
/** sha256 hex of a buffer; WebCrypto when available, portable JS fallback otherwise. */
|
|
138
|
+
export async function sha256Hex(buffer) {
|
|
139
|
+
const subtle = globalThis.crypto?.subtle;
|
|
140
|
+
if (subtle) {
|
|
141
|
+
const digest = await subtle.digest("SHA-256", buffer);
|
|
142
|
+
const bytes = new Uint8Array(digest);
|
|
143
|
+
let hex = "";
|
|
144
|
+
for (let i = 0; i < bytes.length; i++)
|
|
145
|
+
hex += bytes[i].toString(16).padStart(2, "0");
|
|
146
|
+
return hex;
|
|
147
|
+
}
|
|
148
|
+
// Insecure contexts expose no SubtleCrypto; keep integrity checks working
|
|
149
|
+
// with a self-contained implementation instead of skipping verification.
|
|
150
|
+
return sha256HexJs(buffer);
|
|
151
|
+
}
|
|
152
|
+
// FIPS 180-4 SHA-256, used only when `crypto.subtle` is unavailable.
|
|
153
|
+
const SHA256_K = new Uint32Array([
|
|
154
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
155
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
156
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
157
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
158
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
159
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
160
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
161
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
162
|
+
]);
|
|
163
|
+
export function sha256HexJs(buffer) {
|
|
164
|
+
const bytes = new Uint8Array(buffer);
|
|
165
|
+
const paddedLength = (bytes.length + 72) & ~63;
|
|
166
|
+
const data = new Uint8Array(paddedLength);
|
|
167
|
+
data.set(bytes);
|
|
168
|
+
data[bytes.length] = 0x80;
|
|
169
|
+
const view = new DataView(data.buffer);
|
|
170
|
+
view.setUint32(paddedLength - 8, Math.floor(bytes.length / 0x20000000));
|
|
171
|
+
view.setUint32(paddedLength - 4, (bytes.length << 3) >>> 0);
|
|
172
|
+
let h0 = 0x6a09e667;
|
|
173
|
+
let h1 = 0xbb67ae85;
|
|
174
|
+
let h2 = 0x3c6ef372;
|
|
175
|
+
let h3 = 0xa54ff53a;
|
|
176
|
+
let h4 = 0x510e527f;
|
|
177
|
+
let h5 = 0x9b05688c;
|
|
178
|
+
let h6 = 0x1f83d9ab;
|
|
179
|
+
let h7 = 0x5be0cd19;
|
|
180
|
+
const w = new Int32Array(64);
|
|
181
|
+
for (let offset = 0; offset < paddedLength; offset += 64) {
|
|
182
|
+
for (let i = 0; i < 16; i += 1)
|
|
183
|
+
w[i] = view.getInt32(offset + i * 4);
|
|
184
|
+
for (let i = 16; i < 64; i += 1) {
|
|
185
|
+
const w15 = w[i - 15];
|
|
186
|
+
const w2 = w[i - 2];
|
|
187
|
+
const s0 = ((w15 >>> 7) | (w15 << 25)) ^ ((w15 >>> 18) | (w15 << 14)) ^ (w15 >>> 3);
|
|
188
|
+
const s1 = ((w2 >>> 17) | (w2 << 15)) ^ ((w2 >>> 19) | (w2 << 13)) ^ (w2 >>> 10);
|
|
189
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) | 0;
|
|
190
|
+
}
|
|
191
|
+
let a = h0;
|
|
192
|
+
let b = h1;
|
|
193
|
+
let c = h2;
|
|
194
|
+
let d = h3;
|
|
195
|
+
let e = h4;
|
|
196
|
+
let f = h5;
|
|
197
|
+
let g = h6;
|
|
198
|
+
let h = h7;
|
|
199
|
+
for (let i = 0; i < 64; i += 1) {
|
|
200
|
+
const s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
|
|
201
|
+
const ch = (e & f) ^ (~e & g);
|
|
202
|
+
const t1 = (h + s1 + ch + SHA256_K[i] + w[i]) | 0;
|
|
203
|
+
const s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
|
|
204
|
+
const maj = (a & b) ^ (a & c) ^ (b & c);
|
|
205
|
+
const t2 = (s0 + maj) | 0;
|
|
206
|
+
h = g;
|
|
207
|
+
g = f;
|
|
208
|
+
f = e;
|
|
209
|
+
e = (d + t1) | 0;
|
|
210
|
+
d = c;
|
|
211
|
+
c = b;
|
|
212
|
+
b = a;
|
|
213
|
+
a = (t1 + t2) | 0;
|
|
214
|
+
}
|
|
215
|
+
h0 = (h0 + a) | 0;
|
|
216
|
+
h1 = (h1 + b) | 0;
|
|
217
|
+
h2 = (h2 + c) | 0;
|
|
218
|
+
h3 = (h3 + d) | 0;
|
|
219
|
+
h4 = (h4 + e) | 0;
|
|
220
|
+
h5 = (h5 + f) | 0;
|
|
221
|
+
h6 = (h6 + g) | 0;
|
|
222
|
+
h7 = (h7 + h) | 0;
|
|
223
|
+
}
|
|
224
|
+
return [h0, h1, h2, h3, h4, h5, h6, h7]
|
|
225
|
+
.map((x) => (x >>> 0).toString(16).padStart(8, "0"))
|
|
226
|
+
.join("");
|
|
227
|
+
}
|
|
228
|
+
function makeCacheChannel(cacheName) {
|
|
229
|
+
if (typeof caches === "undefined") {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
async fetch(url) {
|
|
234
|
+
try {
|
|
235
|
+
const cache = await caches.open(cacheName);
|
|
236
|
+
const cached = await cache.match(url);
|
|
237
|
+
if (cached) {
|
|
238
|
+
return { response: cached, fromCache: true };
|
|
239
|
+
}
|
|
240
|
+
const response = await fetch(url);
|
|
241
|
+
if (response.ok) {
|
|
242
|
+
await cache.put(url, response.clone());
|
|
243
|
+
}
|
|
244
|
+
return { response, fromCache: false };
|
|
57
245
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
246
|
+
catch {
|
|
247
|
+
return { response: await fetch(url), fromCache: false };
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
async purge(urls) {
|
|
251
|
+
try {
|
|
252
|
+
const cache = await caches.open(cacheName);
|
|
253
|
+
for (const url of urls) {
|
|
254
|
+
await cache.delete(url);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
catch {
|
|
258
|
+
// Best effort: a failed purge must not mask the verification error
|
|
259
|
+
// that triggered it — the load still fails loudly either way.
|
|
61
260
|
}
|
|
62
|
-
return response;
|
|
63
|
-
}
|
|
64
|
-
catch {
|
|
65
|
-
return fetch(url);
|
|
66
261
|
}
|
|
67
262
|
};
|
|
68
263
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-in-flight-promise helper: concurrent cold calls share ONE underlying
|
|
3
|
+
* load (a single filesystem read / browser fetch pass) instead of racing
|
|
4
|
+
* several loads and clobbering each other's results. On rejection the slot
|
|
5
|
+
* is purged so the next call can retry — a failure is never cached forever.
|
|
6
|
+
*
|
|
7
|
+
* Used by `cube3/solver/tables.ts` (phase-1 / phase-2 / solver slots) and
|
|
8
|
+
* `cube3/solver/generated-assets.ts` (the node fs load: a plain
|
|
9
|
+
* `cached ??= await …` re-reads the filesystem when two callers — e.g. mixed
|
|
10
|
+
* phase-1 + phase-2 loads — interleave on the `await`).
|
|
11
|
+
*/
|
|
12
|
+
export interface LoadOnceSlot<T> {
|
|
13
|
+
promise: Promise<T> | undefined;
|
|
14
|
+
}
|
|
15
|
+
export declare function loadOnce<T>(slot: LoadOnceSlot<T>, load: () => Promise<T>): Promise<T>;
|
|
16
|
+
//# sourceMappingURL=load-once.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-once.d.ts","sourceRoot":"","sources":["../../../src/puzzles/shared/load-once.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;CACjC;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CASrF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface SkewbBrowserTables {
|
|
2
2
|
permutationTable: Uint16Array;
|
|
3
3
|
orientationTable: Uint16Array;
|
|
4
4
|
centersTable: Uint16Array;
|
|
@@ -6,15 +6,8 @@ export declare function getCachedBrowserTables(): {
|
|
|
6
6
|
evenCenterRank: Uint16Array;
|
|
7
7
|
orientClass: Uint8Array;
|
|
8
8
|
r7Sets: Uint16Array;
|
|
9
|
-
}
|
|
9
|
+
}
|
|
10
|
+
export declare function getCachedBrowserTables(): SkewbBrowserTables | undefined;
|
|
10
11
|
export declare function clearCachedBrowserTables(): void;
|
|
11
|
-
export declare function loadBrowserTables(baseUrl?: string): Promise<
|
|
12
|
-
permutationTable: Uint16Array;
|
|
13
|
-
orientationTable: Uint16Array;
|
|
14
|
-
centersTable: Uint16Array;
|
|
15
|
-
policyTable: Uint8Array;
|
|
16
|
-
evenCenterRank: Uint16Array;
|
|
17
|
-
orientClass: Uint8Array;
|
|
18
|
-
r7Sets: Uint16Array;
|
|
19
|
-
}>;
|
|
12
|
+
export declare function loadBrowserTables(baseUrl?: string): Promise<SkewbBrowserTables>;
|
|
20
13
|
//# sourceMappingURL=browser-loader.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser-loader.d.ts","sourceRoot":"","sources":["../../../../src/puzzles/skewb/solver/browser-loader.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"browser-loader.d.ts","sourceRoot":"","sources":["../../../../src/puzzles/skewb/solver/browser-loader.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,kBAAkB;IACjC,gBAAgB,EAAE,WAAW,CAAC;IAC9B,gBAAgB,EAAE,WAAW,CAAC;IAC9B,YAAY,EAAE,WAAW,CAAC;IAC1B,WAAW,EAAE,UAAU,CAAC;IACxB,cAAc,EAAE,WAAW,CAAC;IAC5B,WAAW,EAAE,UAAU,CAAC;IACxB,MAAM,EAAE,WAAW,CAAC;CACrB;AAKD,wBAAgB,sBAAsB,IAAI,kBAAkB,GAAG,SAAS,CAEvE;AAED,wBAAgB,wBAAwB,IAAI,IAAI,CAG/C;AAED,wBAAsB,iBAAiB,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAoBrF"}
|
|
@@ -1,25 +1,44 @@
|
|
|
1
1
|
import { SKEWB_SOLVER_TABLE_METADATA } from "./table-metadata.js";
|
|
2
2
|
import { fetchTableBuffer } from "../../shared/compressed-fetch.js";
|
|
3
3
|
const TABLES_DIR = "puzzles/skewb/solver/generated";
|
|
4
|
+
const CACHE_NAME = "cubingcenter-scrambler-tables-v1";
|
|
4
5
|
let cachedBrowserTables;
|
|
6
|
+
let cachedBrowserTablesPromise;
|
|
5
7
|
export function getCachedBrowserTables() {
|
|
6
8
|
return cachedBrowserTables;
|
|
7
9
|
}
|
|
8
10
|
export function clearCachedBrowserTables() {
|
|
9
11
|
cachedBrowserTables = undefined;
|
|
12
|
+
cachedBrowserTablesPromise = undefined;
|
|
10
13
|
}
|
|
11
14
|
export async function loadBrowserTables(baseUrl) {
|
|
12
15
|
if (cachedBrowserTables) {
|
|
13
16
|
return cachedBrowserTables;
|
|
14
17
|
}
|
|
18
|
+
if (cachedBrowserTablesPromise) {
|
|
19
|
+
// One shared in-flight promise: concurrent calls trigger exactly one
|
|
20
|
+
// fetch pass (dedup).
|
|
21
|
+
return cachedBrowserTablesPromise;
|
|
22
|
+
}
|
|
23
|
+
const promise = fetchTables(baseUrl);
|
|
24
|
+
cachedBrowserTablesPromise = promise;
|
|
25
|
+
promise.then(() => { }, () => {
|
|
26
|
+
// A failed load must not poison the dedup slot: purge the rejected
|
|
27
|
+
// promise so a later call retries from scratch.
|
|
28
|
+
if (cachedBrowserTablesPromise === promise)
|
|
29
|
+
cachedBrowserTablesPromise = undefined;
|
|
30
|
+
});
|
|
31
|
+
return promise;
|
|
32
|
+
}
|
|
33
|
+
async function fetchTables(baseUrl) {
|
|
15
34
|
const resolved = resolveBaseUrl(baseUrl);
|
|
16
35
|
const manifest = await fetchManifest(resolved);
|
|
17
36
|
const [permTable, orientTable, centersTable, helpers, policy] = await Promise.all([
|
|
18
|
-
fetchUint16Table(resolved, manifest.files.permutationTable, SKEWB_SOLVER_TABLE_METADATA.permutationTable
|
|
19
|
-
fetchUint16Table(resolved, manifest.files.orientationTable, SKEWB_SOLVER_TABLE_METADATA.orientationTable
|
|
20
|
-
fetchUint16Table(resolved, manifest.files.centersTable, SKEWB_SOLVER_TABLE_METADATA.centersTable
|
|
21
|
-
fetchUint8Table(resolved, manifest.files.helpers, SKEWB_SOLVER_TABLE_METADATA.helpers
|
|
22
|
-
fetchUint8Table(resolved, manifest.files.policyTable, SKEWB_SOLVER_TABLE_METADATA.policyTable
|
|
37
|
+
fetchUint16Table(resolved, manifest.files.permutationTable, SKEWB_SOLVER_TABLE_METADATA.permutationTable),
|
|
38
|
+
fetchUint16Table(resolved, manifest.files.orientationTable, SKEWB_SOLVER_TABLE_METADATA.orientationTable),
|
|
39
|
+
fetchUint16Table(resolved, manifest.files.centersTable, SKEWB_SOLVER_TABLE_METADATA.centersTable),
|
|
40
|
+
fetchUint8Table(resolved, manifest.files.helpers, SKEWB_SOLVER_TABLE_METADATA.helpers),
|
|
41
|
+
fetchUint8Table(resolved, manifest.files.policyTable, SKEWB_SOLVER_TABLE_METADATA.policyTable)
|
|
23
42
|
]);
|
|
24
43
|
const helpersOffset = SKEWB_SOLVER_TABLE_METADATA.helpers;
|
|
25
44
|
const evenCenterRank = new Uint16Array(helpersOffset.evenCenterRankBytes / 2);
|
|
@@ -65,22 +84,31 @@ async function fetchManifest(baseUrl) {
|
|
|
65
84
|
}
|
|
66
85
|
return await response.json();
|
|
67
86
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Fetch + verify with the SAME checks as node-loader.ts: byte length from
|
|
89
|
+
* the manifest entries, FNV-1a checksum against the table-metadata
|
|
90
|
+
* constants (the values node asserts against).
|
|
91
|
+
*/
|
|
92
|
+
async function fetchUint16Table(baseUrl, entry, meta) {
|
|
93
|
+
const buffer = await fetchTableBuffer(baseUrl, TABLES_DIR, entry.file, {
|
|
94
|
+
cacheName: CACHE_NAME,
|
|
95
|
+
compressed: entry.compressed !== false,
|
|
96
|
+
expectedBytes: entry.entries * 2,
|
|
97
|
+
expectedFnv1a: { value: meta.checksum, elementSize: 2 }
|
|
98
|
+
});
|
|
99
|
+
const values = new Uint16Array(entry.entries);
|
|
74
100
|
const view = new DataView(buffer);
|
|
75
101
|
for (let i = 0; i < values.length; i++) {
|
|
76
102
|
values[i] = view.getUint16(i * 2, true);
|
|
77
103
|
}
|
|
78
104
|
return values;
|
|
79
105
|
}
|
|
80
|
-
async function fetchUint8Table(baseUrl, entry,
|
|
81
|
-
const buffer = await fetchTableBuffer(baseUrl, TABLES_DIR, entry.file, {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
106
|
+
async function fetchUint8Table(baseUrl, entry, meta) {
|
|
107
|
+
const buffer = await fetchTableBuffer(baseUrl, TABLES_DIR, entry.file, {
|
|
108
|
+
cacheName: CACHE_NAME,
|
|
109
|
+
compressed: entry.compressed !== false,
|
|
110
|
+
expectedBytes: entry.entries,
|
|
111
|
+
expectedFnv1a: { value: meta.checksum, elementSize: 1 }
|
|
112
|
+
});
|
|
85
113
|
return new Uint8Array(buffer);
|
|
86
114
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generated-assets.d.ts","sourceRoot":"","sources":["../../../../src/puzzles/skewb/solver/generated-assets.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"generated-assets.d.ts","sourceRoot":"","sources":["../../../../src/puzzles/skewb/solver/generated-assets.ts"],"names":[],"mappings":"AAsBA,wBAAsB,8BAA8B,IAAI,OAAO,CAAC;IAC9D,gBAAgB,EAAE,WAAW,CAAC;IAC9B,gBAAgB,EAAE,WAAW,CAAC;IAC9B,YAAY,EAAE,WAAW,CAAC;IAC1B,WAAW,EAAE,UAAU,CAAC;IACxB,cAAc,EAAE,WAAW,CAAC;IAC5B,WAAW,EAAE,UAAU,CAAC;IACxB,MAAM,EAAE,WAAW,CAAC;CACrB,CAAC,CAWD"}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { getCachedBrowserTables } from "./browser-loader.js";
|
|
2
|
-
|
|
2
|
+
import { loadOnce } from "../../shared/load-once.js";
|
|
3
|
+
// One shared in-flight promise for the node fs load (same pattern as
|
|
4
|
+
// cube3/solver/generated-assets.ts): the previous `cached ??= await …` raced —
|
|
5
|
+
// concurrent callers interleaving on the await both ran `loadNodeTables()`
|
|
6
|
+
// (two filesystem reads, last assignment wins). On rejection the slot is
|
|
7
|
+
// purged so the next call can retry.
|
|
8
|
+
const nodeTablesSlot = { promise: undefined };
|
|
3
9
|
function isBrowser() {
|
|
4
10
|
return typeof self !== "undefined";
|
|
5
11
|
}
|
|
@@ -12,8 +18,7 @@ export async function loadGeneratedSkewbSolverTables() {
|
|
|
12
18
|
"In browser environments, call `initializeScrambler(baseUrl)` " +
|
|
13
19
|
"before generating scrambles.");
|
|
14
20
|
}
|
|
15
|
-
|
|
16
|
-
return cachedTables;
|
|
21
|
+
return loadOnce(nodeTablesSlot, loadNodeTables);
|
|
17
22
|
}
|
|
18
23
|
async function loadNodeTables() {
|
|
19
24
|
const loader = await import(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser-loader.d.ts","sourceRoot":"","sources":["../../../../src/puzzles/sq1/solver/browser-loader.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"browser-loader.d.ts","sourceRoot":"","sources":["../../../../src/puzzles/sq1/solver/browser-loader.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAQvD,wBAAgB,sBAAsB,IAAI,mBAAmB,GAAG,SAAS,CAExE;AAED,wBAAgB,wBAAwB,IAAI,IAAI,CAG/C;AAED,wBAAsB,iBAAiB,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAoBtF"}
|