@tina_summer/codepocket 2.6.2 → 2.6.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.
- package/bin/codepocket.js +17 -3
- package/package.json +5 -5
- package/src/index.ts +37 -17
package/bin/codepocket.js
CHANGED
|
@@ -502,14 +502,26 @@ async function startSSE() {
|
|
|
502
502
|
try { const token = await findHubToken(); jwt = await authenticateHub(token) } catch {}
|
|
503
503
|
res = await fetch(url, { headers: { Accept: 'text/event-stream', ...(jwt ? { Authorization: `Bearer ${jwt}` } : {}) } })
|
|
504
504
|
}
|
|
505
|
-
if (!res.ok || !res.body) { setTimeout(startSSE, 5000); return }
|
|
505
|
+
if (!res.ok || !res.body) { log(`[codepocket] SSE connect failed: ${res.status}, retry in 5s`); setTimeout(startSSE, 5000); return }
|
|
506
|
+
log('[codepocket] SSE connected')
|
|
506
507
|
const reader = res.body.getReader()
|
|
507
508
|
const decoder = new TextDecoder()
|
|
508
509
|
let buffer = ''
|
|
510
|
+
let lastDataAt = Date.now()
|
|
511
|
+
// Watchdog: hub emits heartbeat every 10s. If silent for 45s, the TCP
|
|
512
|
+
// connection is zombie (NAT/Nginx/CF silent drop) — force reconnect.
|
|
513
|
+
const watchdog = setInterval(() => {
|
|
514
|
+
if (Date.now() - lastDataAt > 45000) {
|
|
515
|
+
log('[codepocket] SSE watchdog: no data for 45s, forcing reconnect')
|
|
516
|
+
clearInterval(watchdog)
|
|
517
|
+
try { reader.cancel() } catch {}
|
|
518
|
+
}
|
|
519
|
+
}, 15000)
|
|
509
520
|
async function pump() {
|
|
510
521
|
while (true) {
|
|
511
522
|
const { done, value } = await reader.read()
|
|
512
523
|
if (done) break
|
|
524
|
+
lastDataAt = Date.now()
|
|
513
525
|
buffer += decoder.decode(value, { stream: true })
|
|
514
526
|
const lines = buffer.split('\n')
|
|
515
527
|
buffer = lines.pop() || ''
|
|
@@ -519,10 +531,12 @@ async function startSSE() {
|
|
|
519
531
|
}
|
|
520
532
|
}
|
|
521
533
|
}
|
|
534
|
+
clearInterval(watchdog)
|
|
535
|
+
log('[codepocket] SSE stream ended, reconnecting in 5s')
|
|
522
536
|
setTimeout(startSSE, 5000)
|
|
523
537
|
}
|
|
524
|
-
pump().catch(() => { setTimeout(startSSE, 5000) })
|
|
525
|
-
} catch { setTimeout(startSSE, 5000) }
|
|
538
|
+
pump().catch(() => { clearInterval(watchdog); setTimeout(startSSE, 5000) })
|
|
539
|
+
} catch (e) { log(`[codepocket] SSE error: ${e?.message}, retry in 5s`); setTimeout(startSSE, 5000) }
|
|
526
540
|
}
|
|
527
541
|
|
|
528
542
|
// ── End-to-end encryption (X25519 + XSalsa20-Poly1305) ──
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tina_summer/codepocket",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.3",
|
|
4
4
|
"description": "CodePocket — 基于ClaudeCode的微信小程序工作台",
|
|
5
5
|
"bin": {
|
|
6
6
|
"codepocket": "bin/codepocket.js",
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
"ws": "^8.18.0"
|
|
21
21
|
},
|
|
22
22
|
"optionalDependencies": {
|
|
23
|
-
"@tina_summer/codepocket-darwin-arm64": "2.6.
|
|
24
|
-
"@tina_summer/codepocket-darwin-x64": "2.6.
|
|
25
|
-
"@tina_summer/codepocket-linux-x64": "2.6.
|
|
26
|
-
"@tina_summer/codepocket-win32-x64": "2.6.
|
|
23
|
+
"@tina_summer/codepocket-darwin-arm64": "2.6.3",
|
|
24
|
+
"@tina_summer/codepocket-darwin-x64": "2.6.3",
|
|
25
|
+
"@tina_summer/codepocket-linux-x64": "2.6.3",
|
|
26
|
+
"@tina_summer/codepocket-win32-x64": "2.6.3"
|
|
27
27
|
},
|
|
28
28
|
"engines": {
|
|
29
29
|
"node": ">=18"
|
package/src/index.ts
CHANGED
|
@@ -100,32 +100,52 @@ function startSSE() {
|
|
|
100
100
|
return
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
console.log('[SSE] connected')
|
|
103
104
|
const reader = res.body.getReader()
|
|
104
105
|
const decoder = new TextDecoder()
|
|
105
106
|
let buffer = ''
|
|
107
|
+
let lastDataAt = Date.now()
|
|
108
|
+
|
|
109
|
+
// Watchdog: hub emits heartbeat every 10s. If silent for 45s, the TCP
|
|
110
|
+
// connection is zombie (NAT/Nginx/CF silent drop) — force reconnect.
|
|
111
|
+
const watchdog = setInterval(() => {
|
|
112
|
+
if (Date.now() - lastDataAt > 45000) {
|
|
113
|
+
console.error('[SSE] watchdog: no data for 45s, forcing reconnect')
|
|
114
|
+
clearInterval(watchdog)
|
|
115
|
+
try { reader.cancel() } catch {}
|
|
116
|
+
}
|
|
117
|
+
}, 15000)
|
|
106
118
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
119
|
+
try {
|
|
120
|
+
while (true) {
|
|
121
|
+
const { done, value } = await reader.read()
|
|
122
|
+
if (done) break
|
|
123
|
+
|
|
124
|
+
lastDataAt = Date.now()
|
|
125
|
+
buffer += decoder.decode(value, { stream: true })
|
|
126
|
+
const lines = buffer.split('\n')
|
|
127
|
+
buffer = lines.pop() || ''
|
|
128
|
+
|
|
129
|
+
for (const line of lines) {
|
|
130
|
+
if (line.startsWith('data:')) {
|
|
131
|
+
const raw = line.slice(5).trim()
|
|
132
|
+
if (!raw) continue
|
|
133
|
+
try {
|
|
134
|
+
const event = JSON.parse(raw)
|
|
135
|
+
forwardToRelay({ type: 'sse-event', event })
|
|
136
|
+
} catch {}
|
|
137
|
+
}
|
|
123
138
|
}
|
|
124
139
|
}
|
|
140
|
+
console.error('[SSE] stream ended, reconnecting in 5s...')
|
|
141
|
+
} finally {
|
|
142
|
+
clearInterval(watchdog)
|
|
125
143
|
}
|
|
144
|
+
|
|
145
|
+
setTimeout(startSSE, 5000)
|
|
126
146
|
} catch (err: any) {
|
|
127
147
|
if (err.name !== 'AbortError') {
|
|
128
|
-
console.error(
|
|
148
|
+
console.error(`[SSE] error: ${err.message}, reconnecting in 5s...`)
|
|
129
149
|
setTimeout(startSSE, 5000)
|
|
130
150
|
}
|
|
131
151
|
}
|