Skip to content

Commit 793d2bf

Browse files
committed
Prettified Codes
1 parent 0bb2858 commit 793d2bf

File tree

3 files changed

+121
-115
lines changed

3 files changed

+121
-115
lines changed

lib/lookup.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const dns = require('dns')
2-
const IPv4 = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
1+
const dns = require("dns");
2+
const IPv4 = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
33

4-
module.exports = lookup
4+
module.exports = lookup;
55

6-
function lookup (addr, cb) {
7-
if (IPv4.test(addr)) return process.nextTick(cb, null, addr)
8-
dns.lookup(addr, cb)
6+
function lookup(addr, cb) {
7+
if (IPv4.test(addr)) return process.nextTick(cb, null, addr);
8+
dns.lookup(addr, cb);
99
}

lib/queue.js

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,78 @@
11
class RequestQueue {
2-
constructor (size, handle) {
3-
this.size = size
4-
this.mask = size - 1
5-
this.handle = handle
6-
this.top = 0
7-
this.btm = 0
8-
this.list = new Array(size)
9-
this.fill(0)
2+
constructor(size, handle) {
3+
this.size = size;
4+
this.mask = size - 1;
5+
this.handle = handle;
6+
this.top = 0;
7+
this.btm = 0;
8+
this.list = new Array(size);
9+
this.fill(0);
1010
}
1111

1212
// In most cases this will never be called so use
1313
// a simple realloc method, to ensure it always works
14-
grow () {
15-
const size = this.size
16-
const list = new Array(size * 2)
17-
for (var i = 0; i < size; i++) list[i] = this.shift()
18-
this.size = list.length
19-
this.mask = this.size - 1
20-
this.top = size
21-
this.btm = 0
22-
this.list = list
23-
this.fill(size)
14+
grow() {
15+
const size = this.size;
16+
const list = new Array(size * 2);
17+
for (var i = 0; i < size; i++) list[i] = this.shift();
18+
this.size = list.length;
19+
this.mask = this.size - 1;
20+
this.top = size;
21+
this.btm = 0;
22+
this.list = list;
23+
this.fill(size);
2424
}
2525

26-
fill (offset) {
26+
fill(offset) {
2727
for (var i = offset; i < this.list.length; i++) {
28-
this.list[i] = new Request(this.handle)
28+
this.list[i] = new Request(this.handle);
2929
}
3030
}
3131

32-
push (buffer, cb) {
33-
const req = this.list[this.top]
34-
this.top = (this.top + 1) & this.mask
35-
if (this.top === this.btm) this.grow()
36-
return req
32+
push(buffer, cb) {
33+
const req = this.list[this.top];
34+
this.top = (this.top + 1) & this.mask;
35+
if (this.top === this.btm) this.grow();
36+
return req;
3737
}
3838

39-
shift () {
40-
const req = this.list[this.btm]
41-
this.btm = (this.btm + 1) & this.mask
42-
return req
39+
shift() {
40+
const req = this.list[this.btm];
41+
this.btm = (this.btm + 1) & this.mask;
42+
return req;
4343
}
4444

45-
peek () {
46-
return this.list[this.btm]
45+
peek() {
46+
return this.list[this.btm];
4747
}
4848
}
4949

5050
class Request {
51-
constructor (handle) {
52-
this.callback = null
53-
this.buffer = null
54-
this.buffers = null
55-
this.length = 0
56-
this.lengths = null
57-
this.handle = handle ? Buffer.alloc(handle) : null
51+
constructor(handle) {
52+
this.callback = null;
53+
this.buffer = null;
54+
this.buffers = null;
55+
this.length = 0;
56+
this.lengths = null;
57+
this.handle = handle ? Buffer.alloc(handle) : null;
5858
}
5959

60-
donev (err) {
61-
const cb = this.callback
62-
const buffers = this.buffers
63-
const lengths = this.lengths
60+
donev(err) {
61+
const cb = this.callback;
62+
const buffers = this.buffers;
63+
const lengths = this.lengths;
6464

65-
this.callback = this.buffers = this.lengths = null
66-
cb(err, buffers, lengths)
65+
this.callback = this.buffers = this.lengths = null;
66+
cb(err, buffers, lengths);
6767
}
6868

69-
done (err, len) {
70-
const cb = this.callback
71-
const buf = this.buffer
69+
done(err, len) {
70+
const cb = this.callback;
71+
const buf = this.buffer;
7272

73-
this.callback = this.buffer = null
74-
cb(err, buf, len)
73+
this.callback = this.buffer = null;
74+
cb(err, buf, len);
7575
}
7676
}
7777

78-
module.exports = RequestQueue
78+
module.exports = RequestQueue;

lib/server.js

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,108 @@
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");
77

88
class Server extends events.EventEmitter {
9-
constructor (opts) {
10-
if (!opts) opts = {}
11-
super()
9+
constructor(opts) {
10+
if (!opts) opts = {};
11+
super();
1212

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;
1616

1717
// 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;
2023
}
2124

22-
this._closed = false
23-
this._address = null
24-
this._handle = null
25+
this._closed = false;
26+
this._address = null;
27+
this._handle = null;
2528
}
2629

27-
address () {
28-
if (!this._address) throw new Error('Not bound')
30+
address() {
31+
if (!this._address) throw new Error("Not bound");
2932
return {
3033
address: this._address,
31-
family: 'IPv4',
34+
family: "IPv4",
3235
port: binding.turbo_net_tcp_port(this._handle)
33-
}
36+
};
3437
}
3538

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);
4245
}
4346

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);
5054

51-
if (onlistening) this.once('listening', onlistening)
55+
if (onlistening) this.once("listening", onlistening);
5256

53-
const self = this
57+
const self = this;
5458

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"));
5862

59-
self._init()
63+
self._init();
6064

6165
try {
62-
binding.turbo_net_tcp_listen(self._handle, port, ip, backlog)
66+
binding.turbo_net_tcp_listen(self._handle, port, ip, backlog);
6367
} catch (err) {
64-
self.emit('error', err)
68+
self.emit("error", err);
6569
}
6670

67-
self._address = ip
68-
self.emit('listening')
69-
})
71+
self._address = ip;
72+
self.emit("listening");
73+
});
7074
}
7175

72-
_init () {
73-
if (this._handle) return
76+
_init() {
77+
if (this._handle) return;
7478

75-
this._handle = Buffer.alloc(binding.sizeof_turbo_net_tcp_t)
79+
this._handle = Buffer.alloc(binding.sizeof_turbo_net_tcp_t);
7680

77-
binding.turbo_net_tcp_init(this._handle, this,
81+
binding.turbo_net_tcp_init(
82+
this._handle,
83+
this,
7884
this._onallocconnection,
7985
null,
8086
null,
8187
null,
8288
null,
8389
this._onclose,
8490
this.reusePort
85-
)
91+
);
8692
}
8793

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");
94100
}
95101

96-
_onallocconnection () {
97-
var c = new Connection(this)
98-
return c._handle
102+
_onallocconnection() {
103+
var c = new Connection(this);
104+
return c._handle;
99105
}
100106
}
101107

102-
module.exports = Server
108+
module.exports = Server;

0 commit comments

Comments
 (0)