mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
fix(encoding/csv/stream): cancel lineReader if readable is canceled (#2401)
This commit is contained in:
parent
dab98b6e1b
commit
eb4f512cc4
@ -56,6 +56,7 @@ export class CSVStream implements TransformStream<string, Array<string>> {
|
||||
this.#lineReader = new StreamLineReader(this.#lines.readable.getReader());
|
||||
this.#readable = new ReadableStream<Array<string>>({
|
||||
pull: (controller) => this.#pull(controller),
|
||||
cancel: () => this.#lineReader.cancel(),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -295,3 +295,31 @@ function createReadableStreamFromString(s: string): ReadableStream<string> {
|
||||
new TextDecoderStream(),
|
||||
);
|
||||
}
|
||||
|
||||
// Work around resource leak error with TextDecoderStream:
|
||||
// https://github.com/denoland/deno/issues/13142
|
||||
export const MyTextDecoderStream = () => {
|
||||
const textDecoder = new TextDecoder();
|
||||
return new TransformStream({
|
||||
transform(chunk: Uint8Array, controller: TransformStreamDefaultController) {
|
||||
controller.enqueue(textDecoder.decode(chunk));
|
||||
},
|
||||
flush(controller: TransformStreamDefaultController) {
|
||||
controller.enqueue(textDecoder.decode());
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
Deno.test({
|
||||
name:
|
||||
"[encoding/csv/stream] cancel CSVStream during iteration does not leak file",
|
||||
permissions: { read: [testdataDir] },
|
||||
fn: async () => {
|
||||
const file = await Deno.open(join(testdataDir, "large.csv"));
|
||||
const readable = file.readable.pipeThrough(MyTextDecoderStream())
|
||||
.pipeThrough(new CSVStream());
|
||||
for await (const _record of readable) {
|
||||
break;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
32768
encoding/testdata/large.csv
vendored
Normal file
32768
encoding/testdata/large.csv
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user