UNPKG

@dfeidao/send-msg

Version:

消息总线

79 lines (78 loc) 3.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const log4js_1 = require("log4js"); const path_1 = require("path"); const config_1 = __importDefault(require("./config")); const logger = (0, log4js_1.getLogger)(); async function resolve_path(file_name, path) { // !!! We could use require.resolve here, because electron does not support. const full_path = (0, path_1.join)(path, `${file_name}.js`); await fs_1.promises.access(full_path, fs_1.constants.F_OK); return full_path; } async function invoke(cwd, headers, content) { const tm = new Date().getTime(); const headerstr = JSON.stringify(headers); logger.info(`Begin dealing message, headers=${headerstr}`); const body = Buffer.isBuffer(content) ? 'Blob Data' : JSON.stringify(content); try { logger.debug(`Begin dealing message, body=${body}, headers=${headerstr}`); const modelid = content.modelid; const file_name = modelid; logger.debug(`trying get service file:${file_name}, headers=${headerstr}`); const path = await (async () => { try { // !!! await added here to get exception return await resolve_path(file_name, (0, path_1.join)(cwd, 'dist')); } catch (error) { logger.trace(error); logger.error(`Could not load service:${file_name}, headers=${headerstr}`); throw new Error(`Could not load service:${file_name}`); } })(); if (config_1.default.debug) { delete require.cache[path]; } // todo sessionid, usersessionid 多余,要在下个大版本去掉 const { actionid, sessionid, usersessionid } = headers; const result = await require(path).default(content, actionid, (usersessionid || sessionid), headers); if (result === undefined) { logger.error(`Service:${file_name} should return something. actionid=${actionid}, msg=${body}`); throw new Error(`Service:${file_name} should return something.`); } else { return result; } } catch (e) { const err = e; logger.trace(err); const err_msg = (() => { try { JSON.parse(err.message); return err.message; } catch (error) { return JSON.stringify({ msg: err.message, detail: err.message }); } })(); logger.error(`Service Error:${err_msg}, , headers=${headerstr}`); throw new Error(err_msg); } finally { const cost = new Date().getTime() - tm; logger.debug(`End dealing message:${body}, ${cost}ms cost, , headers=${headerstr}`); if (cost > 500) { logger.error(`Service cost ${cost} ms, please check the service! Message body=[${body}], , headers=${headerstr}`); } else if (cost > 200) { logger.warn(`Service cost ${cost} ms, maybe you should please check the service! Message body=[${body}], headers=${headerstr}`); } } } exports.default = invoke;