@yuneta/gobj-ui 7.27.28 → 7.27.30

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.
@@ -6,8 +6,10 @@
6
6
  * wants (the tree, the trace levels of a gclass, a trace set) and
7
7
  * the host loads the answers. Asserted here: what reaches the host,
8
8
  * what the TRACES block of a card shows for each origin of a level,
9
- * that a reload keeps the card open, and that a LOCAL tree publishes
10
- * none of it (its hosts declare none of these events).
9
+ * that a reload keeps the card open, that the open gclass viewer is
10
+ * locked only by a set that moves what it shows (and unlocked by
11
+ * the answer it waits for), and that a LOCAL tree publishes none of
12
+ * it (its hosts declare none of these events).
11
13
  *
12
14
  * The real gclass, on a G6 Graph double.
13
15
  *
@@ -94,6 +96,7 @@ const {
94
96
  gobj_start, gobj_read_attr, gobj_send_event, set_log_callback, register_c_timer,
95
97
  } = await import("@yuneta/gobj-js");
96
98
  const {register_c_yui_gobj_tree_js} = await import("./c_yui_gobj_tree_js.js");
99
+ const {register_c_yui_window} = await import("./c_yui_window.js");
97
100
 
98
101
  const logged = [];
99
102
  const wanted = [];
@@ -144,6 +147,7 @@ beforeAll(async () => {
144
147
  });
145
148
  register_c_timer();
146
149
  register_c_yui_gobj_tree_js();
150
+ register_c_yui_window();
147
151
  gclass_create("C_TEST_TREE_YUNO", [], [["ST_IDLE", []]], {}, 0,
148
152
  [SDATA_END()], {}, 0, 0, 0, 0);
149
153
  gclass_create("C_TEST_TREE_HOST",
@@ -379,6 +383,102 @@ describe("C_YUI_GOBJ_TREE_JS, source remote", () => {
379
383
  });
380
384
  });
381
385
 
386
+ /* The `view-gclass` answer for C_TCP, enough for the viewer. */
387
+ const TCP_DESCRIPTION = {
388
+ id: "C_TCP", gcflag: [], instances: 1, attrs: [], commands: [],
389
+ gclass_methods: [], internal_methods: [],
390
+ FSM: {events: [], states: {}},
391
+ info_gclass_trace: {traffic: "Trace dump traffic", connections: "Trace connections"},
392
+ };
393
+
394
+ /* The tree with the gclass viewer of C_TCP open, and its levels read. */
395
+ async function open_tcp_viewer()
396
+ {
397
+ const view = await mount("remote", host);
398
+ await open_tcp_card(view);
399
+ gobj_send_event(view, "EV_TRACE_INFO", TCP_INFO, host);
400
+ gobj_send_event(view, "EV_OPEN_GCLASS", {}, view);
401
+ gobj_send_event(view, "EV_GCLASS_DESCRIPTION",
402
+ {gclass: "C_TCP", description: TCP_DESCRIPTION}, host);
403
+ const viewer = view.priv.gclass_view && view.priv.gclass_view.json_gobj;
404
+ expect(viewer).toBeTruthy();
405
+ wanted.length = 0;
406
+ return {view, viewer};
407
+ }
408
+
409
+ function viewer_box(viewer, level)
410
+ {
411
+ return gobj_read_attr(viewer, "$container").querySelectorAll(".GCLASS_TRACE_GCLASS")
412
+ .find((b) => b.getAttribute("data-level") === level);
413
+ }
414
+
415
+ describe("C_YUI_GOBJ_TREE_JS, source remote: the open gclass viewer", () => {
416
+ test("a set of the viewer's gclass locks it until ITS answer", async () => {
417
+ const {view, viewer} = await open_tcp_viewer();
418
+ expect(viewer_box(viewer, "traffic").disabled).toBe(false);
419
+ gobj_send_event(view, "EV_SET_GCLASS_TRACE", {gclass: "C_TCP", levels: ["traffic"], set: true}, viewer);
420
+ expect(viewer_box(viewer, "traffic").disabled).toBe(true);
421
+
422
+ gobj_send_event(view, "EV_TRACE_INFO", Object.assign({}, TCP_INFO, {gclass: "C_TIMER"}), host);
423
+ expect(viewer_box(viewer, "traffic").disabled).toBe(true);
424
+
425
+ gobj_send_event(view, "EV_TRACE_INFO", Object.assign({}, TCP_INFO,
426
+ {gclass_on: ["connections", "traffic"]}), host);
427
+ expect([viewer_box(viewer, "traffic").checked, viewer_box(viewer, "traffic").disabled])
428
+ .toEqual([true, false]);
429
+ expect(problems()).toEqual([]);
430
+ });
431
+
432
+ test("a gobj set on the card of ANOTHER gclass leaves the viewer alone", async () => {
433
+ const {view, viewer} = await open_tcp_viewer();
434
+ const root_id = node_id_of(view, "C_YUNO^gate");
435
+ gobj_send_event(view, "EV_NODE_CLICK", {id: root_id, client_x: 10, client_y: 10}, view);
436
+ gobj_send_event(view, "EV_TRACE_INFO", YUNO_INFO, host);
437
+
438
+ const {$gobj} = row_of(view, "ev_kw");
439
+ $gobj.checked = true;
440
+ $gobj.dispatchEvent(fake_event("change"));
441
+ expect(wanted.filter((w) => w.event === "EV_SET_TRACE_LEVEL").map((w) => [w.kw.scope, w.kw.gclass]))
442
+ .toEqual([["gobj", "C_YUNO"]]);
443
+ expect(viewer_box(viewer, "traffic").disabled).toBe(false);
444
+
445
+ gobj_send_event(view, "EV_TRACE_INFO", YUNO_INFO, host);
446
+ expect(viewer_box(viewer, "traffic").disabled).toBe(false);
447
+ expect(problems()).toEqual([]);
448
+ });
449
+
450
+ test("a set of the yuno's scope locks it until the yuno's answer AND its own", async () => {
451
+ const {view, viewer} = await open_tcp_viewer();
452
+ gobj_read_attr(view, "$container").querySelector(".GOBJ_TREE_YUNO_TRACES_BUTTON").click();
453
+ gobj_send_event(view, "EV_TRACE_INFO", YUNO_INFO, host);
454
+ wanted.length = 0;
455
+
456
+ const ev_kw = row_of(view, "ev_kw");
457
+ ev_kw.$global.checked = true;
458
+ ev_kw.$global.dispatchEvent(fake_event("change"));
459
+ expect(viewer_box(viewer, "traffic").disabled).toBe(true);
460
+
461
+ /* the answer of an unrelated gclass, arriving first */
462
+ gobj_send_event(view, "EV_TRACE_INFO", Object.assign({}, TCP_INFO, {gclass: "C_TIMER"}), host);
463
+ expect(viewer_box(viewer, "traffic").disabled).toBe(true);
464
+ wanted.length = 0;
465
+
466
+ /* the yuno's: the viewer's own is asked for, still locked */
467
+ gobj_send_event(view, "EV_TRACE_INFO", Object.assign({}, YUNO_INFO,
468
+ {global_on: ["machine", "ev_kw"]}), host);
469
+ expect(wanted.map((w) => [w.event, w.kw.gclass])).toEqual([["EV_TRACE_INFO_WANTED", "C_TCP"]]);
470
+ expect(viewer_box(viewer, "traffic").disabled).toBe(true);
471
+
472
+ gobj_send_event(view, "EV_TRACE_INFO", Object.assign({}, TCP_INFO,
473
+ {global_levels: YUNO_INFO.global_levels,
474
+ gclass_on: ["connections", "ev_kw"], global_on: ["ev_kw"]}), host);
475
+ expect(viewer_box(viewer, "traffic").disabled).toBe(false);
476
+ expect([viewer_box(viewer, "ev_kw").checked, viewer_box(viewer, "ev_kw").disabled])
477
+ .toEqual([true, true]);
478
+ expect(problems()).toEqual([]);
479
+ });
480
+ });
481
+
382
482
  describe("C_YUI_GOBJ_TREE_JS, source local", () => {
383
483
  test("publishes nothing: its host declares none of the remote events", async () => {
384
484
  const view = await mount("local", yuno);
@@ -104,6 +104,14 @@ export function yui_shell_popup_layer(shell)
104
104
  return (priv && priv.layers && priv.layers.popup) || null;
105
105
  }
106
106
 
107
+ /* Public: the toasts' layer (z 120), above the dialogs, for a notice
108
+ * that is not a toast and stays until it is answered (the bar of
109
+ * C_YUI_UPDATE_WATCH). Null when there is no shell. */
110
+ export function yui_shell_notification_layer(shell)
111
+ {
112
+ return notification_layer(shell) || null;
113
+ }
114
+
107
115
 
108
116
  /***************************************************************
109
117
  * i18n bridge:
@@ -3,7 +3,9 @@
3
3
  *
4
4
  * EV_VIEW_SHOWN: a view the shell shows AGAIN after hiding it
5
5
  * (`keep_alive`) is told so, if it declares the event, and
6
- * C_YUI_SERVICE_VIEW hands it on to the view it hosts.
6
+ * C_YUI_SERVICE_VIEW hands it on to the view it hosts. A
7
+ * C_YUI_NODE tree tells the content on screen once, from its own
8
+ * route change, and the shell does not tell it again.
7
9
  *
8
10
  * Driven through the real C_YUI_SHELL on a document double.
9
11
  *
@@ -23,6 +25,7 @@ const {
23
25
  } = await import("@yuneta/gobj-js");
24
26
  const {register_c_yui_shell, yui_shell_navigate} = await import("./c_yui_shell.js");
25
27
  const {register_c_yui_service_view} = await import("./c_yui_service_view.js");
28
+ const {register_c_yui_node} = await import("./c_yui_node.js");
26
29
  const {register_c_yui_nav} = await import("./c_yui_nav.js");
27
30
 
28
31
  const logged = [];
@@ -68,6 +71,7 @@ beforeAll(async () => {
68
71
  register_c_yui_shell();
69
72
  register_c_yui_nav();
70
73
  register_c_yui_service_view();
74
+ register_c_yui_node();
71
75
  yuno = gobj_create_yuno("view_shown_yuno", "C_TEST_SHOWN_HOST", {});
72
76
  gobj_start(yuno);
73
77
  gobj_create_service("test_transport", "C_TEST_SHOWN_HOST", {}, yuno);
@@ -112,6 +116,31 @@ function config()
112
116
  target: target("C_TEST_PLAIN_VIEW", "keep_alive", "plain_view")},
113
117
  {id: "gone", name: "gone", route: "/gone",
114
118
  target: target("C_TEST_SHOWN_VIEW", "lazy_destroy", "gone_view")},
119
+ {id: "tree", name: "tree", route: "/tree",
120
+ target: {
121
+ stage: "main", gclass: "C_YUI_NODE", lifecycle: "keep_alive",
122
+ name: "the_tree",
123
+ kw: {
124
+ node_id: "tree", base_route: "/tree",
125
+ content: {gclass: "C_TEST_SHOWN_VIEW", kw: {}},
126
+ children: [
127
+ {id: "acc", label: "acc",
128
+ content: {gclass: "C_TEST_SHOWN_VIEW", kw: {}}},
129
+ {id: "deep", label: "deep", children: [
130
+ {id: "leaf", label: "leaf",
131
+ content: {gclass: "C_TEST_SHOWN_VIEW", kw: {}}}
132
+ ]},
133
+ {id: "svc", label: "svc", content: {
134
+ gclass: "C_YUI_SERVICE_VIEW",
135
+ kw: {
136
+ view_gclass: "C_TEST_SHOWN_VIEW",
137
+ service_name: "#tree_svc_view",
138
+ transport_service: "test_transport"
139
+ }
140
+ }}
141
+ ]
142
+ }
143
+ }},
115
144
  {id: "hosted", name: "hosted", route: "/hosted",
116
145
  target: {
117
146
  stage: "main", gclass: "C_YUI_SERVICE_VIEW", lifecycle: "keep_alive",
@@ -181,6 +210,47 @@ describe("EV_VIEW_SHOWN", () => {
181
210
  expect(shown.filter((s) => s.name === "gone_view")).toEqual([]);
182
211
  });
183
212
 
213
+ /* The tree is told by its own route change (each node on the path
214
+ * comes back from ST_OFF to a content it already built), so the
215
+ * shell must not tell it a second time: C_YUI_NODE does not declare
216
+ * EV_VIEW_SHOWN. A view that asks for its data when shown and is
217
+ * busy until the answer took the second one as "action in progress". */
218
+ test("a keep_alive C_YUI_NODE tree tells the content on screen ONCE", () => {
219
+ const shell = new_shell();
220
+ yui_shell_navigate(shell, "/tree/acc");
221
+ expect(shown).toEqual([]);
222
+
223
+ yui_shell_navigate(shell, "/plain");
224
+ yui_shell_navigate(shell, "/tree/acc");
225
+ expect(shown.map((s) => s.kw.route)).toEqual(["/tree/acc"]);
226
+ expect(errors()).toEqual([]);
227
+ });
228
+
229
+ test("the root's own content, and a content deeper down, are told once", () => {
230
+ const shell = new_shell();
231
+ yui_shell_navigate(shell, "/tree");
232
+ yui_shell_navigate(shell, "/plain");
233
+ yui_shell_navigate(shell, "/tree");
234
+ expect(shown.map((s) => s.kw.route)).toEqual(["/tree"]);
235
+
236
+ shown.length = 0;
237
+ yui_shell_navigate(shell, "/tree/deep/leaf");
238
+ expect(shown).toEqual([]);
239
+ yui_shell_navigate(shell, "/plain");
240
+ yui_shell_navigate(shell, "/tree/deep/leaf");
241
+ expect(shown.map((s) => s.kw.route)).toEqual(["/tree/deep/leaf"]);
242
+ expect(errors()).toEqual([]);
243
+ });
244
+
245
+ test("a C_YUI_SERVICE_VIEW content of a node hands it on, once", () => {
246
+ const shell = new_shell();
247
+ yui_shell_navigate(shell, "/tree/svc");
248
+ yui_shell_navigate(shell, "/plain");
249
+ yui_shell_navigate(shell, "/tree/svc");
250
+ expect(shown.map((s) => s.name)).toEqual(["#tree_svc_view"]);
251
+ expect(errors()).toEqual([]);
252
+ });
253
+
184
254
  test("C_YUI_SERVICE_VIEW hands it on to the view it hosts", () => {
185
255
  const shell = new_shell();
186
256
  yui_shell_navigate(shell, "/hosted");
@@ -4,7 +4,8 @@
4
4
  * The html transform of vite-plugin-yuneta-html.js: the policy is
5
5
  * ALWAYS emitted (connect-src 'self' alone without csp_connect_src,
6
6
  * and without a config.json at all), and the text of config.json is
7
- * escaped where it lands in markup.
7
+ * escaped where it lands in markup. And the build stamp written
8
+ * into the emitted index.html: the same bundle, the same stamp.
8
9
  *
9
10
  * Copyright (c) 2026, ArtGins.
10
11
  * All Rights Reserved.
@@ -14,6 +15,7 @@ import fs from "fs";
14
15
  import os from "os";
15
16
  import path from "path";
16
17
  import {yunetaHtmlPlugin} from "../vite-plugin-yuneta-html.js";
18
+ import {parse_build} from "./yui_build_stamp.js";
17
19
 
18
20
  const HTML = "<head><title></title>\n<!-- METADATA_PLACEHOLDER -->\n<!-- CSP_PLACEHOLDER --></head>";
19
21
 
@@ -92,3 +94,34 @@ describe("vite-plugin-yuneta-html", () => {
92
94
  expect(html).not.toContain('wss://a"onload');
93
95
  });
94
96
  });
97
+
98
+ function stamped(names)
99
+ {
100
+ const bundle = {"index.html": {source: "<html><head><title>x</title></head><body></body></html>"}};
101
+ for(const name of names) {
102
+ bundle[name] = {source: ""};
103
+ }
104
+ yunetaHtmlPlugin({}).generateBundle.handler({}, bundle);
105
+ return parse_build(String(bundle["index.html"].source));
106
+ }
107
+
108
+ describe("vite-plugin-yuneta-html: the build stamp", () => {
109
+ test("the emitted page carries the stamp and the app's version", () => {
110
+ const build = stamped(["assets/index-AAAA.js", "assets/index-BBBB.css"]);
111
+ expect(build.stamp).toMatch(/^[0-9a-f]{16}$/);
112
+ const pkg = JSON.parse(fs.readFileSync(path.resolve(process.cwd(), "package.json"), "utf-8"));
113
+ expect(build.version).toBe(pkg.version);
114
+ });
115
+
116
+ test("the same files give the same stamp, in any order; the maps do not count", () => {
117
+ const a = stamped(["assets/a-1.js", "assets/b-2.css"]);
118
+ const b = stamped(["assets/b-2.css", "assets/a-1.js", "assets/a-1.js.map"]);
119
+ expect(b.stamp).toBe(a.stamp);
120
+ });
121
+
122
+ test("one changed chunk gives another stamp", () => {
123
+ const a = stamped(["assets/a-1.js", "assets/lazy-1.js"]);
124
+ const b = stamped(["assets/a-1.js", "assets/lazy-2.js"]);
125
+ expect(b.stamp).not.toBe(a.stamp);
126
+ });
127
+ });
@@ -0,0 +1,125 @@
1
+ /***********************************************************************
2
+ * yui_build_stamp.js
3
+ *
4
+ * Which build is this page, and which build does the server
5
+ * hand out now.
6
+ *
7
+ * The build writes its own identity into index.html as
8
+ *
9
+ * <meta name="yuneta-build" content="<stamp>" data-version="<version>">
10
+ *
11
+ * (vite-plugin-yuneta-html.js, at build time only). The stamp is
12
+ * a hash of the NAMES of every file of the bundle, and those
13
+ * names carry the hash of their content: the same code built
14
+ * twice gives the same stamp, and any change of a chunk, of a
15
+ * lazy chunk or of a stylesheet gives another. A build date
16
+ * would announce a "new version" after every rebuild of
17
+ * unchanged code, and the deploy round rebuilds every app after
18
+ * each publication of this library.
19
+ *
20
+ * The page that runs reads its stamp from its own DOM, where the
21
+ * meta is never touched again; the deployed one is read from the
22
+ * text of the same document fetched again. Pure functions: the
23
+ * gclass that compares them is C_YUI_UPDATE_WATCH.
24
+ *
25
+ * Copyright (c) 2026, ArtGins.
26
+ * All Rights Reserved.
27
+ ***********************************************************************/
28
+
29
+ const BUILD_META_NAME = "yuneta-build";
30
+
31
+ /************************************************************
32
+ * The build of the page that runs: {stamp, version}, or
33
+ * null when its document carries no stamp (`vite dev`, or
34
+ * an app built without the plugin).
35
+ ************************************************************/
36
+ function running_build(doc)
37
+ {
38
+ if(!doc || typeof doc.querySelector !== "function") {
39
+ return null;
40
+ }
41
+ let $meta = doc.querySelector(`meta[name="${BUILD_META_NAME}"]`);
42
+ if(!$meta) {
43
+ return null;
44
+ }
45
+ let stamp = $meta.getAttribute("content") || "";
46
+ if(!stamp) {
47
+ return null;
48
+ }
49
+ return {
50
+ stamp: stamp,
51
+ version: $meta.getAttribute("data-version") || ""
52
+ };
53
+ }
54
+
55
+ /************************************************************
56
+ * The build named by the text of an index.html: {stamp,
57
+ * version}, or null when the text carries no stamp -- an
58
+ * error page, a captive portal, or a build older than the
59
+ * stamp. Null is NOT "another build": a hotel wifi that
60
+ * answers 200 with its own login page must not tell the
61
+ * reader to reload into it.
62
+ ************************************************************/
63
+ function parse_build(html)
64
+ {
65
+ if(typeof html !== "string" || !html) {
66
+ return null;
67
+ }
68
+ let metas = html.match(/<meta\b[^>]*>/gi) || [];
69
+ for(let tag of metas) {
70
+ if(read_html_attr(tag, "name") !== BUILD_META_NAME) {
71
+ continue;
72
+ }
73
+ let stamp = read_html_attr(tag, "content");
74
+ if(!stamp) {
75
+ return null;
76
+ }
77
+ return {
78
+ stamp: stamp,
79
+ version: read_html_attr(tag, "data-version")
80
+ };
81
+ }
82
+ return null;
83
+ }
84
+
85
+ /************************************************************
86
+ * The value of one attribute of one tag's text, quoted
87
+ * with " or ' (the plugin writes ", a minifier may not).
88
+ * The entities the plugin escapes are read back.
89
+ ************************************************************/
90
+ function read_html_attr(tag, name)
91
+ {
92
+ let re = new RegExp(`\\s${name}\\s*=\\s*("([^"]*)"|'([^']*)')`, "i");
93
+ let m = tag.match(re);
94
+ if(!m) {
95
+ return "";
96
+ }
97
+ let value = (m[2] !== undefined) ? m[2] : m[3];
98
+ return value
99
+ .replace(/&quot;/g, "\"")
100
+ .replace(/&lt;/g, "<")
101
+ .replace(/&gt;/g, ">")
102
+ .replace(/&amp;/g, "&");
103
+ }
104
+
105
+ /************************************************************
106
+ * The url to ask for the deployed document: the page's own,
107
+ * without its hash. Every route of these apps is a hash, so
108
+ * this is the deploy root, whatever path the app is served
109
+ * under.
110
+ ************************************************************/
111
+ function build_page_url(loc)
112
+ {
113
+ if(!loc) {
114
+ return "";
115
+ }
116
+ return `${loc.origin || ""}${loc.pathname || "/"}${loc.search || ""}`;
117
+ }
118
+
119
+
120
+ export {
121
+ BUILD_META_NAME,
122
+ running_build,
123
+ parse_build,
124
+ build_page_url,
125
+ };
@@ -0,0 +1,56 @@
1
+ /***********************************************************************
2
+ * yui_build_stamp.test.js
3
+ *
4
+ * The build stamp read back: from the DOM of the page that runs,
5
+ * from the text of the page the server hands out, and NOT from a
6
+ * page that carries none (an error page, a captive portal).
7
+ *
8
+ * Copyright (c) 2026, ArtGins.
9
+ * All Rights Reserved.
10
+ ***********************************************************************/
11
+ import {describe, test, expect} from "vitest";
12
+ import {running_build, parse_build, build_page_url} from "./yui_build_stamp.js";
13
+
14
+ describe("yui_build_stamp", () => {
15
+ test("parse_build reads the stamp and the version", () => {
16
+ const html = '<head><meta charset="utf-8">\n' +
17
+ '<meta name="yuneta-build" content="0123456789abcdef" data-version="2.0.41">\n</head>';
18
+ expect(parse_build(html)).toEqual({stamp: "0123456789abcdef", version: "2.0.41"});
19
+ });
20
+
21
+ test("parse_build takes single quotes and any attribute order", () => {
22
+ const html = "<meta data-version='1.0.0' content='abc' name='yuneta-build'>";
23
+ expect(parse_build(html)).toEqual({stamp: "abc", version: "1.0.0"});
24
+ });
25
+
26
+ test("a page with no stamp is no build, not another build", () => {
27
+ expect(parse_build("<html><head><title>Hotel wifi</title></head></html>")).toBe(null);
28
+ expect(parse_build('<meta name="yuneta-build" content="">')).toBe(null);
29
+ expect(parse_build("")).toBe(null);
30
+ expect(parse_build(null)).toBe(null);
31
+ });
32
+
33
+ test("another meta is not mistaken for the stamp", () => {
34
+ const html = '<meta name="description" content="x"><meta name="yuneta-build" content="s1" data-version="v">';
35
+ expect(parse_build(html)).toEqual({stamp: "s1", version: "v"});
36
+ });
37
+
38
+ test("running_build reads the meta of a document", () => {
39
+ const attrs = {content: "s2", "data-version": "3.1.4"};
40
+ const doc = {
41
+ querySelector: (sel) => sel === 'meta[name="yuneta-build"]'
42
+ ? {getAttribute: (k) => attrs[k] || null}
43
+ : null
44
+ };
45
+ expect(running_build(doc)).toEqual({stamp: "s2", version: "3.1.4"});
46
+ expect(running_build({querySelector: () => null})).toBe(null);
47
+ expect(running_build(null)).toBe(null);
48
+ });
49
+
50
+ test("build_page_url is the page without its hash", () => {
51
+ expect(build_page_url({origin: "https://a.b", pathname: "/", search: "", hash: "#/x/y"}))
52
+ .toBe("https://a.b/");
53
+ expect(build_page_url({origin: "https://a.b", pathname: "/sub/", search: "?q=1"}))
54
+ .toBe("https://a.b/sub/?q=1");
55
+ });
56
+ });
@@ -9,9 +9,21 @@
9
9
  * (always emitted: connect-src 'self' alone when the
10
10
  * list is missing, with a warning; an entry that is
11
11
  * not one CSP source -- a `;`, `,` or space -- is refused)
12
+ * - at build time only, the build stamp read by
13
+ * C_YUI_UPDATE_WATCH (src/yui_build_stamp.js):
14
+ * <meta name="yuneta-build" content="<stamp>" data-version="<version>">
15
+ * <stamp> is a hash of the names of every file of the
16
+ * bundle -- names that carry the hash of their content --
17
+ * so the same code built twice has the same stamp;
18
+ * <version> is the app's package.json version
12
19
  *
13
- * Usage in vite.config.js (vendored copy):
14
- * import { yunetaHtmlPlugin } from "./src/gobj-ui/vite-plugin-yuneta-html.js";
20
+ * The title, the metadata and the policy are written before
21
+ * Vite's own transforms ("pre"); the stamp needs the finished
22
+ * bundle, so it is written into the emitted index.html in
23
+ * generateBundle, after every other plugin ("post").
24
+ *
25
+ * Usage in vite.config.js:
26
+ * import { yunetaHtmlPlugin } from "@yuneta/gobj-ui/vite-plugin-yuneta-html.js";
15
27
  * plugins: [ yunetaHtmlPlugin({ defaultTitle: "My App" }) ]
16
28
  *
17
29
  * Copyright (c) 2025-2026, ArtGins.
@@ -19,6 +31,7 @@
19
31
  ***********************************************************************/
20
32
  import fs from "fs";
21
33
  import path from "path";
34
+ import {createHash} from "crypto";
22
35
 
23
36
  /* config.json is build input, but its text lands inside markup. */
24
37
  function escape_html(value) {
@@ -157,6 +170,51 @@ export function yunetaHtmlPlugin(options = {}) {
157
170
  .replace("<!-- METADATA_PLACEHOLDER -->", metadataHtml)
158
171
  .replace("<!-- CSP_PLACEHOLDER -->", cspHtml);
159
172
  }
173
+ },
174
+
175
+ /*------------------------------------------*
176
+ * Build stamp (see the header)
177
+ *
178
+ * The names of the bundle's files are known
179
+ * only once it is complete. A source map is
180
+ * left out: with `sourcemap: "hidden"` its
181
+ * name only repeats its chunk's.
182
+ *------------------------------------------*/
183
+ generateBundle: {
184
+ order: "post",
185
+ handler(output_options, bundle) {
186
+ const names = Object.keys(bundle)
187
+ .filter(name => !name.endsWith(".map") && !name.endsWith(".html"))
188
+ .sort();
189
+ const pages = Object.keys(bundle).filter(name => name.endsWith(".html"));
190
+ if (names.length === 0 || pages.length === 0) {
191
+ console.error("❌ yuneta-build-stamp: no bundle or no page, no build stamp written");
192
+ return;
193
+ }
194
+ const stamp = createHash("sha256")
195
+ .update(names.join("\n"))
196
+ .digest("hex")
197
+ .slice(0, 16);
198
+
199
+ let version = "";
200
+ const pkgPath = path.resolve(process.cwd(), "package.json");
201
+ if (fs.existsSync(pkgPath)) {
202
+ version = JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version || "";
203
+ }
204
+ const meta = `<meta name="yuneta-build" content="${stamp}" ` +
205
+ `data-version="${escape_html(version)}">`;
206
+
207
+ for (const name of pages) {
208
+ const page = bundle[name];
209
+ const html = String(page.source);
210
+ if (!html.includes("</head>")) {
211
+ console.error(`❌ yuneta-build-stamp: ${name} has no </head>, no build stamp written`);
212
+ continue;
213
+ }
214
+ page.source = html.replace("</head>", ` ${meta}\n </head>`);
215
+ }
216
+ }
160
217
  }
161
218
  };
162
219
  }
220
+