dsh-pocket 0.1.0 → 0.1.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/lib/proxy.mjs +9 -3
- package/lib/web-rpc.js +7 -1
- package/package.json +1 -1
package/lib/proxy.mjs
CHANGED
|
@@ -33,12 +33,13 @@ function loopbackAuthority(headers, upstream) {
|
|
|
33
33
|
* @param {{host:string,port:number}} [opts.upstream] 上游 dsh web(默认 127.0.0.1:3080)
|
|
34
34
|
* @returns {Promise<{server:import('node:http').Server, close:()=>Promise<void>}>}
|
|
35
35
|
*/
|
|
36
|
-
export function createPocketProxy({ port = 3081, host = '0.0.0.0', upstream = DEFAULT_UPSTREAM } = {}) {
|
|
36
|
+
export function createPocketProxy({ port = 3081, host = '0.0.0.0', upstream = DEFAULT_UPSTREAM, log = null } = {}) {
|
|
37
37
|
const server = createServer((req, res) => {
|
|
38
38
|
const headers = loopbackAuthority({ ...req.headers }, upstream);
|
|
39
39
|
const proxyReq = httpRequest(
|
|
40
40
|
{ host: upstream.host, port: upstream.port, method: req.method, path: req.url, headers, agent: false },
|
|
41
41
|
(proxyRes) => {
|
|
42
|
+
log?.(`${req.method} ${req.url} -> ${proxyRes.statusCode}`);
|
|
42
43
|
res.writeHead(proxyRes.statusCode ?? 502, proxyRes.headers);
|
|
43
44
|
proxyRes.pipe(res);
|
|
44
45
|
},
|
|
@@ -50,7 +51,7 @@ export function createPocketProxy({ port = 3081, host = '0.0.0.0', upstream = DE
|
|
|
50
51
|
req.pipe(proxyReq);
|
|
51
52
|
});
|
|
52
53
|
|
|
53
|
-
// WebSocket upgrade(DSH 的 /api/events.host 流式通道)原样透传
|
|
54
|
+
// WebSocket upgrade(DSH 的 /api/events.mux + events.host 流式通道)原样透传
|
|
54
55
|
server.on('upgrade', (req, socket, head) => {
|
|
55
56
|
const headers = loopbackAuthority({ ...req.headers }, upstream);
|
|
56
57
|
const proxyReq = httpRequest({
|
|
@@ -64,11 +65,16 @@ export function createPocketProxy({ port = 3081, host = '0.0.0.0', upstream = DE
|
|
|
64
65
|
raw.push(`${k}: ${Array.isArray(v) ? v.join(', ') : v}`);
|
|
65
66
|
}
|
|
66
67
|
socket.write(`${raw.join('\r\n')}\r\n\r\n`);
|
|
67
|
-
socket.write(proxyHead);
|
|
68
|
+
if (proxyHead?.length) socket.write(proxyHead);
|
|
68
69
|
proxySocket.pipe(socket);
|
|
69
70
|
socket.pipe(proxySocket);
|
|
70
71
|
});
|
|
71
72
|
proxyReq.on('error', () => socket.destroy());
|
|
73
|
+
// 关键:浏览器在握手请求后可能立即发出首帧(如 mux 流的初始 RPC),
|
|
74
|
+
// node 把它放在 upgrade 事件的 head 里。必须先于 end() 写入 proxyReq,
|
|
75
|
+
// 让上游在 upgrade 事件里就拿到它(与直连行为一致);等 101 之后再写
|
|
76
|
+
// 会变成迟到的 socket 数据,DSH 的 mux 协议可能错过这个窗口。
|
|
77
|
+
if (head?.length) proxyReq.write(head);
|
|
72
78
|
proxyReq.end();
|
|
73
79
|
socket.on('error', () => socket.destroy());
|
|
74
80
|
});
|
package/lib/web-rpc.js
CHANGED
|
@@ -6,8 +6,14 @@ function ok(value) {
|
|
|
6
6
|
return { ok: true, value };
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* 构造符合 DSH rpcErrorSchema 的错误(按 code 的 discriminated union,
|
|
11
|
+
* details 必填且分分支定形;'internal' 不在合法 code 集合里)。
|
|
12
|
+
*/
|
|
9
13
|
function fail(code, message) {
|
|
10
|
-
return { ok: false, error: { code, message } };
|
|
14
|
+
if (code === 'cancelled') return { ok: false, error: { code: 'cancelled', message, details: {} } };
|
|
15
|
+
// 其余一律归入 bad-request(issues 是自由数组)
|
|
16
|
+
return { ok: false, error: { code: 'bad-request', message, details: { issues: [{ message }] } } };
|
|
11
17
|
}
|
|
12
18
|
|
|
13
19
|
/** 注册 /dsh-pocket 逻辑通道(仅本机 loopback 可调)。 */
|