Skip to content

Commit

Permalink
Switch built-in module imports to getBuiltinModule(…).
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarron committed Aug 30, 2024
1 parent 9521f7c commit ff5f6eb
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 44 deletions.
Binary file modified bun.lockb
Binary file not shown.
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@types/three": "^0.165.0",
"@types/web-bluetooth": "^0.0.20",
"comlink": "^4.4.1",
"getbuiltinmodule-ponyfill": "^1.0.0",
"random-uint-below": "v3.3.0",
"three": "^0.165.0"
},
Expand All @@ -68,7 +69,7 @@
"@types/chai": "^4.3.16",
"@types/dom-speech-recognition": "^0.0.4",
"@types/mocha": "^10.0.7",
"@types/node": "^20.14.9",
"@types/node": "^22.5.1",
"@types/yargs": "^17.0.32",
"@web/dev-server-esbuild": "^1.0.2",
"@web/test-runner": "^0.18.2",
Expand All @@ -81,7 +82,7 @@
"playwright": "^1.45.0",
"tsup": "^8.1.0",
"typedoc": "^0.26.2",
"typescript": "^5.5.2",
"typescript": "^5.5.4",
"yargs": "^17.7.2"
},
"minimalDevDependencies": [
Expand All @@ -92,14 +93,10 @@
"yargs"
],
"engines": {
"node": ">=19",
"node": ">=20.16.0",
"bun": ">=1.0.30"
},
"files": [
"./dist/bin/",
"./dist/lib/",
"*/index.d.ts"
],
"files": ["./dist/bin/", "./dist/lib/", "*/index.d.ts"],
"scripts": {
"default": "make default",
"build": "make build",
Expand Down
12 changes: 10 additions & 2 deletions script/test/src/import-restrictions/allowedImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,14 @@ export const mainAllowedImports: AllowedImports = {
static: ["three", "src/cubing/vendor/mit/three"],
},
"src/cubing/vendor": {
static: ["src/cubing/alg", "random-uint-below"],
static: [
"src/cubing/alg",
"random-uint-below",
"getbuiltinmodule-ponyfill",
],
},
"src/cubing/vendor/apache/comlink-everywhere": {
static: ["comlink"],
static: ["comlink", "getbuiltinmodule-ponyfill"],
},
"src/cubing/vendor/mit/cs0x7f/cstimer/src/js/scramble/444-solver.ts": {
static: ["src/cubing/search/cubing-private"],
Expand All @@ -141,6 +145,9 @@ export const mainAllowedImports: AllowedImports = {
"src/sites": {
static: ["src/cubing", "jszip", "three"],
},
"src/sites/experiments.cubing.net/cubing.js/rust/wasm": {
static: ["getbuiltinmodule-ponyfill"],
},
};

// This is a separate definition because the `spec` files are interleaved with source files, and it's easier to run a separate check for them.
Expand All @@ -154,6 +161,7 @@ export const specAllowedImports: AllowedImports = {
"comlink",
"three",
"random-uint-below",
"getbuiltinmodule-ponyfill",
],
},
"src/test/chai-workarounds": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cube3x3x3, type PuzzleLoader } from "../../../puzzles";
import type { HintFaceletStyle } from "../../model/props/puzzle/display/HintFaceletProp";
import type { FaceletScale } from "../../model/props/puzzle/display/FaceletScaleProp";
import type { HintFaceletStyle } from "../../model/props/puzzle/display/HintFaceletProp";
import { Cube3D, type Cube3DOptions } from "../../views/3D/puzzles/Cube3D";
import { PG3D } from "../../views/3D/puzzles/PG3D";

Expand Down
13 changes: 5 additions & 8 deletions src/cubing/vendor/apache/comlink-everywhere/inside/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { expose as comlinkExpose } from "comlink";
import { getBuiltinModule } from "getbuiltinmodule-ponyfill";
import nodeEndpoint from "../node-adapter";

const useNodeWorkarounds =
typeof globalThis.Worker === "undefined" &&
typeof (globalThis as any).WorkerNavigator === "undefined";

// Mangled so that bundlers don't try to inline the source.
const worker_threads_mangled = "node:w-orker-_threa-ds";
const worker_threads_unmangled = () => worker_threads_mangled.replace(/-/g, "");

export async function nodeEndpointPort(): Promise<
Worker & {
nodeWorker?: import("node:worker_threads").Worker;
}
> {
const { parentPort } = await import(
/* @vite-ignore */ worker_threads_unmangled()
).catch();
return nodeEndpoint(parentPort);
const { parentPort } = await getBuiltinModule("node:worker_threads");
return nodeEndpoint(
parentPort as unknown as import("node:worker_threads").Worker,
);
}

export function expose(api: any) {
Expand Down
10 changes: 2 additions & 8 deletions src/cubing/vendor/apache/comlink-everywhere/outside/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { getBuiltinModule } from "getbuiltinmodule-ponyfill";
import nodeEndpoint from "../node-adapter";

export { wrap } from "comlink";
// Mangled so that bundlers don't try to inline the source.

const worker_threads_mangled = "node:w-orker-_threa-ds";
const worker_threads_unmangled = () => worker_threads_mangled.replace(/-/g, "");

const useNodeWorkarounds =
typeof globalThis.Worker === "undefined" &&
typeof (globalThis as any).WorkerNavigator === "undefined";
Expand All @@ -14,9 +10,7 @@ async function nodeWorker(
source: string | URL,
options?: { eval?: boolean },
): Promise<Worker> {
const { Worker: NodeWorker } = await import(
/* @vite-ignore */ worker_threads_unmangled()
);
const { Worker: NodeWorker } = await getBuiltinModule("node:worker_threads");
const worker = new NodeWorker(source, options);
worker.unref();
return nodeEndpoint(worker);
Expand Down
7 changes: 3 additions & 4 deletions src/cubing/vendor/mpl/twsearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import "./chunk-FTOVDLBG.js";
import { Alg } from "../../../alg";

// .temp/rust-wasm/twsearch_wasm.js
var node_fs_promises_mangled = "node:-fs/pr-omises";
var node_fs_promises_unmangled = () => node_fs_promises_mangled.replace(/-/g, "");
import { getBuiltinModule } from "getbuiltinmodule-ponyfill";
var wasm;
var cachedTextDecoder = typeof TextDecoder !== "undefined" ? new TextDecoder("utf-8", { ignoreBOM: true, fatal: true }) : { decode: () => {
throw Error("TextDecoder not available");
Expand Down Expand Up @@ -447,7 +446,7 @@ async function __wbg_init(input) {
if (!(e instanceof TypeError)) {
throw e;
}
input = await (await import(node_fs_promises_unmangled())).readFile(input);
input = await (await getBuiltinModule("node:fs/promises")).readFile(input);
}
}
__wbg_init_memory(imports);
Expand All @@ -460,7 +459,7 @@ var twsearch_wasm_default = __wbg_init;
var cachedInitWrapper;
async function initWrapper() {
await (cachedInitWrapper ??= (async () => {
const wasmUint8Array = (await import("./twsearch_wasm_bg-V4F3SIUO.js")).default;
const wasmUint8Array = (await import("./twsearch_wasm_bg-AVUYD5YC.js")).default;
await twsearch_wasm_default(wasmUint8Array.buffer);
})());
}
Expand Down
3 changes: 3 additions & 0 deletions src/cubing/vendor/mpl/twsearch/twsearch_wasm_bg-AVUYD5YC.js
Git LFS file not shown
3 changes: 0 additions & 3 deletions src/cubing/vendor/mpl/twsearch/twsearch_wasm_bg-V4F3SIUO.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly internal_init: () => void;
readonly invert_alg: (a: number, b: number, c: number) => void;
readonly greet: () => void;
readonly internal_init: () => void;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_malloc: (a: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number) => number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Mangled so that bundlers don't try to inline the source.
const node_fs_promises_mangled = "node:-fs/pr-omises";
const node_fs_promises_unmangled = () => node_fs_promises_mangled.replace(/-/g, "");
import { getBuiltinModule } from "getbuiltinmodule-ponyfill";

let wasm;

Expand Down Expand Up @@ -258,7 +256,7 @@ async function init(input) {
if (!(e instanceof TypeError)) {
throw e;
}
input = await (await import(node_fs_promises_unmangled())).readFile(input);
input = await getBuiltinModule("node:fs/promises").readFile(input);
}
}

Expand All @@ -269,5 +267,5 @@ async function init(input) {
return finalizeInit(instance, module);
}

export { initSync }
export { initSync };
export default init;
Git LFS file not shown
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* tslint:disable */
/* eslint-disable */
declare const memory: WebAssembly.Memory;
declare function internal_init(): void;
declare function invert_alg(a: number, b: number, c: number): void;
declare function greet(): void;
declare function internal_init(): void;
declare function __wbindgen_add_to_stack_pointer(a: number): number;
declare function __wbindgen_malloc(a: number): number;
declare function __wbindgen_realloc(a: number, b: number, c: number): number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
<!-- From: https://github.com/lgarron/minimal-html-style (v1.0.0) -->
<meta name="viewport" content="width=device-width, initial-scale=0.75">
<link rel="stylesheet" href="./index.css">
<script type="importmap">
{ "imports": { "cubing/alg": "https://cdn.cubing.net/js/cubing/alg" } }
</script>
<script src="./index.js" type="module"></script>
</head>

<body>
<textarea class="input" placeholder="Enter an alg" spellcheck="false"></textarea>
<br>
<button id="use-wr">Use the WR</button>
<button id="use-40x40x40x40-solution">Use a huge alg</button>

<fieldset id="rust">
<legend>Rust<span class="duration"></span></legend>
Expand Down

0 comments on commit ff5f6eb

Please sign in to comment.