@akohn/dashql-react 0.0.20-dev.673.g3732f233a → 0.0.20-dev.675.gf906d82d4
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/README.md +85 -0
- package/dist/platform/content_length.d.ts +1 -0
- package/dist/platform/content_length.js +7 -0
- package/dist/platform/core_provider.d.ts +18 -0
- package/dist/platform/core_provider.js +106 -0
- package/dist/platform/embedded_database_provider.d.ts +19 -0
- package/dist/platform/embedded_database_provider.js +53 -0
- package/dist/platform/page_runtime.d.ts +3 -0
- package/dist/platform/page_runtime.js +29 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -34,6 +34,91 @@ import { useScriptModel } from '@akohn/dashql-react/models/script_model';
|
|
|
34
34
|
next to the modules (`new URL('./symbols.svg', import.meta.url)`), which the bundler of the host emits as an asset.
|
|
35
35
|
The atlas is served by the host: browsers resolve the `<use>` of a symbol only from the origin of the page.
|
|
36
36
|
|
|
37
|
+
## A page with a runtime
|
|
38
|
+
|
|
39
|
+
`platform/` holds what a page needs for a runtime: the core, the embedded Hyper and the runtime of the page. The
|
|
40
|
+
files of the core and of the engine are assets of the host, it passes their URLs. The embedded Hyper needs a
|
|
41
|
+
cross-origin isolated page (`Cross-Origin-Opener-Policy: same-origin`, `Cross-Origin-Embedder-Policy: require-corp`)
|
|
42
|
+
and a script of the page's origin that loads the worker of the engine:
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
// hyperdb_worker_bridge.js, served by the host
|
|
46
|
+
const workerUrl = new URL(self.location.href).searchParams.get('workerUrl');
|
|
47
|
+
if (workerUrl == null) throw new Error('Missing HyperDB worker URL');
|
|
48
|
+
self.importScripts(workerUrl);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
import coreWasm from '@akohn/dashql/wasm?url';
|
|
53
|
+
import { BufferLogger } from '@akohn/dashql/platform/logger/logger';
|
|
54
|
+
import { setupWebHyperDB } from '@akohn/dashql/platform/hyperdb/hyperdb_web';
|
|
55
|
+
import { migrateStorage } from '@akohn/dashql/platform/storage/storage_migration';
|
|
56
|
+
import { openPageStorage } from '@akohn/dashql/platform/storage/page_storage';
|
|
57
|
+
import { createWorkbookResultFiles } from '@akohn/dashql/platform/workbook/workbook_result_files';
|
|
58
|
+
import { WebHttpClient } from '@akohn/dashql/platform/web_http_client';
|
|
59
|
+
import { createEmbeddedChannelConnector } from '@akohn/dashql/runtime/embedded_channels';
|
|
60
|
+
import { createRuntime } from '@akohn/dashql/runtime/runtime';
|
|
61
|
+
import { FRAME_UPDATE_SCHEDULER } from '@akohn/dashql/runtime/store/update_scheduler';
|
|
62
|
+
import { createRuntimeArchives } from '@akohn/dashql/runtime/workbook_archives';
|
|
63
|
+
import { RuntimeContextProvider } from '@akohn/dashql-react/hooks';
|
|
64
|
+
import { LoggerContext } from '@akohn/dashql-react/logger';
|
|
65
|
+
import { DashQLCoreProvider, useDashQLCore, useDashQLCoreSetup } from '@akohn/dashql-react/platform/core_provider';
|
|
66
|
+
import { EmbeddedDatabaseProvider, useEmbeddedDatabaseSetup } from '@akohn/dashql-react/platform/embedded_database_provider';
|
|
67
|
+
import { usePageRuntime } from '@akohn/dashql-react/platform/page_runtime';
|
|
68
|
+
|
|
69
|
+
const HYPERDB = {
|
|
70
|
+
engine: hyperdbEngineUrl, // hyperdb-wasm/dist/hyperdb-wasm.js
|
|
71
|
+
wasm: hyperdbWasmUrl, // hyperdb-wasm/dist/hyperdb-wasm.wasm
|
|
72
|
+
worker: hyperdbWorkerUrl, // hyperdb-wasm/dist/browser_worker.js
|
|
73
|
+
workerBridge: '/hyperdb_worker_bridge.js',
|
|
74
|
+
};
|
|
75
|
+
const logger = new BufferLogger();
|
|
76
|
+
|
|
77
|
+
export const Page = () => (
|
|
78
|
+
<LoggerContext.Provider value={logger}>
|
|
79
|
+
<DashQLCoreProvider wasmUrl={coreWasm}>
|
|
80
|
+
<EmbeddedDatabaseProvider setupDatabase={(context, logger, progress, options) => setupWebHyperDB(context, logger, HYPERDB, progress, options)}>
|
|
81
|
+
<Console />
|
|
82
|
+
</EmbeddedDatabaseProvider>
|
|
83
|
+
</DashQLCoreProvider>
|
|
84
|
+
</LoggerContext.Provider>
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
function Console() {
|
|
88
|
+
const setupCore = useDashQLCoreSetup();
|
|
89
|
+
const core = useDashQLCore();
|
|
90
|
+
// Migrate the storage before anything reads it, a fresh one too, then open it
|
|
91
|
+
const [storage, setStorage] = React.useState<Awaited<ReturnType<typeof openPageStorage>> | null>(null);
|
|
92
|
+
React.useEffect(() => {
|
|
93
|
+
void migrateStorage(logger, setupCore).then(async () => setStorage(await openPageStorage(await setupCore('storage'), logger)));
|
|
94
|
+
}, [setupCore]);
|
|
95
|
+
if (core == null || storage == null) return <Loading />;
|
|
96
|
+
return <ConsoleRuntime core={core} storage={storage} />;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function ConsoleRuntime(props: { core: DashQL; storage: StorageBackend }) {
|
|
100
|
+
const database = useEmbeddedDatabaseSetup();
|
|
101
|
+
const runtime = usePageRuntime(() => createRuntime({
|
|
102
|
+
logger,
|
|
103
|
+
scheduler: FRAME_UPDATE_SCHEDULER,
|
|
104
|
+
core: props.core,
|
|
105
|
+
storage: props.storage,
|
|
106
|
+
http: new WebHttpClient(),
|
|
107
|
+
channels: createEmbeddedChannelConnector(database),
|
|
108
|
+
computeDatabase: () => database('compute'),
|
|
109
|
+
results: createWorkbookResultFiles(),
|
|
110
|
+
archives: handles => createRuntimeArchives(handles, database),
|
|
111
|
+
settings: { capabilities: [] },
|
|
112
|
+
}));
|
|
113
|
+
return <RuntimeContextProvider value={runtime}>{/* the hooks and models of the console */}</RuntimeContextProvider>;
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Only one tab of an origin may write its storage: `AppOwnership` of `@akohn/dashql/platform/app_ownership` holds a Web
|
|
118
|
+
Lock for the tab and hands over to another one. `settings.capabilities` turns off the extensions of SQL that the
|
|
119
|
+
host doesn't offer ([capabilities.md](../../docs/design/capabilities.md)). The OAuth flow of a Salesforce connection is
|
|
120
|
+
not in the packages yet.
|
|
121
|
+
|
|
37
122
|
How the layers fit, the runtime, the models and the components, is described in
|
|
38
123
|
[dashql_runtime.md](../../docs/design/dashql_runtime.md) and
|
|
39
124
|
[writing_components.md](../../docs/agents/writing_components.md).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function decodedContentLength(response: Response): number;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
function decodedContentLength(response) {
|
|
2
|
+
const encoding = response.headers.get("content-encoding");
|
|
3
|
+
if (encoding != null && encoding.trim().toLowerCase() !== "identity") return 0;
|
|
4
|
+
const length = parseInt(response.headers.get("content-length") ?? "", 10);
|
|
5
|
+
return Number.isFinite(length) && length > 0 ? length : 0;
|
|
6
|
+
}
|
|
7
|
+
export { decodedContentLength };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as dashql from '@akohn/dashql';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { type CoreSetup } from '../host.js';
|
|
4
|
+
export interface InstantiationProgress {
|
|
5
|
+
startedAt: Date;
|
|
6
|
+
updatedAt: Date;
|
|
7
|
+
bytesTotal: bigint;
|
|
8
|
+
bytesLoaded: bigint;
|
|
9
|
+
}
|
|
10
|
+
interface Props {
|
|
11
|
+
wasmUrl: string | URL;
|
|
12
|
+
children?: React.ReactNode;
|
|
13
|
+
}
|
|
14
|
+
export declare function DashQLCoreProvider(props: Props): React.FunctionComponentElement<React.ProviderProps<import("../host.js").Host>>;
|
|
15
|
+
export declare const useDashQLCoreSetupProgress: () => InstantiationProgress | null;
|
|
16
|
+
export declare function useDashQLCoreSetup(): CoreSetup;
|
|
17
|
+
export declare function useDashQLCore(): dashql.DashQL | null;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { HostProvider, useHost } from "../host.js";
|
|
2
|
+
import { useLogger } from "../logger.js";
|
|
3
|
+
import { decodedContentLength } from "./content_length.js";
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
import { stringifyError } from "@akohn/dashql/platform/logger/logger";
|
|
6
|
+
import * as dashql from "@akohn/dashql";
|
|
7
|
+
import { createTrace } from "@akohn/dashql/platform/logger/trace_context";
|
|
8
|
+
var PROGRESS_CONTEXT = React.createContext(null);
|
|
9
|
+
var CORE_CONTEXT = React.createContext(null);
|
|
10
|
+
function DashQLCoreProvider(props) {
|
|
11
|
+
const logger = useLogger();
|
|
12
|
+
const outer = useHost();
|
|
13
|
+
const wasmUrl = props.wasmUrl.toString();
|
|
14
|
+
const instantiation = React.useRef(null);
|
|
15
|
+
const lifecycle = React.useRef({ disposed: false });
|
|
16
|
+
const [progress, setProgress] = React.useState(null);
|
|
17
|
+
const [core, setCore] = React.useState(null);
|
|
18
|
+
const instantiator = React.useCallback(async (context) => {
|
|
19
|
+
const owner = lifecycle.current;
|
|
20
|
+
if (owner.disposed) throw new DOMException("Core provider was disposed", "AbortError");
|
|
21
|
+
if (instantiation.current != null) return await instantiation.current;
|
|
22
|
+
const now = /* @__PURE__ */ new Date();
|
|
23
|
+
const internal = {
|
|
24
|
+
startedAt: now,
|
|
25
|
+
updatedAt: now,
|
|
26
|
+
bytesTotal: BigInt(0),
|
|
27
|
+
bytesLoaded: BigInt(0)
|
|
28
|
+
};
|
|
29
|
+
const fetchWithProgress = async (url, traced) => {
|
|
30
|
+
traced.info("Fetching core wasm", { "context": context }, "core");
|
|
31
|
+
const response = await fetch(new Request(url));
|
|
32
|
+
if (!response.ok) throw new Error(`Failed to fetch core wasm: ${response.status} ${response.statusText}`);
|
|
33
|
+
const contentLength = decodedContentLength(response);
|
|
34
|
+
const now = /* @__PURE__ */ new Date();
|
|
35
|
+
internal.startedAt = now;
|
|
36
|
+
internal.updatedAt = now;
|
|
37
|
+
internal.bytesTotal = BigInt(contentLength) || BigInt(0);
|
|
38
|
+
internal.bytesLoaded = BigInt(0);
|
|
39
|
+
return new Response(response.body?.pipeThrough(new TransformStream({ transform(chunk, ctrl) {
|
|
40
|
+
const prevUpdate = internal.updatedAt;
|
|
41
|
+
internal.updatedAt = /* @__PURE__ */ new Date();
|
|
42
|
+
internal.bytesLoaded += BigInt(chunk.byteLength);
|
|
43
|
+
if (internal.updatedAt.getTime() - prevUpdate.getTime() > 20) setProgress((_) => ({ ...internal }));
|
|
44
|
+
ctrl.enqueue(chunk);
|
|
45
|
+
} })), response);
|
|
46
|
+
};
|
|
47
|
+
const instantiate = async () => {
|
|
48
|
+
const traced = logger.withTrace(createTrace());
|
|
49
|
+
const initStart = performance.now();
|
|
50
|
+
try {
|
|
51
|
+
traced.info("Loading core Wasm", { "context": context }, "core");
|
|
52
|
+
const response = await fetchWithProgress(wasmUrl, traced);
|
|
53
|
+
const wasmBinary = new Uint8Array(await response.arrayBuffer());
|
|
54
|
+
const instance = await dashql.DashQL.create({ wasmBinary });
|
|
55
|
+
if (owner.disposed) throw new DOMException("Core provider was disposed", "AbortError");
|
|
56
|
+
traced.info("Instantiated core", {
|
|
57
|
+
"context": context,
|
|
58
|
+
"duration": Math.floor(performance.now() - initStart).toString()
|
|
59
|
+
}, "core");
|
|
60
|
+
setProgress((_) => ({
|
|
61
|
+
...internal,
|
|
62
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
63
|
+
}));
|
|
64
|
+
return instance;
|
|
65
|
+
} catch (e) {
|
|
66
|
+
traced.error("Failed to instantiate core", {
|
|
67
|
+
"error": stringifyError(e),
|
|
68
|
+
"duration": Math.floor(performance.now() - initStart).toString()
|
|
69
|
+
}, "core");
|
|
70
|
+
throw e;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
instantiation.current = instantiate();
|
|
74
|
+
instantiation.current.then((instance) => {
|
|
75
|
+
if (!owner.disposed) setCore(instance);
|
|
76
|
+
}, () => {});
|
|
77
|
+
return await instantiation.current;
|
|
78
|
+
}, [logger, wasmUrl]);
|
|
79
|
+
React.useEffect(() => {
|
|
80
|
+
if (lifecycle.current.disposed) lifecycle.current = { disposed: false };
|
|
81
|
+
const owner = lifecycle.current;
|
|
82
|
+
return () => {
|
|
83
|
+
owner.disposed = true;
|
|
84
|
+
instantiation.current = null;
|
|
85
|
+
};
|
|
86
|
+
}, []);
|
|
87
|
+
const host = React.useMemo(() => ({
|
|
88
|
+
...outer,
|
|
89
|
+
setupCore: instantiator
|
|
90
|
+
}), [outer, instantiator]);
|
|
91
|
+
return React.createElement(HostProvider, { value: host }, React.createElement(PROGRESS_CONTEXT.Provider, { value: progress }, React.createElement(CORE_CONTEXT.Provider, { value: core }, props.children)));
|
|
92
|
+
}
|
|
93
|
+
var useDashQLCoreSetupProgress = () => React.useContext(PROGRESS_CONTEXT);
|
|
94
|
+
function useDashQLCoreSetup() {
|
|
95
|
+
return useHost().setupCore;
|
|
96
|
+
}
|
|
97
|
+
function useDashQLCore() {
|
|
98
|
+
const setup = useHost().setupCore;
|
|
99
|
+
const core = React.useContext(CORE_CONTEXT);
|
|
100
|
+
const [instantiated] = React.useState(() => setup == null ? dashql.DashQL.instantiated() : null);
|
|
101
|
+
React.useEffect(() => {
|
|
102
|
+
if (setup != null && core == null) setup("runtime").catch(() => {});
|
|
103
|
+
}, [setup, core]);
|
|
104
|
+
return setup == null ? instantiated : core;
|
|
105
|
+
}
|
|
106
|
+
export { DashQLCoreProvider, useDashQLCore, useDashQLCoreSetup, useDashQLCoreSetupProgress };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { EmbeddedComputeDatabase } from '@akohn/dashql/platform/embedded_database';
|
|
3
|
+
import type { EmbeddedDatabaseOptions, SetupProgress } from '@akohn/dashql/platform/hyperdb/hyperdb_web';
|
|
4
|
+
import type { Logger } from '@akohn/dashql/platform/logger/logger';
|
|
5
|
+
export type { EmbeddedDatabaseOptions, SetupProgress };
|
|
6
|
+
export type EmbeddedDatabaseFactory = (context: string, logger: Logger, onSetupProgress?: (progress: SetupProgress) => void, options?: EmbeddedDatabaseOptions) => Promise<EmbeddedComputeDatabase>;
|
|
7
|
+
export type EmbeddedDatabaseSetupFn = (context: string, onSetupProgress?: (progress: SetupProgress) => void) => Promise<EmbeddedComputeDatabase>;
|
|
8
|
+
interface EmbeddedDatabase {
|
|
9
|
+
setup: EmbeddedDatabaseSetupFn;
|
|
10
|
+
terminate: () => Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
interface Props {
|
|
13
|
+
setupDatabase: EmbeddedDatabaseFactory;
|
|
14
|
+
options?: EmbeddedDatabaseOptions | null;
|
|
15
|
+
children?: React.ReactNode;
|
|
16
|
+
}
|
|
17
|
+
export declare function EmbeddedDatabaseProvider(props: Props): React.FunctionComponentElement<React.ProviderProps<EmbeddedDatabase | null>>;
|
|
18
|
+
export declare function useEmbeddedDatabaseSetup(): EmbeddedDatabaseSetupFn;
|
|
19
|
+
export declare function useEmbeddedDatabaseTermination(): () => Promise<void>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { useLogger } from "../logger.js";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
var DATABASE_CTX = React.createContext(null);
|
|
4
|
+
var DEFAULT_OPTIONS = {};
|
|
5
|
+
function EmbeddedDatabaseProvider(props) {
|
|
6
|
+
const logger = useLogger();
|
|
7
|
+
const instantiation = React.useRef(null);
|
|
8
|
+
const [options, resolveOptions] = React.useMemo(() => {
|
|
9
|
+
let resolve;
|
|
10
|
+
return [new Promise((r) => {
|
|
11
|
+
resolve = r;
|
|
12
|
+
}), resolve];
|
|
13
|
+
}, []);
|
|
14
|
+
const knownOptions = props.options === void 0 ? DEFAULT_OPTIONS : props.options;
|
|
15
|
+
React.useEffect(() => {
|
|
16
|
+
if (knownOptions != null) resolveOptions(knownOptions);
|
|
17
|
+
}, [knownOptions, resolveOptions]);
|
|
18
|
+
const { setupDatabase } = props;
|
|
19
|
+
const setup = React.useCallback(async (context, onSetupProgress) => {
|
|
20
|
+
if (instantiation.current != null) return await instantiation.current;
|
|
21
|
+
const instantiate = async () => setupDatabase(context, logger, onSetupProgress, await options);
|
|
22
|
+
instantiation.current = instantiate().catch((error) => {
|
|
23
|
+
instantiation.current = null;
|
|
24
|
+
throw error;
|
|
25
|
+
});
|
|
26
|
+
return await instantiation.current;
|
|
27
|
+
}, [
|
|
28
|
+
logger,
|
|
29
|
+
setupDatabase,
|
|
30
|
+
options
|
|
31
|
+
]);
|
|
32
|
+
const terminate = React.useCallback(async () => {
|
|
33
|
+
const pending = instantiation.current;
|
|
34
|
+
instantiation.current = null;
|
|
35
|
+
if (pending) await (await pending.catch(() => null))?.terminate();
|
|
36
|
+
}, []);
|
|
37
|
+
React.useEffect(() => () => {
|
|
38
|
+
terminate().catch(() => {});
|
|
39
|
+
}, [terminate]);
|
|
40
|
+
const database = React.useMemo(() => ({
|
|
41
|
+
setup,
|
|
42
|
+
terminate
|
|
43
|
+
}), [setup, terminate]);
|
|
44
|
+
return React.createElement(DATABASE_CTX.Provider, { value: database }, props.children);
|
|
45
|
+
}
|
|
46
|
+
function useEmbeddedDatabaseSetup() {
|
|
47
|
+
return React.useContext(DATABASE_CTX)?.setup ?? null;
|
|
48
|
+
}
|
|
49
|
+
function useEmbeddedDatabaseTermination() {
|
|
50
|
+
return React.useContext(DATABASE_CTX)?.terminate ?? NO_TERMINATION;
|
|
51
|
+
}
|
|
52
|
+
var NO_TERMINATION = async () => {};
|
|
53
|
+
export { EmbeddedDatabaseProvider, useEmbeddedDatabaseSetup, useEmbeddedDatabaseTermination };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useOwnedRuntime } from "../hooks.js";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { stringifyError } from "@akohn/dashql/platform/logger/logger";
|
|
4
|
+
function usePageRuntime(create) {
|
|
5
|
+
const runtime = useOwnedRuntime(create);
|
|
6
|
+
React.useEffect(() => runtime.computeDatabase.prepare(), [runtime]);
|
|
7
|
+
React.useEffect(() => {
|
|
8
|
+
const flush = (closing) => void flushRuntimeStorage(runtime, closing);
|
|
9
|
+
const flushIfHidden = () => {
|
|
10
|
+
if (document.visibilityState === "hidden") flush(false);
|
|
11
|
+
};
|
|
12
|
+
const flushOnPageHide = (event) => flush(!event.persisted);
|
|
13
|
+
window.addEventListener("pagehide", flushOnPageHide);
|
|
14
|
+
document.addEventListener("visibilitychange", flushIfHidden);
|
|
15
|
+
return () => {
|
|
16
|
+
window.removeEventListener("pagehide", flushOnPageHide);
|
|
17
|
+
document.removeEventListener("visibilitychange", flushIfHidden);
|
|
18
|
+
};
|
|
19
|
+
}, [runtime]);
|
|
20
|
+
return runtime;
|
|
21
|
+
}
|
|
22
|
+
async function flushRuntimeStorage(runtime, closing) {
|
|
23
|
+
try {
|
|
24
|
+
await runtime.storage.flush({ closing });
|
|
25
|
+
} catch (error) {
|
|
26
|
+
runtime.logger.error("Failed to flush the storage writer", { error: stringifyError(error) }, "runtime");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export { flushRuntimeStorage, usePageRuntime };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akohn/dashql-react",
|
|
3
|
-
"version": "0.0.20-dev.
|
|
3
|
+
"version": "0.0.20-dev.675.gf906d82d4",
|
|
4
4
|
"description": "DashQL in React: the hooks of the runtime of @akohn/dashql, the models that components show, the components, and the design tokens, fonts and icons of DashQL",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"react-window": "^2.5.3"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@akohn/dashql": "0.0.20-dev.
|
|
30
|
+
"@akohn/dashql": "0.0.20-dev.675.gf906d82d4",
|
|
31
31
|
"apache-arrow": "^21.2.0",
|
|
32
32
|
"react": "^19.3.1",
|
|
33
33
|
"react-dom": "^19.3.1",
|