@altertable/data-app 0.76.0 → 0.78.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 (69) hide show
  1. package/CONTRIBUTING.md +3 -0
  2. package/REFERENCE.md +392 -276
  3. package/dist/chunks/contract-gu1rDsV0.js +1588 -0
  4. package/dist/chunks/contract-gu1rDsV0.js.map +1 -0
  5. package/dist/chunks/data-client-BMdL73Ab.js +122 -0
  6. package/dist/chunks/data-client-BMdL73Ab.js.map +1 -0
  7. package/dist/chunks/{date-time-range-value-TE8KCsfV.js → date-time-range-value-D-XfqVTT.js} +28 -31
  8. package/dist/chunks/date-time-range-value-D-XfqVTT.js.map +1 -0
  9. package/dist/chunks/{diagnostics-DhCqGklq.js → diagnostics-CX9SsU79.js} +240 -63
  10. package/dist/chunks/diagnostics-CX9SsU79.js.map +1 -0
  11. package/dist/chunks/record-Cr4Uk1K3.js +9 -0
  12. package/dist/chunks/record-Cr4Uk1K3.js.map +1 -0
  13. package/dist/client/index.js +4 -86
  14. package/dist/client/index.js.map +1 -1
  15. package/dist/core/appearance.js +3 -5
  16. package/dist/core/appearance.js.map +1 -1
  17. package/dist/core/config.js +11 -3
  18. package/dist/core/config.js.map +1 -1
  19. package/dist/core/contract.js +3 -4
  20. package/dist/core/format.js +1 -1
  21. package/dist/internal.js +58 -124
  22. package/dist/internal.js.map +1 -1
  23. package/dist/types/chunks/contract.d.ts +592 -308
  24. package/dist/types/chunks/data-client.d.ts +46 -0
  25. package/dist/types/chunks/date-time-range-value.d.ts +28 -2
  26. package/dist/types/chunks/index.d.ts +65 -53
  27. package/dist/types/client/index.d.ts +4 -3
  28. package/dist/types/core/config.d.ts +16 -6
  29. package/dist/types/core/contract.d.ts +3 -5
  30. package/dist/types/core/format.d.ts +1 -2
  31. package/dist/types/index.d.ts +4 -4
  32. package/dist/types/internal.d.ts +26 -7
  33. package/docs/app-authoring.md +23 -14
  34. package/docs/client.md +14 -48
  35. package/docs/contract.md +58 -56
  36. package/docs/data-context.md +13 -6
  37. package/docs/diagnostics.md +16 -23
  38. package/docs/embed.md +60 -25
  39. package/docs/filters.md +271 -0
  40. package/docs/hosted-apps.md +11 -10
  41. package/docs/layout.md +45 -18
  42. package/docs/local-data-apps.md +5 -6
  43. package/docs/react-embed.md +44 -2
  44. package/docs/react.md +1 -2
  45. package/docs/server-bun.md +8 -5
  46. package/docs/stories-and-export.md +23 -14
  47. package/docs/styling.md +2 -1
  48. package/docs/ui.md +18 -8
  49. package/docs/views.md +63 -26
  50. package/docs/widgets.md +40 -16
  51. package/docs/worker.md +1 -1
  52. package/package.json +1 -5
  53. package/dist/chunks/contract-DsyRYgN_.js +0 -626
  54. package/dist/chunks/contract-DsyRYgN_.js.map +0 -1
  55. package/dist/chunks/contract-kNFYPHRK.js +0 -713
  56. package/dist/chunks/contract-kNFYPHRK.js.map +0 -1
  57. package/dist/chunks/date-time-range-value-TE8KCsfV.js.map +0 -1
  58. package/dist/chunks/diagnostics-DhCqGklq.js.map +0 -1
  59. package/dist/chunks/operation-7tiT4cpJ.js +0 -134
  60. package/dist/chunks/operation-7tiT4cpJ.js.map +0 -1
  61. package/dist/chunks/request-body-H1qPmmTN.js +0 -50
  62. package/dist/chunks/request-body-H1qPmmTN.js.map +0 -1
  63. package/dist/server/index.js +0 -90
  64. package/dist/server/index.js.map +0 -1
  65. package/dist/types/chunks/contract2.d.ts +0 -207
  66. package/dist/types/chunks/date-time-range.d.ts +0 -29
  67. package/dist/types/server/index.d.ts +0 -17
  68. package/docs/server.md +0 -41
  69. package/docs/variables.md +0 -280
@@ -1,713 +0,0 @@
1
- import { c as invariant, i as parseDateTimeRange, l as parseDateTimeRangeInput, n as formatDateTimeRange, o as serializeDateTimeRange } from "./date-time-range-value-TE8KCsfV.js";
2
- import { h as isQueryParameters } from "./contract-DsyRYgN_.js";
3
- //#region src/core/filters.ts
4
- const all = { kind: "all" };
5
- const numberOperatorLabels = {
6
- eq: "Equals",
7
- ne: "Does not equal",
8
- gt: "Greater than",
9
- gte: "At least",
10
- lt: "Less than",
11
- lte: "At most"
12
- };
13
- /** Exact numeric predicates; range bounds are inclusive and may be open-ended. */
14
- function numberFilter({ key, label, min, max, defaultValue = all, history = "push", presentation }) {
15
- invariant(!!key.trim(), "Numeric filter requires a URL key.");
16
- invariant((min === void 0 || Number.isFinite(min)) && (max === void 0 || Number.isFinite(max)) && (min === void 0 || max === void 0 || min <= max), "Invalid numeric filter bounds.");
17
- function validNumber(value) {
18
- return typeof value === "number" && Number.isFinite(value) && (min === void 0 || value >= min) && (max === void 0 || value <= max);
19
- }
20
- function valid(value) {
21
- if (!value || typeof value !== "object") return false;
22
- if (value.kind === "all") return true;
23
- if (value.kind === "comparison") return typeof value.operator === "string" && Object.hasOwn(numberOperatorLabels, value.operator) && validNumber(value.value);
24
- return value.kind === "range" && (value.min !== void 0 || value.max !== void 0) && (value.min === void 0 || validNumber(value.min)) && (value.max === void 0 || validNumber(value.max)) && (value.min === void 0 || value.max === void 0 || value.min <= value.max);
25
- }
26
- function encode(value) {
27
- return value.kind === "all" ? null : value.kind === "range" ? JSON.stringify({
28
- kind: "range",
29
- min: value.min,
30
- max: value.max
31
- }) : JSON.stringify({
32
- kind: "comparison",
33
- operator: value.operator,
34
- value: value.value
35
- });
36
- }
37
- invariant(valid(defaultValue), `Invalid default for ${label}.`);
38
- return {
39
- kind: "numberFilter",
40
- presentation,
41
- label,
42
- min,
43
- max,
44
- defaultValue,
45
- clearValue: all,
46
- urlKeys: [key],
47
- history,
48
- valid,
49
- same: (left, right) => encode(left) === encode(right),
50
- describe(value) {
51
- return value.kind === "all" ? "Any" : value.kind === "comparison" ? `${numberOperatorLabels[value.operator]} ${value.value}` : value.min === void 0 ? `At most ${value.max}` : value.max === void 0 ? `At least ${value.min}` : `${value.min}–${value.max}`;
52
- },
53
- read(params) {
54
- const raw = params.get(key);
55
- if (raw === null) return defaultValue;
56
- if (raw.length > 512) return defaultValue;
57
- try {
58
- const value = raw === "all" ? all : JSON.parse(raw);
59
- return valid(value) ? value : defaultValue;
60
- } catch {
61
- return defaultValue;
62
- }
63
- },
64
- write(value) {
65
- return { [key]: encode(value) === encode(defaultValue) ? null : value.kind === "all" ? "all" : encode(value) };
66
- }
67
- };
68
- }
69
- /** Boolean predicate with a distinct unrestricted state. */
70
- function booleanFilter({ key, label, defaultValue = all, history = "push", presentation }) {
71
- invariant(!!key.trim(), "Boolean filter requires a URL key.");
72
- function valid(value) {
73
- return !!value && typeof value === "object" && (value.kind === "all" || value.kind === "is" && typeof value.value === "boolean");
74
- }
75
- function encode(value) {
76
- return value.kind === "all" ? "all" : String(value.value);
77
- }
78
- invariant(valid(defaultValue), `Invalid default for ${label}.`);
79
- return {
80
- kind: "booleanFilter",
81
- presentation,
82
- label,
83
- defaultValue,
84
- clearValue: all,
85
- urlKeys: [key],
86
- history,
87
- valid,
88
- describe: (value) => value.kind === "all" ? "Any" : value.value ? "Yes" : "No",
89
- same: (left, right) => encode(left) === encode(right),
90
- read(params) {
91
- const raw = params.get(key);
92
- return raw === "all" ? all : raw === "true" || raw === "false" ? {
93
- kind: "is",
94
- value: raw === "true"
95
- } : defaultValue;
96
- },
97
- write(value) {
98
- return { [key]: encode(value) === encode(defaultValue) ? null : encode(value) };
99
- }
100
- };
101
- }
102
- function parseNumberSelection(value, filter) {
103
- invariant(filter.valid(value), `Invalid ${filter.label} selection.`);
104
- return value;
105
- }
106
- function parseBooleanSelection(value, filter) {
107
- invariant(filter.valid(value), `Invalid ${filter.label} selection.`);
108
- return value;
109
- }
110
- //#endregion
111
- //#region src/core/date-time-variable.ts
112
- /** Monitoring offers minute/hour windows; analytics offers longer periods and to-date windows. Spread either menu to add custom presets. */
113
- const dateTimeRangePresets = {
114
- monitoring: [
115
- {
116
- label: "Past 5 minutes",
117
- range: "5m",
118
- shortcut: "5m"
119
- },
120
- {
121
- label: "Past 15 minutes",
122
- range: "15m",
123
- shortcut: "15m"
124
- },
125
- {
126
- label: "Past hour",
127
- range: "1h",
128
- shortcut: "1h"
129
- },
130
- {
131
- label: "Past 4 hours",
132
- range: "4h",
133
- shortcut: "4h"
134
- },
135
- {
136
- label: "Past 24 hours",
137
- range: "1d",
138
- shortcut: "1d"
139
- }
140
- ],
141
- analytics: [
142
- {
143
- label: "Past 7 days",
144
- range: "7d",
145
- shortcut: "7d"
146
- },
147
- {
148
- label: "Past 30 days",
149
- range: "30d",
150
- shortcut: "30d"
151
- },
152
- {
153
- label: "Past 90 days",
154
- range: "90d",
155
- shortcut: "90d"
156
- },
157
- {
158
- label: "Month to date",
159
- range: "MTD",
160
- shortcut: "MTD"
161
- },
162
- {
163
- label: "Year to date",
164
- range: "YTD",
165
- shortcut: "YTD"
166
- },
167
- {
168
- label: "Past year",
169
- range: "1y",
170
- shortcut: "1y"
171
- }
172
- ]
173
- };
174
- function dateTimeRangeVariable(options) {
175
- const base = createDateTimeRangeVariable(options);
176
- return options.allowComparison ? withComparison(base, options) : base;
177
- }
178
- /** One declaration drives the generated picker, URL state, and structured query parameter. */
179
- function createDateTimeRangeVariable({ key, label = "Date range", defaultValue, maxRangeDays, contract, presets, history = "push", presentation }) {
180
- invariant(maxRangeDays === void 0 || Number.isFinite(maxRangeDays) && maxRangeDays > 0, "Maximum range days must be positive.");
181
- function parse(value) {
182
- const range = parseDateTimeRange(value, { maxRangeDays });
183
- return contract ? contract.datetime.parse(range) : range;
184
- }
185
- const parsed = typeof defaultValue === "string" ? parseDateTimeRangeInput(defaultValue) : null;
186
- invariant(!parsed || parsed.ok, "Invalid default datetime range.");
187
- const initial = parse(parsed?.ok ? parsed.range : defaultValue);
188
- function valid(value) {
189
- try {
190
- parse(value);
191
- return true;
192
- } catch {
193
- return false;
194
- }
195
- }
196
- function same(left, right) {
197
- return serializeDateTimeRange(left) === serializeDateTimeRange(right);
198
- }
199
- return {
200
- kind: "dateTimeRange",
201
- label,
202
- presentation,
203
- urlKeys: [key],
204
- defaultValue: initial,
205
- clearValue: initial,
206
- maxRangeDays: contract ? Math.min(maxRangeDays ?? Infinity, contract.bounds().maxRangeDays) : maxRangeDays,
207
- contract,
208
- presets,
209
- history,
210
- parse,
211
- valid,
212
- same,
213
- describe: formatDateTimeRange,
214
- read(params) {
215
- const text = params.get(key);
216
- if (!text || text.length > 8192) return parse(initial);
217
- try {
218
- return parse(JSON.parse(text));
219
- } catch {
220
- return parse(initial);
221
- }
222
- },
223
- write(value) {
224
- return { [key]: same(value, initial) ? null : serializeDateTimeRange(parse(value)) };
225
- }
226
- };
227
- }
228
- function withComparison(base, options) {
229
- const contract = options.contract;
230
- const compareKey = options.key === "period" ? "compare" : `${options.key}-compare`;
231
- const initial = { range: base.defaultValue };
232
- function parseSelection(value, now = /* @__PURE__ */ new Date()) {
233
- invariant(!!value && typeof value === "object" && !Array.isArray(value), "Choose a datetime range.");
234
- const selection = value;
235
- invariant(Object.keys(selection).every((key) => key === "range" || key === "comparison") && (selection.comparison === void 0 || selection.comparison === "previous"), "Invalid comparison selection.");
236
- const range = contract.datetime.parse(parseDateTimeRange(selection.range, { maxRangeDays: base.maxRangeDays }), now);
237
- invariant(!selection.comparison || contract.datetime.comparison(range, now), "Comparison is outside available source coverage.");
238
- return {
239
- range,
240
- ...selection.comparison ? { comparison: "previous" } : {}
241
- };
242
- }
243
- function input(value, now = /* @__PURE__ */ new Date()) {
244
- const selection = parseSelection(value, now);
245
- return {
246
- ...contract.datetime.request(selection.range, selection.comparison === "previous", now),
247
- selection,
248
- reference: now.toISOString()
249
- };
250
- }
251
- function same(left, right) {
252
- return left.comparison === right.comparison && base.same(left.range, right.range);
253
- }
254
- function valid(value) {
255
- try {
256
- parseSelection(value);
257
- return true;
258
- } catch {
259
- return false;
260
- }
261
- }
262
- return {
263
- kind: "dateTimeRangeComparison",
264
- label: base.label,
265
- presentation: base.presentation,
266
- history: base.history,
267
- urlKeys: [...base.urlKeys, compareKey],
268
- defaultValue: initial,
269
- clearValue: initial,
270
- contract,
271
- maxRangeDays: base.maxRangeDays,
272
- presets: base.presets,
273
- input,
274
- parseInput(value) {
275
- invariant(!!value && typeof value === "object" && !Array.isArray(value), "Invalid datetime request.");
276
- const request = value;
277
- invariant(Object.keys(request).every((key) => [
278
- "range",
279
- "comparison",
280
- "selection",
281
- "reference"
282
- ].includes(key)) && typeof request.reference === "string", "Invalid datetime request.");
283
- const now = new Date(request.reference);
284
- invariant(Number.isFinite(now.getTime()) && now.toISOString() === request.reference, "Invalid reference date.");
285
- const expected = input(request.selection, now);
286
- base.parse(request.range);
287
- const actual = parseDateTimeRange(request.range);
288
- invariant(base.same(actual, expected.range), "The request must preserve the selected datetime range.");
289
- invariant(request.comparison === null ? expected.comparison === null : !!expected.comparison && base.same(parseDateTimeRange(request.comparison), expected.comparison), "Comparison must be the preceding equal-length range.");
290
- return expected;
291
- },
292
- previous(selection) {
293
- return contract.datetime.comparison(selection.range);
294
- },
295
- valid,
296
- same,
297
- describe: (selection) => `${base.describe(selection.range)}${selection.comparison ? " vs previous period" : ""}`,
298
- read(params) {
299
- const range = base.read(params);
300
- const compared = {
301
- range,
302
- comparison: "previous"
303
- };
304
- return params.get(compareKey) === "previous" && valid(compared) ? compared : { range };
305
- },
306
- write(value) {
307
- const selection = parseSelection(value);
308
- return {
309
- ...base.write(selection.range),
310
- [compareKey]: selection.comparison ?? null
311
- };
312
- }
313
- };
314
- }
315
- //#endregion
316
- //#region src/core/navigation.ts
317
- function validLocation(value) {
318
- if (!value || typeof value !== "object") return false;
319
- const message = value;
320
- return typeof message.search === "string" && message.search.length <= 16384 && (message.search === "" || message.search.startsWith("?")) && typeof message.hash === "string" && message.hash.length <= 4096 && (message.hash === "" || message.hash.startsWith("#"));
321
- }
322
- //#endregion
323
- //#region src/core/messages.ts
324
- const errorBrand = Symbol.for("@altertable/data-app/MessageRoutingError");
325
- /** Only these deliberate, public failures cross the message boundary. Other errors stay private. */
326
- var MessageRoutingError = class extends Error {
327
- code;
328
- requestId;
329
- [errorBrand] = true;
330
- static [Symbol.hasInstance](value) {
331
- return value instanceof Error && value[errorBrand] === true;
332
- }
333
- constructor(code, message, requestId, options) {
334
- super(message, options);
335
- this.code = code;
336
- this.requestId = requestId;
337
- this.name = "MessageRoutingError";
338
- }
339
- };
340
- function defineMessageRoute(route) {
341
- return route;
342
- }
343
- /** Contracts are shared; handlers are supplied by the host and never belong in the app bundle. */
344
- function createMessageRouter(routes, handlers) {
345
- const registered = new Map(Object.entries(routes).map(([name, contract]) => {
346
- const handler = handlers[name];
347
- if (!name || name.length > 256 || typeof handler !== "function") throw new Error("Each message route requires a handler and a bounded name.");
348
- return [name, {
349
- contract,
350
- handler
351
- }];
352
- }));
353
- async function dispatch(value, context) {
354
- if (!value || typeof value !== "object" || typeof value.route !== "string") throw new MessageRoutingError("invalid_message", "Invalid routed message.");
355
- const message = value;
356
- const entry = registered.get(message.route);
357
- if (!entry) throw new MessageRoutingError("unknown_route", "Unknown message route.");
358
- let payload;
359
- try {
360
- payload = entry.contract.input(message.payload);
361
- } catch (error) {
362
- if (error instanceof MessageRoutingError) throw error;
363
- throw new MessageRoutingError("invalid_payload", "Invalid message payload.");
364
- }
365
- context.signal.throwIfAborted();
366
- let result;
367
- try {
368
- result = await entry.handler(payload, context);
369
- } catch (error) {
370
- if (context.signal.aborted) throw context.signal.reason;
371
- if (error instanceof MessageRoutingError) throw error;
372
- throw new MessageRoutingError("request_failed", "The message request failed. Retry the request.");
373
- }
374
- context.signal.throwIfAborted();
375
- try {
376
- return entry.contract.output(result, payload);
377
- } catch (error) {
378
- if (error instanceof MessageRoutingError) throw error;
379
- throw new MessageRoutingError("invalid_response", "Invalid message response.");
380
- }
381
- }
382
- return { dispatch };
383
- }
384
- /** Omit contracts only in generic hosts such as the CLI shell, where Bun validates operations. */
385
- function defineDataQueryRoute(operations) {
386
- return {
387
- operations,
388
- input(value) {
389
- if (!value || typeof value !== "object") throw new Error("Invalid query.");
390
- const request = value;
391
- if (typeof request.operation !== "string" || !request.operation || request.operation.length > 256 || request.operation.includes("/")) throw new Error("Invalid operation.");
392
- const operation = operations && Object.hasOwn(operations, request.operation) ? operations[request.operation] : void 0;
393
- if (operations && !operation) throw new Error("Unknown operation.");
394
- return {
395
- operation: request.operation,
396
- input: operation ? operation.input(request.input) : request.input
397
- };
398
- },
399
- output(value) {
400
- if (!value || typeof value !== "object") throw new Error("Invalid data response.");
401
- const response = value;
402
- if (!Number.isInteger(response.status) || response.status < 100 || response.status > 599 || !response.body || typeof response.body !== "object" || Array.isArray(response.body)) throw new Error("Invalid data response.");
403
- const body = response.body;
404
- if (response.status < 200 || response.status >= 300) {
405
- const failure = body.error;
406
- throw new MessageRoutingError(typeof failure?.code === "string" ? failure.code : "request_failed", typeof failure?.message === "string" ? failure.message : "Could not load data.", typeof failure?.requestId === "string" ? failure.requestId : void 0);
407
- }
408
- if (!("data" in body) || typeof body.requestId !== "string" || typeof body.queriedAt !== "string" || !Array.isArray(body.queryIds) || !body.queryIds.every((id) => typeof id === "string")) throw new Error("Invalid data envelope.");
409
- if (body.queries !== void 0 && (!Array.isArray(body.queries) || !body.queries.every((query) => query && typeof query === "object" && typeof query.name === "string" && typeof query.statement === "string" && (query.params === void 0 || isQueryParameters(query.params)) && (query.queryId === void 0 || typeof query.queryId === "string")))) throw new Error("Invalid query evidence.");
410
- return {
411
- status: response.status,
412
- body
413
- };
414
- }
415
- };
416
- }
417
- const navigationUpdateRoute = /* @__PURE__ */ defineMessageRoute({
418
- input(value) {
419
- if (!value || typeof value !== "object") throw new Error("Invalid navigation.");
420
- const input = value;
421
- if (!validLocation(input) || input.mode !== "push" && input.mode !== "replace" || input.title !== void 0 && (typeof input.title !== "string" || input.title.length > 512)) throw new Error("Invalid navigation.");
422
- return {
423
- search: input.search,
424
- hash: input.hash,
425
- mode: input.mode,
426
- ...input.title === void 0 ? {} : { title: input.title }
427
- };
428
- },
429
- output(value) {
430
- if (value !== null) throw new Error("Invalid navigation response.");
431
- return null;
432
- }
433
- });
434
- /** SQL delivery for browser-owned operations. Hosts must enforce backend access and resource limits. */
435
- const sqlQueryRoute = defineMessageRoute({
436
- input(value) {
437
- if (!value || typeof value !== "object") throw new Error("Invalid SQL query.");
438
- const query = value;
439
- if (typeof query.statement !== "string" || !query.statement.trim() || typeof query.limit !== "number" || !Number.isSafeInteger(query.limit) || query.limit < 1 || query.params !== void 0 && !isQueryParameters(query.params)) throw new Error("Invalid SQL query.");
440
- return {
441
- statement: query.statement,
442
- limit: query.limit,
443
- ...query.params === void 0 ? {} : { params: query.params }
444
- };
445
- },
446
- output(value, input) {
447
- if (!value || typeof value !== "object") throw new Error("Invalid query result.");
448
- const result = value;
449
- if (!Array.isArray(result.columns) || !result.columns.every((column) => column && typeof column.name === "string" && (column.type === void 0 || typeof column.type === "string")) || !Array.isArray(result.rows) || result.rows.length > input.limit || !result.rows.every((row) => Array.isArray(row) && row.length === result.columns.length) || result.queryId !== void 0 && typeof result.queryId !== "string") throw new Error("Invalid query result.");
450
- return {
451
- columns: result.columns,
452
- rows: result.rows,
453
- ...result.queryId === void 0 ? {} : { queryId: result.queryId }
454
- };
455
- }
456
- });
457
- const dataAppRoutes = {
458
- "data:query": /* @__PURE__ */ defineDataQueryRoute(),
459
- "navigation:update": navigationUpdateRoute
460
- };
461
- //#endregion
462
- //#region src/core/annotations.ts
463
- function object(value) {
464
- if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("Invalid annotation.");
465
- return value;
466
- }
467
- function text(value, limit, required = false) {
468
- if (typeof value !== "string" || value.length > limit || required && !value.trim()) throw new Error("Invalid annotation text.");
469
- return value;
470
- }
471
- function names(value) {
472
- if (!Array.isArray(value) || value.length > 20) throw new Error("Invalid annotation references.");
473
- return value.map((name) => text(name, 128, true));
474
- }
475
- function number(value, positive = false) {
476
- if (typeof value !== "number" || !Number.isFinite(value) || Math.abs(value) > 1e6 || positive && value < 0) throw new Error("Invalid annotation geometry.");
477
- return value;
478
- }
479
- /** Reconstruct at the boundary; discard extra fields and bound the entire JSON payload. */
480
- function parseDataAppAnnotationDraft(value) {
481
- const input = object(value);
482
- const target = object(input.target);
483
- const context = object(input.context);
484
- const viewport = object(context.viewport);
485
- const rect = object(context.rect);
486
- if (target.kind !== "widget" && target.kind !== "element" && target.kind !== "app") throw new Error("Invalid annotation target.");
487
- let screenshot;
488
- if (context.screenshot !== void 0) {
489
- const image = object(context.screenshot);
490
- const dataUrl = text(image.dataUrl, 35e4, true);
491
- if (image.mimeType !== "image/png" || !/^data:image\/png;base64,iVBORw0KGgo[A-Za-z0-9+/]*={0,2}$/.test(dataUrl)) throw new Error("Invalid annotation screenshot.");
492
- const width = number(image.width, true);
493
- const height = number(image.height, true);
494
- if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || width > 1024 || height > 1024) throw new Error("Invalid screenshot dimensions.");
495
- const encoded = dataUrl.slice(22);
496
- if (encoded.length % 4 !== 0 || encoded.length / 4 * 3 - (encoded.endsWith("==") ? 2 : encoded.endsWith("=") ? 1 : 0) > 262144) throw new Error("Screenshot is too large.");
497
- const header = atob(encoded.slice(0, 44));
498
- if (header.length < 24 || header.slice(12, 16) !== "IHDR") throw new Error("Invalid PNG header.");
499
- const bytes = Uint8Array.from(header, (character) => character.charCodeAt(0));
500
- const dimensions = new DataView(bytes.buffer);
501
- if (dimensions.getUint32(16) !== width || dimensions.getUint32(20) !== height) throw new Error("Screenshot dimensions do not match the image.");
502
- screenshot = {
503
- mimeType: "image/png",
504
- dataUrl,
505
- width,
506
- height
507
- };
508
- }
509
- let anchor;
510
- if (context.anchor !== void 0) {
511
- const point = object(context.anchor);
512
- const x = number(point.x, true);
513
- const y = number(point.y, true);
514
- if (x > 1 || y > 1) throw new Error("Invalid annotation anchor.");
515
- anchor = {
516
- x,
517
- y
518
- };
519
- }
520
- let cursor;
521
- if (context.cursor !== void 0) {
522
- const point = object(context.cursor);
523
- cursor = {
524
- x: number(point.x),
525
- y: number(point.y)
526
- };
527
- }
528
- let region;
529
- if (context.region !== void 0) {
530
- const area = object(context.region);
531
- region = {
532
- x: number(area.x, true),
533
- y: number(area.y, true),
534
- width: number(area.width, true),
535
- height: number(area.height, true)
536
- };
537
- if (target.kind !== "app" || region.width <= 0 || region.height <= 0 || region.x + region.width > 1.000001 || region.y + region.height > 1.000001) throw new Error("Invalid annotation selection.");
538
- }
539
- const draft = {
540
- id: text(input.id, 128, true),
541
- target: {
542
- id: text(target.id, 128, true),
543
- label: text(target.label, 256, true),
544
- kind: target.kind,
545
- text: text(target.text, 1024),
546
- queryNames: names(target.queryNames),
547
- glossaryIds: names(target.glossaryIds)
548
- },
549
- context: {
550
- ...anchor ? { anchor } : {},
551
- ...region ? { region } : {},
552
- ...cursor ? { cursor } : {},
553
- ...screenshot ? { screenshot } : {},
554
- search: text(context.search, 2048),
555
- hash: text(context.hash, 1024),
556
- viewport: {
557
- width: number(viewport.width, true),
558
- height: number(viewport.height, true)
559
- },
560
- rect: {
561
- x: number(rect.x),
562
- y: number(rect.y),
563
- width: number(rect.width, true),
564
- height: number(rect.height, true)
565
- },
566
- ...context.view === void 0 ? {} : { view: text(context.view, 128) },
567
- ...context.displayedInput === void 0 ? {} : { displayedInput: context.displayedInput }
568
- },
569
- comment: text(input.comment, 2e3, true).trim()
570
- };
571
- const json = JSON.stringify({
572
- ...draft,
573
- context: {
574
- ...draft.context,
575
- screenshot: void 0
576
- }
577
- });
578
- if (new TextEncoder().encode(json).byteLength > 12e3) throw new Error("Annotation is too large.");
579
- return JSON.parse(JSON.stringify(draft));
580
- }
581
- const annotationDraftRoute = /* @__PURE__ */ defineMessageRoute({
582
- input: parseDataAppAnnotationDraft,
583
- output(value) {
584
- if (value !== null) throw new Error("Invalid annotation response.");
585
- return null;
586
- }
587
- });
588
- const annotationEditorStateRoute = /* @__PURE__ */ defineMessageRoute({
589
- input(value) {
590
- const input = object(value);
591
- if (typeof input.hasUnsavedChanges !== "boolean") throw new Error("Invalid annotation editor state.");
592
- return { hasUnsavedChanges: input.hasUnsavedChanges };
593
- },
594
- output(value) {
595
- if (value !== null) throw new Error("Invalid annotation response.");
596
- return null;
597
- }
598
- });
599
- /** Request host-owned batch submission without transferring ownership of drafts. */
600
- const annotationSendRoute = /* @__PURE__ */ defineMessageRoute({
601
- input(value) {
602
- if (value !== null) throw new Error("Invalid annotation send request.");
603
- return null;
604
- },
605
- output(value) {
606
- if (value !== null) throw new Error("Invalid annotation response.");
607
- return null;
608
- }
609
- });
610
- const annotationUpdateRoute = /* @__PURE__ */ defineMessageRoute({
611
- input(value) {
612
- const input = object(value);
613
- return {
614
- id: text(input.id, 128, true),
615
- comment: text(input.comment, 2e3, true).trim()
616
- };
617
- },
618
- output(value) {
619
- if (value !== null) throw new Error("Invalid annotation response.");
620
- return null;
621
- }
622
- });
623
- /** A controlled shell acknowledges local exits (for example Escape). */
624
- const annotationModeRoute = /* @__PURE__ */ defineMessageRoute({
625
- input(value) {
626
- const input = object(value);
627
- if (typeof input.active !== "boolean") throw new Error("Invalid annotation mode.");
628
- return { active: input.active };
629
- },
630
- output(value) {
631
- if (value !== null) throw new Error("Invalid annotation response.");
632
- return null;
633
- }
634
- });
635
- function isAnnotationPresentation(value) {
636
- if (!value || typeof value !== "object") return false;
637
- const input = value;
638
- return typeof input.enabled === "boolean" && (input.active === void 0 || typeof input.active === "boolean") && (input.pinsVisible === void 0 || typeof input.pinsVisible === "boolean") && (input.showHint === void 0 || typeof input.showHint === "boolean") && (input.readOnly === void 0 || typeof input.readOnly === "boolean") && (input.selectedAnnotationId === void 0 || typeof input.selectedAnnotationId === "string" && input.selectedAnnotationId.length <= 128) && (input.dismissal === void 0 || input.dismissal !== null && typeof input.dismissal === "object" && typeof input.dismissal.id === "string" && input.dismissal.id.length <= 128 && typeof input.dismissal.occurredAt === "number" && Number.isFinite(input.dismissal.occurredAt)) && (input.selectionId === void 0 || typeof input.selectionId === "string" && input.selectionId.length <= 128) && (input.selectedTargetId === void 0 || typeof input.selectedTargetId === "string" && input.selectedTargetId.length <= 128) && (input.targets === void 0 || Array.isArray(input.targets) && input.targets.length <= 50 && input.targets.every((target) => target && typeof target.id === "string" && target.id.length <= 128 && typeof target.targetId === "string" && target.targetId.length <= 128 && Number.isSafeInteger(target.number) && target.number > 0 && (target.anchor === void 0 || typeof target.anchor.x === "number" && Number.isFinite(target.anchor.x) && target.anchor.x >= 0 && target.anchor.x <= 1 && typeof target.anchor.y === "number" && Number.isFinite(target.anchor.y) && target.anchor.y >= 0 && target.anchor.y <= 1) && (target.region === void 0 || typeof target.region === "object" && target.region !== null && [
639
- "x",
640
- "y",
641
- "width",
642
- "height"
643
- ].every((key) => {
644
- const value = target.region[key];
645
- return typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 1;
646
- }) && target.region.width > 0 && target.region.height > 0 && target.region.x + target.region.width <= 1.000001 && target.region.y + target.region.height <= 1.000001) && (target.comment === void 0 || typeof target.comment === "string" && target.comment.length <= 2e3)));
647
- }
648
- //#endregion
649
- //#region src/core/external-link.ts
650
- /** Absolute HTTPS destinations only; URLs must not contain embedded credentials. */
651
- function parseExternalUrl(value) {
652
- if (typeof value !== "string" || value.length > 8192 || /[\s\\\p{Cc}\p{Cf}]/u.test(value)) throw new Error("Expected an absolute HTTPS URL.");
653
- const url = new URL(value);
654
- if (!/^https:\/\//i.test(value) || url.protocol !== "https:" || url.username || url.password) throw new Error("Expected an HTTPS URL without credentials.");
655
- return url.href;
656
- }
657
- /** Shift takes precedence over Ctrl/Cmd; an unmodified event requests no navigation. */
658
- function resolveExternalLinkTarget(input) {
659
- if (input.target !== "event") return input.target ?? "tab";
660
- return input.keys.shiftKey ? "window" : input.keys.metaKey || input.keys.ctrlKey ? "tab" : null;
661
- }
662
- /** Opt-in host route. Validation does not replace the host's destination policy. */
663
- const openExternalLinkRoute = /* @__PURE__ */ defineMessageRoute({
664
- input(value) {
665
- if (!value || typeof value !== "object") throw new Error("Invalid external link.");
666
- const input = value;
667
- const url = parseExternalUrl(input.url);
668
- if (input.target === void 0) return { url };
669
- if (input.target === "tab" || input.target === "window") return {
670
- url,
671
- target: input.target
672
- };
673
- if (input.target === "event" && input.keys && typeof input.keys === "object") {
674
- const keys = input.keys;
675
- if (typeof keys.shiftKey === "boolean" && typeof keys.metaKey === "boolean" && typeof keys.ctrlKey === "boolean") return {
676
- url,
677
- target: "event",
678
- keys: {
679
- shiftKey: keys.shiftKey,
680
- metaKey: keys.metaKey,
681
- ctrlKey: keys.ctrlKey
682
- }
683
- };
684
- }
685
- throw new Error("Invalid external navigation target.");
686
- },
687
- output(value) {
688
- if (value !== "opened" && value !== "cancelled" && value !== "ignored") throw new Error("Invalid external link response.");
689
- return value;
690
- }
691
- });
692
- //#endregion
693
- //#region src/core/contract.ts
694
- const sourceErrorBrand = Symbol.for("@altertable/data-app/DataSourceError");
695
- var DataSourceError = class extends Error {
696
- reason;
697
- status;
698
- [sourceErrorBrand] = true;
699
- queryName;
700
- static [Symbol.hasInstance](value) {
701
- return value instanceof Error && value[sourceErrorBrand] === true;
702
- }
703
- constructor(reason, status, options) {
704
- super(`Data source ${reason}${status ? ` (${status})` : ""}.`, options);
705
- this.reason = reason;
706
- this.status = status;
707
- this.name = "DataSourceError";
708
- }
709
- };
710
- //#endregion
711
- export { numberFilter as C, parseNumberSelection as E, booleanFilter as S, parseBooleanSelection as T, navigationUpdateRoute as _, annotationDraftRoute as a, dateTimeRangePresets as b, annotationSendRoute as c, parseDataAppAnnotationDraft as d, MessageRoutingError as f, defineMessageRoute as g, defineDataQueryRoute as h, resolveExternalLinkTarget as i, annotationUpdateRoute as l, dataAppRoutes as m, openExternalLinkRoute as n, annotationEditorStateRoute as o, createMessageRouter as p, parseExternalUrl as r, annotationModeRoute as s, DataSourceError as t, isAnnotationPresentation as u, sqlQueryRoute as v, numberOperatorLabels as w, dateTimeRangeVariable as x, validLocation as y };
712
-
713
- //# sourceMappingURL=contract-kNFYPHRK.js.map