Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsr: imports #957

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/jsr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Look, Ma! JSR imports!

```js echo
import {printProgress} from "jsr:@luca/flag";

printProgress();
```

```js echo
import.meta.resolve("jsr:@luca/flag")
```
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"test/build/src/bin/",
"test/build/src/client/",
"test/build/src/convert.js",
"test/build/src/jsr.js",
"test/build/src/observableApiConfig.js",
"test/build/src/preview.js"
],
Expand Down Expand Up @@ -81,6 +82,7 @@
"rollup-plugin-esbuild": "^6.1.0",
"semver": "^7.5.4",
"send": "^0.18.0",
"tar": "^6.2.0",
"tar-stream": "^3.1.6",
"tsx": "^4.7.1",
"untildify": "^5.0.0",
Expand All @@ -98,6 +100,7 @@
"@types/node": "^20.7.1",
"@types/prompts": "^2.4.9",
"@types/send": "^0.17.2",
"@types/tar": "^6.1.11",
"@types/tar-stream": "^3.1.3",
"@types/ws": "^8.5.6",
"@typescript-eslint/eslint-plugin": "^7.2.0",
Expand Down
11 changes: 7 additions & 4 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,13 @@ export async function build(
// these, too, but it would involve rewriting the files since populateNpmCache
// doesn’t let you pass in a resolver.
for (const path of globalImports) {
if (!path.startsWith("/_npm/")) continue; // skip _observablehq
effects.output.write(`${faint("copy")} npm:${extractNpmSpecifier(path)} ${faint("→")} `);
const sourcePath = await populateNpmCache(root, path); // TODO effects
await effects.copyFile(sourcePath, path);
if (path.startsWith("/_npm/")) {
effects.output.write(`${faint("copy")} npm:${extractNpmSpecifier(path)} ${faint("→")} `);
const sourcePath = await populateNpmCache(root, path); // TODO effects
await effects.copyFile(sourcePath, path);
} else if (path.startsWith("/_jsr/")) {
// TODO jsr:
}
}

// Copy over imported local modules, overriding import resolution so that
Expand Down
53 changes: 53 additions & 0 deletions src/jsr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {mkdir, readFile} from "node:fs/promises";
import {join} from "node:path/posix";
import {Readable} from "node:stream";
import {finished} from "node:stream/promises";
import {satisfies} from "semver";
import {x} from "tar";
import {formatNpmSpecifier, parseNpmSpecifier} from "./npm.js";
import {faint} from "./tty.js";

const jsrRequests = new Map<string, Promise<Record<string, any>>>();

async function getJsrPackage(root: string, specifier: string): Promise<Record<string, any>> {
let promise = jsrRequests.get(specifier);
if (promise) return promise;
promise = (async function () {
process.stdout.write(`jsr:${specifier} ${faint("→")} `);
const {name, range = "latest"} = parseNpmSpecifier(specifier);
const metaHref = `https://npm.jsr.io/@jsr/${name.replace(/^@/, "").replace(/\//, "__")}`;
const metaResponse = await fetch(metaHref);
if (!metaResponse.ok) throw new Error(`unable to fetch: ${metaHref}`);
const meta = await metaResponse.json();
let version: {version: string; dist: {tarball: string}} | undefined;
if (meta["dist-tags"][range]) {
version = meta["versions"][meta["dist-tags"][range]];
} else if (range) {
if (meta.versions[range]) {
version = meta.versions[range]; // exact match; ignore yanked
} else {
for (const key in meta.versions) {
if (satisfies(key, range) && !meta.versions[key].yanked) {
version = meta.versions[key];
}
}
}
}
if (!version) throw new Error(`unable to resolve version: ${specifier}`);
const tarballResponse = await fetch(version.dist.tarball);
if (!tarballResponse.ok) throw new Error(`unable to fetch: ${version.dist.tarball}`);
const dir = join(root, ".observablehq", "cache", "_jsr", formatNpmSpecifier({name, range: version.version}));
await mkdir(dir, {recursive: true});
await finished(Readable.fromWeb(tarballResponse.body as any).pipe(x({strip: 1, C: dir})));
process.stdout.write(`${version.version}\n`);
return JSON.parse(await readFile(join(dir, "package.json"), "utf8"));
})();
jsrRequests.set(specifier, promise);
return promise;
}

export async function resolveJsrImport(root: string, specifier: string): Promise<string> {
const version = await getJsrPackage(root, specifier);
const {name, path = version.exports["."]} = parseNpmSpecifier(specifier);
return join("/", "_jsr", `${name}@${version.version}`, path);
}
3 changes: 3 additions & 0 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ export class PreviewServer {
} else if (pathname.startsWith("/_npm/")) {
await populateNpmCache(root, pathname);
send(req, pathname, {root: join(root, ".observablehq", "cache")}).pipe(res);
} else if (pathname.startsWith("/_jsr/")) {
// TODO await populateJsrCache
send(req, pathname, {root: join(root, ".observablehq", "cache")}).pipe(res);
} else if (pathname.startsWith("/_import/")) {
const path = pathname.slice("/_import".length);
const filepath = join(root, path);
Expand Down
53 changes: 35 additions & 18 deletions src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {LoaderResolver} from "./dataloader.js";
import {findAssets} from "./html.js";
import {defaultGlobals} from "./javascript/globals.js";
import {getFileHash, getModuleHash, getModuleInfo} from "./javascript/module.js";
import {resolveJsrImport} from "./jsr.js";
import {getImplicitDependencies, getImplicitDownloads} from "./libraries.js";
import {getImplicitFileImports, getImplicitInputImports} from "./libraries.js";
import {getImplicitStylesheets} from "./libraries.js";
Expand Down Expand Up @@ -169,40 +170,52 @@ export async function getResolvers(
globalImports.add(i);
}

// Resolve npm: imports.
// Resolve npm: and jsr: imports.
for (const i of globalImports) {
if (i.startsWith("npm:") && !builtins.has(i)) {
if (builtins.has(i)) continue;
if (i.startsWith("npm:")) {
resolutions.set(i, await resolveNpmImport(root, i.slice("npm:".length)));
} else if (i.startsWith("jsr:")) {
resolutions.set(i, await resolveJsrImport(root, i.slice("jsr:".length)));
}
}

// Follow transitive imports of npm imports. This has the side-effect of
// populating the npm cache.
for (const value of resolutions.values()) {
for (const i of await resolveNpmImports(root, value)) {
if (i.type === "local") {
const path = resolvePath(value, i.name);
const specifier = `npm:${extractNpmSpecifier(path)}`;
globalImports.add(specifier);
resolutions.set(specifier, path);
if (value.startsWith("/_npm/")) {
for (const i of await resolveNpmImports(root, value)) {
if (i.type === "local") {
const path = resolvePath(value, i.name);
const specifier = `npm:${extractNpmSpecifier(path)}`;
globalImports.add(specifier);
resolutions.set(specifier, path);
}
}
} else if (value.startsWith("/_jsr/")) {
// TODO jsr:
}
}

// Resolve transitive static npm: imports.
const npmStaticResolutions = new Set<string>();
// Resolve transitive static npm: and jsr: imports.
const globalStaticResolutions = new Set<string>();
for (const i of staticImports) {
const r = resolutions.get(i);
if (r) npmStaticResolutions.add(r);
if (r) globalStaticResolutions.add(r);
}
for (const value of npmStaticResolutions) {
for (const i of await resolveNpmImports(root, value)) {
if (i.type === "local" && i.method === "static") {
const path = resolvePath(value, i.name);
const specifier = `npm:${extractNpmSpecifier(path)}`;
staticImports.add(specifier);
npmStaticResolutions.add(path);

for (const value of globalStaticResolutions) {
if (value.startsWith("/_npm/")) {
for (const i of await resolveNpmImports(root, value)) {
if (i.type === "local" && i.method === "static") {
const path = resolvePath(value, i.name);
const specifier = `npm:${extractNpmSpecifier(path)}`;
staticImports.add(specifier);
globalStaticResolutions.add(path);
}
}
} else {
// TODO jsr:
}
}

Expand All @@ -213,6 +226,8 @@ export async function getResolvers(
const path = await resolveNpmImport(root, specifier.slice("npm:".length));
resolutions.set(specifier, path);
await populateNpmCache(root, path);
} else if (specifier.startsWith("jsr:")) {
// TODO jsr:
}
}

Expand All @@ -224,6 +239,8 @@ export async function getResolvers(
const path = await resolveNpmImport(root, specifier.slice("npm:".length));
resolutions.set(specifier, path);
await populateNpmCache(root, path);
} else if (specifier.startsWith("jsr:")) {
// TODO jsr:
}
}

Expand Down
62 changes: 62 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,14 @@
dependencies:
"@types/node" "*"

"@types/tar@^6.1.11":
version "6.1.11"
resolved "https://registry.yarnpkg.com/@types/tar/-/tar-6.1.11.tgz#48de9ccee8db37efb0d5a9f288567fc0378cb734"
integrity sha512-ThA1WD8aDdVU4VLuyq5NEqriwXErF5gEIJeyT6gHBWU7JtSmW2a5qjNv3/vR82O20mW+1vhmeZJfBQPT3HCugg==
dependencies:
"@types/node" "*"
minipass "^4.0.0"

"@types/tough-cookie@*":
version "4.0.5"
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.5.tgz#cb6e2a691b70cb177c6e3ae9c1d2e8b2ea8cd304"
Expand Down Expand Up @@ -1076,6 +1084,11 @@ chokidar@3.5.3:
optionalDependencies:
fsevents "~2.3.2"

chownr@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/chownr/-/chownr-2.0.0.tgz#15bfbe53d2eab4cf70f18a8cd68ebe5b3cb1dece"
integrity sha512-bIomtDF5KGpdogkLd9VspvFzk9KfpyyGlS8YFVZl7TGPBHL5snIOnxeshwVgPteQ9b4Eydl+pVbIyE1DcvCWgQ==

ci-info@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-4.0.0.tgz#65466f8b280fc019b9f50a5388115d17a63a44f2"
Expand Down Expand Up @@ -1869,6 +1882,13 @@ fresh@0.5.2:
resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7"
integrity sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==

fs-minipass@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-2.1.0.tgz#7f5036fdbf12c63c169190cbe4199c852271f9fb"
integrity sha512-V/JgOLFCS+R6Vcq0slCuaeWEdNC3ouDlJMNIsacH2VtALiu9mV4LPrHc5cDl8k5aw6J8jwgWWpiTo5RYhmIzvg==
dependencies:
minipass "^3.0.0"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down Expand Up @@ -2713,6 +2733,23 @@ minimist@^1.2.0, minimist@^1.2.6:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

minipass@^3.0.0:
version "3.3.6"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-3.3.6.tgz#7bba384db3a1520d18c9c0e5251c3444e95dd94a"
integrity sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw==
dependencies:
yallist "^4.0.0"

minipass@^4.0.0:
version "4.2.8"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-4.2.8.tgz#f0010f64393ecfc1d1ccb5f582bcaf45f48e1a3a"
integrity sha512-fNzuVyifolSLFL4NzpF+wEF4qrgqaaKX0haXPQEdQ7NKAN+WecoKMHV09YcuL/DHxrUsYQOK3MiuDf7Ip2OXfQ==

minipass@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-5.0.0.tgz#3e9788ffb90b694a5d0ec94479a45b5d8738133d"
integrity sha512-3FnjYuehv9k6ovOEbyOswadCDPX1piCfhV8ncmYtHOjuPwylVWsghTLo7rabjC3Rx5xD4HDx8Wm1xnMF7S5qFQ==

"minipass@^5.0.0 || ^6.0.2 || ^7.0.0":
version "7.0.4"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-7.0.4.tgz#dbce03740f50a4786ba994c1fb908844d27b038c"
Expand All @@ -2723,6 +2760,19 @@ minisearch@^6.3.0:
resolved "https://registry.yarnpkg.com/minisearch/-/minisearch-6.3.0.tgz#985a2f1ca3c73c2d65af94f0616bfe57164b0b6b"
integrity sha512-ihFnidEeU8iXzcVHy74dhkxh/dn8Dc08ERl0xwoMMGqp4+LvRSCgicb+zGqWthVokQKvCSxITlh3P08OzdTYCQ==

minizlib@^2.1.1:
version "2.1.2"
resolved "https://registry.yarnpkg.com/minizlib/-/minizlib-2.1.2.tgz#e90d3466ba209b932451508a11ce3d3632145931"
integrity sha512-bAxsR8BVfj60DWXHE3u30oHzfl4G7khkSuPW+qvpd7jFRHm7dLxOjUk1EHACJ/hxLY8phGJ0YhYHZo7jil7Qdg==
dependencies:
minipass "^3.0.0"
yallist "^4.0.0"

mkdirp@^1.0.3:
version "1.0.4"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

mocha@^10.2.0:
version "10.3.0"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-10.3.0.tgz#0e185c49e6dccf582035c05fa91084a4ff6e3fe9"
Expand Down Expand Up @@ -3488,6 +3538,18 @@ tar-stream@^3.1.6:
fast-fifo "^1.2.0"
streamx "^2.15.0"

tar@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/tar/-/tar-6.2.0.tgz#b14ce49a79cb1cd23bc9b016302dea5474493f73"
integrity sha512-/Wo7DcT0u5HUV486xg675HtjNd3BXZ6xDbzsCUZPt5iw8bTQ63bP0Raut3mvro9u+CUyq7YQd8Cx55fsZXxqLQ==
dependencies:
chownr "^2.0.0"
fs-minipass "^2.0.0"
minipass "^5.0.0"
minizlib "^2.1.1"
mkdirp "^1.0.3"
yallist "^4.0.0"

temp-dir@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/temp-dir/-/temp-dir-3.0.0.tgz#7f147b42ee41234cc6ba3138cd8e8aa2302acffa"
Expand Down