UNPKG

746 BJavaScriptView Raw
1var Transform = require('stream').Transform;
2var BSON = require('bson');
3var bson = new BSON();
4
5module.exports = function rawTransform(raw) {
6 // split up incoming raw bytes and return documents one by one; NODE-1906
7 // this is only necessary for native sampler
8 function transform(chunk, encoding, cb) {
9 // only transform for raw bytes
10 if (raw) {
11 var response = bson.deserialize(chunk);
12 response.cursor.firstBatch.forEach(function(doc) {
13 this.push(bson.serialize(doc));
14 }.bind(this));
15 return cb();
16 }
17 // otherwise go back to the main stream
18 return cb(null, chunk);
19 }
20
21 return new Transform({
22 readableObjectMode: true,
23 writableObjectMode: true,
24 transform: transform
25 });
26};