@tina_summer/codepocket 2.6.2 → 2.6.4
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 +20 -4
- package/package.json +5 -5
- package/src/index.ts +37 -17
package/bin/codepocket.js
CHANGED
|
@@ -337,7 +337,9 @@ const CONNECT_ONLY = ['connect', 'c'].includes(args[0])
|
|
|
337
337
|
const filteredArgs = CONNECT_ONLY ? args.slice(1) : args
|
|
338
338
|
|
|
339
339
|
let hubUrl = process.env.CPKT_HUB_URL || process.env.HAPI_HUB_URL || 'http://127.0.0.1:3006'
|
|
340
|
-
|
|
340
|
+
// Must match miniprogram/utils/config.local.js default — otherwise new users running
|
|
341
|
+
// `codepocket --key XXX` (no --relay) silently fail to connect.
|
|
342
|
+
const DEFAULT_RELAY_URL = 'wss://bkstory.cn/ws'
|
|
341
343
|
let relayUrl = process.env.RELAY_WS_URL || process.env.RELAY_URL || DEFAULT_RELAY_URL
|
|
342
344
|
let relayKey = process.env.RELAY_KEY || ''
|
|
343
345
|
let noHub = false
|
|
@@ -502,14 +504,26 @@ async function startSSE() {
|
|
|
502
504
|
try { const token = await findHubToken(); jwt = await authenticateHub(token) } catch {}
|
|
503
505
|
res = await fetch(url, { headers: { Accept: 'text/event-stream', ...(jwt ? { Authorization: `Bearer ${jwt}` } : {}) } })
|
|
504
506
|
}
|
|
505
|
-
if (!res.ok || !res.body) { setTimeout(startSSE, 5000); return }
|
|
507
|
+
if (!res.ok || !res.body) { log(`[codepocket] SSE connect failed: ${res.status}, retry in 5s`); setTimeout(startSSE, 5000); return }
|
|
508
|
+
log('[codepocket] SSE connected')
|
|
506
509
|
const reader = res.body.getReader()
|
|
507
510
|
const decoder = new TextDecoder()
|
|
508
511
|
let buffer = ''
|
|
512
|
+
let lastDataAt = Date.now()
|
|
513
|
+
// Watchdog: hub emits heartbeat every 10s. If silent for 45s, the TCP
|
|
514
|
+
// connection is zombie (NAT/Nginx/CF silent drop) — force reconnect.
|
|
515
|
+
const watchdog = setInterval(() => {
|
|
516
|
+
if (Date.now() - lastDataAt > 45000) {
|
|
517
|
+
log('[codepocket] SSE watchdog: no data for 45s, forcing reconnect')
|
|
518
|
+
clearInterval(watchdog)
|
|
519
|
+
try { reader.cancel() } catch {}
|
|
520
|
+
}
|
|
521
|
+
}, 15000)
|
|
509
522
|
async function pump() {
|
|
510
523
|
while (true) {
|
|
511
524
|
const { done, value } = await reader.read()
|
|
512
525
|
if (done) break
|
|
526
|
+
lastDataAt = Date.now()
|
|
513
527
|
buffer += decoder.decode(value, { stream: true })
|
|
514
528
|
const lines = buffer.split('\n')
|
|
515
529
|
buffer = lines.pop() || ''
|
|
@@ -519,10 +533,12 @@ async function startSSE() {
|
|
|
519
533
|
}
|
|
520
534
|
}
|
|
521
535
|
}
|
|
536
|
+
clearInterval(watchdog)
|
|
537
|
+
log('[codepocket] SSE stream ended, reconnecting in 5s')
|
|
522
538
|
setTimeout(startSSE, 5000)
|
|
523
539
|
}
|
|
524
|
-
pump().catch(() => { setTimeout(startSSE, 5000) })
|
|
525
|
-
} catch { setTimeout(startSSE, 5000) }
|
|
540
|
+
pump().catch(() => { clearInterval(watchdog); setTimeout(startSSE, 5000) })
|
|
541
|
+
} catch (e) { log(`[codepocket] SSE error: ${e?.message}, retry in 5s`); setTimeout(startSSE, 5000) }
|
|
526
542
|
}
|
|
527
543
|
|
|
528
544
|
// ── 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.4",
|
|
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.4",
|
|
24
|
+
"@tina_summer/codepocket-darwin-x64": "2.6.4",
|
|
25
|
+
"@tina_summer/codepocket-linux-x64": "2.6.4",
|
|
26
|
+
"@tina_summer/codepocket-win32-x64": "2.6.4"
|
|
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
|
}
|