-
Notifications
You must be signed in to change notification settings - Fork 0
/
icecream.js
61 lines (56 loc) · 1.28 KB
/
icecream.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
var ID = 9;
const QUEUE = [];
const MAP = new Map();
function forward(req, res) {
const current = ID++;
const method = req.method;
const url = req.url;
const headers = req.headers;
const body = req.body;
return new Promise((resolve, reject) => {
const t = setTimeout(() => {
// reject("TimeOut"); // TODO: error handling
resolve(undefined);
}, 31000);
MAP.set(current, {
method: method,
url: url,
headers: headers,
body: body,
timeout: t,
resolve: resolve,
reject: reject
});
QUEUE.push(current);
});
}
function shift() {
const id = QUEUE.shift();
if (id) {
const icecream = MAP.get(id);
if (icecream) {
return {
id: id,
method: icecream.method,
url: icecream.url,
headers: icecream.headers,
body: icecream.body
};
}
}
return {};
}
function done(taste) {
const id = taste.id;
const icecream = MAP.get(id);
MAP.delete(id);
if (icecream) {
clearTimeout(icecream.timeout);
icecream.resolve(taste);
}
}
module.exports = {
forward,
shift,
done
}