UNPKG

627 BJavaScriptView Raw
1// encoder.js
2
3exports.Encoder = Encoder;
4
5var EventLite = require("event-lite");
6var encode = require("./write-core").encode;
7var EncodeBuffer = require("./encode-buffer").EncodeBuffer;
8
9function Encoder(options) {
10 if (!(this instanceof Encoder)) return new Encoder(options);
11 EncodeBuffer.call(this, options);
12}
13
14Encoder.prototype = new EncodeBuffer();
15
16EventLite.mixin(Encoder.prototype);
17
18Encoder.prototype.encode = function(chunk) {
19 encode(this, chunk);
20 this.emit("data", this.read());
21};
22
23Encoder.prototype.end = function(chunk) {
24 if (arguments.length) this.encode(chunk);
25 this.flush();
26 this.emit("end");
27};