@openevstack/ocpp-rpc
Version:
⚡ A lightweight, production-ready RPC server built with Express and WebSocket for handling OCPP-based EV charger communication. Part of the OpenEVStack ecosystem.
74 lines (73 loc) • 3.19 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRPCError = exports.getErrorPlainObject = exports.getPackageIdent = void 0;
const errors = __importStar(require("../errors"));
const package_json_1 = __importDefault(require("../../package.json"));
// Define the error lookup type more strictly
const rpcErrorLUT = {
GenericError: errors.RPCGenericError,
NotImplemented: errors.RPCNotImplementedError,
NotSupported: errors.RPCNotSupportedError,
InternalError: errors.RPCInternalError,
ProtocolError: errors.RPCProtocolError,
SecurityError: errors.RPCSecurityError,
FormationViolation: errors.RPCFormationViolationError,
FormatViolation: errors.RPCFormatViolationError,
PropertyConstraintViolation: errors.RPCPropertyConstraintViolationError,
OccurenceConstraintViolation: errors.RPCOccurenceConstraintViolationError,
OccurrenceConstraintViolation: errors.RPCOccurrenceConstraintViolationError,
TypeConstraintViolation: errors.RPCTypeConstraintViolationError,
MessageTypeNotSupported: errors.RPCMessageTypeNotSupportedError,
RpcFrameworkError: errors.RPCFrameworkError,
};
function getPackageIdent() {
return `${package_json_1.default.name}/${package_json_1.default.version} (${process.platform})`;
}
exports.getPackageIdent = getPackageIdent;
function getErrorPlainObject(e) {
try {
// Serialize including non-enumerable properties
return JSON.parse(JSON.stringify(e, Object.getOwnPropertyNames(e)));
}
catch (error) {
return {
stack: error.stack,
message: error.message,
};
}
}
exports.getErrorPlainObject = getErrorPlainObject;
function createRPCError(type, message, details) {
var _a;
const E = (_a = rpcErrorLUT[type]) !== null && _a !== void 0 ? _a : errors.RPCGenericError;
const err = new E(message !== null && message !== void 0 ? message : "");
err.details = details;
return err;
}
exports.createRPCError = createRPCError;