yieldray/node-url-importCodeIssuesChange requestsActionsDiscussionsCommitsBranchesReleasesSearchContributorsCompare 53 lines · 2 KBtext/plain; charset=utf-8 | 1 | /** |
| 2 | * JSR specifier support (`jsr:@scope/pkg[@version][/subpath]`). |
| 3 | * |
| 4 | * Rather than talking to the JSR registry directly, specifiers are rewritten |
| 5 | |
* to esm.sh's `/jsr/` endpoint, which resolves versions and ranges,
| 6 | * transpiles TypeScript, and rewrites transitive dependencies to esm.sh |
| 9 | * jsr:@std/encoding@1.0.0/base64 → https://esm.sh/jsr/@std/encoding@1.0.0/base64 |
| 10 | * jsr:@hono/hono@4 → https://esm.sh/jsr/@hono/hono@4 |
| 12 | * The feature is only enabled when --cdn points at esm.sh (the default |
| 13 | * CDN). From there on, mapped URLs flow through the regular HTTP module |
| 14 | * pipeline (disk cache, lock file, redirects). |
| 18 | * Whether the --cdn base URL is esm.sh — the only CDN with a /jsr/ |
| 19 | * endpoint, so the only one jsr: specifiers can be mapped to. |
| 20 | * The CDN base always carries a trailing slash (see cli.ts). |
| 22 | export function isEsmShCdn(cdnBase: string): boolean { |
| 23 | return cdnBase.startsWith("https://esm.sh/"); |
| 26 | /** jsr:@<scope>/<name>[@<version>][/<subpath>] — scopes/names are lowercase alnum + dashes. */ |
| 27 | const JSR_SPECIFIER_RE = |
| 28 | /^jsr:@([a-z0-9-]+)\/([a-z0-9-]+?)(?:@([^/]+))?(?:\/(.+))?$/; |
| 31 | * Validate a `jsr:` specifier's shape. |
| 32 | * @throws If it doesn't match `jsr:@<scope>/<name>[@<version>][/<subpath>]`. |
| 34 | export function parseJsrSpecifier(specifier: string): void { |
| 35 | if (!JSR_SPECIFIER_RE.test(specifier)) { |
| 37 | `Invalid JSR specifier "${specifier}". ` + |
| 38 | `Expected jsr:@<scope>/<name>[@<version>][/<subpath>], e.g. jsr:@std/uuid@1.0.1/v1.`, |
| 44 | * Rewrite a `jsr:` specifier to its esm.sh `/jsr/` URL. |
| 45 | * `cdnBase` must be an esm.sh base URL with a trailing slash. |
| 46 | * The version slot is passed through verbatim — esm.sh resolves exact |
| 47 | * versions and ranges (`@1.0.1`, `@4`, `@^1.0.6`) server-side. |
| 49 | export function jsrToCdnUrl(specifier: string, cdnBase: string): string { |
| 50 | parseJsrSpecifier(specifier); |
| 51 | return `${cdnBase}jsr/${specifier.slice(4)}`; |