snapshot: adding snapshot.toJSON convenience method#55
snapshot: adding snapshot.toJSON convenience method#55thlorenz wants to merge 1 commit intonode-inspector:masterfrom
Conversation
|
BTW if you want I can also supply a |
|
Hello, @thlorenz , sorry for pending this pr. I have a lot of work on node-inspector. I see a problems with using reserved method I'd like to see also stream feature. How about a method that can work with callback and stream? var snapshot = profiler.takeSnapshot();
snapshot.export(function(err, result) {});
snapshot.export(writeStream);
snapshot.export().pipe(readStream||writeStream)Something like this: function ExportStream() {
Stream.Transform.call(this);
this._transform = function noTransform(chunk, encoding, done) {
done(null, chunk);
}
}
inherits(ExportStream, Stream.Transform);
/**
* @param {Stream.Writable|function} dataReceiver
* @returns {Stream|function}
*/
Snapshot.prototype.export = function(dataReceiver) {
dataReceiver = dataReceiver || new ExportStream();
var toStream = dataReceiver instanceof Stream,
chunks = toStream ? null : [];
function onChunk(chunk, len) {
if (toStream) dataReceiver.write(chunk);
else chunks.push(chunk);
}
function onDone() {
if (toStream) dataReceiver.end();
else dataReceiver(null, chunks.join(''));
}
this.serialize(onChunk, onDone);
return dataReceiver;
};In future this allows me to implement also |
d8da5e6 to
1a24629
Compare
7f49e61 to
d0b888b
Compare
6089785 to
3638152
Compare
In some cases you want JSON send or write to a file.
The intuitive way is to
JSON.stringifya snapshot object but that fails due to circular references.This method makes that easier.