@altertable/data-app 0.68.0 → 0.69.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.
Files changed (175) hide show
  1. package/AGENTS.md +6 -0
  2. package/CONTRIBUTING.md +27 -1
  3. package/README.md +18 -21
  4. package/THIRD_PARTY_NOTICES +18 -0
  5. package/dist/chunks/index-22d576sn.js +153 -0
  6. package/dist/chunks/index-22d576sn.js.map +11 -0
  7. package/dist/chunks/{index-ktmvs916.js → index-2sbp9h2b.js} +37 -21
  8. package/dist/chunks/index-2sbp9h2b.js.map +13 -0
  9. package/dist/chunks/{index-23dktynd.js → index-ap60vggk.js} +4 -4
  10. package/dist/chunks/{index-m6n8ctfc.js → index-cv36cyna.js} +4 -4
  11. package/dist/chunks/index-cv36cyna.js.map +10 -0
  12. package/dist/chunks/{index-ag0cgnvq.js → index-k766wwwr.js} +13 -9
  13. package/dist/chunks/index-k766wwwr.js.map +13 -0
  14. package/dist/chunks/{index-mt93ff2b.js → index-kwnqycjt.js} +2 -2
  15. package/dist/chunks/index-kwnqycjt.js.map +10 -0
  16. package/dist/chunks/index-m0gbxtgp.js +355 -0
  17. package/dist/chunks/index-m0gbxtgp.js.map +13 -0
  18. package/dist/chunks/{index-gvers8jw.js → index-qhzdzca2.js} +3709 -2170
  19. package/dist/chunks/index-qhzdzca2.js.map +118 -0
  20. package/dist/chunks/{index-x69p7cv7.js → index-r7b1ftwy.js} +3 -3
  21. package/dist/chunks/index-r7b1ftwy.js.map +10 -0
  22. package/dist/chunks/{index-ev2b5aaf.js → index-sx488hvs.js} +4 -4
  23. package/dist/chunks/{index-f5cnbrjz.js → index-wss3gd3h.js} +39 -9
  24. package/dist/chunks/index-wss3gd3h.js.map +13 -0
  25. package/dist/chunks/{index-qd5xgx6y.js → index-zcqxfwdq.js} +5 -12
  26. package/dist/chunks/index-zcqxfwdq.js.map +13 -0
  27. package/dist/client/index.js +6 -6
  28. package/dist/core/appearance.js +1 -1
  29. package/dist/core/config.js +3 -3
  30. package/dist/core/contract.js +3 -3
  31. package/dist/core/format.js +10 -2
  32. package/dist/core/format.js.map +1 -1
  33. package/dist/embed/index.js +26 -25
  34. package/dist/embed/index.js.map +4 -4
  35. package/dist/index.js +3 -3
  36. package/dist/local.js +166 -144
  37. package/dist/local.js.map +8 -7
  38. package/dist/preview.js +1456 -0
  39. package/dist/preview.js.map +17 -0
  40. package/dist/react/embed/index.js +1 -1
  41. package/dist/react/index.js +250 -93
  42. package/dist/react/index.js.map +10 -10
  43. package/dist/react/ui/index.js +1168 -1165
  44. package/dist/react/ui/index.js.map +13 -11
  45. package/dist/server.js +68 -57
  46. package/dist/server.js.map +7 -6
  47. package/dist/types/client/download.d.ts +5 -0
  48. package/dist/types/client/iframe.d.ts +4 -2
  49. package/dist/types/client/logger.d.ts +10 -2
  50. package/dist/types/core/annotations.d.ts +5 -0
  51. package/dist/types/core/appearance.d.ts +2 -0
  52. package/dist/types/core/bridge-endpoint.d.ts +2 -2
  53. package/dist/types/core/bridge.d.ts +22 -0
  54. package/dist/types/core/config.d.ts +5 -0
  55. package/dist/types/core/contract.d.ts +3 -1
  56. package/dist/types/core/date-format.d.ts +24 -0
  57. package/dist/types/core/dimension.d.ts +2 -1
  58. package/dist/types/core/filters.d.ts +4 -3
  59. package/dist/types/core/format.d.ts +5 -3
  60. package/dist/types/core/messages.d.ts +3 -1
  61. package/dist/types/core/operation-types.d.ts +7 -7
  62. package/dist/types/core/time-interval.d.ts +96 -0
  63. package/dist/types/core/variables.d.ts +13 -4
  64. package/dist/types/embed/host.d.ts +4 -4
  65. package/dist/types/react/annotations/annotation-editor-state.d.ts +2 -0
  66. package/dist/types/react/annotations/annotation-targets.d.ts +1 -0
  67. package/dist/types/react/annotations/getAnnotationProps.d.ts +5 -3
  68. package/dist/types/react/annotations/useAnnotationGeometry.d.ts +2 -2
  69. package/dist/types/react/bindings.d.ts +26 -3
  70. package/dist/types/react/date-formatting.d.ts +12 -0
  71. package/dist/types/react/hooks.d.ts +10 -9
  72. package/dist/types/react/index.d.ts +7 -2
  73. package/dist/types/react/search-suggestions.d.ts +8 -0
  74. package/dist/types/react/style-contract.d.ts +1 -5
  75. package/dist/types/react/ui/ChartLegend.d.ts +3 -2
  76. package/dist/types/react/ui/ChoicePicker.d.ts +2 -0
  77. package/dist/types/react/ui/DataApp.d.ts +5 -4
  78. package/dist/types/react/ui/DataAppFrame.d.ts +14 -3
  79. package/dist/types/react/ui/DataTable.d.ts +6 -4
  80. package/dist/types/react/ui/DataWidget.d.ts +4 -0
  81. package/dist/types/react/ui/DateDisplay.d.ts +9 -0
  82. package/dist/types/react/ui/DateRangePicker.d.ts +3 -1
  83. package/dist/types/react/ui/DateTimeTooltip.d.ts +3 -2
  84. package/dist/types/react/ui/DimensionPicker.d.ts +3 -1
  85. package/dist/types/react/ui/EmptyState.d.ts +1 -1
  86. package/dist/types/react/ui/GradientScroll.d.ts +8 -0
  87. package/dist/types/react/ui/Grid.d.ts +1 -1
  88. package/dist/types/react/ui/MetricWidget.d.ts +2 -0
  89. package/dist/types/react/ui/NumberFilterPicker.d.ts +3 -1
  90. package/dist/types/react/ui/PeriodSummary.d.ts +2 -1
  91. package/dist/types/react/ui/PickerField.d.ts +15 -0
  92. package/dist/types/react/ui/PopoverSurface.d.ts +5 -0
  93. package/dist/types/react/ui/RequestHint.d.ts +2 -2
  94. package/dist/types/react/ui/SearchField.d.ts +3 -1
  95. package/dist/types/react/ui/SearchTypeahead.d.ts +15 -0
  96. package/dist/types/react/ui/SectionHeading.d.ts +8 -0
  97. package/dist/types/react/ui/Select.d.ts +5 -1
  98. package/dist/types/react/ui/Sheet.d.ts +3 -1
  99. package/dist/types/react/ui/TableWidget.d.ts +8 -0
  100. package/dist/types/react/ui/Tabs.d.ts +4 -3
  101. package/dist/types/react/ui/VariableBar.d.ts +1 -1
  102. package/dist/types/react/ui/VisualizationWidget.d.ts +1 -1
  103. package/dist/types/react/ui/chart-date-axis.d.ts +15 -0
  104. package/dist/types/react/ui/chart-interaction.d.ts +21 -0
  105. package/dist/types/react/ui/chart-primitives.d.ts +4 -4
  106. package/dist/types/react/ui/chart-time.d.ts +16 -0
  107. package/dist/types/react/ui/chartColor.d.ts +1 -1
  108. package/dist/types/react/ui/icons.d.ts +3 -1
  109. package/dist/types/react/ui/index.d.ts +8 -4
  110. package/dist/types/react/ui/metric-comparison.d.ts +2 -2
  111. package/dist/types/react/ui/metric.d.ts +2 -2
  112. package/dist/types/react/ui/presentation.d.ts +2 -0
  113. package/dist/types/react/ui/searchItems.d.ts +2 -0
  114. package/dist/types/react/ui/variables.d.ts +1 -1
  115. package/dist/types/react/view-control-bar.d.ts +16 -0
  116. package/dist/types/react/view-controls.d.ts +3 -2
  117. package/dist/types/react/view-runtime.d.ts +3 -1
  118. package/dist/types/react/view.d.ts +14 -2
  119. package/dist/types/server/local.d.ts +3 -1
  120. package/dist/types/server/preview/host.d.ts +1 -0
  121. package/dist/types/server/preview/index.d.ts +16 -0
  122. package/dist/types/server/reload-channel.d.ts +6 -0
  123. package/dist/types/server/request-body.d.ts +4 -0
  124. package/dist/worker.js +1 -1
  125. package/docs/app-authoring.md +17 -9
  126. package/docs/client.md +1 -1
  127. package/docs/contract.md +8 -9
  128. package/docs/embed.md +11 -3
  129. package/docs/formatting-and-appearance.md +36 -5
  130. package/docs/layout.md +31 -8
  131. package/docs/react-embed.md +4 -4
  132. package/docs/react.md +1 -1
  133. package/docs/server-bun.md +25 -0
  134. package/docs/server.md +1 -1
  135. package/docs/styling.md +7 -28
  136. package/docs/ui.md +21 -18
  137. package/docs/variables.md +92 -15
  138. package/docs/views.md +13 -36
  139. package/docs/widgets.md +38 -26
  140. package/examples/starter-data-app/index.tsx +0 -1
  141. package/examples/starter-local-data-app/.bun-version +1 -0
  142. package/examples/starter-local-data-app/.node-version +1 -0
  143. package/examples/starter-local-data-app/.oxfmtrc.json +4 -0
  144. package/examples/starter-local-data-app/.oxlintrc.json +33 -0
  145. package/examples/starter-local-data-app/AGENTS.md +22 -0
  146. package/examples/starter-local-data-app/README.md +54 -0
  147. package/examples/starter-local-data-app/app.ts +22 -0
  148. package/examples/starter-local-data-app/bunfig.toml +2 -0
  149. package/examples/starter-local-data-app/package.json +33 -0
  150. package/examples/starter-local-data-app/src/App.tsx +8 -0
  151. package/examples/starter-local-data-app/src/assets.d.ts +1 -0
  152. package/examples/starter-local-data-app/src/data-context.ts +21 -0
  153. package/examples/starter-local-data-app/src/favicon.svg +13 -0
  154. package/examples/starter-local-data-app/src/index.html +14 -0
  155. package/examples/starter-local-data-app/src/main.tsx +7 -0
  156. package/examples/starter-local-data-app/src/operations.ts +6 -0
  157. package/examples/starter-local-data-app/src/server.ts +6 -0
  158. package/examples/starter-local-data-app/tsconfig.json +15 -0
  159. package/package.json +13 -3
  160. package/dist/chunks/index-ag0cgnvq.js.map +0 -13
  161. package/dist/chunks/index-dq7b35fn.js +0 -307
  162. package/dist/chunks/index-dq7b35fn.js.map +0 -13
  163. package/dist/chunks/index-f5cnbrjz.js.map +0 -13
  164. package/dist/chunks/index-gvers8jw.js.map +0 -109
  165. package/dist/chunks/index-ktmvs916.js.map +0 -13
  166. package/dist/chunks/index-m6n8ctfc.js.map +0 -10
  167. package/dist/chunks/index-mev09s5v.js +0 -77
  168. package/dist/chunks/index-mev09s5v.js.map +0 -10
  169. package/dist/chunks/index-mt93ff2b.js.map +0 -10
  170. package/dist/chunks/index-qd5xgx6y.js.map +0 -13
  171. package/dist/chunks/index-x69p7cv7.js.map +0 -10
  172. package/dist/types/react/shellStyles.d.ts +0 -3
  173. package/dist/types/react/ui/DataAppSkeleton.d.ts +0 -7
  174. /package/dist/chunks/{index-23dktynd.js.map → index-ap60vggk.js.map} +0 -0
  175. /package/dist/chunks/{index-ev2b5aaf.js.map → index-sx488hvs.js.map} +0 -0
package/dist/local.js CHANGED
@@ -11,14 +11,74 @@ class DataSourceError extends Error {
11
11
  static [Symbol.hasInstance](value) {
12
12
  return value instanceof Error && value[sourceErrorBrand] === true;
13
13
  }
14
- constructor(reason, status) {
15
- super(`Data source ${reason}${status ? ` (${status})` : ""}.`);
14
+ constructor(reason, status, options) {
15
+ super(`Data source ${reason}${status ? ` (${status})` : ""}.`, options);
16
16
  this.reason = reason;
17
17
  this.status = status;
18
18
  this.name = "DataSourceError";
19
19
  }
20
20
  }
21
21
 
22
+ // src/server/local.ts
23
+ import {
24
+ AltertableLakehouseClient,
25
+ AltertableLakehouseError,
26
+ NetworkError,
27
+ TimeoutError,
28
+ QueryError
29
+ } from "@altertable/lakehouse";
30
+
31
+ // src/server/request-body.ts
32
+ class RequestBodyTooLargeError extends Error {
33
+ }
34
+ async function readRequestBody(request, maxBytes) {
35
+ const body = request.body;
36
+ if (!body)
37
+ return "";
38
+ const reader = body.getReader();
39
+ const chunks = [];
40
+ let size = 0;
41
+ let abort;
42
+ function cancelReader() {
43
+ reader.cancel().catch(() => {});
44
+ }
45
+ try {
46
+ request.signal.throwIfAborted();
47
+ const cancelled = new Promise((_, reject) => {
48
+ function rejectAborted() {
49
+ reject(new Error("Request aborted."));
50
+ cancelReader();
51
+ }
52
+ abort = rejectAborted;
53
+ request.signal.addEventListener("abort", rejectAborted, { once: true });
54
+ if (request.signal.aborted)
55
+ rejectAborted();
56
+ });
57
+ while (true) {
58
+ const { done, value } = await Promise.race([reader.read(), cancelled]);
59
+ if (done)
60
+ break;
61
+ size += value.byteLength;
62
+ if (size > maxBytes) {
63
+ cancelReader();
64
+ throw new RequestBodyTooLargeError;
65
+ }
66
+ chunks.push(value);
67
+ }
68
+ const bytes = new Uint8Array(size);
69
+ let offset = 0;
70
+ for (const chunk of chunks) {
71
+ bytes.set(chunk, offset);
72
+ offset += chunk.byteLength;
73
+ }
74
+ return new TextDecoder().decode(bytes);
75
+ } finally {
76
+ if (abort)
77
+ request.signal.removeEventListener("abort", abort);
78
+ reader.releaseLock();
79
+ }
80
+ }
81
+
22
82
  // src/core/operation.ts
23
83
  async function executeDataOperation(operation, value, {
24
84
  operationName,
@@ -46,7 +106,7 @@ async function executeDataOperation(operation, value, {
46
106
  queryParams
47
107
  });
48
108
  const body = {
49
- data: operation.output(output),
109
+ data: output,
50
110
  requestId,
51
111
  queriedAt: new Date().toISOString(),
52
112
  queryIds: execution.queryIds,
@@ -199,10 +259,10 @@ function createDataHandler(operations, authorize) {
199
259
  if (request.signal.aborted)
200
260
  return problem(504, "timeout", "The data request timed out.", requestId);
201
261
  try {
202
- const body = await readInput(request);
262
+ const body = await readRequestBody(request, MAX_INPUT_BYTES);
203
263
  input = JSON.parse(body);
204
264
  } catch (error) {
205
- if (error instanceof InputTooLargeError)
265
+ if (error instanceof RequestBodyTooLargeError)
206
266
  return problem(413, "input_too_large", "Input is too large.", requestId);
207
267
  return problem(400, "invalid_input", "Check this operation's inputs.", requestId);
208
268
  }
@@ -222,8 +282,17 @@ function createDataHandler(operations, authorize) {
222
282
  }
223
283
  });
224
284
  } catch (error) {
225
- const source = error instanceof DataSourceError ? error : null;
226
- console.error(`Data operation ${name} failed (${requestId}): ${source ? `${source.reason}${source.status ? ` (${source.status})` : ""}${source.queryName ? ` in ${source.queryName}` : ""}` : error instanceof Error ? error.name : "unknown"}`);
285
+ let detail = error instanceof Error ? error.name : "unknown";
286
+ if (error instanceof DataSourceError) {
287
+ detail = error.reason;
288
+ if (error.status)
289
+ detail += ` (${error.status})`;
290
+ if (error.queryName)
291
+ detail += ` in ${error.queryName}`;
292
+ if (error.cause instanceof Error && error.cause.message)
293
+ detail += `: ${error.cause.message}`;
294
+ }
295
+ console.error(`Data operation ${name} failed (${requestId}): ${detail}`);
227
296
  const failure = toDataOperationFailure(error, signal.aborted);
228
297
  return problem(failure.status, failure.code, failure.message, requestId);
229
298
  }
@@ -235,55 +304,6 @@ function problem(status, code, message, requestId) {
235
304
  }
236
305
  var MAX_INPUT_BYTES = 16384;
237
306
 
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
307
  // src/server/local.ts
288
308
  function serveLocalApp({
289
309
  page,
@@ -291,6 +311,7 @@ function serveLocalApp({
291
311
  title,
292
312
  port = Number(process.env.PORT ?? 25837)
293
313
  }) {
314
+ const lakehouse = localLakehouse();
294
315
  const server = Bun.serve({
295
316
  hostname: "127.0.0.1",
296
317
  port,
@@ -298,113 +319,114 @@ function serveLocalApp({
298
319
  idleTimeout: 60,
299
320
  routes: { "/": page },
300
321
  fetch: createDataHandler(operations, async () => ({
301
- lakehouse: localLakehouse()
322
+ lakehouse
302
323
  }))
303
324
  });
304
325
  console.log(`${title} running at ${server.url}`);
305
326
  return server;
306
327
  }
328
+ function validateLakehouseCredentials(environment = process.env) {
329
+ const proxyUrl = environment.ALTERTABLE_DATA_PROXY_URL;
330
+ const proxyToken = environment.ALTERTABLE_DATA_PROXY_TOKEN;
331
+ const username = environment.ALTERTABLE_LAKEHOUSE_USERNAME;
332
+ const password = environment.ALTERTABLE_LAKEHOUSE_PASSWORD;
333
+ if (proxyUrl && !proxyToken)
334
+ throw new Error("Missing ALTERTABLE_DATA_PROXY_TOKEN. Set it when ALTERTABLE_DATA_PROXY_URL is configured.");
335
+ if (!proxyUrl && (!username || !password))
336
+ throw new Error("Missing lakehouse credentials. Set both ALTERTABLE_LAKEHOUSE_USERNAME and ALTERTABLE_LAKEHOUSE_PASSWORD, or use the CLI lakehouse proxy.");
337
+ }
307
338
  function localLakehouse(environment = process.env, request = fetch) {
339
+ validateLakehouseCredentials(environment);
340
+ const proxyUrl = environment.ALTERTABLE_DATA_PROXY_URL;
341
+ const proxyToken = environment.ALTERTABLE_DATA_PROXY_TOKEN;
342
+ const username = environment.ALTERTABLE_LAKEHOUSE_USERNAME;
343
+ const password = environment.ALTERTABLE_LAKEHOUSE_PASSWORD;
308
344
  return {
309
345
  async queryAll(statement, { limit, signal, params }) {
310
- const proxyUrl = environment.ALTERTABLE_DATA_PROXY_URL;
311
- const proxyToken = environment.ALTERTABLE_DATA_PROXY_TOKEN;
312
- const username = environment.ALTERTABLE_LAKEHOUSE_USERNAME;
313
- const password = environment.ALTERTABLE_LAKEHOUSE_PASSWORD;
314
- if (!proxyUrl && (!username || !password))
315
- throw new DataSourceError("unauthorized");
316
- if (proxyUrl && !proxyToken)
317
- throw new DataSourceError("unauthorized");
318
- const base = proxyUrl ?? environment.ALTERTABLE_API_BASE ?? "https://api.altertable.ai";
319
- let response;
346
+ signal.throwIfAborted();
347
+ const cancellation = new AbortController;
348
+ const client = new AltertableLakehouseClient({
349
+ baseUrl: proxyUrl || environment.ALTERTABLE_API_BASE || undefined,
350
+ username,
351
+ password,
352
+ basicAuthToken: proxyUrl ? proxyToken : undefined,
353
+ retryPolicy: { maxAttempts: 1 },
354
+ fetch: Object.assign(async (url, options) => {
355
+ const headers = new Headers(options?.headers);
356
+ if (proxyUrl)
357
+ headers.set("authorization", `Bearer ${proxyToken}`);
358
+ const requestSignal = AbortSignal.any([
359
+ signal,
360
+ cancellation.signal,
361
+ ...options?.signal ? [options.signal] : []
362
+ ]);
363
+ const response = await request(url, {
364
+ ...options,
365
+ headers,
366
+ signal: requestSignal
367
+ });
368
+ return boundedQueryResponse(response, requestSignal);
369
+ }, { preconnect() {} })
370
+ });
320
371
  try {
321
- response = await request(new URL("query", `${base.replace(/\/$/, "")}/`), {
322
- method: "POST",
323
- headers: {
324
- authorization: proxyUrl ? `Bearer ${proxyToken}` : `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`,
325
- "content-type": "application/json"
326
- },
327
- body: JSON.stringify({
328
- statement,
329
- limit,
330
- ...params === undefined ? {} : { params }
331
- }),
332
- signal
333
- });
372
+ const result = await client.query({ statement, limit, params });
373
+ const { columns, metadata } = result;
374
+ if (!metadata || typeof metadata !== "object" || Array.isArray(metadata) || metadata.query_id !== undefined && typeof metadata.query_id !== "string")
375
+ throw new Error("Invalid query metadata.");
376
+ if (new Set(columns.map((column) => column.name)).size !== columns.length)
377
+ throw new Error("Query columns must have unique names.");
378
+ const rows = [];
379
+ for await (const row of result.rows) {
380
+ signal.throwIfAborted();
381
+ if (rows.length >= limit)
382
+ throw new Error("Lakehouse returned more rows than requested.");
383
+ const values = columns.map((column) => Object.hasOwn(row, column.name) ? row[column.name] : undefined);
384
+ if (values.some((value) => value === undefined))
385
+ throw new Error("Query row is missing a column.");
386
+ rows.push(values);
387
+ }
388
+ return { columns, rows, queryId: metadata.query_id };
334
389
  } catch (error) {
335
- if (signal.aborted)
336
- throw error;
337
- throw new DataSourceError("unavailable");
338
- }
339
- if (!response.ok) {
340
- const reason = response.status === 401 ? "unauthorized" : response.status === 403 ? "forbidden" : response.status === 429 ? "rate_limited" : response.status === 400 || response.status === 422 ? "query_rejected" : "unavailable";
341
- throw new DataSourceError(reason, response.status);
390
+ signal.throwIfAborted();
391
+ if (error instanceof QueryError)
392
+ throw new DataSourceError("query_rejected", undefined, {
393
+ cause: error
394
+ });
395
+ if (error instanceof NetworkError || error instanceof TimeoutError)
396
+ throw new DataSourceError("unavailable", undefined, { cause: error });
397
+ if (error instanceof AltertableLakehouseError && error.statusCode !== undefined) {
398
+ const status = error.statusCode;
399
+ const reason = status === 401 ? "unauthorized" : status === 403 ? "forbidden" : status === 429 ? "rate_limited" : status === 400 || status === 422 ? "query_rejected" : "unavailable";
400
+ throw new DataSourceError(reason, status, { cause: error });
401
+ }
402
+ throw error;
403
+ } finally {
404
+ cancellation.abort();
342
405
  }
343
- const body = await readQueryBody(response);
344
- return parseQueryResult(body, limit);
345
406
  }
346
407
  };
347
408
  }
348
- async function readQueryBody(response) {
349
- const reader = response.body?.getReader();
350
- if (!reader)
351
- throw new Error("Query response is empty.");
352
- const decoder = new TextDecoder;
353
- let body = "";
409
+ function boundedQueryResponse(response, signal) {
410
+ if (!response.body)
411
+ return response;
354
412
  let bytes = 0;
355
- while (true) {
356
- const chunk = await reader.read();
357
- if (chunk.done)
358
- break;
359
- bytes += chunk.value.byteLength;
360
- if (bytes > 5000000) {
361
- await reader.cancel();
362
- throw new Error("Query response is too large.");
413
+ const body = response.body.pipeThrough(new TransformStream({
414
+ transform(chunk, controller) {
415
+ bytes += chunk.byteLength;
416
+ if (bytes > 5000000)
417
+ throw new Error("Query response is too large.");
418
+ controller.enqueue(chunk);
363
419
  }
364
- body += decoder.decode(chunk.value, { stream: true });
365
- }
366
- body += decoder.decode();
367
- return body;
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;
420
+ }), { signal });
421
+ return new Response(body, {
422
+ status: response.status,
423
+ headers: response.headers
392
424
  });
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
425
  }
405
426
  export {
406
427
  localLakehouse,
407
- serveLocalApp
428
+ serveLocalApp,
429
+ validateLakehouseCredentials
408
430
  };
409
431
 
410
- //# debugId=E6005345F1D6B04A64756E2164756E21
432
+ //# debugId=37AA50328E705C9864756E2164756E21