@haku-sci/utils
Version:
utils from haku-sci. Library only
61 lines • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HAKU_SCI_ACTION = exports.HAKU_SCI_RESOURCE = void 0;
exports.executeFunction = executeFunction;
exports.getSortedParameters = getSortedParameters;
exports.microServiceName = microServiceName;
exports.getHttpRequestMaxSize = getHttpRequestMaxSize;
exports.withWatchdog = withWatchdog;
const promises_1 = require("fs/promises");
const path = require("path");
const common_1 = require("@nestjs/common");
exports.HAKU_SCI_RESOURCE = 'haku:resource';
exports.HAKU_SCI_ACTION = 'haku:action';
function executeFunction(parent, funcName, params) {
const func = parent[funcName];
const boundFunc = func.bind(parent);
return boundFunc(...getSortedParameters(func, params));
}
function getSortedParameters(func, params) {
const paramNames = getFunctionParameterNames(func);
return paramNames.map(name => params[name]);
}
function getFunctionParameterNames(func) {
const fnStr = func.toString();
const paramMatch = fnStr.match(/\(([^)]*)\)/);
if (!paramMatch || !paramMatch[1]) {
return [];
}
return paramMatch[1]
.split(",")
.map(param => param.trim())
.filter(param => param);
}
async function microServiceName() {
if (!this.msName) {
var data = await (0, promises_1.readFile)(path.join(process.cwd(), 'package.json'));
const { name } = JSON.parse(data.toString());
this.msName = name;
}
return this.msName;
}
function getHttpRequestMaxSize(requestMaxSize) {
return (parseInt(requestMaxSize ?? process.env.REQUEST_MAX_SIZE ?? '100', 10) - 10) * 1024;
}
function withWatchdog(promise, timeoutMs) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new common_1.HttpException(`Gateway Timeout`, common_1.HttpStatus.GATEWAY_TIMEOUT));
}, timeoutMs);
promise
.then((result) => {
clearTimeout(timer);
resolve(result);
})
.catch((err) => {
clearTimeout(timer);
reject(err);
});
});
}
//# sourceMappingURL=utils.js.map