[Network] fetch proxy 读取分块流到 EOF 时抛 TypeError: Cannot read properties of undefined (reading 'length')
Nessuno ha ancora preso questa issue.
- #750 di @lnuxe — chiusa senza merge
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 76/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- typescript
Direzione di ricerca
Leggi src/network/fetch.proxy.ts, in particolare ResponseProxyHandler.mockReader intorno alla riga 81, e confronta il risultato EOF del reader con il percorso di accumulo. Aggiungi un test di regressione per una risposta suddivisa in chunk il cui reader restituisce dati e poi { done: true, value: undefined }. Il risultato atteso è che EOF non generi più un errore e che la risposta dello stream continui a riportare la dimensione finale e lo stato di completamento.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
现象
开启 vConsole(默认含 Network 面板)后,用 fetch + response.body.getReader() 消费分块(chunked)响应,读到流结束时抛异常:
TypeError: Cannot read properties of undefined (reading 'length')
at ResponseProxyHandler.mockReader (src/network/fetch.proxy.ts:81)
调用方正常的"流结束"路径被异常打断;不加载 vConsole 时同一段代码正常结束。
复现(vConsole 3.15.1 实测;master 源码同一段未修复)
- 最小服务端(Node,不设置 Content-Length,响应自动为 chunked):
require('http').createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/event-stream' })
res.write('data: hello\n\n')
setTimeout(() => res.end(), 300)
}).listen(5301)
- 页面(先加载并初始化 vConsole,再发起请求):
<script src="https://unpkg.com/[email protected]/dist/vconsole.min.js"></script>
<script>
new VConsole()
;(async () => {
const res = await fetch('/sse')
const reader = res.body.getReader()
for (;;) {
const chunk = await reader.read()
if (chunk.done) break
}
console.log('stream ended normally')
})().catch(e => console.error('read failed:', e))
</script>
- 结果:
- 加 vConsole:第一次
read()返回{ done: false, value: 15 bytes },第二次read()抛上述 TypeError; - 去掉 vConsole:输出
stream ended normally。
- 加 vConsole:第一次
原因
src/network/fetch.proxy.ts 的 mockReader() 在每次 read() 结果上累积字节:
if (!readerReceivedValue) {
readerReceivedValue = new Uint8Array(result.value); // L79
} else {
const newValue = new Uint8Array(readerReceivedValue.length + result.value.length); // L81
newValue.set(readerReceivedValue);
newValue.set(result.value, readerReceivedValue.length);
readerReceivedValue = newValue;
}
按 Streams 规范,流结束的那次读取返回 { value: undefined, done: true }——done 块本来就没有 value。只要之前已收到过数据,就会走 else 分支,result.value.length 抛 TypeError。
只有 chunked 响应会命中:afterFetch 对非 chunked 响应把 item.readyState 置 4,getReader() 直接返回原生 reader、不做包装;SSE 和各类流式接口都是 chunked,所以受影响的主要就是流式场景。
影响
任何用 getReader() 消费流式响应(SSE、流式对话、流式下载)的应用,在 vConsole 开启时都会在 EOF 收到一个伪异常。调用方通常把它当成网络中断或流失败处理(重试、错误提示、错误上报),而实际上数据已经完整收到。
建议修复
done 块不参与累积,直接更新统计并返回:
reader.read = () => {
return _read.apply(reader).then((result) => {
if (result.value) {
if (!readerReceivedValue) {
readerReceivedValue = new Uint8Array(result.value);
} else {
const newValue = new Uint8Array(readerReceivedValue.length + result.value.length);
newValue.set(readerReceivedValue);
newValue.set(result.value, readerReceivedValue.length);
readerReceivedValue = newValue;
}
}
this.item.endTime = Date.now();
this.item.costTime = this.item.endTime - (this.item.startTime || this.item.endTime);
this.item.readyState = result.done ? 4 : 3;
this.item.statusText = result.done ? String(this.item.status) : 'Loading';
this.item.responseSize = readerReceivedValue ? readerReceivedValue.length : 0;
this.item.responseSizeText = tool.getBytesText(this.item.responseSize);
if (result.done && readerReceivedValue) {
this.item.response = Helper.genResonseByResponseType(this.item.responseType, readerReceivedValue);
}
this.onUpdateCallback(this.item);
return result;
});
};
关联
- #669 同类问题(vConsole 3.15.1,2024-11 关闭,未修复)
- #735 同类现象(仍开启)
- Lingua principale
- TypeScript
- Stelle
- 17.5k
- Fork
- 3k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Tencent/vConsole
-
Uncaught TypeError when assigning null to xhr.onreadystatechange/onprogress (breaks hls.js abort)Forse già presa @cyhano l’ha presa 76 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
-
Prototype Pollution in network.model.tsForse già presa @dvd233 l’ha presa 77 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
不发新版 npm 包了吗Aperta
Difficoltà 2/5 Meno di un'ora Idoneità per principianti 15/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
-
处理流式数据的错误报警Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 42/100
Tutte le issue di Tencent/vConsole
Issue simili
-
status: triage type: bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
WhiteTowerAI/ohmygame#1225 ·
I maintainer di solito rispondono entro 1 giorno
-
Add: KBS TV SDApertastreams:add
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
ardia-browser/ardia#5042 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
keycloakify/oidc-spa#211 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno