Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a maximum bytes limit on a row #88

Merged
merged 2 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ Default: `0`
Specifies the number of lines at the beginning of a data file that the parser should
skip over, prior to parsing headers.

##### maxRowBytes

Type: `Number`<br>
Default: `Number.MAX_SAFE_INTEGER`

Maximum number of bytes per row. An error is thrown if a line exeeds this value. The default value is on 8 peta byte.

##### strict

Type: `Boolean`<br>
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const defaults = {
raw: false,
separator: ',',
skipLines: null,
maxRowBytes: Number.MAX_SAFE_INTEGER,
strict: false
}

Expand Down Expand Up @@ -52,6 +53,7 @@ class CsvParser extends Transform {
this._escaped = false
this._empty = this._raw ? bufferAlloc(0) : ''
this._Row = null
this._currentRowBytes = 0
this._line = 0

if (this.headers || this.headers === false) {
Expand Down Expand Up @@ -214,6 +216,11 @@ class CsvParser extends Transform {
const chr = buf[i]
const nextChr = i + 1 < bufLen ? buf[i + 1] : null

this._currentRowBytes++
if (this._currentRowBytes > this.maxRowBytes) {
return cb(new Error('Row exceeds the maximum size'))
}

if (!this._escaped && chr === this.escape && nextChr === this.quote && i !== start) {
this._escaped = true
continue
Expand Down Expand Up @@ -241,6 +248,7 @@ class CsvParser extends Transform {
if (chr === this.newline) {
this._online(buf, this._prevEnd, i + 1)
this._prevEnd = i + 1
this._currentRowBytes = 0
}
}
}
Expand Down
Loading