dsh-sidebar-entry-manager 0.0.0-stage → 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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +66 -2
- package/README.zh.md +68 -0
- package/cordis.patch.yml +4 -0
- package/lib/client.js +1114 -0
- package/lib/index.js +288 -0
- package/package.json +60 -4
package/lib/index.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-sidebar-entry-manager — host half.
|
|
3
|
+
*
|
|
4
|
+
* Owns exactly one thing: the persisted list of sidebar entry ids the user has
|
|
5
|
+
* hidden, exposed to the browser half over a fenced, loopback-only route pair.
|
|
6
|
+
*
|
|
7
|
+
* Why a host route instead of `localStorage`: the DSH web GUI is served on a
|
|
8
|
+
* port that changes between launches, so an origin-scoped store would forget
|
|
9
|
+
* the choice on every restart. The file below survives restarts and is shared
|
|
10
|
+
* by every browser pointed at this Host.
|
|
11
|
+
*
|
|
12
|
+
* The file is plain JSON so it can be inspected, backed up or repaired by hand:
|
|
13
|
+
* ~/.dsh/sidebar-entry-manager.json (override: DSH_SIDEBAR_ENTRIES_FILE)
|
|
14
|
+
*
|
|
15
|
+
* Nothing here renders. All visibility work happens in `lib/client.js`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
|
|
19
|
+
import { homedir } from 'node:os'
|
|
20
|
+
import { dirname, join } from 'node:path'
|
|
21
|
+
|
|
22
|
+
/** Route family served to the browser half (absolute; the client uses document-relative URLs). */
|
|
23
|
+
export const ROUTES = {
|
|
24
|
+
state: '/api/dsh-sidebar-entry-manager/state',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Loader slug reported to the Host. */
|
|
28
|
+
export const name = 'sidebar-entry-manager'
|
|
29
|
+
|
|
30
|
+
/** Largest accepted request body (the payload is a short id list). */
|
|
31
|
+
const MAX_BODY_BYTES = 64 * 1024
|
|
32
|
+
|
|
33
|
+
/** Longest accepted entry id. */
|
|
34
|
+
const MAX_ID_LENGTH = 120
|
|
35
|
+
|
|
36
|
+
/** Most ids one payload may carry. */
|
|
37
|
+
const MAX_IDS = 200
|
|
38
|
+
|
|
39
|
+
/** Services this plugin needs from the Host. */
|
|
40
|
+
export const inject = ['webServer']
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Absolute path of the state file.
|
|
44
|
+
* @returns the resolved file path.
|
|
45
|
+
*/
|
|
46
|
+
export function stateFile() {
|
|
47
|
+
const override = process.env.DSH_SIDEBAR_ENTRIES_FILE
|
|
48
|
+
if (typeof override === 'string' && override.trim() !== '') return override.trim()
|
|
49
|
+
return join(homedir(), '.dsh', 'sidebar-entry-manager.json')
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Keep only well-formed ids, de-duplicated and in first-seen order.
|
|
54
|
+
* @param value - candidate list from disk or the wire.
|
|
55
|
+
* @returns the sanitized id list (never throws).
|
|
56
|
+
*/
|
|
57
|
+
export function sanitizeIds(value) {
|
|
58
|
+
if (!Array.isArray(value)) return []
|
|
59
|
+
const seen = new Set()
|
|
60
|
+
const out = []
|
|
61
|
+
for (const raw of value) {
|
|
62
|
+
if (typeof raw !== 'string') continue
|
|
63
|
+
const id = raw.trim()
|
|
64
|
+
if (id === '' || id.length > MAX_ID_LENGTH) continue
|
|
65
|
+
if (seen.has(id)) continue
|
|
66
|
+
seen.add(id)
|
|
67
|
+
out.push(id)
|
|
68
|
+
if (out.length >= MAX_IDS) break
|
|
69
|
+
}
|
|
70
|
+
return out
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Read the stored state. A missing or malformed file reads as "nothing hidden,
|
|
75
|
+
* auto-update off", so a corrupted state can never lock the user out of the
|
|
76
|
+
* sidebar (or silently start installing updates).
|
|
77
|
+
* @returns {{hidden: string[], autoUpdate: boolean}} the stored state.
|
|
78
|
+
*/
|
|
79
|
+
export async function readState() {
|
|
80
|
+
try {
|
|
81
|
+
const raw = await readFile(stateFile(), 'utf8')
|
|
82
|
+
const parsed = JSON.parse(raw)
|
|
83
|
+
return {
|
|
84
|
+
hidden: sanitizeIds(parsed?.hidden),
|
|
85
|
+
autoUpdate: parsed?.autoUpdate === true,
|
|
86
|
+
}
|
|
87
|
+
} catch {
|
|
88
|
+
return { hidden: [], autoUpdate: false }
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Merge a patch into the stored state and persist it atomically (temp sibling +
|
|
94
|
+
* rename), so a crash mid write never leaves a half-written state file behind.
|
|
95
|
+
* Omitted fields keep their stored value.
|
|
96
|
+
* @param {{hidden?: string[], autoUpdate?: boolean}} patch - fields to replace.
|
|
97
|
+
* @returns {{hidden: string[], autoUpdate: boolean}} the state that was written.
|
|
98
|
+
*/
|
|
99
|
+
export async function writeState(patch) {
|
|
100
|
+
const current = await readState()
|
|
101
|
+
const next = {
|
|
102
|
+
hidden: patch?.hidden !== undefined ? sanitizeIds(patch.hidden) : current.hidden,
|
|
103
|
+
autoUpdate: patch?.autoUpdate !== undefined ? patch.autoUpdate === true : current.autoUpdate,
|
|
104
|
+
}
|
|
105
|
+
const file = stateFile()
|
|
106
|
+
await mkdir(dirname(file), { recursive: true })
|
|
107
|
+
const temp = `${file}.tmp-${process.pid}-${Date.now()}`
|
|
108
|
+
await writeFile(temp, `${JSON.stringify({ version: 2, ...next }, null, 2)}\n`, 'utf8')
|
|
109
|
+
await rename(temp, file)
|
|
110
|
+
return next
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Whether this request must be refused.
|
|
115
|
+
*
|
|
116
|
+
* Prefers the Host's own admission service (`connection.requestRejection`,
|
|
117
|
+
* DSH 0.2.0+), which already encodes the deployment's LAN / pairing policy.
|
|
118
|
+
* Deployments without it fall back to a strict loopback fence: loopback socket,
|
|
119
|
+
* loopback Host header, no forwarding headers, and no cross-site browser
|
|
120
|
+
* marker.
|
|
121
|
+
*
|
|
122
|
+
* @param ctx - Host plugin context.
|
|
123
|
+
* @param req - incoming HTTP request.
|
|
124
|
+
* @returns 401/403 when refused, otherwise undefined.
|
|
125
|
+
*/
|
|
126
|
+
export function rejectionOf(ctx, req) {
|
|
127
|
+
try {
|
|
128
|
+
const connection = ctx.get?.('connection', false)
|
|
129
|
+
if (connection !== undefined && connection !== null && typeof connection.requestRejection === 'function') {
|
|
130
|
+
const verdict = connection.requestRejection(req)
|
|
131
|
+
if (verdict === 401 || verdict === 403) return verdict
|
|
132
|
+
return undefined
|
|
133
|
+
}
|
|
134
|
+
} catch {
|
|
135
|
+
// Fall through to the loopback fence; admission must never throw.
|
|
136
|
+
}
|
|
137
|
+
return isLoopbackRequest(req) ? undefined : 403
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Whether a socket address is in the IPv4 127/8 or ::1 range. */
|
|
141
|
+
function isLoopbackAddress(address) {
|
|
142
|
+
if (typeof address !== 'string' || address === '') return false
|
|
143
|
+
const normalized = address.toLowerCase()
|
|
144
|
+
if (normalized === '::1') return true
|
|
145
|
+
if (normalized.startsWith('::ffff:')) return isLoopbackV4(normalized.slice(7))
|
|
146
|
+
return isLoopbackV4(normalized)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Whether a dotted IPv4 name is 127/8. */
|
|
150
|
+
function isLoopbackV4(value) {
|
|
151
|
+
const parts = value.split('.')
|
|
152
|
+
if (parts.length !== 4 || parts[0] !== '127') return false
|
|
153
|
+
return parts.every((part) => /^\d{1,3}$/.test(part) && Number(part) <= 255)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Whether a Host header names a loopback authority. */
|
|
157
|
+
function isLoopbackHostname(hostname) {
|
|
158
|
+
if (hostname === 'localhost' || hostname === '[::1]') return true
|
|
159
|
+
return isLoopbackV4(hostname)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Strict loopback fence used when the Host exposes no admission service. */
|
|
163
|
+
function isLoopbackRequest(request) {
|
|
164
|
+
const socket = request?.socket
|
|
165
|
+
if (!socket || !isLoopbackAddress(socket.remoteAddress)) return false
|
|
166
|
+
const headers = request.headers ?? {}
|
|
167
|
+
if (headers['cf-connecting-ip'] !== undefined || headers['x-forwarded-for'] !== undefined) return false
|
|
168
|
+
if (headers['sec-fetch-site'] === 'cross-site') return false
|
|
169
|
+
const host = headers.host
|
|
170
|
+
if (typeof host !== 'string') return false
|
|
171
|
+
let hostUrl
|
|
172
|
+
try {
|
|
173
|
+
hostUrl = new URL(`http://${host}`)
|
|
174
|
+
} catch {
|
|
175
|
+
return false
|
|
176
|
+
}
|
|
177
|
+
if (!isLoopbackHostname(hostUrl.hostname)) return false
|
|
178
|
+
const origin = headers.origin
|
|
179
|
+
if (origin === undefined) return true
|
|
180
|
+
try {
|
|
181
|
+
return new URL(origin).host === hostUrl.host
|
|
182
|
+
} catch {
|
|
183
|
+
return false
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Write a JSON response. */
|
|
188
|
+
function writeJson(res, status, payload) {
|
|
189
|
+
const body = JSON.stringify(payload)
|
|
190
|
+
res.statusCode = status
|
|
191
|
+
res.setHeader?.('content-type', 'application/json; charset=utf-8')
|
|
192
|
+
res.setHeader?.('cache-control', 'no-store')
|
|
193
|
+
res.setHeader?.('content-length', Buffer.byteLength(body))
|
|
194
|
+
res.end(body)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Read a JSON body with a hard size cap.
|
|
199
|
+
* @param req - incoming request.
|
|
200
|
+
* @returns the parsed value, or undefined when the body is empty.
|
|
201
|
+
*/
|
|
202
|
+
async function readJsonBody(req) {
|
|
203
|
+
const chunks = []
|
|
204
|
+
let size = 0
|
|
205
|
+
for await (const chunk of req) {
|
|
206
|
+
size += chunk.length
|
|
207
|
+
if (size > MAX_BODY_BYTES) {
|
|
208
|
+
const error = new Error('request body too large')
|
|
209
|
+
error.status = 413
|
|
210
|
+
throw error
|
|
211
|
+
}
|
|
212
|
+
chunks.push(chunk)
|
|
213
|
+
}
|
|
214
|
+
if (chunks.length === 0) return undefined
|
|
215
|
+
const text = Buffer.concat(chunks).toString('utf8')
|
|
216
|
+
if (text.trim() === '') return undefined
|
|
217
|
+
try {
|
|
218
|
+
return JSON.parse(text)
|
|
219
|
+
} catch {
|
|
220
|
+
const error = new Error('request body is not valid JSON')
|
|
221
|
+
error.status = 400
|
|
222
|
+
throw error
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Handle one request to the state route: GET reads, POST replaces.
|
|
228
|
+
* @param ctx - Host plugin context (unused beyond symmetry).
|
|
229
|
+
* @param req - incoming request.
|
|
230
|
+
* @param res - response sink.
|
|
231
|
+
*/
|
|
232
|
+
export async function handleState(ctx, req, res) {
|
|
233
|
+
const rejection = rejectionOf(ctx, req)
|
|
234
|
+
if (rejection !== undefined) {
|
|
235
|
+
writeJson(res, rejection, { ok: false, error: rejection === 401 ? 'unauthorized' : 'forbidden' })
|
|
236
|
+
return
|
|
237
|
+
}
|
|
238
|
+
if (req.method === 'GET') {
|
|
239
|
+
const state = await readState()
|
|
240
|
+
writeJson(res, 200, { ok: true, hidden: state.hidden, autoUpdate: state.autoUpdate })
|
|
241
|
+
return
|
|
242
|
+
}
|
|
243
|
+
if (req.method !== 'POST') {
|
|
244
|
+
res.setHeader?.('allow', 'GET, POST')
|
|
245
|
+
writeJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
246
|
+
return
|
|
247
|
+
}
|
|
248
|
+
try {
|
|
249
|
+
const payload = await readJsonBody(req)
|
|
250
|
+
const hasHidden = payload !== undefined && payload !== null && Object.prototype.hasOwnProperty.call(payload, 'hidden')
|
|
251
|
+
const hasAutoUpdate = payload !== undefined && payload !== null && Object.prototype.hasOwnProperty.call(payload, 'autoUpdate')
|
|
252
|
+
if (!hasHidden && !hasAutoUpdate) {
|
|
253
|
+
writeJson(res, 400, { ok: false, error: 'body must carry "hidden" and/or "autoUpdate"' })
|
|
254
|
+
return
|
|
255
|
+
}
|
|
256
|
+
if (hasHidden && !Array.isArray(payload.hidden)) {
|
|
257
|
+
writeJson(res, 400, { ok: false, error: '"hidden" must be a string array' })
|
|
258
|
+
return
|
|
259
|
+
}
|
|
260
|
+
if (hasAutoUpdate && typeof payload.autoUpdate !== 'boolean') {
|
|
261
|
+
writeJson(res, 400, { ok: false, error: '"autoUpdate" must be a boolean' })
|
|
262
|
+
return
|
|
263
|
+
}
|
|
264
|
+
const state = await writeState({
|
|
265
|
+
hidden: hasHidden ? payload.hidden : undefined,
|
|
266
|
+
autoUpdate: hasAutoUpdate ? payload.autoUpdate : undefined,
|
|
267
|
+
})
|
|
268
|
+
writeJson(res, 200, { ok: true, hidden: state.hidden, autoUpdate: state.autoUpdate })
|
|
269
|
+
} catch (error) {
|
|
270
|
+
writeJson(res, error?.status ?? 500, { ok: false, error: error instanceof Error ? error.message : String(error) })
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Register the state route.
|
|
276
|
+
* @param ctx - Host plugin context.
|
|
277
|
+
*/
|
|
278
|
+
export function apply(ctx) {
|
|
279
|
+
ctx.effect(
|
|
280
|
+
() =>
|
|
281
|
+
ctx.webServer.register({
|
|
282
|
+
kind: 'exact',
|
|
283
|
+
path: ROUTES.state,
|
|
284
|
+
handler: (req, res) => handleState(ctx, req, res),
|
|
285
|
+
}),
|
|
286
|
+
'dsh-sidebar-entry-manager: state route',
|
|
287
|
+
)
|
|
288
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-sidebar-entry-manager",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Manage which plugin entries appear in the DeepSeek Harness left sidebar: hide or show any sidebar panel row without disabling the plugin.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/index.js",
|
|
9
|
+
"./client": "./lib/client.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"dsh": {
|
|
13
|
+
"engines": {
|
|
14
|
+
"dsh": ">=0.2.1-rc.2"
|
|
15
|
+
},
|
|
16
|
+
"bundle": {
|
|
17
|
+
"patch": "./cordis.patch.yml"
|
|
18
|
+
},
|
|
19
|
+
"client": {
|
|
20
|
+
"platform": "web",
|
|
21
|
+
"inject": []
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"lib",
|
|
26
|
+
"cordis.patch.yml",
|
|
27
|
+
"README.md",
|
|
28
|
+
"README.zh.md",
|
|
29
|
+
"CHANGELOG.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"keywords": [
|
|
33
|
+
"dsh-plugin",
|
|
34
|
+
"deepseek-harness",
|
|
35
|
+
"dsh",
|
|
36
|
+
"deepseek",
|
|
37
|
+
"sidebar",
|
|
38
|
+
"sidebar-entry-manager",
|
|
39
|
+
"ui"
|
|
40
|
+
],
|
|
41
|
+
"author": {
|
|
42
|
+
"name": "weiaisuyan",
|
|
43
|
+
"url": "https://github.com/weiaisuyan"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/weiaisuyan/dsh-sidebar-entry-manager.git"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://github.com/weiaisuyan/dsh-sidebar-entry-manager#readme",
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/weiaisuyan/dsh-sidebar-entry-manager/issues"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "node scripts/build.mjs",
|
|
55
|
+
"test": "node --test",
|
|
56
|
+
"check": "node --check src/host.js && node --check src/client.js && node --check scripts/build.mjs"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=22.19.0"
|
|
60
|
+
},
|
|
61
|
+
"license": "MIT"
|
|
62
|
+
}
|