UNPKG

proxy-protocol-js

Version:

A PROXY protocol builder and parser for JavaScript

42 lines 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.V1ProxyProtocol = exports.V1ProxyProtocolParseError = void 0; var INETProtocol_1 = require("./enum/INETProtocol"); var Peer_1 = require("./Peer"); var V1ProxyProtocolParseError = (function () { function V1ProxyProtocolParseError(message) { this.message = message; this.name = this.constructor.name; } return V1ProxyProtocolParseError; }()); exports.V1ProxyProtocolParseError = V1ProxyProtocolParseError; var V1ProxyProtocol = (function () { function V1ProxyProtocol(inetProtocol, source, destination, data) { this.inetProtocol = inetProtocol; this.source = source; this.destination = destination; this.data = data; } V1ProxyProtocol.prototype.build = function () { return "PROXY ".concat(this.inetProtocol, " ").concat(this.source.ipAddress, " ").concat(this.destination.ipAddress, " ").concat(this.source.port, " ").concat(this.destination.port, "\r\n").concat(this.data ? this.data : ''); }; V1ProxyProtocol.parse = function (input) { var matched = V1ProxyProtocol.v1ProxyProtocolRegexp.exec(input); if (!matched) { throw new V1ProxyProtocolParseError("given data isn't suitable for V1 PROXY protocols definition"); } return new V1ProxyProtocol(INETProtocol_1.INETProtocol[matched[1]], new Peer_1.Peer(matched[2], Number(matched[4])), new Peer_1.Peer(matched[3], Number(matched[5])), matched[6]); }; V1ProxyProtocol.isValidProtocolSignature = function (input) { return input.startsWith(V1ProxyProtocol.protocolSignature); }; V1ProxyProtocol.protocolSignature = 'PROXY'; V1ProxyProtocol.v1ProxyProtocolRegexp = (function () { var inetProtoMatcher = Object.keys(INETProtocol_1.INETProtocol).join('|'); return new RegExp("^".concat(V1ProxyProtocol.protocolSignature, " (").concat(inetProtoMatcher, ") ([^ ]+) ([^ ]+) ([0-9]+) ([0-9]+)\r\n(.*)"), 's'); })(); return V1ProxyProtocol; }()); exports.V1ProxyProtocol = V1ProxyProtocol; //# sourceMappingURL=V1ProxyProtocol.js.map