| 5 | // that would not normal fit into memory |
| 6 | // or V8 string size limits |
| 7 | export function streamToJson ( dataArray, filePath ) { |
| 8 | return new Promise( resolve => { |
| 9 | const output = fs.createWriteStream( filePath, 'utf8' ) |
| 10 | |
| 11 | // When the stream is finished |
| 12 | output.on( 'finish', () => { |
| 13 | resolve( output ) |
| 14 | }) |
| 15 | |
| 16 | // Write opening array bracket |
| 17 | output.write( '[' ) |
| 18 | |
| 19 | // Write each item in the array |
| 20 | for ( const item of dataArray ) { |
| 21 | output.write( JSON.stringify( item ) + ',' ) |
| 22 | } |
| 23 | |
| 24 | // Write closing array bracket |
| 25 | output.write( ']' ) |
| 26 | |
| 27 | // Close the stream |
| 28 | output.end() |
| 29 | |
| 30 | // Return the stream |
| 31 | return output |
| 32 | }) |
| 33 | } |