Skip to content

Commit 78eed2d

Browse files
authored
Replace getport with get-port (#1314)
1 parent ce26486 commit 78eed2d

File tree

4 files changed

+122
-142
lines changed

4 files changed

+122
-142
lines changed

packages/redux-devtools-cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"cross-spawn": "^7.0.3",
5050
"electron": "^22.0.0",
5151
"express": "^4.18.2",
52-
"getport": "^0.1.0",
52+
"get-port": "^5.1.1",
5353
"graphql": "^16.6.0",
5454
"knex": "^2.3.0",
5555
"lodash": "^4.17.21",

packages/redux-devtools-cli/src/getport.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 114 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import express from 'express';
22
import http from 'http';
3-
import getPort from 'getport';
3+
import getPort from 'get-port';
44
import socketClusterServer from 'socketcluster-server';
5-
import getOptions, { Options } from './options';
5+
import getOptions from './options';
66
import routes from './routes';
77
import createStore from './store';
88

9-
// var LOG_LEVEL_NONE = 0;
10-
const LOG_LEVEL_ERROR = 1;
9+
// const LOG_LEVEL_NONE = 0;
10+
// const LOG_LEVEL_ERROR = 1;
1111
const LOG_LEVEL_WARN = 2;
1212
const LOG_LEVEL_INFO = 3;
1313

14-
export interface ExtendedOptions extends Options {
15-
allowClientPublish: boolean;
16-
}
17-
18-
export default function (argv: { [arg: string]: any }): Promise<{
14+
export default async function (argv: { [arg: string]: any }): Promise<{
1915
portAlreadyUsed?: boolean;
2016
listener: (eventName: 'ready') => { once(): Promise<void> };
2117
}> {
@@ -25,131 +21,120 @@ export default function (argv: { [arg: string]: any }): Promise<{
2521
const port = options.port;
2622
const logLevel =
2723
options.logLevel === undefined ? LOG_LEVEL_INFO : options.logLevel;
28-
return new Promise(function (resolve) {
29-
// Check port already used
30-
getPort(port, function (err, p) {
31-
/* eslint-disable no-console */
32-
if (err) {
33-
if (logLevel >= LOG_LEVEL_ERROR) {
34-
console.error(err);
35-
}
36-
return;
37-
}
38-
if (port !== p) {
39-
if (logLevel >= LOG_LEVEL_WARN) {
40-
console.log(`[ReduxDevTools] Server port ${port} is already used.`);
41-
}
42-
resolve({
43-
portAlreadyUsed: true,
44-
listener: function (eventName: 'ready') {
45-
return {
46-
once() {
47-
return Promise.resolve();
48-
},
49-
};
24+
// Check port already used
25+
const p = await getPort({ port });
26+
if (port !== p) {
27+
if (logLevel >= LOG_LEVEL_WARN) {
28+
console.log(`[ReduxDevTools] Server port ${port} is already used.`);
29+
}
30+
return {
31+
portAlreadyUsed: true,
32+
listener: function () {
33+
return {
34+
once() {
35+
return Promise.resolve();
5036
},
51-
});
52-
} else {
53-
if (logLevel >= LOG_LEVEL_INFO) {
54-
console.log('[ReduxDevTools] Start server...');
55-
console.log('-'.repeat(80) + '\n');
56-
}
57-
const httpServer = http.createServer();
58-
const agServer = socketClusterServer.attach(httpServer, options);
37+
};
38+
},
39+
};
40+
}
5941

60-
const app = express();
61-
httpServer.on('request', app);
62-
const store = createStore(options);
63-
app.use(routes(options, store, agServer));
42+
if (logLevel >= LOG_LEVEL_INFO) {
43+
console.log('[ReduxDevTools] Start server...');
44+
console.log('-'.repeat(80) + '\n');
45+
}
46+
const httpServer = http.createServer();
47+
const agServer = socketClusterServer.attach(httpServer, options);
6448

65-
agServer.setMiddleware(
66-
agServer.MIDDLEWARE_INBOUND,
67-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
68-
async (middlewareStream) => {
69-
for await (const action of middlewareStream) {
70-
if (action.type === action.TRANSMIT) {
71-
const channel = action.receiver;
72-
const data = action.data;
73-
if (
74-
channel.substring(0, 3) === 'sc-' ||
75-
channel === 'respond' ||
76-
channel === 'log'
77-
) {
78-
void agServer.exchange.transmitPublish(channel, data);
79-
} else if (channel === 'log-noid') {
80-
void agServer.exchange.transmitPublish('log', {
81-
id: action.socket.id,
82-
data: data,
83-
});
84-
}
85-
} else if (action.type === action.SUBSCRIBE) {
86-
if (action.channel === 'report') {
87-
store
88-
.list()
89-
.then(function (data) {
90-
void agServer.exchange.transmitPublish('report', {
91-
type: 'list',
92-
data: data,
93-
});
94-
})
95-
.catch(function (error) {
96-
console.error(error); // eslint-disable-line no-console
97-
});
98-
}
99-
}
100-
action.allow();
101-
}
102-
}
103-
);
49+
const app = express();
50+
httpServer.on('request', app);
51+
const store = createStore(options);
52+
app.use(routes(options, store, agServer));
10453

105-
void (async () => {
106-
for await (const { socket } of agServer.listener('connection')) {
107-
let channelToWatch: string, channelToEmit: string;
108-
void (async () => {
109-
for await (const request of socket.procedure('login')) {
110-
const credentials = request.data;
111-
if (credentials === 'master') {
112-
channelToWatch = 'respond';
113-
channelToEmit = 'log';
114-
} else {
115-
channelToWatch = 'log';
116-
channelToEmit = 'respond';
117-
}
118-
request.end(channelToWatch);
119-
}
120-
})();
121-
void (async () => {
122-
for await (const request of socket.procedure('getReport')) {
123-
const id = request.data as string;
124-
store
125-
.get(id)
126-
.then(function (data) {
127-
request.end(data);
128-
})
129-
.catch(function (error) {
130-
console.error(error); // eslint-disable-line no-console
131-
});
132-
}
133-
})();
134-
void (async () => {
135-
for await (const data of socket.listener('disconnect')) {
136-
const channel = agServer.exchange.channel('sc-' + socket.id);
137-
channel.unsubscribe();
138-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
139-
void agServer.exchange.transmitPublish(channelToEmit!, {
140-
id: socket.id,
141-
type: 'DISCONNECTED',
54+
agServer.setMiddleware(
55+
agServer.MIDDLEWARE_INBOUND,
56+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
57+
async (middlewareStream) => {
58+
for await (const action of middlewareStream) {
59+
if (action.type === action.TRANSMIT) {
60+
const channel = action.receiver;
61+
const data = action.data;
62+
if (
63+
channel.substring(0, 3) === 'sc-' ||
64+
channel === 'respond' ||
65+
channel === 'log'
66+
) {
67+
void agServer.exchange.transmitPublish(channel, data);
68+
} else if (channel === 'log-noid') {
69+
void agServer.exchange.transmitPublish('log', {
70+
id: action.socket.id,
71+
data: data,
72+
});
73+
}
74+
} else if (action.type === action.SUBSCRIBE) {
75+
if (action.channel === 'report') {
76+
store
77+
.list()
78+
.then(function (data) {
79+
void agServer.exchange.transmitPublish('report', {
80+
type: 'list',
81+
data: data,
14282
});
143-
}
144-
})();
83+
})
84+
.catch(function (error) {
85+
console.error(error); // eslint-disable-line no-console
86+
});
14587
}
146-
})();
147-
148-
httpServer.listen(options.port);
149-
// @ts-expect-error Shouldn't there be a 'ready' event?
150-
resolve(agServer);
88+
}
89+
action.allow();
15190
}
152-
/* eslint-enable no-console */
153-
});
154-
});
91+
}
92+
);
93+
94+
void (async () => {
95+
for await (const { socket } of agServer.listener('connection')) {
96+
let channelToWatch: string, channelToEmit: string;
97+
void (async () => {
98+
for await (const request of socket.procedure('login')) {
99+
const credentials = request.data;
100+
if (credentials === 'master') {
101+
channelToWatch = 'respond';
102+
channelToEmit = 'log';
103+
} else {
104+
channelToWatch = 'log';
105+
channelToEmit = 'respond';
106+
}
107+
request.end(channelToWatch);
108+
}
109+
})();
110+
void (async () => {
111+
for await (const request of socket.procedure('getReport')) {
112+
const id = request.data as string;
113+
store
114+
.get(id)
115+
.then(function (data) {
116+
request.end(data);
117+
})
118+
.catch(function (error) {
119+
console.error(error); // eslint-disable-line no-console
120+
});
121+
}
122+
})();
123+
void (async () => {
124+
for await (const data of socket.listener('disconnect')) {
125+
const channel = agServer.exchange.channel('sc-' + socket.id);
126+
channel.unsubscribe();
127+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
128+
void agServer.exchange.transmitPublish(channelToEmit!, {
129+
id: socket.id,
130+
type: 'DISCONNECTED',
131+
});
132+
}
133+
})();
134+
}
135+
})();
136+
137+
httpServer.listen(options.port);
138+
// @ts-expect-error Shouldn't there be a 'ready' event?
139+
return agServer;
155140
}

pnpm-lock.yaml

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)