Skip to content

Commit 580993b

Browse files
committed
feat(server): made startUnixSocket take one callback
1 parent 5ff9c2c commit 580993b

File tree

3 files changed

+21
-60
lines changed

3 files changed

+21
-60
lines changed

lib/Server.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -764,20 +764,16 @@ class Server {
764764
}
765765
};
766766

767-
const listenCallback = () => {
767+
const onListeningCallback = () => {
768768
if (typeof this.options.onListening === 'function') {
769769
this.options.onListening(this);
770770
}
771771
};
772772

773-
const setupAndListenCallback = () => {
774-
setupCallback();
775-
listenCallback();
776-
};
777-
778773
const fullCallback = (err) => {
779-
setupAndListenCallback();
774+
setupCallback();
780775
userCallback(err);
776+
onListeningCallback();
781777
};
782778

783779
// try to follow the Node standard in terms of deciding
@@ -789,12 +785,7 @@ class Server {
789785
// set this so that status helper can identify how the project is being run correctly
790786
this.options.socket = socket;
791787

792-
startUnixSocket(
793-
this.listeningApp,
794-
socket,
795-
setupAndListenCallback,
796-
userCallback
797-
);
788+
startUnixSocket(this.listeningApp, socket, fullCallback);
798789
} else {
799790
this.listeningApp.listen(port, hostname, fullCallback);
800791
}

lib/utils/startUnixSocket.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,7 @@
33
const fs = require('fs');
44
const net = require('net');
55

6-
// userCallback should always be called to indicate that either the server has
7-
// started listening, or an error was thrown.
8-
// setupAndListenCallback should only be called if the server starts listening
9-
function startUnixSocket(
10-
listeningApp,
11-
socket,
12-
setupAndListenCallback,
13-
userCallback
14-
) {
15-
const fullCallback = (err) => {
16-
setupAndListenCallback();
17-
userCallback(err);
18-
};
19-
6+
function startUnixSocket(listeningApp, socket, cb) {
207
const chmodSocket = (done) => {
218
// chmod 666 (rw rw rw) - octal
229
const READ_WRITE = 438;
@@ -25,16 +12,16 @@ function startUnixSocket(
2512

2613
const startSocket = () => {
2714
listeningApp.on('error', (err) => {
28-
userCallback(err);
15+
cb(err);
2916
});
3017

3118
// 511 is the default value for the server.listen backlog parameter
3219
// https://nodejs.org/api/net.html#net_server_listen
3320
listeningApp.listen(socket, 511, (err) => {
3421
if (err) {
35-
fullCallback(err);
22+
cb(err);
3623
} else {
37-
chmodSocket(fullCallback);
24+
chmodSocket(cb);
3825
}
3926
});
4027
};
@@ -62,9 +49,7 @@ function startUnixSocket(
6249
// it means that the socket is in use
6350
const err = new Error('This socket is already used');
6451
clientSocket.destroy();
65-
// do not call onListening or the setup method, since the server
66-
// cannot start listening on a used socket
67-
userCallback(err);
52+
cb(err);
6853
});
6954
}
7055
});

test/server/utils/startUnixSocket.test.js

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@ describe('startUnixSocket', () => {
1515
let err;
1616
beforeAll((done) => {
1717
testUnixSocket = new TestUnixSocket();
18-
startUnixSocket(
19-
testUnixSocket.server,
20-
socketPath,
21-
() => {},
22-
(e) => {
23-
err = e;
24-
done();
25-
}
26-
);
18+
startUnixSocket(testUnixSocket.server, socketPath, (e) => {
19+
err = e;
20+
done();
21+
});
2722
});
2823

2924
it('should work as Unix socket or error on windows', (done) => {
@@ -55,7 +50,7 @@ describe('startUnixSocket', () => {
5550
beforeAll((done) => {
5651
fs.writeFileSync(socketPath, '');
5752
testUnixSocket = new TestUnixSocket();
58-
startUnixSocket(testUnixSocket.server, socketPath, () => {}, done);
53+
startUnixSocket(testUnixSocket.server, socketPath, done);
5954
});
6055

6156
it('should work as Unix socket', (done) => {
@@ -89,17 +84,12 @@ describe('startUnixSocket', () => {
8984

9085
it('should throw already used error', (done) => {
9186
testUnixSocket = new TestUnixSocket();
92-
startUnixSocket(
93-
testUnixSocket.server,
94-
socketPath,
95-
() => {},
96-
(err) => {
97-
expect(err).not.toBeNull();
98-
expect(err).not.toBeUndefined();
99-
expect(err.message).toEqual('This socket is already used');
100-
testUnixSocket.close(done);
101-
}
102-
);
87+
startUnixSocket(testUnixSocket.server, socketPath, (err) => {
88+
expect(err).not.toBeNull();
89+
expect(err).not.toBeUndefined();
90+
expect(err.message).toEqual('This socket is already used');
91+
testUnixSocket.close(done);
92+
});
10393
});
10494

10595
afterAll((done) => {
@@ -120,12 +110,7 @@ describe('startUnixSocket', () => {
120110
// could be removed
121111
it('should only call server.listen callback once', (done) => {
122112
const userCallback = jest.fn();
123-
startUnixSocket(
124-
testUnixSocket.server,
125-
socketPath,
126-
() => {},
127-
userCallback
128-
);
113+
startUnixSocket(testUnixSocket.server, socketPath, userCallback);
129114
setTimeout(() => {
130115
expect(userCallback).toBeCalledTimes(1);
131116
done();

0 commit comments

Comments
 (0)