feat: 集成漫画源并支持在线阅读
This commit is contained in:
+36
-1
@@ -38,6 +38,37 @@ function fetchWithElectron(url, options = {}) {
|
||||
return fetchWithProxy(url, options);
|
||||
}
|
||||
|
||||
async function responseBytes(res, maxBytes) {
|
||||
const declared = Number(res.headers && res.headers.get && res.headers.get('content-length'));
|
||||
if (Number.isFinite(declared) && declared > maxBytes) {
|
||||
try { if (res.body && res.body.cancel) await res.body.cancel(); } catch (e) { /* ignore */ }
|
||||
return null;
|
||||
}
|
||||
if (!res.body || typeof res.body.getReader !== 'function') {
|
||||
const bytes = Buffer.from(await res.arrayBuffer());
|
||||
return bytes.length <= maxBytes ? bytes : null;
|
||||
}
|
||||
const reader = res.body.getReader();
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
try {
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
const chunk = Buffer.from(value);
|
||||
size += chunk.length;
|
||||
if (size > maxBytes) {
|
||||
await reader.cancel();
|
||||
return null;
|
||||
}
|
||||
chunks.push(chunk);
|
||||
}
|
||||
} finally {
|
||||
try { reader.releaseLock(); } catch (e) { /* ignore */ }
|
||||
}
|
||||
return Buffer.concat(chunks, size);
|
||||
}
|
||||
|
||||
// 简易 cookie jar: Map<domain, Map<name, value>>
|
||||
const cookieJar = new Map();
|
||||
|
||||
@@ -218,4 +249,8 @@ async function withRetry(fn, { tries = 2, delay = 1000 } = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { UA, fetchRaw, fetchText, fetchJson, decodeEntities, stripTags, clampPage, tooShort, isRetryable, withRetry, getCookies, setCookies, clearCookies, setProxy, getProxy, fetchWithProxy };
|
||||
module.exports = {
|
||||
UA, fetchRaw, fetchText, fetchJson, responseBytes, decodeEntities, stripTags,
|
||||
clampPage, tooShort, isRetryable, withRetry, getCookies, setCookies, clearCookies,
|
||||
setProxy, getProxy, fetchWithProxy
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user