jsgui3-server 0.0.150 → 0.0.152

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 (86) hide show
  1. package/.github/instructions/copilot.instructions.md +1 -0
  2. package/AGENTS.md +2 -0
  3. package/README.md +89 -13
  4. package/admin-ui/v1/controls/admin_shell.js +702 -669
  5. package/admin-ui/v1/server.js +14 -1
  6. package/docs/api-reference.md +504 -306
  7. package/docs/books/creating-a-new-admin-ui/README.md +20 -20
  8. package/docs/books/website-design/01-introduction.md +73 -0
  9. package/docs/books/website-design/02-current-state.md +195 -0
  10. package/docs/books/website-design/03-base-class.md +181 -0
  11. package/docs/books/website-design/04-webpage.md +307 -0
  12. package/docs/books/website-design/05-website.md +456 -0
  13. package/docs/books/website-design/06-pages-storage.md +170 -0
  14. package/docs/books/website-design/07-api-layer.md +285 -0
  15. package/docs/books/website-design/08-server-integration.md +271 -0
  16. package/docs/books/website-design/09-cross-agent-review.md +190 -0
  17. package/docs/books/website-design/10-open-questions.md +196 -0
  18. package/docs/books/website-design/11-converged-recommendation.md +205 -0
  19. package/docs/books/website-design/12-content-model.md +395 -0
  20. package/docs/books/website-design/13-webpage-module-spec.md +404 -0
  21. package/docs/books/website-design/14-website-module-spec.md +541 -0
  22. package/docs/books/website-design/15-multi-repo-plan.md +275 -0
  23. package/docs/books/website-design/16-minimal-first.md +203 -0
  24. package/docs/books/website-design/17-implementation-report-codex.md +81 -0
  25. package/docs/books/website-design/README.md +43 -0
  26. package/docs/comprehensive-documentation.md +220 -220
  27. package/docs/configuration-reference.md +281 -204
  28. package/docs/middleware-guide.md +236 -0
  29. package/docs/proposals/jsgui3-website-and-webpage-design-jsgui3-server-support.md +257 -0
  30. package/docs/proposals/jsgui3-website-and-webpage-design-review.md +73 -0
  31. package/docs/proposals/jsgui3-website-and-webpage-design.md +732 -0
  32. package/docs/swagger.md +316 -0
  33. package/docs/system-architecture.md +24 -18
  34. package/examples/controls/1) window/server.js +6 -1
  35. package/examples/controls/21) mvvm and declarative api/check.js +94 -0
  36. package/examples/controls/21) mvvm and declarative api/check_output.txt +25 -0
  37. package/examples/controls/21) mvvm and declarative api/check_output_2.txt +27 -0
  38. package/examples/controls/21) mvvm and declarative api/client.js +241 -0
  39. declarative api/e2e-screenshot-1-name-change.png +0 -0
  40. declarative api/e2e-screenshot-2-toggled.png +0 -0
  41. declarative api/e2e-screenshot-3-final.png +0 -0
  42. declarative api/e2e-screenshot-final.png +0 -0
  43. package/examples/controls/21) mvvm and declarative api/e2e-test.js +175 -0
  44. package/examples/controls/21) mvvm and declarative api/out.html +1 -0
  45. package/examples/controls/21) mvvm and declarative api/page_out.html +1 -0
  46. package/examples/controls/21) mvvm and declarative api/server.js +18 -0
  47. package/examples/data-views/01) query-endpoint/server.js +61 -0
  48. package/labs/website-design/001-base-class-overhead/check.js +162 -0
  49. package/labs/website-design/002-pages-storage/check.js +244 -0
  50. package/labs/website-design/002-pages-storage/results.txt +0 -0
  51. package/labs/website-design/003-type-detection/check.js +193 -0
  52. package/labs/website-design/003-type-detection/results.txt +0 -0
  53. package/labs/website-design/004-two-stage-validation/check.js +314 -0
  54. package/labs/website-design/004-two-stage-validation/results.txt +0 -0
  55. package/labs/website-design/005-normalize-input/check.js +303 -0
  56. package/labs/website-design/006-serve-website-spike/check.js +290 -0
  57. package/labs/website-design/README.md +34 -0
  58. package/labs/website-design/manifest.json +68 -0
  59. package/labs/website-design/run-all.js +60 -0
  60. package/middleware/compression.js +217 -0
  61. package/middleware/index.js +15 -0
  62. package/middleware/json-body.js +126 -0
  63. package/module.js +3 -0
  64. package/openapi.js +474 -0
  65. package/package.json +11 -8
  66. package/publishers/Publishers.js +6 -5
  67. package/publishers/http-function-publisher.js +135 -126
  68. package/publishers/http-webpage-publisher.js +89 -11
  69. package/publishers/query-publisher.js +116 -0
  70. package/publishers/swagger-publisher.js +203 -0
  71. package/publishers/swagger-ui.js +578 -0
  72. package/resources/adapters/array-adapter.js +143 -0
  73. package/resources/query-resource.js +131 -0
  74. package/serve-factory.js +756 -18
  75. package/server.js +502 -123
  76. package/tests/README.md +23 -1
  77. package/tests/admin-ui-jsgui-controls.test.js +16 -1
  78. package/tests/helpers/playwright-e2e-harness.js +326 -0
  79. package/tests/openapi.test.js +319 -0
  80. package/tests/playwright-smoke.test.js +134 -0
  81. package/tests/publish-enhancements.test.js +673 -0
  82. package/tests/query-publisher.test.js +430 -0
  83. package/tests/quick-json-body-test.js +169 -0
  84. package/tests/serve.test.js +425 -122
  85. package/tests/swagger-publisher.test.js +1076 -0
  86. package/tests/test-runner.js +1 -0
@@ -0,0 +1,307 @@
1
+ # Chapter 4: Designing the Webpage
2
+
3
+ A `Webpage` represents a single page in the abstract. It holds everything needed to describe what the page *is*, without knowing how to serve it. This chapter explores three approaches with increasing richness.
4
+
5
+ ---
6
+
7
+ ## What properties should a Webpage have?
8
+
9
+ Before choosing an implementation, we need to agree on what a page knows about itself.
10
+
11
+ ### Essential properties
12
+
13
+ | Property | Type | Purpose |
14
+ |----------|------|---------|
15
+ | `name` | `string` | Human-readable identifier |
16
+ | `title` | `string` | HTML `<title>` tag content |
17
+ | `path` | `string` | URL route (e.g. `/about`) |
18
+ | `content` | `Control` or constructor | The page body |
19
+ | `meta` | `object` | SEO and social metadata |
20
+
21
+ ### Extended properties
22
+
23
+ | Property | Type | Purpose |
24
+ |----------|------|---------|
25
+ | `client_js` | `string` | Path to client-side JS entry point |
26
+ | `favicon` | `string` | Page-specific favicon |
27
+ | `scripts` | `string[]` | Additional script tags |
28
+ | `stylesheets` | `string[]` | Additional stylesheet links |
29
+
30
+ ### Debatable properties
31
+
32
+ | Property | Type | Discussion |
33
+ |----------|------|-----------|
34
+ | `render_mode` | `'static'` or `'dynamic'` | See discussion below |
35
+ | `cache_policy` | `object` | Cache headers — abstraction or server detail? |
36
+ | `route_priority` | `number` | Ordering hint — useful or over-engineering? |
37
+
38
+ ### Should `render_mode` live on the page?
39
+
40
+ The question: is rendering strategy a page concern or a server concern?
41
+
42
+ **The case for "server concern"**: The server decides how to bundle and serve. A page author shouldn't need to think about pre-rendering vs. per-request rendering — that's infrastructure. Keeping `render_mode` off the page keeps the page purely descriptive.
43
+
44
+ **The case for "page concern"**: Some pages *inherently* need dynamic rendering — a user profile that changes per-request can't be pre-rendered. The page author knows this; the server doesn't. Without a `render_mode` hint, the server either guesses (fragile) or forces all pages through the same pipeline (limiting).
45
+
46
+ **The deciding argument**: Today, `is_dynamic` (Approach B below) infers rendering strategy from the content type — `typeof content === 'function'` means dynamic. But this conflates "content is a class constructor" with "content must be rendered per-request." A control constructor might produce static output that's identical for every visitor. An explicit `render_mode` separates the *how to serve* question from the *what is the content* question.
47
+
48
+ **Resolution**: `render_mode` belongs on the Webpage as an **optional, recommended** field. It defaults to `undefined` (server decides based on content type, matching current behavior). When explicitly set, it overrides the server's inference. This is additive — existing code that doesn't set `render_mode` works exactly as before.
49
+
50
+ ---
51
+
52
+ ## Approach A: Enhanced Property Bag
53
+
54
+ The simplest evolution of the current skeleton. Explicitly declare known properties instead of blindly `Object.assign`.
55
+
56
+ ```js
57
+ const { Evented_Class } = require('jsgui3-html');
58
+
59
+ class Webpage extends Evented_Class {
60
+ constructor(spec = {}) {
61
+ super();
62
+
63
+ this.name = spec.name || undefined;
64
+ this.title = spec.title || undefined;
65
+ this.path = spec.path; // No default — explicit is safer
66
+ this.content = spec.content || undefined;
67
+ this.meta = spec.meta || {};
68
+ this.client_js = spec.client_js || undefined;
69
+ }
70
+ }
71
+
72
+ module.exports = Webpage;
73
+ ```
74
+
75
+ ### Discussion
76
+
77
+ **What's good about this:**
78
+ - Dead simple — 13 lines of actual code
79
+ - Every property is visible in the constructor — excellent for discoverability
80
+ - Extends `Evented_Class` so lifecycle events are available if needed
81
+ - No default `path` — in a multi-page website, defaulting to `'/'` could silently create collisions
82
+
83
+ **What's missing:**
84
+ - No validation — `content` could be a string, a number, anything
85
+ - No computed properties — can't ask "does this page have content?"
86
+ - No serialization — no `toJSON()` for admin/tooling introspection
87
+ - No way to tell if a spec property was explicitly set vs. just absent
88
+
89
+ **Best for:** Getting started quickly. You can always add methods later without breaking anything because the base class is right.
90
+
91
+ ---
92
+
93
+ ## Approach B: With Computed Properties and Introspection
94
+
95
+ Adds helpers that make the Webpage useful to servers, admin UIs, and tooling.
96
+
97
+ ```js
98
+ const { Evented_Class } = require('jsgui3-html');
99
+
100
+ class Webpage extends Evented_Class {
101
+ constructor(spec = {}) {
102
+ super();
103
+
104
+ this.name = spec.name || undefined;
105
+ this.title = spec.title || undefined;
106
+ this.path = spec.path;
107
+ this.content = spec.content || undefined;
108
+ this.meta = spec.meta || {};
109
+ this.client_js = spec.client_js || undefined;
110
+ this.favicon = spec.favicon || undefined;
111
+ this.scripts = spec.scripts || [];
112
+ this.stylesheets = spec.stylesheets || [];
113
+ }
114
+
115
+ /** Does this page have renderable content? */
116
+ get has_content() {
117
+ return this.content !== undefined && this.content !== null;
118
+ }
119
+
120
+ /**
121
+ * Is the content a constructor/function (needs instantiation)
122
+ * vs. an existing instance?
123
+ */
124
+ get is_dynamic() {
125
+ return typeof this.content === 'function';
126
+ }
127
+
128
+ /** Stable, deterministic serialization for admin/tooling */
129
+ toJSON() {
130
+ return {
131
+ name: this.name,
132
+ title: this.title,
133
+ path: this.path,
134
+ has_content: this.has_content,
135
+ is_dynamic: this.is_dynamic,
136
+ meta: this.meta
137
+ };
138
+ }
139
+ }
140
+
141
+ module.exports = Webpage;
142
+ ```
143
+
144
+ ### Discussion
145
+
146
+ **What's good about this:**
147
+
148
+ - `has_content` and `is_dynamic` answer questions that publishers currently figure out themselves — moves that logic to the source of truth
149
+ - `toJSON()` gives admin UIs and diagnostic tools a stable contract to rely on — they don't need to understand the class internals
150
+ - `scripts` and `stylesheets` arrays let pages declare additional assets beyond what the bundler produces — useful for third-party libraries, analytics scripts, etc.
151
+ - Still very readable — the computed properties are clearly getters, not hidden magic
152
+
153
+ **What's debatable:**
154
+
155
+ - Should `toJSON()` include `content`? The content is usually a class constructor, which doesn't serialize. Including it could cause confusion. Excluding it means the JSON is a summary, not a full representation.
156
+ - Are `scripts` and `stylesheets` page concerns or server concerns? Today the server/publisher decides what goes into the HTML `<head>`. Moving that to the page definition shifts responsibility.
157
+ - `is_dynamic` currently means "content is a function" — but the term "dynamic" could also mean "rendered per-request." The naming might be confusing when `render_mode` enters the picture.
158
+
159
+ **Best for:** A practical middle ground that's immediately useful without over-engineering.
160
+
161
+ ---
162
+
163
+ ## Approach C: With Validation and Lifecycle
164
+
165
+ Adds input validation and lifecycle semantics.
166
+
167
+ ```js
168
+ const { Evented_Class, tof } = require('jsgui3-html');
169
+
170
+ class Webpage extends Evented_Class {
171
+ constructor(spec = {}) {
172
+ super();
173
+
174
+ // Validate path format
175
+ if (spec.path !== undefined) {
176
+ if (typeof spec.path !== 'string') {
177
+ throw new TypeError(`Webpage path must be a string, got ${tof(spec.path)}`);
178
+ }
179
+ // Normalize: ensure leading slash
180
+ this.path = spec.path.startsWith('/') ? spec.path : '/' + spec.path;
181
+ } else {
182
+ this.path = undefined;
183
+ }
184
+
185
+ // Validate content type
186
+ if (spec.content !== undefined) {
187
+ const ct = tof(spec.content);
188
+ if (ct !== 'function' && ct !== 'object') {
189
+ throw new TypeError(
190
+ `Webpage content must be a Control constructor or instance, got ${ct}`
191
+ );
192
+ }
193
+ }
194
+
195
+ this.name = spec.name || undefined;
196
+ this.title = spec.title || undefined;
197
+ this.content = spec.content || undefined;
198
+ this.meta = spec.meta || {};
199
+ this.client_js = spec.client_js || undefined;
200
+ this.favicon = spec.favicon || undefined;
201
+ this.scripts = spec.scripts || [];
202
+ this.stylesheets = spec.stylesheets || [];
203
+
204
+ this._finalized = false;
205
+ }
206
+
207
+ get has_content() {
208
+ return this.content !== undefined && this.content !== null;
209
+ }
210
+
211
+ get is_dynamic() {
212
+ return typeof this.content === 'function';
213
+ }
214
+
215
+ /**
216
+ * Mark this webpage as finalized — no further mutations expected.
217
+ * Called by the publisher before bundling. Optional but useful
218
+ * for catching accidental late modifications.
219
+ */
220
+ finalize() {
221
+ if (this._finalized) return this;
222
+ this._finalized = true;
223
+ this.raise('finalized');
224
+ return this;
225
+ }
226
+
227
+ get finalized() {
228
+ return this._finalized;
229
+ }
230
+
231
+ toJSON() {
232
+ return {
233
+ name: this.name,
234
+ title: this.title,
235
+ path: this.path,
236
+ has_content: this.has_content,
237
+ is_dynamic: this.is_dynamic,
238
+ finalized: this._finalized,
239
+ meta: this.meta
240
+ };
241
+ }
242
+ }
243
+
244
+ module.exports = Webpage;
245
+ ```
246
+
247
+ ### Discussion
248
+
249
+ **What's good about this:**
250
+
251
+ - **Path normalization** — ensures leading slash, catches non-string paths early. Today a missing `'/'` prefix would silently create a broken route.
252
+ - **Content validation** — catches `content: 42` or `content: 'whoops'` at construction time instead of deep in the publisher pipeline where the error is confusing.
253
+ - **`finalize()` lifecycle** — this was suggested by the OpenAI reviewer as an alternative to full reactivity. The idea: a Webpage is mutable during composition and read-mostly after `finalize()` is called. This gives publishers confidence that the page won't change under them without requiring a full event system for every property.
254
+ - Uses `tof()` from jsgui3-html for type checking — ecosystem-consistent.
255
+
256
+ **What's debatable:**
257
+
258
+ - Is validation in the constructor too strict? If someone passes `content: null` temporarily and sets it later, the validation could get in the way. Counter-argument: if content is set later, `null` would pass validation (only non-null non-function/object types are rejected).
259
+ - `finalize()` is a new concept not used elsewhere in jsgui3. It adds cognitive load. Counter-argument: it's simple enough to ignore — just don't call it.
260
+ - Path normalization makes an opinionated choice. What about root-relative vs. absolute paths? What about paths with query strings or fragments?
261
+
262
+ **Best for:** When you want construction-time safety and a clear lifecycle boundary.
263
+
264
+ ---
265
+
266
+ ## Comparison
267
+
268
+ | Criterion | A (Property Bag) | B (Computed + toJSON) | C (Validation + Lifecycle) |
269
+ |---|:---:|:---:|:---:|
270
+ | Lines of code | ~13 | ~40 | ~65 |
271
+ | Input validation | ☆ | ☆ | ★★★ |
272
+ | Introspection | ☆ | ★★★ | ★★★ |
273
+ | Lifecycle support | ☆ | ☆ | ★★ |
274
+ | Ease of understanding | ★★★ | ★★★ | ★★☆ |
275
+ | Catches bugs early | ☆ | ★ | ★★★ |
276
+ | Can evolve into C | ★★★ | ★★★ | — |
277
+
278
+ Note that all three extend `Evented_Class`, so they all have event capability. The difference is in how much the class *does* with that capability.
279
+
280
+ ---
281
+
282
+ ## Should `Object.assign(this, spec)` stay?
283
+
284
+ The current code does `Object.assign(this, spec)`, which copies ALL spec properties onto the instance — including unknown/unexpected ones. This is a double-edged sword:
285
+
286
+ **For**: Maximum flexibility. Users can put anything in spec and it "just works." Good for rapid prototyping and forward compatibility (adding new fields doesn't require class changes).
287
+
288
+ **Against**: No discoverability. Typos in property names silently create wrong properties. No way to enumerate "known" vs. "extra" properties. Makes the class contract unclear.
289
+
290
+ **A middle ground**: Explicitly assign known properties, then optionally store extras:
291
+
292
+ ```js
293
+ // Known properties
294
+ this.name = spec.name;
295
+ this.title = spec.title;
296
+ // ...
297
+
298
+ // Store any additional spec properties for extensibility
299
+ this.extra = {};
300
+ for (const key of Object.keys(spec)) {
301
+ if (!['name', 'title', 'path', 'content', 'meta', 'client_js'].includes(key)) {
302
+ this.extra[key] = spec[key];
303
+ }
304
+ }
305
+ ```
306
+
307
+ This preserves flexibility while making the primary contract explicit. Whether this is worth the complexity depends on how important forward compatibility is.