Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/bundlejs-api.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695/** * CDN Format Utilities * * Utilities for working with various JavaScript CDNs and registries. * Supports npm CDNs (unpkg, esm.sh, jsdelivr, skypack), JSR, GitHub, and Deno. * * This module provides the URL handling logic for bundlejs, enabling * cross-platform package resolution from multiple sources. * * @module * * @example CDN Style Detection * ```ts * import { getCDNStyle, getCDNOrigin, getCDNUrl } from "./cdn-format.ts"; * * getCDNStyle("esm:react") * // "npm" * * getCDNStyle("jsr:@std/path") * // "jsr" * * getCDNStyle("https://jsr.io/@std/path/1.0.0/mod.ts") * // "jsr" * * getCDNStyle("github:user/repo") * // "github" * ``` * * @example URL Generation * ```ts * getCDNUrl("jsr:@std/path@1.0.0") * // { * // import: "jsr:@std/path@1.0.0", * // path: "@std/path@1.0.0", * // origin: "https://jsr.io/", * // cdn: "https://jsr.io/", * // url: URL { href: "https://jsr.io/@std/path@1.0.0" } * // } * * getCDNUrl("esm:react@18") * // { * // import: "esm:react@18", * // path: "react@18", * // origin: "https://esm.sh/", * // cdn: "https://esm.sh/", * // url: URL { href: "https://esm.sh/react@18" } * // } * ``` */
// =============================================================================// Types// =============================================================================
/** * CDN style categories. * * Different CDNs have different URL patterns and capabilities: * - `npm`: Supports semver versions in URLs (unpkg, esm.sh, jsdelivr, skypack) * - `jsr`: JSR registry - TypeScript-first, direct module access * - `github`: Raw GitHub file access (jsdelivr.gh, raw.githubusercontent.com) * - `deno`: Deno's deno.land/x registry * - `tarball`: Tarball providers (pkg.pr.new for PR previews) * - `registry`: npm registry tarball mode — fetches whole-package tarballs from * registry.npmjs.org instead of individual files from a CDN. Faster for large * packages with many internal imports (one fetch vs. hundreds). * - `other`: Unknown CDN patterns */export type CDNStyle = "npm" | "jsr" | "github" | "deno" | "tarball" | "registry" | "other";
/** * URL scheme prefixes that indicate a specific CDN. */export type CDNScheme = | "skypack" | "esm" | "esm.sh" | "unpkg" | "jsdelivr" | "esm.run" | "jsdelivr.gh" | "github" | "deno" | "jsr" | "jsr.registry" | "npm" | "npm.registry";
/** * Result from getCDNUrl. */export interface CDNUrlResult { /** Original import string */ import: string; /** Pure import path (without scheme/host) */ path: string; /** CDN origin URL (with trailing /) */ origin: string; /** Default CDN that was used */ cdn: string; /** Full resolved URL */ url: URL;}
// =============================================================================// Constants// =============================================================================
/** * Default CDN host for npm packages. */export const DEFAULT_CDN_HOST = "https://unpkg.com";
/** * npm registry base URL. * * Used in "registry" CDN mode to fetch whole-package tarballs directly * from the npm registry, bypassing CDN proxies like unpkg. * * Tarball URL pattern: * `https://registry.npmjs.org/<name>/-/<basename>-<version>.tgz` * * @example Scoped package * ``` * https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.0.0.tgz * ``` * * @example Unscoped package * ``` * https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz * ``` */export const NPM_REGISTRY = "https://registry.npmjs.org";
/** * JSR registry base URL. */export const JSR_REGISTRY = "https://jsr.io";
/** * Map of CDN schemes to their origin URLs. * * @example * ```ts * CDN_SCHEME_TO_ORIGIN["esm"] * // "https://esm.sh" * * CDN_SCHEME_TO_ORIGIN["jsr"] * // "https://jsr.io" * ``` */export const CDN_SCHEME_TO_ORIGIN: Record<CDNScheme, string> = { skypack: "https://cdn.skypack.dev", esm: "https://esm.sh", "esm.sh": "https://esm.sh", unpkg: "https://unpkg.com", jsdelivr: "https://cdn.jsdelivr.net/npm", "esm.run": "https://cdn.jsdelivr.net/npm", "jsdelivr.gh": "https://cdn.jsdelivr.net/gh", github: "https://raw.githubusercontent.com", deno: "https://deno.land/x", jsr: "https://jsr.io", "jsr.registry": "https://jsr.io", npm: NPM_REGISTRY, "npm.registry": NPM_REGISTRY,};
// =============================================================================// CDN Style Detection// =============================================================================
/** * Detect the CDN style from a URL or scheme. * * Returns the category of CDN, which determines how URLs should be handled: * - `npm`: Package + version in URL, npm registry compatible * - `jsr`: JSR registry, TypeScript-first * - `github`: Raw GitHub file access * - `deno`: Deno's deno.land/x * - `tarball`: Tarball providers like pkg.pr.new * - `other`: Unknown * * @param urlStr URL string or scheme prefix * @returns CDN style category * * @example Scheme detection * ```ts * getCDNStyle("esm:react") // "npm" * getCDNStyle("esm.sh:react") // "npm" * getCDNStyle("unpkg:lodash") // "npm" * getCDNStyle("skypack:react") // "npm" * getCDNStyle("jsdelivr:axios") // "npm" * getCDNStyle("esm.run:vue") // "npm" * ``` * * @example JSR detection * ```ts * getCDNStyle("jsr:@std/path") // "jsr" * getCDNStyle("https://jsr.io/@std/path/1.0.0/mod.ts") // "jsr" * ``` * * @example GitHub detection * ```ts * getCDNStyle("github:user/repo/file.js") // "github" * getCDNStyle("jsdelivr.gh:user/repo/file.js") // "github" * getCDNStyle("https://raw.githubusercontent.com/user/repo") // "github" * getCDNStyle("https://cdn.jsdelivr.net/gh/user/repo") // "github" * ``` * * @example Deno detection * ```ts * getCDNStyle("deno:oak") // "deno" * getCDNStyle("https://deno.land/x/oak/mod.ts") // "deno" * ``` * * @example Tarball detection * ```ts * getCDNStyle("https://pkg.pr.new/user/repo@commit") // "tarball" * ``` * * @example Registry detection * ```ts * getCDNStyle("npm:react@18") // "registry" * getCDNStyle("npm.registry:react@18") // "registry" * getCDNStyle("https://registry.npmjs.org/react/-/react-18.2.0.tgz") // "registry" * ``` * * @example JSR registry detection * ```ts * getCDNStyle("jsr.registry:@std/path") // "jsr" * ``` */export function getCDNStyle(urlStr: string): CDNStyle { // JSR - check first as it's a distinct ecosystem // NOTE: jsr.registry: must be checked before jsr: since "jsr.registry:" // does NOT start with "jsr:" (the char after "jsr" is ".", not ":"). // Both jsr: and jsr.registry: map to the same "jsr" style — direct // access to jsr.io. The jsr.registry: alias exists for naming consistency // with npm.registry: ("go directly to the registry, not through a CDN proxy"). if ( /^jsr(\.registry)?\:/.test(urlStr) || /^https?:\/\/(jsr\.io)/.test(urlStr) ) { return "jsr"; }
// npm-style CDNs (unpkg, esm.sh, jsdelivr/npm, skypack) // NOTE: The scheme regex REQUIRES a colon after the scheme name. // Without it, "jsdelivr" would greedily match "jsdelivr.gh:..." before // the more-specific GitHub check below can run. if ( /^(skypack|esm|esm\.sh|unpkg|jsdelivr|esm\.run)\:/.test(urlStr) || /^https?:\/\/(cdn\.skypack\.dev|cdn\.esm\.sh|esm\.sh|cdn\.jsdelivr\.net\/npm|unpkg\.com)/.test(urlStr) ) { return "npm"; }
// GitHub-style (raw files) if ( /^(jsdelivr\.gh|github)\:?/.test(urlStr) || /^https?:\/\/(cdn\.jsdelivr\.net\/gh|raw\.githubusercontent\.com)/.test(urlStr) ) { return "github"; }
// Deno-style if ( /^deno\:/.test(urlStr) || /^https?:\/\/deno\.land\/x/.test(urlStr) ) { return "deno"; }
// Tarball providers (PR previews, etc.) if (/^https?:\/\/pkg\.pr\.new/.test(urlStr)) { return "tarball"; }
// npm registry tarball mode — whole-package tarballs from registry.npmjs.org. // Matches the scheme prefixes (npm: and npm.registry:) and the origin URL. // // NOTE: npm: is checked AFTER the npm-CDN check above, so "unpkg:", "esm:", // etc. are not affected. The npm: scheme is only reached for strings that // literally start with "npm:" and did NOT match any earlier CDN pattern. if ( /^npm(\.registry)?\:/.test(urlStr) || /^https?:\/\/registry\.npmjs\.(org|com)/.test(urlStr) ) { return "registry"; }
return "other";}
/** * Check if a URL or scheme uses a specific CDN style. * * @param urlStr URL string or scheme prefix * @param style CDN style to check * @returns True if the URL matches the style * * @example * ```ts * isCDNStyle("esm:react", "npm") // true * isCDNStyle("jsr:@std/path", "jsr") // true * isCDNStyle("jsr:@std/path", "npm") // false * ``` */export function isCDNStyle(urlStr: string, style: CDNStyle): boolean { return getCDNStyle(urlStr) === style;}
// =============================================================================// CDN Origin Resolution// =============================================================================
/** * Get the CDN origin URL for an import string. * * Handles scheme prefixes (esm:, jsr:, etc.) and falls back to the * provided default CDN. * * @param importStr Import string (may include scheme prefix) * @param cdn Default CDN host to use if no scheme present * @returns CDN origin URL (always ends with /) * * @example Scheme prefixes * ```ts * getCDNOrigin("skypack:react") * // "https://cdn.skypack.dev/" * * getCDNOrigin("esm:react") * // "https://esm.sh/" * * getCDNOrigin("esm.sh:react") * // "https://esm.sh/" * * getCDNOrigin("unpkg:lodash") * // "https://unpkg.com/" * * getCDNOrigin("jsdelivr:axios") * // "https://cdn.jsdelivr.net/npm/" * * getCDNOrigin("esm.run:vue") * // "https://cdn.jsdelivr.net/npm/" * ``` * * @example JSR * ```ts * getCDNOrigin("jsr:@std/path") * // "https://jsr.io/" * ``` * * @example GitHub * ```ts * getCDNOrigin("jsdelivr.gh:user/repo") * // "https://cdn.jsdelivr.net/gh/" * * getCDNOrigin("github:user/repo") * // "https://raw.githubusercontent.com/" * ``` * * @example Deno * ```ts * getCDNOrigin("deno:oak") * // "https://deno.land/x/" * ``` * * @example Default fallback * ```ts * getCDNOrigin("react") * // "https://unpkg.com/" (default) * * getCDNOrigin("react", "https://cdn.esm.sh") * // "https://cdn.esm.sh/" * ``` */export function getCDNOrigin(importStr: string, cdn = DEFAULT_CDN_HOST): string { // Check for scheme prefixes if (/^skypack\:/.test(importStr)) { cdn = CDN_SCHEME_TO_ORIGIN.skypack; } else if (/^jsr\:/.test(importStr)) { cdn = CDN_SCHEME_TO_ORIGIN.jsr; } else if (/^jsr\.registry\:/.test(importStr)) { cdn = CDN_SCHEME_TO_ORIGIN["jsr.registry"]; } else if (/^(esm\.sh|esm)\:/.test(importStr)) { cdn = CDN_SCHEME_TO_ORIGIN.esm; } else if (/^unpkg\:/.test(importStr)) { cdn = CDN_SCHEME_TO_ORIGIN.unpkg; } else if (/^(jsdelivr|esm\.run)\:/.test(importStr)) { cdn = CDN_SCHEME_TO_ORIGIN.jsdelivr; } else if (/^jsdelivr\.gh\:/.test(importStr)) { cdn = CDN_SCHEME_TO_ORIGIN["jsdelivr.gh"]; } else if (/^deno\:/.test(importStr)) { cdn = CDN_SCHEME_TO_ORIGIN.deno; } else if (/^github\:/.test(importStr)) { cdn = CDN_SCHEME_TO_ORIGIN.github; } else if (/^npm(\.registry)?\:/.test(importStr)) { cdn = CDN_SCHEME_TO_ORIGIN["npm.registry"]; }
// Ensure trailing slash return cdn.endsWith("/") ? cdn : `${cdn}/`;}
// =============================================================================// Path Extraction// =============================================================================
/** * Remove CDN scheme prefixes and known CDN hosts from an import string. * * Returns just the package/file path portion. * * @param importStr Import string * @returns Pure import path * * @example Scheme removal * ```ts * getPureImportPath("esm:react@18") * // "react@18" * * getPureImportPath("jsr:@std/path@1.0.0") * // "@std/path@1.0.0" * * getPureImportPath("unpkg:lodash@4.17.0/get") * // "lodash@4.17.0/get" * ``` * * @example URL host removal * ```ts * getPureImportPath("https://esm.sh/react@18") * // "react@18" * * getPureImportPath("https://jsr.io/@std/path/1.0.0/mod.ts") * // "@std/path/1.0.0/mod.ts" * * getPureImportPath("https://cdn.jsdelivr.net/npm/lodash@4/lodash.min.js") * // "lodash@4/lodash.min.js" * ``` */export function getPureImportPath(importStr: string): string { return importStr // Remove scheme prefixes .replace(/^(skypack|esm|esm\.sh|unpkg|jsdelivr|jsdelivr\.gh|esm\.run|deno|github|jsr\.registry|jsr|npm\.registry|npm)\:/, "") // Remove known CDN hosts .replace( /^(https?:\/\/)?(cdn\.skypack\.dev|(cdn\.)?esm\.sh|cdn\.jsdelivr\.net\/npm|unpkg\.com|cdn\.jsdelivr\.net\/gh|raw\.githubusercontent\.com|deno\.land\/x|jsr\.io|registry\.npmjs\.(org|com))/, "" ) // Remove leading slash .replace(/^\//, "");}
// =============================================================================// URL Generation// =============================================================================
/** * Generate a full CDN URL for an import. * * Combines origin resolution and path extraction to produce * a complete URL object. * * @param importStr Import string (with optional scheme prefix) * @param cdn Default CDN host * @returns CDN URL result with all components * * @example npm packages * ```ts * getCDNUrl("react@18") * // { * // import: "react@18", * // path: "react@18", * // origin: "https://unpkg.com/", * // cdn: "https://unpkg.com", * // url: URL { href: "https://unpkg.com/react@18" } * // } * * getCDNUrl("esm:react@18") * // { * // import: "esm:react@18", * // path: "react@18", * // origin: "https://esm.sh/", * // cdn: "https://unpkg.com", // default unchanged * // url: URL { href: "https://esm.sh/react@18" } * // } * ``` * * @example JSR packages * ```ts * getCDNUrl("jsr:@std/path@1.0.0") * // { * // import: "jsr:@std/path@1.0.0", * // path: "@std/path@1.0.0", * // origin: "https://jsr.io/", * // cdn: "https://unpkg.com", * // url: URL { href: "https://jsr.io/@std/path@1.0.0" } * // } * ``` * * @example GitHub files * ```ts * getCDNUrl("github:user/repo/main/file.js") * // { * // import: "github:user/repo/main/file.js", * // path: "user/repo/main/file.js", * // origin: "https://raw.githubusercontent.com/", * // cdn: "https://unpkg.com", * // url: URL { href: "https://raw.githubusercontent.com/user/repo/main/file.js" } * // } * ``` */export function getCDNUrl(importStr: string, cdn = DEFAULT_CDN_HOST): CDNUrlResult { const origin = getCDNOrigin(importStr, cdn); const path = getPureImportPath(importStr); const url = new URL(path, origin);
return { import: importStr, path, origin, cdn, url, };}
// =============================================================================// JSR-Specific Utilities// =============================================================================
/** * Parse a JSR specifier into its components. * * JSR specifiers have the format: jsr:@scope/name@version/subpath * * @param specifier JSR specifier string * @returns Parsed components or null if invalid * * @example * ```ts * parseJSRSpecifier("jsr:@std/path@1.0.0") * // { scope: "std", name: "path", version: "1.0.0", subpath: "" } * * parseJSRSpecifier("jsr:@std/path@1.0.0/posix") * // { scope: "std", name: "path", version: "1.0.0", subpath: "/posix" } * * parseJSRSpecifier("jsr:@std/path") * // { scope: "std", name: "path", version: null, subpath: "" } * * parseJSRSpecifier("npm:lodash") * // null (not a JSR specifier) * ``` */export function parseJSRSpecifier(specifier: string): { scope: string; name: string; version: string | null; subpath: string;} | null { const match = /^jsr:@([a-z][a-z0-9-]*)\/([a-z][a-z0-9-]*)(?:@([^\/]+))?(\/.*)?$/.exec(specifier); if (!match) return null;
const [, scope, name, version, subpath] = match; return { scope, name, version: version ?? null, subpath: subpath ?? "", };}
/** * Generate a direct JSR module URL. * * Creates a URL that can be used directly in import statements * without needing a CDN proxy like esm.sh. * * @param scope Package scope (without @) * @param name Package name * @param version Version string * @param path File path (default: /mod.ts) * @returns Direct JSR URL * * @example * ```ts * getJSRDirectUrl("std", "path", "1.0.0") * // "https://jsr.io/@std/path/1.0.0/mod.ts" * * getJSRDirectUrl("std", "path", "1.0.0", "/posix.ts") * // "https://jsr.io/@std/path/1.0.0/posix.ts" * * // Can be used directly in Deno: * // import { join } from "https://jsr.io/@std/path/1.0.0/mod.ts"; * ``` */export function getJSRDirectUrl( scope: string, name: string, version: string, path = "/mod.ts"): string { const cleanPath = path.startsWith("/") ? path : `/${path}`; return `${JSR_REGISTRY}/@${scope}/${name}/${version}${cleanPath}`;}
/** * Generate a JSR URL via esm.sh proxy. * * Useful for environments that can't use jsr.io directly. * * @param scope Package scope (without @) * @param name Package name * @param version Version string (optional) * @param subpath Subpath (optional) * @returns esm.sh proxy URL * * @example * ```ts * getJSRProxyUrl("std", "path", "1.0.0") * // "https://esm.sh/jsr/@std/path@1.0.0" * * getJSRProxyUrl("std", "path", "1.0.0", "/posix") * // "https://esm.sh/jsr/@std/path@1.0.0/posix" * * getJSRProxyUrl("std", "path") * // "https://esm.sh/jsr/@std/path" * ``` */export function getJSRProxyUrl( scope: string, name: string, version?: string, subpath = ""): string { const versionPart = version ? `@${version}` : ""; return `https://esm.sh/jsr/@${scope}/${name}${versionPart}${subpath}`;}
// =============================================================================// Utility Checks// =============================================================================
/** * Check if a string is a JSR specifier. * * @param str String to check * @returns True if it's a jsr: specifier * * @example * ```ts * isJSRSpecifier("jsr:@std/path") // true * isJSRSpecifier("npm:lodash") // false * isJSRSpecifier("@std/path") // false * ``` */export function isJSRSpecifier(str: string): boolean { return str.startsWith("jsr:");}
/** * Check if a string is an npm CDN URL or scheme. * * @param str String to check * @returns True if it's an npm CDN URL/scheme * * @example * ```ts * isNpmCDN("esm:react") // true * isNpmCDN("https://unpkg.com/react") // true * isNpmCDN("https://esm.sh/lodash") // true * isNpmCDN("jsr:@std/path") // false * isNpmCDN("https://raw.githubusercontent.com") // false * ``` */export function isNpmCDN(str: string): boolean { return getCDNStyle(str) === "npm";}
/** * Check if a string is a GitHub raw URL or scheme. * * @param str String to check * @returns True if it's a GitHub URL/scheme * * @example * ```ts * isGitHubRaw("github:user/repo") // true * isGitHubRaw("https://raw.githubusercontent.com") // true * isGitHubRaw("https://github.com/user/repo") // false (not raw) * ``` */export function isGitHubRaw(str: string): boolean { return getCDNStyle(str) === "github";}