jskelet 0.6.3 → 0.6.4
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/AGENTS.md +136 -136
- package/CHANGELOG.md +628 -620
- package/LICENSE +21 -21
- package/README.md +2 -0
- package/bin/jskelet.mjs +130 -130
- package/docs/01-baslangic.md +291 -291
- package/docs/02-mimari.md +310 -310
- package/docs/03-routing.md +515 -515
- package/docs/04-render-ve-sablonlar.md +667 -661
- package/docs/05-islands.md +486 -486
- package/docs/06-cache.md +1467 -1443
- package/docs/07-yapilandirma.md +1208 -1197
- package/docs/08-build.md +429 -429
- package/docs/09-dev-araclari.md +364 -364
- package/docs/10-dagitim.md +348 -338
- package/docs/12-panel-ve-oturum.md +479 -478
- package/docs/README.md +83 -83
- package/docs/en/01-getting-started.md +298 -298
- package/docs/en/02-architecture.md +329 -329
- package/docs/en/03-routing.md +531 -531
- package/docs/en/04-rendering.md +675 -669
- package/docs/en/05-islands.md +497 -497
- package/docs/en/06-caching.md +1476 -1453
- package/docs/en/07-configuration.md +1229 -1219
- package/docs/en/08-build.md +447 -447
- package/docs/en/09-dev-tools.md +373 -373
- package/docs/en/10-deployment.md +351 -340
- package/docs/en/11-migration.md +398 -398
- package/docs/en/12-dashboards-and-sessions.md +489 -488
- package/docs/en/README.md +87 -87
- package/package.json +137 -137
- package/src/build/ensure-build.mjs +19 -19
- package/src/build/paths.mjs +153 -153
- package/src/build/resolve-peer.mjs +36 -36
- package/src/build/tasks/client.mjs +349 -349
- package/src/build/tasks/css.mjs +235 -235
- package/src/build/tasks/fonts.mjs +146 -146
- package/src/build/tasks/icons.mjs +357 -357
- package/src/build/tasks/images.mjs +244 -244
- package/src/build/tasks/precompress.mjs +78 -78
- package/src/build/tasks/templates.mjs +20 -20
- package/src/client/admin/i18n.js +764 -764
- package/src/client/admin/login.html +74 -74
- package/src/client/admin/panel.css +809 -809
- package/src/client/admin/panel.html +495 -495
- package/src/client/admin/panel.js +1251 -1251
- package/src/client/devtools/report.html +185 -185
- package/src/client/devtools/report.js +745 -745
- package/src/client/devtools/seo.js +628 -628
- package/src/client/dom.js +95 -95
- package/src/client/form.js +192 -192
- package/src/client/index.js +45 -45
- package/src/client/registry.js +305 -305
- package/src/client/safe-image.js +91 -91
- package/src/client/shared-cookie.js +225 -225
- package/src/client/store.js +36 -36
- package/src/client/swap.js +188 -188
- package/src/compile/codegen.js +336 -336
- package/src/compile/compile-all.js +149 -149
- package/src/compile/errors.js +66 -66
- package/src/compile/expr.js +409 -409
- package/src/compile/index.js +17 -17
- package/src/compile/parse.js +541 -541
- package/src/compile/resolve.js +211 -211
- package/src/compile/scan-exports.js +51 -51
- package/src/config/defaults.js +541 -534
- package/src/config/index.js +1500 -1469
- package/src/config/pattern.js +107 -107
- package/src/generate.mjs +163 -163
- package/src/http/control-flow.js +71 -71
- package/src/http/cookies-entry.js +21 -21
- package/src/http/cookies.js +277 -277
- package/src/http/request-cache.js +46 -46
- package/src/http/request-context.js +165 -165
- package/src/http/shared-cookie.js +178 -178
- package/src/index.js +101 -101
- package/src/init.mjs +232 -230
- package/src/migrate/apply.mjs +262 -262
- package/src/migrate/babel.mjs +79 -79
- package/src/migrate/classify.mjs +155 -155
- package/src/migrate/config.mjs +126 -126
- package/src/migrate/fs-walk.mjs +191 -191
- package/src/migrate/parse.mjs +26 -26
- package/src/migrate/scan.mjs +177 -177
- package/src/migrate/transform/expr-source.mjs +168 -168
- package/src/migrate/transform/island.mjs +67 -67
- package/src/migrate/transform/jsx-to-component.mjs +302 -302
- package/src/migrate/transform/jsx-to-jsk.mjs +330 -330
- package/src/migrate/transform/page-split.mjs +435 -435
- package/src/migrate/write.mjs +81 -81
- package/src/migrate.mjs +171 -171
- package/src/runtime/alias-hooks.mjs +119 -119
- package/src/runtime/register.mjs +4 -4
- package/src/server/admin/actions.js +229 -229
- package/src/server/admin/auth.js +125 -125
- package/src/server/admin/event-log.js +151 -151
- package/src/server/admin/gate.js +209 -209
- package/src/server/admin/inventory.js +188 -188
- package/src/server/admin/mount.js +56 -56
- package/src/server/admin/router.js +216 -216
- package/src/server/admin/snapshot.js +241 -241
- package/src/server/assets.js +147 -147
- package/src/server/auth/handoff.js +309 -309
- package/src/server/cache-blob.js +70 -70
- package/src/server/cache-control.js +45 -0
- package/src/server/cache-deps.js +42 -42
- package/src/server/cache-vary.js +113 -113
- package/src/server/cloudflare.js +607 -607
- package/src/server/create-app.js +366 -366
- package/src/server/data-cache.js +553 -553
- package/src/server/dev/report.js +485 -485
- package/src/server/dev/socket.js +170 -170
- package/src/server/dev/version-check.mjs +139 -139
- package/src/server/disk-cache.js +233 -233
- package/src/server/ejs-adapter.js +59 -59
- package/src/server/html-cache.js +1196 -1196
- package/src/server/image-optimizer.js +500 -500
- package/src/server/logs/access-middleware.js +66 -66
- package/src/server/logs/file-sink.js +193 -193
- package/src/server/logs/pipeline.js +165 -165
- package/src/server/logs/s3-put.js +214 -214
- package/src/server/logs/s3-sink.js +112 -112
- package/src/server/metadata.js +102 -102
- package/src/server/middleware/compression.js +205 -205
- package/src/server/middleware/csrf.js +134 -134
- package/src/server/middleware/dev-gate.js +75 -75
- package/src/server/middleware/headers.js +37 -37
- package/src/server/middleware/redirects.js +32 -32
- package/src/server/middleware/robots-txt.js +341 -341
- package/src/server/middleware/static-precompressed.js +121 -121
- package/src/server/middleware/trailing-slash.js +53 -53
- package/src/server/middleware/upstream-proxy.js +141 -141
- package/src/server/og-image.js +369 -356
- package/src/server/port-guard.js +255 -255
- package/src/server/prewarm.js +1082 -1082
- package/src/server/redis.js +588 -588
- package/src/server/render.js +910 -910
- package/src/server/router.js +157 -157
- package/src/server/status-page.js +265 -265
- package/src/server/upstream-limiter.js +376 -376
- package/src/server/upstream-tracking.js +166 -166
- package/src/shared/cookie-domain.js +66 -66
- package/src/start.mjs +22 -22
- package/src/templates/layout.ejs +30 -30
- package/src/templates/layout.jsk +30 -30
- package/src/version.mjs +31 -31
- package/src/views/components/loader.js +101 -101
- package/src/views/helpers/html.js +102 -102
- package/src/views/helpers/tags.js +375 -375
- package/types/config/defaults.d.ts +6 -0
- package/types/config/index.d.ts +6 -0
- package/types/server/cache-control.d.ts +28 -0
- package/types/server/og-image.d.ts +5 -0
|
@@ -1,488 +1,489 @@
|
|
|
1
|
-
# 12 — Dashboards, sessions and per-visitor pages
|
|
2
|
-
|
|
3
|
-
JSkelet's center of gravity is public, cacheable pages. A dashboard sits on the
|
|
4
|
-
opposite axis: different HTML for every visitor, no cache, heavy interaction.
|
|
5
|
-
This document covers that axis — `private: true`, session cookies, CSRF,
|
|
6
|
-
fragment endpoints and region swapping.
|
|
7
|
-
|
|
8
|
-
One thing is deliberately out of scope: **live data transport**. Choosing
|
|
9
|
-
between SSE, WebSocket and polling belongs to the application; the framework
|
|
10
|
-
only provides the "refresh this region from the server" step. The compression
|
|
11
|
-
layer skips `text/event-stream` responses and streams that write their own
|
|
12
|
-
headers, so wiring up SSE by hand does not break anything either.
|
|
13
|
-
|
|
14
|
-
The runnable counterpart is `examples/dashboard/`, which is where the snippets
|
|
15
|
-
below come from.
|
|
16
|
-
|
|
17
|
-
## Why a separate path is needed
|
|
18
|
-
|
|
19
|
-
The HTML cache key is only the path and the query string:
|
|
20
|
-
|
|
21
|
-
```
|
|
22
|
-
`${req.path}?${new URLSearchParams(query).toString()}`
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
Identity is not part of the key. So if a session-dependent page is registered
|
|
26
|
-
with a plain `route()`, the first visitor's HTML is served to **everyone** for
|
|
27
|
-
the length of the TTL. Nothing about this mistake fails loudly: the page works,
|
|
28
|
-
the tests pass, and the problem only appears once a second user arrives —
|
|
29
|
-
usually in production.
|
|
30
|
-
|
|
31
|
-
## `private: true`
|
|
32
|
-
|
|
33
|
-
```js
|
|
34
|
-
export default function register(app, { route, redirect }) {
|
|
35
|
-
app.get(
|
|
36
|
-
"/dashboard",
|
|
37
|
-
route(
|
|
38
|
-
async ({ req }) => {
|
|
39
|
-
const user = currentUser(req);
|
|
40
|
-
if (!user) redirect("/sign-in?next=%2Fdashboard");
|
|
41
|
-
|
|
42
|
-
return { view: "pages/overview", data: { user } };
|
|
43
|
-
},
|
|
44
|
-
{ private: true },
|
|
45
|
-
),
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
What the flag does:
|
|
51
|
-
|
|
52
|
-
| Behaviour | Public `route()` | `private: true` |
|
|
53
|
-
| --- | --- | --- |
|
|
54
|
-
| HTML cache | On when a TTL exists | Off, cannot be turned on |
|
|
55
|
-
| `cache.html` pattern | Overrides the TTL | Ignored |
|
|
56
|
-
| `Cache-Control` | `public,
|
|
57
|
-
| `
|
|
58
|
-
|
|
|
59
|
-
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
- `req.
|
|
78
|
-
- `
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
the
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
signature
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
the
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
`
|
|
172
|
-
`
|
|
173
|
-
`
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
`
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
`
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
call
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
region
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
<
|
|
383
|
-
</
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
-
|
|
391
|
-
- **
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
)
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
*
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
- [ ]
|
|
481
|
-
- [ ]
|
|
482
|
-
- [ ]
|
|
483
|
-
- [ ]
|
|
484
|
-
- [ ]
|
|
485
|
-
- [ ] `
|
|
486
|
-
- [ ] `
|
|
487
|
-
- [ ]
|
|
488
|
-
|
|
1
|
+
# 12 — Dashboards, sessions and per-visitor pages
|
|
2
|
+
|
|
3
|
+
JSkelet's center of gravity is public, cacheable pages. A dashboard sits on the
|
|
4
|
+
opposite axis: different HTML for every visitor, no cache, heavy interaction.
|
|
5
|
+
This document covers that axis — `private: true`, session cookies, CSRF,
|
|
6
|
+
fragment endpoints and region swapping.
|
|
7
|
+
|
|
8
|
+
One thing is deliberately out of scope: **live data transport**. Choosing
|
|
9
|
+
between SSE, WebSocket and polling belongs to the application; the framework
|
|
10
|
+
only provides the "refresh this region from the server" step. The compression
|
|
11
|
+
layer skips `text/event-stream` responses and streams that write their own
|
|
12
|
+
headers, so wiring up SSE by hand does not break anything either.
|
|
13
|
+
|
|
14
|
+
The runnable counterpart is `examples/dashboard/`, which is where the snippets
|
|
15
|
+
below come from.
|
|
16
|
+
|
|
17
|
+
## Why a separate path is needed
|
|
18
|
+
|
|
19
|
+
The HTML cache key is only the path and the query string:
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
`${req.path}?${new URLSearchParams(query).toString()}`
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Identity is not part of the key. So if a session-dependent page is registered
|
|
26
|
+
with a plain `route()`, the first visitor's HTML is served to **everyone** for
|
|
27
|
+
the length of the TTL. Nothing about this mistake fails loudly: the page works,
|
|
28
|
+
the tests pass, and the problem only appears once a second user arrives —
|
|
29
|
+
usually in production.
|
|
30
|
+
|
|
31
|
+
## `private: true`
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
export default function register(app, { route, redirect }) {
|
|
35
|
+
app.get(
|
|
36
|
+
"/dashboard",
|
|
37
|
+
route(
|
|
38
|
+
async ({ req }) => {
|
|
39
|
+
const user = currentUser(req);
|
|
40
|
+
if (!user) redirect("/sign-in?next=%2Fdashboard");
|
|
41
|
+
|
|
42
|
+
return { view: "pages/overview", data: { user } };
|
|
43
|
+
},
|
|
44
|
+
{ private: true },
|
|
45
|
+
),
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
What the flag does:
|
|
51
|
+
|
|
52
|
+
| Behaviour | Public `route()` | `private: true` |
|
|
53
|
+
| --- | --- | --- |
|
|
54
|
+
| HTML cache | On when a TTL exists | Off, cannot be turned on |
|
|
55
|
+
| `cache.html` pattern | Overrides the TTL | Ignored |
|
|
56
|
+
| `Cache-Control` | `public, max-age=0` | `private, no-store` |
|
|
57
|
+
| `CDN-Cache-Control` | `max-age=<ttl>, stale-while-revalidate=…` | Not written |
|
|
58
|
+
| `Vary` | `Accept-Encoding` | `Cookie, Accept-Encoding` |
|
|
59
|
+
| ETag | Present | Absent |
|
|
60
|
+
| `X-JSkelet-Cache` | `HIT`/`STALE`/`MISS` | Not written |
|
|
61
|
+
|
|
62
|
+
Dropping the ETag looks like a detail but is not: a strong ETag over a
|
|
63
|
+
per-visitor body is a fingerprint of that visitor, and a layer that ignores
|
|
64
|
+
`no-store` could use it to tell them apart.
|
|
65
|
+
|
|
66
|
+
A redirect thrown from a per-visitor page is not cacheable either. "You need to
|
|
67
|
+
sign in" is a session-dependent decision; storing it means sending signed-in
|
|
68
|
+
users to the sign-in page too.
|
|
69
|
+
|
|
70
|
+
## When you forget the flag
|
|
71
|
+
|
|
72
|
+
The framework watches for reads that touch identity. The `req` passed to the
|
|
73
|
+
controller is wrapped in a thin Proxy that marks these accesses:
|
|
74
|
+
|
|
75
|
+
- `req.headers.cookie`, `req.headers.authorization`,
|
|
76
|
+
`req.headers["proxy-authorization"]`
|
|
77
|
+
- `req.get("Cookie")` / `req.header("Authorization")`
|
|
78
|
+
- `req.cookies`, `req.signedCookies`, `req.session`, `req.user`
|
|
79
|
+
- `parseCookies(req)` and `getSignedCookie(req, …)`, which report it directly
|
|
80
|
+
|
|
81
|
+
A marked render is **never stored**. In production the response is sent with
|
|
82
|
+
`no-store` and this line is logged:
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
[render] /dashboard read identity-bound data (req.headers.cookie), not cached.
|
|
86
|
+
Register the route with 'private: true'.
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
In development the same situation fails the request. The reason it is not silent
|
|
90
|
+
is simple: this mistake produces a working page, so it is never noticed on its
|
|
91
|
+
own.
|
|
92
|
+
|
|
93
|
+
`csrfField()` marks the render the same way. A page that prints a token cannot
|
|
94
|
+
come from the cache — if it did, every visitor would share the same token and
|
|
95
|
+
the double-submit check would verify nothing.
|
|
96
|
+
|
|
97
|
+
## Sessions: signed cookies
|
|
98
|
+
|
|
99
|
+
The framework does not provide identity. The only thing it gives you is the
|
|
100
|
+
guarantee that "I wrote this value and it has not been tampered with":
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
import { clearCookie, getSignedCookie, setSignedCookie } from "jskelet/cookies";
|
|
104
|
+
|
|
105
|
+
export function startSession(res, username) {
|
|
106
|
+
setSignedCookie(res, "session", username, { maxAge: 60 * 60 * 8 });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function currentUser(req) {
|
|
110
|
+
const username = getSignedCookie(req, "session");
|
|
111
|
+
return username ? findUser(username) : null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function endSession(res) {
|
|
115
|
+
clearCookie(res, "session");
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The signature is HMAC-SHA256 and the comparison is constant time. If the
|
|
120
|
+
signature does not match, `getSignedCookie` returns `null` — a tampered value is
|
|
121
|
+
never used on the assumption that it "might be valid".
|
|
122
|
+
|
|
123
|
+
The secret comes from `security.cookieSecret` or the `JSKELET_SECRET`
|
|
124
|
+
environment variable. Without a secret the signed API **throws**; the rule that
|
|
125
|
+
a configuration error must not take the site down does not apply here, because
|
|
126
|
+
the silent alternative would be trusting an unsigned cookie.
|
|
127
|
+
|
|
128
|
+
The defaults are on the restrictive side: `HttpOnly`, `SameSite=Lax`, `Secure`
|
|
129
|
+
outside development, `Path=/`. `SameSite=Lax` alone closes most of CSRF — the
|
|
130
|
+
cookie is simply not sent on cross-site POSTs.
|
|
131
|
+
|
|
132
|
+
Cookies are **signed, not encrypted**. The value is readable, so store the
|
|
133
|
+
identifier of a secret rather than the secret itself.
|
|
134
|
+
|
|
135
|
+
## Cross-subdomain: shared cookies
|
|
136
|
+
|
|
137
|
+
Host-based i18n (`tr.example.com` / `en.example.com`) often needs a session on
|
|
138
|
+
every locale host. The supported pattern is a **short session id** plus an
|
|
139
|
+
optional `Domain=.example.com` — do not put a JWT or a large access token in a
|
|
140
|
+
shared cookie (`large token ≠ shared cookie`).
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
// jskelet.config.mjs
|
|
144
|
+
export default {
|
|
145
|
+
brand: {
|
|
146
|
+
sharedCookieRoots: [".investvio.com", ".localhost"],
|
|
147
|
+
},
|
|
148
|
+
auth: {
|
|
149
|
+
crossSubdomainHandoff: {
|
|
150
|
+
allowedCookieNames: ["sid"], // required allowlist
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Server
|
|
157
|
+
|
|
158
|
+
```js
|
|
159
|
+
import { writeSharedCookie } from "jskelet/cookies";
|
|
160
|
+
|
|
161
|
+
export function startSession(res, req, sessionId) {
|
|
162
|
+
const result = writeSharedCookie(res, "sid", sessionId, {
|
|
163
|
+
req,
|
|
164
|
+
maxAge: 60 * 60 * 8,
|
|
165
|
+
});
|
|
166
|
+
// result.ok === false → result.handoff; the client should use handoff
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
`Secure` follows the **protocol** (`https` / `x-forwarded-proto`), not
|
|
172
|
+
`NODE_ENV`. The Domain is set when the request Host matches
|
|
173
|
+
`sharedCookieRoots`. Values larger than ~512 bytes are refused with
|
|
174
|
+
`handoff: true`.
|
|
175
|
+
|
|
176
|
+
### Client
|
|
177
|
+
|
|
178
|
+
```js
|
|
179
|
+
import {
|
|
180
|
+
writeSharedCookie,
|
|
181
|
+
createHandoffUrl,
|
|
182
|
+
handoffViaWindowName,
|
|
183
|
+
consumeWindowNameHandoff,
|
|
184
|
+
} from "jskelet/client";
|
|
185
|
+
|
|
186
|
+
const result = writeSharedCookie("sid", sessionId, {
|
|
187
|
+
roots: [".investvio.com", ".localhost"],
|
|
188
|
+
maxAge: 60 * 60 * 8,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
if (!result.ok && result.handoff) {
|
|
192
|
+
const url = await createHandoffUrl({
|
|
193
|
+
name: "sid",
|
|
194
|
+
value: sessionId,
|
|
195
|
+
next: "https://tr.investvio.com/panel",
|
|
196
|
+
});
|
|
197
|
+
if (url) location.assign(url);
|
|
198
|
+
else handoffViaWindowName("https://tr.investvio.com/panel", {
|
|
199
|
+
name: "sid",
|
|
200
|
+
value: sessionId,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// On the target host (layout / island bootstrap):
|
|
205
|
+
consumeWindowNameHandoff();
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
If `roots` is omitted, the client reads
|
|
209
|
+
`<html data-jskelet-cookie-roots=".investvio.com,.localhost">`. After writing,
|
|
210
|
+
a **read-back** runs; if the browser rejected the Domain, `handoff: true`.
|
|
211
|
+
|
|
212
|
+
### Handoff ticket
|
|
213
|
+
|
|
214
|
+
With `auth.crossSubdomainHandoff` on:
|
|
215
|
+
|
|
216
|
+
1. `POST /_jskelet/auth/handoff` `{ name, value, next }` → `{ url }` (with
|
|
217
|
+
`?handoff=`). Mint is mounted **after** the CSRF middleware; `name` must be
|
|
218
|
+
in `allowedCookieNames` and an RFC 6265 token.
|
|
219
|
+
2. On the target host a GET middleware redeems the one-time ticket, sets the
|
|
220
|
+
cookie (shared Domain first, else host-only), and 303-redirects without
|
|
221
|
+
`handoff`
|
|
222
|
+
|
|
223
|
+
`next` must be under the same `sharedCookieRoots`. Tickets live ~60s in process
|
|
224
|
+
memory, with pending-ticket and per-IP mint limits. Do not put a JWT in the URL.
|
|
225
|
+
|
|
226
|
+
The `window.name` bridge is the cookie-less fallback:
|
|
227
|
+
`handoffViaWindowName` on the source page, `consumeWindowNameHandoff` on the
|
|
228
|
+
target. Prefer the server handoff when possible — `window.name` remains readable
|
|
229
|
+
across origins in the same tab.
|
|
230
|
+
|
|
231
|
+
## CSRF
|
|
232
|
+
|
|
233
|
+
The framework parses the request body (`express.urlencoded` + `express.json`),
|
|
234
|
+
which makes it the layer that accepts state-changing requests. The protection
|
|
235
|
+
has two layers.
|
|
236
|
+
|
|
237
|
+
### Layer 1 — origin check (on by default)
|
|
238
|
+
|
|
239
|
+
Unsafe methods get a 403 when `Origin` does not match our host, or when
|
|
240
|
+
`Sec-Fetch-Site: cross-site` arrives. **If neither header is present the request
|
|
241
|
+
passes**: browsers always send `Origin` on a cross-origin POST, while webhooks
|
|
242
|
+
and server-to-server calls send neither. That distinction keeps the protection
|
|
243
|
+
on without breaking integrations.
|
|
244
|
+
|
|
245
|
+
```js
|
|
246
|
+
security: {
|
|
247
|
+
csrf: {
|
|
248
|
+
// Legitimate exceptions, such as a panel on a separate domain.
|
|
249
|
+
allowedOrigins: ["https://admin.example.com"],
|
|
250
|
+
// Endpoints that do not come from a browser.
|
|
251
|
+
exclude: ["/webhook/:path*"],
|
|
252
|
+
},
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Layer 2 — double-submit token (optional)
|
|
257
|
+
|
|
258
|
+
Enabled with `security.csrf.token: true`. Forms print the token with
|
|
259
|
+
`csrfField()`:
|
|
260
|
+
|
|
261
|
+
```ejs
|
|
262
|
+
<form method="post" action="/dashboard/notes">
|
|
263
|
+
<%- csrfField() %>
|
|
264
|
+
…
|
|
265
|
+
</form>
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
The token is **not** produced by the middleware but by `csrfField()` — that is,
|
|
269
|
+
at the moment it is actually printed into a form. The reason is concrete: if the
|
|
270
|
+
token were written on every response, a public and cacheable page would carry a
|
|
271
|
+
`Set-Cookie` too, a CDN would store that response, and every visitor would share
|
|
272
|
+
the same token.
|
|
273
|
+
|
|
274
|
+
On the server the token in the signed cookie must match the submitted field; an
|
|
275
|
+
`X-CSRF-Token` header is accepted in place of the field.
|
|
276
|
+
|
|
277
|
+
`csrfField()` returns an empty string while `security.csrf.token` is off, so the
|
|
278
|
+
template renders under any configuration.
|
|
279
|
+
|
|
280
|
+
## Fragment endpoints
|
|
281
|
+
|
|
282
|
+
`fragment()` produces a partial response with a fixed policy: no layout, sent
|
|
283
|
+
with `private, no-store` and no ETag, never touching the HTML cache.
|
|
284
|
+
|
|
285
|
+
```js
|
|
286
|
+
export default function register(app, { fragment }) {
|
|
287
|
+
app.get(
|
|
288
|
+
"/_fragment/orders",
|
|
289
|
+
fragment(async ({ req, query }) => {
|
|
290
|
+
const user = currentUser(req);
|
|
291
|
+
if (!user) return { view: "partials/session-expired", status: 401 };
|
|
292
|
+
|
|
293
|
+
return {
|
|
294
|
+
view: "partials/order-table",
|
|
295
|
+
data: { orders: getOrders(user.username, Number(query.page ?? 1)) },
|
|
296
|
+
};
|
|
297
|
+
}),
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
The controller returns either `{ view, data?, status? }` or an HTML string
|
|
303
|
+
directly. On failure it responds with a small partial rather than a whole page
|
|
304
|
+
(`<div role="alert" data-fragment-error>`), because the swapped region must not
|
|
305
|
+
end up containing an entire error page.
|
|
306
|
+
|
|
307
|
+
Using the same template both inside the page and at the fragment endpoint is the
|
|
308
|
+
central idea: the markup has a single source on the server and the client does
|
|
309
|
+
not carry a second template.
|
|
310
|
+
|
|
311
|
+
## Client: swapping a region
|
|
312
|
+
|
|
313
|
+
```js
|
|
314
|
+
import { registerAll, start, startForms, startSwapLinks } from "jskelet/client";
|
|
315
|
+
|
|
316
|
+
registerAll({ "live-clock": () => import("../islands/live-clock.js") });
|
|
317
|
+
|
|
318
|
+
start();
|
|
319
|
+
startSwapLinks();
|
|
320
|
+
startForms();
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
`startSwapLinks()` binds links carrying `data-swap`:
|
|
324
|
+
|
|
325
|
+
```html
|
|
326
|
+
<a href="/_fragment/orders?page=2" data-swap="#orders">Next</a>
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
Because `href` is a real URL, the link falls back to normal navigation without
|
|
330
|
+
JavaScript. For programmatic use there is `swap()`:
|
|
331
|
+
|
|
332
|
+
```js
|
|
333
|
+
import { swap } from "jskelet/client";
|
|
334
|
+
|
|
335
|
+
await swap("#orders", "/_fragment/orders?page=2", { history: true });
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
In order, `swap()` unmounts the islands in the old subtree, replaces the
|
|
339
|
+
content, hydrates the new subtree and restores focus if it was lost. While the
|
|
340
|
+
request is in flight the region gets `aria-busy="true"` — binding the pending
|
|
341
|
+
indicator to the accessibility state instead of a separate class also keeps the
|
|
342
|
+
two from drifting apart.
|
|
343
|
+
|
|
344
|
+
If it meets a redirect (the session expired and the server points at the sign-in
|
|
345
|
+
page), it navigates there instead of swapping the partial in.
|
|
346
|
+
|
|
347
|
+
### Unmounting islands
|
|
348
|
+
|
|
349
|
+
This is the half of swapping that is easiest to skip. A `mount()` function may
|
|
350
|
+
return a cleanup callback:
|
|
351
|
+
|
|
352
|
+
```js
|
|
353
|
+
export function mount(element) {
|
|
354
|
+
const timer = setInterval(() => tick(element), 1000);
|
|
355
|
+
return () => clearInterval(timer);
|
|
356
|
+
}
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
The islands inside a region replaced with `innerHTML` leave the DOM, but the
|
|
360
|
+
listeners they installed on `document`/`window` and their `setInterval` timers
|
|
361
|
+
keep running; after a few swaps the same work runs dozens of times. `swap()` and
|
|
362
|
+
the form helpers call `unmount()` for you; when you change the DOM by hand, you
|
|
363
|
+
call it:
|
|
364
|
+
|
|
365
|
+
```js
|
|
366
|
+
import { hydrate, unmount } from "jskelet/client";
|
|
367
|
+
|
|
368
|
+
unmount(region);
|
|
369
|
+
region.innerHTML = html;
|
|
370
|
+
hydrate(region);
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
## Forms
|
|
374
|
+
|
|
375
|
+
`startForms()` binds forms carrying `data-enhance`. The contract is progressive
|
|
376
|
+
enhancement: the form is a normal `<form method="post" action="…">` and
|
|
377
|
+
JavaScript only removes the full page round trip in between.
|
|
378
|
+
|
|
379
|
+
```html
|
|
380
|
+
<form method="post" action="/dashboard/notes" data-enhance data-target="#notes">
|
|
381
|
+
<%- csrfField() %>
|
|
382
|
+
<textarea name="text" required minlength="3"></textarea>
|
|
383
|
+
<button type="submit">Save</button>
|
|
384
|
+
</form>
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
The server answers with one of three things:
|
|
388
|
+
|
|
389
|
+
- **a redirect** → followed with `location.assign` (successful mutation, and the
|
|
390
|
+
no-JavaScript path)
|
|
391
|
+
- **4xx + a partial** → swapped in place of the form (validation errors)
|
|
392
|
+
- **2xx + a partial** → swapped into the `data-target` region, and the form is
|
|
393
|
+
reset
|
|
394
|
+
|
|
395
|
+
The server side handles both clients:
|
|
396
|
+
|
|
397
|
+
```js
|
|
398
|
+
app.post(
|
|
399
|
+
"/dashboard/notes",
|
|
400
|
+
fragment(async ({ req }) => {
|
|
401
|
+
const user = currentUser(req);
|
|
402
|
+
if (!user) seeOther("/sign-in?next=%2Fdashboard");
|
|
403
|
+
|
|
404
|
+
const result = addNote(user.username, req.body?.text);
|
|
405
|
+
|
|
406
|
+
// Without JavaScript the client sends no `X-Requested-With`: full round trip.
|
|
407
|
+
if (req.get("X-Requested-With") !== "fragment") {
|
|
408
|
+
seeOther(result.ok ? "/dashboard" : "/dashboard?note=error");
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return result.ok
|
|
412
|
+
? { view: "partials/note-list", data: { notes: getNotes(user.username) } }
|
|
413
|
+
: { view: "partials/note-form", data: { error: result.error }, status: 422 };
|
|
414
|
+
}),
|
|
415
|
+
);
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
Using `redirect()` instead of `seeOther()` here would be a bug: `redirect()`
|
|
419
|
+
sends 307 and 307 preserves the method, so the browser POSTs to the target
|
|
420
|
+
again. The post-form flow needs 303.
|
|
421
|
+
|
|
422
|
+
Routing the POST handler through `fragment()` has a reason too: alongside the
|
|
423
|
+
layout-less render and `no-store`, it establishes the **request context**.
|
|
424
|
+
Without the context, `csrfField()` in a form re-printed after a validation error
|
|
425
|
+
comes out empty and the user's second attempt gets a 403.
|
|
426
|
+
|
|
427
|
+
On a validation error focus moves to the first invalid field; the markers looked
|
|
428
|
+
for are `aria-invalid="true"` and `data-field-error`.
|
|
429
|
+
|
|
430
|
+
## Configuration
|
|
431
|
+
|
|
432
|
+
```js
|
|
433
|
+
export default {
|
|
434
|
+
security: {
|
|
435
|
+
/**
|
|
436
|
+
* Turn this off when you are not behind a reverse proxy: while it is on, a
|
|
437
|
+
* client can forge its own `X-Forwarded-For`.
|
|
438
|
+
*/
|
|
439
|
+
trustProxy: true,
|
|
440
|
+
|
|
441
|
+
cookieSecret: process.env.JSKELET_SECRET,
|
|
442
|
+
|
|
443
|
+
csrf: {
|
|
444
|
+
enabled: true,
|
|
445
|
+
token: false,
|
|
446
|
+
allowedOrigins: [],
|
|
447
|
+
exclude: [],
|
|
448
|
+
cookieName: "csrf_token",
|
|
449
|
+
fieldName: "_csrf",
|
|
450
|
+
headerName: "x-csrf-token",
|
|
451
|
+
},
|
|
452
|
+
},
|
|
453
|
+
};
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
Two more settings pay off for dashboard paths:
|
|
457
|
+
|
|
458
|
+
```js
|
|
459
|
+
// Speculatively fetching a link with side effects can sign the user out.
|
|
460
|
+
navigation: { exclude: ["/dashboard/:path*", "/sign-out"] },
|
|
461
|
+
|
|
462
|
+
// The warmer has no session; protected pages cannot be warmed.
|
|
463
|
+
prewarmSkip: ["/api/", "/_fragment/", "/dashboard", "/sign-out"],
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
And indexing is turned off through `headers()` — `no-store` prevents caching,
|
|
467
|
+
but indexing has to be said separately:
|
|
468
|
+
|
|
469
|
+
```js
|
|
470
|
+
{
|
|
471
|
+
source: "/dashboard/:path*",
|
|
472
|
+
headers: [{ key: "X-Robots-Tag", value: "noindex, nofollow" }],
|
|
473
|
+
}
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
## Checklist
|
|
477
|
+
|
|
478
|
+
When adding a per-visitor section:
|
|
479
|
+
|
|
480
|
+
- [ ] Pages are registered with `route(fn, { private: true })`.
|
|
481
|
+
- [ ] Fragment endpoints are registered with `fragment()`.
|
|
482
|
+
- [ ] `security.cookieSecret` comes from the environment, not from the source.
|
|
483
|
+
- [ ] Mutation forms contain `csrfField()` and `security.csrf.token` is on.
|
|
484
|
+
- [ ] Signing out is a POST, not a GET.
|
|
485
|
+
- [ ] The `next` parameter after sign-in only accepts same-site paths.
|
|
486
|
+
- [ ] `prewarmSkip` and `navigation.exclude` exclude the protected prefix.
|
|
487
|
+
- [ ] `X-Robots-Tag: noindex` under `headers()`.
|
|
488
|
+
- [ ] The smoke test checks the `no-store` header and the rejection of a POST
|
|
489
|
+
without a token — nothing on screen changes when those break.
|