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

avoid top-level await; import npm:sql.js #1759

Merged
merged 6 commits into from
Nov 2, 2024
Merged
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
8 changes: 8 additions & 0 deletions docs/lib/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
import SQLite from "npm:@observablehq/sqlite";
```

If you prefer to use sql.js directly, you can import and initialize it like so:

```js run=false
import initSqlJs from "npm:sql.js";

const SQLite = await initSqlJs({locateFile: (name) => import.meta.resolve("npm:sql.js/dist/") + name});
```

We also provide `SQLiteDatabaseClient`, a [`DatabaseClient`](https://observablehq.com/@observablehq/database-client-specification) implementation.

```js run=false
Expand Down
13 changes: 5 additions & 8 deletions src/client/stdlib/sqlite.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// https://github.com/sql-js/sql.js/issues/284
const SQLite = await (async () => {
const exports = {};
const response = await fetch(import.meta.resolve("npm:sql.js/dist/sql-wasm.js"));
new Function("exports", await response.text())(exports);
return exports.Module({locateFile: (name) => import.meta.resolve("npm:sql.js/dist/") + name});
})();
import initSqlJs from "npm:sql.js";

const SQLite = initSqlJs({locateFile: (name) => import.meta.resolve("npm:sql.js/dist/") + name});

export default SQLite;

Expand All @@ -15,7 +11,8 @@ export class SQLiteDatabaseClient {
});
}
static async open(source) {
return new SQLiteDatabaseClient(new SQLite.Database(await load(await source)));
const [sqlite, data] = await Promise.all([SQLite, Promise.resolve(source).then(load)]);
return new SQLiteDatabaseClient(new sqlite.Database(data));
}
async query(query, params) {
return await exec(this._db, query, params);
Expand Down
12 changes: 10 additions & 2 deletions src/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,22 @@ export async function populateNpmCache(root: string, path: string): Promise<stri
let promise = npmRequests.get(outputPath);
if (promise) return promise; // coalesce concurrent requests
promise = (async () => {
const specifier = extractNpmSpecifier(path);
let specifier = extractNpmSpecifier(path);
const s = parseNpmSpecifier(specifier);
// https://github.com/sql-js/sql.js/issues/284
if (s.name === "sql.js" && s.path === "+esm") {
specifier = formatNpmSpecifier({...s, path: "dist/sql-wasm.js"});
}
const href = `https://cdn.jsdelivr.net/npm/${specifier}`;
console.log(`npm:${specifier} ${faint("→")} ${outputPath}`);
const response = await fetch(href);
if (!response.ok) throw new Error(`unable to fetch: ${href}`);
await mkdir(dirname(outputPath), {recursive: true});
if (/^application\/javascript(;|$)/i.test(response.headers.get("content-type")!)) {
const source = await response.text();
let source = await response.text();
if (s.name === "sql.js" && s.path === "+esm") {
source = "var module;\n" + source + "\nexport default initSqlJs;";
}
const resolver = await getDependencyResolver(root, path, source);
await writeFile(outputPath, rewriteNpmImports(source, resolver), "utf-8");
} else {
Expand Down