-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
262 lines (233 loc) · 7.1 KB
/
index.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// #!/usr/bin/env babel-node
// -*- coding: utf-8 -*-
/** @module web-node */
'use strict'
/* !
region header
[Project page](https://torben.website/webNode)
Copyright Torben Sickert (info["~at~"]torben.website) 16.12.2012
License
-------
This library written by Torben Sickert stand under a creative commons
naming 3.0 unported license.
See https://creativecommons.org/licenses/by/3.0/deed.de
endregion
*/
// region imports
// NOTE: http2 compatibility mode does work for unencrypted connections yet.
import {
CLOSE_EVENT_NAMES,
evaluateDynamicData,
extend,
isFileSync,
MAXIMAL_NUMBER_OF_ITERATIONS,
modifyObject,
RecursivePartial,
represent,
timeout,
UTILITY_SCOPE
} from 'clientnode'
import {createServer as createHTTP1Server} from 'http'
import {createServer, createSecureServer} from 'http2'
import {resolve} from 'path'
import reverseProxyBufferedRequest, {
addParsedContentToRequest,
applyStateAPIs,
determineForwarder,
logging,
resolveForwarders
} from './helper'
import packageConfiguration from './package.json'
import {
BufferedHTTPServerRequest,
BufferedSocket,
Configuration,
HTTPServerResponse,
HTTPServerRequest,
HTTPStream,
OutgoingHTTPHeaders,
ResolvedForwarder,
ResolvedForwarders,
Server
} from './type'
// endregion
declare const ORIGINAL_MAIN_MODULE: object
// region live cycle methods
const onIncomingMessage = (
request: HTTPServerRequest, response: HTTPServerResponse
): void => {
const bufferedRequest: BufferedHTTPServerRequest =
request as BufferedHTTPServerRequest
void (async (): Promise<void> => {
void logging.info(
`|${'-'.repeat(80 - 2)}|\nStart processing`,
`${bufferedRequest.method} request: ${bufferedRequest.url}\n` +
(bufferedRequest as unknown as string)
)
// NOTE: We have to wait until client request is fully buffered.
for (
let iteration = 0;
iteration < (MAXIMAL_NUMBER_OF_ITERATIONS.value * 1000);
iteration++
) {
await timeout()
if (bufferedRequest.socket.buffer.finished)
break
}
if (CONFIGURATION.parseBody)
addParsedContentToRequest(bufferedRequest)
const forwarder: null | ResolvedForwarder =
determineForwarder(bufferedRequest, response, FORWARDERS)
if (forwarder === null) {
void logging.error(
'No forwarder found for given request:', bufferedRequest
)
response.statusCode = 502
response.end()
return
}
try {
const {result, scope} =
await applyStateAPIs(bufferedRequest, response, forwarder)
if (result)
await reverseProxyBufferedRequest(
bufferedRequest, response, forwarder, scope
)
} catch (error) {
void logging.error(error)
}
void logging.info(
`\nEnd processing`,
`${bufferedRequest.method} request: ${bufferedRequest.url}\n` +
`${bufferedRequest as unknown as string}|${'-'.repeat(80 - 2)}|`
)
})()
}
const onIncomingStream = (
stream: HTTPStream, headers: OutgoingHTTPHeaders
) => {
void logging.info('Got stream', stream, headers)
}
// endregion
// region configuration
const BASE_CONFIGURATION: Configuration = packageConfiguration.configuration
for (const path of [
'configuration.json', 'secure-configuration.json'
] as const) {
const configurationPath: string = resolve(process.cwd(), path)
if (isFileSync(configurationPath)) {
const configuration: RecursivePartial<Configuration> =
eval(`require('${configurationPath}')`) as
RecursivePartial<Configuration>
extend(
true,
modifyObject<Configuration>(BASE_CONFIGURATION, configuration),
configuration
)
}
}
const CONFIGURATION: Configuration = evaluateDynamicData<Configuration>(
BASE_CONFIGURATION,
{
...UTILITY_SCOPE,
configuration: BASE_CONFIGURATION,
environment: process.env
}
)
const FORWARDERS: ResolvedForwarders =
resolveForwarders(CONFIGURATION.forwarders)
// endregion
// region initialize server
const server: Server = {
instance: CONFIGURATION.publicKeyPath && CONFIGURATION.privateKeyPath ?
createSecureServer(
CONFIGURATION.nodeServerOptions, onIncomingMessage
) :
// NOTE: See import notice.
(createHTTP1Server as unknown as typeof createServer)(
onIncomingMessage
),
streams: [],
sockets: [],
start: () => {
server.instance.listen(
CONFIGURATION.port,
() => {
void logging.info(
`Listen on port ${String(CONFIGURATION.port)} for ` +
'incoming requests.'
)
}
)
},
stop: () => {
server.instance.close(() => {
void logging.info('Shut server down.')
})
for (const connections of [server.sockets, server.streams])
if (Array.isArray(connections))
for (const connection of connections)
connection.destroy()
}
}
server.instance.on(
'connection',
(socket: BufferedSocket): void => {
server.sockets.push(socket)
socket.buffer = {
data: [],
finished: false
}
socket.on('data', (data: Buffer) => {
socket.buffer.data.push(data)
})
for (const name of [
'done', 'finish', 'writableEnded', 'writableFinished'
])
socket.on(name, () => {
socket.buffer.finished = true
})
/*
NOTE: Workaround since none of the events above trigger before
responding to client occurred.
*/
void timeout(() => {
socket.buffer.finished = true
})
socket.on('close', () => {
socket.buffer.finished = true
server.sockets.splice(server.sockets.indexOf(socket), 1)
})
}
)
server.instance.on(
'stream',
(stream: HTTPStream, headers: OutgoingHTTPHeaders) => {
server.streams.push(stream)
onIncomingStream(stream, headers)
stream.on('close', () => {
server.streams.splice(server.streams.indexOf(stream), 1)
})
}
)
// endregion
// region start / stop
if (
require.main === module ||
eval('require.main') !== require.main &&
typeof ORIGINAL_MAIN_MODULE !== 'undefined' &&
ORIGINAL_MAIN_MODULE === eval('require.main')
) {
void logging.info(
'Start server with configuration:', represent(CONFIGURATION)
)
void logging.debug('Apply resolved forwarder:', represent(FORWARDERS))
server.start()
for (const name of CLOSE_EVENT_NAMES)
process.on(name, () => {
void logging.info(`\nGot "${name}" signal: stopping server.`)
server.stop()
})
}
// endregion
export default server