1 |
|
2 |
|
3 | exports.createEncodeStream = EncodeStream;
|
4 |
|
5 | var util = require("util");
|
6 | var Transform = require("stream").Transform;
|
7 | var EncodeBuffer = require("./encode-buffer").EncodeBuffer;
|
8 | var encode = require("./write-core").encode;
|
9 |
|
10 | util.inherits(EncodeStream, Transform);
|
11 |
|
12 | var DEFAULT_OPTIONS = {objectMode: true};
|
13 |
|
14 | function EncodeStream(options) {
|
15 | if (!(this instanceof EncodeStream)) return new EncodeStream(options);
|
16 | if (options) {
|
17 | options.objectMode = true;
|
18 | } else {
|
19 | options = DEFAULT_OPTIONS;
|
20 | }
|
21 | Transform.call(this, options);
|
22 |
|
23 | var stream = this;
|
24 | var encoder = this.encoder = new EncodeBuffer(options);
|
25 | encoder.push = function(chunk) {
|
26 | stream.push(chunk);
|
27 | };
|
28 | }
|
29 |
|
30 | EncodeStream.prototype._transform = function(chunk, encoding, callback) {
|
31 | encode(this.encoder, chunk);
|
32 | if (callback) callback();
|
33 | };
|
34 |
|
35 | EncodeStream.prototype._flush = function(callback) {
|
36 | this.encoder.flush();
|
37 | if (callback) callback();
|
38 | };
|