Skip to content

Commit

Permalink
tidy duckdb manifest; no top-level await
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 2, 2024
1 parent 02dd892 commit 27bc864
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 48 deletions.
42 changes: 21 additions & 21 deletions src/client/stdlib/duckdb.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global DUCKDB_MANIFEST */
import * as duckdb from "npm:@duckdb/duckdb-wasm";

// Adapted from https://observablehq.com/@cmudig/duckdb-client
Expand Down Expand Up @@ -29,25 +30,21 @@ import * as duckdb from "npm:@duckdb/duckdb-wasm";
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

// Baked-in manifest.
// eslint-disable-next-line no-undef
const manifest = DUCKDB_MANIFEST;
const candidates = {
...(manifest.bundles.includes("mvp") && {
mvp: {
mainModule: import.meta.resolve("npm:@duckdb/duckdb-wasm/dist/duckdb-mvp.wasm"),
mainWorker: import.meta.resolve("npm:@duckdb/duckdb-wasm/dist/duckdb-browser-mvp.worker.js")
}
}),
...(manifest.bundles.includes("eh") && {
eh: {
mainModule: import.meta.resolve("npm:@duckdb/duckdb-wasm/dist/duckdb-eh.wasm"),
mainWorker: import.meta.resolve("npm:@duckdb/duckdb-wasm/dist/duckdb-browser-eh.worker.js")
}
})
const bundles = {
mvp: DUCKDB_MANIFEST.platforms.mvp
? {
mainModule: import.meta.resolve("npm:@duckdb/duckdb-wasm/dist/duckdb-mvp.wasm"),
mainWorker: import.meta.resolve("npm:@duckdb/duckdb-wasm/dist/duckdb-browser-mvp.worker.js")
}
: undefined,
eh: DUCKDB_MANIFEST.platforms.eh
? {
mainModule: import.meta.resolve("npm:@duckdb/duckdb-wasm/dist/duckdb-eh.wasm"),
mainWorker: import.meta.resolve("npm:@duckdb/duckdb-wasm/dist/duckdb-browser-eh.worker.js")
}
: undefined
};
const bundle = await duckdb.selectBundle(candidates);
const activePlatform = manifest.bundles.find((key) => bundle.mainModule === candidates[key].mainModule);
const bundle = duckdb.selectBundle(bundles);
const logger = new duckdb.ConsoleLogger(duckdb.LogLevel.WARNING);

let db;
Expand Down Expand Up @@ -190,10 +187,12 @@ export class DuckDBClient {
Object.defineProperty(DuckDBClient.prototype, "dialect", {value: "duckdb"});

async function registerExtensions(db, extensions) {
const {mainModule} = await bundle;
const platform = Object.keys(bundles).find((platform) => mainModule === bundles[platform].mainModule);
const con = await db.connect();
try {
await Promise.all(
manifest.extensions.map(([name, {[activePlatform]: ref, load}]) =>
Object.entries(DUCKDB_MANIFEST.extensions).map(([name, {load, [platform]: ref}]) =>
con
.query(`INSTALL "${name}" FROM '${import.meta.resolve(ref)}'`)
.then(() => (extensions === undefined ? load : extensions.includes(name)) && con.query(`LOAD "${name}"`))
Expand Down Expand Up @@ -328,9 +327,10 @@ async function insertArray(database, name, array, options) {
}

async function createDuckDB() {
const worker = await duckdb.createWorker(bundle.mainWorker);
const {mainWorker, mainModule} = await bundle;
const worker = await duckdb.createWorker(mainWorker);
const db = new duckdb.AsyncDuckDB(logger, worker);
await db.instantiate(bundle.mainModule);
await db.instantiate(mainModule);
return db;
}

Expand Down
8 changes: 4 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {pathToFileURL} from "node:url";
import he from "he";
import type MarkdownIt from "markdown-it";
import wrapAnsi from "wrap-ansi";
import {DUCKDB_BUNDLES, DUCKDB_CORE_EXTENSIONS} from "./duckdb.js";
import {DUCKDB_CORE_EXTENSIONS, DUCKDB_PLATFORMS} from "./duckdb.js";
import {visitFiles} from "./files.js";
import {formatIsoDate, formatLocaleDate} from "./format.js";
import type {FrontMatter} from "./frontMatter.js";
Expand Down Expand Up @@ -78,7 +78,7 @@ export interface SearchConfigSpec {
}

export interface DuckDBConfig {
bundles: string[];
platforms: Record<string, true>;
extensions: {[name: string]: DuckDBExtensionConfig};
}

Expand Down Expand Up @@ -522,7 +522,7 @@ export function stringOrNull(spec: unknown): string | null {
return spec == null || spec === false ? null : String(spec);
}

// TODO configure bundles?
// TODO configure platforms?
function normalizeDuckDB(spec: unknown): DuckDBConfig {
const extensions: {[name: string]: DuckDBExtensionConfig} = {};
let extspec: Record<string, unknown> = spec?.["extensions"] ?? {};
Expand All @@ -548,7 +548,7 @@ function normalizeDuckDB(spec: unknown): DuckDBConfig {
load: Boolean(load)
};
}
return {bundles: DUCKDB_BUNDLES, extensions};
return {platforms: DUCKDB_PLATFORMS, extensions};
}

function normalizeDuckDBSource(source: string): string {
Expand Down
44 changes: 23 additions & 21 deletions src/duckdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const downloadRequests = new Map<string, Promise<string>>();

export const DUCKDB_WASM_VERSION = "1.29.0";
export const DUCKDB_VERSION = "1.1.1";
export const DUCKDB_BUNDLES = ["eh", "mvp"];
export const DUCKDB_PLATFORMS: DuckDBConfig["platforms"] = {eh: true, mvp: true};

// https://duckdb.org/docs/extensions/core_extensions.html
export const DUCKDB_CORE_EXTENSIONS: [name: string, autoload: boolean][] = [
Expand Down Expand Up @@ -37,30 +37,32 @@ export const DUCKDB_CORE_EXTENSIONS: [name: string, autoload: boolean][] = [
];

export async function getDuckDBManifest(
duckdb: DuckDBConfig,
{platforms, extensions}: DuckDBConfig,
{root, aliases}: {root: string; aliases?: Map<string, string>}
) {
return {
bundles: duckdb.bundles,
extensions: await Promise.all(
Array.from(Object.entries(duckdb.extensions), ([name, {install, load, source}]) =>
(async () => [
name,
{
install,
load,
...Object.fromEntries(
await Promise.all(
duckdb.bundles.map(async (platform) => [
platform,
install
? await getDuckDBExtension(root, resolveDuckDBExtension(source, platform, name), aliases)
: source
])
platforms,
extensions: Object.fromEntries(
await Promise.all(
Object.entries(extensions).map(([name, {install, load, source}]) =>
(async () => [
name,
{
install,
load,
...Object.fromEntries(
await Promise.all(
Object.keys(platforms).map(async (platform) => [
platform,
install
? await getDuckDBExtension(root, resolveDuckDBExtension(source, platform, name), aliases)
: source
])
)
)
)
}
])()
}
])()
)
)
)
};
Expand Down
2 changes: 1 addition & 1 deletion src/libraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function getImplicitDownloads(imports: Iterable<string>, duckdb?: DuckDBC
implicits.add("npm:@duckdb/duckdb-wasm/dist/duckdb-browser-eh.worker.js");
if (!duckdb) throw new Error("Implementation error: missing duckdb configuration");
for (const [name, {source}] of Object.entries(duckdb.extensions)) {
for (const platform of duckdb.bundles) {
for (const platform in duckdb.platforms) {
implicits.add(`duckdb:${resolveDuckDBExtension(source, platform, name)}`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/libraries-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("getImplicitStylesheets(imports)", () => {
describe("getImplicitDownloads(imports)", () => {
it("supports known imports", () => {
assert.deepStrictEqual(
getImplicitDownloads(["npm:@observablehq/duckdb"], {extensions: {}, bundles: []}),
getImplicitDownloads(["npm:@observablehq/duckdb"]),
new Set([
"npm:@duckdb/duckdb-wasm/dist/duckdb-mvp.wasm",
"npm:@duckdb/duckdb-wasm/dist/duckdb-browser-mvp.worker.js",
Expand Down

0 comments on commit 27bc864

Please sign in to comment.