dsh-workspace-scratch 0.0.0-stage → 0.2.1
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/README.md +220 -2
- package/client.js +382 -0
- package/cordis.patch.yml +6 -0
- package/icon.svg +12 -0
- package/index.js +638 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +61 -4
- package/plugin-index.json +64 -0
- package/settings.js +301 -0
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-workspace-scratch",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Keep a DeepSeek Harness workspace clean: every session gets a hidden .dsh directory for its scratch files and durable memory notes, plus scratch/memory tools, prompt guidance and a Settings page.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"icon": "./icon.svg",
|
|
7
|
+
"main": "./index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./index.js",
|
|
10
|
+
"./client": "./client.js",
|
|
11
|
+
"./locale/*.json": "./locale/*.json",
|
|
12
|
+
"./icon": "./icon.svg",
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"settings.js",
|
|
18
|
+
"client.js",
|
|
19
|
+
"cordis.patch.yml",
|
|
20
|
+
"locale/*.json",
|
|
21
|
+
"icon.svg",
|
|
22
|
+
"README.md",
|
|
23
|
+
"plugin-index.json"
|
|
24
|
+
],
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=20.0.0",
|
|
27
|
+
"dsh": ">=0.2.0-0 <1.0.0-0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "node --test \"test/**/*.test.mjs\"",
|
|
31
|
+
"build:index": "node scripts/build-index.mjs",
|
|
32
|
+
"prepublishOnly": "npm run build:index && npm test"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"dsh",
|
|
36
|
+
"deepseek-harness",
|
|
37
|
+
"cordis",
|
|
38
|
+
"workspace",
|
|
39
|
+
"scratch",
|
|
40
|
+
"memory"
|
|
41
|
+
],
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"dsh": {
|
|
44
|
+
"bundle": {
|
|
45
|
+
"patch": "./cordis.patch.yml"
|
|
46
|
+
},
|
|
47
|
+
"client": {
|
|
48
|
+
"platform": "web",
|
|
49
|
+
"immediately": true,
|
|
50
|
+
"inject": [
|
|
51
|
+
"@deepseek-ai/dsh-client-ui-settings-general"
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@deepseek-ai/dsh-plugin-market-index": "^0.2.1-alpha.4"
|
|
57
|
+
},
|
|
58
|
+
"author": "qhdhiman",
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"registry": "https://registry.npmjs.org/",
|
|
61
|
+
"access": "public"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"format": 1,
|
|
3
|
+
"title": {
|
|
4
|
+
"en": "QwenWork DSH plugins",
|
|
5
|
+
"zh": "QwenWork DSH 插件"
|
|
6
|
+
},
|
|
7
|
+
"description": {
|
|
8
|
+
"en": "Plugins published by QwenWork for DeepSeek Harness.",
|
|
9
|
+
"zh": "QwenWork 发布给 DeepSeek Harness 使用的插件。"
|
|
10
|
+
},
|
|
11
|
+
"metadata": {
|
|
12
|
+
"categories": {
|
|
13
|
+
"workspace": {
|
|
14
|
+
"title": {
|
|
15
|
+
"en": "Workspace",
|
|
16
|
+
"zh": "工作区"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"plugins": [
|
|
22
|
+
{
|
|
23
|
+
"name": "dsh-workspace-scratch",
|
|
24
|
+
"title": {
|
|
25
|
+
"en": "Workspace scratch and memory",
|
|
26
|
+
"zh": "工作区临时文件与记忆"
|
|
27
|
+
},
|
|
28
|
+
"description": {
|
|
29
|
+
"en": "Gives every session a hidden .dsh directory in the workspace root for scratch files and memory notes, with scratch and memory tools, so the workspace itself stays clean.",
|
|
30
|
+
"zh": "每次会话在工作区根目录建立隐藏的 .dsh 目录,把临时文件和记忆文件都收进去,并提供 scratch / memory 工具,让工作区保持干净。"
|
|
31
|
+
},
|
|
32
|
+
"icon": "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCIgd2lkdGg9IjI0IiBoZWlnaHQ9IjI0IiByb2xlPSJpbWciIGFyaWEtbGFiZWw9IldvcmsgYXJlYSBzY3JhdGNoIGFuZCBtZW1vcnkiPgogIDx0aXRsZT5Xb3JrIGFyZWEgc2NyYXRjaCBhbmQgbWVtb3J5PC90aXRsZT4KICA8IS0tIENvbG9yOiBEZWVwU2VlayBicmFuZCBibHVlICM0MTc2ZTYsIHRoZSB0b2tlbiBEU0ggdXNlcyBmb3IgYWxpYXMtbGluawogICAgICAgYW5kIHN0YXRlLWJ1c2luZXNzLXByaW1hcnkuIDQuMjM6MSBvbiB3aGl0ZSBhbmQgNC4zMToxIG9uIHRoZSBkYXJrCiAgICAgICBzdXJmYWNlLCBzbyBvbmUgbGl0ZXJhbCBjb2xvciByZWFkcyBvbiBib3RoIHRoZW1lcy4KICAgICAgIFNoYXBlOiBhIGNvbXBhY3QgZGlyZWN0b3J5IHRha2luZyB+ODAlIG9mIHRoZSBib3gsIHNvIHRoZSBpY29uIGNhcnJpZXMKICAgICAgIHRoZSBzYW1lIHZpc3VhbCB3ZWlnaHQgYXMgaXRzIG5laWdoYm91cnMuIC0tPgogIDxnIGZpbGw9Im5vbmUiIHN0cm9rZT0iIzQxNzZlNiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIj4KICAgIDxwYXRoIGQ9Ik0zLjQgNi40YTIuNCAyLjQgMCAwIDEgMi40LTIuNGgzLjlsMi40IDIuOGg2LjFhMi40IDIuNCAwIDAgMSAyLjQgMi40djguNGEyLjQgMi40IDAgMCAxLTIuNCAyLjRINS44YTIuNCAyLjQgMCAwIDEtMi40LTIuNHoiIHN0cm9rZS13aWR0aD0iMiIvPgogICAgPGNpcmNsZSBjeD0iMTIuNCIgY3k9IjEzLjYiIHI9IjIuNSIgZmlsbD0iIzQxNzZlNiIgc3Ryb2tlPSJub25lIi8+CiAgPC9nPgo8L3N2Zz4K",
|
|
33
|
+
"author": "qhdhiman",
|
|
34
|
+
"metadata": {
|
|
35
|
+
"category": "workspace",
|
|
36
|
+
"keywords": [
|
|
37
|
+
"workspace",
|
|
38
|
+
".dsh",
|
|
39
|
+
"scratch",
|
|
40
|
+
"temp files",
|
|
41
|
+
"memory",
|
|
42
|
+
"clean workspace",
|
|
43
|
+
"工作区",
|
|
44
|
+
"临时文件",
|
|
45
|
+
"记忆"
|
|
46
|
+
],
|
|
47
|
+
"labels": [],
|
|
48
|
+
"sortKeys": {}
|
|
49
|
+
},
|
|
50
|
+
"versions": [
|
|
51
|
+
{
|
|
52
|
+
"version": "0.2.1",
|
|
53
|
+
"publishTimestamp": 1795578491,
|
|
54
|
+
"source": {
|
|
55
|
+
"type": "npm"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"dsh": ">=0.2.0-0 <1.0.0-0"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
}
|
package/settings.js
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration layers and the settings page's HTTP API for
|
|
3
|
+
* dsh-workspace-scratch.
|
|
4
|
+
*
|
|
5
|
+
* Effective config is the shallow merge of three layers, later winning:
|
|
6
|
+
* 1. `DEFAULT_CONFIG` — hard-coded defaults.
|
|
7
|
+
* 2. the composition row's `config:` block (hand-edited, reviewable).
|
|
8
|
+
* 3. the settings file written by the GUI page (this module owns it).
|
|
9
|
+
*
|
|
10
|
+
* The settings file lives in the harness home, never in the workspace, and is
|
|
11
|
+
* written atomically so a crash cannot leave a half-written document.
|
|
12
|
+
*
|
|
13
|
+
* Imports nothing from `@deepseek-ai/*`: a `link:`-ed package resolves its
|
|
14
|
+
* imports from its own real directory, where DSH packages are not installed.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'
|
|
18
|
+
import { homedir } from 'node:os'
|
|
19
|
+
import { dirname, join } from 'node:path'
|
|
20
|
+
|
|
21
|
+
/** Defaults for every supported config field. */
|
|
22
|
+
export const DEFAULT_CONFIG = {
|
|
23
|
+
/** Workspace-relative name of the hidden directory this plugin owns. */
|
|
24
|
+
dirName: '.dsh',
|
|
25
|
+
/** Subdirectory holding per-session scratch files. */
|
|
26
|
+
tmpDirName: 'tmp',
|
|
27
|
+
/** Subdirectory holding durable memory notes. */
|
|
28
|
+
memoryDirName: 'memory',
|
|
29
|
+
/** Days a session scratch directory survives without being touched. */
|
|
30
|
+
retentionDays: 7,
|
|
31
|
+
/** Write `<dir>/.gitignore` so git never reports the directory's contents. */
|
|
32
|
+
writeGitIgnore: true,
|
|
33
|
+
/** Append `/<dir>/` to `.git/info/exclude` in git workspaces. */
|
|
34
|
+
gitExclude: true,
|
|
35
|
+
/** Register the system-prompt guidance section and runtime context. */
|
|
36
|
+
promptGuidance: true,
|
|
37
|
+
/** Register the `scratch` and `memory` tools. */
|
|
38
|
+
tools: true,
|
|
39
|
+
/** Register the `scratch` tool. */
|
|
40
|
+
scratchTool: true,
|
|
41
|
+
/** Register the `memory` tool. */
|
|
42
|
+
memoryTool: true,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Fields the settings page may change, in display order. */
|
|
46
|
+
export const EDITABLE_FIELDS = Object.keys(DEFAULT_CONFIG)
|
|
47
|
+
|
|
48
|
+
const SEGMENT = (value) => typeof value === 'string' && value.length > 0 && value !== '.' && value !== '..' && !/[\\/]/.test(value)
|
|
49
|
+
const FLAG = (value) => typeof value === 'boolean'
|
|
50
|
+
|
|
51
|
+
/** Per-field validators, mirroring the shape each default declares. */
|
|
52
|
+
const FIELDS = {
|
|
53
|
+
dirName: { check: SEGMENT, expectation: 'a single path segment' },
|
|
54
|
+
tmpDirName: { check: SEGMENT, expectation: 'a single path segment' },
|
|
55
|
+
memoryDirName: { check: SEGMENT, expectation: 'a single path segment' },
|
|
56
|
+
retentionDays: { check: (value) => Number.isInteger(value) && value >= 0 && value <= 3650, expectation: 'an integer between 0 and 3650' },
|
|
57
|
+
writeGitIgnore: { check: FLAG, expectation: 'a boolean' },
|
|
58
|
+
gitExclude: { check: FLAG, expectation: 'a boolean' },
|
|
59
|
+
promptGuidance: { check: FLAG, expectation: 'a boolean' },
|
|
60
|
+
tools: { check: FLAG, expectation: 'a boolean' },
|
|
61
|
+
scratchTool: { check: FLAG, expectation: 'a boolean' },
|
|
62
|
+
memoryTool: { check: FLAG, expectation: 'a boolean' },
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Keep only the keys a layer actually sets, each validated.
|
|
67
|
+
* @param raw - an untrusted layer: a row config or a settings file.
|
|
68
|
+
* @param label - layer name used in error messages.
|
|
69
|
+
* @returns the accepted, validated subset.
|
|
70
|
+
* @throws for a non-object layer or an unusable value.
|
|
71
|
+
*/
|
|
72
|
+
export function validateLayer(raw, label = 'config') {
|
|
73
|
+
if (raw === undefined || raw === null) return {}
|
|
74
|
+
if (typeof raw !== 'object' || Array.isArray(raw)) throw new Error(`workspace-scratch: ${label} must be an object`)
|
|
75
|
+
const accepted = {}
|
|
76
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
77
|
+
if (value === undefined) continue
|
|
78
|
+
const field = FIELDS[key]
|
|
79
|
+
if (field === undefined) throw new Error(`workspace-scratch: ${label}.${key} is not a supported field`)
|
|
80
|
+
if (!field.check(value)) {
|
|
81
|
+
throw new Error(`workspace-scratch: ${label}.${key} must be ${field.expectation}, received ${JSON.stringify(value)}`)
|
|
82
|
+
}
|
|
83
|
+
accepted[key] = value
|
|
84
|
+
}
|
|
85
|
+
return accepted
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The harness home: `$DSH_HOME`, else `~/.dsh`. */
|
|
89
|
+
export function dshHome() {
|
|
90
|
+
const configured = process.env.DSH_HOME
|
|
91
|
+
if (typeof configured === 'string' && configured.trim().length > 0) return configured.trim()
|
|
92
|
+
return join(homedir(), '.dsh')
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Absolute path of the settings file the GUI page writes. */
|
|
96
|
+
export function settingsFile() {
|
|
97
|
+
return join(dshHome(), 'workspace-scratch', 'settings.json')
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Read the stored settings layer.
|
|
102
|
+
* @param path - settings file path.
|
|
103
|
+
* @returns the accepted overrides; `{}` when the file is absent.
|
|
104
|
+
* @throws when the file exists but is not a JSON object of usable values.
|
|
105
|
+
*/
|
|
106
|
+
export async function readSettings(path = settingsFile()) {
|
|
107
|
+
let text
|
|
108
|
+
try {
|
|
109
|
+
text = await readFile(path, 'utf8')
|
|
110
|
+
} catch (error) {
|
|
111
|
+
if (error?.code === 'ENOENT') return {}
|
|
112
|
+
throw error
|
|
113
|
+
}
|
|
114
|
+
let parsed
|
|
115
|
+
try {
|
|
116
|
+
parsed = JSON.parse(text)
|
|
117
|
+
} catch {
|
|
118
|
+
throw new Error(`workspace-scratch: settings file ${JSON.stringify(path)} is not valid JSON; delete it to start over`)
|
|
119
|
+
}
|
|
120
|
+
return validateLayer(parsed, 'settings')
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Replace the stored settings layer atomically.
|
|
125
|
+
* @param values - the complete override set to persist.
|
|
126
|
+
* @param path - settings file path.
|
|
127
|
+
* @returns the persisted values.
|
|
128
|
+
*/
|
|
129
|
+
export async function writeSettings(values, path = settingsFile()) {
|
|
130
|
+
const validated = validateLayer(values, 'settings')
|
|
131
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o755 })
|
|
132
|
+
const staging = `${path}.${process.pid}.tmp`
|
|
133
|
+
await writeFile(staging, `${JSON.stringify(validated, null, 2)}\n`, { encoding: 'utf8', mode: 0o600 })
|
|
134
|
+
await rename(staging, path)
|
|
135
|
+
return validated
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Delete the settings file so every field falls back to the row config. */
|
|
139
|
+
export async function removeSettings(path = settingsFile()) {
|
|
140
|
+
await rm(path, { force: true })
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Compose one effective config object from the three layers.
|
|
145
|
+
* @param layers - row and stored layers, later winning over earlier.
|
|
146
|
+
* @returns a fresh plain object carrying every field.
|
|
147
|
+
*/
|
|
148
|
+
export function composeConfig(...layers) {
|
|
149
|
+
return Object.assign({}, DEFAULT_CONFIG, ...layers.map((layer) => validateLayer(layer)))
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Copy a new effective config onto the live object tools and prompts close
|
|
154
|
+
* over, so a settings change takes effect without re-registering anything.
|
|
155
|
+
* @param target - the live config object.
|
|
156
|
+
* @param next - the composed config to mirror.
|
|
157
|
+
*/
|
|
158
|
+
export function applyConfig(target, next) {
|
|
159
|
+
for (const key of Object.keys(target)) if (!Object.hasOwn(next, key)) delete target[key]
|
|
160
|
+
Object.assign(target, next)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Write one JSON response with no caching. */
|
|
164
|
+
function sendJson(res, status, body) {
|
|
165
|
+
res.statusCode = status
|
|
166
|
+
res.setHeader('Content-Type', 'application/json; charset=utf-8')
|
|
167
|
+
res.setHeader('Cache-Control', 'no-store')
|
|
168
|
+
res.end(JSON.stringify(body))
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Read a JSON request body, bounded so a malformed client cannot exhaust memory. */
|
|
172
|
+
async function readJsonBody(req, limit = 64 * 1024) {
|
|
173
|
+
const chunks = []
|
|
174
|
+
let size = 0
|
|
175
|
+
for await (const chunk of req) {
|
|
176
|
+
size += chunk.length
|
|
177
|
+
if (size > limit) throw new Error(`workspace-scratch: request body exceeds ${limit} bytes`)
|
|
178
|
+
chunks.push(chunk)
|
|
179
|
+
}
|
|
180
|
+
if (size === 0) return {}
|
|
181
|
+
const text = Buffer.concat(chunks).toString('utf8')
|
|
182
|
+
const parsed = JSON.parse(text)
|
|
183
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) throw new Error('workspace-scratch: request body must be a JSON object')
|
|
184
|
+
return parsed
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Build the settings page's routes.
|
|
189
|
+
* @param options - live accessors owned by the plugin.
|
|
190
|
+
* @returns exact-path routes ready for `webServer.register`.
|
|
191
|
+
*/
|
|
192
|
+
export function createApiRoutes(options) {
|
|
193
|
+
/**
|
|
194
|
+
* Read the stored layer without letting a damaged file break the page: a
|
|
195
|
+
* corrupt document must stay visible and repairable from the settings UI
|
|
196
|
+
* (saving or resetting rewrites it), not answer 500.
|
|
197
|
+
* @returns the accepted overrides and any problem worth showing.
|
|
198
|
+
*/
|
|
199
|
+
const safeStored = async () => {
|
|
200
|
+
try {
|
|
201
|
+
return { values: await options.readStored(), problem: undefined }
|
|
202
|
+
} catch (error) {
|
|
203
|
+
return { values: {}, problem: error instanceof Error ? error.message : String(error) }
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const state = async () => {
|
|
208
|
+
const { values: stored, problem } = await safeStored()
|
|
209
|
+
const warnings = [...options.warnings()]
|
|
210
|
+
if (problem !== undefined && !warnings.includes(problem)) warnings.push(problem)
|
|
211
|
+
return {
|
|
212
|
+
ok: true,
|
|
213
|
+
effective: { ...options.effective() },
|
|
214
|
+
defaults: { ...DEFAULT_CONFIG },
|
|
215
|
+
row: { ...options.rowLayer() },
|
|
216
|
+
stored: { ...stored },
|
|
217
|
+
fields: EDITABLE_FIELDS,
|
|
218
|
+
settingsFile: options.settingsFile(),
|
|
219
|
+
warnings,
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const update = async (req, res) => {
|
|
224
|
+
try {
|
|
225
|
+
const patch = await readJsonBody(req)
|
|
226
|
+
const { values: stored } = await safeStored()
|
|
227
|
+
const nextStored = validateLayer({ ...stored, ...patch }, 'settings')
|
|
228
|
+
await options.writeStored(nextStored)
|
|
229
|
+
await options.reload()
|
|
230
|
+
sendJson(res, 200, await state())
|
|
231
|
+
} catch (error) {
|
|
232
|
+
sendJson(res, 400, { ok: false, message: error instanceof Error ? error.message : String(error) })
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const reset = async (req, res) => {
|
|
237
|
+
try {
|
|
238
|
+
const body = await readJsonBody(req)
|
|
239
|
+
const keys = Array.isArray(body.keys) ? body.keys : undefined
|
|
240
|
+
if (keys === undefined) {
|
|
241
|
+
await options.clearStored()
|
|
242
|
+
} else {
|
|
243
|
+
const { values: stored } = await safeStored()
|
|
244
|
+
const nextStored = { ...stored }
|
|
245
|
+
for (const key of keys) {
|
|
246
|
+
if (typeof key !== 'string') throw new Error('workspace-scratch: keys must be field names')
|
|
247
|
+
delete nextStored[key]
|
|
248
|
+
}
|
|
249
|
+
await options.writeStored(nextStored)
|
|
250
|
+
}
|
|
251
|
+
await options.reload()
|
|
252
|
+
sendJson(res, 200, await state())
|
|
253
|
+
} catch (error) {
|
|
254
|
+
sendJson(res, 400, { ok: false, message: error instanceof Error ? error.message : String(error) })
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Answer GET as state and POST as a write; other methods get 405.
|
|
260
|
+
*
|
|
261
|
+
* Each request first passes the GUI's own admission gate. Routes registered
|
|
262
|
+
* straight on the webserver are otherwise reachable without the browser
|
|
263
|
+
* session the official `/api/*` routes require, which would let any local
|
|
264
|
+
* process read and rewrite these settings.
|
|
265
|
+
*/
|
|
266
|
+
const route = (path, onGet, onPost) => ({
|
|
267
|
+
kind: 'exact',
|
|
268
|
+
path,
|
|
269
|
+
handler: async (req, res) => {
|
|
270
|
+
const rejection = options.rejectRequest?.(req)
|
|
271
|
+
if (rejection !== undefined) {
|
|
272
|
+
res.statusCode = rejection
|
|
273
|
+
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
|
|
274
|
+
res.setHeader('Cache-Control', 'no-store')
|
|
275
|
+
res.end(rejection === 401 ? 'dsh web authentication required; reopen the URL printed by dsh web.\n' : 'forbidden\n')
|
|
276
|
+
return
|
|
277
|
+
}
|
|
278
|
+
const method = typeof req?.method === 'string' ? req.method.toUpperCase() : 'GET'
|
|
279
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
280
|
+
try {
|
|
281
|
+
sendJson(res, 200, await onGet())
|
|
282
|
+
} catch (error) {
|
|
283
|
+
sendJson(res, 500, { ok: false, message: error instanceof Error ? error.message : String(error) })
|
|
284
|
+
}
|
|
285
|
+
return
|
|
286
|
+
}
|
|
287
|
+
if (method === 'POST') {
|
|
288
|
+
await onPost(req, res)
|
|
289
|
+
return
|
|
290
|
+
}
|
|
291
|
+
res.setHeader('Allow', 'GET, HEAD, POST')
|
|
292
|
+
sendJson(res, 405, { ok: false, message: `workspace-scratch: ${method} is not allowed here` })
|
|
293
|
+
},
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
return [
|
|
297
|
+
route('/workspace-scratch/api/state', state, update),
|
|
298
|
+
route('/workspace-scratch/api/config', state, update),
|
|
299
|
+
route('/workspace-scratch/api/reset', state, reset),
|
|
300
|
+
]
|
|
301
|
+
}
|