@micro.ts/core
Version:
Microservice framework with Typescript
119 lines (118 loc) • 4.84 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisBroker = void 0;
const AbstractBroker_1 = require("../AbstractBroker");
const ioredis_1 = __importDefault(require("ioredis"));
class RedisBroker extends AbstractBroker_1.AbstractBroker {
constructor() {
super(...arguments);
this.name = 'RedisBroker';
this.requestMapper = (path, fullPath, body) => __awaiter(this, void 0, void 0, function* () {
const action = {
request: {
params: {},
path: fullPath,
headers: {},
body,
method: 'post',
qs: {},
raw: { pattern: fullPath, channel: path, message: body },
},
connection: this.server,
};
return action;
});
this.routeMapper = (def) => {
const handlerParams = this.extractParamNames(def.handler, '/');
const handlerPatternPath = handlerParams
.map((item) => {
if (item.param) {
return '*';
}
else {
return item.name;
}
})
.join(':');
return `${def.base}:${def.controller}:${handlerPatternPath}`.replace(/\//g, ':');
};
}
construct() {
this.server = new ioredis_1.default(this.config);
this.subscriber = new ioredis_1.default(this.config);
}
consumeMessage(path, fullPath, message) {
return __awaiter(this, void 0, void 0, function* () {
const handlerPairs = this.registeredRoutes.get(path);
if (handlerPairs) {
let body;
if (handlerPairs[0].def.json) {
try {
body = JSON.parse(message);
}
catch (err) {
body = message;
}
}
else {
body = message;
}
const def = handlerPairs[0].def;
const originalPath = `${def.base}/${def.controller}/${def.handler}`.replace(/\/\//g, '/');
const action = yield this.requestMapper(path, fullPath, body);
action.request.params = this.parseParams(fullPath, originalPath);
const handler = this.actionToRouteMapper(path, action, handlerPairs);
const result = yield handler(action);
yield this.server.del(path);
}
});
}
parseParams(fullPath, originalPath) {
const patternSplit = fullPath.split(':');
const originalParams = this.extractParamNames(originalPath, '/');
const returnValue = {};
originalParams.forEach((param, index) => {
if (param.param) {
returnValue[param.name] = patternSplit[index];
}
});
return returnValue;
}
start() {
return __awaiter(this, void 0, void 0, function* () {
const routes = [];
this.registeredRoutes.forEach((def, key) => {
routes.push(key);
});
yield new Promise((resolve, reject) => {
this.subscriber.on('connect', () => {
console.log(`Redis connected on ${this.config}`);
resolve();
});
this.subscriber.on('error', (err) => {
reject(err);
});
});
this.subscriber.on('pmessage', (channel, pattern, message) => {
const path = channel;
const fullPath = pattern;
const body = message;
this.consumeMessage(path, fullPath, body);
});
yield this.subscriber.psubscribe(...routes);
});
}
}
exports.RedisBroker = RedisBroker;