Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating derived Read / Write streams.
- Loading branch information
Showing
2 changed files
with
103 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,9 @@ | ||
"use strict"; | ||
|
||
const { Cc, Ci, components: { Constructor: CC } } = require("chrome"); | ||
const { Buffer } = require("buffer"); | ||
const { EventEmitterTrait: EventEmitter } = require("events"); | ||
const StreamReader = CC("@mozilla.org/binaryinputstream;1", | ||
"nsIBinaryInputStream", "setInputStream"); | ||
|
||
function Stream(channel) { | ||
function Stream() { | ||
return this instanceof Stream ? this : Object.create(Stream.prototype); | ||
} | ||
Stream.prototype = EventEmitter.create(Stream.prototype); | ||
Object.defineProperties(Stream.prototype, { | ||
open: { value: function open(channel) { | ||
let emit = this._emit.bind(this); | ||
channel.asyncOpen({ | ||
onDataAvailable: function onData(request, context, inputStream, offset, length) { | ||
try { | ||
emit("data", new Buffer(StreamReader(inputStream).readByteArray(length))); | ||
} catch (error) { | ||
emit("error", error); | ||
} | ||
}, | ||
onStartRequest: function onStartRequest() { emit("start"); }, | ||
onStopRequest: function onStopRequest() { emit("end"); } | ||
}, null); | ||
}} | ||
}); | ||
exports.Stream = Stream; |