Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
stream: handle empty input streams
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Dec 6, 2018
1 parent 40b0bf0 commit eb295df
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ New features:

Fix:

* stream: handle empty input streams
* cast: function may return non-string values
* stream: pass stream options without modification

Expand Down
9 changes: 7 additions & 2 deletions lib/es5/index.js
Expand Up @@ -316,8 +316,13 @@ function (_Transform) {
trimChars = _this$state.trimChars;
var buf;

if (previousBuf === undefined && nextBuf !== undefined) {
buf = nextBuf;
if (previousBuf === undefined) {
if (nextBuf === undefined) {
this.push(null);
return;
} else {
buf = nextBuf;
}
} else if (previousBuf !== undefined && nextBuf === undefined) {
buf = previousBuf;
} else {
Expand Down
9 changes: 7 additions & 2 deletions lib/index.js
Expand Up @@ -210,8 +210,13 @@ class Parser extends Transform {
let {record_delimiter} = this.options
const {previousBuf, rawBuffer, escapeIsQuote, trimChars} = this.state
let buf
if(previousBuf === undefined && nextBuf !== undefined){
buf = nextBuf
if(previousBuf === undefined){
if(nextBuf === undefined){
this.push(null)
return
}else{
buf = nextBuf
}
}else if(previousBuf !== undefined && nextBuf === undefined){
buf = previousBuf
}else{
Expand Down
11 changes: 11 additions & 0 deletions test/api.pipe.coffee
@@ -1,5 +1,6 @@

fs = require 'fs'
{ Readable } = require 'stream'
generate = require 'csv-generate'
parse = require '../lib'

Expand Down Expand Up @@ -45,4 +46,14 @@ describe 'API pipe', ->
next()
rs.pipe(parser)

it 'handle empty string', (next) ->
s = new Readable()
s._read = ->
@push null
s.pipe parse
delimiter: ','
, (err, records) ->
records.should.eql [] unless err
next err


0 comments on commit eb295df

Please sign in to comment.