Skip to content

Commit 0df32c6

Browse files
committed
fix(csv-parse): call destroy on end (fix #410)
1 parent 01e9061 commit 0df32c6

File tree

8 files changed

+97
-29
lines changed

8 files changed

+97
-29
lines changed

packages/csv-parse/dist/cjs/index.cjs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1333,9 +1333,16 @@ class Parser extends stream.Transform {
13331333
}, () => {
13341334
this.push(null);
13351335
this.end();
1336-
this.destroy();
1337-
// Note 231005, end wasnt used and destroy was called as:
1338-
// this.on('end', this.destroy);
1336+
// Fix #333 and break #410
1337+
// ko: api.stream.iterator.coffee
1338+
// ko with v21.4.0, ok with node v20.5.1: api.stream.finished # aborted (with generate())
1339+
// ko: api.stream.finished # aborted (with Readable)
1340+
// this.destroy()
1341+
// Fix #410 and partially break #333
1342+
// ok: api.stream.iterator.coffee
1343+
// ok: api.stream.finished # aborted (with generate())
1344+
// broken: api.stream.finished # aborted (with Readable)
1345+
this.on('end', this.destroy);
13391346
});
13401347
if(err !== undefined){
13411348
this.state.stop = true;

packages/csv-parse/dist/esm/index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -6455,9 +6455,16 @@ class Parser extends Transform {
64556455
}, () => {
64566456
this.push(null);
64576457
this.end();
6458-
this.destroy();
6459-
// Note 231005, end wasnt used and destroy was called as:
6460-
// this.on('end', this.destroy);
6458+
// Fix #333 and break #410
6459+
// ko: api.stream.iterator.coffee
6460+
// ko with v21.4.0, ok with node v20.5.1: api.stream.finished # aborted (with generate())
6461+
// ko: api.stream.finished # aborted (with Readable)
6462+
// this.destroy()
6463+
// Fix #410 and partially break #333
6464+
// ok: api.stream.iterator.coffee
6465+
// ok: api.stream.finished # aborted (with generate())
6466+
// broken: api.stream.finished # aborted (with Readable)
6467+
this.on('end', this.destroy);
64616468
});
64626469
if(err !== undefined){
64636470
this.state.stop = true;

packages/csv-parse/dist/iife/index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -6458,9 +6458,16 @@ var csv_parse = (function (exports) {
64586458
}, () => {
64596459
this.push(null);
64606460
this.end();
6461-
this.destroy();
6462-
// Note 231005, end wasnt used and destroy was called as:
6463-
// this.on('end', this.destroy);
6461+
// Fix #333 and break #410
6462+
// ko: api.stream.iterator.coffee
6463+
// ko with v21.4.0, ok with node v20.5.1: api.stream.finished # aborted (with generate())
6464+
// ko: api.stream.finished # aborted (with Readable)
6465+
// this.destroy()
6466+
// Fix #410 and partially break #333
6467+
// ok: api.stream.iterator.coffee
6468+
// ok: api.stream.finished # aborted (with generate())
6469+
// broken: api.stream.finished # aborted (with Readable)
6470+
this.on('end', this.destroy);
64646471
});
64656472
if(err !== undefined){
64666473
this.state.stop = true;

packages/csv-parse/dist/umd/index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -6461,9 +6461,16 @@
64616461
}, () => {
64626462
this.push(null);
64636463
this.end();
6464-
this.destroy();
6465-
// Note 231005, end wasnt used and destroy was called as:
6466-
// this.on('end', this.destroy);
6464+
// Fix #333 and break #410
6465+
// ko: api.stream.iterator.coffee
6466+
// ko with v21.4.0, ok with node v20.5.1: api.stream.finished # aborted (with generate())
6467+
// ko: api.stream.finished # aborted (with Readable)
6468+
// this.destroy()
6469+
// Fix #410 and partially break #333
6470+
// ok: api.stream.iterator.coffee
6471+
// ok: api.stream.finished # aborted (with generate())
6472+
// broken: api.stream.finished # aborted (with Readable)
6473+
this.on('end', this.destroy);
64676474
});
64686475
if(err !== undefined){
64696476
this.state.stop = true;

packages/csv-parse/lib/index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ class Parser extends Transform {
3333
}, () => {
3434
this.push(null);
3535
this.end();
36-
this.destroy();
37-
// Note 231005, end wasnt used and destroy was called as:
38-
// this.on('end', this.destroy);
36+
// Fix #333 and break #410
37+
// ko: api.stream.iterator.coffee
38+
// ko with v21.4.0, ok with node v20.5.1: api.stream.finished # aborted (with generate())
39+
// ko: api.stream.finished # aborted (with Readable)
40+
// this.destroy()
41+
// Fix #410 and partially break #333
42+
// ok: api.stream.iterator.coffee
43+
// ok: api.stream.finished # aborted (with generate())
44+
// broken: api.stream.finished # aborted (with Readable)
45+
this.on('end', this.destroy);
3946
});
4047
if(err !== undefined){
4148
this.state.stop = true;

packages/csv-parse/test/api.stream.finished.coffee

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
import { Readable } from 'node:stream'
23
import * as stream from 'node:stream/promises'
34
import { generate } from 'csv-generate'
45
import { parse } from '../lib/index.js'
@@ -15,8 +16,10 @@ describe 'API stream.finished', ->
1516
await stream.finished parser
1617
records.length.should.eql 10
1718

18-
it 'resolved with `to_line`', ->
19+
it 'aborted (with generate())', ->
1920
# See https://github.com/adaltas/node-csv/issues/333
21+
# See https://github.com/adaltas/node-csv/issues/410
22+
# Prevent `Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close`
2023
records = []
2124
parser = generate(length: 10).pipe parse to_line: 3
2225
parser.on 'readable', () =>
@@ -25,6 +28,24 @@ describe 'API stream.finished', ->
2528
await stream.finished parser
2629
records.length.should.eql 3
2730

31+
it.skip 'aborted (with Readable)', ->
32+
# See https://github.com/adaltas/node-csv/issues/333
33+
# See https://github.com/adaltas/node-csv/issues/410
34+
# Prevent `Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close`
35+
records = []
36+
reader = new Readable
37+
highWaterMark: 10
38+
read: (size) ->
39+
for i in [0...size]
40+
this.push "#{size},#{i}\n"
41+
parser = reader.pipe parse to_line: 3
42+
parser.on 'readable', () =>
43+
while (record = parser.read()) isnt null
44+
records.push record
45+
await stream.finished parser
46+
console.log records
47+
records.length.should.eql 3
48+
2849
it 'rejected on error', ->
2950
parser = parse to_line: 3
3051
parser.write 'a,b,c\n'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
import * as stream from 'node:stream/promises'
3+
import { generate } from 'csv-generate'
4+
import { parse } from '../lib/index.js'
5+
6+
describe 'API stream.iterator', ->
7+
8+
it 'classic', ->
9+
parser = generate(length: 10).pipe parse()
10+
records = []
11+
for await record from parser
12+
records.push record
13+
records.length.should.eql 10
14+
15+
it 'with iteractor stoped in between', ->
16+
# See https://github.com/adaltas/node-csv/issues/333
17+
# See https://github.com/adaltas/node-csv/issues/410
18+
# Prevent `Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close`
19+
records = []
20+
parser = generate(length: 10).pipe parse
21+
to_line: 2
22+
records = []
23+
for await record from parser
24+
records.push record
25+
records.length.should.eql 2

packages/csv-parse/test/option.to_line.coffee

-13
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,3 @@ describe 'Option `to_line`', ->
100100
[ 'd','e','f' ]
101101
] unless err
102102
next err
103-
104-
it 'resolved with `to_line`', ->
105-
# Prevent `Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close`
106-
reader = new Readable
107-
highWaterMark: 100
108-
read: (size) ->
109-
setImmediate =>
110-
for i in [0...size]
111-
this.push "#{size},#{i}\n"
112-
parser = reader.pipe parse to_line: 3
113-
parser.on 'readable', () =>
114-
while parser.read() isnt null then true
115-
await finished parser

0 commit comments

Comments
 (0)