@tylix/core 0.7.0 → 1.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tylix/core",
3
- "version": "0.7.0",
3
+ "version": "1.1.0",
4
4
  "description": "The Tylix server core: HTTP server and router, logging, metrics and health checks, a shared store (memory, database, Redis), cache, rate limiting, queue, scheduler and channels.",
5
5
  "license": "MIT",
6
6
  "author": "Mbiyrukzz",
@@ -44,9 +44,9 @@
44
44
  "test": "node --test 'src/**/*.test.js'"
45
45
  },
46
46
  "dependencies": {
47
- "@tylix/auth": "^0.7.0",
48
- "@tylix/orm": "^0.7.0",
49
- "@tylix/shared": "^0.7.0",
47
+ "@tylix/auth": "^1.1.0",
48
+ "@tylix/orm": "^1.1.0",
49
+ "@tylix/shared": "^1.1.0",
50
50
  "ws": "^8.18.0"
51
51
  },
52
52
  "devDependencies": {
@@ -10,6 +10,7 @@ import { parseQuery } from './parseQuery.js'
10
10
  import { parseCookies } from './parseCookies.js'
11
11
  import { buildDebugPayload } from './debug/buildDebugPayload.js'
12
12
  import { renderDebugPage } from './debug/renderDebugPage.js'
13
+ import { renderStatusPage } from './statusPage.js'
13
14
 
14
15
  const UNSAFE_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE'])
15
16
  const DEFAULT_MAX_BODY_BYTES = 1024 * 1024 // 1 MB
@@ -143,6 +144,14 @@ function wantsHtml(req) {
143
144
  )
144
145
  }
145
146
 
147
+ // The request's own path, for a "try again" link. Anything that isn't a
148
+ // plain same-site path ("//other.example" is a valid request line) goes
149
+ // to the home page instead.
150
+ function samePagePath(url) {
151
+ const value = String(url ?? '/')
152
+ return value.startsWith('/') && !value.startsWith('//') && !value.startsWith('/\\') ? value : '/'
153
+ }
154
+
146
155
  export class Server {
147
156
  constructor(
148
157
  router,
@@ -345,6 +354,15 @@ export class Server {
345
354
  const handled = await this.notFoundHandler(req, res)
346
355
  if (handled) return
347
356
  }
357
+ if (wantsHtml(req)) {
358
+ this.sendStatusPage(res, 404, {
359
+ tone: 'notfound',
360
+ code: '404',
361
+ title: 'Page not found',
362
+ message: "The page you're looking for doesn't exist or has been moved.",
363
+ })
364
+ return
365
+ }
348
366
  res.status(404).json({ error: 'Not found' })
349
367
  return
350
368
  }
@@ -429,6 +447,14 @@ export class Server {
429
447
  }
430
448
 
431
449
  if (badRequest) {
450
+ if (wantsHtml(req)) {
451
+ this.sendStatusPage(res, 400, {
452
+ code: '400',
453
+ title: 'Bad request',
454
+ message: "The address of this page isn't valid. Check the link and try again.",
455
+ })
456
+ return
457
+ }
432
458
  res.status(400).json({ error: 'Bad request' })
433
459
  return
434
460
  }
@@ -443,11 +469,34 @@ export class Server {
443
469
  }
444
470
  }
445
471
 
472
+ // A browser navigation gets a page, not JSON. The details stay in the
473
+ // log; the page shows the request id to match it up.
474
+ if (wantsHtml(req)) {
475
+ this.sendStatusPage(res, 500, {
476
+ code: '500',
477
+ title: 'Something went wrong',
478
+ message: 'The server hit an unexpected error. Please try again in a moment.',
479
+ requestId: req.id,
480
+ actions: [
481
+ { label: 'Try again', href: samePagePath(req.url) },
482
+ { label: 'Go home', href: '/' },
483
+ ],
484
+ })
485
+ return
486
+ }
487
+
446
488
  res.status(500).json({
447
489
  error: this.production ? 'Internal server error' : err.message,
448
490
  })
449
491
  }
450
492
 
493
+ sendStatusPage(res, status, page) {
494
+ res.statusCode = status
495
+ res.setHeader('Content-Type', 'text/html; charset=utf-8')
496
+ res.setHeader('Cache-Control', 'no-store')
497
+ res.end(renderStatusPage(page))
498
+ }
499
+
451
500
  // Starts listening on `port`. If that port is taken and autoPort is on,
452
501
  // tries port + 1, port + 2, ... up to maxPortAttempts times.
453
502
  //
@@ -0,0 +1,20 @@
1
+ export interface StatusPageAction {
2
+ label: string
3
+ href: string
4
+ }
5
+
6
+ export interface StatusPageOptions {
7
+ title: string
8
+ message: string
9
+ /** Colour and icon. Defaults to 'error'. */
10
+ tone?: 'success' | 'error' | 'notfound'
11
+ /** Small label above the title, e.g. '404'. */
12
+ code?: string | null
13
+ /** Shown as a reference the visitor can quote; matches the request's log line. */
14
+ requestId?: string | null
15
+ /** Buttons; the first is the primary one. Defaults to "Go home". */
16
+ actions?: StatusPageAction[]
17
+ }
18
+
19
+ /** A self-contained, themed HTML page for a status a browser should see as a page. */
20
+ export function renderStatusPage(options: StatusPageOptions): string
@@ -0,0 +1,120 @@
1
+ function esc(value) {
2
+ return String(value ?? '')
3
+ .replace(/&/g, '&')
4
+ .replace(/</g, '&lt;')
5
+ .replace(/>/g, '&gt;')
6
+ .replace(/"/g, '&quot;')
7
+ }
8
+
9
+ const ICONS = {
10
+ success:
11
+ '<svg viewBox="0 0 24 24" width="28" height="28" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6L9 17l-5-5"/></svg>',
12
+ error:
13
+ '<svg viewBox="0 0 24 24" width="28" height="28" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6L6 18M6 6l12 12"/></svg>',
14
+ notfound:
15
+ '<svg viewBox="0 0 24 24" width="28" height="28" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="7"/><path d="M21 21l-4.3-4.3"/></svg>',
16
+ }
17
+
18
+ // A themed, self-contained page for a status a browser should see as a
19
+ // page rather than JSON: 404, a production 500, an email link's result.
20
+ // It needs nothing from the app (styles are inline), so it still works
21
+ // when the app itself is what broke.
22
+ //
23
+ // tone: 'success' | 'error' | 'notfound'
24
+ // requestId: shown as a reference the visitor can quote when reporting
25
+ // the problem; the same id is on the server's log line for the request.
26
+ export function renderStatusPage({
27
+ title,
28
+ message,
29
+ tone = 'error',
30
+ code = null,
31
+ requestId = null,
32
+ actions = [{ label: 'Go home', href: '/' }],
33
+ }) {
34
+ const buttons = actions
35
+ .map(
36
+ (a, i) =>
37
+ `<a class="btn${i === 0 ? ' btn-primary' : ''}" href="${esc(a.href)}">${esc(a.label)}</a>`,
38
+ )
39
+ .join('')
40
+
41
+ return `<!DOCTYPE html>
42
+ <html lang="en">
43
+ <head>
44
+ <meta charset="UTF-8">
45
+ <meta name="viewport" content="width=device-width, initial-scale=1">
46
+ <title>${esc(title)}</title>
47
+ <link rel="icon" type="image/png" href="/favicon.png">
48
+ <script>
49
+ (function () {
50
+ var stored = null;
51
+ try { stored = localStorage.getItem("theme"); } catch (e) {}
52
+ var prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
53
+ if (stored === "dark" || (!stored && prefersDark)) document.documentElement.classList.add("dark");
54
+ })();
55
+ </script>
56
+ <style>
57
+ * { box-sizing: border-box; }
58
+ :root {
59
+ --bg:#f8fafc; --card:#ffffff; --border:#e2e8f0; --border-strong:#cbd5e1;
60
+ --text:#0f172a; --muted:#64748b;
61
+ --success:#059669; --success-soft:rgba(16,185,129,.12);
62
+ --error:#e11d48; --error-soft:rgba(225,29,72,.10);
63
+ --notfound:#d97706; --notfound-soft:rgba(245,158,11,.14);
64
+ }
65
+ html.dark {
66
+ --bg:#020617; --card:#0f172a; --border:#1e293b; --border-strong:#334155;
67
+ --text:#e2e8f0; --muted:#94a3b8;
68
+ --success:#34d399; --success-soft:rgba(52,211,153,.12);
69
+ --error:#fb7185; --error-soft:rgba(251,113,133,.12);
70
+ --notfound:#fbbf24; --notfound-soft:rgba(251,191,36,.12);
71
+ }
72
+ html { color-scheme: light; } html.dark { color-scheme: dark; }
73
+ body {
74
+ margin:0; min-height:100vh; display:flex; align-items:center; justify-content:center; padding:24px;
75
+ background:var(--bg); color:var(--text); -webkit-font-smoothing:antialiased;
76
+ font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Inter, sans-serif;
77
+ }
78
+ .card {
79
+ width:100%; max-width:440px; text-align:center; padding:36px 32px 32px;
80
+ background:var(--card); border:1px solid var(--border); border-radius:20px;
81
+ box-shadow:0 1px 2px rgba(15,23,42,.05), 0 12px 32px rgba(15,23,42,.06);
82
+ }
83
+ html.dark .card { box-shadow:none; }
84
+ .brand { display:inline-flex; align-items:center; gap:8px; margin-bottom:24px; font-weight:600; color:var(--text); text-decoration:none; }
85
+ .brand img { width:26px; height:26px; }
86
+ .icon { width:60px; height:60px; margin:0 auto 18px; border-radius:50%; display:grid; place-items:center;
87
+ color:var(--tone); background:var(--tone-soft); }
88
+ .tone-success { --tone:var(--success); --tone-soft:var(--success-soft); }
89
+ .tone-error { --tone:var(--error); --tone-soft:var(--error-soft); }
90
+ .tone-notfound { --tone:var(--notfound); --tone-soft:var(--notfound-soft); }
91
+ .code { font-size:12px; font-weight:700; letter-spacing:.1em; color:var(--tone); margin:0 0 6px; }
92
+ h1 { margin:0 0 8px; font-size:22px; letter-spacing:-.01em; }
93
+ p.msg { margin:0 0 24px; color:var(--muted); font-size:14px; line-height:1.6; }
94
+ .actions { display:flex; gap:10px; justify-content:center; flex-wrap:wrap; }
95
+ .btn {
96
+ font-size:14px; font-weight:600; text-decoration:none; padding:9px 18px; border-radius:999px;
97
+ color:var(--text); border:1px solid var(--border-strong); background:transparent; transition:all .15s;
98
+ }
99
+ .btn:hover { border-color:var(--success); color:var(--success); }
100
+ .btn-primary { color:#fff; border-color:transparent; background:linear-gradient(90deg,#34d399,#059669); }
101
+ .btn-primary:hover { color:#fff; transform:translateY(-1px); box-shadow:0 6px 16px rgba(5,150,105,.3); }
102
+ .ref { margin:22px 0 0; font-size:12px; color:var(--muted); }
103
+ .ref code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size:11px; padding:2px 6px;
104
+ border-radius:6px; background:var(--bg); border:1px solid var(--border); user-select:all; }
105
+ </style>
106
+ </head>
107
+ <body>
108
+ <div class="card tone-${esc(tone)}">
109
+ <a class="brand" href="/"><img src="/logo-mark.png" alt="" onerror="this.remove()">Tylix</a>
110
+ <div class="icon">${ICONS[tone] ?? ICONS.error}</div>
111
+ ${code ? `<p class="code">${esc(code)}</p>` : ''}
112
+ <h1>${esc(title)}</h1>
113
+ <p class="msg">${esc(message)}</p>
114
+ <div class="actions">${buttons}</div>
115
+ ${requestId ? `<p class="ref">Reference <code>${esc(requestId)}</code></p>` : ''}
116
+ </div>
117
+ </body>
118
+ </html>`
119
+ }
120
+
package/src/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './router/Router.js'
2
2
  export * from './http/Server.js'
3
+ export * from './http/statusPage.js'
3
4
  export * from './http/enhanceResponse.js'
4
5
  export * from './http/parseBody.js'
5
6
  export * from './http/parseQuery.js'
package/src/index.js CHANGED
@@ -1,5 +1,8 @@
1
1
  export * from './router/Router.js'
2
2
  export * from './http/Server.js'
3
+ export * from './http/statusPage.js'
4
+ export { buildDebugPayload } from './http/debug/buildDebugPayload.js'
5
+ export { renderDebugPage } from './http/debug/renderDebugPage.js'
3
6
  export * from './http/enhanceResponse.js'
4
7
  export * from './http/parseBody.js'
5
8
  export * from './http/parseQuery.js'