-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (32 loc) · 1.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
let uuid = require('uuid');
let stream = require('stream');
let util = require('util');
let OUTPUT_LOCATION = uuid.v4();
function JsonInputStream(data) {
stream.Transform.call(this, {objectMode: true});
if (typeof data !== 'object') {
throw new Error('Invalid JSON object given.');
}
let stringObject = JSON.stringify(data);
if (stringObject.indexOf(OUTPUT_LOCATION) === -1) {
throw new Error('No output location has been found in given object.');
}
this.objectParts = stringObject.split(OUTPUT_LOCATION);
this.push(this.objectParts[0]);
}
util.inherits(JsonInputStream, stream.Transform);
JsonInputStream.prototype._transform = function (row, enc, callback) {
let data = JSON.stringify(row, null);
data = data.slice(1, data.length - 1);
this.push(data);
callback();
};
JsonInputStream.prototype._flush = function (callback) {
this.push(this.objectParts[1]);
this.objectParts = null;
callback();
};
module.exports = function (object) {
return new JsonInputStream(object);
};
module.exports.OUTPUT_LOCATION = OUTPUT_LOCATION;