Skip to content

Commit

Permalink
fix: uncaught errors with large stream chunks (fix #386)
Browse files Browse the repository at this point in the history
  • Loading branch information
haggholm authored and wdavidw committed Apr 16, 2023
1 parent db7cba9 commit 1d500ed
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 46 deletions.
10 changes: 9 additions & 1 deletion packages/csv-stringify/dist/cjs/index.cjs
Expand Up @@ -627,7 +627,15 @@ const stringify = function(){
callback(err);
});
stringifier.on('end', function(){
callback(undefined, chunks.join(''));
try {
callback(undefined, chunks.join(''));
} catch (err) {
// This can happen if the `chunks` is extremely long; it may throw
// "Cannot create a string longer than 0x1fffffe8 characters"
// See [#386](https://github.com/adaltas/node-csv/pull/386)
callback(err);
return;
}
});
}
if(data !== undefined){
Expand Down
10 changes: 9 additions & 1 deletion packages/csv-stringify/dist/esm/index.js
Expand Up @@ -5685,7 +5685,15 @@ const stringify = function(){
callback(err);
});
stringifier.on('end', function(){
callback(undefined, chunks.join(''));
try {
callback(undefined, chunks.join(''));
} catch (err) {
// This can happen if the `chunks` is extremely long; it may throw
// "Cannot create a string longer than 0x1fffffe8 characters"
// See [#386](https://github.com/adaltas/node-csv/pull/386)
callback(err);
return;
}
});
}
if(data !== undefined){
Expand Down
10 changes: 9 additions & 1 deletion packages/csv-stringify/dist/iife/index.js
Expand Up @@ -5688,7 +5688,15 @@ var csv_stringify = (function (exports) {
callback(err);
});
stringifier.on('end', function(){
callback(undefined, chunks.join(''));
try {
callback(undefined, chunks.join(''));
} catch (err) {
// This can happen if the `chunks` is extremely long; it may throw
// "Cannot create a string longer than 0x1fffffe8 characters"
// See [#386](https://github.com/adaltas/node-csv/pull/386)
callback(err);
return;
}
});
}
if(data !== undefined){
Expand Down
10 changes: 9 additions & 1 deletion packages/csv-stringify/dist/umd/index.js
Expand Up @@ -5691,7 +5691,15 @@
callback(err);
});
stringifier.on('end', function(){
callback(undefined, chunks.join(''));
try {
callback(undefined, chunks.join(''));
} catch (err) {
// This can happen if the `chunks` is extremely long; it may throw
// "Cannot create a string longer than 0x1fffffe8 characters"
// See [#386](https://github.com/adaltas/node-csv/pull/386)
callback(err);
return;
}
});
}
if(data !== undefined){
Expand Down
10 changes: 9 additions & 1 deletion packages/csv-stringify/lib/index.js
Expand Up @@ -88,7 +88,15 @@ const stringify = function(){
callback(err);
});
stringifier.on('end', function(){
callback(undefined, chunks.join(''));
try {
callback(undefined, chunks.join(''));
} catch (err) {
// This can happen if the `chunks` is extremely long; it may throw
// "Cannot create a string longer than 0x1fffffe8 characters"
// See [#386](https://github.com/adaltas/node-csv/pull/386)
callback(err);
return;
}
});
}
if(data !== undefined){
Expand Down
43 changes: 43 additions & 0 deletions packages/csv-stringify/test/api.callback.coffee
@@ -0,0 +1,43 @@

import fs from 'fs'
import { generate } from 'csv-generate'
import { stringify } from '../lib/index.js'

describe 'api.callback', ->

it '2 args: data, callback', (next) ->
data = ''
stringifier = stringify [
['field_1','field_2'], ['value 1','value 2']
], (err, data) ->
data.should.eql 'field_1,field_2\nvalue 1,value 2\n'
next()

it '2 args: options, callback', (next) ->
data = ''
stringifier = stringify eof: false, (err, data) ->
data.should.eql 'field_1,field_2\nvalue 1,value 2'
next()
stringifier.write ['field_1','field_2']
stringifier.write ['value 1','value 2']
stringifier.end()

it '3 args: data, options, callback', (next) ->
data = ''
stringifier = stringify [
['field_1','field_2'], ['value 1','value 2']
], eof: false, (err, data) ->
data.should.eql 'field_1,field_2\nvalue 1,value 2'
next()

it 'catch error in end handler, see #386', (next) ->
input =
Array.from( length: 200000 ).map ->
Array.from( length: 100 ).map ->
'ABCDEFGHIJKLMNOPQRSTUVXYZ0123456789'
stringify input, (err, res) ->
err.should.match
code: 'ERR_STRING_TOO_LONG'
message: 'Cannot create a string longer than 0x1fffffe8 characters'
next()

33 changes: 4 additions & 29 deletions packages/csv-stringify/test/api.coffee
Expand Up @@ -5,7 +5,7 @@ import { stringify } from '../lib/index.js'

describe 'API', ->

it '0 arg', (next) ->
it '0 arg: write input and stream output', (next) ->
data = ''
stringifier = stringify()
stringifier.on 'readable', ->
Expand All @@ -19,7 +19,7 @@ describe 'API', ->
stringifier.write ['value 1','value 2']
stringifier.end()

it '1 arg: option; write and data using the stream API', (next) ->
it '1 arg: option; write input and stream output', (next) ->
data = ''
generator = generate length: 2, objectMode: true, seed: 1, columns: 2
stringifier = stringify eof: false
Expand All @@ -37,7 +37,7 @@ describe 'API', ->
"""
next()

it '1 arg: data and pipe result', (next) ->
it '1 arg: data and stream output', (next) ->
data = ''
stringifier = stringify [
['field_1','field_2'], ['value 1','value 2']
Expand All @@ -48,7 +48,7 @@ describe 'API', ->
data.should.eql 'field_1,field_2\nvalue 1,value 2\n'
next()

it '2 args: data, option and pipe result', (next) ->
it '2 args: data, option and stream output', (next) ->
data = ''
stringifier = stringify [
['field_1','field_2'], ['value 1','value 2']
Expand All @@ -58,28 +58,3 @@ describe 'API', ->
stringifier.on 'finish', ->
data.should.eql 'field_1,field_2\nvalue 1,value 2'
next()

it '2 args: data, callback', (next) ->
data = ''
stringifier = stringify [
['field_1','field_2'], ['value 1','value 2']
], (err, data) ->
data.should.eql 'field_1,field_2\nvalue 1,value 2\n'
next()

it '2 args: options, callback', (next) ->
data = ''
stringifier = stringify eof: false, (err, data) ->
data.should.eql 'field_1,field_2\nvalue 1,value 2'
next()
stringifier.write ['field_1','field_2']
stringifier.write ['value 1','value 2']
stringifier.end()

it '3 args: data, options, callback', (next) ->
data = ''
stringifier = stringify [
['field_1','field_2'], ['value 1','value 2']
], eof: false, (err, data) ->
data.should.eql 'field_1,field_2\nvalue 1,value 2'
next()
12 changes: 10 additions & 2 deletions packages/csv/dist/cjs/index.cjs
Expand Up @@ -267,7 +267,7 @@ let CsvError$1 = class CsvError extends Error {
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError$1);
Error.captureStackTrace(this, CsvError);
}
this.code = code;
for(const context of contexts){
Expand Down Expand Up @@ -2423,7 +2423,15 @@ const stringify = function(){
callback(err);
});
stringifier.on('end', function(){
callback(undefined, chunks.join(''));
try {
callback(undefined, chunks.join(''));
} catch (err) {
// This can happen if the `chunks` is extremely long; it may throw
// "Cannot create a string longer than 0x1fffffe8 characters"
// See [#386](https://github.com/adaltas/node-csv/pull/386)
callback(err);
return;
}
});
}
if(data !== undefined){
Expand Down
2 changes: 1 addition & 1 deletion packages/csv/dist/cjs/sync.cjs
Expand Up @@ -260,7 +260,7 @@ let CsvError$1 = class CsvError extends Error {
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError$1);
Error.captureStackTrace(this, CsvError);
}
this.code = code;
for(const context of contexts){
Expand Down
12 changes: 10 additions & 2 deletions packages/csv/dist/esm/index.js
Expand Up @@ -5394,7 +5394,7 @@ let CsvError$1 = class CsvError extends Error {
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError$1);
Error.captureStackTrace(this, CsvError);
}
this.code = code;
for(const context of contexts){
Expand Down Expand Up @@ -7550,7 +7550,15 @@ const stringify = function(){
callback(err);
});
stringifier.on('end', function(){
callback(undefined, chunks.join(''));
try {
callback(undefined, chunks.join(''));
} catch (err) {
// This can happen if the `chunks` is extremely long; it may throw
// "Cannot create a string longer than 0x1fffffe8 characters"
// See [#386](https://github.com/adaltas/node-csv/pull/386)
callback(err);
return;
}
});
}
if(data !== undefined){
Expand Down
2 changes: 1 addition & 1 deletion packages/csv/dist/esm/sync.js
Expand Up @@ -5387,7 +5387,7 @@ let CsvError$1 = class CsvError extends Error {
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError$1);
Error.captureStackTrace(this, CsvError);
}
this.code = code;
for(const context of contexts){
Expand Down
12 changes: 10 additions & 2 deletions packages/csv/dist/iife/index.js
Expand Up @@ -5397,7 +5397,7 @@ var csv = (function (exports) {
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError$1);
Error.captureStackTrace(this, CsvError);
}
this.code = code;
for(const context of contexts){
Expand Down Expand Up @@ -7553,7 +7553,15 @@ var csv = (function (exports) {
callback(err);
});
stringifier.on('end', function(){
callback(undefined, chunks.join(''));
try {
callback(undefined, chunks.join(''));
} catch (err) {
// This can happen if the `chunks` is extremely long; it may throw
// "Cannot create a string longer than 0x1fffffe8 characters"
// See [#386](https://github.com/adaltas/node-csv/pull/386)
callback(err);
return;
}
});
}
if(data !== undefined){
Expand Down
2 changes: 1 addition & 1 deletion packages/csv/dist/iife/sync.js
Expand Up @@ -5390,7 +5390,7 @@ var csv_sync = (function (exports) {
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError$1);
Error.captureStackTrace(this, CsvError);
}
this.code = code;
for(const context of contexts){
Expand Down
12 changes: 10 additions & 2 deletions packages/csv/dist/umd/index.js
Expand Up @@ -5400,7 +5400,7 @@
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError$1);
Error.captureStackTrace(this, CsvError);
}
this.code = code;
for(const context of contexts){
Expand Down Expand Up @@ -7556,7 +7556,15 @@
callback(err);
});
stringifier.on('end', function(){
callback(undefined, chunks.join(''));
try {
callback(undefined, chunks.join(''));
} catch (err) {
// This can happen if the `chunks` is extremely long; it may throw
// "Cannot create a string longer than 0x1fffffe8 characters"
// See [#386](https://github.com/adaltas/node-csv/pull/386)
callback(err);
return;
}
});
}
if(data !== undefined){
Expand Down
2 changes: 1 addition & 1 deletion packages/csv/dist/umd/sync.js
Expand Up @@ -5393,7 +5393,7 @@
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError$1);
Error.captureStackTrace(this, CsvError);
}
this.code = code;
for(const context of contexts){
Expand Down

0 comments on commit 1d500ed

Please sign in to comment.