-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathTLSServer.js
More file actions
71 lines (66 loc) · 2.38 KB
/
TLSServer.js
File metadata and controls
71 lines (66 loc) · 2.38 KB
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
'use strict';
import { Image } from 'react-native';
import Server from './Server';
import TLSSocket from './TLSSocket';
/**
* @typedef {object} TLSServerOptions
* @property {any} keystore
* @property {string} [passphrase] - Optional passphrase for keystore
*
* @extends {Server}
*/
export default class TLSServer extends Server {
/**
* @param {(socket: TLSSocket) => void} [secureConnectionListener] Automatically set as a listener for the `'secureConnection'` event.
*/
constructor(secureConnectionListener) {
super();
if (secureConnectionListener) this.on('secureConnection', secureConnectionListener);
this._registerTLSEvents();
}
/**
* @param {TLSServerOptions} options TLS server options
*/
setSecureContext(options) {
/** @private */
this._options = { ...options };
this._options.keystore = Image.resolveAssetSource(this._options.keystore).uri;
}
/**
* Start a server listening for connections.
*
* This function is asynchronous. When the server starts listening, the `'listening'` event will be emitted.
* The last parameter `callback` will be added as a listener for the `'listening'` event.
*
* The `server.listen()` method can be called again if and only if there was an error during the first
* `server.listen()` call or `server.close()` has been called. Otherwise, an `ERR_SERVER_ALREADY_LISTEN`
* error will be thrown.
*
* @param {{ port: number; host: string; reuseAddress?: boolean}} options
* @param {() => void} [callback]
* @override
*/
listen(options, callback) {
const newOptions = { ...options };
// @ts-ignore
newOptions['tls'] = this._options;
return super.listen(newOptions, callback);
}
/**
* @private
*/
_registerTLSEvents() {
this._secureConnectionListener = this._eventEmitter.addListener(
'secureConnection',
(evt) => {
if (evt.id !== this._id) return;
const standardSocket = this._buildSocket(evt.info);
standardSocket._unregisterEvents();
const tlsSocket = new TLSSocket(standardSocket);
this._addConnection(tlsSocket);
this.emit('connection', standardSocket);
this.emit('secureConnection', tlsSocket);
}
);
}
}