| 4 | import type { ResolveHookContext } from "node:module"; |
| 5 | import { mkdtempSync, mkdirSync, realpathSync, rmSync, writeFileSync } from "node:fs"; |
| 6 | import { tmpdir } from "node:os"; |
| 7 | import { join } from "node:path"; |
| 8 | import { pathToFileURL } from "node:url"; |
| 9 |
| 10 | describe("npm: specifier", () => { |
| 11 | it("should strip npm: prefix", () => { |
| 12 | assert.equal(stripNpmSpecifier("npm:ms"), "ms"); |
| 13 | assert.equal(stripNpmSpecifier("npm:lodash-es"), "lodash-es"); |
| 14 | }); |
| 15 |
| 16 | it("should strip npm: prefix and version", () => { |
| 17 | assert.equal(stripNpmSpecifier("npm:ms@2.1.3"), "ms"); |
| 18 | assert.equal(stripNpmSpecifier("npm:lodash-es@4.17.21"), "lodash-es"); |
| 19 | }); |
| 20 |
| 21 | it("should handle scoped packages", () => { |
| 22 | assert.equal(stripNpmSpecifier("npm:@types/node"), "@types/node"); |
| 23 | assert.equal(stripNpmSpecifier("npm:@scope/pkg"), "@scope/pkg"); |
| 24 | }); |
| 25 |
| 26 | it("should strip version from scoped packages", () => { |
| 27 | assert.equal(stripNpmSpecifier("npm:@types/node@22.0.0"), "@types/node"); |
| 28 | assert.equal(stripNpmSpecifier("npm:@scope/pkg@1.0.0"), "@scope/pkg"); |
| 29 | }); |
| 30 |
| 31 | it("should preserve subpath", () => { |
| 32 | assert.equal(stripNpmSpecifier("npm:lodash-es/add"), "lodash-es/add"); |
| 33 | assert.equal( |
| 34 | stripNpmSpecifier("npm:lodash-es@4.17.21/add"), |
| 35 | "lodash-es/add", |
| 36 | ); |
| 37 | }); |
| 38 |
| 39 | it("should preserve subpath for scoped packages", () => { |
| 40 | assert.equal(stripNpmSpecifier("npm:@scope/pkg/sub"), "@scope/pkg/sub"); |
| 41 | assert.equal( |
| 42 | stripNpmSpecifier("npm:@scope/pkg@1.0.0/sub"), |
| 43 | "@scope/pkg/sub", |
| 44 | ); |
| 45 | assert.equal( |
| 46 | stripNpmSpecifier("npm:@scope/pkg@1.0.0/deep/path"), |
| 47 | "@scope/pkg/deep/path", |
| 48 | ); |
| 49 | }); |
| 50 |
| 51 | it("falls back to the default CDN while preserving versions and subpaths", () => { |
| 52 | const context: ResolveHookContext = { conditions: [], importAttributes: {}, parentURL: "https://modules.test/main.mjs" }; |
| 53 | const fetched: string[] = []; |
| 54 | const resolve = createResolve((url) => { |
| 55 | fetched.push(url); |
| 56 | return { url, finalUrl: url, source: "export default 1;", contentType: "application/javascript", redirectChain: [url] }; |
| 57 | }); |
| 58 | const specifiers = ["npm:node-url-import-missing@2/sub", "npm:@url-import-test/missing@3/deep/path"]; |
| 59 | for (const specifier of specifiers) { |
| 60 | const result = resolve(specifier, context, () => assert.fail("Missing packages should fall back to HTTP")); |
| 61 | assert.equal(result.url, `https://esm.sh/${specifier.slice(4)}`); |
| 62 | } |
| 63 | assert.deepEqual(fetched, specifiers.map((specifier) => `https://esm.sh/${specifier.slice(4)}`)); |
| 64 | }); |
| 65 |
| 66 | it("should resolve from node_modules at runtime", async () => { |
| 67 | // @ts-expect-error: npm: specifier resolved by our loader at runtime |
| 68 | const ts = await import("npm:typescript"); |
| 69 | assert.equal(typeof ts.default.version, "string"); |
| 70 | }); |
| 71 | }); |
| 72 |
| 73 | describe("npm: local package resolution", () => { |
| 74 | let workspace: string; |
| 75 | let parentURL: string; |
| 76 | before(() => { |
| 77 | workspace = mkdtempSync(join(tmpdir(), "node-url-import-package-test-")); |
| 78 | const packageDirectory = join(workspace, "node_modules", "installed-fixture"); |
| 79 | mkdirSync(packageDirectory, { recursive: true }); |
| 80 | writeFileSync(join(packageDirectory, "package.json"), JSON.stringify({ |
| 81 | name: "installed-fixture", type: "module", exports: { ".": { require: "./require.mjs", import: "./import.mjs" } }, |
| 82 | })); |
| 83 | writeFileSync(join(packageDirectory, "require.mjs"), "export default 'require';"); |
| 84 | writeFileSync(join(packageDirectory, "import.mjs"), "export default 'import';"); |
| 85 | parentURL = pathToFileURL(join(workspace, "app.mjs")).href; |
| 86 | }); |
| 87 | after(() => { rmSync(workspace, { recursive: true, force: true }); }); |
| 88 |
| 89 | it("uses require resolution for installed npm packages", () => { |
| 90 | const resolve = createResolve(() => assert.fail("Installed packages must remain local")); |
| 91 | const result = resolve("npm:installed-fixture@1", { conditions: [], importAttributes: {}, parentURL }, (url) => ({ url })); |
| 92 | assert.equal(result.url, pathToFileURL(realpathSync(join(workspace, "node_modules", "installed-fixture", "require.mjs"))).href); |
| 93 | }); |
| 94 |
| 95 | it("preserves local export errors instead of downloading an installed package", () => { |
| 96 | const resolve = createResolve(() => assert.fail("Invalid local exports must not fall back to HTTP")); |
| 97 | assert.throws(() => resolve("npm:installed-fixture/private", { conditions: [], importAttributes: {}, parentURL }, |
| 98 | (url) => ({ url })), { code: "ERR_PACKAGE_PATH_NOT_EXPORTED" }); |
| 99 | }); |
| 100 | }); |
| 101 |