-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (50 loc) · 1.54 KB
/
index.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
var Transform = require('readable-stream/transform')
var Promise = require('native-or-bluebird')
var inherits = require('util').inherits
var assert = require('assert')
/**
* Create a new transform constructor.
*/
exports.create = function (transform, flush) {
assert(typeof transform === 'function')
inherits(Stream, Transform)
function Stream(options) {
if (!(this instanceof Stream)) return new Stream(options)
Transform.call(this, options)
// always object mode!
this._readableState.objectMode =
this._writableState.objectMode = true
}
Stream.prototype._transform = function (doc, NULL, cb) {
cb(null, Promise.resolve(transform.call(this, doc)))
}
if (typeof flush === 'function') {
// TODO: assert that flush doesn't have any arguments
Stream.prototype._flush = function (cb) {
Promise.resolve(flush.call(this)).then(cb.bind(null, null), cb)
}
}
return Stream
}
/**
* Create a new transform instance, `through`-style.
* NOT RECOMMENDED!
*/
exports.map = function (transform, flush) {
return exports.create(transform, flush)()
}
/**
* Create a transform stream that resolves all the promises.
*/
exports.resolve = Resolve
inherits(Resolve, Transform)
function Resolve(options) {
if (!(this instanceof Resolve)) return new Resolve(options)
Transform.call(this, options)
// always object mode!
this._readableState.objectMode =
this._writableState.objectMode = true
}
Resolve.prototype._transform = function (doc, NULL, cb) {
Promise.resolve(doc).then(cb.bind(null, null)).catch(cb)
}