@observablehq/notebook-kit 1.1.0-rc.7 → 1.1.0-rc.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package.json +2 -2
- package/dist/src/databases/index.d.ts +4 -2
- package/dist/src/databases/index.js +35 -2
- package/dist/src/lib/hash.d.ts +2 -0
- package/dist/src/lib/hash.js +20 -0
- package/dist/src/lib/hash.test.d.ts +1 -0
- package/dist/src/lib/hash.test.js +28 -0
- package/dist/src/lib/sluggify.d.ts +6 -0
- package/dist/src/lib/sluggify.js +22 -0
- package/dist/src/lib/sluggify.test.d.ts +1 -0
- package/dist/src/lib/sluggify.test.js +51 -0
- package/dist/src/runtime/index.d.ts +2 -2
- package/dist/src/runtime/stdlib/databaseClient.d.ts +3 -4
- package/dist/src/runtime/stdlib/databaseClient.js +5 -11
- package/dist/src/runtime/stdlib/index.d.ts +2 -2
- package/dist/src/vite/observable.js +7 -26
- package/package.json +2 -2
package/dist/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/observablehq/notebook-kit.git"
|
|
7
7
|
},
|
|
8
|
-
"version": "1.1.0-rc.
|
|
8
|
+
"version": "1.1.0-rc.9",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"scripts": {
|
|
11
11
|
"test": "vitest",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"lint": "tsc --noEmit && eslint bin src types",
|
|
14
14
|
"notebooks": "tsx bin/notebooks.ts",
|
|
15
15
|
"download": "tsx bin/notebooks.ts download",
|
|
16
|
-
"docs:preview": "tsx --watch bin/notebooks.ts preview --root docs --template docs/observable.tmpl",
|
|
16
|
+
"docs:preview": "tsx --watch bin/notebooks.ts preview --base /notebook-kit/ --root docs --template docs/observable.tmpl",
|
|
17
17
|
"docs:build": "tsx bin/notebooks.ts build --root docs --template docs/observable.tmpl -- $(find docs -path 'docs/.observable' -prune -o -name '*.html' -print)"
|
|
18
18
|
},
|
|
19
19
|
"bin": {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ColumnSchema, QueryParam } from "../runtime/index.js";
|
|
2
|
+
export { hash as getQueryHash, nameHash as getNameHash } from "../lib/hash.js";
|
|
2
3
|
export type DatabaseConfig = DuckDBConfig | SnowflakeConfig | PostgresConfig;
|
|
3
4
|
export type DuckDBConfig = {
|
|
4
5
|
type: "duckdb";
|
|
@@ -29,12 +30,13 @@ export type PostgresConfig = {
|
|
|
29
30
|
export type DatabaseContext = {
|
|
30
31
|
cwd: string;
|
|
31
32
|
};
|
|
32
|
-
export type QueryTemplateFunction = (strings: string[], ...params: QueryParam[]) => Promise<SerializableQueryResult>;
|
|
33
|
+
export type QueryTemplateFunction = (strings: readonly string[], ...params: QueryParam[]) => Promise<SerializableQueryResult>;
|
|
33
34
|
export type SerializableQueryResult = {
|
|
34
35
|
rows: Record<string, unknown>[];
|
|
35
36
|
schema: ColumnSchema[];
|
|
36
37
|
duration: number;
|
|
37
38
|
date: Date;
|
|
38
39
|
};
|
|
40
|
+
export declare function getDatabaseConfig(sourcePath: string, databaseName: string): Promise<DatabaseConfig>;
|
|
39
41
|
export declare function getDatabase(config: DatabaseConfig, context: DatabaseContext): Promise<QueryTemplateFunction>;
|
|
40
|
-
export declare function
|
|
42
|
+
export declare function getQueryCachePath(sourcePath: string, databaseName: string, strings: readonly string[], ...params: unknown[]): Promise<string>;
|
|
@@ -1,3 +1,34 @@
|
|
|
1
|
+
import { createReadStream } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { json } from "node:stream/consumers";
|
|
4
|
+
import { isEnoent } from "../lib/error.js";
|
|
5
|
+
import { hash as getQueryHash, nameHash as getNameHash } from "../lib/hash.js";
|
|
6
|
+
export { hash as getQueryHash, nameHash as getNameHash } from "../lib/hash.js";
|
|
7
|
+
export async function getDatabaseConfig(sourcePath, databaseName) {
|
|
8
|
+
const sourceDir = dirname(sourcePath);
|
|
9
|
+
const configPath = join(sourceDir, ".observable", "databases.json");
|
|
10
|
+
let config;
|
|
11
|
+
try {
|
|
12
|
+
const configStream = createReadStream(configPath, "utf-8");
|
|
13
|
+
const configs = (await json(configStream));
|
|
14
|
+
config = configs[databaseName];
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
if (!isEnoent(error))
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
if (config === undefined) {
|
|
21
|
+
if (databaseName === "postgres")
|
|
22
|
+
config = { type: "postgres" };
|
|
23
|
+
else if (databaseName === "duckdb")
|
|
24
|
+
config = { type: "duckdb" };
|
|
25
|
+
else if (/\.(duck)?db$/i.test(databaseName))
|
|
26
|
+
config = { type: "duckdb", path: databaseName };
|
|
27
|
+
else
|
|
28
|
+
throw new Error(`database not found: ${databaseName}`);
|
|
29
|
+
}
|
|
30
|
+
return config;
|
|
31
|
+
}
|
|
1
32
|
export async function getDatabase(config, context) {
|
|
2
33
|
switch (config.type) {
|
|
3
34
|
case "duckdb":
|
|
@@ -10,6 +41,8 @@ export async function getDatabase(config, context) {
|
|
|
10
41
|
throw new Error(`unsupported database type: ${config["type"]}`);
|
|
11
42
|
}
|
|
12
43
|
}
|
|
13
|
-
export function
|
|
14
|
-
|
|
44
|
+
export async function getQueryCachePath(sourcePath, databaseName, strings, ...params) {
|
|
45
|
+
const sourceDir = dirname(sourcePath);
|
|
46
|
+
const cacheName = `${await getNameHash(databaseName)}-${await getQueryHash(strings, ...params)}.json`;
|
|
47
|
+
return join(sourceDir, ".observable", "cache", cacheName);
|
|
15
48
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { sluggify } from "./sluggify.js";
|
|
2
|
+
async function sha256(input) {
|
|
3
|
+
const encoded = new TextEncoder().encode(input);
|
|
4
|
+
const buffer = await crypto.subtle.digest("SHA-256", encoded);
|
|
5
|
+
return new Uint8Array(buffer).reduce((i, byte) => (i << 8n) | BigInt(byte), 0n);
|
|
6
|
+
}
|
|
7
|
+
function base36(int, length) {
|
|
8
|
+
return int.toString(36).padStart(length, "0").slice(0, length);
|
|
9
|
+
}
|
|
10
|
+
export async function hash(strings, ...params) {
|
|
11
|
+
return base36(await sha256(JSON.stringify([strings, ...params])), 16);
|
|
12
|
+
}
|
|
13
|
+
export async function nameHash(name) {
|
|
14
|
+
return /^[\w-]+$/.test(name)
|
|
15
|
+
? name
|
|
16
|
+
: `${sluggify(basename(name))}.${base36(await sha256(name), 8)}`;
|
|
17
|
+
}
|
|
18
|
+
function basename(name) {
|
|
19
|
+
return name.replace(/^.*\//, "");
|
|
20
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { assert, describe, test } from "vitest";
|
|
2
|
+
import { hash, nameHash } from "./hash.js";
|
|
3
|
+
describe("nameHash", () => {
|
|
4
|
+
test("returns a simple name as-is", async () => {
|
|
5
|
+
assert.strictEqual(await nameHash("foo"), "foo");
|
|
6
|
+
assert.strictEqual(await nameHash("foo-bar"), "foo-bar");
|
|
7
|
+
assert.strictEqual(await nameHash("foo-"), "foo-");
|
|
8
|
+
assert.strictEqual(await nameHash("-foo"), "-foo");
|
|
9
|
+
});
|
|
10
|
+
test("sluggifies and hashes names with special characters", async () => {
|
|
11
|
+
assert.strictEqual(await nameHash("foo.db"), "foo-db.2s9flvsm");
|
|
12
|
+
assert.strictEqual(await nameHash("./foo.db"), "foo-db.3ee6cmxd");
|
|
13
|
+
assert.strictEqual(await nameHash("data/foo.db"), "foo-db.61nrbwb0");
|
|
14
|
+
assert.strictEqual(await nameHash("bar/foo.db"), "foo-db.1jlqjad7");
|
|
15
|
+
assert.strictEqual(await nameHash("foo bar"), "foo-bar.69w36b7f");
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
describe("hash", () => {
|
|
19
|
+
test("returns the expected static hash", async () => {
|
|
20
|
+
assert.strictEqual(await hash `foo`, "1n7k0l3ilxvth9al");
|
|
21
|
+
assert.strictEqual(await hash `bar`, "39v7mmkf7sfehxh1");
|
|
22
|
+
assert.strictEqual(await hash ``, "gepym5nmvuej8503");
|
|
23
|
+
});
|
|
24
|
+
test("returns the expected dynamic hash", async () => {
|
|
25
|
+
assert.strictEqual(await hash `SELECT 1 + ${2}`, "64iqby4orqj5tgek");
|
|
26
|
+
assert.strictEqual(await hash `SELECT 1 + ${3}`, "1azi8mazfb39kln7");
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function sluggify(string, { length = 50, fallback = "untitled", separator = "-" } = {}) {
|
|
2
|
+
const parts = string
|
|
3
|
+
.normalize("NFD")
|
|
4
|
+
.replace(/[\u0300-\u036f'‘’]/g, "")
|
|
5
|
+
.toLowerCase()
|
|
6
|
+
.split(/\W+/g)
|
|
7
|
+
.filter(nonempty);
|
|
8
|
+
let i = -1;
|
|
9
|
+
for (let l = 0, n = parts.length; ++i < n;) {
|
|
10
|
+
if ((l += parts[i].length) + i > length) {
|
|
11
|
+
parts[i] = parts[i].substring(0, length - l + parts[i].length - i);
|
|
12
|
+
break;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return (parts
|
|
16
|
+
.slice(0, i + 1)
|
|
17
|
+
.filter(Boolean)
|
|
18
|
+
.join(separator) || fallback.slice(0, length));
|
|
19
|
+
}
|
|
20
|
+
function nonempty(string) {
|
|
21
|
+
return string.length > 0;
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { assert, test } from "vitest";
|
|
2
|
+
import { sluggify } from "./sluggify.js";
|
|
3
|
+
test("returns the default fallback for empty slugs", () => {
|
|
4
|
+
assert.strictEqual(sluggify(""), "untitled");
|
|
5
|
+
assert.strictEqual(sluggify(" "), "untitled");
|
|
6
|
+
assert.strictEqual(sluggify("---"), "untitled");
|
|
7
|
+
assert.strictEqual(sluggify("##)!@#(*"), "untitled");
|
|
8
|
+
});
|
|
9
|
+
test("returns the given fallback for empty slugs", () => {
|
|
10
|
+
assert.strictEqual(sluggify("", { fallback: "foo" }), "foo");
|
|
11
|
+
assert.strictEqual(sluggify(" ", { fallback: "foo" }), "foo");
|
|
12
|
+
assert.strictEqual(sluggify("---", { fallback: "foo" }), "foo");
|
|
13
|
+
assert.strictEqual(sluggify("##)!@#(*", { fallback: "foo" }), "foo");
|
|
14
|
+
});
|
|
15
|
+
test("lowercases", () => {
|
|
16
|
+
assert.strictEqual(sluggify("HELLO WORLD"), "hello-world");
|
|
17
|
+
assert.strictEqual(sluggify("HelLo WorlD"), "hello-world");
|
|
18
|
+
});
|
|
19
|
+
test("removes emoji", () => {
|
|
20
|
+
assert.strictEqual(sluggify("HELLO 😎"), "hello");
|
|
21
|
+
assert.strictEqual(sluggify("HELLO 😎 world"), "hello-world");
|
|
22
|
+
assert.strictEqual(sluggify("HELLO 💩 world"), "hello-world");
|
|
23
|
+
});
|
|
24
|
+
test("trims leading and trailing spaces", () => {
|
|
25
|
+
assert.strictEqual(sluggify(" hello world "), "hello-world");
|
|
26
|
+
});
|
|
27
|
+
test("collapses contiguous spaces", () => {
|
|
28
|
+
assert.strictEqual(sluggify(" hello world "), "hello-world");
|
|
29
|
+
});
|
|
30
|
+
test("removes punctuation", () => {
|
|
31
|
+
assert.strictEqual(sluggify("Hello, world!"), "hello-world");
|
|
32
|
+
assert.strictEqual(sluggify("Hello, 'world'!"), "hello-world");
|
|
33
|
+
assert.strictEqual(sluggify('Hello, "world"!'), "hello-world");
|
|
34
|
+
assert.strictEqual(sluggify("Hello, “world”!"), "hello-world");
|
|
35
|
+
assert.strictEqual(sluggify("Hello, ‘world’!"), "hello-world");
|
|
36
|
+
assert.strictEqual(sluggify("Hello, fo'c's'le!"), "hello-focsle");
|
|
37
|
+
assert.strictEqual(sluggify("Hello, fo’c’s’le!"), "hello-focsle");
|
|
38
|
+
});
|
|
39
|
+
test("removes diacritics and combiners", () => {
|
|
40
|
+
assert.strictEqual(sluggify("Héllö, wørld!"), "hello-w-rld");
|
|
41
|
+
assert.strictEqual(sluggify("z̷̢̡̟͍̺͛͆͐̀ą̸̻̰̪͈͒͝ͅl̸͇̘̓g̶̡͈͒̾̉̽̑̅ö̸̧̟́͆"), "zalgo");
|
|
42
|
+
});
|
|
43
|
+
test("allows up to 50 characters after stripping", () => {
|
|
44
|
+
assert.strictEqual(sluggify("‘A‘ohe pu‘u ki‘eki‘e ke ho ‘ā‘o ‘ia e pi‘i"), "aohe-puu-kiekie-ke-ho-ao-ia-e-pii");
|
|
45
|
+
assert.strictEqual(sluggify("0123456789012345678901234567890123456789012345678"), "0123456789012345678901234567890123456789012345678");
|
|
46
|
+
assert.strictEqual(sluggify("01234567890123456789012345678901234567890123456789"), "01234567890123456789012345678901234567890123456789");
|
|
47
|
+
assert.strictEqual(sluggify("012345678901234567890123456789012345678901234567890"), "01234567890123456789012345678901234567890123456789");
|
|
48
|
+
assert.strictEqual(sluggify("01234567890 1234567890 1234567890 1234567890 12345678"), "01234567890-1234567890-1234567890-1234567890-12345");
|
|
49
|
+
assert.strictEqual(sluggify("01234567890 1234567890 1234567890 1234567890 123456789"), "01234567890-1234567890-1234567890-1234567890-12345");
|
|
50
|
+
assert.strictEqual(sluggify("01234567890 1234567890 1234567890 1234567890 1234567890"), "01234567890-1234567890-1234567890-1234567890-12345");
|
|
51
|
+
});
|
|
@@ -58,12 +58,12 @@ export declare class NotebookRuntime {
|
|
|
58
58
|
width: () => AsyncGenerator<number, void, unknown>;
|
|
59
59
|
DatabaseClient: () => {
|
|
60
60
|
(name: string, options?: import("./stdlib/databaseClient.js").QueryOptionsSpec): import("./stdlib/databaseClient.js").DatabaseClient;
|
|
61
|
-
hash: (strings: string[], ...params: unknown[]) => Promise<string>;
|
|
62
61
|
revive: ({ rows, schema, date, ...meta }: import("../databases/index.js").SerializableQueryResult) => import("./stdlib/databaseClient.js").QueryResult;
|
|
63
62
|
prototype: {
|
|
64
63
|
readonly name: string;
|
|
65
64
|
readonly options: import("./stdlib/databaseClient.js").QueryOptions;
|
|
66
|
-
sql(strings: string[], ...params: import("./stdlib/databaseClient.js").QueryParam[]): Promise<import("./stdlib/databaseClient.js").QueryResult>;
|
|
65
|
+
sql(strings: readonly string[], ...params: import("./stdlib/databaseClient.js").QueryParam[]): Promise<import("./stdlib/databaseClient.js").QueryResult>;
|
|
66
|
+
cachePath(strings: readonly string[], ...params: import("./stdlib/databaseClient.js").QueryParam[]): Promise<string>;
|
|
67
67
|
};
|
|
68
68
|
};
|
|
69
69
|
FileAttachment: () => {
|
|
@@ -27,11 +27,10 @@ export interface QueryOptions extends QueryOptionsSpec {
|
|
|
27
27
|
export interface DatabaseClient {
|
|
28
28
|
readonly name: string;
|
|
29
29
|
readonly options: QueryOptions;
|
|
30
|
-
sql(strings: string[], ...params: QueryParam[]): Promise<QueryResult>;
|
|
30
|
+
sql(strings: readonly string[], ...params: QueryParam[]): Promise<QueryResult>;
|
|
31
31
|
}
|
|
32
32
|
export declare const DatabaseClient: {
|
|
33
33
|
(name: string, options?: QueryOptionsSpec): DatabaseClient;
|
|
34
|
-
hash: typeof hash;
|
|
35
34
|
revive: typeof revive;
|
|
36
35
|
prototype: DatabaseClientImpl;
|
|
37
36
|
};
|
|
@@ -39,8 +38,8 @@ declare class DatabaseClientImpl implements DatabaseClient {
|
|
|
39
38
|
readonly name: string;
|
|
40
39
|
readonly options: QueryOptions;
|
|
41
40
|
constructor(name: string, options: QueryOptions);
|
|
42
|
-
sql(strings: string[], ...params: QueryParam[]): Promise<QueryResult>;
|
|
41
|
+
sql(strings: readonly string[], ...params: QueryParam[]): Promise<QueryResult>;
|
|
42
|
+
cachePath(strings: readonly string[], ...params: QueryParam[]): Promise<string>;
|
|
43
43
|
}
|
|
44
|
-
declare function hash(strings: string[], ...params: unknown[]): Promise<string>;
|
|
45
44
|
declare function revive({ rows, schema, date, ...meta }: SerializableQueryResult): QueryResult;
|
|
46
45
|
export {};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
+
import { hash, nameHash } from "../../lib/hash.js";
|
|
1
2
|
export const DatabaseClient = (name, options) => {
|
|
2
|
-
if (!/^[\w-]+$/.test(name))
|
|
3
|
-
throw new Error(`invalid database: ${name}`);
|
|
4
3
|
return new DatabaseClientImpl(name, normalizeOptions(options));
|
|
5
4
|
};
|
|
6
5
|
function normalizeOptions({ id, since } = {}) {
|
|
@@ -31,19 +30,15 @@ class DatabaseClientImpl {
|
|
|
31
30
|
});
|
|
32
31
|
}
|
|
33
32
|
async sql(strings, ...params) {
|
|
34
|
-
const path =
|
|
33
|
+
const path = await this.cachePath(strings, ...params);
|
|
35
34
|
const response = await fetch(path);
|
|
36
35
|
if (!response.ok)
|
|
37
36
|
throw new Error(`failed to fetch: ${path}`);
|
|
38
37
|
return await response.json().then(revive);
|
|
39
38
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const buffer = await crypto.subtle.digest("SHA-256", encoded);
|
|
44
|
-
const int = new Uint8Array(buffer).reduce((i, byte) => (i << 8n) | BigInt(byte), 0n);
|
|
45
|
-
const length = 16;
|
|
46
|
-
return int.toString(36).padStart(length, "0").slice(0, length);
|
|
39
|
+
async cachePath(strings, ...params) {
|
|
40
|
+
return `.observable/cache/${await nameHash(this.name)}-${await hash(strings, ...params)}.json`;
|
|
41
|
+
}
|
|
47
42
|
}
|
|
48
43
|
function revive({ rows, schema, date, ...meta }) {
|
|
49
44
|
for (const column of schema) {
|
|
@@ -77,7 +72,6 @@ function revive({ rows, schema, date, ...meta }) {
|
|
|
77
72
|
function asDate(value) {
|
|
78
73
|
return new Date(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(?::\d{2})?$/.test(value) ? value + "Z" : value);
|
|
79
74
|
}
|
|
80
|
-
DatabaseClient.hash = hash;
|
|
81
75
|
DatabaseClient.revive = revive;
|
|
82
76
|
DatabaseClient.prototype = DatabaseClientImpl.prototype; // instanceof
|
|
83
77
|
Object.defineProperty(DatabaseClientImpl, "name", { value: "DatabaseClient" }); // prevent mangling
|
|
@@ -48,12 +48,12 @@ export declare const library: {
|
|
|
48
48
|
width: () => AsyncGenerator<number, void, unknown>;
|
|
49
49
|
DatabaseClient: () => {
|
|
50
50
|
(name: string, options?: import("./databaseClient.js").QueryOptionsSpec): DatabaseClient;
|
|
51
|
-
hash: (strings: string[], ...params: unknown[]) => Promise<string>;
|
|
52
51
|
revive: ({ rows, schema, date, ...meta }: import("../../databases/index.js").SerializableQueryResult) => import("./databaseClient.js").QueryResult;
|
|
53
52
|
prototype: {
|
|
54
53
|
readonly name: string;
|
|
55
54
|
readonly options: import("./databaseClient.js").QueryOptions;
|
|
56
|
-
sql(strings: string[], ...params: import("./databaseClient.js").QueryParam[]): Promise<import("./databaseClient.js").QueryResult>;
|
|
55
|
+
sql(strings: readonly string[], ...params: import("./databaseClient.js").QueryParam[]): Promise<import("./databaseClient.js").QueryResult>;
|
|
56
|
+
cachePath(strings: readonly string[], ...params: import("./databaseClient.js").QueryParam[]): Promise<string>;
|
|
57
57
|
};
|
|
58
58
|
};
|
|
59
59
|
FileAttachment: () => {
|
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
2
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
-
import { json } from "node:stream/consumers";
|
|
4
3
|
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { relative } from "node:path/posix";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
import { JSDOM } from "jsdom";
|
|
7
|
-
import { getDatabase,
|
|
8
|
-
import { isEnoent } from "../lib/error.js";
|
|
7
|
+
import { getDatabase, getDatabaseConfig, getQueryCachePath } from "../databases/index.js";
|
|
9
8
|
import { deserialize } from "../lib/serialize.js";
|
|
10
9
|
import { Sourcemap } from "../javascript/sourcemap.js";
|
|
11
10
|
import { transpile } from "../javascript/transpile.js";
|
|
12
11
|
import { parseTemplate } from "../javascript/template.js";
|
|
13
12
|
import { collectAssets } from "../runtime/stdlib/assets.js";
|
|
14
|
-
import { DatabaseClient } from "../runtime/stdlib/databaseClient.js";
|
|
15
13
|
import { highlight } from "../runtime/stdlib/highlight.js";
|
|
16
14
|
import { MarkdownRenderer } from "../runtime/stdlib/md.js";
|
|
17
15
|
export function observable({ window = new JSDOM().window, parser = new window.DOMParser(), template = fileURLToPath(import.meta.resolve("../templates/default.html")), transformTemplate = (template) => template, transformNotebook = (notebook) => notebook } = {}) {
|
|
@@ -69,30 +67,13 @@ export function observable({ window = new JSDOM().window, parser = new window.DO
|
|
|
69
67
|
const template = parseTemplate(value);
|
|
70
68
|
if (!template.expressions.length) {
|
|
71
69
|
const dir = dirname(context.filename);
|
|
72
|
-
const
|
|
73
|
-
const hash = await DatabaseClient.hash.call(null, [value]);
|
|
74
|
-
const cacheName = `${cell.database}-${hash}.json`;
|
|
75
|
-
const cachePath = join(cacheDir, cacheName);
|
|
70
|
+
const cachePath = await getQueryCachePath(context.filename, cell.database, [value]);
|
|
76
71
|
if (!existsSync(cachePath)) {
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
const configPath = join(dir, ".observable", "databases.json");
|
|
80
|
-
const configStream = createReadStream(configPath, "utf-8");
|
|
81
|
-
const configs = (await json(configStream));
|
|
82
|
-
config = configs[cell.database];
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
if (!isEnoent(error))
|
|
86
|
-
throw error;
|
|
87
|
-
}
|
|
88
|
-
if (isDefaultDatabase(cell.database))
|
|
89
|
-
config ?? (config = { type: cell.database });
|
|
90
|
-
if (!config)
|
|
91
|
-
throw new Error(`database not found: ${cell.database}`);
|
|
72
|
+
const config = await getDatabaseConfig(context.filename, cell.database);
|
|
92
73
|
try {
|
|
93
74
|
const database = await getDatabase(config, { cwd: dir });
|
|
94
75
|
const results = await database.call(null, [value]);
|
|
95
|
-
await mkdir(
|
|
76
|
+
await mkdir(dirname(cachePath), { recursive: true });
|
|
96
77
|
await writeFile(cachePath, JSON.stringify(results));
|
|
97
78
|
}
|
|
98
79
|
catch (error) {
|
|
@@ -100,7 +81,7 @@ export function observable({ window = new JSDOM().window, parser = new window.DO
|
|
|
100
81
|
}
|
|
101
82
|
}
|
|
102
83
|
cell.mode = "js";
|
|
103
|
-
cell.value = `FileAttachment(${JSON.stringify(
|
|
84
|
+
cell.value = `FileAttachment(${JSON.stringify(relative(dir, cachePath))}).json().then(DatabaseClient.revive)${hidden ? "" : `.then(Inputs.table)${cell.output ? ".then(view)" : ""}`}`;
|
|
104
85
|
}
|
|
105
86
|
}
|
|
106
87
|
collectAssets(assets, div);
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/observablehq/notebook-kit.git"
|
|
7
7
|
},
|
|
8
|
-
"version": "1.1.0-rc.
|
|
8
|
+
"version": "1.1.0-rc.9",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"scripts": {
|
|
11
11
|
"test": "vitest",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"lint": "tsc --noEmit && eslint bin src types",
|
|
14
14
|
"notebooks": "tsx bin/notebooks.ts",
|
|
15
15
|
"download": "tsx bin/notebooks.ts download",
|
|
16
|
-
"docs:preview": "tsx --watch bin/notebooks.ts preview --root docs --template docs/observable.tmpl",
|
|
16
|
+
"docs:preview": "tsx --watch bin/notebooks.ts preview --base /notebook-kit/ --root docs --template docs/observable.tmpl",
|
|
17
17
|
"docs:build": "tsx bin/notebooks.ts build --root docs --template docs/observable.tmpl -- $(find docs -path 'docs/.observable' -prune -o -name '*.html' -print)"
|
|
18
18
|
},
|
|
19
19
|
"bin": {
|