|
1 |
| -const binding = require('./binding') |
2 |
| -const Connection = require('./connection') |
3 |
| -const lookup = require('./lookup') |
4 |
| -const events = require('events') |
5 |
| -const semver = require('semver') |
6 |
| -const os = require('os') |
| 1 | +const binding = require("./binding"); |
| 2 | +const Connection = require("./connection"); |
| 3 | +const lookup = require("./lookup"); |
| 4 | +const events = require("events"); |
| 5 | +const semver = require("semver"); |
| 6 | +const os = require("os"); |
7 | 7 |
|
8 | 8 | class Server extends events.EventEmitter {
|
9 |
| - constructor (opts) { |
10 |
| - if (!opts) opts = {} |
11 |
| - super() |
| 9 | + constructor(opts) { |
| 10 | + if (!opts) opts = {}; |
| 11 | + super(); |
12 | 12 |
|
13 |
| - this.connections = [] |
14 |
| - this.allowHalfOpen = !!opts.allowHalfOpen |
15 |
| - this.reusePort = (opts.reusePort || opts.reusePort == null) ? 1 : 0 |
| 13 | + this.connections = []; |
| 14 | + this.allowHalfOpen = !!opts.allowHalfOpen; |
| 15 | + this.reusePort = opts.reusePort || opts.reusePort == null ? 1 : 0; |
16 | 16 |
|
17 | 17 | // SO_REUSEPORT is only supported on kernel 3.9+
|
18 |
| - if (os.platform() === 'linux' && !semver.satisfies(semver.coerce(os.release()), '>=3.9')) { |
19 |
| - this.reusePort = 0 |
| 18 | + if ( |
| 19 | + os.platform() === "linux" && |
| 20 | + !semver.satisfies(semver.coerce(os.release()), ">=3.9") |
| 21 | + ) { |
| 22 | + this.reusePort = 0; |
20 | 23 | }
|
21 | 24 |
|
22 |
| - this._closed = false |
23 |
| - this._address = null |
24 |
| - this._handle = null |
| 25 | + this._closed = false; |
| 26 | + this._address = null; |
| 27 | + this._handle = null; |
25 | 28 | }
|
26 | 29 |
|
27 |
| - address () { |
28 |
| - if (!this._address) throw new Error('Not bound') |
| 30 | + address() { |
| 31 | + if (!this._address) throw new Error("Not bound"); |
29 | 32 | return {
|
30 | 33 | address: this._address,
|
31 |
| - family: 'IPv4', |
| 34 | + family: "IPv4", |
32 | 35 | port: binding.turbo_net_tcp_port(this._handle)
|
33 |
| - } |
| 36 | + }; |
34 | 37 | }
|
35 | 38 |
|
36 |
| - close (onclose) { |
37 |
| - if (!this._address) return |
38 |
| - if (onclose) this.once('close', onclose) |
39 |
| - if (this._closed) return |
40 |
| - this._closed = true |
41 |
| - binding.turbo_net_tcp_close(this._handle) |
| 39 | + close(onclose) { |
| 40 | + if (!this._address) return; |
| 41 | + if (onclose) this.once("close", onclose); |
| 42 | + if (this._closed) return; |
| 43 | + this._closed = true; |
| 44 | + binding.turbo_net_tcp_close(this._handle); |
42 | 45 | }
|
43 | 46 |
|
44 |
| - listen (port, address, backlog, onlistening) { |
45 |
| - if (typeof port === 'function') return this.listen(0, null, port) |
46 |
| - if (typeof address === 'function') return this.listen(port, null, address) |
47 |
| - if (typeof backlog === 'function') return this.listen(port, address, 511, backlog) |
48 |
| - if (!port) port = 0 |
49 |
| - if (typeof port !== 'number') port = Number(port) |
| 47 | + listen(port, address, backlog, onlistening) { |
| 48 | + if (typeof port === "function") return this.listen(0, null, port); |
| 49 | + if (typeof address === "function") return this.listen(port, null, address); |
| 50 | + if (typeof backlog === "function") |
| 51 | + return this.listen(port, address, 511, backlog); |
| 52 | + if (!port) port = 0; |
| 53 | + if (typeof port !== "number") port = Number(port); |
50 | 54 |
|
51 |
| - if (onlistening) this.once('listening', onlistening) |
| 55 | + if (onlistening) this.once("listening", onlistening); |
52 | 56 |
|
53 |
| - const self = this |
| 57 | + const self = this; |
54 | 58 |
|
55 |
| - lookup(address || '0.0.0.0', function (err, ip) { |
56 |
| - if (err) return self.emit('error', err) |
57 |
| - if (self._address) self.emit('error', new Error('Already bound')) |
| 59 | + lookup(address || "0.0.0.0", function(err, ip) { |
| 60 | + if (err) return self.emit("error", err); |
| 61 | + if (self._address) self.emit("error", new Error("Already bound")); |
58 | 62 |
|
59 |
| - self._init() |
| 63 | + self._init(); |
60 | 64 |
|
61 | 65 | try {
|
62 |
| - binding.turbo_net_tcp_listen(self._handle, port, ip, backlog) |
| 66 | + binding.turbo_net_tcp_listen(self._handle, port, ip, backlog); |
63 | 67 | } catch (err) {
|
64 |
| - self.emit('error', err) |
| 68 | + self.emit("error", err); |
65 | 69 | }
|
66 | 70 |
|
67 |
| - self._address = ip |
68 |
| - self.emit('listening') |
69 |
| - }) |
| 71 | + self._address = ip; |
| 72 | + self.emit("listening"); |
| 73 | + }); |
70 | 74 | }
|
71 | 75 |
|
72 |
| - _init () { |
73 |
| - if (this._handle) return |
| 76 | + _init() { |
| 77 | + if (this._handle) return; |
74 | 78 |
|
75 |
| - this._handle = Buffer.alloc(binding.sizeof_turbo_net_tcp_t) |
| 79 | + this._handle = Buffer.alloc(binding.sizeof_turbo_net_tcp_t); |
76 | 80 |
|
77 |
| - binding.turbo_net_tcp_init(this._handle, this, |
| 81 | + binding.turbo_net_tcp_init( |
| 82 | + this._handle, |
| 83 | + this, |
78 | 84 | this._onallocconnection,
|
79 | 85 | null,
|
80 | 86 | null,
|
81 | 87 | null,
|
82 | 88 | null,
|
83 | 89 | this._onclose,
|
84 | 90 | this.reusePort
|
85 |
| - ) |
| 91 | + ); |
86 | 92 | }
|
87 | 93 |
|
88 |
| - _onclose () { |
89 |
| - this._closed = false |
90 |
| - this._address = null |
91 |
| - binding.turbo_net_tcp_destroy(this._handle) |
92 |
| - this._handle = null |
93 |
| - this.emit('close') |
| 94 | + _onclose() { |
| 95 | + this._closed = false; |
| 96 | + this._address = null; |
| 97 | + binding.turbo_net_tcp_destroy(this._handle); |
| 98 | + this._handle = null; |
| 99 | + this.emit("close"); |
94 | 100 | }
|
95 | 101 |
|
96 |
| - _onallocconnection () { |
97 |
| - var c = new Connection(this) |
98 |
| - return c._handle |
| 102 | + _onallocconnection() { |
| 103 | + var c = new Connection(this); |
| 104 | + return c._handle; |
99 | 105 | }
|
100 | 106 | }
|
101 | 107 |
|
102 |
| -module.exports = Server |
| 108 | +module.exports = Server; |
0 commit comments