@gitsang/dsh-gateway 0.0.0-stage → 0.5.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/src/pages.js ADDED
@@ -0,0 +1,149 @@
1
+ import { PWA_HEAD_TAGS } from './pwa.js'
2
+
3
+ function escapeHtml(value) {
4
+ return String(value)
5
+ .replaceAll('&', '&')
6
+ .replaceAll('<', '&lt;')
7
+ .replaceAll('>', '&gt;')
8
+ .replaceAll('"', '&quot;')
9
+ .replaceAll("'", '&#39;')
10
+ }
11
+
12
+ /**
13
+ * Read a request body as UTF-8. Keeps an upper bound so a stray client cannot
14
+ * make the gateway buffer an unbounded body. Used for the gateway's own JSON
15
+ * settings API; proxied traffic streams through untouched.
16
+ */
17
+ export function readRequestBody(req, limit = 64 * 1024) {
18
+ return new Promise((resolve, reject) => {
19
+ const chunks = []
20
+ let received = 0
21
+ req.on('data', (chunk) => {
22
+ received += chunk.length
23
+ if (received > limit) {
24
+ reject(new Error('request body too large'))
25
+ req.destroy()
26
+ return
27
+ }
28
+ chunks.push(chunk)
29
+ })
30
+ req.on('end', () => {
31
+ resolve(Buffer.concat(chunks).toString('utf8'))
32
+ })
33
+ req.on('error', reject)
34
+ })
35
+ }
36
+
37
+ // Mirrors the dsh web design tokens (@deepseek-ai/dsh-client-ui-theme).
38
+ // This standalone page cannot read the web settings store, so it follows the
39
+ // system color scheme; that is also the web UI's default theme preference.
40
+ const PAGE_THEME_CSS = `
41
+ :root {
42
+ color-scheme: light dark;
43
+ --dsw-font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC',
44
+ 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
45
+ --dsw-alias-bg-base: #ffffff;
46
+ --dsw-alias-bg-layer-1: #ffffff;
47
+ --dsw-alias-border-l2: rgba(0, 0, 0, 0.10);
48
+ --dsw-alias-brand-primary: #0f1115;
49
+ --dsw-alias-label-primary: #0f1115;
50
+ --dsw-alias-label-secondary: #61666b;
51
+ --dsw-alias-label-tertiary: #81858c;
52
+ --dsw-alias-label-dimmed: #e1e5ee;
53
+ --dsw-shadow-lv3: 0 0 1px 0 rgba(0, 0, 0, 0.20), 0 0 4px 0 rgba(0, 0, 0, 0.02), 0 12px 32px 0 rgba(0, 0, 0, 0.08);
54
+ }
55
+
56
+ @media (prefers-color-scheme: dark) {
57
+ :root {
58
+ --dsw-alias-bg-base: #151517;
59
+ --dsw-alias-bg-layer-1: #232325;
60
+ --dsw-alias-border-l2: rgba(255, 255, 255, 0.12);
61
+ --dsw-alias-brand-primary: #f9fafb;
62
+ --dsw-alias-label-primary: #f9fafb;
63
+ --dsw-alias-label-secondary: #cfd3d6;
64
+ --dsw-alias-label-tertiary: #adb2b8;
65
+ --dsw-alias-label-dimmed: #43454a;
66
+ }
67
+ }
68
+
69
+ body {
70
+ margin: 0;
71
+ min-height: 100vh;
72
+ display: flex;
73
+ align-items: center;
74
+ justify-content: center;
75
+ background: var(--dsw-alias-bg-base);
76
+ color: var(--dsw-alias-label-primary);
77
+ font-family: var(--dsw-font-family);
78
+ }
79
+
80
+ main {
81
+ width: 100%;
82
+ max-width: 380px;
83
+ margin: 0 auto;
84
+ padding: 32px;
85
+ box-sizing: border-box;
86
+ background: var(--dsw-alias-bg-layer-1);
87
+ border: 0.5px solid var(--dsw-alias-border-l2);
88
+ border-radius: 16px;
89
+ box-shadow: var(--dsw-shadow-lv3);
90
+ }
91
+
92
+ h1 {
93
+ margin: 0 0 8px;
94
+ font-size: 20px;
95
+ line-height: 28px;
96
+ font-weight: 600;
97
+ }
98
+
99
+ p {
100
+ margin: 0 0 12px;
101
+ font-size: 14px;
102
+ line-height: 22px;
103
+ color: var(--dsw-alias-label-secondary);
104
+ }
105
+
106
+ p.hint {
107
+ color: var(--dsw-alias-label-tertiary);
108
+ }
109
+
110
+ code {
111
+ font-family: var(--dsw-font-mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
112
+ font-size: 12px;
113
+ padding: 1px 4px;
114
+ border-radius: 4px;
115
+ background: var(--dsw-alias-bg-base);
116
+ border: 0.5px solid var(--dsw-alias-border-l2);
117
+ }
118
+ `
119
+
120
+ /**
121
+ * Render the page shown when dsh rejects a browser that has no valid session.
122
+ *
123
+ * The gateway no longer authenticates anyone itself: authentication is dsh's
124
+ * launch token exchanged for a signed cookie, and the only way to obtain it is
125
+ * the URL printed by `dsh web` (or a `?token=` URL the gateway relays). This
126
+ * page exists so a plain visit explains that instead of showing dsh's bare
127
+ * one-line 401.
128
+ */
129
+ export function renderUnauthorizedPage({ pwa }) {
130
+ return `<!doctype html>
131
+ <html lang="zh-CN">
132
+ <head>
133
+ <meta charset="utf-8">
134
+ <meta name="viewport" content="width=device-width, initial-scale=1">
135
+ <title>需要登录 · dsh gateway</title>
136
+ ${pwa === false ? '' : PWA_HEAD_TAGS}
137
+ <style>${PAGE_THEME_CSS}</style>
138
+ </head>
139
+ <body>
140
+ <main>
141
+ <h1>需要登录</h1>
142
+ <p>网关已把请求转给 dsh,但 dsh 没有收到有效的登录凭据。</p>
143
+ <p>请用 <code>dsh web</code> 启动时打印的链接打开本页,地址里带一个一次性的 <code>?token=</code> 参数。它会被换成 dsh 自己的签名 cookie,随后地址栏里的 token 会自动去掉。</p>
144
+ <p class="hint">dsh 重启后 launch token 会变,旧 cookie 也会失效:重新打开一次新的启动链接即可。</p>
145
+ </main>
146
+ </body>
147
+ </html>
148
+ `
149
+ }