Replace getport with get-port (#1314)

This commit is contained in:
Nathan Bierema 2023-01-05 20:12:28 -05:00 committed by GitHub
parent ce26486f83
commit 78eed2dd20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 122 additions and 142 deletions

View File

@ -49,7 +49,7 @@
"cross-spawn": "^7.0.3", "cross-spawn": "^7.0.3",
"electron": "^22.0.0", "electron": "^22.0.0",
"express": "^4.18.2", "express": "^4.18.2",
"getport": "^0.1.0", "get-port": "^5.1.1",
"graphql": "^16.6.0", "graphql": "^16.6.0",
"knex": "^2.3.0", "knex": "^2.3.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",

View File

@ -1,6 +0,0 @@
declare module 'getport' {
export default function getport(
start: number,
callback: (e: Error | undefined, port: number) => void
): void;
}

View File

@ -1,21 +1,17 @@
import express from 'express'; import express from 'express';
import http from 'http'; import http from 'http';
import getPort from 'getport'; import getPort from 'get-port';
import socketClusterServer from 'socketcluster-server'; import socketClusterServer from 'socketcluster-server';
import getOptions, { Options } from './options'; import getOptions from './options';
import routes from './routes'; import routes from './routes';
import createStore from './store'; import createStore from './store';
// var LOG_LEVEL_NONE = 0; // const LOG_LEVEL_NONE = 0;
const LOG_LEVEL_ERROR = 1; // const LOG_LEVEL_ERROR = 1;
const LOG_LEVEL_WARN = 2; const LOG_LEVEL_WARN = 2;
const LOG_LEVEL_INFO = 3; const LOG_LEVEL_INFO = 3;
export interface ExtendedOptions extends Options { export default async function (argv: { [arg: string]: any }): Promise<{
allowClientPublish: boolean;
}
export default function (argv: { [arg: string]: any }): Promise<{
portAlreadyUsed?: boolean; portAlreadyUsed?: boolean;
listener: (eventName: 'ready') => { once(): Promise<void> }; listener: (eventName: 'ready') => { once(): Promise<void> };
}> { }> {
@ -25,131 +21,120 @@ export default function (argv: { [arg: string]: any }): Promise<{
const port = options.port; const port = options.port;
const logLevel = const logLevel =
options.logLevel === undefined ? LOG_LEVEL_INFO : options.logLevel; options.logLevel === undefined ? LOG_LEVEL_INFO : options.logLevel;
return new Promise(function (resolve) { // Check port already used
// Check port already used const p = await getPort({ port });
getPort(port, function (err, p) { if (port !== p) {
/* eslint-disable no-console */ if (logLevel >= LOG_LEVEL_WARN) {
if (err) { console.log(`[ReduxDevTools] Server port ${port} is already used.`);
if (logLevel >= LOG_LEVEL_ERROR) { }
console.error(err); return {
} portAlreadyUsed: true,
return; listener: function () {
} return {
if (port !== p) { once() {
if (logLevel >= LOG_LEVEL_WARN) { return Promise.resolve();
console.log(`[ReduxDevTools] Server port ${port} is already used.`);
}
resolve({
portAlreadyUsed: true,
listener: function (eventName: 'ready') {
return {
once() {
return Promise.resolve();
},
};
}, },
}); };
} else { },
if (logLevel >= LOG_LEVEL_INFO) { };
console.log('[ReduxDevTools] Start server...'); }
console.log('-'.repeat(80) + '\n');
}
const httpServer = http.createServer();
const agServer = socketClusterServer.attach(httpServer, options);
const app = express(); if (logLevel >= LOG_LEVEL_INFO) {
httpServer.on('request', app); console.log('[ReduxDevTools] Start server...');
const store = createStore(options); console.log('-'.repeat(80) + '\n');
app.use(routes(options, store, agServer)); }
const httpServer = http.createServer();
const agServer = socketClusterServer.attach(httpServer, options);
agServer.setMiddleware( const app = express();
agServer.MIDDLEWARE_INBOUND, httpServer.on('request', app);
// eslint-disable-next-line @typescript-eslint/no-misused-promises const store = createStore(options);
async (middlewareStream) => { app.use(routes(options, store, agServer));
for await (const action of middlewareStream) {
if (action.type === action.TRANSMIT) { agServer.setMiddleware(
const channel = action.receiver; agServer.MIDDLEWARE_INBOUND,
const data = action.data; // eslint-disable-next-line @typescript-eslint/no-misused-promises
if ( async (middlewareStream) => {
channel.substring(0, 3) === 'sc-' || for await (const action of middlewareStream) {
channel === 'respond' || if (action.type === action.TRANSMIT) {
channel === 'log' const channel = action.receiver;
) { const data = action.data;
void agServer.exchange.transmitPublish(channel, data); if (
} else if (channel === 'log-noid') { channel.substring(0, 3) === 'sc-' ||
void agServer.exchange.transmitPublish('log', { channel === 'respond' ||
id: action.socket.id, channel === 'log'
data: data, ) {
}); void agServer.exchange.transmitPublish(channel, data);
} } else if (channel === 'log-noid') {
} else if (action.type === action.SUBSCRIBE) { void agServer.exchange.transmitPublish('log', {
if (action.channel === 'report') { id: action.socket.id,
store data: data,
.list() });
.then(function (data) {
void agServer.exchange.transmitPublish('report', {
type: 'list',
data: data,
});
})
.catch(function (error) {
console.error(error); // eslint-disable-line no-console
});
}
}
action.allow();
}
} }
); } else if (action.type === action.SUBSCRIBE) {
if (action.channel === 'report') {
void (async () => { store
for await (const { socket } of agServer.listener('connection')) { .list()
let channelToWatch: string, channelToEmit: string; .then(function (data) {
void (async () => { void agServer.exchange.transmitPublish('report', {
for await (const request of socket.procedure('login')) { type: 'list',
const credentials = request.data; data: data,
if (credentials === 'master') {
channelToWatch = 'respond';
channelToEmit = 'log';
} else {
channelToWatch = 'log';
channelToEmit = 'respond';
}
request.end(channelToWatch);
}
})();
void (async () => {
for await (const request of socket.procedure('getReport')) {
const id = request.data as string;
store
.get(id)
.then(function (data) {
request.end(data);
})
.catch(function (error) {
console.error(error); // eslint-disable-line no-console
});
}
})();
void (async () => {
for await (const data of socket.listener('disconnect')) {
const channel = agServer.exchange.channel('sc-' + socket.id);
channel.unsubscribe();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
void agServer.exchange.transmitPublish(channelToEmit!, {
id: socket.id,
type: 'DISCONNECTED',
}); });
} })
})(); .catch(function (error) {
console.error(error); // eslint-disable-line no-console
});
} }
})(); }
action.allow();
httpServer.listen(options.port);
// @ts-expect-error Shouldn't there be a 'ready' event?
resolve(agServer);
} }
/* eslint-enable no-console */ }
}); );
});
void (async () => {
for await (const { socket } of agServer.listener('connection')) {
let channelToWatch: string, channelToEmit: string;
void (async () => {
for await (const request of socket.procedure('login')) {
const credentials = request.data;
if (credentials === 'master') {
channelToWatch = 'respond';
channelToEmit = 'log';
} else {
channelToWatch = 'log';
channelToEmit = 'respond';
}
request.end(channelToWatch);
}
})();
void (async () => {
for await (const request of socket.procedure('getReport')) {
const id = request.data as string;
store
.get(id)
.then(function (data) {
request.end(data);
})
.catch(function (error) {
console.error(error); // eslint-disable-line no-console
});
}
})();
void (async () => {
for await (const data of socket.listener('disconnect')) {
const channel = agServer.exchange.channel('sc-' + socket.id);
channel.unsubscribe();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
void agServer.exchange.transmitPublish(channelToEmit!, {
id: socket.id,
type: 'DISCONNECTED',
});
}
})();
}
})();
httpServer.listen(options.port);
// @ts-expect-error Shouldn't there be a 'ready' event?
return agServer;
} }

View File

@ -951,7 +951,7 @@ importers:
eslint-config-prettier: ^8.5.0 eslint-config-prettier: ^8.5.0
eslint-plugin-jest: ^27.1.7 eslint-plugin-jest: ^27.1.7
express: ^4.18.2 express: ^4.18.2
getport: ^0.1.0 get-port: ^5.1.1
graphql: ^16.6.0 graphql: ^16.6.0
jest: ^29.3.1 jest: ^29.3.1
knex: ^2.3.0 knex: ^2.3.0
@ -983,7 +983,7 @@ importers:
cross-spawn: 7.0.3 cross-spawn: 7.0.3
electron: 22.0.0 electron: 22.0.0
express: 4.18.2 express: 4.18.2
getport: 0.1.0 get-port: 5.1.1
graphql: 16.6.0 graphql: 16.6.0
knex: 2.3.0_sqlite3@5.1.4 knex: 2.3.0_sqlite3@5.1.4
lodash: 4.17.21 lodash: 4.17.21
@ -13892,6 +13892,11 @@ packages:
resolution: {integrity: sha512-41eOxtlGgHQRbFyA8KTH+w+32Em3cRdfBud7j67ulzmIfmaHX9doq47s0fa4P5o9H64BZX9nrYI6sJvk46Op+Q==} resolution: {integrity: sha512-41eOxtlGgHQRbFyA8KTH+w+32Em3cRdfBud7j67ulzmIfmaHX9doq47s0fa4P5o9H64BZX9nrYI6sJvk46Op+Q==}
dev: false dev: false
/get-port/5.1.1:
resolution: {integrity: sha512-g/Q1aTSDOxFpchXC4i8ZWvxA1lnPqx/JHqcpIw0/LX9T8x/GBbi6YnlN5nhaKIFkT8oFsscUKgDJYxfwfS6QsQ==}
engines: {node: '>=8'}
dev: false
/get-stdin/4.0.1: /get-stdin/4.0.1:
resolution: {integrity: sha512-F5aQMywwJ2n85s4hJPTT9RPxGmubonuB10MNYo17/xph174n2MIR33HRguhzVag10O/npM7SPk73LMZNP+FaWw==} resolution: {integrity: sha512-F5aQMywwJ2n85s4hJPTT9RPxGmubonuB10MNYo17/xph174n2MIR33HRguhzVag10O/npM7SPk73LMZNP+FaWw==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
@ -13932,10 +13937,6 @@ packages:
resolution: {integrity: sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA==} resolution: {integrity: sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA==}
dev: false dev: false
/getport/0.1.0:
resolution: {integrity: sha512-hx+r6Q5IutZH+5k+zeZe78J4vzgW9IqLzfz8+hqMa9NrM1ccpgPIxrxqiBg+aioJMKcCK5qNKKcGdes3PeTlKQ==}
dev: false
/github-slugger/1.5.0: /github-slugger/1.5.0:
resolution: {integrity: sha512-wIh+gKBI9Nshz2o46B0B3f5k/W+WI9ZAv6y5Dn5WJ5SK1t0TnDimB4WE5rmTD05ZAIn8HALCZVmCsvj0w0v0lw==} resolution: {integrity: sha512-wIh+gKBI9Nshz2o46B0B3f5k/W+WI9ZAv6y5Dn5WJ5SK1t0TnDimB4WE5rmTD05ZAIn8HALCZVmCsvj0w0v0lw==}
dev: true dev: true