dsh-session-guard 4.2.4 → 4.2.6
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.ja.md +41 -41
- package/CHANGELOG.ko.md +41 -41
- package/CHANGELOG.md +40 -40
- package/README.de.md +1 -1
- package/README.en.md +7 -7
- package/README.es.md +1 -1
- package/README.fr.md +1 -1
- package/README.it.md +1 -1
- package/README.md +8 -8
- package/README.ru.md +1 -1
- package/dsh.plugin.json +16 -16
- package/lib/client.js +13 -4
- package/lib/client.js.map +1 -1
- package/package.json +109 -109
- package/src/client/family-section.tsx +20 -7
- package/src/client/index.ts +4 -0
- package/src/client/settings-bridge.ts +202 -202
- package/src/client/settings-card.tsx +397 -397
- package/src/settings-bridge.js +141 -141
package/src/settings-bridge.js
CHANGED
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-session-guard — settings webServer 桥(M4/T20-b 泛化)。
|
|
3
|
-
*
|
|
4
|
-
* 设置卡双轨数据源的桥轨:≤0.1.5(settingsScope fiber 永不解 / 0.1.0-0.1.1 无
|
|
5
|
-
* client 服务)时,client BridgeDocHandle 经 describe/mutate 两端点读写宿主
|
|
6
|
-
* settings 服务。桥只是 HTTP 皮:读 = svc.describe 取自家 ns 描述符;写 =
|
|
7
|
-
* svc.mutate 转 ops 落宿主服务(不落第二份状态);mutate 成功即 describe 回填,
|
|
8
|
-
* 带 expectedRevision 乐观并发。守卫:POST-only + redactSecrets + 256KB。
|
|
9
|
-
* 语义与 perm-gate src/bridge.ts(5260fe6 泛化)逐字对齐,本仓为 JS 形态。
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/** 一个 settings ns 的桥路由对——client 侧(src/client/settings-bridge.ts)必须同式派生(测试钉死一致)。 */
|
|
13
|
-
function settingsBridgeRoutesFor(ns) {
|
|
14
|
-
return { describe: `/api/${ns}/settings/describe`, mutate: `/api/${ns}/settings/mutate` }
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function readJsonBody(req, limit = 256 * 1024) {
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
const chunks = []
|
|
20
|
-
let size = 0
|
|
21
|
-
req.on('data', (chunk) => {
|
|
22
|
-
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)
|
|
23
|
-
size += buf.length
|
|
24
|
-
if (size > limit) {
|
|
25
|
-
reject(new Error('bridge body too large'))
|
|
26
|
-
if (typeof req.resume === 'function') req.resume()
|
|
27
|
-
return
|
|
28
|
-
}
|
|
29
|
-
chunks.push(buf)
|
|
30
|
-
})
|
|
31
|
-
req.on('end', () => {
|
|
32
|
-
const raw = Buffer.concat(chunks).toString('utf8')
|
|
33
|
-
if (raw === '') {
|
|
34
|
-
resolve({})
|
|
35
|
-
return
|
|
36
|
-
}
|
|
37
|
-
try {
|
|
38
|
-
resolve(JSON.parse(raw))
|
|
39
|
-
} catch (e) {
|
|
40
|
-
reject(e instanceof Error ? e : new Error(String(e)))
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
req.on('error', reject)
|
|
44
|
-
})
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function sendJson(res, code, body) {
|
|
48
|
-
res.writeHead(code, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-cache' })
|
|
49
|
-
res.end(JSON.stringify(body))
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function isConflict(e) {
|
|
53
|
-
return e instanceof Error && /conflict|revision/i.test(e.message)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function describeNs(svc, ns) {
|
|
57
|
-
return svc.describe({ redactSecrets: true }).find((d) => d.ns === ns)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 注册 describe/mutate 两端点(kind:'exact')。
|
|
62
|
-
* @param {unknown} server 宿主 webServer 服务(须有 register(kind,path,handler))
|
|
63
|
-
* @param {() => unknown} getSettingsSvc 惰性取宿主 settings 服务(cordis ctx.get 永不 throw,可安全常取)
|
|
64
|
-
* @param {string} ns 自家 settings 命名空间
|
|
65
|
-
* @returns {Array<() => void>} 注销函数数组
|
|
66
|
-
*/
|
|
67
|
-
export function registerSettingsBridgeRoutes(server, getSettingsSvc, ns) {
|
|
68
|
-
if (typeof server !== 'object' || server === null) return []
|
|
69
|
-
const ws = typeof server.register === 'function' ? server : undefined
|
|
70
|
-
if (ws === undefined) return []
|
|
71
|
-
const offs = []
|
|
72
|
-
const routes = settingsBridgeRoutesFor(ns)
|
|
73
|
-
|
|
74
|
-
offs.push(ws.register({
|
|
75
|
-
kind: 'exact',
|
|
76
|
-
path: routes.describe,
|
|
77
|
-
handler: (req, res) => {
|
|
78
|
-
if (req.method !== 'POST') {
|
|
79
|
-
sendJson(res, 405, { ok: false, code: 'method-not-allowed' })
|
|
80
|
-
return
|
|
81
|
-
}
|
|
82
|
-
const svc = getSettingsSvc()
|
|
83
|
-
if (svc === undefined || svc === null || typeof svc.describe !== 'function') {
|
|
84
|
-
sendJson(res, 200, { ok: false, code: 'settings-unavailable', message: 'settings service not resolved yet' })
|
|
85
|
-
return
|
|
86
|
-
}
|
|
87
|
-
try {
|
|
88
|
-
const descriptor = describeNs(svc, ns)
|
|
89
|
-
if (descriptor === undefined) {
|
|
90
|
-
sendJson(res, 200, { ok: false, code: 'ns-not-registered', message: `settings namespace "${ns}" is not registered` })
|
|
91
|
-
return
|
|
92
|
-
}
|
|
93
|
-
sendJson(res, 200, { ok: true, value: { descriptor, writable: svc.writable !== false } })
|
|
94
|
-
} catch (e) {
|
|
95
|
-
sendJson(res, 200, { ok: false, code: 'internal', message: e instanceof Error ? e.message : String(e) })
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
|
-
}))
|
|
99
|
-
|
|
100
|
-
offs.push(ws.register({
|
|
101
|
-
kind: 'exact',
|
|
102
|
-
path: routes.mutate,
|
|
103
|
-
handler: (req, res) => {
|
|
104
|
-
if (req.method !== 'POST') {
|
|
105
|
-
sendJson(res, 405, { ok: false, code: 'method-not-allowed' })
|
|
106
|
-
return
|
|
107
|
-
}
|
|
108
|
-
const svc = getSettingsSvc()
|
|
109
|
-
if (svc === undefined || svc === null || typeof svc.mutate !== 'function') {
|
|
110
|
-
sendJson(res, 200, { ok: false, code: 'settings-unavailable', message: 'settings service mutate not available' })
|
|
111
|
-
return
|
|
112
|
-
}
|
|
113
|
-
readJsonBody(req).then((body) => {
|
|
114
|
-
const ops = body.ops
|
|
115
|
-
const valid = Array.isArray(ops) && ops.every((op) => op !== null && typeof op === 'object' && (op.op === 'set' || op.op === 'unset'))
|
|
116
|
-
if (!valid) {
|
|
117
|
-
sendJson(res, 200, { ok: false, code: 'settings-rejected', message: 'malformed bridge mutate ops' })
|
|
118
|
-
return
|
|
119
|
-
}
|
|
120
|
-
const expectedRevision = typeof body.expectedRevision === 'number' ? body.expectedRevision : undefined
|
|
121
|
-
svc.mutate(ns, ops, expectedRevision).then(() => {
|
|
122
|
-
// mutate 成功即 describe 回填(写路径归口宿主服务,无第二份状态)
|
|
123
|
-
const descriptor = describeNs(svc, ns)
|
|
124
|
-
if (descriptor === undefined) {
|
|
125
|
-
sendJson(res, 200, { ok: false, code: 'internal', message: `namespace "${ns}" disposed after mutate` })
|
|
126
|
-
return
|
|
127
|
-
}
|
|
128
|
-
sendJson(res, 200, { ok: true, value: { descriptor } })
|
|
129
|
-
}).catch((e) => {
|
|
130
|
-
sendJson(res, 200, isConflict(e)
|
|
131
|
-
? { ok: false, code: 'settings-conflict', message: e instanceof Error ? e.message : String(e) }
|
|
132
|
-
: { ok: false, code: 'internal', message: e instanceof Error ? e.message : String(e) })
|
|
133
|
-
})
|
|
134
|
-
}).catch((e) => {
|
|
135
|
-
sendJson(res, 200, { ok: false, code: 'bad-request', message: e instanceof Error ? e.message : String(e) })
|
|
136
|
-
})
|
|
137
|
-
},
|
|
138
|
-
}))
|
|
139
|
-
|
|
140
|
-
return offs
|
|
141
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* dsh-session-guard — settings webServer 桥(M4/T20-b 泛化)。
|
|
3
|
+
*
|
|
4
|
+
* 设置卡双轨数据源的桥轨:≤0.1.5(settingsScope fiber 永不解 / 0.1.0-0.1.1 无
|
|
5
|
+
* client 服务)时,client BridgeDocHandle 经 describe/mutate 两端点读写宿主
|
|
6
|
+
* settings 服务。桥只是 HTTP 皮:读 = svc.describe 取自家 ns 描述符;写 =
|
|
7
|
+
* svc.mutate 转 ops 落宿主服务(不落第二份状态);mutate 成功即 describe 回填,
|
|
8
|
+
* 带 expectedRevision 乐观并发。守卫:POST-only + redactSecrets + 256KB。
|
|
9
|
+
* 语义与 perm-gate src/bridge.ts(5260fe6 泛化)逐字对齐,本仓为 JS 形态。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** 一个 settings ns 的桥路由对——client 侧(src/client/settings-bridge.ts)必须同式派生(测试钉死一致)。 */
|
|
13
|
+
function settingsBridgeRoutesFor(ns) {
|
|
14
|
+
return { describe: `/api/${ns}/settings/describe`, mutate: `/api/${ns}/settings/mutate` }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function readJsonBody(req, limit = 256 * 1024) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const chunks = []
|
|
20
|
+
let size = 0
|
|
21
|
+
req.on('data', (chunk) => {
|
|
22
|
+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)
|
|
23
|
+
size += buf.length
|
|
24
|
+
if (size > limit) {
|
|
25
|
+
reject(new Error('bridge body too large'))
|
|
26
|
+
if (typeof req.resume === 'function') req.resume()
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
chunks.push(buf)
|
|
30
|
+
})
|
|
31
|
+
req.on('end', () => {
|
|
32
|
+
const raw = Buffer.concat(chunks).toString('utf8')
|
|
33
|
+
if (raw === '') {
|
|
34
|
+
resolve({})
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
resolve(JSON.parse(raw))
|
|
39
|
+
} catch (e) {
|
|
40
|
+
reject(e instanceof Error ? e : new Error(String(e)))
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
req.on('error', reject)
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function sendJson(res, code, body) {
|
|
48
|
+
res.writeHead(code, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-cache' })
|
|
49
|
+
res.end(JSON.stringify(body))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isConflict(e) {
|
|
53
|
+
return e instanceof Error && /conflict|revision/i.test(e.message)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function describeNs(svc, ns) {
|
|
57
|
+
return svc.describe({ redactSecrets: true }).find((d) => d.ns === ns)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 注册 describe/mutate 两端点(kind:'exact')。
|
|
62
|
+
* @param {unknown} server 宿主 webServer 服务(须有 register(kind,path,handler))
|
|
63
|
+
* @param {() => unknown} getSettingsSvc 惰性取宿主 settings 服务(cordis ctx.get 永不 throw,可安全常取)
|
|
64
|
+
* @param {string} ns 自家 settings 命名空间
|
|
65
|
+
* @returns {Array<() => void>} 注销函数数组
|
|
66
|
+
*/
|
|
67
|
+
export function registerSettingsBridgeRoutes(server, getSettingsSvc, ns) {
|
|
68
|
+
if (typeof server !== 'object' || server === null) return []
|
|
69
|
+
const ws = typeof server.register === 'function' ? server : undefined
|
|
70
|
+
if (ws === undefined) return []
|
|
71
|
+
const offs = []
|
|
72
|
+
const routes = settingsBridgeRoutesFor(ns)
|
|
73
|
+
|
|
74
|
+
offs.push(ws.register({
|
|
75
|
+
kind: 'exact',
|
|
76
|
+
path: routes.describe,
|
|
77
|
+
handler: (req, res) => {
|
|
78
|
+
if (req.method !== 'POST') {
|
|
79
|
+
sendJson(res, 405, { ok: false, code: 'method-not-allowed' })
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
const svc = getSettingsSvc()
|
|
83
|
+
if (svc === undefined || svc === null || typeof svc.describe !== 'function') {
|
|
84
|
+
sendJson(res, 200, { ok: false, code: 'settings-unavailable', message: 'settings service not resolved yet' })
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
const descriptor = describeNs(svc, ns)
|
|
89
|
+
if (descriptor === undefined) {
|
|
90
|
+
sendJson(res, 200, { ok: false, code: 'ns-not-registered', message: `settings namespace "${ns}" is not registered` })
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
sendJson(res, 200, { ok: true, value: { descriptor, writable: svc.writable !== false } })
|
|
94
|
+
} catch (e) {
|
|
95
|
+
sendJson(res, 200, { ok: false, code: 'internal', message: e instanceof Error ? e.message : String(e) })
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
}))
|
|
99
|
+
|
|
100
|
+
offs.push(ws.register({
|
|
101
|
+
kind: 'exact',
|
|
102
|
+
path: routes.mutate,
|
|
103
|
+
handler: (req, res) => {
|
|
104
|
+
if (req.method !== 'POST') {
|
|
105
|
+
sendJson(res, 405, { ok: false, code: 'method-not-allowed' })
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
const svc = getSettingsSvc()
|
|
109
|
+
if (svc === undefined || svc === null || typeof svc.mutate !== 'function') {
|
|
110
|
+
sendJson(res, 200, { ok: false, code: 'settings-unavailable', message: 'settings service mutate not available' })
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
readJsonBody(req).then((body) => {
|
|
114
|
+
const ops = body.ops
|
|
115
|
+
const valid = Array.isArray(ops) && ops.every((op) => op !== null && typeof op === 'object' && (op.op === 'set' || op.op === 'unset'))
|
|
116
|
+
if (!valid) {
|
|
117
|
+
sendJson(res, 200, { ok: false, code: 'settings-rejected', message: 'malformed bridge mutate ops' })
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
const expectedRevision = typeof body.expectedRevision === 'number' ? body.expectedRevision : undefined
|
|
121
|
+
svc.mutate(ns, ops, expectedRevision).then(() => {
|
|
122
|
+
// mutate 成功即 describe 回填(写路径归口宿主服务,无第二份状态)
|
|
123
|
+
const descriptor = describeNs(svc, ns)
|
|
124
|
+
if (descriptor === undefined) {
|
|
125
|
+
sendJson(res, 200, { ok: false, code: 'internal', message: `namespace "${ns}" disposed after mutate` })
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
sendJson(res, 200, { ok: true, value: { descriptor } })
|
|
129
|
+
}).catch((e) => {
|
|
130
|
+
sendJson(res, 200, isConflict(e)
|
|
131
|
+
? { ok: false, code: 'settings-conflict', message: e instanceof Error ? e.message : String(e) }
|
|
132
|
+
: { ok: false, code: 'internal', message: e instanceof Error ? e.message : String(e) })
|
|
133
|
+
})
|
|
134
|
+
}).catch((e) => {
|
|
135
|
+
sendJson(res, 200, { ok: false, code: 'bad-request', message: e instanceof Error ? e.message : String(e) })
|
|
136
|
+
})
|
|
137
|
+
},
|
|
138
|
+
}))
|
|
139
|
+
|
|
140
|
+
return offs
|
|
141
|
+
}
|