/
stream.js
215 lines (183 loc) · 3.67 KB
/
stream.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import http from 'http'
import stream from 'stream'
import split from 'split'
import HttpApi from 'lib-http-api'
class Server {
constructor() {
this.users = {}
}
async putUsers(gen) {
for (let i of gen) {
console.log('putUsers', await i)
}
}
async getUsers() {
let users = this.users
return function*(){
for(user of users) {
yield user
}
}()
}
async getUser(props) {
let {uid} = props
let user = this.users[uid]
return function*(){
if (user) yield {fname: 'boe'}
}()
}
}
function* strToGen(_st){
let st = _st.pipe(new split())
var done = false
var data = []
var prom
st.on('data', line => {
// console.log('data>')
try {
data.push(JSON.parse(line))
}
catch(e) {
// console.error(e)
return
}
if (prom) {
prom(data.shift())
prom = null
}
})
st.on('end', _ => {
// console.log('end>')
done = true
if (prom) prom(null)
})
while(!done || (data.length > 0)) {
if (data.length > 0) {
yield data.shift()
}
else {
// console.log('promise>')
yield new Promise(done => {
prom = done
})
}
}
}
function str(gen) {
var str = new stream.PassThrough()
let next = async function() {
for (let i of gen) {
str.write(JSON.stringify(await i) + '\n')
}
}()
next.then(function(){
str.end()
})
return str
}
class Client {
constructor(api) {
this.api = api
}
_makeRequest(name, props, gen) {
let opt = this.api.request(name, props)
var fin
let req = http.request(opt, function(res){
fin(strToGen(res)) //2938
})
if (gen && gen[Symbol.iterator]) {
// this is a generator
str(gen).pipe(req)
}
else if (gen) {
// this is an object or value
req.end(JSON.stringify(gen))
}
else {
// nothing returned
req.end()
}
return new Promise(done => {
fin = done
})
}
putUsers(gen) {
return this._makeRequest('putUsers', {}, gen)
}
getUsers(props) {
return this._makeRequest('getUsers', props)
}
getUser(props) {
return this._makeRequest('getUser', props)
}
}
let api = HttpApi().New(8080, 'localhost')
let sv = new Server()
var cl = new Client(api)
let st = new stream.PassThrough()
api.add('getUser', {
method : 'GET',
route : '/user/:uid'
})
api.add('getUsers', {
method : 'GET',
route : '/users'
})
api.add('putUsers', {
method : 'PUT',
route : '/users'
})
var sr = http.createServer(function (req, res){
let {handle, params} = api.handle(req.method, req.url)
sv[handle](strToGen(req))
.then(function(gen){
res.statusCode = 200
if (gen && gen[Symbol.iterator]) {
// this is a generator
str(gen).pipe(res)
}
else if (gen) {
// this is an object or value
res.end(JSON.stringify(gen))
}
else {
// nothing returned
res.end()
}
})
.catch(function(e){
console.error(e)
console.error(e.stack)
res.statusCode = 500
res.end()
})
})
sr.listen(8080, function(){
cl.getUser({uid: 'bob'})
.then(async function(gen){
for (let u of gen) {
console.log('getUser', await u)
}
})
.then(function(){
return cl.putUsers(function*(){
yield {fname: 'bob'}
yield {fname: 'kim'}
yield {fname: 'joe'}
}())
})
.then(async function(gen){
for(var u of gen) {
console.log('putUsers', await u)
}
})
.then(ok => {
let names = []
return cl.getUsers(names)
})
.then(async function(gen) {
for (var u of gen) {
console.log('getUsers', await u)
}
})
})