@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
|
@@ -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
|
+
})();
|