Skip to content

Commit 1d500ed

Browse files
haggholmwdavidw
authored andcommittedApr 16, 2023
fix: uncaught errors with large stream chunks (fix #386)
1 parent db7cba9 commit 1d500ed

File tree

15 files changed

+136
-46
lines changed

15 files changed

+136
-46
lines changed
 

‎packages/csv-stringify/dist/cjs/index.cjs

+9-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,15 @@ const stringify = function(){
627627
callback(err);
628628
});
629629
stringifier.on('end', function(){
630-
callback(undefined, chunks.join(''));
630+
try {
631+
callback(undefined, chunks.join(''));
632+
} catch (err) {
633+
// This can happen if the `chunks` is extremely long; it may throw
634+
// "Cannot create a string longer than 0x1fffffe8 characters"
635+
// See [#386](https://github.com/adaltas/node-csv/pull/386)
636+
callback(err);
637+
return;
638+
}
631639
});
632640
}
633641
if(data !== undefined){

‎packages/csv-stringify/dist/esm/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -5685,7 +5685,15 @@ const stringify = function(){
56855685
callback(err);
56865686
});
56875687
stringifier.on('end', function(){
5688-
callback(undefined, chunks.join(''));
5688+
try {
5689+
callback(undefined, chunks.join(''));
5690+
} catch (err) {
5691+
// This can happen if the `chunks` is extremely long; it may throw
5692+
// "Cannot create a string longer than 0x1fffffe8 characters"
5693+
// See [#386](https://github.com/adaltas/node-csv/pull/386)
5694+
callback(err);
5695+
return;
5696+
}
56895697
});
56905698
}
56915699
if(data !== undefined){

‎packages/csv-stringify/dist/iife/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -5688,7 +5688,15 @@ var csv_stringify = (function (exports) {
56885688
callback(err);
56895689
});
56905690
stringifier.on('end', function(){
5691-
callback(undefined, chunks.join(''));
5691+
try {
5692+
callback(undefined, chunks.join(''));
5693+
} catch (err) {
5694+
// This can happen if the `chunks` is extremely long; it may throw
5695+
// "Cannot create a string longer than 0x1fffffe8 characters"
5696+
// See [#386](https://github.com/adaltas/node-csv/pull/386)
5697+
callback(err);
5698+
return;
5699+
}
56925700
});
56935701
}
56945702
if(data !== undefined){

‎packages/csv-stringify/dist/umd/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -5691,7 +5691,15 @@
56915691
callback(err);
56925692
});
56935693
stringifier.on('end', function(){
5694-
callback(undefined, chunks.join(''));
5694+
try {
5695+
callback(undefined, chunks.join(''));
5696+
} catch (err) {
5697+
// This can happen if the `chunks` is extremely long; it may throw
5698+
// "Cannot create a string longer than 0x1fffffe8 characters"
5699+
// See [#386](https://github.com/adaltas/node-csv/pull/386)
5700+
callback(err);
5701+
return;
5702+
}
56955703
});
56965704
}
56975705
if(data !== undefined){

‎packages/csv-stringify/lib/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,15 @@ const stringify = function(){
8888
callback(err);
8989
});
9090
stringifier.on('end', function(){
91-
callback(undefined, chunks.join(''));
91+
try {
92+
callback(undefined, chunks.join(''));
93+
} catch (err) {
94+
// This can happen if the `chunks` is extremely long; it may throw
95+
// "Cannot create a string longer than 0x1fffffe8 characters"
96+
// See [#386](https://github.com/adaltas/node-csv/pull/386)
97+
callback(err);
98+
return;
99+
}
92100
});
93101
}
94102
if(data !== undefined){
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
import fs from 'fs'
3+
import { generate } from 'csv-generate'
4+
import { stringify } from '../lib/index.js'
5+
6+
describe 'api.callback', ->
7+
8+
it '2 args: data, callback', (next) ->
9+
data = ''
10+
stringifier = stringify [
11+
['field_1','field_2'], ['value 1','value 2']
12+
], (err, data) ->
13+
data.should.eql 'field_1,field_2\nvalue 1,value 2\n'
14+
next()
15+
16+
it '2 args: options, callback', (next) ->
17+
data = ''
18+
stringifier = stringify eof: false, (err, data) ->
19+
data.should.eql 'field_1,field_2\nvalue 1,value 2'
20+
next()
21+
stringifier.write ['field_1','field_2']
22+
stringifier.write ['value 1','value 2']
23+
stringifier.end()
24+
25+
it '3 args: data, options, callback', (next) ->
26+
data = ''
27+
stringifier = stringify [
28+
['field_1','field_2'], ['value 1','value 2']
29+
], eof: false, (err, data) ->
30+
data.should.eql 'field_1,field_2\nvalue 1,value 2'
31+
next()
32+
33+
it 'catch error in end handler, see #386', (next) ->
34+
input =
35+
Array.from( length: 200000 ).map ->
36+
Array.from( length: 100 ).map ->
37+
'ABCDEFGHIJKLMNOPQRSTUVXYZ0123456789'
38+
stringify input, (err, res) ->
39+
err.should.match
40+
code: 'ERR_STRING_TOO_LONG'
41+
message: 'Cannot create a string longer than 0x1fffffe8 characters'
42+
next()
43+

‎packages/csv-stringify/test/api.coffee

+4-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { stringify } from '../lib/index.js'
55

66
describe 'API', ->
77

8-
it '0 arg', (next) ->
8+
it '0 arg: write input and stream output', (next) ->
99
data = ''
1010
stringifier = stringify()
1111
stringifier.on 'readable', ->
@@ -19,7 +19,7 @@ describe 'API', ->
1919
stringifier.write ['value 1','value 2']
2020
stringifier.end()
2121

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

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

51-
it '2 args: data, option and pipe result', (next) ->
51+
it '2 args: data, option and stream output', (next) ->
5252
data = ''
5353
stringifier = stringify [
5454
['field_1','field_2'], ['value 1','value 2']
@@ -58,28 +58,3 @@ describe 'API', ->
5858
stringifier.on 'finish', ->
5959
data.should.eql 'field_1,field_2\nvalue 1,value 2'
6060
next()
61-
62-
it '2 args: data, callback', (next) ->
63-
data = ''
64-
stringifier = stringify [
65-
['field_1','field_2'], ['value 1','value 2']
66-
], (err, data) ->
67-
data.should.eql 'field_1,field_2\nvalue 1,value 2\n'
68-
next()
69-
70-
it '2 args: options, callback', (next) ->
71-
data = ''
72-
stringifier = stringify eof: false, (err, data) ->
73-
data.should.eql 'field_1,field_2\nvalue 1,value 2'
74-
next()
75-
stringifier.write ['field_1','field_2']
76-
stringifier.write ['value 1','value 2']
77-
stringifier.end()
78-
79-
it '3 args: data, options, callback', (next) ->
80-
data = ''
81-
stringifier = stringify [
82-
['field_1','field_2'], ['value 1','value 2']
83-
], eof: false, (err, data) ->
84-
data.should.eql 'field_1,field_2\nvalue 1,value 2'
85-
next()

‎packages/csv/dist/cjs/index.cjs

+10-2
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ let CsvError$1 = class CsvError extends Error {
267267
if(Array.isArray(message)) message = message.join(' ').trim();
268268
super(message);
269269
if(Error.captureStackTrace !== undefined){
270-
Error.captureStackTrace(this, CsvError$1);
270+
Error.captureStackTrace(this, CsvError);
271271
}
272272
this.code = code;
273273
for(const context of contexts){
@@ -2423,7 +2423,15 @@ const stringify = function(){
24232423
callback(err);
24242424
});
24252425
stringifier.on('end', function(){
2426-
callback(undefined, chunks.join(''));
2426+
try {
2427+
callback(undefined, chunks.join(''));
2428+
} catch (err) {
2429+
// This can happen if the `chunks` is extremely long; it may throw
2430+
// "Cannot create a string longer than 0x1fffffe8 characters"
2431+
// See [#386](https://github.com/adaltas/node-csv/pull/386)
2432+
callback(err);
2433+
return;
2434+
}
24272435
});
24282436
}
24292437
if(data !== undefined){

‎packages/csv/dist/cjs/sync.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ let CsvError$1 = class CsvError extends Error {
260260
if(Array.isArray(message)) message = message.join(' ').trim();
261261
super(message);
262262
if(Error.captureStackTrace !== undefined){
263-
Error.captureStackTrace(this, CsvError$1);
263+
Error.captureStackTrace(this, CsvError);
264264
}
265265
this.code = code;
266266
for(const context of contexts){

‎packages/csv/dist/esm/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -5394,7 +5394,7 @@ let CsvError$1 = class CsvError extends Error {
53945394
if(Array.isArray(message)) message = message.join(' ').trim();
53955395
super(message);
53965396
if(Error.captureStackTrace !== undefined){
5397-
Error.captureStackTrace(this, CsvError$1);
5397+
Error.captureStackTrace(this, CsvError);
53985398
}
53995399
this.code = code;
54005400
for(const context of contexts){
@@ -7550,7 +7550,15 @@ const stringify = function(){
75507550
callback(err);
75517551
});
75527552
stringifier.on('end', function(){
7553-
callback(undefined, chunks.join(''));
7553+
try {
7554+
callback(undefined, chunks.join(''));
7555+
} catch (err) {
7556+
// This can happen if the `chunks` is extremely long; it may throw
7557+
// "Cannot create a string longer than 0x1fffffe8 characters"
7558+
// See [#386](https://github.com/adaltas/node-csv/pull/386)
7559+
callback(err);
7560+
return;
7561+
}
75547562
});
75557563
}
75567564
if(data !== undefined){

‎packages/csv/dist/esm/sync.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5387,7 +5387,7 @@ let CsvError$1 = class CsvError extends Error {
53875387
if(Array.isArray(message)) message = message.join(' ').trim();
53885388
super(message);
53895389
if(Error.captureStackTrace !== undefined){
5390-
Error.captureStackTrace(this, CsvError$1);
5390+
Error.captureStackTrace(this, CsvError);
53915391
}
53925392
this.code = code;
53935393
for(const context of contexts){

‎packages/csv/dist/iife/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -5397,7 +5397,7 @@ var csv = (function (exports) {
53975397
if(Array.isArray(message)) message = message.join(' ').trim();
53985398
super(message);
53995399
if(Error.captureStackTrace !== undefined){
5400-
Error.captureStackTrace(this, CsvError$1);
5400+
Error.captureStackTrace(this, CsvError);
54015401
}
54025402
this.code = code;
54035403
for(const context of contexts){
@@ -7553,7 +7553,15 @@ var csv = (function (exports) {
75537553
callback(err);
75547554
});
75557555
stringifier.on('end', function(){
7556-
callback(undefined, chunks.join(''));
7556+
try {
7557+
callback(undefined, chunks.join(''));
7558+
} catch (err) {
7559+
// This can happen if the `chunks` is extremely long; it may throw
7560+
// "Cannot create a string longer than 0x1fffffe8 characters"
7561+
// See [#386](https://github.com/adaltas/node-csv/pull/386)
7562+
callback(err);
7563+
return;
7564+
}
75577565
});
75587566
}
75597567
if(data !== undefined){

‎packages/csv/dist/iife/sync.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5390,7 +5390,7 @@ var csv_sync = (function (exports) {
53905390
if(Array.isArray(message)) message = message.join(' ').trim();
53915391
super(message);
53925392
if(Error.captureStackTrace !== undefined){
5393-
Error.captureStackTrace(this, CsvError$1);
5393+
Error.captureStackTrace(this, CsvError);
53945394
}
53955395
this.code = code;
53965396
for(const context of contexts){

‎packages/csv/dist/umd/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -5400,7 +5400,7 @@
54005400
if(Array.isArray(message)) message = message.join(' ').trim();
54015401
super(message);
54025402
if(Error.captureStackTrace !== undefined){
5403-
Error.captureStackTrace(this, CsvError$1);
5403+
Error.captureStackTrace(this, CsvError);
54045404
}
54055405
this.code = code;
54065406
for(const context of contexts){
@@ -7556,7 +7556,15 @@
75567556
callback(err);
75577557
});
75587558
stringifier.on('end', function(){
7559-
callback(undefined, chunks.join(''));
7559+
try {
7560+
callback(undefined, chunks.join(''));
7561+
} catch (err) {
7562+
// This can happen if the `chunks` is extremely long; it may throw
7563+
// "Cannot create a string longer than 0x1fffffe8 characters"
7564+
// See [#386](https://github.com/adaltas/node-csv/pull/386)
7565+
callback(err);
7566+
return;
7567+
}
75607568
});
75617569
}
75627570
if(data !== undefined){

‎packages/csv/dist/umd/sync.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5393,7 +5393,7 @@
53935393
if(Array.isArray(message)) message = message.join(' ').trim();
53945394
super(message);
53955395
if(Error.captureStackTrace !== undefined){
5396-
Error.captureStackTrace(this, CsvError$1);
5396+
Error.captureStackTrace(this, CsvError);
53975397
}
53985398
this.code = code;
53995399
for(const context of contexts){

0 commit comments

Comments
 (0)
Please sign in to comment.