This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
68 lines (59 loc) · 2.12 KB
/
setup.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
import { createConnection } from 'net'
import { createInsideOutPromise } from '@dr-js/core/module/common/function'
import { WEBSOCKET_EVENT } from '@dr-js/core/module/node/server/WebSocket/function'
import { parseHostString } from 'source/module/ServerExot'
// TODO: under DEV
const setupTunnel = async ({
webSocket,
webSocketTunnelHost,
loggerExot,
log = loggerExot && loggerExot.add
}) => {
// setup tcp tunnel socket
const { hostname, port } = parseHostString(webSocketTunnelHost, '127.0.0.1')
const { promise, resolve, reject } = createInsideOutPromise()
const socket = createConnection(port, hostname, resolve)
socket.on('error', reject)
await promise
socket.off('error', reject)
// TODO: redo with Runlet
const cleanup = () => {
webSocket.close()
socket.destroy()
socket.off('error', cleanup)
socket.off('close', cleanup)
socket.off('end', cleanup)
socket.on('error', () => {})
}
socket.on('error', cleanup)
socket.on('close', cleanup)
socket.on('end', cleanup)
socket.on('data', (buffer) => { webSocket.isOpen() && webSocket.sendBuffer(buffer) })
// since this is before WebSocket OPEN, should not have pending data server side
webSocket.on(WEBSOCKET_EVENT.CLOSE, cleanup)
// webSocket.on(WEBSOCKET_EVENT.OPEN, () => { log && log(' - websocket open') })
webSocket.on(WEBSOCKET_EVENT.FRAME, async ({ dataBuffer }) => { socket.writable && socket.write(dataBuffer) })
}
const setup = async ({
name = 'feature:websocket-tunnel',
loggerExot, routePrefix = '',
featureAuth: { createResponderCheckAuth },
webSocketTunnelHost, // hostname:port
URL_WEBSOCKET_TUNNEL = `${routePrefix}/websocket-tunnel`
}) => {
const webSocketRouteList = [
[ URL_WEBSOCKET_TUNNEL, 'GET', createResponderCheckAuth({
responderNext: async (store) => {
const { webSocket } = store
await setupTunnel({ webSocket, webSocketTunnelHost, loggerExot })
store.setState({ protocol: webSocket.protocolList[ 0 ] }) // set protocol to allow upgrade and keep socket
}
}) ]
]
return {
URL_WEBSOCKET_TUNNEL,
webSocketRouteList,
name
}
}
export { setup }