-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.js
93 lines (78 loc) · 2.14 KB
/
chunk.js
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
'use strict';
var Promise = require('bluebird');
/**
* @class Chunk
* @param {Buffer} data - initial data
* @param {stream.Readable} stream - stream to read from
*/
var Chunk = module.exports = function (data, stream) {
this.data = new Buffer(0);
if (Buffer.isBuffer(data)) {
this.data = data;
}
if (typeof data === 'string') {
this.data = new Buffer(data);
}
if (stream) {
this._stream = stream;
stream.pause();
stream.removeAllListeners('error');
stream.removeAllListeners('data');
stream.removeAllListeners('end');
this._next = new Promise(function (resolve, reject) {
stream.on('error', reject);
stream.on('data', function (data) {
resolve(new Chunk(data, stream));
});
stream.on('end', function () {
resolve(new Chunk());
});
});
}
};
/**
* Checks whether there are more chunks to read.
* If not, the end of the stream is reached.
* @returns {boolean}
*/
Chunk.prototype.hasNext = function () {
return !!this._next;
};
/**
* Reads the next chunk in the stream.
* @param {Function} [onSuccess] - the handler to call on success
* @param {Function} [onFailure] - the handler to call on failure
* @returns {?Promise} A promise if the end of the stream has not been reached, null otherwise.
*/
Chunk.prototype.next = function (onSuccess, onFailure) {
// Switch chunk from present to past,
// resume and unlink stream.
var stream = this._stream;
if (stream) {
this._stream = null;
stream.resume();
}
return this._next && this._next.then(onSuccess, onFailure);
};
/**
* Checks whether the next chunk is available for synchronous reading.
* @returns {boolean}
*/
Chunk.prototype.hasNextAvailable = function () {
return !!this._next && !this._next.isPending();
};
/**
* Reads the next chunk which is available for synchronous reading.
* @throws An error if reading the next chunk yielded an error.
* @returns {?Chunk}
*/
Chunk.prototype.nextAvailable = function () {
if (this._next) {
if (this._next.isFulfilled()) {
return this._next.value();
}
if (this._next.isRejected()) {
throw this._next.reason();
}
}
};