csv-generate
Advanced tools
Comparing version 4.2.8 to 4.3.0
@@ -12,8 +12,12 @@ | ||
// Convert Stream Readable options if underscored | ||
if(opts.object_mode){ | ||
opts.objectMode = opts.object_mode; | ||
} | ||
if(opts.high_water_mark){ | ||
opts.highWaterMark = opts.high_water_mark; | ||
} | ||
if(opts.object_mode){ | ||
opts.objectMode = opts.object_mode; | ||
} | ||
// See https://nodejs.org/api/stream.html#stream_new_stream_readable_options | ||
// Node.js 20 introduced `stream.getDefaultHighWaterMark(opts.objectMode)` | ||
// opts.highWaterMark = opts.highWaterMark ?? (opts.objectMode ? 16 : 16384); | ||
// opts.highWaterMark = opts.highWaterMark ?? stream.getDefaultHighWaterMark(opts.objectMode); | ||
// Clone and camelize options | ||
@@ -64,2 +68,2 @@ const options = {}; | ||
export {normalize_options}; | ||
export { normalize_options }; |
const read = (options, state, size, push, close) => { | ||
// Already started | ||
const data = []; | ||
let length = state.fixed_size_buffer.length; | ||
if(length !== 0){ | ||
data.push(state.fixed_size_buffer); | ||
let recordsLength = 0; | ||
// Get remaining buffer when fixedSize is enable | ||
if (options.fixedSize) { | ||
recordsLength = state.fixed_size_buffer.length; | ||
if(recordsLength !== 0){ | ||
data.push(state.fixed_size_buffer); | ||
} | ||
} | ||
// eslint-disable-next-line | ||
while(true){ | ||
// Time for some rest: flush first and stop later | ||
if((state.count_created === options.length) || (options.end && Date.now() > options.end) || (options.duration && Date.now() > state.start_time + options.duration)){ | ||
// Exit | ||
if ( | ||
state.count_created === options.length || | ||
(options.end && Date.now() > options.end) || | ||
(options.duration && Date.now() > state.start_time + options.duration) | ||
) { | ||
// Flush | ||
if(data.length){ | ||
if(options.objectMode){ | ||
for(const record of data){ | ||
if (data.length) { | ||
if (options.objectMode) { | ||
for (const record of data) { | ||
push(record); | ||
} | ||
}else{ | ||
push(data.join('') + (options.eof ? options.eof : '')); | ||
} else { | ||
push(data.join("") + (options.eof ? options.eof : "")); | ||
} | ||
state.end = true; | ||
}else{ | ||
} else { | ||
close(); | ||
@@ -36,3 +43,3 @@ } | ||
if(result !== null && type !== 'string' && type !== 'number'){ | ||
return Error([ | ||
close(Error([ | ||
'INVALID_VALUE:', | ||
@@ -42,3 +49,4 @@ 'values returned by column function must be', | ||
`got ${JSON.stringify(result)}` | ||
].join(' ')); | ||
].join(' '))); | ||
return; | ||
} | ||
@@ -61,3 +69,3 @@ record.push(result); | ||
state.count_created++; | ||
if(length + recordLength > size){ | ||
if(recordsLength + recordLength > size){ | ||
if(options.objectMode){ | ||
@@ -70,4 +78,4 @@ data.push(record); | ||
if(options.fixedSize){ | ||
state.fixed_size_buffer = record.substr(size - length); | ||
data.push(record.substr(0, size - length)); | ||
state.fixed_size_buffer = record.substr(size - recordsLength); | ||
data.push(record.substr(0, size - recordsLength)); | ||
}else{ | ||
@@ -80,3 +88,3 @@ data.push(record); | ||
} | ||
length += recordLength; | ||
recordsLength += recordLength; | ||
data.push(record); | ||
@@ -83,0 +91,0 @@ } |
@@ -28,15 +28,19 @@ | ||
Generator.prototype._read = function(size){ | ||
const self = this; | ||
const err = read(this.options, this.state, size, function(chunk) { | ||
self.__push(chunk); | ||
}, function(){ | ||
self.push(null); | ||
setImmediate(() => { | ||
this.__read(size); | ||
}); | ||
if(err){ | ||
this.destroy(err); | ||
} | ||
}; | ||
Generator.prototype.__read = function(size){ | ||
read(this.options, this.state, size, (chunk) => { | ||
this.__push(chunk); | ||
}, (err) => { | ||
if(err){ | ||
this.destroy(err); | ||
}else{ | ||
this.push(null); | ||
} | ||
}); | ||
}; | ||
// Put new data into the read queue. | ||
Generator.prototype.__push = function(record){ | ||
// console.log('push', record) | ||
const push = () => { | ||
@@ -43,0 +47,0 @@ this.state.count_written++; |
@@ -25,4 +25,2 @@ | ||
let work = true; | ||
// See https://nodejs.org/api/stream.html#stream_new_stream_readable_options | ||
options.highWaterMark = options.objectMode ? 16 : 16384; | ||
const generator = new Generator(options); | ||
@@ -33,10 +31,6 @@ generator.push = function(chunk){ | ||
} | ||
if(options.objectMode){ | ||
chunks.push(chunk); | ||
}else{ | ||
chunks.push(chunk); | ||
} | ||
chunks.push(chunk); | ||
}; | ||
while(work){ | ||
generator._read(options.highWaterMark); | ||
generator.__read(options.highWaterMark); | ||
} | ||
@@ -43,0 +37,0 @@ if(!options.objectMode){ |
{ | ||
"version": "4.2.8", | ||
"version": "4.3.0", | ||
"name": "csv-generate", | ||
@@ -126,3 +126,3 @@ "description": "CSV and object generation implementing the Node.js `stream.Readable` API", | ||
}, | ||
"gitHead": "c3347b15c75224255c839b3d8a406f74efd90123" | ||
"gitHead": "632c10eb8a9a27749b58831fe6c4ead43ad63354" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1200446
29457