mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 17:20:35 +08:00
Perf: speed up text line stream
This commit is contained in:
parent
de9429ce92
commit
14ed4c01e5
@ -19,13 +19,12 @@ export const downloadMockAssets = task(require.main === module, __filename)((spa
|
|||||||
([filename, url]) => span
|
([filename, url]) => span
|
||||||
.traceChildAsync(url, async () => {
|
.traceChildAsync(url, async () => {
|
||||||
const res = await fetchWithRetry(url);
|
const res = await fetchWithRetry(url);
|
||||||
|
|
||||||
const src = path.join(OUTPUT_MOCK_DIR, filename);
|
|
||||||
if (!res.body) {
|
if (!res.body) {
|
||||||
throw new Error(`Empty body from ${url}`);
|
throw new Error(`Empty body from ${url}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
await mkdirp(OUTPUT_MOCK_DIR);
|
await mkdirp(OUTPUT_MOCK_DIR);
|
||||||
|
const src = path.join(OUTPUT_MOCK_DIR, filename);
|
||||||
|
|
||||||
return pipeline(
|
return pipeline(
|
||||||
Readable.fromWeb(res.body),
|
Readable.fromWeb(res.body),
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import { processLine } from './process-line';
|
|||||||
|
|
||||||
const getReadableStream = (file: string | FileHandle): ReadableStream => {
|
const getReadableStream = (file: string | FileHandle): ReadableStream => {
|
||||||
if (typeof file === 'string') {
|
if (typeof file === 'string') {
|
||||||
|
// return fs.openAsBlob(file).then(blob => blob.stream())
|
||||||
return Readable.toWeb(fs.createReadStream(file/* , { encoding: 'utf-8' } */));
|
return Readable.toWeb(fs.createReadStream(file/* , { encoding: 'utf-8' } */));
|
||||||
}
|
}
|
||||||
return file.readableWebStream();
|
return file.readableWebStream();
|
||||||
|
|||||||
@ -25,23 +25,25 @@ export class TextLineStream extends TransformStream<string, string> {
|
|||||||
allowCR = false
|
allowCR = false
|
||||||
}: TextLineStreamOptions = {}) {
|
}: TextLineStreamOptions = {}) {
|
||||||
let __buf = '';
|
let __buf = '';
|
||||||
|
let chunkIndex = 0;
|
||||||
|
|
||||||
super({
|
super({
|
||||||
transform(chunk, controller) {
|
transform(chunk, controller) {
|
||||||
chunk = __buf + chunk;
|
chunk = __buf + chunk;
|
||||||
|
chunkIndex = 0;
|
||||||
|
|
||||||
for (; ;) {
|
for (; ;) {
|
||||||
const lfIndex = chunk.indexOf('\n');
|
const lfIndex = chunk.indexOf('\n', chunkIndex);
|
||||||
|
|
||||||
if (allowCR) {
|
if (allowCR) {
|
||||||
const crIndex = chunk.indexOf('\r');
|
const crIndex = chunk.indexOf('\r', chunkIndex);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
crIndex !== -1 && crIndex !== (chunk.length - 1)
|
crIndex !== -1 && crIndex !== (chunk.length - 1)
|
||||||
&& (lfIndex === -1 || (lfIndex - 1) > crIndex)
|
&& (lfIndex === -1 || (lfIndex - 1) > crIndex)
|
||||||
) {
|
) {
|
||||||
controller.enqueue(chunk.slice(0, crIndex));
|
controller.enqueue(chunk.slice(chunkIndex, crIndex));
|
||||||
chunk = chunk.slice(crIndex + 1);
|
chunkIndex = crIndex + 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,15 +53,15 @@ export class TextLineStream extends TransformStream<string, string> {
|
|||||||
if (chunk[lfIndex - 1] === '\r') {
|
if (chunk[lfIndex - 1] === '\r') {
|
||||||
crOrLfIndex--;
|
crOrLfIndex--;
|
||||||
}
|
}
|
||||||
controller.enqueue(chunk.slice(0, crOrLfIndex));
|
controller.enqueue(chunk.slice(chunkIndex, crOrLfIndex));
|
||||||
chunk = chunk.slice(lfIndex + 1);
|
chunkIndex = lfIndex + 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
__buf = chunk;
|
__buf = chunk.slice(chunkIndex);
|
||||||
},
|
},
|
||||||
flush(controller) {
|
flush(controller) {
|
||||||
if (__buf.length > 0) {
|
if (__buf.length > 0) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user