@malloy-publisher/server 0.0.203 → 0.0.205
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/build.ts +10 -1
- package/dist/app/api-doc.yaml +146 -0
- package/dist/app/assets/{EnvironmentPage-BVQ7glKP.js → EnvironmentPage-CAge6UHD.js} +1 -1
- package/dist/app/assets/HomePage-DhTe8qpa.js +1 -0
- package/dist/app/assets/{MainPage-bYOWcgDP.js → MainPage-CeTxxGex.js} +2 -2
- package/dist/app/assets/MaterializationsPage-CpDHB70t.js +1 -0
- package/dist/app/assets/ModelPage-D9sSMb75.js +1 -0
- package/dist/app/assets/PackagePage-LRqQWrFY.js +1 -0
- package/dist/app/assets/{RouteError-_J-EBz7W.js → RouteError-xT6kuCNw.js} +1 -1
- package/dist/app/assets/{WorkbookPage-Bjs9Nm-_.js → WorkbookPage-DsIh9svZ.js} +1 -1
- package/dist/app/assets/{core-BPLlx5VM.es-C2ARtwWI.js → core-C2sQrwVu.es-Bjem0hym.js} +1 -1
- package/dist/app/assets/{index-CqUWJELr.js → index-BdOZDcce.js} +2 -2
- package/dist/app/assets/index-DHHAcY5o.js +1812 -0
- package/dist/app/assets/index-RX3QOTde.js +455 -0
- package/dist/app/assets/index.umd-D2WH3D-f.js +2469 -0
- package/dist/app/index.html +1 -1
- package/dist/package_load_worker.mjs +392 -67
- package/dist/runtime/publisher.js +318 -0
- package/dist/server.mjs +982 -346
- package/package.json +15 -14
- package/scripts/bake-duckdb-extensions.js +104 -0
- package/src/controller/watch-mode.controller.ts +176 -46
- package/src/ducklake_version.spec.ts +43 -0
- package/src/ducklake_version.ts +26 -0
- package/src/errors.spec.ts +21 -0
- package/src/errors.ts +18 -1
- package/src/mcp/error_messages.spec.ts +35 -0
- package/src/mcp/error_messages.ts +14 -1
- package/src/mcp/handler_utils.ts +12 -0
- package/src/package_load/package_load_pool.ts +0 -5
- package/src/package_load/package_load_worker.ts +41 -99
- package/src/package_load/protocol.ts +1 -7
- package/src/runtime/publisher.js +318 -0
- package/src/server.ts +479 -2
- package/src/service/annotations.spec.ts +118 -0
- package/src/service/annotations.ts +91 -0
- package/src/service/authorize.spec.ts +132 -0
- package/src/service/authorize.ts +241 -0
- package/src/service/authorize_integration.spec.ts +932 -0
- package/src/service/compile_authorize.spec.ts +85 -0
- package/src/service/connection.ts +1 -1
- package/src/service/environment.ts +67 -9
- package/src/service/environment_store.ts +142 -11
- package/src/service/filter.spec.ts +14 -3
- package/src/service/filter.ts +5 -1
- package/src/service/filter_bypass.spec.ts +418 -0
- package/src/service/given.ts +37 -12
- package/src/service/givens_integration.spec.ts +34 -7
- package/src/service/materialization_service.ts +25 -20
- package/src/service/materialized_table_gc.spec.ts +6 -5
- package/src/service/materialized_table_gc.ts +2 -50
- package/src/service/model.spec.ts +203 -8
- package/src/service/model.ts +349 -155
- package/src/service/package.ts +17 -6
- package/src/service/package_worker_path.spec.ts +113 -0
- package/src/service/quoting.ts +0 -20
- package/src/service/restricted_mode.spec.ts +299 -0
- package/src/service/source_extraction.ts +226 -0
- package/src/storage/StorageManager.ts +73 -0
- package/src/storage/duckdb/DuckDBConnection.ts +70 -124
- package/tests/fixtures/authorize-compile/model.malloy +9 -0
- package/tests/fixtures/authorize-compile/publisher.json +4 -0
- package/tests/fixtures/html-pages-nopublic/model.malloy +1 -0
- package/tests/fixtures/html-pages-nopublic/publisher.json +5 -0
- package/tests/fixtures/html-pages-test/data.csv +3 -0
- package/tests/fixtures/html-pages-test/public/assets/app.css +3 -0
- package/tests/fixtures/html-pages-test/public/data.json +1 -0
- package/tests/fixtures/html-pages-test/public/index.html +9 -0
- package/tests/fixtures/html-pages-test/public/sub/page2.html +9 -0
- package/tests/fixtures/html-pages-test/publisher.json +5 -0
- package/tests/fixtures/html-pages-test/report.malloy +1 -0
- package/tests/integration/authorize/compile_authorize_http.integration.spec.ts +92 -0
- package/tests/integration/duckdb_storage/duckdb_storage.integration.spec.ts +138 -0
- package/tests/integration/html_pages/html_pages.integration.spec.ts +378 -0
- package/tests/integration/watch-mode/watch_mode.integration.spec.ts +421 -0
- package/tests/unit/duckdb/attached_databases.test.ts +111 -0
- package/tests/unit/duckdb/duckdb_connection.test.ts +181 -0
- package/tests/unit/duckdb/repositories.test.ts +208 -0
- package/dist/app/assets/HomePage-D9drXoZX.js +0 -1
- package/dist/app/assets/ModelPage-DT0gjNy1.js +0 -1
- package/dist/app/assets/PackagePage-N1ZBNJul.js +0 -1
- package/dist/app/assets/index-BeNwIeYQ.js +0 -454
- package/dist/app/assets/index-Dx7qi2LO.js +0 -1803
- package/dist/app/assets/index.umd-BXm2lnUO.js +0 -1145
|
@@ -67,7 +67,6 @@
|
|
|
67
67
|
* it resolves.
|
|
68
68
|
*/
|
|
69
69
|
import type {
|
|
70
|
-
Annotation,
|
|
71
70
|
FetchSchemaOptions,
|
|
72
71
|
InfoConnection,
|
|
73
72
|
LookupConnection,
|
|
@@ -811,15 +810,11 @@ export class PackageLoadPool {
|
|
|
811
810
|
|
|
812
811
|
function buildFetchOptions(options: {
|
|
813
812
|
refreshTimestamp?: number;
|
|
814
|
-
modelAnnotation?: Annotation;
|
|
815
813
|
}): FetchSchemaOptions {
|
|
816
814
|
const out: FetchSchemaOptions = {};
|
|
817
815
|
if (options.refreshTimestamp !== undefined) {
|
|
818
816
|
out.refreshTimestamp = options.refreshTimestamp;
|
|
819
817
|
}
|
|
820
|
-
if (options.modelAnnotation !== undefined) {
|
|
821
|
-
out.modelAnnotation = options.modelAnnotation;
|
|
822
|
-
}
|
|
823
818
|
return out;
|
|
824
819
|
}
|
|
825
820
|
|
|
@@ -46,7 +46,6 @@
|
|
|
46
46
|
*/
|
|
47
47
|
import {
|
|
48
48
|
contextOverlay,
|
|
49
|
-
type Annotation,
|
|
50
49
|
type BuildManifestEntry,
|
|
51
50
|
type Connection,
|
|
52
51
|
type FetchSchemaOptions,
|
|
@@ -56,16 +55,12 @@ import {
|
|
|
56
55
|
type ModelDef,
|
|
57
56
|
type ModelMaterializer,
|
|
58
57
|
modelDefToModelInfo,
|
|
59
|
-
type NamedModelObject,
|
|
60
58
|
type NamedQueryDef,
|
|
61
59
|
type Query,
|
|
62
60
|
Runtime,
|
|
63
61
|
type SQLSourceDef,
|
|
64
62
|
type SQLSourceRequest,
|
|
65
|
-
type StructDef,
|
|
66
63
|
type TableSourceDef,
|
|
67
|
-
type TurtleDef,
|
|
68
|
-
isSourceDef,
|
|
69
64
|
} from "@malloydata/malloy";
|
|
70
65
|
import * as Malloy from "@malloydata/malloy-interfaces";
|
|
71
66
|
import {
|
|
@@ -84,7 +79,13 @@ import {
|
|
|
84
79
|
PACKAGE_MANIFEST_NAME,
|
|
85
80
|
} from "../constants";
|
|
86
81
|
import { HackyDataStylesAccumulator } from "../data_styles";
|
|
87
|
-
import {
|
|
82
|
+
import { ModelCompilationError } from "../errors";
|
|
83
|
+
import { validateAuthorizeProbes } from "../service/authorize";
|
|
84
|
+
import { type FilterDefinition } from "../service/filter";
|
|
85
|
+
import {
|
|
86
|
+
extractQueriesFromModelDef,
|
|
87
|
+
extractSourcesFromModelDef,
|
|
88
|
+
} from "../service/source_extraction";
|
|
88
89
|
import {
|
|
89
90
|
malloyGivenToApi,
|
|
90
91
|
type MalloyGiven,
|
|
@@ -271,15 +272,13 @@ class ProxyConnection {
|
|
|
271
272
|
|
|
272
273
|
function serializeFetchOptions(options: FetchSchemaOptions): {
|
|
273
274
|
refreshTimestamp?: number;
|
|
274
|
-
modelAnnotation?: Annotation;
|
|
275
275
|
} {
|
|
276
|
-
const out: {
|
|
276
|
+
const out: {
|
|
277
|
+
refreshTimestamp?: number;
|
|
278
|
+
} = {};
|
|
277
279
|
if (options.refreshTimestamp !== undefined) {
|
|
278
280
|
out.refreshTimestamp = options.refreshTimestamp;
|
|
279
281
|
}
|
|
280
|
-
if (options.modelAnnotation !== undefined) {
|
|
281
|
-
out.modelAnnotation = options.modelAnnotation;
|
|
282
|
-
}
|
|
283
282
|
return out;
|
|
284
283
|
}
|
|
285
284
|
|
|
@@ -411,6 +410,7 @@ interface ApiSourceWire {
|
|
|
411
410
|
views?: { name: string; annotations?: string[] }[];
|
|
412
411
|
filters?: unknown[];
|
|
413
412
|
givens?: unknown[];
|
|
413
|
+
authorize?: string[];
|
|
414
414
|
}
|
|
415
415
|
interface ApiQueryWire {
|
|
416
416
|
name: string;
|
|
@@ -472,96 +472,20 @@ function appendLocalSourceInfos(
|
|
|
472
472
|
}
|
|
473
473
|
}
|
|
474
474
|
|
|
475
|
+
// Source / query introspection is shared with the in-process path; see
|
|
476
|
+
// service/source_extraction.ts. The worker has no logger, so a filter parse
|
|
477
|
+
// failure is swallowed silently (no `onParseError` callback) — matching the
|
|
478
|
+
// prior worker-local behavior.
|
|
475
479
|
function extractSources(
|
|
476
|
-
modelPath: string,
|
|
477
480
|
modelDef: ModelDef,
|
|
478
481
|
givens: ApiGivenWire[] | undefined,
|
|
479
482
|
): { sources: ApiSourceWire[]; filterMap: Map<string, FilterDefinition[]> } {
|
|
480
|
-
const filterMap =
|
|
481
|
-
|
|
482
|
-
.filter((obj) => isSourceDef(obj))
|
|
483
|
-
.map((sourceObj) => {
|
|
484
|
-
const sourceName =
|
|
485
|
-
(sourceObj as StructDef).as || (sourceObj as StructDef).name;
|
|
486
|
-
const annotations = (sourceObj as StructDef).annotation?.blockNotes
|
|
487
|
-
?.filter((note) => note.at.url.includes(modelPath))
|
|
488
|
-
.map((note) => note.text);
|
|
489
|
-
|
|
490
|
-
const collected: string[][] = [];
|
|
491
|
-
let cur: Annotation | undefined = (sourceObj as StructDef).annotation;
|
|
492
|
-
while (cur) {
|
|
493
|
-
if (cur.blockNotes) {
|
|
494
|
-
collected.push(cur.blockNotes.map((note) => note.text));
|
|
495
|
-
}
|
|
496
|
-
cur = cur.inherits;
|
|
497
|
-
}
|
|
498
|
-
const allAnnotations = collected.reverse().flat();
|
|
499
|
-
let filters: unknown[] | undefined;
|
|
500
|
-
if (allAnnotations.length > 0) {
|
|
501
|
-
try {
|
|
502
|
-
const parsed = parseFilters(allAnnotations);
|
|
503
|
-
if (parsed.length > 0) {
|
|
504
|
-
filterMap.set(sourceName, parsed);
|
|
505
|
-
const fields = (sourceObj as StructDef).fields;
|
|
506
|
-
filters = parsed.map((f) => {
|
|
507
|
-
const field = fields.find(
|
|
508
|
-
(fd) => (fd.as || fd.name) === f.dimension,
|
|
509
|
-
);
|
|
510
|
-
return {
|
|
511
|
-
name: f.name,
|
|
512
|
-
dimension: f.dimension,
|
|
513
|
-
type: f.type,
|
|
514
|
-
implicit: f.implicit,
|
|
515
|
-
required: f.required,
|
|
516
|
-
dimensionType: field?.type as string | undefined,
|
|
517
|
-
};
|
|
518
|
-
});
|
|
519
|
-
}
|
|
520
|
-
} catch {
|
|
521
|
-
/* parse errors are warnings; matches in-process */
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
const views = (sourceObj as StructDef).fields
|
|
526
|
-
.filter((f) => f.type === "turtle")
|
|
527
|
-
.filter((turtle) =>
|
|
528
|
-
(turtle as TurtleDef).pipeline
|
|
529
|
-
.map((stage) => stage.type)
|
|
530
|
-
.every((type) => type === "reduce"),
|
|
531
|
-
)
|
|
532
|
-
.map((turtle) => ({
|
|
533
|
-
name: turtle.as || turtle.name,
|
|
534
|
-
annotations: turtle?.annotation?.blockNotes
|
|
535
|
-
?.filter((note) => note.at.url.includes(modelPath))
|
|
536
|
-
.map((note) => note.text),
|
|
537
|
-
}));
|
|
538
|
-
|
|
539
|
-
return {
|
|
540
|
-
name: sourceName,
|
|
541
|
-
annotations,
|
|
542
|
-
views,
|
|
543
|
-
filters,
|
|
544
|
-
givens,
|
|
545
|
-
} as ApiSourceWire;
|
|
546
|
-
});
|
|
547
|
-
|
|
548
|
-
return { sources, filterMap };
|
|
483
|
+
const { sources, filterMap } = extractSourcesFromModelDef(modelDef, givens);
|
|
484
|
+
return { sources: sources as unknown as ApiSourceWire[], filterMap };
|
|
549
485
|
}
|
|
550
486
|
|
|
551
|
-
function extractQueries(
|
|
552
|
-
|
|
553
|
-
obj.type === "query";
|
|
554
|
-
return Object.values(modelDef.contents)
|
|
555
|
-
.filter(isNamedQuery)
|
|
556
|
-
.map((q) => ({
|
|
557
|
-
name: q.as || q.name,
|
|
558
|
-
sourceName: typeof q.structRef === "string" ? q.structRef : undefined,
|
|
559
|
-
annotations: q?.annotation?.blockNotes
|
|
560
|
-
?.filter((note: { at: { url: string } }) =>
|
|
561
|
-
note.at.url.includes(modelPath),
|
|
562
|
-
)
|
|
563
|
-
.map((note: { text: string }) => note.text),
|
|
564
|
-
}));
|
|
487
|
+
function extractQueries(modelDef: ModelDef): ApiQueryWire[] {
|
|
488
|
+
return extractQueriesFromModelDef(modelDef) as ApiQueryWire[];
|
|
565
489
|
}
|
|
566
490
|
|
|
567
491
|
function buildRuntimeForModel(
|
|
@@ -621,8 +545,12 @@ async function compileMalloyModel(
|
|
|
621
545
|
);
|
|
622
546
|
appendLocalSourceInfos(modelDef, sourceInfos, importedNames);
|
|
623
547
|
|
|
624
|
-
const { sources, filterMap } = extractSources(
|
|
625
|
-
const queries = extractQueries(
|
|
548
|
+
const { sources, filterMap } = extractSources(modelDef, givens);
|
|
549
|
+
const queries = extractQueries(modelDef);
|
|
550
|
+
// Validate #(authorize) at compile time (shared with Model.create). Throws
|
|
551
|
+
// on an unknown given / source-field reference; compileOneModel's catch
|
|
552
|
+
// turns it into this model's compilationError.
|
|
553
|
+
await validateAuthorizeProbes(mm, sources);
|
|
626
554
|
|
|
627
555
|
return {
|
|
628
556
|
modelPath,
|
|
@@ -798,10 +726,12 @@ async function compileNotebookModel(
|
|
|
798
726
|
collected.importedNames,
|
|
799
727
|
);
|
|
800
728
|
finalSourceInfos = collected.sourceInfos;
|
|
801
|
-
const extracted = extractSources(
|
|
729
|
+
const extracted = extractSources(finalModelDef, finalGivens);
|
|
802
730
|
finalSources = extracted.sources;
|
|
803
731
|
finalFilterMap = extracted.filterMap;
|
|
804
|
-
finalQueries = extractQueries(
|
|
732
|
+
finalQueries = extractQueries(finalModelDef);
|
|
733
|
+
// Validate #(authorize) at compile time (shared with Model.create).
|
|
734
|
+
await validateAuthorizeProbes(mm, finalSources);
|
|
805
735
|
}
|
|
806
736
|
|
|
807
737
|
return {
|
|
@@ -899,6 +829,18 @@ function serializeError(error: unknown): SerializedError {
|
|
|
899
829
|
isCompilationError: true,
|
|
900
830
|
};
|
|
901
831
|
}
|
|
832
|
+
// ModelCompilationError (e.g. an invalid #(authorize) annotation caught by
|
|
833
|
+
// validateAuthorizeProbes) carries no Malloy `problems`, but it must keep its
|
|
834
|
+
// compilation-error classification across the worker boundary so the main
|
|
835
|
+
// thread re-wraps it as a 424, not a generic 500.
|
|
836
|
+
if (error instanceof ModelCompilationError) {
|
|
837
|
+
return {
|
|
838
|
+
name: error.name,
|
|
839
|
+
message: error.message,
|
|
840
|
+
stack: error.stack,
|
|
841
|
+
isCompilationError: true,
|
|
842
|
+
};
|
|
843
|
+
}
|
|
902
844
|
if (error instanceof Error) {
|
|
903
845
|
return {
|
|
904
846
|
name: error.name,
|
|
@@ -64,11 +64,7 @@
|
|
|
64
64
|
* cheaper than `JSON.stringify` for the multi-MB modelDef payloads.
|
|
65
65
|
*/
|
|
66
66
|
|
|
67
|
-
import type {
|
|
68
|
-
Annotation,
|
|
69
|
-
SQLSourceDef,
|
|
70
|
-
TableSourceDef,
|
|
71
|
-
} from "@malloydata/malloy";
|
|
67
|
+
import type { SQLSourceDef, TableSourceDef } from "@malloydata/malloy";
|
|
72
68
|
|
|
73
69
|
// ──────────────────────────────────────────────────────────────────────
|
|
74
70
|
// Direction: main ──▶ worker (load-package job)
|
|
@@ -236,7 +232,6 @@ export interface SchemaForTablesRequest {
|
|
|
236
232
|
tables: Record<string, string>;
|
|
237
233
|
options: {
|
|
238
234
|
refreshTimestamp?: number;
|
|
239
|
-
modelAnnotation?: Annotation;
|
|
240
235
|
};
|
|
241
236
|
}
|
|
242
237
|
|
|
@@ -256,7 +251,6 @@ export interface SchemaForSqlRequest {
|
|
|
256
251
|
sentence: unknown;
|
|
257
252
|
options: {
|
|
258
253
|
refreshTimestamp?: number;
|
|
259
|
-
modelAnnotation?: Annotation;
|
|
260
254
|
};
|
|
261
255
|
}
|
|
262
256
|
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// Publisher runtime helper for in-package HTML dashboards.
|
|
2
|
+
// Served by the Publisher server at /sdk/publisher.js. Hand-authored vanilla
|
|
3
|
+
// JS — no bundler. Loaded via <script src="/sdk/publisher.js">.
|
|
4
|
+
//
|
|
5
|
+
// Exposes window.Publisher with:
|
|
6
|
+
// - Publisher.query(model, malloy, opts?) → Promise<rows[]>
|
|
7
|
+
// - Publisher.queryFull(model, malloy, opts?) → Promise<MalloyResult> (envelope for <malloy-render>)
|
|
8
|
+
// - Publisher.embed(selector, { src, height?, token? })
|
|
9
|
+
// - Publisher.context ({ environment, package } inferred from URL)
|
|
10
|
+
// - Publisher.setToken(token) (override Bearer token; default uses cookies)
|
|
11
|
+
//
|
|
12
|
+
// When loaded inside an iframe served from /environments/<env>/packages/<pkg>/...,
|
|
13
|
+
// the runtime auto-subscribes to a Server-Sent Events live-reload stream
|
|
14
|
+
// (GET .../events) and reloads the page on file changes. It also posts size
|
|
15
|
+
// updates to the parent window so Publisher.embed() in the host can resize
|
|
16
|
+
// the iframe.
|
|
17
|
+
//
|
|
18
|
+
// The "publisher:resize" postMessage protocol below is the SAME contract the
|
|
19
|
+
// SPA host consumes. Its canonical definition lives in
|
|
20
|
+
// packages/sdk/src/utils/pageEmbed.ts (PUBLISHER_RESIZE_MESSAGE_TYPE /
|
|
21
|
+
// PublisherResizeMessage). This file is build-step-free vanilla JS and can't
|
|
22
|
+
// import it, so keep the message type/shape here in sync with that module.
|
|
23
|
+
|
|
24
|
+
(function () {
|
|
25
|
+
"use strict";
|
|
26
|
+
|
|
27
|
+
// --- Context inference -------------------------------------------------
|
|
28
|
+
// URL shape: /environments/<env>/packages/<pkg>/<file>
|
|
29
|
+
//
|
|
30
|
+
// location.pathname is URL-encoded, so we MUST decode the captured
|
|
31
|
+
// segments here. Without this step, a name with a space (e.g.
|
|
32
|
+
// "demo env") would arrive as "demo%20env" — and the encodeURIComponent
|
|
33
|
+
// we apply when building API URLs (below) would produce "demo%2520env",
|
|
34
|
+
// which Publisher then 404s on.
|
|
35
|
+
var pathMatch = location.pathname.match(
|
|
36
|
+
/^\/environments\/([^/]+)\/packages\/([^/]+)\//,
|
|
37
|
+
);
|
|
38
|
+
function safeDecode(s) {
|
|
39
|
+
try {
|
|
40
|
+
return decodeURIComponent(s);
|
|
41
|
+
} catch (_e) {
|
|
42
|
+
return s;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
var ctx = pathMatch
|
|
46
|
+
? {
|
|
47
|
+
environment: safeDecode(pathMatch[1]),
|
|
48
|
+
package: safeDecode(pathMatch[2]),
|
|
49
|
+
}
|
|
50
|
+
: {};
|
|
51
|
+
|
|
52
|
+
var apiBase = location.origin + "/api/v0";
|
|
53
|
+
var bearerToken = null;
|
|
54
|
+
|
|
55
|
+
function authHeaders() {
|
|
56
|
+
return bearerToken ? { Authorization: "Bearer " + bearerToken } : {};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// --- Query helpers -----------------------------------------------------
|
|
60
|
+
function resolveTarget(opts) {
|
|
61
|
+
var env = (opts && opts.environment) || ctx.environment;
|
|
62
|
+
var pkg = (opts && opts.package) || ctx.package;
|
|
63
|
+
if (!env || !pkg) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
"Publisher: no environment/package; either serve the page from " +
|
|
66
|
+
"/environments/<env>/packages/<pkg>/... or pass { environment, package } in opts.",
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return { env: env, pkg: pkg };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function rawQuery(modelPath, malloyQuery, opts, compactJson) {
|
|
73
|
+
opts = opts || {};
|
|
74
|
+
var target = resolveTarget(opts);
|
|
75
|
+
var url =
|
|
76
|
+
apiBase +
|
|
77
|
+
"/environments/" +
|
|
78
|
+
encodeURIComponent(target.env) +
|
|
79
|
+
"/packages/" +
|
|
80
|
+
encodeURIComponent(target.pkg) +
|
|
81
|
+
"/models/" +
|
|
82
|
+
modelPath.split("/").map(encodeURIComponent).join("/") +
|
|
83
|
+
"/query";
|
|
84
|
+
var body = { compactJson: compactJson };
|
|
85
|
+
if (malloyQuery) body.query = malloyQuery;
|
|
86
|
+
if (opts.sourceName) body.sourceName = opts.sourceName;
|
|
87
|
+
if (opts.queryName) body.queryName = opts.queryName;
|
|
88
|
+
if (opts.filterParams) body.filterParams = opts.filterParams;
|
|
89
|
+
if (opts.bypassFilters) body.bypassFilters = true;
|
|
90
|
+
|
|
91
|
+
var headers = Object.assign(
|
|
92
|
+
{ "content-type": "application/json" },
|
|
93
|
+
authHeaders(),
|
|
94
|
+
);
|
|
95
|
+
var res = await fetch(url, {
|
|
96
|
+
method: "POST",
|
|
97
|
+
credentials: "include",
|
|
98
|
+
headers: headers,
|
|
99
|
+
body: JSON.stringify(body),
|
|
100
|
+
});
|
|
101
|
+
var json;
|
|
102
|
+
try {
|
|
103
|
+
json = await res.json();
|
|
104
|
+
} catch (_e) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
"Publisher: server returned non-JSON response (" + res.status + ")",
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
if (!res.ok) {
|
|
110
|
+
var msg = (json && json.message) || res.statusText || "Query failed";
|
|
111
|
+
var err = new Error("Publisher.query: " + msg);
|
|
112
|
+
err.response = json;
|
|
113
|
+
err.status = res.status;
|
|
114
|
+
throw err;
|
|
115
|
+
}
|
|
116
|
+
// The server's QueryResult always has `result` as a JSON-encoded string.
|
|
117
|
+
// Parse it before handing it back so callers see real JS values.
|
|
118
|
+
return JSON.parse(json.result);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function query(modelPath, malloyQuery, opts) {
|
|
122
|
+
return rawQuery(modelPath, malloyQuery, opts, true);
|
|
123
|
+
}
|
|
124
|
+
function queryFull(modelPath, malloyQuery, opts) {
|
|
125
|
+
return rawQuery(modelPath, malloyQuery, opts, false);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// --- Embed helper (host page) -----------------------------------------
|
|
129
|
+
function embed(selector, options) {
|
|
130
|
+
options = options || {};
|
|
131
|
+
var host =
|
|
132
|
+
typeof selector === "string"
|
|
133
|
+
? document.querySelector(selector)
|
|
134
|
+
: selector;
|
|
135
|
+
if (!host) {
|
|
136
|
+
throw new Error("Publisher.embed: selector did not match an element");
|
|
137
|
+
}
|
|
138
|
+
if (!options.src) {
|
|
139
|
+
throw new Error("Publisher.embed: opts.src is required");
|
|
140
|
+
}
|
|
141
|
+
var iframe = document.createElement("iframe");
|
|
142
|
+
iframe.src = options.token
|
|
143
|
+
? options.src +
|
|
144
|
+
(options.src.indexOf("?") === -1 ? "?" : "&") +
|
|
145
|
+
"embed_token=" +
|
|
146
|
+
encodeURIComponent(options.token)
|
|
147
|
+
: options.src;
|
|
148
|
+
iframe.style.border = "0";
|
|
149
|
+
iframe.style.width = "100%";
|
|
150
|
+
iframe.style.display = "block";
|
|
151
|
+
if (options.height) {
|
|
152
|
+
iframe.style.height =
|
|
153
|
+
typeof options.height === "number"
|
|
154
|
+
? options.height + "px"
|
|
155
|
+
: options.height;
|
|
156
|
+
} else {
|
|
157
|
+
iframe.style.height = "0px"; // will be sized via postMessage
|
|
158
|
+
}
|
|
159
|
+
if (options.allow) iframe.allow = options.allow;
|
|
160
|
+
iframe.setAttribute(
|
|
161
|
+
"sandbox",
|
|
162
|
+
"allow-scripts allow-same-origin allow-forms",
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
// Resize listener
|
|
166
|
+
function onMessage(e) {
|
|
167
|
+
if (!e.data || e.data.type !== "publisher:resize") return;
|
|
168
|
+
if (e.source !== iframe.contentWindow) return;
|
|
169
|
+
if (typeof e.data.height === "number") {
|
|
170
|
+
iframe.style.height = Math.max(0, e.data.height) + "px";
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
window.addEventListener("message", onMessage);
|
|
174
|
+
// Best-effort cleanup if the host removes the iframe
|
|
175
|
+
var observer = new MutationObserver(function () {
|
|
176
|
+
if (!host.contains(iframe)) {
|
|
177
|
+
window.removeEventListener("message", onMessage);
|
|
178
|
+
observer.disconnect();
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
observer.observe(host, { childList: true, subtree: false });
|
|
182
|
+
|
|
183
|
+
host.appendChild(iframe);
|
|
184
|
+
return {
|
|
185
|
+
iframe: iframe,
|
|
186
|
+
destroy: function () {
|
|
187
|
+
window.removeEventListener("message", onMessage);
|
|
188
|
+
observer.disconnect();
|
|
189
|
+
if (iframe.parentNode) iframe.parentNode.removeChild(iframe);
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// --- When this runtime is itself inside an iframe ---------------------
|
|
195
|
+
// Post size updates upstream + listen for live-reload SSE events.
|
|
196
|
+
function setUpEmbeddedSelfBehaviors() {
|
|
197
|
+
var inIframe = (function () {
|
|
198
|
+
try {
|
|
199
|
+
return window.self !== window.top;
|
|
200
|
+
} catch (_e) {
|
|
201
|
+
return true; // cross-origin parent — assume embedded
|
|
202
|
+
}
|
|
203
|
+
})();
|
|
204
|
+
|
|
205
|
+
if (inIframe) {
|
|
206
|
+
var lastHeight = -1;
|
|
207
|
+
function measureContentHeight() {
|
|
208
|
+
// We want the "ink height" — where the last piece of visible
|
|
209
|
+
// content ends. NOT document.body.scrollHeight: any rule like
|
|
210
|
+
// `body { min-height: 100vh }` (extremely common in dashboards
|
|
211
|
+
// that look nice standalone) inflates scrollHeight to match
|
|
212
|
+
// whatever the iframe's current viewport is, creating a
|
|
213
|
+
// feedback loop where the iframe ratchets up but never shrinks.
|
|
214
|
+
//
|
|
215
|
+
// Sum the lowest bottom edge across body's children, in
|
|
216
|
+
// document coordinates. This ignores body padding, min-height,
|
|
217
|
+
// and CSS that just fills the viewport.
|
|
218
|
+
var body = document.body;
|
|
219
|
+
if (!body) return document.documentElement.scrollHeight;
|
|
220
|
+
var maxBottom = 0;
|
|
221
|
+
var kids = body.children;
|
|
222
|
+
for (var i = 0; i < kids.length; i++) {
|
|
223
|
+
var rect = kids[i].getBoundingClientRect();
|
|
224
|
+
if (rect.bottom > maxBottom) maxBottom = rect.bottom;
|
|
225
|
+
}
|
|
226
|
+
if (maxBottom <= 0) {
|
|
227
|
+
// Fallback for empty body / hidden children
|
|
228
|
+
return document.documentElement.scrollHeight;
|
|
229
|
+
}
|
|
230
|
+
var scrollTop =
|
|
231
|
+
window.scrollY ||
|
|
232
|
+
document.documentElement.scrollTop ||
|
|
233
|
+
document.body.scrollTop ||
|
|
234
|
+
0;
|
|
235
|
+
// Add body bottom padding (rect.bottom is content-box bottom,
|
|
236
|
+
// body padding isn't part of any child's rect).
|
|
237
|
+
var bodyStyle = window.getComputedStyle(body);
|
|
238
|
+
var pad = parseFloat(bodyStyle.paddingBottom) || 0;
|
|
239
|
+
return Math.ceil(maxBottom + scrollTop + pad);
|
|
240
|
+
}
|
|
241
|
+
function postSize() {
|
|
242
|
+
var h = measureContentHeight();
|
|
243
|
+
if (h !== lastHeight) {
|
|
244
|
+
lastHeight = h;
|
|
245
|
+
try {
|
|
246
|
+
window.parent.postMessage(
|
|
247
|
+
{ type: "publisher:resize", height: h },
|
|
248
|
+
"*",
|
|
249
|
+
);
|
|
250
|
+
} catch (_e) {
|
|
251
|
+
/* ignore */
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
// Initial + observe content changes
|
|
256
|
+
if (document.readyState === "loading") {
|
|
257
|
+
document.addEventListener("DOMContentLoaded", postSize);
|
|
258
|
+
} else {
|
|
259
|
+
postSize();
|
|
260
|
+
}
|
|
261
|
+
window.addEventListener("load", postSize);
|
|
262
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
263
|
+
var ro = new ResizeObserver(postSize);
|
|
264
|
+
// Observe documentElement so we catch any layout change
|
|
265
|
+
ro.observe(document.documentElement);
|
|
266
|
+
} else {
|
|
267
|
+
// Fallback: poll once a second
|
|
268
|
+
setInterval(postSize, 1000);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// --- SSE live reload --------------------------------------------------
|
|
274
|
+
function setUpLiveReload() {
|
|
275
|
+
if (!ctx.environment || !ctx.package) return;
|
|
276
|
+
if (typeof EventSource === "undefined") return;
|
|
277
|
+
var url =
|
|
278
|
+
apiBase +
|
|
279
|
+
"/environments/" +
|
|
280
|
+
encodeURIComponent(ctx.environment) +
|
|
281
|
+
"/packages/" +
|
|
282
|
+
encodeURIComponent(ctx.package) +
|
|
283
|
+
"/events";
|
|
284
|
+
try {
|
|
285
|
+
var es = new EventSource(url, { withCredentials: true });
|
|
286
|
+
var pending = false;
|
|
287
|
+
es.addEventListener("changed", function () {
|
|
288
|
+
if (pending) return;
|
|
289
|
+
pending = true;
|
|
290
|
+
// Tiny debounce to coalesce a flurry of saves
|
|
291
|
+
setTimeout(function () {
|
|
292
|
+
location.reload();
|
|
293
|
+
}, 100);
|
|
294
|
+
});
|
|
295
|
+
es.onerror = function () {
|
|
296
|
+
// Browser will auto-reconnect; nothing to do.
|
|
297
|
+
};
|
|
298
|
+
} catch (_e) {
|
|
299
|
+
// SSE may be blocked (e.g. corp proxy) — non-fatal.
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// --- Public API --------------------------------------------------------
|
|
304
|
+
window.Publisher = {
|
|
305
|
+
query: query,
|
|
306
|
+
queryFull: queryFull,
|
|
307
|
+
embed: embed,
|
|
308
|
+
context: ctx,
|
|
309
|
+
setToken: function (token) {
|
|
310
|
+
bearerToken = token || null;
|
|
311
|
+
},
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
// Auto-init the in-iframe behaviors and live-reload subscription.
|
|
315
|
+
// Both are no-ops if not applicable.
|
|
316
|
+
setUpEmbeddedSelfBehaviors();
|
|
317
|
+
setUpLiveReload();
|
|
318
|
+
})();
|