@malloy-publisher/server 0.0.204 → 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.
Files changed (55) hide show
  1. package/build.ts +10 -1
  2. package/dist/app/api-doc.yaml +133 -4
  3. package/dist/app/assets/{EnvironmentPage-CX06cjOF.js → EnvironmentPage-CAge6UHD.js} +1 -1
  4. package/dist/app/assets/HomePage-DhTe8qpa.js +1 -0
  5. package/dist/app/assets/{MainPage-nUJ9YatG.js → MainPage-CeTxxGex.js} +2 -2
  6. package/dist/app/assets/MaterializationsPage-CpDHB70t.js +1 -0
  7. package/dist/app/assets/ModelPage-D9sSMb75.js +1 -0
  8. package/dist/app/assets/{PackagePage-BaEVdEAG.js → PackagePage-LRqQWrFY.js} +1 -1
  9. package/dist/app/assets/{RouteError-BShQjZio.js → RouteError-xT6kuCNw.js} +1 -1
  10. package/dist/app/assets/{WorkbookPage-CBn6ZjJW.js → WorkbookPage-DsIh9svZ.js} +1 -1
  11. package/dist/app/assets/{core-DECXYL4E.es-OaRfXwuQ.js → core-C2sQrwVu.es-Bjem0hym.js} +1 -1
  12. package/dist/app/assets/{index-BLfPC1gy.js → index-BdOZDcce.js} +1 -1
  13. package/dist/app/assets/{index-Dy3YhAZQ.js → index-DHHAcY5o.js} +1 -1
  14. package/dist/app/assets/{index-DqiJ0bWp.js → index-RX3QOTde.js} +121 -121
  15. package/dist/app/assets/{index.umd-DAN9K8yC.js → index.umd-D2WH3D-f.js} +1 -1
  16. package/dist/app/index.html +1 -1
  17. package/dist/runtime/publisher.js +318 -0
  18. package/dist/server.mjs +567 -194
  19. package/package.json +5 -4
  20. package/scripts/bake-duckdb-extensions.js +104 -0
  21. package/src/controller/watch-mode.controller.ts +176 -46
  22. package/src/errors.spec.ts +21 -0
  23. package/src/mcp/error_messages.spec.ts +35 -0
  24. package/src/mcp/error_messages.ts +14 -1
  25. package/src/mcp/handler_utils.ts +12 -0
  26. package/src/runtime/publisher.js +318 -0
  27. package/src/server.ts +479 -2
  28. package/src/service/authorize_integration.spec.ts +96 -2
  29. package/src/service/compile_authorize.spec.ts +85 -0
  30. package/src/service/environment.ts +63 -5
  31. package/src/service/environment_store.ts +142 -11
  32. package/src/service/model.ts +44 -0
  33. package/src/service/package.ts +17 -6
  34. package/src/storage/duckdb/DuckDBConnection.ts +70 -124
  35. package/tests/fixtures/authorize-compile/model.malloy +9 -0
  36. package/tests/fixtures/authorize-compile/publisher.json +4 -0
  37. package/tests/fixtures/html-pages-nopublic/model.malloy +1 -0
  38. package/tests/fixtures/html-pages-nopublic/publisher.json +5 -0
  39. package/tests/fixtures/html-pages-test/data.csv +3 -0
  40. package/tests/fixtures/html-pages-test/public/assets/app.css +3 -0
  41. package/tests/fixtures/html-pages-test/public/data.json +1 -0
  42. package/tests/fixtures/html-pages-test/public/index.html +9 -0
  43. package/tests/fixtures/html-pages-test/public/sub/page2.html +9 -0
  44. package/tests/fixtures/html-pages-test/publisher.json +5 -0
  45. package/tests/fixtures/html-pages-test/report.malloy +1 -0
  46. package/tests/integration/authorize/compile_authorize_http.integration.spec.ts +92 -0
  47. package/tests/integration/duckdb_storage/duckdb_storage.integration.spec.ts +138 -0
  48. package/tests/integration/html_pages/html_pages.integration.spec.ts +378 -0
  49. package/tests/integration/watch-mode/watch_mode.integration.spec.ts +421 -0
  50. package/tests/unit/duckdb/attached_databases.test.ts +111 -0
  51. package/tests/unit/duckdb/duckdb_connection.test.ts +181 -0
  52. package/tests/unit/duckdb/repositories.test.ts +208 -0
  53. package/dist/app/assets/HomePage-CNFt_eUU.js +0 -1
  54. package/dist/app/assets/MaterializationsPage-B5goxVXW.js +0 -1
  55. package/dist/app/assets/ModelPage-Ba7Xh4lL.js +0 -1
@@ -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
+ })();