-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestServer.ts
101 lines (91 loc) · 2.96 KB
/
RestServer.ts
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
import * as restify from 'restify'
import { Protocol } from './Protocol'
const DEFAULT_HOST = '127.0.0.1'
/**
* Exposes a Protocol on a TCP port
*/
export class RestServer {
public readonly server: restify.Server
private retryTimer?: NodeJS.Timer
private retryReject?: (reason?: any) => void
/**
* Create a new RestServer for the passed protocol. The server will initially be stopped!
* @param protocol Protocol implementation to handle all requests
*/
constructor(private readonly protocol: Protocol) {
this.server = restify.createServer({
name: protocol.name,
version: protocol.version
})
this.server.setMaxListeners(0)
this.server.use(restify.plugins.acceptParser(this.server.acceptable))
this.server.use(restify.plugins.queryParser())
this.server.use(restify.plugins.bodyParser())
this.server.post('/', (req, res, next) => {
return this.protocol.handle(req.body).then(response => {
res.send(response)
return next()
}, next)
})
}
/**
* Start listening for incoming requests.
* If the port is unavailable, the listen call will be retried every second
* until the socket is opened successfully or close() is called.
* @param port TCP port to listen on; omit to use the protocol's default port
* @param host hostname/IP to bind to; omit for localhost
* @return a promise that resolves once the server is up and running
*/
listen(port?: number, host?: string): Promise<void> {
const listenPort = typeof port === 'undefined' ? this.protocol.defaultPort : port
const listenHost = host || DEFAULT_HOST
return new Promise<void>((resolve, reject) => {
const error = (e: any) => {
this.server.removeListener('error', error)
if (e.code === 'EADDRINUSE') {
this.retryReject = reject
this.retryTimer = setTimeout(() => {
this.retryTimer = undefined
this.server.close()
this.server.on('error', error)
this.server.listen(listenPort, listenHost, success)
}, 1000)
} else {
reject()
}
}
const success = () => {
this.server.removeListener('error', error)
resolve()
}
this.server.on('error', error)
this.server.listen(listenPort, listenHost, success)
})
}
/**
* Close the server and stop accepting connections. Call listen() to start the server again.
*/
close(): Promise<void> {
if (this.retryTimer && this.retryReject) {
clearTimeout(this.retryTimer)
this.retryReject()
this.retryTimer = undefined
this.retryReject = undefined
}
if (!this.listening) {
return Promise.resolve()
}
return new Promise<void>(resolve => {
this.server.close(resolve)
})
}
/**
* returns true if the server is active and accepting connections.
*/
get listening() {
return this.server.server.listening
}
get port() {
return this.server.address().port
}
}