Skip to content

Commit ca3f55b

Browse files
committed
fix(csv-parse): destroy on end and call close event (fix #333)
1 parent 568364e commit ca3f55b

File tree

8 files changed

+78
-1
lines changed

8 files changed

+78
-1
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@ class Parser extends stream.Transform {
13221322
this.push(record);
13231323
}, () => {
13241324
this.push(null);
1325+
this.on('end', this.destroy);
13251326
});
13261327
if(err !== undefined){
13271328
this.state.stop = true;
@@ -1337,6 +1338,7 @@ class Parser extends stream.Transform {
13371338
this.push(record);
13381339
}, () => {
13391340
this.push(null);
1341+
this.on('end', this.destroy);
13401342
});
13411343
callback(err);
13421344
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -6380,6 +6380,7 @@ class Parser extends Transform {
63806380
this.push(record);
63816381
}, () => {
63826382
this.push(null);
6383+
this.on('end', this.destroy);
63836384
});
63846385
if(err !== undefined){
63856386
this.state.stop = true;
@@ -6395,6 +6396,7 @@ class Parser extends Transform {
63956396
this.push(record);
63966397
}, () => {
63976398
this.push(null);
6399+
this.on('end', this.destroy);
63986400
});
63996401
callback(err);
64006402
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -6383,6 +6383,7 @@ var csv_parse = (function (exports) {
63836383
this.push(record);
63846384
}, () => {
63856385
this.push(null);
6386+
this.on('end', this.destroy);
63866387
});
63876388
if(err !== undefined){
63886389
this.state.stop = true;
@@ -6398,6 +6399,7 @@ var csv_parse = (function (exports) {
63986399
this.push(record);
63996400
}, () => {
64006401
this.push(null);
6402+
this.on('end', this.destroy);
64016403
});
64026404
callback(err);
64036405
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -6386,6 +6386,7 @@
63866386
this.push(record);
63876387
}, () => {
63886388
this.push(null);
6389+
this.on('end', this.destroy);
63896390
});
63906391
if(err !== undefined){
63916392
this.state.stop = true;
@@ -6401,6 +6402,7 @@
64016402
this.push(record);
64026403
}, () => {
64036404
this.push(null);
6405+
this.on('end', this.destroy);
64046406
});
64056407
callback(err);
64066408
}

packages/csv-parse/lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Parser extends Transform {
3232
this.push(record);
3333
}, () => {
3434
this.push(null);
35+
this.on('end', this.destroy);
3536
});
3637
if(err !== undefined){
3738
this.state.stop = true;
@@ -47,6 +48,7 @@ class Parser extends Transform {
4748
this.push(record);
4849
}, () => {
4950
this.push(null);
51+
this.on('end', this.destroy);
5052
});
5153
callback(err);
5254
}

packages/csv-parse/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"preversion": "npm run build && git add dist",
105105
"pretest": "npm run build",
106106
"test": "mocha 'test/**/*.{coffee,ts}'",
107-
"test:legacy": "mocha --ignore test/api.web_stream.coffee --loader=./test/loaders/legacy/all.js 'test/**/*.{coffee,ts}'"
107+
"test:legacy": "mocha --ignore test/api.web_stream.coffee --ignore test/api.stream.finished.coffee --loader=./test/loaders/legacy/all.js 'test/**/*.{coffee,ts}'"
108108
},
109109
"type": "module",
110110
"types": "dist/esm/index.d.ts",

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

+26
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,29 @@ describe 'API events', ->
7979
parser.on 'error', (err) ->
8080
err.message.should.eql 'Invalid Record Length: expect 3, got 2 on line 2'
8181
next()
82+
83+
it 'emit `destroy` event', (next) ->
84+
parser = parse """
85+
a,a,a
86+
b,b,b
87+
c,c,c
88+
"""
89+
parser.on 'readable', (data) ->
90+
while this.read() isnt null then true
91+
parser.on 'close', next
92+
parser.on 'error', ->
93+
next Error 'Event `error` should not be fired'
94+
95+
it 'emit `destroy` event with `to_line` option', (next) ->
96+
# See https://github.com/adaltas/node-csv/issues/333
97+
parser = parse """
98+
a,a,a
99+
b,b,b
100+
c,c,c
101+
""", to_line: 2
102+
parser.on 'readable', (data) ->
103+
while this.read() isnt null then true
104+
parser.on 'close', next
105+
parser.on 'error', ->
106+
next Error 'Event `error` should not be fired'
107+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
import * as stream from 'node:stream/promises'
3+
import { Readable } from 'stream'
4+
import { generate } from 'csv-generate'
5+
import { parse } from '../lib/index.js'
6+
7+
describe 'API stream.finished', ->
8+
9+
it 'resolved at the end', ->
10+
# See https://github.com/adaltas/node-csv/issues/333
11+
records = []
12+
parser = generate(length: 10).pipe parse()
13+
parser.on 'readable', () =>
14+
while (record = parser.read()) isnt null
15+
records.push record
16+
await stream.finished parser
17+
records.length.should.eql 10
18+
19+
it 'resolved with `to_line`', ->
20+
# See https://github.com/adaltas/node-csv/issues/333
21+
records = []
22+
parser = generate(length: 10).pipe parse to_line: 3
23+
parser.on 'readable', () =>
24+
while (record = parser.read()) isnt null
25+
records.push record
26+
await stream.finished parser
27+
records.length.should.eql 3
28+
29+
it 'rejected on error', ->
30+
parser = parse to_line: 3
31+
parser.write 'a,b,c\n'
32+
parser.write 'd,e,f\n'
33+
parser.write 'h,i,j,ohno\n'
34+
parser.write 'k,l,m\n'
35+
parser.end()
36+
parser.on 'readable', () =>
37+
while (record = parser.read()) isnt null then true
38+
stream
39+
.finished parser
40+
.should.be.rejectedWith
41+
code: 'CSV_RECORD_INCONSISTENT_FIELDS_LENGTH'

0 commit comments

Comments
 (0)