dsh-wtq-pool 1.0.1 → 1.0.3

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.
@@ -1,339 +0,0 @@
1
- /*
2
- * Demo data for the panel preview page (docs/index.html).
3
- *
4
- * Synthetic only - accounts, credits and model ids here are illustrative and
5
- * belong to nobody. The shapes follow what renderer/panel.js actually reads, so
6
- * the preview exercises the real component.
7
- */
8
- (function () {
9
- const iso = (days) => new Date(Date.now() + days * 86400000).toISOString()
10
- const ms = (days) => Date.now() + days * 86400000
11
-
12
- const state = {
13
- sources: { workbuddy: true, trae: true, qoder: true },
14
- disabledRegions: [],
15
- pluginVersion: '1.0.1',
16
- unpublishedDeclarations: [],
17
- modelCap: 64,
18
- distribution: 'priority',
19
- showComposerPoints: true,
20
- autoCheckin: {
21
- enabled: true,
22
- hours: [9],
23
- last: {
24
- slot: '2026-10-10T09',
25
- claimed: 3,
26
- at: iso(-0.2),
27
- travel: { departed: 2, claimed: 2, adopted: 1 },
28
- growth: { streak: 1, tasks: 2, credit: 400, failed: 0 },
29
- qoder: { claimed: 1, skipped: 0, failed: 0 },
30
- trae: { claimed: 1, skipped: 0, failed: 0 },
31
- },
32
- },
33
- preferredAccountIds: { cn: 'wb-cn-1', global: '' },
34
- removedAccounts: [],
35
- declaration: { ok: true, changed: false, providerCount: 6 },
36
- addressDrift: false,
37
- shims: {
38
- cn: { running: true, baseUrl: 'http://127.0.0.1:42831/v1' },
39
- global: { running: true, baseUrl: 'http://127.0.0.1:42841/v1' },
40
- },
41
- warnings: {},
42
- staleSelectionRegions: [],
43
- refreshing: false,
44
-
45
- accounts: [
46
- {
47
- id: 'wb-cn-1',
48
- label: 'lin.cn@example.com',
49
- region: 'cn',
50
- domain: 'codebuddy.cn',
51
- expiresAt: iso(0.02),
52
- cooling: false,
53
- rateLimitHits: 0,
54
- active: true,
55
- preferred: true,
56
- disabled: false,
57
- creditReserve: 2000,
58
- credits: {
59
- total: 18320,
60
- expiringSoon: 1200,
61
- nearestExpiryMs: ms(2),
62
- packages: [
63
- { nameKey: 'credits.planQuota', remain: 14200, size: 20000, monthly: true, refreshAtMs: ms(21) },
64
- { nameKey: 'credits.addOnQuota', remain: 3200, size: 3200, roundExpiresAtMs: ms(2) },
65
- ],
66
- },
67
- checkin: {
68
- active: true,
69
- todayCheckedIn: false,
70
- streakDays: 12,
71
- dailyCredit: 20,
72
- todayCredit: 0,
73
- isStreakDay: false,
74
- nextStreakDay: 14,
75
- streakBonusCredit: 100,
76
- },
77
- },
78
- {
79
- id: 'wb-cn-2',
80
- label: 'du.cn@example.com',
81
- region: 'cn',
82
- domain: 'codebuddy.cn',
83
- expiresAt: iso(4),
84
- cooling: false,
85
- rateLimitHits: 1,
86
- active: false,
87
- preferred: false,
88
- disabled: false,
89
- credits: {
90
- total: 4100,
91
- packages: [
92
- { nameKey: 'credits.planQuota', remain: 4100, size: 8000, monthly: true, refreshAtMs: ms(12) },
93
- ],
94
- },
95
- checkin: { active: true, todayCheckedIn: true, todayCredit: 20, streakDays: 5, dailyCredit: 20 },
96
- },
97
- {
98
- id: 'wb-cn-3',
99
- label: 'wu.cn@example.com',
100
- region: 'cn',
101
- domain: 'codebuddy.cn',
102
- expiresAt: iso(6),
103
- cooling: true,
104
- cooldownUntil: new Date(Date.now() + 9 * 60000).toISOString(),
105
- modelCooldowns: [{ modelId: 'claude-sonnet-4.5', until: new Date(Date.now() + 5 * 60000).toISOString() }],
106
- rateLimitHits: 4,
107
- active: false,
108
- preferred: false,
109
- disabled: false,
110
- credits: { total: 980, packages: [{ packageName: 'Top-up', remain: 980, size: 1000, validDays: 30 }] },
111
- checkin: { active: true, todayCheckedIn: false, streakDays: 3, dailyCredit: 20, todayCredit: 0 },
112
- },
113
- {
114
- id: 'wb-global-1',
115
- label: 'lin.intl@example.com',
116
- region: 'global',
117
- domain: 'workbuddy.ai',
118
- expiresAt: iso(9),
119
- cooling: false,
120
- rateLimitHits: 0,
121
- active: true,
122
- preferred: false,
123
- disabled: false,
124
- credits: {
125
- total: 6400,
126
- packages: [{ nameKey: 'credits.planQuota', remain: 6400, size: 10000, monthly: true, refreshAtMs: ms(17) }],
127
- },
128
- checkin: { active: true, todayCheckedIn: false, streakDays: 2, dailyCredit: 20, todayCredit: 0 },
129
- },
130
- ],
131
-
132
- models: [
133
- {
134
- id: 'claude-sonnet-4.5', name: 'Claude Sonnet 4.5', alias: 'sonnet', region: 'cn', multiplier: 1,
135
- supportsImages: true, contextWindow: 200000, maxOutputTokens: 32000,
136
- nativeContextWindow: 200000, nativeMaxOutputTokens: 32000, nativeSupportsImages: true,
137
- nativeThinkingLevels: ['off', 'low', 'medium', 'high'], advanced: true,
138
- },
139
- {
140
- id: 'gemini-2.5-pro', name: 'Gemini 2.5 Pro', alias: 'gemini', region: 'cn', multiplier: 0.5,
141
- supportsImages: true, contextWindow: 1000000, maxOutputTokens: 64000,
142
- nativeContextWindow: 1000000, nativeMaxOutputTokens: 64000, nativeSupportsImages: true,
143
- nativeThinkingLevels: ['off', 'low', 'medium', 'high'], advanced: false,
144
- },
145
- {
146
- id: 'gpt-5.1-codex', name: 'GPT-5.1 Codex', region: 'cn', multiplier: 0,
147
- promo: { kind: 'night', untilHour: 6 },
148
- supportsImages: false, contextWindow: 400000, maxOutputTokens: 128000,
149
- nativeContextWindow: 400000, nativeMaxOutputTokens: 128000, nativeSupportsImages: false,
150
- nativeThinkingLevels: ['off', 'minimal', 'low', 'medium', 'high', 'xhigh'], advanced: false,
151
- },
152
- {
153
- id: 'kimi-k2-thinking', name: 'Kimi K2 Thinking', alias: 'kimi', region: 'cn', multiplier: 0.2,
154
- supportsImages: false, contextWindow: 256000, maxOutputTokens: 32000,
155
- nativeContextWindow: 256000, nativeMaxOutputTokens: 32000, nativeSupportsImages: false,
156
- nativeThinkingLevels: ['off', 'low', 'medium', 'high'], advanced: true,
157
- },
158
- {
159
- id: 'deepseek-v3.2', name: 'DeepSeek V3.2', region: 'cn', multiplier: 0.15,
160
- supportsImages: false, contextWindow: 128000, maxOutputTokens: 16000,
161
- nativeContextWindow: 128000, nativeMaxOutputTokens: 16000, nativeSupportsImages: false,
162
- nativeThinkingLevels: [], advanced: false,
163
- },
164
- {
165
- id: 'glm-4.6', name: 'GLM-4.6', region: 'cn', multiplier: 0,
166
- promo: { kind: 'night-later', nextHour: 22 },
167
- supportsImages: true, contextWindow: 200000, maxOutputTokens: 32000,
168
- nativeContextWindow: 200000, nativeMaxOutputTokens: 32000, nativeSupportsImages: true,
169
- nativeThinkingLevels: ['off', 'low', 'medium', 'high'], advanced: false,
170
- },
171
- {
172
- id: 'claude-opus-4.1', name: 'Claude Opus 4.1', region: 'global', multiplier: 3,
173
- supportsImages: true, contextWindow: 200000, maxOutputTokens: 32000,
174
- nativeContextWindow: 200000, nativeMaxOutputTokens: 32000, nativeSupportsImages: true,
175
- nativeThinkingLevels: ['off', 'low', 'medium', 'high'], advanced: false,
176
- },
177
- {
178
- id: 'gpt-5.1', name: 'GPT-5.1', alias: 'gpt', region: 'global', multiplier: 1,
179
- supportsImages: true, contextWindow: 400000, maxOutputTokens: 128000,
180
- nativeContextWindow: 400000, nativeMaxOutputTokens: 128000, nativeSupportsImages: true,
181
- nativeThinkingLevels: ['off', 'minimal', 'low', 'medium', 'high', 'xhigh'], advanced: false,
182
- },
183
- ],
184
-
185
- selectionByRegion: {
186
- cn: {
187
- enabledModelIds: ['claude-sonnet-4.5', 'gemini-2.5-pro', 'gpt-5.1-codex', 'kimi-k2-thinking', 'glm-4.6'],
188
- overrides: {
189
- 'claude-sonnet-4.5': {
190
- alias: 'sonnet', contextWindow: 312000, maxOutputTokens: 32000, supportsImages: true,
191
- thinkingLevels: ['off', 'medium', 'high'], defaultThinkingLevel: 'medium',
192
- },
193
- 'gemini-2.5-pro': { alias: 'gemini' },
194
- },
195
- },
196
- global: {
197
- enabledModelIds: ['gpt-5.1'],
198
- overrides: {},
199
- },
200
- },
201
-
202
- trae: {
203
- distribution: 'round-robin',
204
- preferred: { cn: '', ai: '' },
205
- accounts: [
206
- {
207
- label: 'Trae CN', region: 'cn', app: 'Trae CN', email: 'lin.cn@example.com',
208
- expiresAt: ms(3), expired: false,
209
- usage: {
210
- credits: {
211
- total: 1200, nearestExpiryMs: ms(2),
212
- packages: [
213
- { packageName: 'daily', remain: 200, size: 200, roundExpiresAtMs: ms(2) },
214
- { packageName: 'monthly', remain: 1000, size: 5000, monthly: true, refreshAtMs: ms(15) },
215
- ],
216
- },
217
- },
218
- checkin: { active: true, todayCheckedIn: false, deviceCheckedIn: false, amount: 100, unit: 'credits' },
219
- },
220
- {
221
- label: 'Trae International', region: 'ai', app: 'Trae', email: 'lin.intl@example.com',
222
- expiresAt: ms(8), expired: false,
223
- usage: { plan: { name: 'Pro', inTrial: false } },
224
- checkin: { active: false, todayCheckedIn: false, deviceCheckedIn: false },
225
- },
226
- ],
227
- gateways: [
228
- {
229
- region: 'cn', port: 42851, models: 2, portFallback: false, lastError: '',
230
- roster: [
231
- { id: 'deepseek-v3.2', name: 'DeepSeek V3.2', alias: '', nativeContextWindow: 128000,
232
- nativeMaxOutputTokens: 16000, nativeSupportsImages: false, nativeThinkingLevels: [], advanced: false },
233
- { id: 'doubao-seed-1.6', name: 'Doubao Seed 1.6', alias: 'doubao', nativeContextWindow: 256000,
234
- nativeMaxOutputTokens: 32000, nativeSupportsImages: true, nativeThinkingLevels: ['off', 'low', 'medium', 'high'], advanced: true },
235
- { id: 'kimi-k2-thinking', name: 'Kimi K2 Thinking', alias: '', nativeContextWindow: 256000,
236
- nativeMaxOutputTokens: 32000, nativeSupportsImages: false, nativeThinkingLevels: ['off', 'low', 'medium', 'high'], advanced: false },
237
- ],
238
- selection: { enabledModelIds: ['deepseek-v3.2', 'doubao-seed-1.6'], overrides: { 'doubao-seed-1.6': { alias: 'doubao' } } },
239
- },
240
- {
241
- region: 'ai', port: 42861, models: 1, portFallback: false, lastError: '',
242
- roster: [
243
- { id: 'gpt-5.1', name: 'GPT-5.1', alias: '', nativeContextWindow: 400000,
244
- nativeMaxOutputTokens: 128000, nativeSupportsImages: true, nativeThinkingLevels: [], advanced: false },
245
- ],
246
- selection: { enabledModelIds: ['gpt-5.1'], overrides: {} },
247
- },
248
- ],
249
- },
250
-
251
- qoder: {
252
- distribution: 'expiry',
253
- preferred: { cn: '', global: '' },
254
- accounts: [
255
- {
256
- label: 'Qoder CN', region: 'cn', expiresAt: ms(11), expired: false,
257
- usage: {
258
- credits: {
259
- total: 500,
260
- packages: [
261
- { nameKey: 'credits.planQuota', remain: 500, size: 500, neverExpires: true },
262
- { nameKey: 'credits.addOnQuota', remain: 0, size: 0, validDays: 30 },
263
- ],
264
- },
265
- },
266
- checkin: { active: true, todayCheckedIn: true, amount: 50, unit: 'credits' },
267
- },
268
- {
269
- label: 'Qoder Global', region: 'global', expiresAt: ms(5), expired: false,
270
- usage: { plan: { name: 'Trial', inTrial: true } },
271
- checkin: { active: true, todayCheckedIn: false, opensAtHour: 10, amount: 50, unit: 'credits' },
272
- },
273
- ],
274
- gateways: [
275
- {
276
- region: 'cn', port: 42871, models: 1, portFallback: false, lastError: '',
277
- roster: [
278
- { id: 'qwen3-coder-plus', name: 'Qwen3 Coder Plus', alias: '', nativeContextWindow: 128000,
279
- nativeMaxOutputTokens: 32000, nativeSupportsImages: false, nativeThinkingLevels: [], advanced: false },
280
- { id: 'claude-sonnet-4.5', name: 'Claude Sonnet 4.5', alias: '', nativeContextWindow: 200000,
281
- nativeMaxOutputTokens: 32000, nativeSupportsImages: true, nativeThinkingLevels: ['off', 'low', 'medium', 'high'], advanced: false },
282
- ],
283
- selection: { enabledModelIds: ['qwen3-coder-plus'], overrides: {} },
284
- },
285
- {
286
- region: 'global', port: 42881, models: 1, portFallback: false, lastError: '',
287
- roster: [
288
- { id: 'gpt-5.1', name: 'GPT-5.1', alias: '', nativeContextWindow: 400000,
289
- nativeMaxOutputTokens: 128000, nativeSupportsImages: true, nativeThinkingLevels: [], advanced: false },
290
- ],
291
- selection: { enabledModelIds: ['gpt-5.1'], overrides: {} },
292
- },
293
- ],
294
- },
295
- }
296
-
297
- // The check view fills only from `xd.doctor`, never from `xd.state`.
298
- const doctor = {
299
- ok: true,
300
- // Every id and detail below is the shape `doctor()` emits: the provider
301
- // label comes from PROVIDER_NAME_BY_REGION, and each count is the number
302
- // this file's own `models`/`selection` data would actually declare.
303
- findings: [
304
- { id: 'plugin-enabled', ok: true, detail: 'enabled' },
305
- { id: 'source-workbuddy', ok: true, detail: 'on' },
306
- { id: 'source-trae', ok: true, detail: 'on; 2 account(s) (cn x1, ai x1); serving cn: 2 model(s), ai: 1 model(s)' },
307
- { id: 'source-qoder', ok: true, detail: 'on; 2 account(s) (cn x1, global x1); serving cn: 1 model(s), global: 1 model(s)' },
308
- { id: 'accounts', ok: true, detail: '4 account(s): lin.cn@example.com, du.cn@example.com, wu.cn@example.com, lin.intl@example.com' },
309
- { id: 'rotation', ok: true, detail: '3 of 4 account(s) can serve a request now' },
310
- { id: 'host-registered-providers', ok: true, detail: 'the host is serving every provider this plugin declares' },
311
- { id: 'WTQ Pool (CN): endpoint', ok: true, detail: 'listening on http://127.0.0.1:42831/v1' },
312
- { id: 'WTQ Pool (CN): models', ok: true, detail: '6 model(s) in the catalog' },
313
- { id: 'WTQ Pool (CN): declared', ok: true, detail: 'registered with the running host' },
314
- { id: 'WTQ Pool (Global): endpoint', ok: true, detail: 'listening on http://127.0.0.1:42841/v1' },
315
- { id: 'WTQ Pool (Global): models', ok: true, detail: '2 model(s) in the catalog' },
316
- { id: 'WTQ Pool (Global): declared', ok: true, detail: 'registered with the running host' },
317
- { id: 'WTQ Pool (CN): published address', ok: true, detail: 'matches the endpoint this gateway bound (http://127.0.0.1:42831/v1)' },
318
- { id: 'WTQ Pool (Global): published address', ok: true, detail: 'matches the endpoint this gateway bound (http://127.0.0.1:42841/v1)' },
319
- { id: 'WTQ Pool Trae (CN): published address', ok: true, detail: 'matches the endpoint this gateway bound (http://127.0.0.1:42851/v1)' },
320
- { id: 'WTQ Pool Trae (Global): published address', ok: true, detail: 'matches the endpoint this gateway bound (http://127.0.0.1:42861/v1)' },
321
- { id: 'WTQ Pool Qoder (CN): published address', ok: true, detail: 'matches the endpoint this gateway bound (http://127.0.0.1:42871/v1)' },
322
- { id: 'WTQ Pool Qoder (Global): published address', ok: true, detail: 'matches the endpoint this gateway bound (http://127.0.0.1:42881/v1)' },
323
- { id: 'declaration-written', ok: true, detail: 'up to date' },
324
- ],
325
- }
326
-
327
- doctor.report = [
328
- 'WTQ Pool doctor',
329
- new Date().toISOString(),
330
- 'overall: PASS',
331
- '',
332
- ...doctor.findings.map((f) => (f.ok ? 'PASS ' : 'FAIL ') + f.id + ' - ' + f.detail),
333
- '',
334
- ].join('\n')
335
-
336
- const api = { state, doctor }
337
- if (typeof module !== 'undefined' && module.exports) module.exports = api
338
- if (typeof window !== 'undefined') window.__previewData = api
339
- })();
Binary file
Binary file
@@ -1,95 +0,0 @@
1
- <!doctype html>
2
- <html lang="zh-CN">
3
- <head>
4
- <meta charset="UTF-8" />
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
- <title>WTQ Pool panel preview</title>
7
- <!--
8
- dsh frames this panel inside the Plugins page and gives it no window chrome
9
- of its own, so panel.css already defaults --xd-titlebar to 0 and no override
10
- is needed here.
11
- -->
12
- </head>
13
- <body>
14
- <script src="./preview-data.js"></script>
15
- <script>
16
- /*
17
- * Stand-in for the host: the panel asks `window.pluginBridge` for everything
18
- * it shows, so answering those calls is the whole of a host.
19
- *
20
- * The panel is fetched from renderer/ below and mounted as-is, so this
21
- * preview never drifts from what ships.
22
- */
23
- (function () {
24
- "use strict";
25
- var data = window.__previewData;
26
- var appearance = { base: "light", locale: "zh-CN" };
27
- var handlers = {};
28
- function copy(value) { return JSON.parse(JSON.stringify(value)); }
29
-
30
- window.pluginBridge = {
31
- invoke: function (channel, payload) {
32
- if (channel === "xd.state") return Promise.resolve(copy(data.state));
33
- if (channel === "xd.doctor") return Promise.resolve(copy(data.doctor));
34
- if (channel === "app.getAppearance") {
35
- return Promise.resolve({ base: appearance.base, locale: appearance.locale });
36
- }
37
- if (channel === "xd.checkin") {
38
- return Promise.resolve({ state: copy(data.state), result: { credit: 20 } });
39
- }
40
- if (channel === "xd.checkinAll") {
41
- return Promise.resolve({ state: copy(data.state), claimed: 0 });
42
- }
43
- if (channel === "xd.copyReport") return Promise.resolve({ ok: true });
44
- if (channel === "shell.openExternal") {
45
- window.open(payload.url, "_blank", "noopener,noreferrer");
46
- return Promise.resolve();
47
- }
48
- // Every other channel is a write, and a write answers with the whole
49
- // status document - which is what the panel redraws from.
50
- return Promise.resolve(copy(data.state));
51
- },
52
- on: function (channel, handler) { handlers[channel] = handler; },
53
- };
54
-
55
- // Called by the hosting page's theme buttons.
56
- window.__previewSetAppearance = function (base) {
57
- if (base !== "light" && base !== "dark") return;
58
- appearance.base = base;
59
- if (handlers["appearance:changed"]) {
60
- handlers["appearance:changed"]({ base: base, locale: appearance.locale });
61
- }
62
- };
63
- })();
64
-
65
- (function () {
66
- "use strict";
67
- var css = document.createElement("link");
68
- css.rel = "stylesheet";
69
- css.href = "../renderer/panel.css";
70
- document.head.appendChild(css);
71
-
72
- fetch("../renderer/index.html").then(function (response) {
73
- return response.text();
74
- }).then(function (html) {
75
- var parsed = new DOMParser().parseFromString(html, "text/html");
76
- // The panel names its stylesheet and script as siblings. Both are
77
- // re-attached below with the paths this page needs, so the markup comes
78
- // straight from the real file.
79
- var owned = parsed.querySelectorAll("link[rel=stylesheet], script");
80
- for (var i = 0; i < owned.length; i += 1) owned[i].remove();
81
- document.body.innerHTML = parsed.body.innerHTML;
82
-
83
- var js = document.createElement("script");
84
- js.src = "../renderer/panel.js";
85
- document.body.appendChild(js);
86
- }).catch(function (error) {
87
- // Opening this file straight from disk is the likely cause, so the
88
- // message names the command that works.
89
- document.body.textContent = "panel failed to load (" + error + "). " +
90
- "Open the page through the local server instead: node docs/serve.cjs";
91
- });
92
- })();
93
- </script>
94
- </body>
95
- </html>
Binary file
package/docs/serve.cjs DELETED
@@ -1,74 +0,0 @@
1
- // Serve this repository over http://127.0.0.1 so the preview page opens in a
2
- // browser. The preview fetches the panel markup it mounts (browsers block that
3
- // under file://) and the panel's own CSP admits scripts only from its own
4
- // origin; an http origin satisfies both.
5
- //
6
- // node docs/serve.cjs # then open the printed URL
7
- // node docs/serve.cjs 8123 # pick the port
8
- //
9
- // Read-only, and bound to 127.0.0.1.
10
- const http = require('node:http')
11
- const fs = require('node:fs')
12
- const path = require('node:path')
13
-
14
- const ROOT = path.resolve(__dirname, '..')
15
- const PORT = Number(process.argv[2] || 8124)
16
-
17
- const TYPES = {
18
- '.html': 'text/html; charset=utf-8',
19
- '.js': 'text/javascript; charset=utf-8',
20
- '.mjs': 'text/javascript; charset=utf-8',
21
- '.css': 'text/css; charset=utf-8',
22
- '.json': 'application/json; charset=utf-8',
23
- '.png': 'image/png',
24
- '.svg': 'image/svg+xml',
25
- }
26
-
27
- http.createServer((req, res) => {
28
- // A request-target the parser cannot resolve also throws, and it does so
29
- // before the guards below run: same rule as the decode failures, answered
30
- // rather than allowed to kill the server.
31
- let url
32
- try {
33
- url = new URL(req.url, 'http://127.0.0.1')
34
- } catch {
35
- res.writeHead(400, { 'content-type': 'text/plain; charset=utf-8' }).end('bad request')
36
- return
37
- }
38
- // The preview page lives in docs/, so '/' redirects into it: the page's
39
- // relative links (./preview-panel.html, ../renderer/panel.js) resolve against
40
- // the URL, and a rewritten body under the wrong path would 404 every one.
41
- if (url.pathname === '/') {
42
- res.writeHead(302, { location: '/docs/index.html' }).end()
43
- return
44
- }
45
- // A malformed percent-escape (`/%zz`) makes decodeURIComponent throw, and a
46
- // decoded NUL makes fs.readFile throw; both would kill the server from inside
47
- // this callback, so they answer 400 instead. The path guard below still treats
48
- // the decoded value as untrusted.
49
- let rel
50
- try {
51
- rel = decodeURIComponent(url.pathname).replace(/^\/+/, '')
52
- } catch {
53
- res.writeHead(400, { 'content-type': 'text/plain; charset=utf-8' }).end('bad request: ' + url.pathname)
54
- return
55
- }
56
- if (rel.includes('\0')) {
57
- res.writeHead(400, { 'content-type': 'text/plain; charset=utf-8' }).end('bad request: ' + url.pathname)
58
- return
59
- }
60
- const file = path.resolve(ROOT, rel)
61
- if (file !== ROOT && !file.startsWith(ROOT + path.sep)) {
62
- res.writeHead(403).end('forbidden')
63
- return
64
- }
65
- fs.readFile(file, (err, body) => {
66
- if (err) {
67
- res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' }).end('not found: ' + rel)
68
- return
69
- }
70
- res.writeHead(200, { 'content-type': TYPES[path.extname(file)] || 'application/octet-stream' }).end(body)
71
- })
72
- }).listen(PORT, '127.0.0.1', function () {
73
- console.log('preview: http://127.0.0.1:' + this.address().port + '/ (Ctrl+C to stop)')
74
- })