@seip/bluebird-js 0.1.7 → 0.2.0
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 +726 -620
- package/README.md +615 -607
- package/app/models/user.js +19 -0
- package/app/resources/html/home.html +137 -0
- package/app/resources/html/layouts/main.html +54 -0
- package/app/resources/html/notFound.html +14 -0
- package/app/resources/html/terms.html +46 -0
- package/app/resources/index.html +27 -0
- package/app/resources/layouts/AppLayout.jsx +89 -0
- package/app/resources/main.jsx +18 -0
- package/app/resources/pages/Dashboard.jsx +98 -0
- package/app/resources/pages/Home.jsx +140 -0
- package/app/resources/pages/Login.jsx +35 -0
- package/app/resources/pages/NotFound.jsx +16 -0
- package/app/resources/pages/Register.jsx +55 -0
- package/app/resources/routes.jsx +54 -0
- package/app/resources/spa.js +67 -0
- package/app/resources/styles.css +9 -0
- package/app/routes/api.js +85 -50
- package/app/routes/auth.js +209 -0
- package/app/routes/web.js +59 -47
- package/core/ai.js +30 -10
- package/core/app.d.ts +2 -18
- package/core/app.js +987 -801
- package/core/auth.d.ts +2 -13
- package/core/auth.js +444 -326
- package/core/cli/build.js +37 -0
- package/core/cli/dev.js +155 -0
- package/core/cli/doctor.js +37 -2
- package/core/cli/export.js +191 -0
- package/core/cli/init.js +91 -322
- package/core/cli/nginx.js +1 -1
- package/core/cli/templates.js +139 -0
- package/core/cli/view.js +98 -161
- package/core/config.js +44 -2
- package/core/csrf.js +156 -24
- package/core/database.js +1184 -1131
- package/core/debug.js +1 -1
- package/core/html.js +8 -1
- package/core/index.d.ts +126 -310
- package/core/paths.js +55 -0
- package/core/ratelimit.js +10 -1
- package/core/react/client/api.js +237 -0
- package/core/react/client/auth.js +236 -0
- package/core/react/client/components.js +1321 -0
- package/core/react/client/i18n.js +338 -0
- package/core/react/client/index.d.ts +249 -0
- package/core/react/client/index.js +49 -0
- package/core/react/client/router.js +26 -0
- package/core/react/client/theme.css +512 -0
- package/core/render-cache.js +250 -212
- package/core/render.d.ts +25 -21
- package/core/render.js +738 -736
- package/core/router.d.ts +2 -3
- package/core/router.js +171 -171
- package/core/seo.js +10 -0
- package/core/spa.js +599 -0
- package/core/view-loader.js +155 -613
- package/core/vite-plugin.d.ts +18 -0
- package/core/vite-plugin.js +162 -0
- package/core/vite.js +250 -145
- package/index.js +22 -18
- package/jsconfig.json +5 -3
- package/package.json +114 -92
- package/production.md +8 -3
- package/public/js/bluebird.js +1160 -0
- package/templates/_auth/app/database.js +19 -0
- package/templates/_auth/app/models/user.js +22 -0
- package/templates/_auth/app/routes/auth-router.js +304 -0
- package/templates/_base/app/resources/html/layouts/main.html +55 -0
- package/templates/_base/app/resources/html/notFound.html +14 -0
- package/templates/_base/app/resources/index.html +27 -0
- package/templates/_base/app/resources/styles.css +9 -0
- package/templates/api/app/models/note.js +18 -0
- package/templates/api/app/routes/api.js +82 -0
- package/templates/api/app/routes/auth.js +11 -0
- package/templates/api/index.js +19 -0
- package/templates/api/template.json +13 -0
- package/templates/auth/app/mail.js +11 -0
- package/templates/auth/app/models/password_reset.js +18 -0
- package/templates/auth/app/resources/html/home.html +21 -0
- package/templates/auth/app/resources/html/partials/nav.html +2 -0
- package/templates/auth/app/resources/layouts/AppLayout.jsx +60 -0
- package/templates/auth/app/resources/main.jsx +18 -0
- package/templates/auth/app/resources/pages/Dashboard.jsx +51 -0
- package/templates/auth/app/resources/pages/ForgotPassword.jsx +34 -0
- package/templates/auth/app/resources/pages/Login.jsx +36 -0
- package/templates/auth/app/resources/pages/NotFound.jsx +16 -0
- package/templates/auth/app/resources/pages/Profile.jsx +99 -0
- package/templates/auth/app/resources/pages/Register.jsx +50 -0
- package/templates/auth/app/resources/pages/ResetPassword.jsx +50 -0
- package/templates/auth/app/resources/routes.jsx +54 -0
- package/templates/auth/app/resources/spa.js +29 -0
- package/templates/auth/app/routes/api.js +17 -0
- package/templates/auth/app/routes/auth.js +13 -0
- package/templates/auth/app/routes/web.js +21 -0
- package/templates/auth/index.js +19 -0
- package/templates/auth/template.json +15 -0
- package/templates/blank/app/models/.gitkeep +0 -0
- package/templates/blank/app/resources/html/home.html +27 -0
- package/templates/blank/app/resources/html/partials/nav.html +1 -0
- package/templates/blank/app/resources/main.jsx +16 -0
- package/templates/blank/app/resources/pages/Home.jsx +42 -0
- package/templates/blank/app/resources/pages/NotFound.jsx +27 -0
- package/templates/blank/app/resources/routes.jsx +15 -0
- package/templates/blank/app/resources/spa.js +24 -0
- package/templates/blank/app/routes/api.js +11 -0
- package/templates/blank/app/routes/web.js +21 -0
- package/templates/blank/index.js +13 -0
- package/templates/blank/template.json +12 -0
- package/templates/blog/app/lib/blog.js +149 -0
- package/templates/blog/app/models/post.js +22 -0
- package/templates/blog/app/resources/html/home.html +28 -0
- package/templates/blog/app/resources/html/not-found.html +5 -0
- package/templates/blog/app/resources/html/partials/nav.html +2 -0
- package/templates/blog/app/resources/html/partials/post-card.html +9 -0
- package/templates/blog/app/resources/html/post.html +8 -0
- package/templates/blog/app/resources/layouts/AdminLayout.jsx +54 -0
- package/templates/blog/app/resources/main.jsx +18 -0
- package/templates/blog/app/resources/pages/Login.jsx +31 -0
- package/templates/blog/app/resources/pages/NotFound.jsx +16 -0
- package/templates/blog/app/resources/pages/PostEditor.jsx +51 -0
- package/templates/blog/app/resources/pages/Posts.jsx +83 -0
- package/templates/blog/app/resources/pages/Register.jsx +43 -0
- package/templates/blog/app/resources/routes.jsx +51 -0
- package/templates/blog/app/resources/spa.js +25 -0
- package/templates/blog/app/resources/styles.css +40 -0
- package/templates/blog/app/routes/api.js +77 -0
- package/templates/blog/app/routes/auth.js +10 -0
- package/templates/blog/app/routes/web.js +114 -0
- package/templates/blog/index.js +21 -0
- package/templates/blog/template.json +14 -0
- package/vite.config.js +11 -0
- package/app/views/components/Header.jsx +0 -42
- package/app/views/components/LiveCounter.jsx +0 -67
- package/app/views/layouts/main/view.jsx +0 -201
- package/app/views/legal/terms.html +0 -29
- package/app/views/pages/about.jsx +0 -98
- package/app/views/pages/dashboard.jsx +0 -278
- package/app/views/pages/index.jsx +0 -123
- package/app/views/pages/login.jsx +0 -141
- package/core/actions.d.ts +0 -4
- package/core/actions.js +0 -355
- package/core/jsx.d.ts +0 -38
- package/core/jsx.js +0 -1597
- package/core/vite.d.ts +0 -3
- package/public/js/bluebird-client.js +0 -3265
- package/public/js/tailwind.js +0 -8
package/core/render.js
CHANGED
|
@@ -1,736 +1,738 @@
|
|
|
1
|
-
import Config from "./config.js";
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @param {
|
|
35
|
-
* @
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (
|
|
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
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
if (
|
|
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
|
-
const
|
|
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
|
-
const
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
const
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
if (
|
|
512
|
-
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
*
|
|
670
|
-
*
|
|
671
|
-
*
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
const
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
const
|
|
686
|
-
if (
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
}
|
|
736
|
-
|
|
1
|
+
import Config from "./config.js";
|
|
2
|
+
import { escapeHtml, isSafeHtml, SafeHtml } from "./html.js";
|
|
3
|
+
import RenderCache from "./render-cache.js";
|
|
4
|
+
import ViewLoader from "./view-loader.js";
|
|
5
|
+
import ViteBridge from "./vite.js";
|
|
6
|
+
import Csrf from "./csrf.js";
|
|
7
|
+
|
|
8
|
+
const DEBUG = Config.props().debug;
|
|
9
|
+
const RENDER_CACHE_TTL = parseInt(process.env.RENDER_CACHE_TTL || "300", 10);
|
|
10
|
+
const CSRF_PLACEHOLDER = "__BB_CSRF_TOKEN__";
|
|
11
|
+
const IGNORED_CACHE_QUERY_KEYS = new Set(["_partial"]);
|
|
12
|
+
const MAX_PARTIAL_DEPTH = 16;
|
|
13
|
+
const MAX_COMPILED_CACHE = 500;
|
|
14
|
+
const BANNED_PROPS = new Set(["__proto__", "constructor", "prototype"]);
|
|
15
|
+
const TOKEN_REGEX = /(\{\{\{[\s\S]*?\}\}\}|\{\{[\s\S]*?\}\})/g;
|
|
16
|
+
|
|
17
|
+
const compiledCache = new Map();
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Replaces the request CSRF token with a placeholder before storing HTML in a shared cache,
|
|
21
|
+
* so cached responses never leak one visitor's token to another.
|
|
22
|
+
* @param {string} body
|
|
23
|
+
* @param {string} token
|
|
24
|
+
* @returns {string}
|
|
25
|
+
*/
|
|
26
|
+
export function tokenizeForCache(body, token) {
|
|
27
|
+
if (!token || typeof body !== "string") return body;
|
|
28
|
+
return body.split(token).join(CSRF_PLACEHOLDER);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Restores the current request's CSRF token into a cached response.
|
|
33
|
+
* @param {string} body
|
|
34
|
+
* @param {string} token
|
|
35
|
+
* @returns {string}
|
|
36
|
+
*/
|
|
37
|
+
export function detokenizeFromCache(body, token) {
|
|
38
|
+
if (typeof body !== "string" || !body.includes(CSRF_PLACEHOLDER)) return body;
|
|
39
|
+
return body.split(CSRF_PLACEHOLDER).join(token || "");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Adds the per-request CSP nonce (`res.locals.cspNonce`) to every inline `<script>` and `<style>` tag.
|
|
44
|
+
* @param {string} htmlStr
|
|
45
|
+
* @param {import('express').Response} res
|
|
46
|
+
* @returns {string}
|
|
47
|
+
*/
|
|
48
|
+
export function applyCspNonce(htmlStr, res) {
|
|
49
|
+
const nonce = res?.locals?.cspNonce;
|
|
50
|
+
if (!nonce || typeof htmlStr !== "string") return htmlStr;
|
|
51
|
+
return htmlStr.replace(/<(script|style)\b(?![^>]*\bnonce=)/gi, `<$1 nonce="${escapeHtml(nonce)}"`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Removes the current request nonce from HTML before caching it.
|
|
56
|
+
* @param {string} htmlStr
|
|
57
|
+
* @param {import('express').Response} res
|
|
58
|
+
* @returns {string}
|
|
59
|
+
*/
|
|
60
|
+
export function stripCspNonce(htmlStr, res) {
|
|
61
|
+
const nonce = res?.locals?.cspNonce;
|
|
62
|
+
if (!nonce || typeof htmlStr !== "string") return htmlStr;
|
|
63
|
+
return htmlStr.split(` nonce="${escapeHtml(nonce)}"`).join("");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Builds a render cache key from the full request path (`baseUrl` + `path`), an allowlist of query parameters,
|
|
68
|
+
* and the request language (`#lang=xx` when it is not the default `en`), so cached pages never leak across languages.
|
|
69
|
+
* Returns `null` when the request carries query parameters outside the allowlist.
|
|
70
|
+
* @param {string} prefix
|
|
71
|
+
* @param {import('express').Request} req
|
|
72
|
+
* @param {string[]} [queryKeys=[]]
|
|
73
|
+
* @param {string} [suffix=""]
|
|
74
|
+
* @returns {string|null}
|
|
75
|
+
*/
|
|
76
|
+
export function buildCacheKey(prefix, req, queryKeys = [], suffix = "") {
|
|
77
|
+
if (!req) return suffix ? `${prefix}:${suffix}` : prefix;
|
|
78
|
+
const query = req.query || {};
|
|
79
|
+
const allowed = new Set(queryKeys);
|
|
80
|
+
const parts = [];
|
|
81
|
+
for (const key of Object.keys(query).sort()) {
|
|
82
|
+
if (IGNORED_CACHE_QUERY_KEYS.has(key)) continue;
|
|
83
|
+
if (!allowed.has(key)) return null;
|
|
84
|
+
const val = query[key];
|
|
85
|
+
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(Array.isArray(val) ? val.join(",") : String(val))}`);
|
|
86
|
+
}
|
|
87
|
+
const relPath = req.path || (req.originalUrl || req.url || "").split("?")[0] || "/";
|
|
88
|
+
const pathname = req.baseUrl ? `${req.baseUrl}${relPath === "/" ? "" : relPath}` || "/" : relPath;
|
|
89
|
+
const base = suffix ? `${prefix}:${pathname}:${suffix}` : `${prefix}:${pathname}`;
|
|
90
|
+
const langPart = typeof req.lang === "string" && req.lang && req.lang !== "en" ? `#lang=${encodeURIComponent(req.lang)}` : "";
|
|
91
|
+
return `${base}${parts.length ? `?${parts.join("&")}` : ""}${langPart}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Checks whether a request carries authentication credentials.
|
|
96
|
+
* @param {import('express').Request} req
|
|
97
|
+
* @returns {boolean}
|
|
98
|
+
*/
|
|
99
|
+
export function isAuthenticatedRequest(req) {
|
|
100
|
+
return Boolean(req?.user || req?.cookies?.auth || req?.headers?.authorization);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Compiles an HTML template string containing `{{ }}` directives into an AST.
|
|
105
|
+
* @param {string} source
|
|
106
|
+
* @returns {Array<Object>}
|
|
107
|
+
*/
|
|
108
|
+
function compileTemplate(source) {
|
|
109
|
+
if (typeof source !== "string") return [];
|
|
110
|
+
if (!DEBUG && compiledCache.has(source)) {
|
|
111
|
+
return compiledCache.get(source);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const root = { type: "root", body: [] };
|
|
115
|
+
const stack = [{ node: root, branch: "body" }];
|
|
116
|
+
const pushNode = (node) => {
|
|
117
|
+
const top = stack[stack.length - 1];
|
|
118
|
+
top.node[top.branch].push(node);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
let lastIndex = 0;
|
|
122
|
+
TOKEN_REGEX.lastIndex = 0;
|
|
123
|
+
let match;
|
|
124
|
+
|
|
125
|
+
while ((match = TOKEN_REGEX.exec(source)) !== null) {
|
|
126
|
+
const token = match[0];
|
|
127
|
+
if (match.index > 0 && source[match.index - 1] === "\\") {
|
|
128
|
+
pushNode({ type: "text", value: source.slice(lastIndex, match.index - 1) + token });
|
|
129
|
+
lastIndex = TOKEN_REGEX.lastIndex;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (match.index > lastIndex) {
|
|
133
|
+
pushNode({ type: "text", value: source.slice(lastIndex, match.index) });
|
|
134
|
+
}
|
|
135
|
+
lastIndex = TOKEN_REGEX.lastIndex;
|
|
136
|
+
|
|
137
|
+
if (token.startsWith("{{{") && token.endsWith("}}}")) {
|
|
138
|
+
const expr = token.slice(3, -3).trim();
|
|
139
|
+
const viteMatch = expr.match(/^vite\s+(.+)$/);
|
|
140
|
+
if (viteMatch) {
|
|
141
|
+
pushNode({ type: "vite", entry: viteMatch[1].trim().replace(/^['"]|['"]$/g, "") });
|
|
142
|
+
} else {
|
|
143
|
+
pushNode({ type: "raw", expr });
|
|
144
|
+
}
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const inner = token.slice(2, -2).trim();
|
|
149
|
+
if (!inner || inner.startsWith("!")) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (inner.startsWith(">")) {
|
|
154
|
+
pushNode({ type: "partial", name: inner.slice(1).trim() });
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const viteMatch = inner.match(/^vite\s+(.+)$/);
|
|
159
|
+
if (viteMatch) {
|
|
160
|
+
pushNode({ type: "vite", entry: viteMatch[1].trim().replace(/^['"]|['"]$/g, "") });
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const ifMatch = inner.match(/^#?if\s+([\s\S]+)$/);
|
|
165
|
+
if (ifMatch) {
|
|
166
|
+
const node = { type: "if", expr: ifMatch[1].trim(), then: [], else: [] };
|
|
167
|
+
pushNode(node);
|
|
168
|
+
stack.push({ node, branch: "then" });
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (inner === "else" || inner === "#else") {
|
|
173
|
+
const top = stack[stack.length - 1];
|
|
174
|
+
if (top && (top.node.type === "if" || top.node.type === "each")) {
|
|
175
|
+
top.branch = "else";
|
|
176
|
+
}
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (inner === "/if" || inner === "endif") {
|
|
181
|
+
if (stack.length > 1 && stack[stack.length - 1].node.type === "if") {
|
|
182
|
+
stack.pop();
|
|
183
|
+
}
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const eachMatch = inner.match(/^#?each\s+([\s\S]+)$/);
|
|
188
|
+
if (eachMatch) {
|
|
189
|
+
const node = { type: "each", expr: eachMatch[1].trim(), body: [], else: [] };
|
|
190
|
+
pushNode(node);
|
|
191
|
+
stack.push({ node, branch: "body" });
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (inner === "/each" || inner === "endeach") {
|
|
196
|
+
if (stack.length > 1 && stack[stack.length - 1].node.type === "each") {
|
|
197
|
+
stack.pop();
|
|
198
|
+
}
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
pushNode({ type: "var", expr: inner });
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (lastIndex < source.length) {
|
|
206
|
+
pushNode({ type: "text", value: source.slice(lastIndex) });
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (!DEBUG) {
|
|
210
|
+
if (compiledCache.size >= MAX_COMPILED_CACHE) {
|
|
211
|
+
const firstKey = compiledCache.keys().next().value;
|
|
212
|
+
if (firstKey !== undefined) compiledCache.delete(firstKey);
|
|
213
|
+
}
|
|
214
|
+
compiledCache.set(source, root.body);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return root.body;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Safely retrieves an own property or collection `.length` without traversing prototypes.
|
|
222
|
+
* @param {any} target
|
|
223
|
+
* @param {string} key
|
|
224
|
+
* @returns {any}
|
|
225
|
+
*/
|
|
226
|
+
function safeProp(target, key) {
|
|
227
|
+
if (target === null || target === undefined || BANNED_PROPS.has(key)) return undefined;
|
|
228
|
+
if ((Array.isArray(target) || typeof target === "string") && key === "length") {
|
|
229
|
+
return target.length;
|
|
230
|
+
}
|
|
231
|
+
if (typeof target === "object" && Object.prototype.hasOwnProperty.call(target, key)) {
|
|
232
|
+
return target[key];
|
|
233
|
+
}
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Resolves a dot-delimited expression or literal across the scope stack.
|
|
239
|
+
* @param {Array<Object>} scopes
|
|
240
|
+
* @param {string} rawExpr
|
|
241
|
+
* @returns {any}
|
|
242
|
+
*/
|
|
243
|
+
function resolveExpr(scopes, rawExpr) {
|
|
244
|
+
const expr = String(rawExpr || "").trim();
|
|
245
|
+
if (!expr) return undefined;
|
|
246
|
+
if (expr === "true") return true;
|
|
247
|
+
if (expr === "false") return false;
|
|
248
|
+
if (expr === "null") return null;
|
|
249
|
+
if (/^-?\d+(\.\d+)?$/.test(expr)) return Number(expr);
|
|
250
|
+
if ((expr.startsWith('"') && expr.endsWith('"')) || (expr.startsWith("'") && expr.endsWith("'"))) {
|
|
251
|
+
return expr.slice(1, -1);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (expr === "." || expr === "this") {
|
|
255
|
+
const top = scopes[0];
|
|
256
|
+
return top && typeof top === "object" && "__this__" in top ? top.__this__ : top;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
let depthOffset = 0;
|
|
260
|
+
let pathStr = expr;
|
|
261
|
+
while (pathStr.startsWith("../")) {
|
|
262
|
+
depthOffset++;
|
|
263
|
+
pathStr = pathStr.slice(3);
|
|
264
|
+
}
|
|
265
|
+
if (pathStr.startsWith("./")) {
|
|
266
|
+
pathStr = pathStr.slice(2);
|
|
267
|
+
} else if (pathStr.startsWith("this.")) {
|
|
268
|
+
pathStr = pathStr.slice(5);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const segments = pathStr.split(".");
|
|
272
|
+
const firstSeg = segments[0];
|
|
273
|
+
if (BANNED_PROPS.has(firstSeg)) return undefined;
|
|
274
|
+
|
|
275
|
+
let base = undefined;
|
|
276
|
+
let found = false;
|
|
277
|
+
for (let i = depthOffset; i < scopes.length; i++) {
|
|
278
|
+
const scope = scopes[i];
|
|
279
|
+
if (scope === null || scope === undefined) continue;
|
|
280
|
+
const candidateTarget = typeof scope === "object" && "__this__" in scope && firstSeg !== "@index" && firstSeg !== "@number" && firstSeg !== "@first" && firstSeg !== "@last"
|
|
281
|
+
? (typeof scope.__this__ === "object" && scope.__this__ !== null && Object.prototype.hasOwnProperty.call(scope.__this__, firstSeg) ? scope.__this__ : scope)
|
|
282
|
+
: scope;
|
|
283
|
+
if (typeof candidateTarget === "object" && Object.prototype.hasOwnProperty.call(candidateTarget, firstSeg)) {
|
|
284
|
+
base = candidateTarget[firstSeg];
|
|
285
|
+
found = true;
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
if ((Array.isArray(candidateTarget) || typeof candidateTarget === "string") && firstSeg === "length") {
|
|
289
|
+
base = candidateTarget.length;
|
|
290
|
+
found = true;
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (!found) return undefined;
|
|
296
|
+
let current = base;
|
|
297
|
+
for (let i = 1; i < segments.length; i++) {
|
|
298
|
+
current = safeProp(current, segments[i]);
|
|
299
|
+
if (current === undefined || current === null) return current;
|
|
300
|
+
}
|
|
301
|
+
return current;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Evaluates a conditional expression supporting negation and equality operators.
|
|
306
|
+
* @param {Array<Object>} scopes
|
|
307
|
+
* @param {string} rawExpr
|
|
308
|
+
* @returns {boolean}
|
|
309
|
+
*/
|
|
310
|
+
function evaluateCondition(scopes, rawExpr) {
|
|
311
|
+
const expr = String(rawExpr || "").trim();
|
|
312
|
+
if (!expr) return false;
|
|
313
|
+
|
|
314
|
+
const opMatch = expr.match(/^([\s\S]+?)\s*(===|!==|==|!=)\s*([\s\S]+)$/);
|
|
315
|
+
if (opMatch) {
|
|
316
|
+
const left = resolveExpr(scopes, opMatch[1]);
|
|
317
|
+
const op = opMatch[2];
|
|
318
|
+
const right = resolveExpr(scopes, opMatch[3]);
|
|
319
|
+
if (op === "===") return left === right;
|
|
320
|
+
if (op === "!==") return left !== right;
|
|
321
|
+
if (op === "==") return left == right;
|
|
322
|
+
return left != right;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (expr.startsWith("!")) {
|
|
326
|
+
return !evaluateCondition(scopes, expr.slice(1));
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const val = resolveExpr(scopes, expr);
|
|
330
|
+
return Array.isArray(val) ? val.length > 0 : Boolean(val);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Formats an interpolated value as escaped HTML unless it is SafeHtml or raw.
|
|
335
|
+
* @param {any} val
|
|
336
|
+
* @param {boolean} raw
|
|
337
|
+
* @returns {string}
|
|
338
|
+
*/
|
|
339
|
+
function formatOutput(val, raw = false) {
|
|
340
|
+
if (val === null || val === undefined || val === false) return "";
|
|
341
|
+
if (isSafeHtml(val)) return val.toString();
|
|
342
|
+
return raw ? String(val) : escapeHtml(val);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Resolves a Vite asset tag for `{{ vite "..." }}` directives.
|
|
347
|
+
* @param {string} entry
|
|
348
|
+
* @param {Object} ctx
|
|
349
|
+
* @returns {string}
|
|
350
|
+
*/
|
|
351
|
+
function resolveViteTag(entry, ctx) {
|
|
352
|
+
const bridge = ctx?.vite || ViteBridge.from(ctx?.req) || ViteBridge.from(ctx?.res);
|
|
353
|
+
if (bridge) {
|
|
354
|
+
return bridge.assetTags(entry).all;
|
|
355
|
+
}
|
|
356
|
+
const fallback = new ViteBridge({ root: Config.appDir(), dev: DEBUG });
|
|
357
|
+
if (!DEBUG && fallback.loadManifestSync()) {
|
|
358
|
+
return fallback.assetTags(entry).all;
|
|
359
|
+
}
|
|
360
|
+
const clean = String(entry || "").replace(/^\/+/, "");
|
|
361
|
+
if (/\.(css|scss|sass|less|styl)$/i.test(clean)) {
|
|
362
|
+
return `<link rel="stylesheet" href="/${escapeHtml(clean)}">`;
|
|
363
|
+
}
|
|
364
|
+
return `<script type="module" src="/${escapeHtml(clean)}"></script>`;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Evaluates a compiled AST against a scope stack.
|
|
369
|
+
* @param {Array<Object>} nodes
|
|
370
|
+
* @param {Array<Object>} scopes
|
|
371
|
+
* @param {Object} ctx
|
|
372
|
+
* @param {number} depth
|
|
373
|
+
* @returns {Promise<string>}
|
|
374
|
+
*/
|
|
375
|
+
async function evaluateAst(nodes, scopes, ctx, depth = 0) {
|
|
376
|
+
let out = "";
|
|
377
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
378
|
+
const node = nodes[i];
|
|
379
|
+
if (node.type === "text") {
|
|
380
|
+
out += node.value;
|
|
381
|
+
} else if (node.type === "var") {
|
|
382
|
+
out += formatOutput(resolveExpr(scopes, node.expr), false);
|
|
383
|
+
} else if (node.type === "raw") {
|
|
384
|
+
out += formatOutput(resolveExpr(scopes, node.expr), true);
|
|
385
|
+
} else if (node.type === "vite") {
|
|
386
|
+
out += resolveViteTag(node.entry, ctx);
|
|
387
|
+
} else if (node.type === "partial") {
|
|
388
|
+
if (depth >= MAX_PARTIAL_DEPTH) continue;
|
|
389
|
+
const partial = await ViewLoader.loadPartial(node.name);
|
|
390
|
+
if (partial?.content) {
|
|
391
|
+
const partialAst = compileTemplate(partial.content);
|
|
392
|
+
out += await evaluateAst(partialAst, scopes, ctx, depth + 1);
|
|
393
|
+
}
|
|
394
|
+
} else if (node.type === "if") {
|
|
395
|
+
const branch = evaluateCondition(scopes, node.expr) ? node.then : node.else;
|
|
396
|
+
if (branch.length > 0) {
|
|
397
|
+
out += await evaluateAst(branch, scopes, ctx, depth);
|
|
398
|
+
}
|
|
399
|
+
} else if (node.type === "each") {
|
|
400
|
+
const list = resolveExpr(scopes, node.expr);
|
|
401
|
+
if (Array.isArray(list) && list.length > 0) {
|
|
402
|
+
for (let idx = 0; idx < list.length; idx++) {
|
|
403
|
+
const item = list[idx];
|
|
404
|
+
const iterScope =
|
|
405
|
+
item && typeof item === "object" && !Array.isArray(item)
|
|
406
|
+
? {
|
|
407
|
+
...item,
|
|
408
|
+
__this__: item,
|
|
409
|
+
"@index": idx,
|
|
410
|
+
"@number": idx + 1,
|
|
411
|
+
"@first": idx === 0,
|
|
412
|
+
"@last": idx === list.length - 1,
|
|
413
|
+
}
|
|
414
|
+
: {
|
|
415
|
+
__this__: item,
|
|
416
|
+
"@index": idx,
|
|
417
|
+
"@number": idx + 1,
|
|
418
|
+
"@first": idx === 0,
|
|
419
|
+
"@last": idx === list.length - 1,
|
|
420
|
+
};
|
|
421
|
+
out += await evaluateAst(node.body, [iterScope, ...scopes], ctx, depth);
|
|
422
|
+
}
|
|
423
|
+
} else if (node.else.length > 0) {
|
|
424
|
+
out += await evaluateAst(node.else, scopes, ctx, depth);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return out;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* HTML SSR template rendering engine for Bluebird.js.
|
|
433
|
+
* Supports `.html` views, `{{var}}`, `{{{raw}}}`, `{{if}}` / `{{else}}`, `{{each}}`,
|
|
434
|
+
* `{{> partial}}`, `{{ vite "entry" }}`, HTML layouts (`{{{ body }}}`), and multi-tier caching.
|
|
435
|
+
*/
|
|
436
|
+
export class Render {
|
|
437
|
+
/**
|
|
438
|
+
* Renders an HTML template string directly with the supplied data context.
|
|
439
|
+
* @param {string} template
|
|
440
|
+
* @param {Object} [data={}]
|
|
441
|
+
* @param {Object} [ctx={}]
|
|
442
|
+
* @returns {Promise<string>}
|
|
443
|
+
*/
|
|
444
|
+
static async renderString(template, data = {}, ctx = {}) {
|
|
445
|
+
const ast = compileTemplate(template);
|
|
446
|
+
const scope = data && typeof data === "object" ? { ...data } : {};
|
|
447
|
+
return evaluateAst(ast, [scope], ctx, 0);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Renders an HTML partial or view file to an HTML string without layouts.
|
|
452
|
+
* @param {string} viewName
|
|
453
|
+
* @param {Object} [data={}]
|
|
454
|
+
* @param {Object} [ctx={}]
|
|
455
|
+
* @returns {Promise<string>}
|
|
456
|
+
*/
|
|
457
|
+
static async component(viewName, data = {}, ctx = {}) {
|
|
458
|
+
const view = (await ViewLoader.loadPartial(viewName)) || (await ViewLoader.load(viewName));
|
|
459
|
+
if (!view?.content) return "";
|
|
460
|
+
return this.renderString(view.content, data, ctx);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Renders and sends an HTML view (`app/resources/html/*.html`) with `{{ }}` interpolation, optional layout,
|
|
465
|
+
* browser/server caching, CSRF token restoration, and CSP nonce injection.
|
|
466
|
+
* @param {import('express').Response} res
|
|
467
|
+
* @param {string} viewName
|
|
468
|
+
* @param {Object} [data={}]
|
|
469
|
+
* @param {number|Object} [options={}]
|
|
470
|
+
* @param {string|false} [options.layout]
|
|
471
|
+
* @param {number} [options.cache]
|
|
472
|
+
* @param {number} [options.ttl]
|
|
473
|
+
* @param {string[]} [options.query]
|
|
474
|
+
* @param {string[]} [options.cacheQuery]
|
|
475
|
+
* @param {boolean} [options.raw=false]
|
|
476
|
+
* @returns {Promise<any>}
|
|
477
|
+
*/
|
|
478
|
+
static async html(res, viewName, data = {}, options = {}) {
|
|
479
|
+
const templateData = data && typeof data === "object" ? data : {};
|
|
480
|
+
const normalizedOptions = typeof options === "number" ? { cache: options } : options || {};
|
|
481
|
+
return this.send(res, viewName, templateData, normalizedOptions);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Renders and sends an HTML view (`app/resources/html/*.html`) wrapped in an optional layout.
|
|
486
|
+
* Never mutates the caller's `data` object and never leaks CSRF tokens through shared caches.
|
|
487
|
+
* @param {import('express').Response} res
|
|
488
|
+
* @param {string} viewName
|
|
489
|
+
* @param {Object} [data={}]
|
|
490
|
+
* @param {number|{ttl?: number, cache?: number, layout?: string|false, query?: string[], cacheQuery?: string[], raw?: boolean}} [ttlOrOptions]
|
|
491
|
+
* @returns {Promise<any>}
|
|
492
|
+
*/
|
|
493
|
+
static async send(res, viewName, data = {}, ttlOrOptions) {
|
|
494
|
+
const req = res.req;
|
|
495
|
+
const opts = this._normalizeOptions(ttlOrOptions, data);
|
|
496
|
+
const partialRequest =
|
|
497
|
+
opts.layout === false ||
|
|
498
|
+
req?.headers?.["x-bluebird-partial"] === "1" ||
|
|
499
|
+
req?.query?._partial === "1";
|
|
500
|
+
|
|
501
|
+
const canCache = opts.cache > 0 && !DEBUG && !isAuthenticatedRequest(req);
|
|
502
|
+
const cacheKey = canCache
|
|
503
|
+
? buildCacheKey("view", req, opts.query, `${partialRequest ? "partial:" : ""}${viewName}`)
|
|
504
|
+
: null;
|
|
505
|
+
|
|
506
|
+
if (cacheKey) {
|
|
507
|
+
const cached = await RenderCache.lookup(cacheKey);
|
|
508
|
+
if (cached) {
|
|
509
|
+
res.set("Content-Type", "text/html; charset=utf-8");
|
|
510
|
+
res.set("X-Bluebird-Cache", "HIT");
|
|
511
|
+
if (opts.ttl > 0 && !DEBUG) {
|
|
512
|
+
res.set("Cache-Control", `public, max-age=${opts.ttl}`);
|
|
513
|
+
}
|
|
514
|
+
const token = typeof res.cookie === "function" ? Csrf.ensure(req, res) : "";
|
|
515
|
+
return res.send(applyCspNonce(detokenizeFromCache(cached, token), res));
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const view = await ViewLoader.load(viewName);
|
|
520
|
+
if (!view) {
|
|
521
|
+
return res.status(404).json({ message: `View '${viewName}' not found.` });
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const csrfToken = typeof res.cookie === "function" ? Csrf.ensure(req, res) : "";
|
|
525
|
+
const lang = data?.lang || res.locals?.lang || req?.lang || "en";
|
|
526
|
+
const viewData = {
|
|
527
|
+
...(req?.app?.locals || {}),
|
|
528
|
+
...(res.locals || {}),
|
|
529
|
+
lang,
|
|
530
|
+
csrf: csrfToken,
|
|
531
|
+
csrfToken,
|
|
532
|
+
csrfField: new SafeHtml(`<input type="hidden" name="_csrf" value="${escapeHtml(csrfToken)}">`),
|
|
533
|
+
url: req?.originalUrl || req?.url || "/",
|
|
534
|
+
path: req?.path || "/",
|
|
535
|
+
appUrl: Config.props().appUrl,
|
|
536
|
+
...data,
|
|
537
|
+
};
|
|
538
|
+
delete viewData._cache;
|
|
539
|
+
delete viewData._layout;
|
|
540
|
+
delete viewData._raw;
|
|
541
|
+
delete viewData._viteBridge;
|
|
542
|
+
|
|
543
|
+
const renderCtx = {
|
|
544
|
+
req,
|
|
545
|
+
res,
|
|
546
|
+
vite: ViteBridge.from(req) || ViteBridge.from(res),
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
let body = view.content || "";
|
|
550
|
+
if (!opts.raw) {
|
|
551
|
+
body = await this.renderString(body, viewData, renderCtx);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
let rendered = body;
|
|
555
|
+
if (opts.layout && !partialRequest) {
|
|
556
|
+
const layout = await ViewLoader.loadLayout(opts.layout);
|
|
557
|
+
if (layout?.content) {
|
|
558
|
+
const layoutData = {
|
|
559
|
+
...viewData,
|
|
560
|
+
body: new SafeHtml(body),
|
|
561
|
+
};
|
|
562
|
+
rendered = await this.renderString(layout.content, layoutData, renderCtx);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
if (DEBUG && !partialRequest && renderCtx.vite?.mode === "dev" && /<head[\s>]/i.test(rendered)) {
|
|
567
|
+
rendered = await renderCtx.vite.transformHtml(req?.originalUrl || req?.url || "/", rendered);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
if (cacheKey && (res.statusCode === 200 || res.statusCode === undefined)) {
|
|
571
|
+
await RenderCache.store(cacheKey, tokenizeForCache(rendered, csrfToken), opts.cache);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
if (opts.ttl > 0 && !DEBUG) {
|
|
575
|
+
res.set("Cache-Control", `public, max-age=${opts.ttl}`);
|
|
576
|
+
}
|
|
577
|
+
res.set("Content-Type", "text/html; charset=utf-8");
|
|
578
|
+
res.set("X-Bluebird-Cache", "MISS");
|
|
579
|
+
return res.send(applyCspNonce(rendered, res));
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Sends a partial HTML view without applying a layout.
|
|
584
|
+
* @param {import('express').Response} res
|
|
585
|
+
* @param {string} partialName
|
|
586
|
+
* @param {Object} [data={}]
|
|
587
|
+
* @returns {Promise<any>}
|
|
588
|
+
*/
|
|
589
|
+
static async partial(res, partialName, data = {}) {
|
|
590
|
+
const view = (await ViewLoader.loadPartial(partialName)) || (await ViewLoader.load(partialName));
|
|
591
|
+
if (!view) {
|
|
592
|
+
return res.status(404).json({ message: `Partial '${partialName}' not found.` });
|
|
593
|
+
}
|
|
594
|
+
const req = res.req;
|
|
595
|
+
const csrfToken = typeof res.cookie === "function" ? Csrf.ensure(req, res) : "";
|
|
596
|
+
const lang = data?.lang || res.locals?.lang || req?.lang || "en";
|
|
597
|
+
const viewData = {
|
|
598
|
+
...(req?.app?.locals || {}),
|
|
599
|
+
...(res.locals || {}),
|
|
600
|
+
lang,
|
|
601
|
+
csrf: csrfToken,
|
|
602
|
+
csrfToken,
|
|
603
|
+
csrfField: new SafeHtml(`<input type="hidden" name="_csrf" value="${escapeHtml(csrfToken)}">`),
|
|
604
|
+
...data,
|
|
605
|
+
};
|
|
606
|
+
delete viewData._cache;
|
|
607
|
+
delete viewData._layout;
|
|
608
|
+
delete viewData._viteBridge;
|
|
609
|
+
|
|
610
|
+
const rendered = await this.renderString(view.content, viewData, {
|
|
611
|
+
req,
|
|
612
|
+
res,
|
|
613
|
+
vite: ViteBridge.from(req) || ViteBridge.from(res),
|
|
614
|
+
});
|
|
615
|
+
res.set("Content-Type", "text/html; charset=utf-8");
|
|
616
|
+
return res.send(applyCspNonce(rendered, res));
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Creates an Express route handler that renders an HTML view with optional dynamic data loader.
|
|
621
|
+
* @param {string} viewName
|
|
622
|
+
* @param {Object|Function} [dataOrFn={}] - Static data object or `(req, res) => data`.
|
|
623
|
+
* @param {number|Object} [options={}]
|
|
624
|
+
* @returns {Function}
|
|
625
|
+
*/
|
|
626
|
+
static view(viewName, dataOrFn = {}, options = {}) {
|
|
627
|
+
return async (req, res) => {
|
|
628
|
+
const data = typeof dataOrFn === "function" ? await dataOrFn(req, res) : dataOrFn;
|
|
629
|
+
return Render.send(res, viewName, data, options);
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* Normalizes layout, cache, ttl, raw, and query allowlist options.
|
|
635
|
+
* @param {number|Object} [ttlOrOptions]
|
|
636
|
+
* @param {Object} [data={}]
|
|
637
|
+
* @returns {{ cache: number, ttl: number, layout: string|false|undefined, query: string[], raw: boolean }}
|
|
638
|
+
* @private
|
|
639
|
+
*/
|
|
640
|
+
static _normalizeOptions(ttlOrOptions, data = {}) {
|
|
641
|
+
const out = {
|
|
642
|
+
cache: 0,
|
|
643
|
+
ttl: 0,
|
|
644
|
+
layout: data?._layout,
|
|
645
|
+
query: [],
|
|
646
|
+
raw: Boolean(data?._raw),
|
|
647
|
+
};
|
|
648
|
+
if (typeof ttlOrOptions === "number") {
|
|
649
|
+
out.cache = ttlOrOptions;
|
|
650
|
+
} else if (ttlOrOptions && typeof ttlOrOptions === "object") {
|
|
651
|
+
if (ttlOrOptions.cache !== undefined) out.cache = Number(ttlOrOptions.cache) || 0;
|
|
652
|
+
else if (ttlOrOptions.ttl !== undefined && !ttlOrOptions.raw) out.cache = Number(ttlOrOptions.ttl) || 0;
|
|
653
|
+
if (ttlOrOptions.ttl !== undefined) out.ttl = Number(ttlOrOptions.ttl) || 0;
|
|
654
|
+
if (ttlOrOptions.layout !== undefined) out.layout = ttlOrOptions.layout;
|
|
655
|
+
if (Array.isArray(ttlOrOptions.cacheQuery)) out.query = ttlOrOptions.cacheQuery;
|
|
656
|
+
else if (Array.isArray(ttlOrOptions.query)) out.query = ttlOrOptions.query;
|
|
657
|
+
if (ttlOrOptions.raw !== undefined) out.raw = Boolean(ttlOrOptions.raw);
|
|
658
|
+
}
|
|
659
|
+
if (data?._cache !== undefined && !out.cache) {
|
|
660
|
+
out.cache = Number(data._cache) || 0;
|
|
661
|
+
}
|
|
662
|
+
return out;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* Express middleware caching HTML responses of GET routes.
|
|
667
|
+
* @param {number} [seconds]
|
|
668
|
+
* @param {Object} [options={}]
|
|
669
|
+
* @param {boolean} [options.allowAuthenticated=false]
|
|
670
|
+
* @param {string[]} [options.query=[]]
|
|
671
|
+
* @returns {Function}
|
|
672
|
+
*/
|
|
673
|
+
static cache(seconds, options = {}) {
|
|
674
|
+
const ttl = seconds ?? RENDER_CACHE_TTL;
|
|
675
|
+
const allowAuthenticated = options.allowAuthenticated === true;
|
|
676
|
+
const queryKeys = Array.isArray(options.query) ? options.query : [];
|
|
677
|
+
|
|
678
|
+
return async (req, res, next) => {
|
|
679
|
+
if (DEBUG || req.method !== "GET") return next();
|
|
680
|
+
if (!allowAuthenticated && isAuthenticatedRequest(req)) return next();
|
|
681
|
+
|
|
682
|
+
const key = buildCacheKey("route", req, queryKeys);
|
|
683
|
+
if (!key) return next();
|
|
684
|
+
|
|
685
|
+
const cached = await RenderCache.lookup(key);
|
|
686
|
+
if (cached) {
|
|
687
|
+
res.set("Content-Type", "text/html; charset=utf-8");
|
|
688
|
+
res.set("X-Bluebird-Cache", "HIT");
|
|
689
|
+
return res.send(applyCspNonce(detokenizeFromCache(cached, Csrf.ensure(req, res)), res));
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
const originalSend = res.send.bind(res);
|
|
693
|
+
res.send = async (body) => {
|
|
694
|
+
res.send = originalSend;
|
|
695
|
+
if (typeof body === "string" && res.statusCode === 200) {
|
|
696
|
+
const token = res.locals?.csrfToken || req.cookies?._csrf || "";
|
|
697
|
+
await RenderCache.store(key, tokenizeForCache(stripCspNonce(body, res), token), ttl);
|
|
698
|
+
}
|
|
699
|
+
res.set("X-Bluebird-Cache", "MISS");
|
|
700
|
+
return originalSend(body);
|
|
701
|
+
};
|
|
702
|
+
next();
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Performs a standard HTTP redirect.
|
|
708
|
+
* @param {import('express').Request} req
|
|
709
|
+
* @param {import('express').Response} res
|
|
710
|
+
* @param {string} url
|
|
711
|
+
* @param {number} [status=302]
|
|
712
|
+
*/
|
|
713
|
+
static redirect(req, res, url, status = 302) {
|
|
714
|
+
return res.redirect(status, url);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* Clears all in-memory view, compiled template, and render caches.
|
|
719
|
+
*/
|
|
720
|
+
static clearTemplateCache() {
|
|
721
|
+
compiledCache.clear();
|
|
722
|
+
ViewLoader.clearCache();
|
|
723
|
+
RenderCache.clearMemory();
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Invalidates cached views and routes for paths or cache keys.
|
|
728
|
+
* @param {string|string[]} [keys]
|
|
729
|
+
* @returns {Promise<void>}
|
|
730
|
+
*/
|
|
731
|
+
static async invalidate(keys) {
|
|
732
|
+
compiledCache.clear();
|
|
733
|
+
ViewLoader.clearCache();
|
|
734
|
+
await RenderCache.invalidate(keys);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
export default Render;
|