Skip to content

Commit

Permalink
refactor(connector-fabric-socketio): fix strict flag warnings
Browse files Browse the repository at this point in the history
cactus-plugin-ledger-connector-fabric-socketio will compile with global strict flag.

Related issue: #1671

Signed-off-by: Michal Bajer <[email protected]>
  • Loading branch information
outSH committed Apr 15, 2022
1 parent dda3f00 commit 17641c1
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ app.use(
err: { message: string; status?: number },
req: Request,
res: Response,
next: NextFunction
next: NextFunction,
) => {
// set locals, only providing error in development
res.locals.message = err.message;
Expand All @@ -47,7 +47,7 @@ app.use(
// render the error page
res.status(err.status || 500);
res.send(err);
}
},
);

export default app;
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ server.on("listening", onListening);
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
function normalizePort(val: string) {
const port = parseInt(val, 10);

if (isNaN(port)) {
Expand All @@ -89,7 +89,7 @@ function normalizePort(val) {
* Event listener for HTTPS server "error" event.
*/

function onError(error) {
function onError(error: any) {
if (error.syscall !== "listen") {
throw error;
}
Expand Down Expand Up @@ -118,6 +118,12 @@ function onError(error) {

function onListening() {
const addr = server.address();

if (!addr) {
logger.error("Could not get running server address - exit.");
process.exit(1);
}

const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
}
Expand Down Expand Up @@ -148,14 +154,14 @@ io.on("connection", function (client) {
// Check for the existence of the specified function and call it if it exists.
if (Splug.isExistFunction(func)) {
// Can be called with Server plugin function name.
Splug[func](args)
.then((respObj) => {
(Splug as any)[func](args)
.then((respObj: unknown) => {
logger.info("*** RESPONSE ***");
logger.info("Client ID :" + client.id);
logger.info("Response :" + JSON.stringify(respObj));
client.emit("response", respObj);
})
.catch((errObj) => {
.catch((errObj: unknown) => {
logger.error("*** ERROR ***");
logger.error("Client ID :" + client.id);
logger.error("Detail :" + JSON.stringify(errObj));
Expand All @@ -175,14 +181,17 @@ io.on("connection", function (client) {

client.on("request2", function (data) {
const func = data.method.method;
const args = {};
args["contract"] = data.contract;
args["method"] = data.method;
args["args"] = data.args;
let args: Record<string, any> = {
contract: data.contract,
method: data.method,
args: data.args,
};

if (data.reqID !== undefined) {
logger.info(`##add reqID: ${data.reqID}`);
args["reqID"] = data.reqID;
}

logger.info("##[HL-BC] Invoke smart contract to transfer asset(D1)");
logger.info("*** REQUEST ***");
logger.info("Client ID :" + client.id);
Expand Down Expand Up @@ -228,14 +237,14 @@ io.on("connection", function (client) {
// logger.info(`##args: ${JSON.stringify(args)}`);
if (Splug.isExistFunction(func)) {
// Can be called with Server plugin function name.
Splug[func](args)
.then((respObj) => {
(Splug as any)[func](args)
.then((respObj: unknown) => {
logger.info("*** RESPONSE ***");
logger.info("Client ID :" + client.id);
logger.info("Response :" + JSON.stringify(respObj));
client.emit("response", respObj);
})
.catch((errObj) => {
.catch((errObj: unknown) => {
logger.error("*** ERROR ***");
logger.error("Client ID :" + client.id);
logger.error("Detail :" + JSON.stringify(errObj));
Expand Down Expand Up @@ -268,7 +277,7 @@ io.on("connection", function (client) {
**/
client.on("startMonitor", function () {
// Callback to receive monitoring results
const cb = function (callbackData) {
const cb = function (callbackData: Record<string, any>) {
let emitType = "";
if (callbackData.status == 200) {
emitType = "eventReceived";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@ logger.level = config.read<string>("logLevel", "info");
import { ValidatorAuthentication } from "./ValidatorAuthentication";
import safeStringify from "fast-safe-stringify";

export type MonitorCallback = (callback: {
status: number;
blockData?: string;
errorDetail?: string;
}) => void;

/*
* ServerMonitorPlugin
* Server monitoring class definition
*/
export class ServerMonitorPlugin {
_filterTable: object;
_eh: FabricClient.ChannelEventHub;

constructor() {
// Define settings specific to the dependent part
// Initializing filter during monitoring
this._filterTable = {};
this._eh = null;
}
_filterTable = new Map<string, FabricClient>();
_eh?: FabricClient.ChannelEventHub;

/*
* startMonitor
Expand All @@ -50,17 +49,17 @@ export class ServerMonitorPlugin {
* @param {function} cb : Callback function that receives the monitoring result at any time
* @note Always listens on the first peer from config.
*/
startMonitor(clientId, cb) {
startMonitor(clientId: string, cb: MonitorCallback) {
logger.info("*** START MONITOR ***");
logger.info("Client ID :" + clientId);
const filter = this._filterTable[clientId];
let channel: FabricClient.Channel = null;
const filter = this._filterTable.get(clientId);
let channel: FabricClient.Channel;

if (!filter) {
getClientAndChannel()
.then((retobj) => {
channel = retobj.channel; //Set the returned channel
this._filterTable[clientId] = retobj.client;
this._filterTable.set(clientId, retobj.client);
return getSubmitterAndEnroll(retobj.client);
})
.then(() => {
Expand All @@ -69,9 +68,17 @@ export class ServerMonitorPlugin {
);
logger.info("Connecting the event hub");
this._eh.registerBlockEvent(
(block: FabricClient.Block) => {
(block) => {
const txlist = [];
logger.info("*** Block Event ***");

if (!("header" in block && "data" in block)) {
logger.warn(
"Invalid block type fromregisterBlockEvent - expected FabricClient.Block",
);
return;
}

console.log("##[HL-BC] Notify new block data(D2)");
logger.info(
"chain id :" + config.read<string>("fabric.channelName"),
Expand Down Expand Up @@ -160,8 +167,8 @@ export class ServerMonitorPlugin {
* Stop monitoring
* @param {string} clientId : Client ID of the monitoring stop request source
*/
stopMonitor(clientId) {
const filter = this._filterTable[clientId];
stopMonitor(clientId: string) {
const filter = this._filterTable.get(clientId);

if (filter) {
// Stop filter & remove from table
Expand All @@ -173,7 +180,7 @@ export class ServerMonitorPlugin {
logger.info("Disconnecting the event hub");
this._eh.disconnect();
}
delete this._filterTable[clientId];
this._filterTable.delete(clientId);
}
}
}
Loading

0 comments on commit 17641c1

Please sign in to comment.