@sequencemedia/lsof 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/1.d.mts ADDED
@@ -0,0 +1,15 @@
1
+ export {
2
+ getLsof
3
+ } from './src/1/common.mjs'
4
+
5
+ export {
6
+ default as toSet
7
+ } from './src/1/to-set.mjs'
8
+
9
+ export {
10
+ default as toArray
11
+ } from './src/1/to-array.mjs'
12
+
13
+ export function getLsofSet (): Promise<Set<Set<Map<string, string | number>>>>
14
+
15
+ export function getLsofArray (): Promise<Array<Array<Record<string, string | number>>>>
package/1.mjs ADDED
@@ -0,0 +1,41 @@
1
+ import {
2
+ getLsof
3
+ } from './src/1/common.mjs'
4
+
5
+ import toSet from './src/1/to-set.mjs'
6
+
7
+ import toArray from './src/1/to-array.mjs'
8
+
9
+ export {
10
+ getLsof
11
+ }
12
+
13
+ export {
14
+ toSet
15
+ }
16
+
17
+ export {
18
+ toArray
19
+ }
20
+
21
+ /**
22
+ * @returns {Promise<Set<Set<Map<string, string | number>>>>}
23
+ */
24
+ export async function getLsofSet () {
25
+ return (
26
+ toSet(
27
+ await getLsof()
28
+ )
29
+ )
30
+ }
31
+
32
+ /**
33
+ * @returns {Promise<Array<Array<Record<string, string | number>>>>}
34
+ */
35
+ export async function getLsofArray () {
36
+ return (
37
+ toArray(
38
+ await getLsof()
39
+ )
40
+ )
41
+ }
package/2.d.mts ADDED
@@ -0,0 +1,15 @@
1
+ export {
2
+ getLsof
3
+ } from './src/2/common.mjs'
4
+
5
+ export {
6
+ default as toSet
7
+ } from './src/2/to-set.mjs'
8
+
9
+ export {
10
+ default as toArray
11
+ } from './src/2/to-array.mjs'
12
+
13
+ export function getLsofSet (): Promise<Set<Map<string, string | number>>>
14
+
15
+ export function getLsofArray (): Promise<Array<Record<string, string | number>>>
package/2.mjs ADDED
@@ -0,0 +1,35 @@
1
+ import {
2
+ getLsof
3
+ } from './src/2/common.mjs'
4
+
5
+ import toSet from './src/2/to-set.mjs'
6
+
7
+ import toArray from './src/2/to-array.mjs'
8
+
9
+ export {
10
+ getLsof
11
+ }
12
+
13
+ export {
14
+ toSet
15
+ }
16
+
17
+ export {
18
+ toArray
19
+ }
20
+
21
+ export async function getLsofArray () {
22
+ return (
23
+ toArray(
24
+ await getLsof()
25
+ )
26
+ )
27
+ }
28
+
29
+ export async function getLsofSet () {
30
+ return (
31
+ toSet(
32
+ await getLsof()
33
+ )
34
+ )
35
+ }
package/README.md CHANGED
@@ -8,7 +8,7 @@ npm i -P @sequencemedia/lsof
8
8
 
9
9
  ## Usage
10
10
 
11
- Get `lsof` output as a string
11
+ Get `lsof` as a string
12
12
 
13
13
  ```javascript
14
14
  import {
package/index.d.mts CHANGED
@@ -1,15 +1 @@
1
- export {
2
- getLsof
3
- } from './src/common.mjs'
4
-
5
- export {
6
- toSet
7
- } from './src/to-set.mjs'
8
-
9
- export {
10
- toArray
11
- } from './src/to-array.mjs'
12
-
13
- export function getLsofSet (): Promise<Set<Set<Map<string, string | number>>>>
14
-
15
- export function getLsofArray (): Promise<Array<Array<Record<string, string | number>>>>
1
+ export type * from './2.d.mts'
package/index.mjs CHANGED
@@ -1,45 +1 @@
1
- import {
2
- getLsof
3
- } from './src/common.mjs'
4
-
5
- import {
6
- toSet
7
- } from './src/to-set.mjs'
8
-
9
- import {
10
- toArray
11
- } from './src/to-array.mjs'
12
-
13
- export {
14
- getLsof
15
- }
16
-
17
- export {
18
- toSet
19
- }
20
-
21
- export {
22
- toArray
23
- }
24
-
25
- /**
26
- * @returns {Promise<Set<Set<Map<string, string | number>>>>}
27
- */
28
- export async function getLsofSet () {
29
- return (
30
- toSet(
31
- await getLsof()
32
- )
33
- )
34
- }
35
-
36
- /**
37
- * @returns {Promise<Array<Array<Record<string, string | number>>>>}
38
- */
39
- export async function getLsofArray () {
40
- return (
41
- toArray(
42
- await getLsof()
43
- )
44
- )
45
- }
1
+ export * from './2.mjs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sequencemedia/lsof",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "main": "./index.mjs",
5
5
  "type": "module",
6
6
  "author": {
@@ -22,10 +22,15 @@
22
22
  "husky": "^9.1.7"
23
23
  },
24
24
  "devDependencies": {
25
- "@sequencemedia/eslint-config-standard": "^0.2.52",
26
- "@sequencemedia/eslint-config-typescript": "^0.1.82",
27
- "@types/node": "^22.15.3",
25
+ "@sequencemedia/eslint-config-standard": "^0.2.53",
26
+ "@sequencemedia/eslint-config-typescript": "^0.1.83",
27
+ "@types/node": "^22.15.12",
28
28
  "eslint": "^9.26.0",
29
29
  "globals": "^16.0.0"
30
+ },
31
+ "exports": {
32
+ ".": "./index.mjs",
33
+ "./1": "./1.mjs",
34
+ "./2": "./2.mjs"
30
35
  }
31
36
  }
@@ -1,5 +1,5 @@
1
1
  import {
2
- exec
2
+ execFile
3
3
  } from 'node:child_process'
4
4
 
5
5
  /*
@@ -22,7 +22,7 @@ export function fromTsv (string) {
22
22
  */
23
23
  function getPid (s) {
24
24
  return {
25
- PROCESS: Number(s.slice(1))
25
+ pid: Number(s.slice(1))
26
26
  }
27
27
  }
28
28
 
@@ -49,48 +49,64 @@ function getCol (s) {
49
49
  switch (key) {
50
50
  case 'c': // will prefix the COMMAND or (Process Name) column
51
51
  return {
52
- COMMAND: value
52
+ command: value
53
53
  }
54
54
 
55
55
  case 'u': // will prefix the User column that the process is running under
56
56
  return {
57
- USER: value
57
+ user: value
58
58
  }
59
59
 
60
60
  case 'f': // will prefix the File Descriptor column
61
+ {
62
+ const fd = Number(value)
63
+
61
64
  return {
62
- 'FILE DESCRIPTOR': value
65
+ fd: isNaN(fd) ? value : fd
63
66
  }
67
+ }
64
68
 
65
69
  case 't': // will prefix the type column
66
70
  return {
67
- TYPE: value
71
+ type: value
68
72
  }
69
73
 
70
74
  case 'D': // will prefix the Device column
71
75
  return {
72
- DEVICE: value
76
+ device: value
73
77
  }
74
78
 
75
79
  case 's': // will prefix the SizeOff column
80
+ {
81
+ const sizeOff = Number(value)
82
+
76
83
  return {
77
- SIZE: Number(value)
84
+ sizeOff: isNaN(sizeOff) ? value : sizeOff
78
85
  }
86
+ }
79
87
 
80
88
  case 'i': // will prefix the Node column
89
+ {
90
+ const node = Number(value)
91
+
81
92
  return {
82
- NODE: Number(value)
93
+ node: isNaN(node) ? value : node
83
94
  }
95
+ }
84
96
 
85
97
  case 'n': // will prefix the Name or (File Path)
86
98
  return {
87
- 'FILE PATH': value
99
+ name: value
88
100
  }
89
101
 
90
102
  default:
103
+ {
104
+ const number = Number(value)
105
+
91
106
  return {
92
- [key]: value
107
+ [key]: isNaN(number) ? value : number
93
108
  }
109
+ }
94
110
  }
95
111
  }
96
112
 
@@ -169,27 +185,7 @@ export function getLsof () {
169
185
  return (!e) ? resolve(v.trim()) : reject(e)
170
186
  }
171
187
 
172
- exec('lsof -F pcuftDsin', OPTIONS, complete)
173
- })
174
- )
175
- }
176
-
177
- /**
178
- * @returns {Promise<string>}
179
- */
180
- export function lsof () {
181
- return (
182
- new Promise((resolve, reject) => {
183
- /**
184
- * @param {any} [e]
185
- * @param {string} [v]
186
- * @returns {void}
187
- */
188
- function complete (e, v = '') {
189
- return (!e) ? resolve(v.trim()) : reject(e)
190
- }
191
-
192
- exec('lsof', OPTIONS, complete)
188
+ execFile('lsof', ['-F', 'pcuftDsin'], OPTIONS, complete)
193
189
  })
194
190
  )
195
191
  }
@@ -0,0 +1 @@
1
+ export default function toArray (value: string): Array<Array<Record<string, string | number>>>
@@ -7,19 +7,19 @@ import {
7
7
  * @param {...Array<string | number>} args
8
8
  * @returns {Array<Array<string | number>>}
9
9
  */
10
- function toEntries ({ PROCESS, COMMAND, USER }, ...args) {
10
+ function toEntries ({ pid, command, user }, ...args) {
11
11
  return [
12
12
  [
13
- 'PROCESS',
14
- PROCESS
13
+ 'pid',
14
+ pid
15
15
  ],
16
16
  [
17
- 'COMMAND',
18
- COMMAND
17
+ 'command',
18
+ command
19
19
  ],
20
20
  [
21
- 'USER',
22
- USER
21
+ 'user',
22
+ user
23
23
  ],
24
24
  ...args
25
25
  ]
@@ -53,7 +53,7 @@ function reduce (accumulator, current) {
53
53
  * @param {string} value
54
54
  * @returns {Array<Array<Record<string, string | number>>>}
55
55
  */
56
- export function toArray (value) {
56
+ export default function toArray (value) {
57
57
  const outer = getArray(value)
58
58
 
59
59
  return (
@@ -0,0 +1 @@
1
+ export default function toSet (value: string): Set<Set<Map<string, string | number>>>
@@ -6,7 +6,7 @@ import {
6
6
  * @param {string} value
7
7
  * @returns {Set<Set<Map<string, string | number>>>}
8
8
  */
9
- export function toSet (value) {
9
+ export default function toSet (value) {
10
10
  const outer = getSet(value)
11
11
 
12
12
  return (
@@ -28,22 +28,22 @@ export function toSet (value) {
28
28
  ] = Object.entries(current)
29
29
 
30
30
  if (map.has(key)) {
31
- const process = map.get('PROCESS')
32
- const command = map.get('COMMAND')
33
- const user = map.get('USER')
31
+ const pid = map.get('pid')
32
+ const command = map.get('command')
33
+ const user = map.get('user')
34
34
 
35
35
  accumulator.push(
36
36
  new Map([
37
37
  [
38
- 'PROCESS',
39
- process
38
+ 'pid',
39
+ pid
40
40
  ],
41
41
  [
42
- 'COMMAND',
42
+ 'command',
43
43
  command
44
44
  ],
45
45
  [
46
- 'USER',
46
+ 'user',
47
47
  user
48
48
  ],
49
49
  [
@@ -0,0 +1,102 @@
1
+ import {
2
+ execFile
3
+ } from 'node:child_process'
4
+
5
+ /**
6
+ * @typedef {{
7
+ * command: string,
8
+ * pid: number,
9
+ * user: string,
10
+ * fd: string | number,
11
+ * type: string,
12
+ * device: string,
13
+ * sizeOff: string | number,
14
+ * node: string | number,
15
+ * name: string
16
+ * }} LineType
17
+ */
18
+
19
+ const PATTERN = /^\s*(?<command>[!-~]+)\s+(?<pid>\d+)\s+(?<user>\w+)\s+(?<fd>\s+|\w+)\s+(?<type>\s+|\w+)\s+(?<device>\s+|\d+,\d+|0x[\w\d]*)\s+(?<sizeOff>\s+|\w+|\d+)\s+(?<node>\s+|\d+)\s+(?<name>.*)/
20
+
21
+ const LF = '\n'
22
+
23
+ /**
24
+ * @param {string} v
25
+ * @returns {string[]}
26
+ */
27
+ export function getArray (v) {
28
+ return v.trim().split(LF).slice(1)
29
+ }
30
+
31
+ /**
32
+ * @param {string} v
33
+ * @returns {Record<string, string | number>}
34
+ */
35
+ export function toObject (v) {
36
+ const match = PATTERN.exec(v)
37
+
38
+ if (!match) throw new Error('Parsing failed.')
39
+
40
+ const {
41
+ groups: {
42
+ pid: PID
43
+ } = {}
44
+ } = match
45
+
46
+ const pid = Number(PID)
47
+
48
+ if (isNaN(pid)) throw new Error('Invalid pid.')
49
+
50
+ const {
51
+ groups: {
52
+ command,
53
+ user,
54
+ fd: FD,
55
+ type,
56
+ device,
57
+ sizeOff: SIZEOFF,
58
+ node: NODE,
59
+ name
60
+ } = {}
61
+ } = match
62
+
63
+ const fd = Number(FD)
64
+ const sizeOff = Number(SIZEOFF)
65
+ const node = Number(NODE)
66
+
67
+ return {
68
+ pid,
69
+ command,
70
+ user,
71
+ fd: isNaN(fd) ? FD.trim() : fd,
72
+ type,
73
+ device,
74
+ sizeOff: isNaN(sizeOff) ? SIZEOFF.trim() : sizeOff,
75
+ node: isNaN(node) ? NODE.trim() : node,
76
+ name
77
+ }
78
+ }
79
+
80
+ const OPTIONS = {
81
+ maxBuffer: Infinity
82
+ }
83
+
84
+ /**
85
+ * @returns {Promise<string>}
86
+ */
87
+ export function getLsof () {
88
+ return (
89
+ new Promise((resolve, reject) => {
90
+ /**
91
+ * @param {any} [e]
92
+ * @param {string} [v]
93
+ * @returns {void}
94
+ */
95
+ function complete (e, v = '') {
96
+ return (!e) ? resolve(v.trim()) : reject(e)
97
+ }
98
+
99
+ execFile('lsof', OPTIONS, complete)
100
+ })
101
+ )
102
+ }
@@ -0,0 +1 @@
1
+ export default function toArray (value: string): Array<Record<string, string | number>>
@@ -0,0 +1,26 @@
1
+ import {
2
+ getArray,
3
+ toObject
4
+ } from './common.mjs'
5
+
6
+ /**
7
+ * @param {string} value
8
+ * @returns {Record<string, string | number>}
9
+ */
10
+ function mapObject (value) {
11
+ return (
12
+ toObject(value)
13
+ )
14
+ }
15
+
16
+ /**
17
+ * @param {string} value
18
+ * @returns {Array<Record<string, string | number>>}
19
+ */
20
+ export default function toArray (value) {
21
+ const array = getArray(value)
22
+
23
+ return (
24
+ array.map(mapObject)
25
+ )
26
+ }
@@ -0,0 +1 @@
1
+ export default function toSet (value: string): Set<Map<string, string | number>>
@@ -0,0 +1,26 @@
1
+ import {
2
+ getArray,
3
+ toObject
4
+ } from './common.mjs'
5
+
6
+ /**
7
+ * @param {string} value
8
+ * @returns {Map<string, string | number>}
9
+ */
10
+ function mapMap (value) {
11
+ return (
12
+ new Map(Object.entries(toObject(value)))
13
+ )
14
+ }
15
+
16
+ /**
17
+ * @param {string} value
18
+ * @returns {Set<Map<string, string | number>>}
19
+ */
20
+ export default function toSet (value) {
21
+ const array = getArray(value)
22
+
23
+ return (
24
+ new Set(array.map(mapMap))
25
+ )
26
+ }
@@ -1 +0,0 @@
1
- export function toArray (value: string): Array<Array<Record<string, string | number>>>
package/src/to-set.d.mts DELETED
@@ -1 +0,0 @@
1
- export function toSet (value: string): Set<Set<Map<string, string | number>>>