adaltas / node-csv

Full featured CSV parser with simple api and tested against large datasets.
https://csv.js.org
MIT License
3.98k stars 263 forks source link

csv-parse cross pollinates configuration between instances #399

Closed lgandras closed 1 year ago

lgandras commented 1 year ago

Describe the bug

Creating two instances of csv-parse without consuming the first one affects the second one. It seems like csv-parse holds global state.

To Reproduce

test.js:

const fs = require('fs');
const { parse: parseCsv } = require("csv-parse");

async function main() {
    //const parser = fs.createReadStream('test.csv').pipe(parseCsv());
    const parser2 = fs.createReadStream('test.csv').pipe(parseCsv({relax_quotes:true,from_line:2}));
    for await (record of parser2) {
        console.log(record);
    }
}
main();

test.csv:

""
value

Command:

node test.js

Output:

[ 'value' ]

(As expected) If you uncomment the first line and reexecute, you'll; get:

node:events:492
      throw er; // Unhandled 'error' event
      ^

CsvError: Invalid Opening Quote: a quote is found on field 0 at line 1, value is "" (utf8 bom)
    at Object.parse (/home/Projects/test/node_modules/csv-parse/dist/cjs/index.cjs:783:21)
    at Parser._transform (/home/Projects/test/node_modules/csv-parse/dist/cjs/index.cjs:1323:26)
    at Transform._write (node:internal/streams/transform:175:8)
    at writeOrBuffer (node:internal/streams/writable:399:12)
    at _write (node:internal/streams/writable:340:10)
    at Writable.write (node:internal/streams/writable:344:10)
    at ReadStream.ondata (node:internal/streams/readable:785:22)
    at ReadStream.emit (node:events:514:28)
    at addChunk (node:internal/streams/readable:343:12)
    at readableAddChunk (node:internal/streams/readable:316:9)
Emitted 'error' event on Parser instance at:
    at Parser.onerror (node:internal/streams/readable:804:14)
    at Parser.emit (node:events:514:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'INVALID_OPENING_QUOTE',
  bytes: 0,
  comment_lines: 0,
  empty_lines: 0,
  invalid_field_length: 0,
  lines: 1,
  records: 0,
  columns: false,
  error: undefined,
  header: false,
  index: 0,
  raw: undefined,
  column: 0,
  quoting: false,
  field: ''
}

Node.js v20.5.1
wdavidw commented 1 year ago

I tried to replicate your issue with much success. The sample executes as expected:

cd node-csv
git pull
cd demo/issues-cjs
node lib/399.js

Honestly, I doubt there could be any global state sharing. No single object is defined globally and we don't use any dependency beside Node.js.

wdavidw commented 1 year ago

Closing, feel free to re-open.