-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequestRegistry.js
43 lines (35 loc) · 1003 Bytes
/
requestRegistry.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
import { v4 as uuidv4 } from 'uuid'
/**
* @type RequestRegistry = {
* has :: String -> Boolean,
* add :: (Request, Response, Next) -> String,
* complete :: (String, String) -> _,
* fail :: (String, String) -> _,
* }
*/
// createRequestRegistry :: () -> RequestRegistry
export default () => ({
_requests: {},
has: function (id) {
// eslint-disable-next-line no-prototype-builtins
return this._requests.hasOwnProperty(id)
},
add: function (req, res, next) {
const id = uuidv4()
this._requests[id] = { req, res, next }
return id
},
complete: function (id, result, status = 200) {
if (this.has(id)) {
//this._requests[id].res.status(status).send(result)
this._requests[id].res.status(status).end(Buffer.from(result, 'base64'))
delete this._requests[id]
}
},
fail: function (id, status = 500) {
if (this.has(id)) {
this._requests[id].res.status(status).end()
delete this._requests[id]
}
},
})