我有一個庫,通過 npm 作為“按請求”發布,除其他外,它可以自動解壓縮 Web 內容。處理這種情況的部分代碼如下所示:
if (!options.dontDecompress || !binary) {
if (contentEncoding === 'gzip' || (options.autoDecompress && /\b(gzip|gzipped|gunzip)\b/.test(contentType))) {
source = zlib.createGunzip();
res.pipe(source);
}
// *** start of temporary new code ***
else if (contentEncoding === 'x-gzip' && options.autoDecompress) {
source = zlib.createGunzip(); // zlib.createUnzip() doesn't work either
res.pipe(source);
}
// *** end of temporary new code ***
else if (contentEncoding === 'deflate' || (options.autoDecompress && /\bdeflate\b/.test(contentType))) {
source = zlib.createInflate();
res.pipe(source);
}
else if (contentEncoding === 'br') {
source = zlib.createBrotliDecompress();
res.pipe(source);
}
else if (contentEncoding && contentEncoding !== 'identity') {
reject(UNSUPPORTED_MEDIA_TYPE);
return;
}
}
在我嘗試從這里讀取天文資訊檔案之前,代碼一直運行良好:https : //cdsarc.cds.unistra.fr/viz-bin/nph-Cat/txt.gz?VII/118/names。資料
我被擊中了reject(UNSUPPORTED_MEDIA_TYPE)錯誤處理程式,因為我還沒有具體的處理Content-Type的x-gzip。x-gzip然而,簡單地添加一個檢查并不能解決問題。
zlib 正在阻塞資料,回傳此錯誤:
錯誤:
Zlib.zlibOnError [as one rror] (node:zlib:190:17) 處的標頭檢查不正確
我需要不同的解壓庫嗎?我四處尋找,但還沒有找到好的解決方案。根據這個先前的堆疊溢位答案:內容編碼的“x-gzip”和“gzip”之間的區別
...gzip 和 x-gzip 應該是一樣的。它不是那樣的。另一方面,我嘗試過的任何網路瀏覽器都無法從 cdsarc.cds.unistra.fr URL 獲取和顯示文本。
uj5u.com熱心網友回復:
以下解決方案對我有用,將 shell gzip 解壓縮操作替換為 zlib 和createGunzip(). 我能想到這個修復作業的唯一原因可能是導致失敗的特定網站提供的壓縮資料流有點奇怪,shell 命令可以容忍這種情況,但 zlib 不是.
if (!checkedGzipShell) {
checkedGzipShell = true;
hasGzipShell = true;
const gzipProc = spawn('gzip', ['-L']);
await new Promise<void>(resolve => {
gzipProc.once('error', () => { hasGzipShell = false; resolve(); });
gzipProc.stdout.once('end', resolve);
});
}
if (contentEncoding === 'gzip' || contentEncoding === 'x-gzip' ||
(options.autoDecompress && /\b(gzip|gzipped|gunzip)\b/.test(contentType))) {
if (hasGzipShell) {
const gzipProc = spawn('gzip', ['-dc']);
source = gzipProc.stdout;
res.pipe(gzipProc.stdin);
}
else {
source = zlib.createGunzip();
res.pipe(source);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/406712.html
標籤:
