UNPKG

@cliz/inlets

Version:
170 lines (169 loc) 5.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Response = exports.Request = exports.createLogCommand = exports.timestamps = exports.decrypt = exports.encrypt = exports.dataProcessor = void 0; const aes_1 = require("@zodash/aes"); const data_processor_1 = require("./data-processor"); exports.dataProcessor = data_processor_1.default; const debug = require('debug')('inlets:utils'); function getKeyAndIVFromSecret(secret) { const _secret = Buffer.from(secret).toString('hex'); const iv = _secret.slice(0, 16); const key = _secret.slice(_secret.length - 32); return { iv, key, }; } function encrypt(secret, data) { debug('secret:', secret); debug('hex:', Buffer.from(secret).toString('hex')); const { key, iv } = getKeyAndIVFromSecret(secret); return aes_1.default.encrypt('aes-256-cfb', key, iv, data); } exports.encrypt = encrypt; function decrypt(secret, data) { const { key, iv } = getKeyAndIVFromSecret(secret); return aes_1.default.decrypt('aes-256-cfb', key, iv, data); } exports.decrypt = decrypt; function timestamps() { return Date.now(); } exports.timestamps = timestamps; function createLogCommand(logger, verbose) { let cmd = ''; return (_cmd) => { if (verbose) { if (_cmd != '\r' && !/[\r\n]/.test(_cmd)) { cmd += _cmd; } else { if (cmd != '') { logger.info('cmd:', cmd); cmd = ''; } else { logger.info('cmd:', _cmd); } } } }; } exports.createLogCommand = createLogCommand; class Request { constructor(rawData) { this.rawData = rawData; this.headers = {}; this.isValid = false; const [first, ...rest] = rawData.split('\r\n'); if (this.isHTTP(first)) { this.parseMethodPath(first); this.parseHeadersAndBody(rest); this.isValid = true; } else { this.rawBody += rawData; } } isHTTP(first) { return first.split(' ').length === 3; } parseMethodPath(firstLine) { const [method, path, httpVersion] = firstLine.split(' '); this.method = method; this.path = path; this.httpVersion = httpVersion; } parseHeadersAndBody(lines) { const headers = {}; for (const index in lines) { const line = lines[index]; if (line === '') { this.rawBody = lines.slice(+index + 1).join(''); break; } const kv = line.split(':'); const key = kv[0].trim().toLowerCase(); const value = kv[1].trim(); if (!headers[key]) { headers[key] = value; } else { if (!Array.isArray(headers[key])) { headers[key] = [headers[key]]; } headers[key].push(value); } } this.headers = headers; } get(headerKey, defaultValue) { var _a, _b, _c; const _key = headerKey.toLowerCase(); return (_c = (_b = (_a = this.headers) === null || _a === void 0 ? void 0 : _a[_key]) !== null && _b !== void 0 ? _b : defaultValue) !== null && _c !== void 0 ? _c : ''; } getRequestLog(long) { if (!this.isValid) { return `@TODO this is not request start, is chunked, cannot solve currently`; } if (!long) { return `${this.method} ${this.path}`; } return `${this.get('x-real-ip', '-')} - "${this.method} ${this.path}" "${this.get('user-agent')}"`; } } exports.Request = Request; class Response { constructor(rawData) { this.rawData = rawData; this.headers = {}; this.isValid = false; const [first, ...rest] = rawData.split('\r\n'); if (this.isHTTP(first)) { this.parseMethodPath(first); this.parseHeadersAndBody(rest); this.isValid = true; } else { this.rawBody += rawData; } } isHTTP(first) { return /^HTTP/i.test(first); } parseMethodPath(firstLine) { const [httpVersion, status, ...statusTextArr] = firstLine.split(' '); this.httpVersion = httpVersion; this.status = +status; this.statusText = statusTextArr.join(' '); } parseHeadersAndBody(lines) { const headers = {}; for (const index in lines) { const line = lines[index]; if (line === '') { this.rawBody = lines.slice(+index + 1).join(''); break; } const kv = line.split(':'); const key = kv[0].trim().toLowerCase(); const value = kv[1].trim(); if (!headers[key]) { headers[key] = value; } else { if (!Array.isArray(headers[key])) { headers[key] = [headers[key]]; } headers[key].push(value); } } this.headers = headers; } get(headerKey, defaultValue) { var _a, _b, _c; const _key = headerKey.toLowerCase(); return (_c = (_b = (_a = this.headers) === null || _a === void 0 ? void 0 : _a[_key]) !== null && _b !== void 0 ? _b : defaultValue) !== null && _c !== void 0 ? _c : ''; } } exports.Response = Response;