From 57801875db69d126c709490dfa3533fdd97ca21f Mon Sep 17 00:00:00 2001 From: boykopovar Date: Tue, 13 Oct 2026 14:02:46 +0300 Subject: [PATCH] refactor(web): load zip.js from pinned cdn version --- .gitmodules | 3 --- 3rdparty/zip.js | 1 - demo/web/archive.mjs | 2 +- demo/web/tests/archive.test.mjs | 5 ++++- demo/web/tests/cdn-loader.mjs | 9 +++++++++ tools/build_web.py | 17 ----------------- tools/test_web.py | 11 ++++++++++- 7 files changed, 24 insertions(+), 24 deletions(-) delete mode 160000 3rdparty/zip.js create mode 100644 demo/web/tests/cdn-loader.mjs diff --git a/.gitmodules b/.gitmodules index 2a525edc1..ddbcc2295 100644 --- a/.gitmodules +++ b/.gitmodules @@ -37,6 +37,3 @@ [submodule "3rdparty/libusb-cmake"] path = 3rdparty/libusb-cmake url = https://github.com/libusb/libusb-cmake.git -[submodule "3rdparty/zip.js"] - path = 3rdparty/zip.js - url = https://github.com/gildas-lormeau/zip.js.git diff --git a/3rdparty/zip.js b/3rdparty/zip.js deleted file mode 160000 index b608211a0..000000000 --- a/3rdparty/zip.js +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b608211a036192b665b5fd19589fa2454a315017 diff --git a/demo/web/archive.mjs b/demo/web/archive.mjs index 0c9befab3..eaad402cf 100644 --- a/demo/web/archive.mjs +++ b/demo/web/archive.mjs @@ -1,4 +1,4 @@ -import { BlobReader, Uint8ArrayReader, ZipReader, ZipWriter, configure } from "./vendor/zip/zip-no-worker.js"; +import { BlobReader, Uint8ArrayReader, ZipReader, ZipWriter, configure } from "https://cdn.jsdelivr.net/npm/@zip.js/zip.js@2.7.57/lib/zip-no-worker.js/+esm"; configure({ useWebWorkers: false, useCompressionStream: false, chunkSize: 65536 }); diff --git a/demo/web/tests/archive.test.mjs b/demo/web/tests/archive.test.mjs index 714646b6b..cd75fea7b 100644 --- a/demo/web/tests/archive.test.mjs +++ b/demo/web/tests/archive.test.mjs @@ -2,10 +2,13 @@ import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import { pathToFileURL } from "node:url"; import path from "node:path"; +import { register } from "node:module"; + +register("./cdn-loader.mjs", import.meta.url, { data: { url: process.env.ANYPS5_TEST_ZIP_URL, path: process.env.ANYPS5_TEST_ZIP_PATH } }); const site = name => pathToFileURL(path.join(process.argv[2], name)).href; const { Archive, memoryEstimate, MEMORY_LIMIT, packageArchive } = await import(site("archive.mjs")); -const { BlobReader, BlobWriter, Uint8ArrayReader, Uint8ArrayWriter, ZipReader, ZipWriter } = await import(site("vendor/zip/zip-no-worker.js")); +const { BlobReader, BlobWriter, Uint8ArrayReader, Uint8ArrayWriter, ZipReader, ZipWriter } = await import(process.env.ANYPS5_TEST_ZIP_URL); const { default: createRelinker } = await import(site("native/relinker_wasm.mjs")); const native = await createRelinker(); const executable = new Uint8Array(await readFile(path.join(process.argv[3], "main.elf"))); diff --git a/demo/web/tests/cdn-loader.mjs b/demo/web/tests/cdn-loader.mjs new file mode 100644 index 000000000..13bc480b2 --- /dev/null +++ b/demo/web/tests/cdn-loader.mjs @@ -0,0 +1,9 @@ +let library; + +export function initialize(data) { + library = data; +} + +export function resolve(specifier, context, nextResolve) { + return nextResolve(specifier === library.url ? library.path : specifier, context); +} diff --git a/tools/build_web.py b/tools/build_web.py index 5e2ad2ba4..849042afd 100644 --- a/tools/build_web.py +++ b/tools/build_web.py @@ -16,23 +16,6 @@ def copy_sources(output): for file in source.iterdir(): if file.is_file(): shutil.copy2(file, output / file.name) - library = ROOT / "3rdparty/zip.js/lib" - pending = [library / "zip-no-worker.js"] - copied = set() - while pending: - file = pending.pop().resolve() - if file in copied: - continue - if not file.is_relative_to(library.resolve()): - raise ValueError(f"ZIP source import escapes library: {file}") - text = file.read_text(encoding="utf-8") - copied.add(file) - destination = output / "vendor/zip" / file.relative_to(library.resolve()) - destination.parent.mkdir(parents=True, exist_ok=True) - shutil.copy2(file, destination) - for name in re.findall(r"(?:from\s+|import\s*)[\"'](\.[^\"']+)[\"']", text): - pending.append(file.parent / name) - shutil.copy2(ROOT / "3rdparty/zip.js/LICENSE", output / "vendor/zip/LICENSE") (output / ".nojekyll").touch() diff --git a/tools/test_web.py b/tools/test_web.py index bfa998a56..c6c66fe23 100644 --- a/tools/test_web.py +++ b/tools/test_web.py @@ -1,8 +1,11 @@ import argparse +import os from pathlib import Path +import re import subprocess import sys import tempfile +import urllib.request ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / "core/relinker/relinker/tests")) @@ -18,7 +21,13 @@ def main(): fixture = Path(directory) (fixture / "main.elf").write_bytes(main_fixture()) (fixture / "module.elf").write_bytes(module_with_symbol(True)) - subprocess.run(["node", str(ROOT / "demo/web/tests/archive.test.mjs"), str(args.build.resolve() / "web"), str(fixture)], check=True) + source = (args.build / "web/archive.mjs").read_text(encoding="utf-8") + url = re.search(r'from "(https://cdn.jsdelivr.net/[^"\n]+)"', source).group(1) + library = fixture / "zip.mjs" + with urllib.request.urlopen(url, timeout=30) as response: + library.write_bytes(response.read()) + environment = dict(os.environ, ANYPS5_TEST_ZIP_URL=url, ANYPS5_TEST_ZIP_PATH=library.as_uri()) + subprocess.run(["node", str(ROOT / "demo/web/tests/archive.test.mjs"), str(args.build.resolve() / "web"), str(fixture)], env=environment, check=True) if __name__ == "__main__":