Skip to content

Commit 2b4a42b

Browse files
authored
avoid top-level await; import npm:sql.js (#1759)
* avoid top-level await; import npm:sql.js * comment * restore SQLite * remove DuckDB top-level await * fix SQLite export
1 parent 38fbffa commit 2b4a42b

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

docs/lib/sqlite.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
import SQLite from "npm:@observablehq/sqlite";
77
```
88

9+
If you prefer to use sql.js directly, you can import and initialize it like so:
10+
11+
```js run=false
12+
import initSqlJs from "npm:sql.js";
13+
14+
const SQLite = await initSqlJs({locateFile: (name) => import.meta.resolve("npm:sql.js/dist/") + name});
15+
```
16+
917
We also provide `SQLiteDatabaseClient`, a [`DatabaseClient`](https://observablehq.com/@observablehq/database-client-specification) implementation.
1018
1119
```js run=false

src/client/stdlib/sqlite.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
// https://github.com/sql-js/sql.js/issues/284
2-
const SQLite = await (async () => {
3-
const exports = {};
4-
const response = await fetch(import.meta.resolve("npm:sql.js/dist/sql-wasm.js"));
5-
new Function("exports", await response.text())(exports);
6-
return exports.Module({locateFile: (name) => import.meta.resolve("npm:sql.js/dist/") + name});
7-
})();
1+
import initSqlJs from "npm:sql.js";
2+
3+
const SQLite = initSqlJs({locateFile: (name) => import.meta.resolve("npm:sql.js/dist/") + name});
84

95
export default SQLite;
106

@@ -15,7 +11,8 @@ export class SQLiteDatabaseClient {
1511
});
1612
}
1713
static async open(source) {
18-
return new SQLiteDatabaseClient(new SQLite.Database(await load(await source)));
14+
const [sqlite, data] = await Promise.all([SQLite, Promise.resolve(source).then(load)]);
15+
return new SQLiteDatabaseClient(new sqlite.Database(data));
1916
}
2017
async query(query, params) {
2118
return await exec(this._db, query, params);

src/npm.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,22 @@ export async function populateNpmCache(root: string, path: string): Promise<stri
8383
let promise = npmRequests.get(outputPath);
8484
if (promise) return promise; // coalesce concurrent requests
8585
promise = (async () => {
86-
const specifier = extractNpmSpecifier(path);
86+
let specifier = extractNpmSpecifier(path);
87+
const s = parseNpmSpecifier(specifier);
88+
// https://github.com/sql-js/sql.js/issues/284
89+
if (s.name === "sql.js" && s.path === "+esm") {
90+
specifier = formatNpmSpecifier({...s, path: "dist/sql-wasm.js"});
91+
}
8792
const href = `https://cdn.jsdelivr.net/npm/${specifier}`;
8893
console.log(`npm:${specifier} ${faint("→")} ${outputPath}`);
8994
const response = await fetch(href);
9095
if (!response.ok) throw new Error(`unable to fetch: ${href}`);
9196
await mkdir(dirname(outputPath), {recursive: true});
9297
if (/^application\/javascript(;|$)/i.test(response.headers.get("content-type")!)) {
93-
const source = await response.text();
98+
let source = await response.text();
99+
if (s.name === "sql.js" && s.path === "+esm") {
100+
source = "var module;\n" + source + "\nexport default initSqlJs;";
101+
}
94102
const resolver = await getDependencyResolver(root, path, source);
95103
await writeFile(outputPath, rewriteNpmImports(source, resolver), "utf-8");
96104
} else {

0 commit comments

Comments
 (0)