| 4 | import { createLoad, inferFormat, createResolve } from "./hooks.ts"; |
| 5 | import { fetchModuleSync } from "./sync-fetch.ts"; |
| 6 | import { initialize, resolve as resolveLoader } from "./loader.ts"; |
| 7 | import { cachePaths, writeCache } from "./cache.ts"; |
| 8 | import { rmSync } from "node:fs"; |
| 9 | import { resolve as resolvePath } from "node:path"; |
| 10 | import { pathToFileURL } from "node:url"; |
| 11 |
| 12 | const defaultResolveContext: ResolveHookContext = { |
| 13 | conditions: [], |
| 14 | importAttributes: {}, |
| 15 | parentURL: undefined, |
| 16 | }; |
| 17 |
| 18 | const defaultLoadContext: LoadHookContext = { |
| 19 | conditions: [], |
| 20 | format: null, |
| 21 | importAttributes: {}, |
| 22 | }; |
| 23 |
| 24 | const resolve = createResolve(fetchModuleSync); |
| 25 | const load = createLoad(fetchModuleSync); |
| 26 |
| 27 | // A simple passthrough nextResolve that marks itself as called |
| 28 | function makeNextResolve() { |
| 29 | let called = false; |
| 30 | const fn = (specifier: string) => { |
| 31 | called = true; |
| 32 | return { url: specifier }; |
| 33 | }; |
| 34 | return { fn, wasCalled: () => called }; |
| 35 | } |
| 36 |
| 37 | function makeNextLoad() { |
| 38 | let called = false; |
| 39 | const fn = (url: string) => { |
| 40 | called = true; |
| 41 | return { format: "module" as const, source: "" }; |
| 42 | }; |
| 43 | return { fn, wasCalled: () => called }; |
| 44 | } |
| 45 |
| 46 | describe("hooks.resolve (sync)", () => { |
| 47 | it("should resolve HTTP URLs", () => { |
| 48 | const next = makeNextResolve(); |
| 49 | const result = resolve("https://esm.sh/ms@2.1.3", defaultResolveContext, next.fn); |
| 50 | assert.equal(result.shortCircuit, true); |
| 51 | assert.ok(result.url.startsWith("https://")); |
| 52 | assert.equal(next.wasCalled(), false); |
| 53 | }); |
| 54 |
| 55 | it("should pass through non-HTTP specifiers to nextResolve", () => { |
| 56 | const next = makeNextResolve(); |
| 57 | const result = resolve("fs", defaultResolveContext, next.fn); |
| 58 | assert.equal(next.wasCalled(), true); |
| 59 | assert.equal(result.url, "fs"); |
| 60 | }); |
| 61 |
| 62 | it("should strip npm: specifiers and delegate to nextResolve", () => { |
| 63 | const next = makeNextResolve(); |
| 64 | const result = resolve("npm:typescript@6", defaultResolveContext, next.fn); |
| 65 | assert.equal(next.wasCalled(), true); |
| 66 | assert.ok(result.url.startsWith("file:")); |
| 67 | }); |
| 68 |
| 69 | it("should resolve relative imports against HTTP parent URL", () => { |
| 70 | const next = makeNextResolve(); |
| 71 | const resolveFixture = createResolve((url) => ({ |
| 72 | url, finalUrl: url, source: "export default 1;", contentType: "application/javascript", redirectChain: [url], |
| 73 | })); |
| 74 | const result = resolveFixture("./foo.js", { |
| 75 | ...defaultResolveContext, |
| 76 | parentURL: "https://esm.sh/bar/index.js", |
| 77 | }, next.fn); |
| 78 | assert.equal(result.shortCircuit, true); |
| 79 | assert.equal(result.url, "https://esm.sh/bar/foo.js"); |
| 80 | assert.equal(next.wasCalled(), false); |
| 81 | }); |
| 82 | }); |
| 83 |
| 84 | describe("async loader initialization", () => { |
| 85 | it("configures the already captured resolve function without replacing it", async () => { |
| 86 | const url = "https://loader-initialize.test/main.mjs#entry"; |
| 87 | const capturedResolve = resolveLoader; |
| 88 | writeCache([url], "export default 42;", "application/javascript"); |
| 89 | try { |
| 90 | initialize({ mainUrl: url }); |
| 91 | assert.equal(resolveLoader, capturedResolve); |
| 92 | const result = await capturedResolve(pathToFileURL(resolvePath(url)).href, defaultResolveContext, |
| 93 | () => assert.fail("The remote main must not reach native file resolution")); |
| 94 | assert.equal(result.url, url); |
| 95 | } finally { |
| 96 | initialize(); |
| 97 | rmSync(cachePaths(url).dir, { recursive: true, force: true }); |
| 98 | } |
| 99 | }); |
| 100 | }); |
| 101 |
| 102 | describe("hooks.load (sync)", () => { |
| 103 | it("should load HTTP URLs and return source", () => { |
| 104 | const next = makeNextLoad(); |
| 105 | const result = load("https://esm.sh/ms@2.1.3", defaultLoadContext, next.fn); |
| 106 | assert.equal(result.shortCircuit, true); |
| 107 | assert.ok(result.format); |
| 108 | assert.equal(typeof result.source, "string"); |
| 109 | assert.ok((result.source as string).length > 0); |
| 110 | assert.equal(next.wasCalled(), false); |
| 111 | }); |
| 112 |
| 113 | it("should pass through non-HTTP URLs to nextLoad", () => { |
| 114 | const next = makeNextLoad(); |
| 115 | const result = load("file:///tmp/foo.js", defaultLoadContext, next.fn); |
| 116 | assert.equal(next.wasCalled(), true); |
| 117 | }); |
| 118 |
| 119 | it("should load remote TypeScript module with module-typescript format", async () => { |
| 120 | const url = "https://typescript-load.test/mod.ts"; |
| 121 | const source = "export const value: number = 42;"; |
| 122 | writeCache([url], source, "text/typescript"); |
| 123 | try { |
| 124 | const next = makeNextLoad(); |
| 125 | const result = load(url, defaultLoadContext, next.fn); |
| 126 | assert.equal(result.shortCircuit, true); |
| 127 | assert.equal(result.format, "module-typescript"); |
| 128 | assert.equal(result.source, source); |
| 129 | assert.equal(next.wasCalled(), false); |
| 130 | assert.equal((await import(url)).value, 42); |
| 131 | } finally { |
| 132 | rmSync(cachePaths(url).file, { force: true }); |
| 133 | } |
| 134 | }); |
| 135 | }); |
| 136 |
| 137 | describe("inferFormat", () => { |
| 138 | it("should return module-typescript for .ts extension", () => { |
| 139 | assert.equal(inferFormat("https://example.com/mod.ts", "application/javascript"), "module-typescript"); |
| 140 | }); |
| 141 |
| 142 | it("should return module-typescript for .tsx extension", () => { |
| 143 | assert.equal(inferFormat("https://example.com/mod.tsx", "application/javascript"), "module-typescript"); |
| 144 | }); |
| 145 |
| 146 | it("should return module-typescript for .mts extension", () => { |
| 147 | assert.equal(inferFormat("https://example.com/mod.mts", "application/javascript"), "module-typescript"); |
| 148 | }); |
| 149 |
| 150 | it("keeps .cts CommonJS semantics regardless of TypeScript MIME", () => { |
| 151 | for (const contentType of ["application/javascript", "text/typescript", "application/typescript; charset=utf-8"]) { |
| 152 | assert.equal(inferFormat("https://example.com/mod.cts", contentType), "commonjs-typescript"); |
| 153 | } |
| 154 | }); |
| 155 |
| 156 | it("should return module-typescript for text/typescript content-type", () => { |
| 157 | assert.equal(inferFormat("https://example.com/mod", "text/typescript"), "module-typescript"); |
| 158 | }); |
| 159 |
| 160 | it("should return module-typescript for application/typescript content-type", () => { |
| 161 | assert.equal(inferFormat("https://example.com/mod", "application/typescript; charset=utf-8"), "module-typescript"); |
| 162 | }); |
| 163 |
| 164 | it("should return module for .js extension", () => { |
| 165 | assert.equal(inferFormat("https://example.com/mod.js", "application/javascript"), "module"); |
| 166 | }); |
| 167 |
| 168 | it("should return module for .mjs extension", () => { |
| 169 | assert.equal(inferFormat("https://example.com/mod.mjs", "application/javascript"), "module"); |
| 170 | }); |
| 171 |
| 172 | it("should return commonjs for .cjs extension", () => { |
| 173 | assert.equal(inferFormat("https://example.com/mod.cjs", "application/javascript"), "commonjs"); |
| 174 | }); |
| 175 |
| 176 | it("should return json for .json extension", () => { |
| 177 | assert.equal(inferFormat("https://example.com/data.json", "application/json"), "json"); |
| 178 | }); |
| 179 |
| 180 | it("should return wasm for .wasm extension", () => { |
| 181 | assert.equal(inferFormat("https://example.com/mod.wasm", "application/wasm"), "wasm"); |
| 182 | }); |
| 183 |
| 184 | it("should default to module for unknown extension", () => { |
| 185 | assert.equal(inferFormat("https://example.com/mod", "application/javascript"), "module"); |
| 186 | }); |
| 187 |
| 188 | describe("when TypeScript type-stripping is unsupported (old Node)", () => { |
| 189 | it("should throw an explicit error for .ts extension", () => { |
| 190 | assert.throws( |
| 191 | () => inferFormat("https://example.com/mod.ts", "application/javascript", false), |
| 192 | /does not support type stripping/, |
| 193 | ); |
| 194 | }); |
| 195 |
| 196 | it("should throw an explicit error for .tsx extension", () => { |
| 197 | assert.throws( |
| 198 | () => inferFormat("https://example.com/mod.tsx", "application/javascript", false), |
| 199 | /does not support type stripping/, |
| 200 | ); |
| 201 | }); |
| 202 |
| 203 | it("should throw an explicit error for .mts extension", () => { |
| 204 | assert.throws( |
| 205 | () => inferFormat("https://example.com/mod.mts", "application/javascript", false), |
| 206 | /does not support type stripping/, |
| 207 | ); |
| 208 | }); |
| 209 |
| 210 | it("should throw an explicit error for .cts extension", () => { |
| 211 | assert.throws( |
| 212 | () => inferFormat("https://example.com/mod.cts", "application/javascript", false), |
| 213 | /does not support type stripping/, |
| 214 | ); |
| 215 | }); |
| 216 |
| 217 | it("should throw an explicit error for text/typescript content-type", () => { |
| 218 | assert.throws( |
| 219 | () => inferFormat("https://example.com/mod", "text/typescript", false), |
| 220 | /does not support type stripping/, |
| 221 | ); |
| 222 | }); |
| 223 |
| 224 | it("should still resolve non-TypeScript formats without throwing", () => { |
| 225 | assert.equal(inferFormat("https://example.com/mod.js", "application/javascript", false), "module"); |
| 226 | assert.equal(inferFormat("https://example.com/mod.cjs", "application/javascript", false), "commonjs"); |
| 227 | assert.equal(inferFormat("https://example.com/data.json", "application/json", false), "json"); |
| 228 | assert.equal(inferFormat("https://example.com/mod.wasm", "application/wasm", false), "wasm"); |
| 229 | }); |
| 230 | }); |
| 231 | }); |
| 232 |