Repository navigation
Expand file tree
/
Copy pathannotationExtractor.js
More file actions
48 lines (44 loc) · 1.63 KB
/
Copy pathannotationExtractor.js
File metadata and controls
48 lines (44 loc) · 1.63 KB
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
const { Transform } = require('stream');
const zlib = require('zlib');
function createTransform(isIncludeEmptyData) {
const transform = new Transform( { objectMode: true } );
transform._transform = function(chunk, encoding, done) {
const packet = chunk;
if (packet['payload_unit_start_indicator']) {
this._length = packet['payload'].readUInt32BE(0);
this._buffer = packet['payload'].slice(4);
} else {
this._buffer = this._buffer ? Buffer.concat([this._buffer, packet['payload']]) : packet['payload'];
}
if (this._length > 0 && this._length == this._buffer.length) {
const buffer = this._buffer;
this._length = null;
this._buffer = null;
zlib.inflateRaw(buffer, (error, result) => {
if (error) {
console.error(error);
done();
return;
}
try {
const dataString = result.toString('utf-8');
const data = JSON.parse(dataString);
this.push(data);
} catch {
console.error("Failed to parse data");
}
done();
});
} else if (isIncludeEmptyData && packet['payload_unit_start_indicator'] && this._length === 0) {
this.push({});
done();
} else {
done();
}
};
transform._flush = function(done) {
done();
};
return transform;
}
module.exports = createTransform;