| 4 | import { spawnSync } from "node:child_process"; |
| 5 | import { mkdirSync, writeFileSync, rmSync } from "node:fs"; |
| 6 | import { join } from "node:path"; |
| 7 | import { tmpdir } from "node:os"; |
| 8 | import type { ResolveHookContext } from "node:module"; |
| 9 | import { isEsmShCdn, jsrToCdnUrl, parseJsrSpecifier } from "./jsr.ts"; |
| 10 | import { createResolve } from "./hooks.ts"; |
| 11 |
| 12 | describe("parseJsrSpecifier", () => { |
| 13 | it("accepts valid specifiers", () => { |
| 14 | parseJsrSpecifier("jsr:@std/uuid"); |
| 15 | parseJsrSpecifier("jsr:@std/uuid@1.0.1"); |
| 16 | parseJsrSpecifier("jsr:@std/uuid/v1"); |
| 17 | parseJsrSpecifier("jsr:@std/uuid@1.0.1/v1"); |
| 18 | parseJsrSpecifier("jsr:@hono/hono@4"); |
| 19 | parseJsrSpecifier("jsr:@std/http/file-server"); |
| 20 | }); |
| 21 |
| 22 | it("throws on invalid specifiers", () => { |
| 23 | assert.throws(() => parseJsrSpecifier("jsr:uuid"), /Invalid JSR specifier/); |
| 24 | assert.throws(() => parseJsrSpecifier("jsr:@std"), /Invalid JSR specifier/); |
| 25 | assert.throws( |
| 26 | () => parseJsrSpecifier("jsr:@Std/uuid"), |
| 27 | /Invalid JSR specifier/, |
| 28 | ); |
| 29 | }); |
| 30 | }); |
| 31 |
| 32 | describe("isEsmShCdn", () => { |
| 33 | it("matches esm.sh base URLs", () => { |
| 34 | assert.ok(isEsmShCdn("https://esm.sh/")); |
| 35 | }); |
| 36 |
| 37 | it("rejects other CDNs", () => { |
| 38 | assert.equal(isEsmShCdn("https://cdn.jsdelivr.net/npm/"), false); |
| 39 | assert.equal(isEsmShCdn("https://unpkg.com/"), false); |
| 40 | }); |
| 41 | }); |
| 42 |
| 43 | describe("jsrToCdnUrl", () => { |
| 44 | it("maps jsr: specifiers to the esm.sh /jsr/ endpoint", () => { |
| 45 | assert.equal( |
| 46 | jsrToCdnUrl("jsr:@std/encoding@1.0.0/base64", "https://esm.sh/"), |
| 47 | "https://esm.sh/jsr/@std/encoding@1.0.0/base64", |
| 48 | ); |
| 49 | assert.equal( |
| 50 | jsrToCdnUrl("jsr:@hono/hono@4", "https://esm.sh/"), |
| 51 | "https://esm.sh/jsr/@hono/hono@4", |
| 52 | ); |
| 53 | assert.equal( |
| 54 | jsrToCdnUrl("jsr:@std/uuid", "https://esm.sh/"), |
| 55 | "https://esm.sh/jsr/@std/uuid", |
| 56 | ); |
| 57 | }); |
| 58 |
| 59 | it("throws on invalid specifiers", () => { |
| 60 | assert.throws( |
| 61 | () => jsrToCdnUrl("jsr:nope", "https://esm.sh/"), |
| 62 | /Invalid JSR specifier/, |
| 63 | ); |
| 64 | }); |
| 65 | }); |
| 66 |
| 67 | describe("hooks wiring", () => { |
| 68 | const context: ResolveHookContext = { |
| 69 | conditions: [], |
| 70 | importAttributes: {}, |
| 71 | parentURL: undefined, |
| 72 | }; |
| 73 | const nextResolve = (specifier: string) => ({ url: specifier }); |
| 74 | const resolve = createResolve(() => assert.fail("This specifier must be rejected before fetching")); |
| 75 |
| 76 | it("rejects jsr: specifiers when the CDN is not esm.sh", () => { |
| 77 | // The test process runs without URL_IMPORT_CDN, so the feature is off. |
| 78 | assert.throws( |
| 79 | () => resolve("jsr:@std/uuid", context, nextResolve), |
| 80 | /Cannot resolve "jsr:@std\/uuid"/, |
| 81 | ); |
| 82 | }); |
| 83 | }); |
| 84 |
| 85 | // End-to-end through the CLI: --cdn defaults to esm.sh, which enables jsr:. |
| 86 | describe("jsr: end-to-end (network)", () => { |
| 87 | const fixtureDir = join(tmpdir(), "node-url-import-jsr-test"); |
| 88 | const uuidScript = join(fixtureDir, "uuid.mjs"); |
| 89 | const subpathScript = join(fixtureDir, "subpath.mjs"); |
| 90 |
| 91 | function runCli(args: string[]) { |
| 92 | return spawnSync(process.execPath, ["src/cli.ts", ...args], { |
| 93 | cwd: process.cwd(), |
| 94 | env: { ...process.env }, |
| 95 | encoding: "utf-8", |
| 96 | timeout: 60_000, |
| 97 | }); |
| 98 | } |
| 99 |
| 100 | before(() => { |
| 101 | mkdirSync(fixtureDir, { recursive: true }); |
| 102 | writeFileSync( |
| 103 | uuidScript, |
| 104 | `import { v1, NIL_UUID } from "jsr:@std/uuid"; |
| 105 | const id = v1.generate(); |
| 106 | console.log(JSON.stringify({ valid: v1.validate(id), nil: NIL_UUID }));`, |
| 107 | "utf-8", |
| 108 | ); |
| 109 | writeFileSync( |
| 110 | subpathScript, |
| 111 | `import { encodeBase64 } from "jsr:@std/encoding@1.0.10/base64"; |
| 112 | console.log(JSON.stringify({ b64: encodeBase64("hello") }));`, |
| 113 | "utf-8", |
| 114 | ); |
| 115 | }); |
| 116 |
| 117 | after(() => { |
| 118 | rmSync(fixtureDir, { recursive: true, force: true }); |
| 119 | }); |
| 120 |
| 121 | it("imports a jsr: package with --cdn (default esm.sh)", () => { |
| 122 | const result = runCli(["--cdn", uuidScript]); |
| 123 | assert.equal( |
| 124 | result.status, |
| 125 | 0, |
| 126 | `CLI exited with ${result.status}\nstderr: ${result.stderr}`, |
| 127 | ); |
| 128 | const out = JSON.parse(result.stdout.trim()); |
| 129 | assert.equal(out.valid, true); |
| 130 | assert.equal(typeof out.nil, "string"); |
| 131 | }); |
| 132 |
| 133 | it("imports a pinned jsr: subpath export", () => { |
| 134 | const result = runCli(["--cdn", subpathScript]); |
| 135 | assert.equal( |
| 136 | result.status, |
| 137 | 0, |
| 138 | `CLI exited with ${result.status}\nstderr: ${result.stderr}`, |
| 139 | ); |
| 140 | assert.deepEqual(JSON.parse(result.stdout.trim()), { b64: "aGVsbG8=" }); |
| 141 | }); |
| 142 |
| 143 | it("fails with a clear error without --cdn", () => { |
| 144 | const result = runCli([uuidScript]); |
| 145 | assert.equal(result.status, 1); |
| 146 | assert.match(result.stderr, /Cannot resolve "jsr:@std\/uuid"/); |
| 147 | }); |
| 148 | }); |
| 149 |