Skip to content

Commit

Permalink
fix(csv-parse): destroy on end and call close event (fix #333)
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jan 31, 2023
1 parent 568364e commit 2e21ee4
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/csv-parse/dist/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,7 @@ class Parser extends stream.Transform {
this.push(record);
}, () => {
this.push(null);
this.on('end', this.destroy);
});
if(err !== undefined){
this.state.stop = true;
Expand All @@ -1337,6 +1338,7 @@ class Parser extends stream.Transform {
this.push(record);
}, () => {
this.push(null);
this.on('end', this.destroy);
});
callback(err);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/csv-parse/dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6380,6 +6380,7 @@ class Parser extends Transform {
this.push(record);
}, () => {
this.push(null);
this.on('end', this.destroy);
});
if(err !== undefined){
this.state.stop = true;
Expand All @@ -6395,6 +6396,7 @@ class Parser extends Transform {
this.push(record);
}, () => {
this.push(null);
this.on('end', this.destroy);
});
callback(err);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/csv-parse/dist/iife/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6383,6 +6383,7 @@ var csv_parse = (function (exports) {
this.push(record);
}, () => {
this.push(null);
this.on('end', this.destroy);
});
if(err !== undefined){
this.state.stop = true;
Expand All @@ -6398,6 +6399,7 @@ var csv_parse = (function (exports) {
this.push(record);
}, () => {
this.push(null);
this.on('end', this.destroy);
});
callback(err);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/csv-parse/dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6386,6 +6386,7 @@
this.push(record);
}, () => {
this.push(null);
this.on('end', this.destroy);
});
if(err !== undefined){
this.state.stop = true;
Expand All @@ -6401,6 +6402,7 @@
this.push(record);
}, () => {
this.push(null);
this.on('end', this.destroy);
});
callback(err);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/csv-parse/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Parser extends Transform {
this.push(record);
}, () => {
this.push(null);
this.on('end', this.destroy);
});
if(err !== undefined){
this.state.stop = true;
Expand All @@ -47,6 +48,7 @@ class Parser extends Transform {
this.push(record);
}, () => {
this.push(null);
this.on('end', this.destroy);
});
callback(err);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/csv-parse/test/api.stream.events.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,29 @@ describe 'API events', ->
parser.on 'error', (err) ->
err.message.should.eql 'Invalid Record Length: expect 3, got 2 on line 2'
next()

it 'emit `destroy` event', (next) ->
parser = parse """
a,a,a
b,b,b
c,c,c
"""
parser.on 'readable', (data) ->
while this.read() isnt null then true
parser.on 'close', next
parser.on 'error', ->
next Error 'Event `error` should not be fired'

it 'emit `destroy` event with `to_line` option', (next) ->
# See https://github.com/adaltas/node-csv/issues/333
parser = parse """
a,a,a
b,b,b
c,c,c
""", to_line: 2
parser.on 'readable', (data) ->
while this.read() isnt null then true
parser.on 'close', next
parser.on 'error', ->
next Error 'Event `error` should not be fired'

41 changes: 41 additions & 0 deletions packages/csv-parse/test/api.stream.finished.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import * as stream from 'node:stream/promises'
import { Readable } from 'stream'
import { generate } from 'csv-generate'
import { parse } from '../lib/index.js'

describe 'API stream.finished', ->

it 'resolved at the end', ->
# See https://github.com/adaltas/node-csv/issues/333
records = []
parser = generate(length: 10).pipe parse()
parser.on 'readable', () =>
while (record = parser.read()) isnt null
records.push record
await stream.finished parser
records.length.should.eql 10

it 'resolved with `to_line`', ->
# See https://github.com/adaltas/node-csv/issues/333
records = []
parser = generate(length: 10).pipe parse to_line: 3
parser.on 'readable', () =>
while (record = parser.read()) isnt null
records.push record
await stream.finished parser
records.length.should.eql 3

it 'rejected on error', ->
parser = parse to_line: 3
parser.write 'a,b,c\n'
parser.write 'd,e,f\n'
parser.write 'h,i,j,ohno\n'
parser.write 'k,l,m\n'
parser.end()
parser.on 'readable', () =>
while (record = parser.read()) isnt null then true
stream
.finished parser
.should.be.rejectedWith
code: 'CSV_RECORD_INCONSISTENT_FIELDS_LENGTH'

0 comments on commit 2e21ee4

Please sign in to comment.