@@ -26,20 +26,18 @@ async function errHelper(text) {
26
26
await this . emit ( 'end' )
27
27
}
28
28
29
- function sliceIntoChunks ( arr , chunkSize ) {
30
- const res = [ ] ;
31
- for ( let i = 0 ; i < arr . length ; i += chunkSize ) {
32
- const chunk = arr . slice ( i , i + chunkSize ) ;
33
- res . push ( chunk ) ;
34
- }
35
- return res ;
36
- }
37
-
38
29
async function readCSV ( msg , cfg ) {
39
30
const that = this
40
31
const emitBehavior = cfg . emitAll ;
41
32
const { body } = msg ;
42
33
34
+ let batchSize ;
35
+ if ( emitBehavior === 'emitBatch' ) {
36
+ batchSize = body . batchSize ;
37
+ if ( ! isPositiveInteger ( batchSize ) ) {
38
+ throw new Error ( "'batchSize' must be a positive integer!" ) ;
39
+ }
40
+ }
43
41
// check if url provided in msg
44
42
if ( body . url && body . url . length > 0 ) {
45
43
this . logger . info ( 'URL found' )
@@ -83,7 +81,8 @@ async function readCSV(msg, cfg) {
83
81
this . emit ( 'end' )
84
82
return
85
83
}
86
- // control of node data stream
84
+
85
+ const buf = [ ] ;
87
86
class CsvWriter extends Writable {
88
87
async write ( chunk ) {
89
88
let data = { }
@@ -92,9 +91,14 @@ async function readCSV(msg, cfg) {
92
91
} else {
93
92
data = arrayToObj ( chunk )
94
93
}
95
- if ( emitBehavior === 'emitIndividually' || cfg . emitAll === false || cfg . emitAll === 'false' ) {
94
+ if ( emitBehavior === 'emitIndividually' || cfg . emitAll === false || cfg . emitAll === 'false' || emitBehavior === 'emitBatch' ) {
96
95
parseStream . pause ( )
97
- await that . emit ( 'data' , messages . newMessageWithBody ( data ) )
96
+ if ( emitBehavior === 'emitBatch' ) {
97
+ buf . push ( data ) ;
98
+ if ( buf . length >= batchSize ) await that . emit ( 'data' , messages . newMessageWithBody ( { result : buf . splice ( 0 , batchSize ) } ) )
99
+ } else {
100
+ await that . emit ( 'data' , messages . newMessageWithBody ( data ) )
101
+ }
98
102
parseStream . resume ( )
99
103
} else {
100
104
result . push ( data )
@@ -120,17 +124,8 @@ async function readCSV(msg, cfg) {
120
124
121
125
if ( emitBehavior === 'fetchAll' || cfg . emitAll === true || cfg . emitAll === 'true' ) {
122
126
await this . emit ( 'data' , messages . newMessageWithBody ( { result } ) )
123
- } else if ( emitBehavior === 'emitBatch' ) {
124
- const { batchSize } = body ;
125
- if ( ! isPositiveInteger ( batchSize ) ) {
126
- throw new Error ( "'batchSize' must be a positive integer!" ) ;
127
- }
128
- const chunks = sliceIntoChunks ( result , batchSize ) ;
129
- // eslint-disable-next-line no-plusplus
130
- for ( let i = 0 ; i < chunks . length ; i ++ ) {
131
- // eslint-disable-next-line no-await-in-loop
132
- await this . emit ( 'data' , messages . newMessageWithBody ( { result : chunks [ i ] } ) )
133
- }
127
+ } else if ( emitBehavior === 'emitBatch' && buf . length > 0 ) {
128
+ await that . emit ( 'data' , messages . newMessageWithBody ( { result : buf } ) )
134
129
}
135
130
this . logger . info ( `Complete, memory used: ${ process . memoryUsage ( ) . heapUsed / 1024 / 1024 } Mb` )
136
131
}
0 commit comments