@altertable/data-app 0.68.0 → 0.70.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/AGENTS.md +10 -0
- package/CONTRIBUTING.md +27 -1
- package/README.md +18 -21
- package/THIRD_PARTY_NOTICES +18 -0
- package/dist/chunks/index-22d576sn.js +153 -0
- package/dist/chunks/index-22d576sn.js.map +11 -0
- package/dist/chunks/index-2ajhmsc4.js +355 -0
- package/dist/chunks/index-2ajhmsc4.js.map +13 -0
- package/dist/chunks/{index-ktmvs916.js → index-58kks6a8.js} +37 -21
- package/dist/chunks/index-58kks6a8.js.map +13 -0
- package/dist/chunks/{index-23dktynd.js → index-7q99hf1z.js} +19 -12
- package/dist/chunks/index-7q99hf1z.js.map +10 -0
- package/dist/chunks/{index-mt93ff2b.js → index-9zqbrv2z.js} +7 -3
- package/dist/chunks/index-9zqbrv2z.js.map +10 -0
- package/dist/chunks/{index-m6n8ctfc.js → index-cv36cyna.js} +4 -4
- package/dist/chunks/index-cv36cyna.js.map +10 -0
- package/dist/chunks/{index-ag0cgnvq.js → index-q1w84fv9.js} +178 -31
- package/dist/chunks/index-q1w84fv9.js.map +15 -0
- package/dist/chunks/{index-qd5xgx6y.js → index-rew95tbg.js} +89 -21
- package/dist/chunks/index-rew95tbg.js.map +13 -0
- package/dist/chunks/{index-gvers8jw.js → index-vtpygsbq.js} +5651 -2711
- package/dist/chunks/index-vtpygsbq.js.map +126 -0
- package/dist/chunks/{index-x69p7cv7.js → index-wjt6cavj.js} +6 -4
- package/dist/chunks/index-wjt6cavj.js.map +10 -0
- package/dist/chunks/{index-f5cnbrjz.js → index-wss3gd3h.js} +39 -9
- package/dist/chunks/index-wss3gd3h.js.map +13 -0
- package/dist/chunks/{index-ev2b5aaf.js → index-xzb7sx6g.js} +4 -4
- package/dist/client/index.js +6 -6
- package/dist/core/appearance.js +1 -1
- package/dist/core/config.js +3 -3
- package/dist/core/contract.js +7 -7
- package/dist/core/contract.js.map +1 -1
- package/dist/core/format.js +10 -2
- package/dist/core/format.js.map +1 -1
- package/dist/embed/index.js +26 -25
- package/dist/embed/index.js.map +4 -4
- package/dist/index.js +3 -3
- package/dist/local.js +200 -145
- package/dist/local.js.map +10 -7
- package/dist/preview.js +1456 -0
- package/dist/preview.js.map +17 -0
- package/dist/react/embed/index.js +1 -1
- package/dist/react/index.js +500 -401
- package/dist/react/index.js.map +10 -13
- package/dist/react/ui/index.js +1851 -1476
- package/dist/react/ui/index.js.map +24 -21
- package/dist/server.js +102 -58
- package/dist/server.js.map +9 -6
- package/dist/types/client/data-client.d.ts +21 -10
- package/dist/types/client/download.d.ts +5 -0
- package/dist/types/client/iframe.d.ts +4 -2
- package/dist/types/client/logger.d.ts +10 -2
- package/dist/types/core/annotations.d.ts +5 -0
- package/dist/types/core/appearance.d.ts +2 -0
- package/dist/types/core/bridge-endpoint.d.ts +2 -2
- package/dist/types/core/bridge.d.ts +22 -0
- package/dist/types/core/config.d.ts +25 -17
- package/dist/types/core/contract.d.ts +26 -14
- package/dist/types/core/date-format.d.ts +24 -0
- package/dist/types/core/dimension-source.d.ts +64 -0
- package/dist/types/core/dimension.d.ts +33 -24
- package/dist/types/core/facets.d.ts +13 -0
- package/dist/types/core/filters.d.ts +4 -3
- package/dist/types/core/format.d.ts +5 -3
- package/dist/types/core/keys.d.ts +2 -0
- package/dist/types/core/messages.d.ts +3 -1
- package/dist/types/core/operation-types.d.ts +12 -11
- package/dist/types/core/queries.d.ts +75 -7
- package/dist/types/core/time-interval.d.ts +96 -0
- package/dist/types/core/variables.d.ts +49 -14
- package/dist/types/embed/host.d.ts +4 -4
- package/dist/types/embed/sql.d.ts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/react/actions.d.ts +53 -0
- package/dist/types/react/annotations/annotation-editor-state.d.ts +2 -0
- package/dist/types/react/annotations/annotation-targets.d.ts +1 -0
- package/dist/types/react/annotations/getAnnotationProps.d.ts +5 -3
- package/dist/types/react/annotations/useAnnotationGeometry.d.ts +2 -2
- package/dist/types/react/bindings.d.ts +62 -3
- package/dist/types/react/date-formatting.d.ts +12 -0
- package/dist/types/react/details.d.ts +46 -0
- package/dist/types/react/exploration.d.ts +44 -0
- package/dist/types/react/hooks.d.ts +82 -43
- package/dist/types/react/index.d.ts +10 -2
- package/dist/types/react/search-suggestions.d.ts +8 -0
- package/dist/types/react/style-contract.d.ts +1 -5
- package/dist/types/react/ui/AboutData.d.ts +9 -4
- package/dist/types/react/ui/AreaChart.d.ts +2 -2
- package/dist/types/react/ui/BarChart.d.ts +2 -2
- package/dist/types/react/ui/Button.d.ts +2 -1
- package/dist/types/react/ui/ChartLegend.d.ts +3 -2
- package/dist/types/react/ui/ChoicePicker.d.ts +2 -0
- package/dist/types/react/ui/ComposedChartLegend.d.ts +2 -1
- package/dist/types/react/ui/DataApp.d.ts +8 -4
- package/dist/types/react/ui/DataAppFrame.d.ts +14 -3
- package/dist/types/react/ui/DataTable.d.ts +6 -4
- package/dist/types/react/ui/DataWidget.d.ts +7 -0
- package/dist/types/react/ui/DateDisplay.d.ts +9 -0
- package/dist/types/react/ui/DateRangePicker.d.ts +3 -1
- package/dist/types/react/ui/DateTimeTooltip.d.ts +3 -2
- package/dist/types/react/ui/DimensionPicker.d.ts +5 -3
- package/dist/types/react/ui/EmptyState.d.ts +1 -1
- package/dist/types/react/ui/ExplorationActionControl.d.ts +60 -0
- package/dist/types/react/ui/GradientScroll.d.ts +8 -0
- package/dist/types/react/ui/Grid.d.ts +1 -1
- package/dist/types/react/ui/InspectionProvider.d.ts +2 -2
- package/dist/types/react/ui/LineChart.d.ts +2 -2
- package/dist/types/react/ui/Menu.d.ts +3 -2
- package/dist/types/react/ui/MetricWidget.d.ts +4 -0
- package/dist/types/react/ui/NumberFilterPicker.d.ts +3 -1
- package/dist/types/react/ui/PeriodSummary.d.ts +2 -1
- package/dist/types/react/ui/PickerField.d.ts +23 -0
- package/dist/types/react/ui/PieChart.d.ts +2 -2
- package/dist/types/react/ui/PopoverSurface.d.ts +5 -0
- package/dist/types/react/ui/RequestHint.d.ts +2 -2
- package/dist/types/react/ui/SearchField.d.ts +3 -1
- package/dist/types/react/ui/SearchTypeahead.d.ts +15 -0
- package/dist/types/react/ui/SectionHeading.d.ts +8 -0
- package/dist/types/react/ui/Select.d.ts +9 -1
- package/dist/types/react/ui/Sheet.d.ts +4 -2
- package/dist/types/react/ui/TableWidget.d.ts +8 -0
- package/dist/types/react/ui/Tabs.d.ts +4 -3
- package/dist/types/react/ui/TrendChart.d.ts +3 -3
- package/dist/types/react/ui/VariableBar.d.ts +1 -1
- package/dist/types/react/ui/VisualizationWidget.d.ts +1 -1
- package/dist/types/react/ui/chart-data.d.ts +8 -6
- package/dist/types/react/ui/chart-date-axis.d.ts +15 -0
- package/dist/types/react/ui/chart-interaction.d.ts +27 -0
- package/dist/types/react/ui/chart-primitives.d.ts +4 -4
- package/dist/types/react/ui/chart-time.d.ts +19 -0
- package/dist/types/react/ui/chartColor.d.ts +1 -1
- package/dist/types/react/ui/icons.d.ts +5 -1
- package/dist/types/react/ui/index.d.ts +10 -4
- package/dist/types/react/ui/metric-comparison.d.ts +2 -2
- package/dist/types/react/ui/metric.d.ts +2 -2
- package/dist/types/react/ui/presentation.d.ts +2 -0
- package/dist/types/react/ui/searchItems.d.ts +2 -0
- package/dist/types/react/ui/use-chart-scroll-to-end.d.ts +2 -0
- package/dist/types/react/ui/variables.d.ts +1 -1
- package/dist/types/react/view-control-bar.d.ts +18 -0
- package/dist/types/react/view-controls.d.ts +3 -2
- package/dist/types/react/view-runtime.d.ts +8 -3
- package/dist/types/react/view.d.ts +28 -7
- package/dist/types/react/widgets.d.ts +22 -11
- package/dist/types/server/local.d.ts +3 -1
- package/dist/types/server/preview/host.d.ts +1 -0
- package/dist/types/server/preview/index.d.ts +16 -0
- package/dist/types/server/reload-channel.d.ts +6 -0
- package/dist/types/server/request-body.d.ts +4 -0
- package/dist/worker.js +1 -1
- package/docs/app-authoring.md +27 -12
- package/docs/client.md +6 -7
- package/docs/composition.md +84 -0
- package/docs/contract.md +45 -26
- package/docs/data-context.md +2 -0
- package/docs/embed.md +11 -3
- package/docs/formatting-and-appearance.md +36 -5
- package/docs/layout.md +31 -8
- package/docs/react-embed.md +4 -4
- package/docs/react.md +1 -1
- package/docs/server-bun.md +25 -0
- package/docs/server.md +2 -2
- package/docs/styling.md +10 -27
- package/docs/ui.md +32 -18
- package/docs/variables.md +174 -25
- package/docs/views.md +60 -29
- package/docs/widgets.md +131 -26
- package/examples/starter-data-app/index.tsx +10 -23
- package/examples/starter-local-data-app/.bun-version +1 -0
- package/examples/starter-local-data-app/.node-version +1 -0
- package/examples/starter-local-data-app/.oxfmtrc.json +4 -0
- package/examples/starter-local-data-app/.oxlintrc.json +33 -0
- package/examples/starter-local-data-app/AGENTS.md +22 -0
- package/examples/starter-local-data-app/README.md +54 -0
- package/examples/starter-local-data-app/app.ts +28 -0
- package/examples/starter-local-data-app/bunfig.toml +2 -0
- package/examples/starter-local-data-app/package.json +33 -0
- package/examples/starter-local-data-app/src/App.tsx +8 -0
- package/examples/starter-local-data-app/src/assets.d.ts +1 -0
- package/examples/starter-local-data-app/src/data-context.ts +21 -0
- package/examples/starter-local-data-app/src/favicon.svg +13 -0
- package/examples/starter-local-data-app/src/index.html +14 -0
- package/examples/starter-local-data-app/src/main.tsx +7 -0
- package/examples/starter-local-data-app/src/operations.ts +6 -0
- package/examples/starter-local-data-app/src/server.ts +6 -0
- package/examples/starter-local-data-app/tsconfig.json +21 -0
- package/package.json +13 -3
- package/dist/chunks/index-23dktynd.js.map +0 -10
- package/dist/chunks/index-ag0cgnvq.js.map +0 -13
- package/dist/chunks/index-dq7b35fn.js +0 -307
- package/dist/chunks/index-dq7b35fn.js.map +0 -13
- package/dist/chunks/index-f5cnbrjz.js.map +0 -13
- package/dist/chunks/index-gvers8jw.js.map +0 -109
- package/dist/chunks/index-ktmvs916.js.map +0 -13
- package/dist/chunks/index-m6n8ctfc.js.map +0 -10
- package/dist/chunks/index-mev09s5v.js +0 -77
- package/dist/chunks/index-mev09s5v.js.map +0 -10
- package/dist/chunks/index-mt93ff2b.js.map +0 -10
- package/dist/chunks/index-qd5xgx6y.js.map +0 -13
- package/dist/chunks/index-x69p7cv7.js.map +0 -10
- package/dist/types/react/shellStyles.d.ts +0 -3
- package/dist/types/react/ui/DataAppSkeleton.d.ts +0 -7
- /package/dist/chunks/{index-ev2b5aaf.js.map → index-xzb7sx6g.js.map} +0 -0
package/dist/local.js
CHANGED
|
@@ -1,4 +1,35 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
// src/core/invariant.ts
|
|
3
|
+
function invariant(condition, message) {
|
|
4
|
+
if (!condition)
|
|
5
|
+
throw new Error(`Invariant failed: ${message}`);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// src/core/dimension-source.ts
|
|
9
|
+
var dimensionSource = Symbol("data-app.dimensionSource");
|
|
10
|
+
var sources = new WeakMap;
|
|
11
|
+
var parserVariables = new WeakMap;
|
|
12
|
+
var selectorsKey = Symbol.for("@altertable/data-app/dimension-selectors");
|
|
13
|
+
var ownerKey = Symbol.for("@altertable/data-app/dimension-selector-owner");
|
|
14
|
+
var invalidMemberKey = Symbol.for("@altertable/data-app/dimension-member-error");
|
|
15
|
+
|
|
16
|
+
class DimensionMembershipError extends Error {
|
|
17
|
+
[invalidMemberKey] = true;
|
|
18
|
+
static [Symbol.hasInstance](value) {
|
|
19
|
+
return value instanceof Error && value[invalidMemberKey] === true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function withDimensionSelectors(operations) {
|
|
23
|
+
const expanded = { ...operations };
|
|
24
|
+
for (const operation of Object.values(operations))
|
|
25
|
+
for (const [name, selector] of Object.entries(operation[selectorsKey] ?? {})) {
|
|
26
|
+
invariant(!Object.hasOwn(operations, name), "Reserved dimension operation name.");
|
|
27
|
+
invariant(!expanded[name] || expanded[name][ownerKey] === selector[ownerKey], "Dimension source keys must be unique across the app.");
|
|
28
|
+
expanded[name] ??= selector;
|
|
29
|
+
}
|
|
30
|
+
return expanded;
|
|
31
|
+
}
|
|
32
|
+
|
|
2
33
|
// src/core/contract.ts
|
|
3
34
|
import { parseDate, today } from "@internationalized/date";
|
|
4
35
|
var sourceErrorBrand = Symbol.for("@altertable/data-app/DataSourceError");
|
|
@@ -11,14 +42,74 @@ class DataSourceError extends Error {
|
|
|
11
42
|
static [Symbol.hasInstance](value) {
|
|
12
43
|
return value instanceof Error && value[sourceErrorBrand] === true;
|
|
13
44
|
}
|
|
14
|
-
constructor(reason, status) {
|
|
15
|
-
super(`Data source ${reason}${status ? ` (${status})` : ""}
|
|
45
|
+
constructor(reason, status, options) {
|
|
46
|
+
super(`Data source ${reason}${status ? ` (${status})` : ""}.`, options);
|
|
16
47
|
this.reason = reason;
|
|
17
48
|
this.status = status;
|
|
18
49
|
this.name = "DataSourceError";
|
|
19
50
|
}
|
|
20
51
|
}
|
|
21
52
|
|
|
53
|
+
// src/server/local.ts
|
|
54
|
+
import {
|
|
55
|
+
AltertableLakehouseClient,
|
|
56
|
+
AltertableLakehouseError,
|
|
57
|
+
NetworkError,
|
|
58
|
+
TimeoutError,
|
|
59
|
+
QueryError
|
|
60
|
+
} from "@altertable/lakehouse";
|
|
61
|
+
|
|
62
|
+
// src/server/request-body.ts
|
|
63
|
+
class RequestBodyTooLargeError extends Error {
|
|
64
|
+
}
|
|
65
|
+
async function readRequestBody(request, maxBytes) {
|
|
66
|
+
const body = request.body;
|
|
67
|
+
if (!body)
|
|
68
|
+
return "";
|
|
69
|
+
const reader = body.getReader();
|
|
70
|
+
const chunks = [];
|
|
71
|
+
let size = 0;
|
|
72
|
+
let abort;
|
|
73
|
+
function cancelReader() {
|
|
74
|
+
reader.cancel().catch(() => {});
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
request.signal.throwIfAborted();
|
|
78
|
+
const cancelled = new Promise((_, reject) => {
|
|
79
|
+
function rejectAborted() {
|
|
80
|
+
reject(new Error("Request aborted."));
|
|
81
|
+
cancelReader();
|
|
82
|
+
}
|
|
83
|
+
abort = rejectAborted;
|
|
84
|
+
request.signal.addEventListener("abort", rejectAborted, { once: true });
|
|
85
|
+
if (request.signal.aborted)
|
|
86
|
+
rejectAborted();
|
|
87
|
+
});
|
|
88
|
+
while (true) {
|
|
89
|
+
const { done, value } = await Promise.race([reader.read(), cancelled]);
|
|
90
|
+
if (done)
|
|
91
|
+
break;
|
|
92
|
+
size += value.byteLength;
|
|
93
|
+
if (size > maxBytes) {
|
|
94
|
+
cancelReader();
|
|
95
|
+
throw new RequestBodyTooLargeError;
|
|
96
|
+
}
|
|
97
|
+
chunks.push(value);
|
|
98
|
+
}
|
|
99
|
+
const bytes = new Uint8Array(size);
|
|
100
|
+
let offset = 0;
|
|
101
|
+
for (const chunk of chunks) {
|
|
102
|
+
bytes.set(chunk, offset);
|
|
103
|
+
offset += chunk.byteLength;
|
|
104
|
+
}
|
|
105
|
+
return new TextDecoder().decode(bytes);
|
|
106
|
+
} finally {
|
|
107
|
+
if (abort)
|
|
108
|
+
request.signal.removeEventListener("abort", abort);
|
|
109
|
+
reader.releaseLock();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
22
113
|
// src/core/operation.ts
|
|
23
114
|
async function executeDataOperation(operation, value, {
|
|
24
115
|
operationName,
|
|
@@ -46,7 +137,7 @@ async function executeDataOperation(operation, value, {
|
|
|
46
137
|
queryParams
|
|
47
138
|
});
|
|
48
139
|
const body = {
|
|
49
|
-
data:
|
|
140
|
+
data: output,
|
|
50
141
|
requestId,
|
|
51
142
|
queriedAt: new Date().toISOString(),
|
|
52
143
|
queryIds: execution.queryIds,
|
|
@@ -68,6 +159,7 @@ function createOperationLakehouse(operation, source, signal, operationName) {
|
|
|
68
159
|
const queries = [];
|
|
69
160
|
const lakehouse = {
|
|
70
161
|
async queryAll(statement, options) {
|
|
162
|
+
signal.throwIfAborted();
|
|
71
163
|
if (!Number.isInteger(options.limit) || options.limit < 1)
|
|
72
164
|
throw new Error("Query needs a positive row limit.");
|
|
73
165
|
if (operation.queryNames && !Object.values(operation.queryNames).includes(options.name ?? ""))
|
|
@@ -141,7 +233,7 @@ var sourceErrorMessages = {
|
|
|
141
233
|
unavailable: "The lakehouse is unavailable. Check the connection and retry."
|
|
142
234
|
};
|
|
143
235
|
function toDataOperationFailure(error, aborted = false) {
|
|
144
|
-
if (error instanceof OperationInputError)
|
|
236
|
+
if (error instanceof OperationInputError || error instanceof DimensionMembershipError)
|
|
145
237
|
return {
|
|
146
238
|
status: 400,
|
|
147
239
|
code: "invalid_input",
|
|
@@ -163,6 +255,7 @@ function toDataOperationFailure(error, aborted = false) {
|
|
|
163
255
|
|
|
164
256
|
// src/server/handler.ts
|
|
165
257
|
function createDataHandler(operations, authorize) {
|
|
258
|
+
operations = withDimensionSelectors(operations);
|
|
166
259
|
async function handleDataRequest(request) {
|
|
167
260
|
const requestId = crypto.randomUUID();
|
|
168
261
|
const url = new URL(request.url);
|
|
@@ -199,10 +292,10 @@ function createDataHandler(operations, authorize) {
|
|
|
199
292
|
if (request.signal.aborted)
|
|
200
293
|
return problem(504, "timeout", "The data request timed out.", requestId);
|
|
201
294
|
try {
|
|
202
|
-
const body = await
|
|
295
|
+
const body = await readRequestBody(request, MAX_INPUT_BYTES);
|
|
203
296
|
input = JSON.parse(body);
|
|
204
297
|
} catch (error) {
|
|
205
|
-
if (error instanceof
|
|
298
|
+
if (error instanceof RequestBodyTooLargeError)
|
|
206
299
|
return problem(413, "input_too_large", "Input is too large.", requestId);
|
|
207
300
|
return problem(400, "invalid_input", "Check this operation's inputs.", requestId);
|
|
208
301
|
}
|
|
@@ -222,8 +315,17 @@ function createDataHandler(operations, authorize) {
|
|
|
222
315
|
}
|
|
223
316
|
});
|
|
224
317
|
} catch (error) {
|
|
225
|
-
|
|
226
|
-
|
|
318
|
+
let detail = error instanceof Error ? error.name : "unknown";
|
|
319
|
+
if (error instanceof DataSourceError) {
|
|
320
|
+
detail = error.reason;
|
|
321
|
+
if (error.status)
|
|
322
|
+
detail += ` (${error.status})`;
|
|
323
|
+
if (error.queryName)
|
|
324
|
+
detail += ` in ${error.queryName}`;
|
|
325
|
+
if (error.cause instanceof Error && error.cause.message)
|
|
326
|
+
detail += `: ${error.cause.message}`;
|
|
327
|
+
}
|
|
328
|
+
console.error(`Data operation ${name} failed (${requestId}): ${detail}`);
|
|
227
329
|
const failure = toDataOperationFailure(error, signal.aborted);
|
|
228
330
|
return problem(failure.status, failure.code, failure.message, requestId);
|
|
229
331
|
}
|
|
@@ -235,55 +337,6 @@ function problem(status, code, message, requestId) {
|
|
|
235
337
|
}
|
|
236
338
|
var MAX_INPUT_BYTES = 16384;
|
|
237
339
|
|
|
238
|
-
class InputTooLargeError extends Error {
|
|
239
|
-
}
|
|
240
|
-
async function readInput(request) {
|
|
241
|
-
const reader = request.body?.getReader();
|
|
242
|
-
if (!reader)
|
|
243
|
-
return "";
|
|
244
|
-
const chunks = [];
|
|
245
|
-
let size = 0;
|
|
246
|
-
let abort;
|
|
247
|
-
function cancelReader() {
|
|
248
|
-
reader.cancel().catch(() => {});
|
|
249
|
-
}
|
|
250
|
-
try {
|
|
251
|
-
request.signal.throwIfAborted();
|
|
252
|
-
const cancelled = new Promise((_, reject) => {
|
|
253
|
-
function rejectAborted() {
|
|
254
|
-
reject(new Error("Request aborted."));
|
|
255
|
-
cancelReader();
|
|
256
|
-
}
|
|
257
|
-
abort = rejectAborted;
|
|
258
|
-
request.signal.addEventListener("abort", rejectAborted, { once: true });
|
|
259
|
-
if (request.signal.aborted)
|
|
260
|
-
rejectAborted();
|
|
261
|
-
});
|
|
262
|
-
while (true) {
|
|
263
|
-
const { done, value } = await Promise.race([reader.read(), cancelled]);
|
|
264
|
-
if (done)
|
|
265
|
-
break;
|
|
266
|
-
size += value.byteLength;
|
|
267
|
-
if (size > MAX_INPUT_BYTES) {
|
|
268
|
-
cancelReader();
|
|
269
|
-
throw new InputTooLargeError;
|
|
270
|
-
}
|
|
271
|
-
chunks.push(value);
|
|
272
|
-
}
|
|
273
|
-
const bytes = new Uint8Array(size);
|
|
274
|
-
let offset = 0;
|
|
275
|
-
for (const chunk of chunks) {
|
|
276
|
-
bytes.set(chunk, offset);
|
|
277
|
-
offset += chunk.byteLength;
|
|
278
|
-
}
|
|
279
|
-
return new TextDecoder().decode(bytes);
|
|
280
|
-
} finally {
|
|
281
|
-
if (abort)
|
|
282
|
-
request.signal.removeEventListener("abort", abort);
|
|
283
|
-
reader.releaseLock();
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
340
|
// src/server/local.ts
|
|
288
341
|
function serveLocalApp({
|
|
289
342
|
page,
|
|
@@ -291,6 +344,7 @@ function serveLocalApp({
|
|
|
291
344
|
title,
|
|
292
345
|
port = Number(process.env.PORT ?? 25837)
|
|
293
346
|
}) {
|
|
347
|
+
const lakehouse = localLakehouse();
|
|
294
348
|
const server = Bun.serve({
|
|
295
349
|
hostname: "127.0.0.1",
|
|
296
350
|
port,
|
|
@@ -298,113 +352,114 @@ function serveLocalApp({
|
|
|
298
352
|
idleTimeout: 60,
|
|
299
353
|
routes: { "/": page },
|
|
300
354
|
fetch: createDataHandler(operations, async () => ({
|
|
301
|
-
lakehouse
|
|
355
|
+
lakehouse
|
|
302
356
|
}))
|
|
303
357
|
});
|
|
304
358
|
console.log(`${title} running at ${server.url}`);
|
|
305
359
|
return server;
|
|
306
360
|
}
|
|
361
|
+
function validateLakehouseCredentials(environment = process.env) {
|
|
362
|
+
const proxyUrl = environment.ALTERTABLE_DATA_PROXY_URL;
|
|
363
|
+
const proxyToken = environment.ALTERTABLE_DATA_PROXY_TOKEN;
|
|
364
|
+
const username = environment.ALTERTABLE_LAKEHOUSE_USERNAME;
|
|
365
|
+
const password = environment.ALTERTABLE_LAKEHOUSE_PASSWORD;
|
|
366
|
+
if (proxyUrl && !proxyToken)
|
|
367
|
+
throw new Error("Missing ALTERTABLE_DATA_PROXY_TOKEN. Set it when ALTERTABLE_DATA_PROXY_URL is configured.");
|
|
368
|
+
if (!proxyUrl && (!username || !password))
|
|
369
|
+
throw new Error("Missing lakehouse credentials. Set both ALTERTABLE_LAKEHOUSE_USERNAME and ALTERTABLE_LAKEHOUSE_PASSWORD, or use the CLI lakehouse proxy.");
|
|
370
|
+
}
|
|
307
371
|
function localLakehouse(environment = process.env, request = fetch) {
|
|
372
|
+
validateLakehouseCredentials(environment);
|
|
373
|
+
const proxyUrl = environment.ALTERTABLE_DATA_PROXY_URL;
|
|
374
|
+
const proxyToken = environment.ALTERTABLE_DATA_PROXY_TOKEN;
|
|
375
|
+
const username = environment.ALTERTABLE_LAKEHOUSE_USERNAME;
|
|
376
|
+
const password = environment.ALTERTABLE_LAKEHOUSE_PASSWORD;
|
|
308
377
|
return {
|
|
309
378
|
async queryAll(statement, { limit, signal, params }) {
|
|
310
|
-
|
|
311
|
-
const
|
|
312
|
-
const
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
379
|
+
signal.throwIfAborted();
|
|
380
|
+
const cancellation = new AbortController;
|
|
381
|
+
const client = new AltertableLakehouseClient({
|
|
382
|
+
baseUrl: proxyUrl || environment.ALTERTABLE_API_BASE || undefined,
|
|
383
|
+
username,
|
|
384
|
+
password,
|
|
385
|
+
basicAuthToken: proxyUrl ? proxyToken : undefined,
|
|
386
|
+
retryPolicy: { maxAttempts: 1 },
|
|
387
|
+
fetch: Object.assign(async (url, options) => {
|
|
388
|
+
const headers = new Headers(options?.headers);
|
|
389
|
+
if (proxyUrl)
|
|
390
|
+
headers.set("authorization", `Bearer ${proxyToken}`);
|
|
391
|
+
const requestSignal = AbortSignal.any([
|
|
392
|
+
signal,
|
|
393
|
+
cancellation.signal,
|
|
394
|
+
...options?.signal ? [options.signal] : []
|
|
395
|
+
]);
|
|
396
|
+
const response = await request(url, {
|
|
397
|
+
...options,
|
|
398
|
+
headers,
|
|
399
|
+
signal: requestSignal
|
|
400
|
+
});
|
|
401
|
+
return boundedQueryResponse(response, requestSignal);
|
|
402
|
+
}, { preconnect() {} })
|
|
403
|
+
});
|
|
320
404
|
try {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
405
|
+
const result = await client.query({ statement, limit, params });
|
|
406
|
+
const { columns, metadata } = result;
|
|
407
|
+
if (!metadata || typeof metadata !== "object" || Array.isArray(metadata) || metadata.query_id !== undefined && typeof metadata.query_id !== "string")
|
|
408
|
+
throw new Error("Invalid query metadata.");
|
|
409
|
+
if (new Set(columns.map((column) => column.name)).size !== columns.length)
|
|
410
|
+
throw new Error("Query columns must have unique names.");
|
|
411
|
+
const rows = [];
|
|
412
|
+
for await (const row of result.rows) {
|
|
413
|
+
signal.throwIfAborted();
|
|
414
|
+
if (rows.length >= limit)
|
|
415
|
+
throw new Error("Lakehouse returned more rows than requested.");
|
|
416
|
+
const values = columns.map((column) => Object.hasOwn(row, column.name) ? row[column.name] : undefined);
|
|
417
|
+
if (values.some((value) => value === undefined))
|
|
418
|
+
throw new Error("Query row is missing a column.");
|
|
419
|
+
rows.push(values);
|
|
420
|
+
}
|
|
421
|
+
return { columns, rows, queryId: metadata.query_id };
|
|
334
422
|
} catch (error) {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
423
|
+
signal.throwIfAborted();
|
|
424
|
+
if (error instanceof QueryError)
|
|
425
|
+
throw new DataSourceError("query_rejected", undefined, {
|
|
426
|
+
cause: error
|
|
427
|
+
});
|
|
428
|
+
if (error instanceof NetworkError || error instanceof TimeoutError)
|
|
429
|
+
throw new DataSourceError("unavailable", undefined, { cause: error });
|
|
430
|
+
if (error instanceof AltertableLakehouseError && error.statusCode !== undefined) {
|
|
431
|
+
const status = error.statusCode;
|
|
432
|
+
const reason = status === 401 ? "unauthorized" : status === 403 ? "forbidden" : status === 429 ? "rate_limited" : status === 400 || status === 422 ? "query_rejected" : "unavailable";
|
|
433
|
+
throw new DataSourceError(reason, status, { cause: error });
|
|
434
|
+
}
|
|
435
|
+
throw error;
|
|
436
|
+
} finally {
|
|
437
|
+
cancellation.abort();
|
|
342
438
|
}
|
|
343
|
-
const body = await readQueryBody(response);
|
|
344
|
-
return parseQueryResult(body, limit);
|
|
345
439
|
}
|
|
346
440
|
};
|
|
347
441
|
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
throw new Error("Query response is empty.");
|
|
352
|
-
const decoder = new TextDecoder;
|
|
353
|
-
let body = "";
|
|
442
|
+
function boundedQueryResponse(response, signal) {
|
|
443
|
+
if (!response.body)
|
|
444
|
+
return response;
|
|
354
445
|
let bytes = 0;
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
await reader.cancel();
|
|
362
|
-
throw new Error("Query response is too large.");
|
|
446
|
+
const body = response.body.pipeThrough(new TransformStream({
|
|
447
|
+
transform(chunk, controller) {
|
|
448
|
+
bytes += chunk.byteLength;
|
|
449
|
+
if (bytes > 5000000)
|
|
450
|
+
throw new Error("Query response is too large.");
|
|
451
|
+
controller.enqueue(chunk);
|
|
363
452
|
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
}
|
|
369
|
-
function parseQueryResult(body, limit) {
|
|
370
|
-
const lines = body.trimEnd().split(`
|
|
371
|
-
`).filter((line) => line.trim());
|
|
372
|
-
if (!lines.length)
|
|
373
|
-
throw new Error("Query response is empty.");
|
|
374
|
-
const metadata = JSON.parse(lines[0]);
|
|
375
|
-
if (!metadata || typeof metadata !== "object" || Array.isArray(metadata) || "query_id" in metadata && metadata.query_id !== undefined && typeof metadata.query_id !== "string")
|
|
376
|
-
throw new Error("Invalid query metadata.");
|
|
377
|
-
const queryId = "query_id" in metadata ? metadata.query_id : undefined;
|
|
378
|
-
let firstRowIndex = 1;
|
|
379
|
-
let columns = [];
|
|
380
|
-
if (lines[firstRowIndex]) {
|
|
381
|
-
const columnDefinitions = JSON.parse(lines[firstRowIndex]);
|
|
382
|
-
if (Array.isArray(columnDefinitions) && columnDefinitions.every((value) => typeof value === "string" || typeof value === "object" && value !== null && ("name" in value))) {
|
|
383
|
-
columns = columnDefinitions.map((value) => typeof value === "string" ? { name: value } : parseColumn(value));
|
|
384
|
-
firstRowIndex++;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
const rows = lines.slice(firstRowIndex).map((line) => {
|
|
388
|
-
const row = JSON.parse(line);
|
|
389
|
-
if (!Array.isArray(row) || firstRowIndex === 2 && row.length !== columns.length)
|
|
390
|
-
throw new Error("Invalid query row.");
|
|
391
|
-
return row;
|
|
453
|
+
}), { signal });
|
|
454
|
+
return new Response(body, {
|
|
455
|
+
status: response.status,
|
|
456
|
+
headers: response.headers
|
|
392
457
|
});
|
|
393
|
-
if (rows.length > limit)
|
|
394
|
-
throw new Error("Lakehouse returned more rows than requested.");
|
|
395
|
-
return { columns, rows, queryId };
|
|
396
|
-
}
|
|
397
|
-
function parseColumn(value) {
|
|
398
|
-
if (!value || typeof value !== "object" || !("name" in value) || typeof value.name !== "string" || "type" in value && value.type !== undefined && typeof value.type !== "string")
|
|
399
|
-
throw new Error("Invalid query column.");
|
|
400
|
-
return {
|
|
401
|
-
name: value.name,
|
|
402
|
-
..."type" in value && value.type !== undefined ? { type: value.type } : {}
|
|
403
|
-
};
|
|
404
458
|
}
|
|
405
459
|
export {
|
|
406
460
|
localLakehouse,
|
|
407
|
-
serveLocalApp
|
|
461
|
+
serveLocalApp,
|
|
462
|
+
validateLakehouseCredentials
|
|
408
463
|
};
|
|
409
464
|
|
|
410
|
-
//# debugId=
|
|
465
|
+
//# debugId=6A4E2C19477DE8E064756E2164756E21
|