-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathfrom-stream.js
46 lines (39 loc) · 1.2 KB
/
from-stream.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
'use strict';
var flamegraph = require('./');
var through = require('through2');
var concatStream = require('concat-stream');
// Kept here and isn't referenced by index.js in order to keep bundle small when used in the browser
// assuming we won't need streaming in the browser anyways
module.exports =
/**
* Converts a stream of call graph lines into an svg document.
* Not truly streaming, concats all lines before processing.
*
* **Example**:
*
* ```js
* var fromStream = require('flamegraph/from-stream');
* fromStream(process.stdin, opts).pipe(process.stdout);
* ```
*
* @name flamegraph::fromStream
* @function
* @param {ReadableStream} stream that will emit the call graph lines to be parsed
* @param {Object} opts same as `flamegraph`
* @return {ReadableStream} stream that emits the lines of generated svg
*/
function fromStream(stream, opts) {
opts = opts || {};
var out = through();
function ondata(res) {
try {
var svg = flamegraph(res.toString().split('\n'), opts);
out.write(svg);
} catch (err) {
out.emit('error', err);
}
}
// Once memory becomes an issue we need to make this truly streaming
stream.pipe(concatStream(ondata));
return out;
}