1
1
mirror of https://github.com/reduxjs/redux-devtools.git synced 2025-03-02 02:10:49 +03:00
redux-devtools/packages/redux-devtools-cli/src/index.ts

156 lines
5.2 KiB
TypeScript
Raw Normal View History

import express from 'express';
import http from 'http';
import getPort from 'getport';
import socketClusterServer from 'socketcluster-server';
import getOptions, { Options } from './options';
import routes from './routes';
import createStore from './store';
2019-01-04 01:30:48 +03:00
2019-01-10 20:23:33 +03:00
// var LOG_LEVEL_NONE = 0;
const LOG_LEVEL_ERROR = 1;
const LOG_LEVEL_WARN = 2;
const LOG_LEVEL_INFO = 3;
2019-01-04 01:30:48 +03:00
export interface ExtendedOptions extends Options {
allowClientPublish: boolean;
}
export default function (argv: { [arg: string]: any }): Promise<{
portAlreadyUsed?: boolean;
listener: (eventName: 'ready') => { once(): Promise<void> };
}> {
const options = Object.assign(getOptions(argv), {
allowClientPublish: false,
2019-01-04 01:30:48 +03:00
});
const port = options.port;
const logLevel =
2019-01-10 21:51:14 +03:00
options.logLevel === undefined ? LOG_LEVEL_INFO : options.logLevel;
return new Promise(function (resolve) {
2019-01-04 01:30:48 +03:00
// Check port already used
getPort(port, function (err, p) {
2019-01-10 20:23:33 +03:00
/* eslint-disable no-console */
2019-01-04 01:30:48 +03:00
if (err) {
if (logLevel >= LOG_LEVEL_ERROR) {
console.error(err);
}
return;
}
if (port !== p) {
if (logLevel >= LOG_LEVEL_WARN) {
console.log(`[ReduxDevTools] Server port ${port} is already used.`);
2019-01-04 01:30:48 +03:00
}
2019-01-10 21:51:14 +03:00
resolve({
portAlreadyUsed: true,
listener: function (eventName: 'ready') {
return {
once() {
return Promise.resolve();
},
};
},
2019-01-10 21:51:14 +03:00
});
2019-01-04 01:30:48 +03:00
} else {
if (logLevel >= LOG_LEVEL_INFO) {
console.log('[ReduxDevTools] Start server...');
2019-01-04 01:30:48 +03:00
console.log('-'.repeat(80) + '\n');
}
const httpServer = http.createServer();
const agServer = socketClusterServer.attach(httpServer, options);
const app = express();
httpServer.on('request', app);
const store = createStore(options);
app.use(routes(options, store, agServer));
agServer.setMiddleware(
agServer.MIDDLEWARE_INBOUND,
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async (middlewareStream) => {
for await (const action of middlewareStream) {
if (action.type === action.TRANSMIT) {
const channel = action.receiver;
const data = action.data;
if (
channel.substring(0, 3) === 'sc-' ||
channel === 'respond' ||
channel === 'log'
) {
void agServer.exchange.transmitPublish(channel, data);
} else if (channel === 'log-noid') {
void agServer.exchange.transmitPublish('log', {
id: action.socket.id,
data: data,
});
}
} else if (action.type === action.SUBSCRIBE) {
if (action.channel === 'report') {
store
.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();
}
}
);
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?
resolve(agServer);
2019-01-04 01:30:48 +03:00
}
2019-01-10 20:23:33 +03:00
/* eslint-enable no-console */
2019-01-04 01:30:48 +03:00
});
});
}