@malloy-publisher/server 0.18.0 → 0.18.1
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/dist/app/api-doc.yaml +37 -6
- package/dist/app/assets/{EnvironmentPage-BYi5H87v.js → EnvironmentPage-CsFUI8IX.js} +1 -1
- package/dist/app/assets/{HomePage-DB-CSs0p.js → HomePage-B_FVtnN-.js} +1 -1
- package/dist/app/assets/{MainPage-2j-MpcYJ.js → MainPage-wFF9nPEr.js} +1 -1
- package/dist/app/assets/{ModelPage-BQE-ZC6E.js → ModelPage-CxenqDcz.js} +2 -2
- package/dist/app/assets/PackagePage-dcF6vrvw.js +1 -0
- package/dist/app/assets/PackageSplatRoute-CASBZk0k.js +2 -0
- package/dist/app/assets/{RenderedResult-QdYnecnr.es-PKDmuES7.js → RenderedResult--fYcsY1h.es-DzOt5Lwv.js} +3 -3
- package/dist/app/assets/{RouteError-7Fw_wYUy.js → RouteError-CUIolYwb.js} +1 -1
- package/dist/app/assets/{ThemeEditorPage-DmiRg7rU.js → ThemeEditorPage-DgU2YYq6.js} +1 -1
- package/dist/app/assets/{ToggleButtonGroup-BXVbLuei.js → ToggleButtonGroup-Dmr-hVlD.js} +1 -1
- package/dist/app/assets/{documentRoutes-DWIPC8nU.js → documentRoutes-Drv-WDwO.js} +1 -1
- package/dist/app/assets/{editPageMounted-BaGHeFZ5.js → editPageMounted-C5yJiIVi.js} +2 -2
- package/dist/app/assets/{index-CHfKShi-.js → index-BIoSdsU9.js} +1 -1
- package/dist/app/assets/{index-OQqQyWrE.js → index-BSyL0zds.js} +97 -97
- package/dist/app/assets/{index-BRdik0lA.js → index-Dtw4eTxH.js} +1 -1
- package/dist/app/assets/{index-APmAmrTn.js → index-DxF3kMLJ.js} +1 -1
- package/dist/app/assets/{index.es-DLFu6xFm.js → index.es-CX9m4CBD.js} +10 -10
- package/dist/app/assets/usePackageLinkNavigate-qNjcXPcY.js +1 -0
- package/dist/app/assets/{usePackageRoute-pIAy965Q.js → usePackageRoute-wJGaZEiY.js} +1 -1
- package/dist/app/index.html +1 -1
- package/dist/instrumentation.mjs +16 -7
- package/dist/package_load_worker.mjs +144 -43
- package/dist/runtime/publisher.colorScheme.spec.ts +167 -0
- package/dist/runtime/publisher.js +173 -3
- package/dist/runtime/publisher_embed.spec.ts +108 -0
- package/dist/server.mjs +1554 -554
- package/package.json +1 -1
- package/dist/app/assets/PackagePage-BMv6ojk4.js +0 -1
- package/dist/app/assets/PackageSplatRoute-BOAFRQKR.js +0 -2
- package/dist/app/assets/usePackageLinkNavigate-DTlEYsed.js +0 -1
|
@@ -9,11 +9,22 @@
|
|
|
9
9
|
// - Publisher.query(model, malloy, opts?) → Promise<rows[]>
|
|
10
10
|
// - Publisher.queryFull(model, malloy, opts?) → Promise<MalloyResult> (envelope for <malloy-render>)
|
|
11
11
|
// opts: { environment?, package?, sourceName?, queryName?, filterParams?,
|
|
12
|
-
// bypassFilters?, givens?, versionId? }.
|
|
12
|
+
// bypassFilters?, givens?, versionId?, meta? }. meta, an object, is filled
|
|
13
|
+
// with { servedFrom, path, executionTimeMs } from the response (path is
|
|
14
|
+
// the path report: the table that served it and the ones considered)
|
|
15
|
+
// and versionId, the version the query was pinned to, or null. opts.versionId names a published
|
|
13
16
|
// version; a page opened with `?versionId=` queries its own
|
|
14
17
|
// package at that version by default. givens is a name→value map bound as
|
|
15
18
|
// Malloy given: runtime parameters for this query (safe parameterization,
|
|
16
19
|
// not string interpolation) — see the malloy-html-data-apps skill (reference/runtime.md).
|
|
20
|
+
// - Publisher.model(modelPath, opts?) → Promise<Model> (its sources, their docs)
|
|
21
|
+
// - Publisher.packageResource(name, opts?) → Promise<JSON>, name "build-plan" or
|
|
22
|
+
// "materializations": what Publisher builds for the package and its runs,
|
|
23
|
+
// newest first (opts.limit reads the newest runs only).
|
|
24
|
+
// Both read the page's own version when it is pinned, as queries do.
|
|
25
|
+
// - Publisher.colorScheme, Publisher.onColorScheme(fn), Publisher.colorSchemeApplied(mode):
|
|
26
|
+
// the reader's light or dark mode from the host ("publisher:color-scheme"),
|
|
27
|
+
// and the page's answer that it drew it ("publisher:color-scheme-applied").
|
|
17
28
|
// - Publisher.embed(selector, { src, height?, token? })
|
|
18
29
|
// - Publisher.context ({ environment, package } inferred from URL)
|
|
19
30
|
// - Publisher.setToken(token) (override Bearer token; default uses cookies)
|
|
@@ -189,11 +200,149 @@
|
|
|
189
200
|
err.status = res.status;
|
|
190
201
|
throw err;
|
|
191
202
|
}
|
|
203
|
+
if (opts.meta && typeof opts.meta === "object") {
|
|
204
|
+
opts.meta.servedFrom =
|
|
205
|
+
json.servedFrom == null ? null : json.servedFrom;
|
|
206
|
+
opts.meta.path = json.path == null ? null : json.path;
|
|
207
|
+
opts.meta.executionTimeMs =
|
|
208
|
+
json.executionTimeMs == null ? null : json.executionTimeMs;
|
|
209
|
+
opts.meta.versionId = body.versionId || null;
|
|
210
|
+
}
|
|
192
211
|
// The server's QueryResult always has `result` as a JSON-encoded string.
|
|
193
212
|
// Parse it before handing it back so callers see real JS values.
|
|
194
213
|
return JSON.parse(json.result);
|
|
195
214
|
}
|
|
196
215
|
|
|
216
|
+
// A read of the package itself, at the page's pinned version when the read is
|
|
217
|
+
// of the page's own package, as a query is.
|
|
218
|
+
async function packageGet(sub, opts) {
|
|
219
|
+
opts = opts || {};
|
|
220
|
+
var target = resolveTarget(opts);
|
|
221
|
+
var url =
|
|
222
|
+
apiBase +
|
|
223
|
+
"/environments/" +
|
|
224
|
+
encodeURIComponent(target.env) +
|
|
225
|
+
"/packages/" +
|
|
226
|
+
encodeURIComponent(target.pkg) +
|
|
227
|
+
"/" +
|
|
228
|
+
sub;
|
|
229
|
+
var versionId =
|
|
230
|
+
opts.versionId ||
|
|
231
|
+
(target.env === ctx.environment && target.pkg === ctx.package
|
|
232
|
+
? await pinnedVersion()
|
|
233
|
+
: undefined);
|
|
234
|
+
var params = [];
|
|
235
|
+
if (versionId) params.push("versionId=" + encodeURIComponent(versionId));
|
|
236
|
+
// A list (the package's runs) reads its newest `limit` entries.
|
|
237
|
+
if (opts.limit > 0 && Number.isInteger(opts.limit))
|
|
238
|
+
params.push("limit=" + opts.limit);
|
|
239
|
+
if (params.length) url += "?" + params.join("&");
|
|
240
|
+
var res = await fetch(url, {
|
|
241
|
+
credentials: "include",
|
|
242
|
+
headers: authHeaders(),
|
|
243
|
+
});
|
|
244
|
+
if (!res.ok) {
|
|
245
|
+
var err = new Error("Publisher: " + sub + " (" + res.status + ")");
|
|
246
|
+
err.status = res.status;
|
|
247
|
+
throw err;
|
|
248
|
+
}
|
|
249
|
+
return res.json();
|
|
250
|
+
}
|
|
251
|
+
function model(modelPath, opts) {
|
|
252
|
+
return packageGet("models/" + encodeURIComponent(modelPath), opts);
|
|
253
|
+
}
|
|
254
|
+
var PACKAGE_RESOURCES = { "build-plan": true, materializations: true };
|
|
255
|
+
function packageResource(name, opts) {
|
|
256
|
+
if (!Object.prototype.hasOwnProperty.call(PACKAGE_RESOURCES, name)) {
|
|
257
|
+
return Promise.reject(
|
|
258
|
+
new Error(
|
|
259
|
+
'Publisher.packageResource: "' +
|
|
260
|
+
name +
|
|
261
|
+
'" is not one of build-plan, materializations',
|
|
262
|
+
),
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
return packageGet(name, opts);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// --- Light and dark, from the host -------------------------------------
|
|
269
|
+
// The "publisher:color-scheme*" messages are the same contract as
|
|
270
|
+
// PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE and its two siblings in
|
|
271
|
+
// packages/sdk/src/utils/dataAppEmbed.ts; keep the two in sync. The host
|
|
272
|
+
// tells the page the reader's mode; the page, once it has drawn it, answers
|
|
273
|
+
// with colorSchemeApplied, so a host holding the frame light can let it go.
|
|
274
|
+
var colorScheme = null;
|
|
275
|
+
var appliedScheme = null;
|
|
276
|
+
var colorSchemeListeners = [];
|
|
277
|
+
var hostOrigin = null;
|
|
278
|
+
function onColorScheme(fn) {
|
|
279
|
+
colorSchemeListeners.push(fn);
|
|
280
|
+
if (colorScheme) {
|
|
281
|
+
try {
|
|
282
|
+
fn(colorScheme);
|
|
283
|
+
} catch (_e) {
|
|
284
|
+
/* a listener's error is its own */
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return function () {
|
|
288
|
+
colorSchemeListeners = colorSchemeListeners.filter(function (f) {
|
|
289
|
+
return f !== fn;
|
|
290
|
+
});
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function colorSchemeApplied(mode) {
|
|
294
|
+
if (mode !== "light" && mode !== "dark") return;
|
|
295
|
+
appliedScheme = mode;
|
|
296
|
+
if (!window.parent || window.parent === window) return;
|
|
297
|
+
try {
|
|
298
|
+
window.parent.postMessage(
|
|
299
|
+
{ type: "publisher:color-scheme-applied", mode: mode },
|
|
300
|
+
hostOrigin || "*",
|
|
301
|
+
);
|
|
302
|
+
} catch (_e) {
|
|
303
|
+
/* ignore */
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function setUpColorScheme() {
|
|
307
|
+
if (!window.parent || window.parent === window) return;
|
|
308
|
+
if (typeof window.addEventListener !== "function") return;
|
|
309
|
+
window.addEventListener("message", function (e) {
|
|
310
|
+
var d = e.data;
|
|
311
|
+
if (
|
|
312
|
+
e.source !== window.parent ||
|
|
313
|
+
!d ||
|
|
314
|
+
d.type !== "publisher:color-scheme"
|
|
315
|
+
)
|
|
316
|
+
return;
|
|
317
|
+
if (d.mode !== "light" && d.mode !== "dark") return;
|
|
318
|
+
hostOrigin = e.origin && e.origin !== "null" ? e.origin : null;
|
|
319
|
+
// The same mode again: a host that forgot the answer (a frame it reloaded) hears it again.
|
|
320
|
+
if (d.mode === colorScheme) {
|
|
321
|
+
// What the page drew, which a page with a fixed mode (?mode=) may differ from.
|
|
322
|
+
if (appliedScheme) colorSchemeApplied(appliedScheme);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
colorScheme = d.mode;
|
|
326
|
+
colorSchemeListeners.slice().forEach(function (fn) {
|
|
327
|
+
try {
|
|
328
|
+
fn(colorScheme);
|
|
329
|
+
} catch (_e) {
|
|
330
|
+
/* a listener's error is its own */
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
// In case the host spoke before this page was listening. It carries
|
|
335
|
+
// nothing, so it goes to the host's own origin where the browser says it.
|
|
336
|
+
try {
|
|
337
|
+
window.parent.postMessage(
|
|
338
|
+
{ type: "publisher:color-scheme-request" },
|
|
339
|
+
(location.ancestorOrigins && location.ancestorOrigins[0]) || "*",
|
|
340
|
+
);
|
|
341
|
+
} catch (_e) {
|
|
342
|
+
/* ignore */
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
197
346
|
function query(modelPath, malloyQuery, opts) {
|
|
198
347
|
return rawQuery(modelPath, malloyQuery, opts, true);
|
|
199
348
|
}
|
|
@@ -232,10 +381,23 @@
|
|
|
232
381
|
} else {
|
|
233
382
|
iframe.style.height = "0px"; // will be sized via postMessage
|
|
234
383
|
}
|
|
235
|
-
|
|
384
|
+
// The page can save a file, open a link in a new tab, and use the
|
|
385
|
+
// clipboard and full screen. Never top navigation, which would move the
|
|
386
|
+
// host page away, or modal dialogs, and an opened tab keeps the sandbox:
|
|
387
|
+
// in Firefox one that escaped it could redirect the host page. Same as
|
|
388
|
+
// Publisher's own viewer (DataAppViewer).
|
|
389
|
+
// A host passes its own `allow` or `sandbox` to replace the default.
|
|
390
|
+
// `allow: ""` delegates no feature. A host's own `sandbox` replaces every
|
|
391
|
+
// token, so it must keep `allow-scripts allow-same-origin` for the page
|
|
392
|
+
// to run, query and resize; `sandbox: ""` is the strictest frame, where
|
|
393
|
+
// the page draws nothing.
|
|
394
|
+
iframe.allow =
|
|
395
|
+
options.allow == null ? "clipboard-write; fullscreen" : options.allow;
|
|
236
396
|
iframe.setAttribute(
|
|
237
397
|
"sandbox",
|
|
238
|
-
|
|
398
|
+
options.sandbox == null
|
|
399
|
+
? "allow-scripts allow-same-origin allow-forms allow-downloads allow-popups"
|
|
400
|
+
: options.sandbox,
|
|
239
401
|
);
|
|
240
402
|
|
|
241
403
|
// Resize listener
|
|
@@ -432,8 +594,15 @@
|
|
|
432
594
|
window.Publisher = {
|
|
433
595
|
query: query,
|
|
434
596
|
queryFull: queryFull,
|
|
597
|
+
model: model,
|
|
598
|
+
packageResource: packageResource,
|
|
435
599
|
embed: embed,
|
|
436
600
|
context: ctx,
|
|
601
|
+
get colorScheme() {
|
|
602
|
+
return colorScheme;
|
|
603
|
+
},
|
|
604
|
+
onColorScheme: onColorScheme,
|
|
605
|
+
colorSchemeApplied: colorSchemeApplied,
|
|
437
606
|
setToken: function (token) {
|
|
438
607
|
bearerToken = token || null;
|
|
439
608
|
},
|
|
@@ -442,5 +611,6 @@
|
|
|
442
611
|
// Auto-init the in-iframe behaviors and live-reload subscription.
|
|
443
612
|
// Both are no-ops if not applicable.
|
|
444
613
|
setUpEmbeddedSelfBehaviors();
|
|
614
|
+
setUpColorScheme();
|
|
445
615
|
setUpLiveReload();
|
|
446
616
|
})();
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What `Publisher.embed` lets the frame it creates do, read from the frame the
|
|
6
|
+
* real runtime builds on a host page. The same rules as the Console's viewer
|
|
7
|
+
* (DataAppViewer): downloads, new tabs, the clipboard and full screen, and never anything
|
|
8
|
+
* over the host.
|
|
9
|
+
*/
|
|
10
|
+
import { describe, expect, it } from "bun:test";
|
|
11
|
+
import fs from "fs";
|
|
12
|
+
import path from "path";
|
|
13
|
+
import { Window } from "happy-dom";
|
|
14
|
+
|
|
15
|
+
const RUNTIME = fs.readFileSync(path.join(__dirname, "publisher.js"), "utf8");
|
|
16
|
+
|
|
17
|
+
function embedFrame(options: Record<string, unknown> = {}) {
|
|
18
|
+
const window = new Window({ url: "https://host.example.com/page" });
|
|
19
|
+
window.document.body.innerHTML = '<div id="slot"></div>';
|
|
20
|
+
// A host page, not a framed one: the runtime sees it is the top window and
|
|
21
|
+
// installs only window.Publisher.
|
|
22
|
+
new Function("window", "document", "location", "MutationObserver", RUNTIME)(
|
|
23
|
+
window,
|
|
24
|
+
window.document,
|
|
25
|
+
window.location,
|
|
26
|
+
window.MutationObserver,
|
|
27
|
+
);
|
|
28
|
+
const publisher = (
|
|
29
|
+
window as unknown as {
|
|
30
|
+
Publisher: {
|
|
31
|
+
embed: (
|
|
32
|
+
selector: string,
|
|
33
|
+
options: Record<string, unknown>,
|
|
34
|
+
) => { iframe: HTMLIFrameElement };
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
).Publisher;
|
|
38
|
+
const { iframe } = publisher.embed("#slot", {
|
|
39
|
+
src: "https://data.example.com/environments/e/packages/p/index.html",
|
|
40
|
+
...options,
|
|
41
|
+
});
|
|
42
|
+
const sandboxAttribute = iframe.getAttribute("sandbox");
|
|
43
|
+
const sandbox = (sandboxAttribute ?? "").split(/\s+/);
|
|
44
|
+
const allow = (iframe.getAttribute("allow") ?? "")
|
|
45
|
+
.split(";")
|
|
46
|
+
.map((feature: string) => feature.trim())
|
|
47
|
+
.filter(Boolean);
|
|
48
|
+
void window.happyDOM.close();
|
|
49
|
+
return { sandbox, sandboxAttribute, allow };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe("Publisher.embed frame permissions", () => {
|
|
53
|
+
it("lets the page save a file, open a link in a new tab, copy and go full screen", () => {
|
|
54
|
+
const { sandbox, allow } = embedFrame();
|
|
55
|
+
expect(sandbox).toEqual(
|
|
56
|
+
expect.arrayContaining(["allow-downloads", "allow-popups"]),
|
|
57
|
+
);
|
|
58
|
+
expect(allow).toEqual(["clipboard-write", "fullscreen"]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("keeps what the page needs to run, query as the reader and report its size", () => {
|
|
62
|
+
expect(embedFrame().sandbox).toEqual(
|
|
63
|
+
expect.arrayContaining([
|
|
64
|
+
"allow-scripts",
|
|
65
|
+
"allow-same-origin",
|
|
66
|
+
"allow-forms",
|
|
67
|
+
]),
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("grants nothing else: no top navigation, dialogs, clipboard reads, tabs that leave the sandbox or other features", () => {
|
|
72
|
+
const { sandbox, allow } = embedFrame();
|
|
73
|
+
expect([...sandbox].sort()).toEqual([
|
|
74
|
+
"allow-downloads",
|
|
75
|
+
"allow-forms",
|
|
76
|
+
"allow-popups",
|
|
77
|
+
"allow-same-origin",
|
|
78
|
+
"allow-scripts",
|
|
79
|
+
]);
|
|
80
|
+
expect([...allow].sort()).toEqual(["clipboard-write", "fullscreen"]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("takes the host's own allow, and an empty one turns the default off", () => {
|
|
84
|
+
expect(embedFrame({ allow: "fullscreen" }).allow).toEqual(["fullscreen"]);
|
|
85
|
+
expect(embedFrame({ allow: "" }).allow).toEqual([]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("reads a null allow as the default, not as a policy named null", () => {
|
|
89
|
+
expect(embedFrame({ allow: null }).allow).toEqual([
|
|
90
|
+
"clipboard-write",
|
|
91
|
+
"fullscreen",
|
|
92
|
+
]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("takes the host's own sandbox, so a host can keep the old frame", () => {
|
|
96
|
+
expect(
|
|
97
|
+
embedFrame({ sandbox: "allow-scripts allow-same-origin allow-forms" })
|
|
98
|
+
.sandbox,
|
|
99
|
+
).toEqual(["allow-scripts", "allow-same-origin", "allow-forms"]);
|
|
100
|
+
expect(embedFrame({ sandbox: null }).sandbox).toEqual(
|
|
101
|
+
expect.arrayContaining(["allow-downloads", "allow-popups"]),
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("keeps an empty sandbox empty, the strictest frame, rather than the default", () => {
|
|
106
|
+
expect(embedFrame({ sandbox: "" }).sandboxAttribute).toBe("");
|
|
107
|
+
});
|
|
108
|
+
});
|