@marimo-team/marimo-export 0.0.0 → 0.0.1

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.
@@ -0,0 +1,10 @@
1
+ import { j as OutputLoader } from "../types-D52GHn1Q.mjs";
2
+ import { ExtractionOptions, Table } from "@uwdata/flechette";
3
+ //#region ../loader-arrow/src/index.d.ts
4
+ interface ArrowTableLoaderOptions {
5
+ readonly extraction?: ExtractionOptions;
6
+ }
7
+ /** Decode a verified Arrow IPC file as a Flechette table. */
8
+ declare function arrowTableLoader(options?: ArrowTableLoaderOptions): OutputLoader<"apache.arrow.file.v1", Table>;
9
+ //#endregion
10
+ export { ArrowTableLoaderOptions, arrowTableLoader };
@@ -0,0 +1,38 @@
1
+ import { n as defineOutputLoader } from "../loader-CDfnJ5Yb.mjs";
2
+ import { CompressionType, setCompressionCodec, tableFromIPC } from "@uwdata/flechette";
3
+ import { compress, decompress } from "lz4js";
4
+ //#region ../loader-arrow/src/index.ts
5
+ const MAX_DECOMPRESSED_BUFFER_BYTES = 536870912;
6
+ /** Decode a verified Arrow IPC file as a Flechette table. */
7
+ function arrowTableLoader(options = {}) {
8
+ const extraction = {
9
+ useBigInt: true,
10
+ ...options.extraction
11
+ };
12
+ return defineOutputLoader({
13
+ codec: "apache.arrow.file.v1",
14
+ accepts: (_descriptor, mediaType) => mediaType.essence === "application/vnd.apache.arrow.file",
15
+ load({ payload, signal }) {
16
+ signal?.throwIfAborted();
17
+ registerLz4();
18
+ const table = tableFromIPC(payload, extraction);
19
+ signal?.throwIfAborted();
20
+ return table;
21
+ }
22
+ });
23
+ }
24
+ function registerLz4() {
25
+ setCompressionCodec(CompressionType.LZ4_FRAME, {
26
+ decode(bytes, uncompressedLength) {
27
+ if (!Number.isSafeInteger(uncompressedLength) || uncompressedLength < 0 || uncompressedLength > MAX_DECOMPRESSED_BUFFER_BYTES) throw new TypeError("Arrow LZ4 buffer exceeds the decompression limit.");
28
+ const result = decompress(bytes, uncompressedLength);
29
+ if (result.byteLength !== uncompressedLength) throw new TypeError("Arrow LZ4 buffer length does not match its declaration.");
30
+ return result;
31
+ },
32
+ encode(bytes) {
33
+ return compress(bytes);
34
+ }
35
+ });
36
+ }
37
+ //#endregion
38
+ export { arrowTableLoader };
@@ -0,0 +1,6 @@
1
+ import { s as BlobAssetLoader } from "../types-D52GHn1Q.mjs";
2
+ //#region src/loader/html.d.ts
3
+ /** Decode a UTF-8 HTML BlobAsset into an inert string. */
4
+ declare const htmlLoader: () => BlobAssetLoader<string>;
5
+ //#endregion
6
+ export { htmlLoader };
@@ -0,0 +1,10 @@
1
+ import { t as defineBlobAssetLoader } from "../loader-CDfnJ5Yb.mjs";
2
+ import { n as hasUtf8Charset, t as decodeUtf8Blob } from "../utf8-DI-jI2Nm.mjs";
3
+ //#region src/loader/html.ts
4
+ /** Decode a UTF-8 HTML BlobAsset into an inert string. */
5
+ const htmlLoader = () => defineBlobAssetLoader({
6
+ mediaTypes: (mediaType) => mediaType.essence === "text/html" && hasUtf8Charset(mediaType),
7
+ load: decodeUtf8Blob
8
+ });
9
+ //#endregion
10
+ export { htmlLoader };
@@ -0,0 +1,6 @@
1
+ import { H as JsonValue, j as OutputLoader } from "../types-D52GHn1Q.mjs";
2
+ //#region src/loader/json.d.ts
3
+ /** Return one detached, immutable JSON projection. */
4
+ declare const jsonLoader: () => OutputLoader<"marimo.json.v1", JsonValue>;
5
+ //#endregion
6
+ export { jsonLoader };
@@ -0,0 +1,15 @@
1
+ import { _ as portableJsonValue, n as defineOutputLoader } from "../loader-CDfnJ5Yb.mjs";
2
+ //#region src/loader/json.ts
3
+ /** Return one detached, immutable JSON projection. */
4
+ const jsonLoader = () => defineOutputLoader({
5
+ codec: "marimo.json.v1",
6
+ accepts: (_descriptor, mediaType) => mediaType.essence === "application/vnd.marimo.json.v1+json",
7
+ load: ({ payload, signal }) => {
8
+ signal?.throwIfAborted();
9
+ const value = portableJsonValue(payload);
10
+ signal?.throwIfAborted();
11
+ return value;
12
+ }
13
+ });
14
+ //#endregion
15
+ export { jsonLoader };
@@ -0,0 +1,7 @@
1
+ import { j as OutputLoader } from "../types-D52GHn1Q.mjs";
2
+ import { o as MarimoCellSnapshot } from "../marimo-snapshot-Df66Xan0.mjs";
3
+ //#region src/loader/marimo-cell.d.ts
4
+ /** Decode one inert complete-cell snapshot. */
5
+ declare const marimoCellLoader: () => OutputLoader<"marimo.cell.v1", MarimoCellSnapshot>;
6
+ //#endregion
7
+ export { marimoCellLoader };
@@ -0,0 +1,16 @@
1
+ import { n as defineOutputLoader } from "../loader-CDfnJ5Yb.mjs";
2
+ import { t as parseMarimoCellSnapshot } from "../marimo-snapshot--nNjqcq_.mjs";
3
+ //#region src/loader/marimo-cell.ts
4
+ /** Decode one inert complete-cell snapshot. */
5
+ const marimoCellLoader = () => defineOutputLoader({
6
+ codec: "marimo.cell.v1",
7
+ accepts: (_descriptor, mediaType) => mediaType.essence === "application/vnd.marimo.cell.v1+json",
8
+ load: ({ payload, signal }) => {
9
+ signal?.throwIfAborted();
10
+ const snapshot = parseMarimoCellSnapshot(payload);
11
+ signal?.throwIfAborted();
12
+ return snapshot;
13
+ }
14
+ });
15
+ //#endregion
16
+ export { marimoCellLoader };
@@ -0,0 +1,7 @@
1
+ import { j as OutputLoader } from "../types-D52GHn1Q.mjs";
2
+ import { m as MarimoOutputSnapshot } from "../marimo-snapshot-Df66Xan0.mjs";
3
+ //#region src/loader/marimo-output.d.ts
4
+ /** Decode one inert Marimo output snapshot. */
5
+ declare const marimoOutputLoader: () => OutputLoader<"marimo.output.v1", MarimoOutputSnapshot>;
6
+ //#endregion
7
+ export { marimoOutputLoader };
@@ -0,0 +1,16 @@
1
+ import { n as defineOutputLoader } from "../loader-CDfnJ5Yb.mjs";
2
+ import { n as parseMarimoOutputSnapshot } from "../marimo-snapshot--nNjqcq_.mjs";
3
+ //#region src/loader/marimo-output.ts
4
+ /** Decode one inert Marimo output snapshot. */
5
+ const marimoOutputLoader = () => defineOutputLoader({
6
+ codec: "marimo.output.v1",
7
+ accepts: (_descriptor, mediaType) => mediaType.essence === "application/vnd.marimo.output.v1+json",
8
+ load: ({ payload, signal }) => {
9
+ signal?.throwIfAborted();
10
+ const snapshot = parseMarimoOutputSnapshot(payload);
11
+ signal?.throwIfAborted();
12
+ return snapshot;
13
+ }
14
+ });
15
+ //#endregion
16
+ export { marimoOutputLoader };
@@ -0,0 +1,19 @@
1
+ import { j as OutputLoader } from "../types-D52GHn1Q.mjs";
2
+ //#region ../loader-numpy/src/index.d.ts
3
+ type NumpyDTypeKind = "boolean" | "signed-integer" | "unsigned-integer" | "floating-point" | "complex-floating-point";
4
+ interface NumpyDType {
5
+ readonly descriptor: string;
6
+ readonly kind: NumpyDTypeKind;
7
+ readonly itemSize: number;
8
+ readonly byteOrder: "little" | "big" | "not-applicable";
9
+ }
10
+ interface NumpyArray {
11
+ readonly data: ArrayBufferView;
12
+ readonly shape: readonly number[];
13
+ readonly dtype: NumpyDType;
14
+ readonly fortranOrder: boolean;
15
+ }
16
+ /** Decode a verified portable NPY asset. */
17
+ declare function numpyLoader(): OutputLoader<"numpy.npy.v1", NumpyArray>;
18
+ //#endregion
19
+ export { NumpyArray, NumpyDType, NumpyDTypeKind, numpyLoader };
@@ -0,0 +1,168 @@
1
+ import { n as defineOutputLoader } from "../loader-CDfnJ5Yb.mjs";
2
+ //#region ../loader-numpy/src/index.ts
3
+ const MAGIC = [
4
+ 147,
5
+ 78,
6
+ 85,
7
+ 77,
8
+ 80,
9
+ 89
10
+ ];
11
+ const MAX_HEADER_BYTES = 1048576;
12
+ const MAX_TYPED_ARRAY_LENGTH = 4294967295;
13
+ const decoderLatin1 = new TextDecoder("latin1", { fatal: true });
14
+ const decoderUtf8 = new TextDecoder("utf-8", { fatal: true });
15
+ const hostLittleEndian = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
16
+ /** Decode a verified portable NPY asset. */
17
+ function numpyLoader() {
18
+ return defineOutputLoader({
19
+ codec: "numpy.npy.v1",
20
+ accepts: (_descriptor, mediaType) => mediaType.essence === "application/x-npy",
21
+ load({ payload, signal }) {
22
+ signal?.throwIfAborted();
23
+ const result = decodeNpy(payload);
24
+ signal?.throwIfAborted();
25
+ return result;
26
+ }
27
+ });
28
+ }
29
+ function decodeNpy(bytes) {
30
+ if (bytes.byteLength < 10 || MAGIC.some((value, index) => bytes[index] !== value)) throw new TypeError("NPY payload has invalid magic.");
31
+ const major = bytes[6];
32
+ const minor = bytes[7];
33
+ if (minor !== 0 || major !== 1 && major !== 2 && major !== 3) throw new TypeError(`NPY version ${major}.${minor} is unsupported.`);
34
+ const lengthBytes = major === 1 ? 2 : 4;
35
+ const headerStart = 8 + lengthBytes;
36
+ if (bytes.byteLength < headerStart) throw new TypeError("NPY header is truncated.");
37
+ const headerLength = lengthBytes === 2 ? readUint16(bytes, 8) : readUint32(bytes, 8);
38
+ if (headerLength === 0 || headerLength > MAX_HEADER_BYTES) throw new TypeError("NPY header length is outside the supported range.");
39
+ const headerEnd = headerStart + headerLength;
40
+ if (!Number.isSafeInteger(headerEnd) || headerEnd > bytes.byteLength) throw new TypeError("NPY header is truncated.");
41
+ const headerBytes = bytes.subarray(headerStart, headerEnd);
42
+ const parsed = parseHeader((major === 3 ? decoderUtf8 : decoderLatin1).decode(headerBytes).trim());
43
+ const dtype = parseDtype(parsed.descriptor);
44
+ const count = elementCount(parsed.shape);
45
+ if (headerEnd + safeProduct(count, dtype.itemSize, "NPY payload size") !== bytes.byteLength) throw new TypeError("NPY payload length does not match its shape and dtype.");
46
+ const data = decodePayload(bytes.subarray(headerEnd), count, dtype);
47
+ return Object.freeze({
48
+ data,
49
+ shape: Object.freeze(parsed.shape),
50
+ dtype: Object.freeze(dtype),
51
+ fortranOrder: parsed.fortranOrder
52
+ });
53
+ }
54
+ function parseHeader(header) {
55
+ const match = /^\{\s*['"]descr['"]\s*:\s*(['"])([^'"]+)\1\s*,\s*['"]fortran_order['"]\s*:\s*(True|False)\s*,\s*['"]shape['"]\s*:\s*\(([^)]*)\)\s*,?\s*\}$/u.exec(header);
56
+ if (match === null) throw new TypeError("NPY header literal is invalid.");
57
+ const dimensions = match[4].trim();
58
+ const shape = dimensions.length === 0 ? [] : dimensions.split(",").map((value) => value.trim()).filter((value) => value.length > 0).map((value) => {
59
+ if (!/^\d+$/u.test(value)) throw new TypeError("NPY shape is invalid.");
60
+ const dimension = Number(value);
61
+ if (!Number.isSafeInteger(dimension)) throw new TypeError("NPY shape is unsafe.");
62
+ return dimension;
63
+ });
64
+ return {
65
+ descriptor: match[2],
66
+ fortranOrder: match[3] === "True",
67
+ shape
68
+ };
69
+ }
70
+ function parseDtype(descriptor) {
71
+ const match = /^([<>=|])([?biufc])(\d*)$/u.exec(descriptor);
72
+ if (match === null) throw new TypeError(`NPY dtype ${JSON.stringify(descriptor)} is unsupported.`);
73
+ const marker = match[1];
74
+ const code = match[2];
75
+ const itemSize = Number(match[3] || "1");
76
+ if (!Number.isSafeInteger(itemSize) || itemSize <= 0) throw new TypeError("NPY dtype has an invalid item size.");
77
+ const kind = dtypeKind(code);
78
+ if (!(code === "?" && itemSize === 1 || code === "b" && itemSize === 1 || (code === "i" || code === "u") && [
79
+ 1,
80
+ 2,
81
+ 4,
82
+ 8
83
+ ].includes(itemSize) || code === "f" && [
84
+ 2,
85
+ 4,
86
+ 8
87
+ ].includes(itemSize) || code === "c" && [8, 16].includes(itemSize))) throw new TypeError(`NPY dtype ${JSON.stringify(descriptor)} is unsupported.`);
88
+ if (marker === "|" && itemSize !== 1) throw new TypeError("NPY no-endian dtype must use one-byte items.");
89
+ return {
90
+ descriptor,
91
+ kind,
92
+ itemSize,
93
+ byteOrder: itemSize === 1 ? "not-applicable" : marker === ">" ? "big" : marker === "<" ? "little" : hostLittleEndian ? "little" : "big"
94
+ };
95
+ }
96
+ function dtypeKind(code) {
97
+ if (code === "?" || code === "b") return "boolean";
98
+ if (code === "i") return "signed-integer";
99
+ if (code === "u") return "unsigned-integer";
100
+ if (code === "f") return "floating-point";
101
+ return "complex-floating-point";
102
+ }
103
+ function elementCount(shape) {
104
+ let count = 1;
105
+ for (const dimension of shape) count = safeProduct(count, dimension, "NPY shape");
106
+ return count;
107
+ }
108
+ function safeProduct(left, right, label) {
109
+ const result = left * right;
110
+ if (!Number.isSafeInteger(result) || result < 0) throw new TypeError(`${label} is unsafe.`);
111
+ return result;
112
+ }
113
+ function decodePayload(bytes, count, dtype) {
114
+ const code = dtype.descriptor[1];
115
+ if (count > MAX_TYPED_ARRAY_LENGTH) throw new TypeError("NPY array is too large.");
116
+ if (dtype.itemSize === 1) {
117
+ const copy = bytes.slice();
118
+ if (code === "i") return new Int8Array(copy.buffer);
119
+ return copy;
120
+ }
121
+ const littleEndian = dtype.byteOrder === "little";
122
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
123
+ if (code === "i") {
124
+ if (dtype.itemSize === 2) return fillNumbers(Int16Array, count, (offset) => view.getInt16(offset, littleEndian));
125
+ if (dtype.itemSize === 4) return fillNumbers(Int32Array, count, (offset) => view.getInt32(offset, littleEndian));
126
+ return fillBigInts(BigInt64Array, count, (offset) => view.getBigInt64(offset, littleEndian));
127
+ }
128
+ if (code === "u") {
129
+ if (dtype.itemSize === 2) return fillNumbers(Uint16Array, count, (offset) => view.getUint16(offset, littleEndian));
130
+ if (dtype.itemSize === 4) return fillNumbers(Uint32Array, count, (offset) => view.getUint32(offset, littleEndian));
131
+ return fillBigInts(BigUint64Array, count, (offset) => view.getBigUint64(offset, littleEndian));
132
+ }
133
+ if (code === "f") {
134
+ if (dtype.itemSize === 2) return fillNumbers(Float32Array, count, (offset) => decodeFloat16(view.getUint16(offset, littleEndian)), 2);
135
+ if (dtype.itemSize === 4) return fillNumbers(Float32Array, count, (offset) => view.getFloat32(offset, littleEndian));
136
+ return fillNumbers(Float64Array, count, (offset) => view.getFloat64(offset, littleEndian));
137
+ }
138
+ const components = safeProduct(count, 2, "NPY complex component count");
139
+ if (components > MAX_TYPED_ARRAY_LENGTH) throw new TypeError("NPY array is too large.");
140
+ if (dtype.itemSize === 8) return fillNumbers(Float32Array, components, (offset) => view.getFloat32(offset, littleEndian));
141
+ return fillNumbers(Float64Array, components, (offset) => view.getFloat64(offset, littleEndian));
142
+ }
143
+ function fillNumbers(Constructor, length, read, sourceStride = Constructor.BYTES_PER_ELEMENT) {
144
+ const result = new Constructor(length);
145
+ for (let index = 0; index < length; index += 1) result[index] = read(index * sourceStride);
146
+ return result;
147
+ }
148
+ function fillBigInts(Constructor, length, read) {
149
+ const result = new Constructor(length);
150
+ for (let index = 0; index < length; index += 1) result[index] = read(index * Constructor.BYTES_PER_ELEMENT);
151
+ return result;
152
+ }
153
+ function decodeFloat16(value) {
154
+ const sign = (value & 32768) === 0 ? 1 : -1;
155
+ const exponent = value >>> 10 & 31;
156
+ const fraction = value & 1023;
157
+ if (exponent === 0) return sign * 2 ** -14 * (fraction / 1024);
158
+ if (exponent === 31) return fraction === 0 ? sign * Infinity : NaN;
159
+ return sign * 2 ** (exponent - 15) * (1 + fraction / 1024);
160
+ }
161
+ function readUint16(bytes, offset) {
162
+ return bytes[offset] | bytes[offset + 1] << 8;
163
+ }
164
+ function readUint32(bytes, offset) {
165
+ return bytes[offset] + bytes[offset + 1] * 256 + bytes[offset + 2] * 65536 + bytes[offset + 3] * 16777216;
166
+ }
167
+ //#endregion
168
+ export { numpyLoader };
@@ -0,0 +1,14 @@
1
+ import { s as BlobAssetLoader } from "../types-D52GHn1Q.mjs";
2
+ import { Compressors, ParquetReadOptions } from "hyparquet";
3
+ //#region ../loader-parquet/src/index.d.ts
4
+ interface ParquetRowsLoaderOptions extends Omit<ParquetReadOptions, "file" | "onComplete" | "rowFormat"> {
5
+ readonly compressors?: Compressors;
6
+ }
7
+ type ParquetValue = null | boolean | number | bigint | string | Date | Uint8Array | readonly ParquetValue[] | ParquetRow;
8
+ interface ParquetRow {
9
+ readonly [column: string]: ParquetValue;
10
+ }
11
+ /** Read a verified Parquet BlobAsset into row objects. */
12
+ declare function parquetRowsLoader(options?: ParquetRowsLoaderOptions): BlobAssetLoader<readonly ParquetRow[]>;
13
+ //#endregion
14
+ export { type ParquetRow, type ParquetRowsLoaderOptions, type ParquetValue, parquetRowsLoader };
@@ -0,0 +1,43 @@
1
+ import { t as defineBlobAssetLoader } from "../loader-CDfnJ5Yb.mjs";
2
+ import { parquetReadObjects } from "hyparquet";
3
+ //#region ../loader-parquet/src/index.ts
4
+ /** Read a verified Parquet BlobAsset into row objects. */
5
+ function parquetRowsLoader(options = {}) {
6
+ return parquetRowsLoaderWith(parquetReadObjects, options);
7
+ }
8
+ /** @internal */
9
+ function parquetRowsLoaderWith(readObjects, options = {}) {
10
+ const defaults = { ...options };
11
+ return defineBlobAssetLoader({
12
+ mediaTypes: ["application/vnd.apache.parquet", "application/x-parquet"],
13
+ async load({ payload, signal }) {
14
+ signal?.throwIfAborted();
15
+ const data = payload.data.slice();
16
+ const rows = await raceAbort(readObjects({
17
+ ...defaults,
18
+ file: data.buffer
19
+ }), signal);
20
+ signal?.throwIfAborted();
21
+ return Object.freeze(rows);
22
+ }
23
+ });
24
+ }
25
+ async function raceAbort(task, signal) {
26
+ if (signal === void 0) return task;
27
+ signal.throwIfAborted();
28
+ let onAbort;
29
+ const aborted = new Promise((_resolve, reject) => {
30
+ onAbort = () => reject(abortReason(signal));
31
+ signal.addEventListener("abort", onAbort, { once: true });
32
+ });
33
+ try {
34
+ return await Promise.race([task, aborted]);
35
+ } finally {
36
+ if (onAbort !== void 0) signal.removeEventListener("abort", onAbort);
37
+ }
38
+ }
39
+ function abortReason(signal) {
40
+ return signal.reason instanceof Error ? signal.reason : Object.assign(/* @__PURE__ */ new Error("Parquet load was cancelled."), { name: "AbortError" });
41
+ }
42
+ //#endregion
43
+ export { parquetRowsLoader };
@@ -0,0 +1,6 @@
1
+ import { s as BlobAssetLoader } from "../types-D52GHn1Q.mjs";
2
+ //#region src/loader/text.d.ts
3
+ /** Decode a UTF-8 text BlobAsset. */
4
+ declare const textLoader: () => BlobAssetLoader<string>;
5
+ //#endregion
6
+ export { textLoader };
@@ -0,0 +1,10 @@
1
+ import { t as defineBlobAssetLoader } from "../loader-CDfnJ5Yb.mjs";
2
+ import { n as hasUtf8Charset, t as decodeUtf8Blob } from "../utf8-DI-jI2Nm.mjs";
3
+ //#region src/loader/text.ts
4
+ /** Decode a UTF-8 text BlobAsset. */
5
+ const textLoader = () => defineBlobAssetLoader({
6
+ mediaTypes: (mediaType) => mediaType.type === "text" && mediaType.essence !== "text/html" && hasUtf8Charset(mediaType),
7
+ load: decodeUtf8Blob
8
+ });
9
+ //#endregion
10
+ export { textLoader };
@@ -0,0 +1,18 @@
1
+ import { B as JsonObject, S as MountedView, s as BlobAssetLoader } from "../types-D52GHn1Q.mjs";
2
+ import { EmbedOptions, Result } from "vega-embed";
3
+ //#region ../loader-vegalite/src/index.d.ts
4
+ type VegaLiteSpec = Readonly<JsonObject>;
5
+ type VegaLiteMountOptions = EmbedOptions & {
6
+ readonly signal?: AbortSignal;
7
+ };
8
+ interface MountedVegaLite extends MountedView {
9
+ readonly result: Result;
10
+ }
11
+ interface VegaLiteChart {
12
+ readonly spec: VegaLiteSpec;
13
+ mount(element: HTMLElement, options?: VegaLiteMountOptions): Promise<MountedVegaLite>;
14
+ }
15
+ /** Load an exported Vega-Lite value and prepare it for browser mounting. */
16
+ declare function vegaLiteLoader(defaults?: EmbedOptions): BlobAssetLoader<VegaLiteChart>;
17
+ //#endregion
18
+ export { type MountedVegaLite, type VegaLiteChart, type VegaLiteMountOptions, type VegaLiteSpec, vegaLiteLoader };
@@ -0,0 +1,98 @@
1
+ import { g as portableJsonObject, t as defineBlobAssetLoader } from "../loader-CDfnJ5Yb.mjs";
2
+ //#region ../loader-vegalite/src/index.ts
3
+ const MEDIA_TYPE = /^application\/vnd\.vegalite\.v[1-9]\d*\+json$/u;
4
+ const OWNED_CLASSES = ["vega-embed", "has-actions"];
5
+ /** Load an exported Vega-Lite value and prepare it for browser mounting. */
6
+ function vegaLiteLoader(defaults = {}) {
7
+ return vegaLiteLoaderWith(async () => (await import("vega-embed")).default, defaults);
8
+ }
9
+ /** @internal */
10
+ function vegaLiteLoaderWith(loadEmbed, defaults = {}) {
11
+ const defaultOptions = { ...defaults };
12
+ return defineBlobAssetLoader({
13
+ mediaTypes: (mediaType) => MEDIA_TYPE.test(mediaType.essence),
14
+ load(input) {
15
+ return loadChart(input, defaultOptions, loadEmbed);
16
+ }
17
+ });
18
+ }
19
+ async function loadChart(input, defaults, loadEmbed) {
20
+ input.signal?.throwIfAborted();
21
+ const template = portableJsonObject(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(input.payload.data)), "Vega-Lite output");
22
+ input.signal?.throwIfAborted();
23
+ return Object.freeze({
24
+ spec: template,
25
+ async mount(element, options = {}) {
26
+ const { signal, ...embedOptions } = options;
27
+ signal?.throwIfAborted();
28
+ const container = element.ownerDocument.createElement("div");
29
+ element.replaceChildren(container);
30
+ let embedTask;
31
+ let result;
32
+ try {
33
+ const embed = await raceAbort(loadEmbed(), signal, "Vega-Lite mount was cancelled.");
34
+ signal?.throwIfAborted();
35
+ embedTask = embed(container, visualizationSpec(template), {
36
+ renderer: "canvas",
37
+ ...defaults,
38
+ ...embedOptions
39
+ });
40
+ result = await raceAbort(embedTask, signal, "Vega-Lite mount was cancelled.");
41
+ signal?.throwIfAborted();
42
+ } catch (error) {
43
+ if (embedTask !== void 0) embedTask.then((lateResult) => finalizeLate(lateResult, container), () => void 0);
44
+ clearMount(container);
45
+ throw error;
46
+ }
47
+ let disposed = false;
48
+ return Object.freeze({
49
+ result,
50
+ dispose() {
51
+ if (disposed) return;
52
+ disposed = true;
53
+ try {
54
+ result.finalize();
55
+ } finally {
56
+ clearMount(container);
57
+ }
58
+ }
59
+ });
60
+ }
61
+ });
62
+ }
63
+ async function raceAbort(task, signal, message) {
64
+ if (signal === void 0) return task;
65
+ signal.throwIfAborted();
66
+ let onAbort;
67
+ const aborted = new Promise((_resolve, reject) => {
68
+ onAbort = () => reject(abortReason(signal, message));
69
+ signal.addEventListener("abort", onAbort, { once: true });
70
+ });
71
+ try {
72
+ return await Promise.race([task, aborted]);
73
+ } finally {
74
+ if (onAbort !== void 0) signal.removeEventListener("abort", onAbort);
75
+ }
76
+ }
77
+ function abortReason(signal, message) {
78
+ return signal.reason instanceof Error ? signal.reason : Object.assign(new Error(message), { name: "AbortError" });
79
+ }
80
+ function finalizeLate(result, container) {
81
+ try {
82
+ result.finalize();
83
+ } catch (error) {
84
+ console.error("Vega-Lite finalized after its mount was cancelled.", error);
85
+ } finally {
86
+ clearMount(container);
87
+ }
88
+ }
89
+ function clearMount(container) {
90
+ container.replaceChildren();
91
+ container.classList.remove(...OWNED_CLASSES);
92
+ container.remove();
93
+ }
94
+ function visualizationSpec(value) {
95
+ return structuredClone(value);
96
+ }
97
+ //#endregion
98
+ export { vegaLiteLoader };