@webfaas/webfaas-core
Version:
WebFaaS Framework - Core
261 lines • 10.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageUtil = exports.JsonRpcErrorTypeEnum = void 0;
const WebFaasError_1 = require("../WebFaasError/WebFaasError");
var JsonRpcErrorTypeEnum;
(function (JsonRpcErrorTypeEnum) {
JsonRpcErrorTypeEnum[JsonRpcErrorTypeEnum["PARSE_ERROR"] = -32700] = "PARSE_ERROR";
JsonRpcErrorTypeEnum[JsonRpcErrorTypeEnum["INVALID_REQUEST"] = -32600] = "INVALID_REQUEST";
JsonRpcErrorTypeEnum[JsonRpcErrorTypeEnum["METHOD_NOT_FOUND"] = -32601] = "METHOD_NOT_FOUND";
JsonRpcErrorTypeEnum[JsonRpcErrorTypeEnum["INVALID_PARAMS"] = -32602] = "INVALID_PARAMS";
JsonRpcErrorTypeEnum[JsonRpcErrorTypeEnum["INTERNAL_ERROR"] = -32603] = "INTERNAL_ERROR";
JsonRpcErrorTypeEnum[JsonRpcErrorTypeEnum["SERVER_ERROR"] = -32000] = "SERVER_ERROR";
})(JsonRpcErrorTypeEnum = exports.JsonRpcErrorTypeEnum || (exports.JsonRpcErrorTypeEnum = {}));
class MessageUtil {
static parseModuleInfo(urlPath, urlBasePath) {
let result = {};
let nameAndMethod;
let version;
let lastIndexUsed = 0;
let context = MessageUtil.parseString(urlPath);
if (context.substring(0, 1) === "/") {
context = context.substring(1);
}
if (urlBasePath) {
if (urlBasePath.substring(0, 1) !== "/") {
urlBasePath = "/" + urlBasePath;
}
context = context.substring(urlBasePath.length);
}
const pathArray = context.split("/");
if (pathArray.length === 1) {
return null;
}
else if (pathArray.length === 2) {
nameAndMethod = pathArray[0];
version = pathArray[1];
lastIndexUsed = 1;
}
else {
if (pathArray[0].substring(0, 1) === "@") {
nameAndMethod = pathArray[0] + "/" + pathArray[1];
version = pathArray[2];
lastIndexUsed = 2;
}
else {
nameAndMethod = pathArray[0];
version = pathArray[1];
lastIndexUsed = 1;
}
}
let path = "";
for (let i = lastIndexUsed + 1; i < pathArray.length; i++) {
path += "/" + pathArray[i];
}
let nameAndMethodArray = nameAndMethod.split(":");
if (nameAndMethodArray.length > 1) {
result.name = nameAndMethodArray[0];
result.method = nameAndMethodArray[1];
}
else {
result.name = nameAndMethodArray[0];
result.method = "";
}
result.version = version;
result.path = path;
return result;
}
;
static parseAuthorizationHeader(value) {
let result = {};
if (value) {
let authorizationArray = MessageUtil.parseString(value).split(" ");
if (authorizationArray.length === 2) {
result.type = authorizationArray[0].toLowerCase();
result.token = authorizationArray[1];
}
else {
return undefined;
}
}
else {
return undefined;
}
return result;
}
;
static parseString(value) {
if (typeof (value) === "string") {
return value;
}
else {
return "";
}
}
;
static parseVersion(version) {
let versionArray = version.split(".");
if (versionArray.length === 1) {
return version + ".*";
}
if (versionArray.length === 2) {
return version + ".*";
}
return version;
}
static parseMessageByUrlPath(urlPath, urlBasePath, payload, http_method, http_headers) {
let msg = {};
let moduleInfo = MessageUtil.parseModuleInfo(urlPath, urlBasePath);
if (moduleInfo) {
msg.header = {};
msg.header.name = moduleInfo.name;
msg.header.method = moduleInfo.method;
msg.header.version = MessageUtil.parseVersion(moduleInfo.version);
msg.header.messageID = "";
if (http_headers) {
msg.header.messageID = MessageUtil.parseString(http_headers["X-Request-ID"]);
msg.header.authorization = MessageUtil.parseAuthorizationHeader(http_headers["Authorization"]);
}
msg.header.http = {};
msg.header.http.path = moduleInfo.path;
msg.header.http.method = (http_method || "GET").toUpperCase();
msg.header.http.headers = http_headers || null;
msg.payload = payload;
if (!msg.header.method) {
msg.header.method = msg.header.http.method.toLowerCase();
}
return msg;
}
else {
return null;
}
}
static parseJsonRpcResponseError(typeErrorOrCode, err) {
let payloadJsonRpc = {};
payloadJsonRpc.jsonrpc = "2.0";
payloadJsonRpc.error = {};
payloadJsonRpc.error.code = typeErrorOrCode;
payloadJsonRpc.error.message = err.message;
payloadJsonRpc.error.detail = Object.assign({}, err);
if (!payloadJsonRpc.error.detail.name) {
payloadJsonRpc.error.detail.name = err.name;
}
payloadJsonRpc.id = null;
return payloadJsonRpc;
}
static parseJsonRpcResponseSuccess(payload, id) {
let payloadJsonRpc = {};
payloadJsonRpc.jsonrpc = "2.0";
payloadJsonRpc.result = payload;
payloadJsonRpc.id = id;
return payloadJsonRpc;
}
static parseJsonRpcRequest(payload) {
let payloadObj;
if (!payload) {
throw new WebFaasError_1.WebFaasError.SecurityError(WebFaasError_1.WebFaasError.SecurityErrorTypeEnum.PAYLOAD_INVALID, "payload required");
}
try {
payloadObj = JSON.parse(payload.toString());
}
catch (errParse) {
throw new WebFaasError_1.WebFaasError.SecurityError(WebFaasError_1.WebFaasError.SecurityErrorTypeEnum.PAYLOAD_INVALID, errParse.message);
}
if (typeof (payloadObj.method) !== "string") {
throw new WebFaasError_1.WebFaasError.NotFoundError(WebFaasError_1.WebFaasError.NotFoundErrorTypeEnum.METHOD, "The JSON sent is not a valid Request object.");
}
return payloadObj;
}
static parseMessageByPayloadJsonRpc(payloadJsonRpcRequest, http_urlPath, http_method, http_headers) {
let msg = {};
if (payloadJsonRpcRequest && payloadJsonRpcRequest.method) {
let moduleInfo = MessageUtil.parseModuleInfo(payloadJsonRpcRequest.method, "");
if (moduleInfo) {
msg.header = {};
msg.header.name = moduleInfo.name;
msg.header.method = moduleInfo.method;
msg.header.version = MessageUtil.parseVersion(moduleInfo.version);
msg.header.messageID = (payloadJsonRpcRequest.id || "").toString();
if (http_headers) {
msg.header.authorization = MessageUtil.parseAuthorizationHeader(http_headers["Authorization"]);
}
if (http_urlPath) {
msg.header.http = {};
msg.header.http.path = http_urlPath;
msg.header.http.method = http_method || "GET";
msg.header.http.headers = http_headers || null;
}
msg.payload = payloadJsonRpcRequest.params || null;
return msg;
}
else {
return null;
}
}
else {
return null;
}
}
static convertErrorToCodeHttp(errSend) {
if (errSend instanceof WebFaasError_1.WebFaasError.ClientHttpError) {
return 502;
}
else if (errSend instanceof WebFaasError_1.WebFaasError.CompileError) {
return 501;
}
else if (errSend instanceof WebFaasError_1.WebFaasError.NotFoundError) {
return 404;
}
else if (errSend instanceof WebFaasError_1.WebFaasError.ValidateError) {
return 400;
}
else if (errSend instanceof WebFaasError_1.WebFaasError.SecurityError) {
let httpError = errSend;
let statusCode = 400;
if (httpError.type === WebFaasError_1.WebFaasError.SecurityErrorTypeEnum.MISSING_CREDENTIALS) {
statusCode = 401;
}
else if (httpError.type === WebFaasError_1.WebFaasError.SecurityErrorTypeEnum.FORBIDDEN) {
statusCode = 403;
}
else if (httpError.type === WebFaasError_1.WebFaasError.SecurityErrorTypeEnum.INVALID_CREDENTIALS) {
statusCode = 401;
}
else if (httpError.type === WebFaasError_1.WebFaasError.SecurityErrorTypeEnum.PAYLOAD_INVALID) {
statusCode = 400;
}
else if (httpError.type === WebFaasError_1.WebFaasError.SecurityErrorTypeEnum.PAYLOAD_LARGE) {
statusCode = 413;
}
else if (httpError.type === WebFaasError_1.WebFaasError.SecurityErrorTypeEnum.THROTTLED) {
statusCode = 429;
}
else if (httpError.type === WebFaasError_1.WebFaasError.SecurityErrorTypeEnum.UNCLASSIFIED) {
statusCode = 400;
}
return statusCode;
}
else {
return 500;
}
}
static convertErrorToCodeJsonRpc(errSend) {
if (errSend instanceof WebFaasError_1.WebFaasError.ClientHttpError) {
return -32600; //Invalid Request
}
else if (errSend instanceof WebFaasError_1.WebFaasError.NotFoundError) {
return -32601; //Method not found
}
else if (errSend instanceof WebFaasError_1.WebFaasError.ValidateError) {
return -32600; //Invalid Request
}
else if (errSend instanceof WebFaasError_1.WebFaasError.SecurityError) {
return -32600; //Invalid Request
}
else {
return -32000; //Server error
}
}
}
exports.MessageUtil = MessageUtil;
//# sourceMappingURL=MessageUtil.js.map