UNPKG

list-open-files

Version:
167 lines 4.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("../../utils"); function rawTypeToType(rawType) { switch (rawType) { case 'IPv4': case 'IPv6': return 'IP'; } return rawType; } exports.rawTypeToType = rawTypeToType; function makeFileBase(file) { return { type: rawTypeToType(file.t), access: file.a, lock: file.l, size: file.s == null ? void 0 : utils_1.toInteger(file.s), iNode: file.i, linkCount: file.k, name: file.n, }; } exports.makeFileBase = makeFileBase; function makeFileDescriptorBase(file) { const fd = parseInt('' + file.f, 10); if ('' + fd !== '' + file.f) throw new Error(`Invalid 'f', expect number: ${file.f}`); return { fd, ...makeFileBase(file), }; } exports.makeFileDescriptorBase = makeFileDescriptorBase; function makeInternalFile(file) { return { fd: '' + file.f, ...makeFileBase(file), }; } exports.makeInternalFile = makeInternalFile; function makeFile(file) { return { ...makeFileDescriptorBase(file), majorMinorDeviceNumber: file.D == null ? void 0 : utils_1.tryToInteger(file.D, false), flags: file.G, offset: file.o == null ? void 0 : utils_1.tryToInteger(file.o, false), }; } exports.makeFile = makeFile; function makeUnknownFile(file) { return { ...makeFileDescriptorBase(file), cmd: file.c, fileStructureShareCount: file.C == null ? void 0 : utils_1.tryToInteger(file.C, false), charCode: file.d, majorMinorDeviceNumber: file.D == null ? void 0 : utils_1.tryToInteger(file.D, false), fileStructureAddress: file.F, flags: file.G, loginName: file.L, node: file.N, offset: file.o == null ? void 0 : utils_1.tryToInteger(file.o, false), protocolName: file.P, deviceNumber: file.r, streamIdentification: file.S, tcpTpiInformation: file.T, solarisZoneName: file.z, seLinuxSecurityContext: file.Z, 1: file['1'], 2: file['2'], 3: file['3'], 4: file['4'], 5: file['5'], 6: file['6'], 7: file['7'], 8: file['8'], 9: file['9'], }; } exports.makeUnknownFile = makeUnknownFile; function makeIP(file) { const parseIP = (text) => { const m = text.match(/^(.*):([^:]+)$/); if (!m) return void 0; const address = m[1]; const port = parseInt(m[2], 10); return { address, port }; }; const { n, t } = file; if (!n || !t) return; const [_from, _to] = n.includes('->') ? n.split('->') : [void 0, void 0]; const from = _from ? parseIP(_from) : void 0; const to = parseIP(_to ? _to : n); return { ...makeFileDescriptorBase(file), type: 'IP', protocol: file.P, version: parseInt(t.charAt(3), 10), from, to, }; } exports.makeIP = makeIP; function makeProcess(rawLsofProcess) { const pid = rawLsofProcess.proc.p; const ppid = rawLsofProcess.proc.R; const gpid = rawLsofProcess.proc.g; const tid = rawLsofProcess.proc.K; const user = rawLsofProcess.proc.u; const name = rawLsofProcess.proc.c; const rawCwd = rawLsofProcess.files.find(file => file.f === 'cwd'); const cwd = rawCwd ? makeInternalFile(rawCwd) : void 0; return { pid, ppid, gpid, tid, user, name, cwd, }; } exports.makeProcess = makeProcess; function rawToParsed(rawFile, pid) { try { switch (rawFile.t) { case 'IPv4': case 'IPv6': return makeIP(rawFile) || makeUnknownFile(rawFile); case 'CHR': case 'REG': case 'KQUEUE': case 'PIPE': case 'NPOLICY': case 'unix': case 'systm': case 'PSXSHM': return makeFile(rawFile); default: return makeUnknownFile(rawFile); } } catch (err) { err.pid = pid; throw err; } } function transformParts(rawLsofProcesses) { const isOrdinaryFile = (file) => ('' + file.f) === '' + (parseInt('' + file.f, 10)); return rawLsofProcesses.map(rawLsofProcess => { const process = makeProcess(rawLsofProcess); const texts = rawLsofProcess.files .filter(file => !isOrdinaryFile(file)) .map(file => makeInternalFile(file)); const files = rawLsofProcess.files .filter(file => Object.keys(file).length > 0) .filter(file => isOrdinaryFile(file)) .map(file => rawToParsed(file, process.pid)); return { process, texts, files }; }); } exports.transformParts = transformParts; //# sourceMappingURL=lsof-to-api.js.map