-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
184 lines (150 loc) · 2.85 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Dependencies.
*/
const {
Readable,
Stream
} = require('stream')
const pump = require('pump')
const toString = Object.prototype.toString
/**
* Objects supported for morphing.
*/
const map = {
'Promise' : promise,
'Object': object,
'Array': stringify,
'Error': error,
'Function': callback
}
/**
* Expose 'morph'
*/
module.exports = morph
/**
* Transform any value into a readable stream.
*
* @param {String | Number | Boolean | Promises} value
* @param {Boolean?} objectMode
* @param {Stream?} readable
* @return {Stream}
* @api public
*/
function morph (value, input) {
const result = input || readable()
const cb = map[type(value)] || end
cb(result, value)
return new Proxy(result, {
get(target, key, receiver) {
if (key !== 'pipe') return target[key]
return function (dest) {
pump(result, dest)
return dest
}
}
})
}
/**
* Create readable stream.
*
* @api private
*/
function readable () {
const input = new Readable({
objectMode: true
})
input._read = () => {}
return input
}
/**
* Parse value type.
*
* @param {Any} value
* @return {String}
* @api private
*/
function type (value) {
const proto = toString.call(value)
return proto.substring(8, proto.length - 1)
}
/**
* End input stream with given value.
*
* @param {Stream} input
* @param {Any} value
* @api private
*/
function end (input, value) {
if (value != null) input.push(String(value))
input.push(null)
}
/**
* End input stream with given object.
*
* @param {Stream} input
* @param {Object} value
* @api private
*/
function object (input, value) {
if (value instanceof Stream) stream(input, value)
else stringify(input, value)
}
/**
* Stringify object and push down the pipe.
*
* @param {Stream} input
* @param {Object} value
* @api private
*/
function stringify (input, value) {
input.push(JSON.stringify(value))
input.push(null)
}
/**
* End input stream with given stream.
*
* @param {Stream} input
* @param {Stream} value
* @api private
*/
function stream (input, value) {
value.on('data', data => input.push(data))
value.on('error', err => error(input, err))
value.on('end', () => input.push(null))
}
/**
* End input stream with given promise.
*
* @param {Stream} input
* @param {Promise} value
* @api private
*/
function promise (input, value) {
value.then(val => {
morph(val, input)
}, reason => {
input.emit('error', reason)
})
}
/**
* End input stream with given error.
*
* @param {Stream} input
* @param {Error} value
* @api private
*/
function error (input, value) {
//@note we should debug errors
// make error asynchronous
input.destroy(value)
}
/**
* End input stream with given function.
*
* @param {Stream} input
* @param {Function} value
* @api private
*/
function callback (input, value) {
morph(value(), input)
}