@modernrelay/orbit-data 0.2.0
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/LICENSE +21 -0
- package/dist/arrow.d.ts +48 -0
- package/dist/arrow.js +71 -0
- package/dist/arrow.js.map +1 -0
- package/dist/chunk-H22YHC2C.js +455 -0
- package/dist/chunk-H22YHC2C.js.map +1 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.js +450 -0
- package/dist/index.js.map +1 -0
- package/dist/parquet.d.ts +34 -0
- package/dist/parquet.js +74 -0
- package/dist/parquet.js.map +1 -0
- package/dist/types-Bf3sle3D.d.ts +88 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ModernRelay
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/arrow.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { G as GraphByteSource, a as GraphColumnMapping, b as GraphPrepareOptions, P as PreparedGraph } from './types-Bf3sle3D.js';
|
|
2
|
+
import '@modernrelay/orbit-core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Arrow format entry (T04) — `@modernrelay/orbit-data/arrow`.
|
|
6
|
+
*
|
|
7
|
+
* NEVER imported by the root entry (spec §5.2/§18 bundle isolation): the
|
|
8
|
+
* 'apache-arrow' optional peer is loaded with a DYNAMIC import inside the
|
|
9
|
+
* call, so this module is import-safe without it (the clear install-hint
|
|
10
|
+
* error fires only when preparation actually runs), and root-entry bundles
|
|
11
|
+
* carry no arrow code.
|
|
12
|
+
*
|
|
13
|
+
* Sources per role are Arrow IPC bytes (file or stream framing —
|
|
14
|
+
* Blob/ArrayBuffer/AsyncIterable<Uint8Array>) or an already-constructed
|
|
15
|
+
* arrow Table (duck-typed via `schema.fields`). Tables become row objects
|
|
16
|
+
* through `getChild` vectors and feed the shared builder; columns come from
|
|
17
|
+
* the SCHEMA (exact, not sampled). Value normalization for the object lane:
|
|
18
|
+
* arrow nulls → absent keys, bigints → number when safely representable else
|
|
19
|
+
* decimal string (JSON-safe artifacts), everything else passes through.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** Structural subset of an apache-arrow Table (any supported major). */
|
|
23
|
+
interface ArrowTableLike {
|
|
24
|
+
readonly numRows: number;
|
|
25
|
+
readonly schema: {
|
|
26
|
+
readonly fields: ReadonlyArray<{
|
|
27
|
+
readonly name: string;
|
|
28
|
+
}>;
|
|
29
|
+
};
|
|
30
|
+
getChild(name: string): {
|
|
31
|
+
get(index: number): unknown;
|
|
32
|
+
} | null;
|
|
33
|
+
}
|
|
34
|
+
type ArrowGraphSource = GraphByteSource | ArrowTableLike;
|
|
35
|
+
type ArrowGraphPrepareInput = {
|
|
36
|
+
nodes: ArrowGraphSource;
|
|
37
|
+
edges: ArrowGraphSource;
|
|
38
|
+
} | {
|
|
39
|
+
edges: ArrowGraphSource;
|
|
40
|
+
deriveNodes: true;
|
|
41
|
+
};
|
|
42
|
+
/** Test seam: lets suites simulate the missing-optional-dependency path. */
|
|
43
|
+
declare const _internals: {
|
|
44
|
+
importArrow: () => Promise<unknown>;
|
|
45
|
+
};
|
|
46
|
+
declare function prepareArrowGraphData<N = Record<string, unknown>, E = Record<string, unknown>>(input: ArrowGraphPrepareInput, mapping: GraphColumnMapping, options: Omit<GraphPrepareOptions, 'format'>): Promise<PreparedGraph<N, E>>;
|
|
47
|
+
|
|
48
|
+
export { type ArrowGraphPrepareInput, type ArrowGraphSource, type ArrowTableLike, _internals, prepareArrowGraphData };
|
package/dist/arrow.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { buildPrepared, collectBytes, EMPTY_ROW_TABLE } from './chunk-H22YHC2C.js';
|
|
2
|
+
|
|
3
|
+
// src/arrow.ts
|
|
4
|
+
var _internals = {
|
|
5
|
+
importArrow: () => import('apache-arrow')
|
|
6
|
+
};
|
|
7
|
+
async function loadArrowModule() {
|
|
8
|
+
let mod;
|
|
9
|
+
try {
|
|
10
|
+
mod = await _internals.importArrow();
|
|
11
|
+
} catch (cause) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
"@modernrelay/orbit-data/arrow requires the optional peer dependency 'apache-arrow'. Install it (e.g. `pnpm add apache-arrow`) to use prepareArrowGraphData.",
|
|
14
|
+
{ cause }
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
const tableFromIPC = mod["tableFromIPC"];
|
|
18
|
+
if (typeof tableFromIPC !== "function") {
|
|
19
|
+
throw new Error(
|
|
20
|
+
"@modernrelay/orbit-data/arrow: the installed 'apache-arrow' does not expose tableFromIPC (unsupported version?)"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
return { tableFromIPC };
|
|
24
|
+
}
|
|
25
|
+
async function prepareArrowGraphData(input, mapping, options) {
|
|
26
|
+
const arrow = await loadArrowModule();
|
|
27
|
+
const signal = options.signal;
|
|
28
|
+
const deriveNodes = "deriveNodes" in input && input.deriveNodes === true;
|
|
29
|
+
const edgeTable = await arrowRowTable(input.edges, arrow, signal);
|
|
30
|
+
const nodeTable = deriveNodes ? null : await arrowRowTable(input.nodes, arrow, signal);
|
|
31
|
+
return await buildPrepared({
|
|
32
|
+
nodeTable,
|
|
33
|
+
edgeTable,
|
|
34
|
+
mapping,
|
|
35
|
+
format: "arrow",
|
|
36
|
+
options: { ...options, format: "arrow" }
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function isArrowTable(source) {
|
|
40
|
+
return typeof source === "object" && source !== null && "schema" in source && typeof source.getChild === "function" && Array.isArray(source.schema?.fields);
|
|
41
|
+
}
|
|
42
|
+
async function arrowRowTable(source, arrow, signal) {
|
|
43
|
+
const table = isArrowTable(source) ? source : arrow.tableFromIPC(await collectBytes(source, signal));
|
|
44
|
+
const columns = table.schema.fields.map((f) => f.name);
|
|
45
|
+
if (columns.length === 0 && table.numRows === 0) return EMPTY_ROW_TABLE;
|
|
46
|
+
const vectors = columns.map((name) => table.getChild(name));
|
|
47
|
+
return {
|
|
48
|
+
columns,
|
|
49
|
+
rows: (async function* () {
|
|
50
|
+
for (let i = 0; i < table.numRows; i++) {
|
|
51
|
+
const row = {};
|
|
52
|
+
for (let j = 0; j < columns.length; j++) {
|
|
53
|
+
const value = vectors[j]?.get(i);
|
|
54
|
+
if (value === null || value === void 0) continue;
|
|
55
|
+
row[columns[j]] = normalizeArrowValue(value);
|
|
56
|
+
}
|
|
57
|
+
yield row;
|
|
58
|
+
}
|
|
59
|
+
})()
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function normalizeArrowValue(value) {
|
|
63
|
+
if (typeof value === "bigint") {
|
|
64
|
+
return value >= BigInt(Number.MIN_SAFE_INTEGER) && value <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(value) : value.toString();
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { _internals, prepareArrowGraphData };
|
|
70
|
+
//# sourceMappingURL=arrow.js.map
|
|
71
|
+
//# sourceMappingURL=arrow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/arrow.ts"],"names":[],"mappings":"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","file":"arrow.js","sourcesContent":["/**\n * Arrow format entry (T04) — `@modernrelay/orbit-data/arrow`.\n *\n * NEVER imported by the root entry (spec §5.2/§18 bundle isolation): the\n * 'apache-arrow' optional peer is loaded with a DYNAMIC import inside the\n * call, so this module is import-safe without it (the clear install-hint\n * error fires only when preparation actually runs), and root-entry bundles\n * carry no arrow code.\n *\n * Sources per role are Arrow IPC bytes (file or stream framing —\n * Blob/ArrayBuffer/AsyncIterable<Uint8Array>) or an already-constructed\n * arrow Table (duck-typed via `schema.fields`). Tables become row objects\n * through `getChild` vectors and feed the shared builder; columns come from\n * the SCHEMA (exact, not sampled). Value normalization for the object lane:\n * arrow nulls → absent keys, bigints → number when safely representable else\n * decimal string (JSON-safe artifacts), everything else passes through.\n */\n\nimport { buildPrepared } from './builder';\nimport { EMPTY_ROW_TABLE, type RowTable } from './rowTable';\nimport { collectBytes } from './sources';\nimport type {\n GraphByteSource,\n GraphColumnMapping,\n GraphPrepareOptions,\n PreparedGraph,\n} from './types';\n\n/** Structural subset of an apache-arrow Table (any supported major). */\nexport interface ArrowTableLike {\n readonly numRows: number;\n readonly schema: { readonly fields: ReadonlyArray<{ readonly name: string }> };\n getChild(name: string): { get(index: number): unknown } | null;\n}\n\nexport type ArrowGraphSource = GraphByteSource | ArrowTableLike;\n\nexport type ArrowGraphPrepareInput =\n | { nodes: ArrowGraphSource; edges: ArrowGraphSource }\n | { edges: ArrowGraphSource; deriveNodes: true };\n\n/** Test seam: lets suites simulate the missing-optional-dependency path. */\nexport const _internals = {\n importArrow: (): Promise<unknown> => import('apache-arrow'),\n};\n\nasync function loadArrowModule(): Promise<{\n tableFromIPC(bytes: Uint8Array): ArrowTableLike;\n}> {\n let mod: unknown;\n try {\n mod = await _internals.importArrow();\n } catch (cause) {\n throw new Error(\n \"@modernrelay/orbit-data/arrow requires the optional peer dependency 'apache-arrow'. \" +\n 'Install it (e.g. `pnpm add apache-arrow`) to use prepareArrowGraphData.',\n { cause },\n );\n }\n const tableFromIPC = (mod as Record<string, unknown>)['tableFromIPC'];\n if (typeof tableFromIPC !== 'function') {\n throw new Error(\n \"@modernrelay/orbit-data/arrow: the installed 'apache-arrow' does not expose tableFromIPC \" +\n '(unsupported version?)',\n );\n }\n return { tableFromIPC: tableFromIPC as (bytes: Uint8Array) => ArrowTableLike };\n}\n\nexport async function prepareArrowGraphData<\n N = Record<string, unknown>,\n E = Record<string, unknown>,\n>(\n input: ArrowGraphPrepareInput,\n mapping: GraphColumnMapping,\n options: Omit<GraphPrepareOptions, 'format'>,\n): Promise<PreparedGraph<N, E>> {\n const arrow = await loadArrowModule();\n const signal = options.signal;\n const deriveNodes = 'deriveNodes' in input && input.deriveNodes === true;\n const edgeTable = await arrowRowTable(input.edges, arrow, signal);\n const nodeTable = deriveNodes\n ? null\n : await arrowRowTable((input as { nodes: ArrowGraphSource }).nodes, arrow, signal);\n return (await buildPrepared({\n nodeTable,\n edgeTable,\n mapping,\n format: 'arrow',\n options: { ...options, format: 'arrow' },\n })) as PreparedGraph<N, E>;\n}\n\nfunction isArrowTable(source: ArrowGraphSource): source is ArrowTableLike {\n return (\n typeof source === 'object' &&\n source !== null &&\n 'schema' in source &&\n typeof (source as ArrowTableLike).getChild === 'function' &&\n Array.isArray((source as ArrowTableLike).schema?.fields)\n );\n}\n\nasync function arrowRowTable(\n source: ArrowGraphSource,\n arrow: { tableFromIPC(bytes: Uint8Array): ArrowTableLike },\n signal: AbortSignal | undefined,\n): Promise<RowTable> {\n const table = isArrowTable(source)\n ? source\n : arrow.tableFromIPC(await collectBytes(source as GraphByteSource, signal));\n const columns = table.schema.fields.map((f) => f.name);\n if (columns.length === 0 && table.numRows === 0) return EMPTY_ROW_TABLE;\n const vectors = columns.map((name) => table.getChild(name));\n return {\n columns,\n rows: (async function* () {\n for (let i = 0; i < table.numRows; i++) {\n const row: Record<string, unknown> = {};\n for (let j = 0; j < columns.length; j++) {\n const value = vectors[j]?.get(i);\n if (value === null || value === undefined) continue; // arrow null → absent\n row[columns[j]!] = normalizeArrowValue(value);\n }\n yield row;\n }\n })(),\n };\n}\n\nfunction normalizeArrowValue(value: unknown): unknown {\n if (typeof value === 'bigint') {\n return value >= BigInt(Number.MIN_SAFE_INTEGER) && value <= BigInt(Number.MAX_SAFE_INTEGER)\n ? Number(value)\n : value.toString();\n }\n return value;\n}\n"]}
|
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import { coerceNumeric } from '@modernrelay/orbit-core';
|
|
2
|
+
|
|
3
|
+
// src/fingerprint.ts
|
|
4
|
+
function canonicalJson(value) {
|
|
5
|
+
if (value === null || typeof value !== "object") {
|
|
6
|
+
return JSON.stringify(value) ?? "null";
|
|
7
|
+
}
|
|
8
|
+
if (Array.isArray(value)) {
|
|
9
|
+
return "[" + value.map((v) => canonicalJson(v)).join(",") + "]";
|
|
10
|
+
}
|
|
11
|
+
const record = value;
|
|
12
|
+
const keys = Object.keys(record).filter((k) => record[k] !== void 0).sort();
|
|
13
|
+
return "{" + keys.map((k) => JSON.stringify(k) + ":" + canonicalJson(record[k])).join(",") + "}";
|
|
14
|
+
}
|
|
15
|
+
var FNV_OFFSET = 0xcbf29ce484222325n;
|
|
16
|
+
var FNV_PRIME = 0x100000001b3n;
|
|
17
|
+
var MASK_64 = 0xffffffffffffffffn;
|
|
18
|
+
function fnv1a64Hex(text) {
|
|
19
|
+
const bytes = new TextEncoder().encode(text);
|
|
20
|
+
let hash = FNV_OFFSET;
|
|
21
|
+
for (const byte of bytes) {
|
|
22
|
+
hash ^= BigInt(byte);
|
|
23
|
+
hash = hash * FNV_PRIME & MASK_64;
|
|
24
|
+
}
|
|
25
|
+
return hash.toString(16).padStart(16, "0");
|
|
26
|
+
}
|
|
27
|
+
function computeMappingFingerprint(mapping, format, columns) {
|
|
28
|
+
const columnList = {
|
|
29
|
+
nodes: [...columns.nodes].sort(),
|
|
30
|
+
edges: [...columns.edges].sort()
|
|
31
|
+
};
|
|
32
|
+
return fnv1a64Hex(
|
|
33
|
+
canonicalJson(mapping) + "\n" + format + "\n" + canonicalJson(columnList)
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
var RESERVOIR_CAP = 1e4;
|
|
37
|
+
var UNIQUE_CAP = 1e4;
|
|
38
|
+
var CATEGORY_CAP = 1e4;
|
|
39
|
+
var TOP_CATEGORIES = 20;
|
|
40
|
+
var QUANTILE_PROBS = [0.25, 0.5, 0.75];
|
|
41
|
+
var ColumnAccumulator = class {
|
|
42
|
+
/** Values with a usable value; `nullCount` falls out as rows − present. */
|
|
43
|
+
presentCount = 0;
|
|
44
|
+
numericCount = 0;
|
|
45
|
+
min = Infinity;
|
|
46
|
+
max = -Infinity;
|
|
47
|
+
/** Algorithm-R reservoir of numeric values (exact sample while ≤ cap). */
|
|
48
|
+
reservoir = [];
|
|
49
|
+
numericSeen = 0;
|
|
50
|
+
categories = /* @__PURE__ */ new Map();
|
|
51
|
+
categoriesOverflowed = false;
|
|
52
|
+
hasCategorical = false;
|
|
53
|
+
uniques = /* @__PURE__ */ new Set();
|
|
54
|
+
uniquesOverflowed = false;
|
|
55
|
+
add(value) {
|
|
56
|
+
if (value === null || value === void 0) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (typeof value === "number") {
|
|
60
|
+
const coerced = coerceNumeric(value);
|
|
61
|
+
if (coerced === null) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
this.presentCount++;
|
|
65
|
+
this.numericCount++;
|
|
66
|
+
if (coerced < this.min) this.min = coerced;
|
|
67
|
+
if (coerced > this.max) this.max = coerced;
|
|
68
|
+
this.numericSeen++;
|
|
69
|
+
if (this.reservoir.length < RESERVOIR_CAP) {
|
|
70
|
+
this.reservoir.push(coerced);
|
|
71
|
+
} else {
|
|
72
|
+
const j = Math.floor(Math.random() * this.numericSeen);
|
|
73
|
+
if (j < RESERVOIR_CAP) this.reservoir[j] = coerced;
|
|
74
|
+
}
|
|
75
|
+
this.addUnique("n:" + coerced);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (typeof value === "string" || typeof value === "boolean") {
|
|
79
|
+
this.presentCount++;
|
|
80
|
+
this.hasCategorical = true;
|
|
81
|
+
const key = String(value);
|
|
82
|
+
const existing = this.categories.get(key);
|
|
83
|
+
if (existing !== void 0) {
|
|
84
|
+
this.categories.set(key, existing + 1);
|
|
85
|
+
} else if (this.categories.size < CATEGORY_CAP) {
|
|
86
|
+
this.categories.set(key, 1);
|
|
87
|
+
} else {
|
|
88
|
+
this.categoriesOverflowed = true;
|
|
89
|
+
}
|
|
90
|
+
this.addUnique((typeof value === "string" ? "s:" : "b:") + key);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.presentCount++;
|
|
94
|
+
}
|
|
95
|
+
addUnique(key) {
|
|
96
|
+
if (this.uniques.size < UNIQUE_CAP) {
|
|
97
|
+
this.uniques.add(key);
|
|
98
|
+
} else if (!this.uniques.has(key)) {
|
|
99
|
+
this.uniquesOverflowed = true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
finalize(totalRows) {
|
|
103
|
+
const summary = {
|
|
104
|
+
count: totalRows,
|
|
105
|
+
nullCount: totalRows - this.presentCount
|
|
106
|
+
};
|
|
107
|
+
if (this.numericCount > 0) {
|
|
108
|
+
summary.min = this.min;
|
|
109
|
+
summary.max = this.max;
|
|
110
|
+
summary.quantiles = computeQuantiles(this.reservoir);
|
|
111
|
+
}
|
|
112
|
+
if (this.presentCount > 0 && (this.numericCount > 0 || this.hasCategorical)) {
|
|
113
|
+
summary.approximateUnique = this.uniquesOverflowed ? UNIQUE_CAP : this.uniques.size;
|
|
114
|
+
}
|
|
115
|
+
if (this.hasCategorical) {
|
|
116
|
+
const entries = [...this.categories.entries()];
|
|
117
|
+
entries.sort((a, b) => b[1] - a[1]);
|
|
118
|
+
summary.categories = entries.slice(0, TOP_CATEGORIES).map(([value, count]) => ({ value, count }));
|
|
119
|
+
if (this.categoriesOverflowed || this.categories.size > TOP_CATEGORIES) {
|
|
120
|
+
summary.categoriesTruncated = true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return summary;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
function computeQuantiles(sample) {
|
|
127
|
+
const sorted = [...sample].sort((a, b) => a - b);
|
|
128
|
+
const n = sorted.length;
|
|
129
|
+
return QUANTILE_PROBS.map((p) => {
|
|
130
|
+
if (n === 1) return sorted[0];
|
|
131
|
+
const idx = (n - 1) * p;
|
|
132
|
+
const lo = Math.floor(idx);
|
|
133
|
+
const hi = Math.ceil(idx);
|
|
134
|
+
const loV = sorted[lo];
|
|
135
|
+
const hiV = sorted[hi];
|
|
136
|
+
return lo === hi ? loV : loV + (hiV - loV) * (idx - lo);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
var TableSummarizer = class {
|
|
140
|
+
columns = /* @__PURE__ */ new Map();
|
|
141
|
+
rows = 0;
|
|
142
|
+
seed(columnNames) {
|
|
143
|
+
for (const name of columnNames) {
|
|
144
|
+
if (!this.columns.has(name)) this.columns.set(name, new ColumnAccumulator());
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
addRow(attrs) {
|
|
148
|
+
this.rows++;
|
|
149
|
+
if (attrs === void 0) return;
|
|
150
|
+
for (const key of Object.keys(attrs)) {
|
|
151
|
+
let acc = this.columns.get(key);
|
|
152
|
+
if (acc === void 0) {
|
|
153
|
+
acc = new ColumnAccumulator();
|
|
154
|
+
this.columns.set(key, acc);
|
|
155
|
+
}
|
|
156
|
+
acc.add(attrs[key]);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
finalize() {
|
|
160
|
+
const out = {};
|
|
161
|
+
for (const [name, acc] of this.columns) {
|
|
162
|
+
out[name] = acc.finalize(this.rows);
|
|
163
|
+
}
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// src/validate.ts
|
|
169
|
+
function validateMapping(input) {
|
|
170
|
+
const { mapping, nodeColumns, edgeColumns, deriveNodes } = input;
|
|
171
|
+
const problems = [];
|
|
172
|
+
if (!deriveNodes) {
|
|
173
|
+
if (mapping.nodes === void 0) {
|
|
174
|
+
problems.push("mapping.nodes is required when a node source is supplied");
|
|
175
|
+
} else if (nodeColumns !== null && !nodeColumns.includes(mapping.nodes.id)) {
|
|
176
|
+
problems.push(
|
|
177
|
+
`node id column ${JSON.stringify(mapping.nodes.id)} not found (available: ${formatColumns(nodeColumns)})`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (edgeColumns !== null) {
|
|
182
|
+
const missing = [];
|
|
183
|
+
if (!edgeColumns.includes(mapping.edges.source)) missing.push("source");
|
|
184
|
+
if (!edgeColumns.includes(mapping.edges.target)) missing.push("target");
|
|
185
|
+
for (const role of missing) {
|
|
186
|
+
const column = role === "source" ? mapping.edges.source : mapping.edges.target;
|
|
187
|
+
problems.push(
|
|
188
|
+
`edge ${role} column ${JSON.stringify(column)} not found (available: ${formatColumns(edgeColumns)})`
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (problems.length > 0) {
|
|
193
|
+
throw new TypeError(
|
|
194
|
+
`prepareGraphData: mapping validation failed \u2014 ${problems.join("; ")}`
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function formatColumns(columns) {
|
|
199
|
+
if (columns.length === 0) return "none";
|
|
200
|
+
return columns.map((c) => JSON.stringify(c)).join(", ");
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// src/builder.ts
|
|
204
|
+
async function buildPrepared(args) {
|
|
205
|
+
const { nodeTable, edgeTable, mapping, format, options } = args;
|
|
206
|
+
validateOptions(options);
|
|
207
|
+
validateMapping({
|
|
208
|
+
mapping,
|
|
209
|
+
nodeColumns: nodeTable === null ? null : nodeTable.columns,
|
|
210
|
+
edgeColumns: edgeTable.columns,
|
|
211
|
+
deriveNodes: nodeTable === null
|
|
212
|
+
});
|
|
213
|
+
const nodeSummarizer = new TableSummarizer();
|
|
214
|
+
const edgeSummarizer = new TableSummarizer();
|
|
215
|
+
const edgeIdentity = new Set(
|
|
216
|
+
[mapping.edges.id, mapping.edges.source, mapping.edges.target].filter(
|
|
217
|
+
(c) => c !== void 0
|
|
218
|
+
)
|
|
219
|
+
);
|
|
220
|
+
const edgeAttrPick = attrPicker(edgeTable.columns, edgeIdentity, mapping.edges.includeFields);
|
|
221
|
+
if (edgeTable.columns !== null) {
|
|
222
|
+
edgeSummarizer.seed(edgeAttrPick.seedColumns);
|
|
223
|
+
for (const column of edgeAttrPick.seedColumns) edgeAttrPick.admitted.add(column);
|
|
224
|
+
}
|
|
225
|
+
const edges = [];
|
|
226
|
+
const endpointOrder = /* @__PURE__ */ new Set();
|
|
227
|
+
let edgeOrdinal = 0;
|
|
228
|
+
for await (const row of edgeTable.rows) {
|
|
229
|
+
const source = identityString(row[mapping.edges.source], "edge source", edgeOrdinal, mapping.edges.source);
|
|
230
|
+
const target = identityString(row[mapping.edges.target], "edge target", edgeOrdinal, mapping.edges.target);
|
|
231
|
+
const edge = { source, target };
|
|
232
|
+
if (mapping.edges.id !== void 0) {
|
|
233
|
+
const raw = row[mapping.edges.id];
|
|
234
|
+
if (raw !== void 0 && raw !== null) {
|
|
235
|
+
edge.id = identityString(raw, "edge id", edgeOrdinal, mapping.edges.id);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
const attrs = edgeAttrPick.pick(row);
|
|
239
|
+
if (attrs !== void 0) edge.attrs = attrs;
|
|
240
|
+
edgeSummarizer.addRow(attrs);
|
|
241
|
+
edges.push(edge);
|
|
242
|
+
if (nodeTable === null) {
|
|
243
|
+
endpointOrder.add(source);
|
|
244
|
+
endpointOrder.add(target);
|
|
245
|
+
}
|
|
246
|
+
edgeOrdinal++;
|
|
247
|
+
}
|
|
248
|
+
const nodes = [];
|
|
249
|
+
let nodeAdmitted = /* @__PURE__ */ new Set();
|
|
250
|
+
if (nodeTable === null) {
|
|
251
|
+
for (const id of endpointOrder) nodes.push({ id });
|
|
252
|
+
} else {
|
|
253
|
+
const nodeMapping = mapping.nodes;
|
|
254
|
+
const nodeIdentity = /* @__PURE__ */ new Set([nodeMapping.id]);
|
|
255
|
+
const nodeAttrPick = attrPicker(nodeTable.columns, nodeIdentity, nodeMapping.includeFields);
|
|
256
|
+
nodeAdmitted = nodeAttrPick.admitted;
|
|
257
|
+
if (nodeTable.columns !== null) {
|
|
258
|
+
nodeSummarizer.seed(nodeAttrPick.seedColumns);
|
|
259
|
+
for (const column of nodeAttrPick.seedColumns) nodeAttrPick.admitted.add(column);
|
|
260
|
+
}
|
|
261
|
+
let nodeOrdinal = 0;
|
|
262
|
+
for await (const row of nodeTable.rows) {
|
|
263
|
+
const id = identityString(row[nodeMapping.id], "node id", nodeOrdinal, nodeMapping.id);
|
|
264
|
+
const node = { id };
|
|
265
|
+
const attrs = nodeAttrPick.pick(row);
|
|
266
|
+
if (attrs !== void 0) node.attrs = attrs;
|
|
267
|
+
nodeSummarizer.addRow(attrs);
|
|
268
|
+
nodes.push(node);
|
|
269
|
+
nodeOrdinal++;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const mappingFingerprint = computeMappingFingerprint(mapping, format, {
|
|
273
|
+
nodes: [...nodeAdmitted],
|
|
274
|
+
edges: [...edgeAttrPick.admitted]
|
|
275
|
+
});
|
|
276
|
+
const snapshot = {
|
|
277
|
+
datasetKey: options.datasetKey,
|
|
278
|
+
sourceRevision: options.sourceRevision,
|
|
279
|
+
nodes,
|
|
280
|
+
edges
|
|
281
|
+
};
|
|
282
|
+
return {
|
|
283
|
+
snapshot,
|
|
284
|
+
summaries: { nodes: nodeSummarizer.finalize(), edges: edgeSummarizer.finalize() },
|
|
285
|
+
mappingFingerprint
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function validateOptions(options) {
|
|
289
|
+
if (typeof options.datasetKey !== "string" || options.datasetKey === "") {
|
|
290
|
+
throw new TypeError("prepareGraphData: options.datasetKey must be a non-empty string");
|
|
291
|
+
}
|
|
292
|
+
const rev = options.sourceRevision;
|
|
293
|
+
if (typeof rev !== "string" && typeof rev !== "number") {
|
|
294
|
+
throw new TypeError("prepareGraphData: options.sourceRevision must be a string or number");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function identityString(value, role, ordinal, column) {
|
|
298
|
+
if (typeof value === "string") {
|
|
299
|
+
if (value === "") {
|
|
300
|
+
throw new TypeError(
|
|
301
|
+
`prepareGraphData: ${role} in column ${JSON.stringify(column)} is empty at row ${ordinal}`
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
return value;
|
|
305
|
+
}
|
|
306
|
+
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
|
307
|
+
if (typeof value === "bigint") return String(value);
|
|
308
|
+
throw new TypeError(
|
|
309
|
+
`prepareGraphData: ${role} in column ${JSON.stringify(column)} is missing or unusable at row ${ordinal} (got ${value === null ? "null" : typeof value})`
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
function attrPicker(columns, identity, includeFields) {
|
|
313
|
+
const admitted = /* @__PURE__ */ new Set();
|
|
314
|
+
if (includeFields !== void 0) {
|
|
315
|
+
const allow = new Set(includeFields);
|
|
316
|
+
return {
|
|
317
|
+
seedColumns: columns === null ? [...allow] : columns.filter((c) => allow.has(c)),
|
|
318
|
+
admitted,
|
|
319
|
+
pick: (row) => {
|
|
320
|
+
let attrs;
|
|
321
|
+
for (const key of Object.keys(row)) {
|
|
322
|
+
if (!allow.has(key)) continue;
|
|
323
|
+
const value = row[key];
|
|
324
|
+
if (value === void 0) continue;
|
|
325
|
+
admitted.add(key);
|
|
326
|
+
(attrs ??= {})[key] = value;
|
|
327
|
+
}
|
|
328
|
+
return attrs;
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
return {
|
|
333
|
+
seedColumns: columns === null ? [] : columns.filter((c) => !identity.has(c)),
|
|
334
|
+
admitted,
|
|
335
|
+
pick: (row) => {
|
|
336
|
+
let attrs;
|
|
337
|
+
for (const key of Object.keys(row)) {
|
|
338
|
+
if (identity.has(key)) continue;
|
|
339
|
+
const value = row[key];
|
|
340
|
+
if (value === void 0) continue;
|
|
341
|
+
admitted.add(key);
|
|
342
|
+
(attrs ??= {})[key] = value;
|
|
343
|
+
}
|
|
344
|
+
return attrs;
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// src/rowTable.ts
|
|
350
|
+
var EMPTY_ROW_TABLE = {
|
|
351
|
+
columns: null,
|
|
352
|
+
rows: (async function* () {
|
|
353
|
+
})()
|
|
354
|
+
};
|
|
355
|
+
function isPlainRowObject(row) {
|
|
356
|
+
return typeof row === "object" && row !== null && !Array.isArray(row);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// src/sources.ts
|
|
360
|
+
function throwIfAborted(signal) {
|
|
361
|
+
if (signal?.aborted) {
|
|
362
|
+
if (typeof signal.throwIfAborted === "function") signal.throwIfAborted();
|
|
363
|
+
throw new Error("prepareGraphData: aborted");
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function isBlob(value) {
|
|
367
|
+
return typeof Blob !== "undefined" && value instanceof Blob;
|
|
368
|
+
}
|
|
369
|
+
function isAsyncIterable(value) {
|
|
370
|
+
return typeof value === "object" && value !== null && typeof value[Symbol.asyncIterator] === "function";
|
|
371
|
+
}
|
|
372
|
+
async function peekAsyncIterable(source) {
|
|
373
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
374
|
+
const head = await iterator.next();
|
|
375
|
+
const first = head.done ? void 0 : head.value;
|
|
376
|
+
async function* replay() {
|
|
377
|
+
if (!head.done) yield head.value;
|
|
378
|
+
while (true) {
|
|
379
|
+
const next = await iterator.next();
|
|
380
|
+
if (next.done) return;
|
|
381
|
+
yield next.value;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return { first, rest: replay() };
|
|
385
|
+
}
|
|
386
|
+
function byteChunks(source, signal) {
|
|
387
|
+
if (source instanceof ArrayBuffer) {
|
|
388
|
+
const chunk = new Uint8Array(source);
|
|
389
|
+
return (async function* () {
|
|
390
|
+
throwIfAborted(signal);
|
|
391
|
+
yield chunk;
|
|
392
|
+
})();
|
|
393
|
+
}
|
|
394
|
+
if (isBlob(source)) {
|
|
395
|
+
return (async function* () {
|
|
396
|
+
const stream = source.stream();
|
|
397
|
+
for await (const chunk of stream) {
|
|
398
|
+
throwIfAborted(signal);
|
|
399
|
+
yield chunk;
|
|
400
|
+
}
|
|
401
|
+
})();
|
|
402
|
+
}
|
|
403
|
+
return (async function* () {
|
|
404
|
+
for await (const chunk of source) {
|
|
405
|
+
throwIfAborted(signal);
|
|
406
|
+
if (!(chunk instanceof Uint8Array)) {
|
|
407
|
+
throw new TypeError(
|
|
408
|
+
"prepareGraphData: byte sources must yield Uint8Array chunks"
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
yield chunk;
|
|
412
|
+
}
|
|
413
|
+
})();
|
|
414
|
+
}
|
|
415
|
+
async function collectBytes(source, signal) {
|
|
416
|
+
const chunks = [];
|
|
417
|
+
let total = 0;
|
|
418
|
+
for await (const chunk of byteChunks(source, signal)) {
|
|
419
|
+
chunks.push(chunk);
|
|
420
|
+
total += chunk.byteLength;
|
|
421
|
+
}
|
|
422
|
+
const out = new Uint8Array(total);
|
|
423
|
+
let offset = 0;
|
|
424
|
+
for (const chunk of chunks) {
|
|
425
|
+
out.set(chunk, offset);
|
|
426
|
+
offset += chunk.byteLength;
|
|
427
|
+
}
|
|
428
|
+
return out;
|
|
429
|
+
}
|
|
430
|
+
function rowStream(source, signal) {
|
|
431
|
+
if (Array.isArray(source)) {
|
|
432
|
+
const rows = source;
|
|
433
|
+
return (async function* () {
|
|
434
|
+
for (let i = 0; i < rows.length; i++) {
|
|
435
|
+
if ((i & 255) === 0) throwIfAborted(signal);
|
|
436
|
+
yield rows[i];
|
|
437
|
+
}
|
|
438
|
+
})();
|
|
439
|
+
}
|
|
440
|
+
if (isAsyncIterable(source)) {
|
|
441
|
+
return (async function* () {
|
|
442
|
+
for await (const row of source) {
|
|
443
|
+
throwIfAborted(signal);
|
|
444
|
+
yield row;
|
|
445
|
+
}
|
|
446
|
+
})();
|
|
447
|
+
}
|
|
448
|
+
throw new TypeError(
|
|
449
|
+
"prepareGraphData: row sources must be an array or an AsyncIterable of row objects"
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export { EMPTY_ROW_TABLE, buildPrepared, byteChunks, canonicalJson, collectBytes, computeMappingFingerprint, fnv1a64Hex, isAsyncIterable, isBlob, isPlainRowObject, peekAsyncIterable, rowStream, throwIfAborted };
|
|
454
|
+
//# sourceMappingURL=chunk-H22YHC2C.js.map
|
|
455
|
+
//# sourceMappingURL=chunk-H22YHC2C.js.map
|