-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpullstream.js
More file actions
136 lines (116 loc) · 3.12 KB
/
pullstream.js
File metadata and controls
136 lines (116 loc) · 3.12 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
module.exports = PullStream;
var inherits = require("util").inherits;
var PassThrough = require('readable-stream/passthrough');
var over = require('over');
var SliceStream = require('slice-stream');
function PullStream(opts) {
var self = this;
this.opts = opts || {};
PassThrough.call(this, opts);
this.once('finish', function() {
self._writesFinished = true;
if (self._flushed) {
self._finish();
}
});
this.on('readable', function() {
self._process();
});
}
inherits(PullStream, PassThrough);
PullStream.prototype.pull = over([
[over.numberOptionalWithDefault(null), over.func, function (len, callback) {
if (len === 0) {
return callback(null, new Buffer(0));
}
var self = this;
pullServiceRequest();
function pullServiceRequest() {
self._serviceRequests = null;
if (self._flushed) {
return callback(new Error('End of Stream'));
}
var data = self.read(len || undefined);
if (data) {
setImmediate(callback.bind(null, null, data));
} else {
self._serviceRequests = pullServiceRequest;
}
}
}]
]);
PullStream.prototype.pullUpTo = over([
[over.numberOptionalWithDefault(null), function (len) {
var data = this.read(len);
if (len && !data) {
data = this.read();
}
return data;
}]
]);
PullStream.prototype.pipe = over([
[over.numberOptionalWithDefault(null), over.object, function (len, destStream) {
if (!len) {
return PassThrough.prototype.pipe.call(this, destStream);
}
if (len === 0) {
return destStream.end();
}
var pullstream = this;
pullstream
.pipe(new SliceStream({ length: len }, function (buf, sliceEnd, extra) {
if (!sliceEnd) {
return this.push(buf);
}
pullstream.unpipe();
pullstream.unshift(extra);
this.push(buf);
return this.push(null);
}))
.pipe(destStream);
return destStream;
}]
]);
PullStream.prototype._process = function () {
if (this._serviceRequests) {
this._serviceRequests();
}
};
PullStream.prototype.prepend = function (chunk) {
this.unshift(chunk);
};
PullStream.prototype.drain = function (len, callback) {
if (this._flushed) {
return callback(new Error('End of Stream'));
}
var data = this.pullUpTo(len);
var bytesDrained = data && data.length || 0;
if (bytesDrained === len) {
setImmediate(callback);
} else if (bytesDrained > 0) {
this.drain(len - bytesDrained, callback);
} else {
//internal buffer is empty, wait until data can be consumed
this.once('readable', this.drain.bind(this, len - bytesDrained, callback));
}
};
PullStream.prototype._flush = function (callback) {
var self = this;
if (this._readableState.length > 0) {
return setImmediate(self._flush.bind(self, callback));
}
this._flushed = true;
if (self._writesFinished) {
self._finish(callback);
} else {
callback();
}
};
PullStream.prototype._finish = function (callback) {
callback = callback || function () {};
if (this._serviceRequests) {
this._serviceRequests();
}
setImmediate(callback);
};