@sequencemedia/lsof 0.0.5 → 0.0.7

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # `@sequencemedia/lsof`
2
2
 
3
- Get a list of open files in a convenient JS structure
3
+ Get a list of open files in a convenient structure
4
4
 
5
5
  ```bash
6
6
  npm i -P @sequencemedia/lsof
package/package.json CHANGED
@@ -1,6 +1,10 @@
1
1
  {
2
2
  "name": "@sequencemedia/lsof",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
+ "keywords": [
5
+ "lsof",
6
+ "list open files"
7
+ ],
4
8
  "main": "./index.mjs",
5
9
  "type": "module",
6
10
  "author": {
@@ -11,7 +15,7 @@
11
15
  "license": "ISC",
12
16
  "repository": {
13
17
  "type": "git",
14
- "url": "git://github.com/sequencemedia/lsof.git"
18
+ "url": "git+https://github.com/sequencemedia/lsof.git"
15
19
  },
16
20
  "scripts": {
17
21
  "lint": "eslint .",
@@ -22,8 +26,8 @@
22
26
  "husky": "^9.1.7"
23
27
  },
24
28
  "devDependencies": {
25
- "@sequencemedia/eslint-config-standard": "^0.2.53",
26
- "@sequencemedia/eslint-config-typescript": "^0.1.83",
29
+ "@sequencemedia/eslint-config-standard": "^0.2.54",
30
+ "@sequencemedia/eslint-config-typescript": "^0.1.85",
27
31
  "@types/node": "^22.15.12",
28
32
  "eslint": "^9.26.0",
29
33
  "globals": "^16.0.0"
package/src/1/common.mjs CHANGED
@@ -2,6 +2,18 @@ import {
2
2
  execFile
3
3
  } from 'node:child_process'
4
4
 
5
+ export const PID = 'pid'
6
+ export const COMMAND = 'command'
7
+ export const USER = 'user'
8
+ export const FD = 'fd'
9
+ export const TYPE = 'type'
10
+ export const DEVICE = 'device'
11
+ export const SIZEOFF = 'sizeOff'
12
+ export const NODE = 'node'
13
+ export const NAME = 'name'
14
+
15
+ const LF = '\n'
16
+
5
17
  /*
6
18
  export function toCamelCase (value) {
7
19
  return value.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, v) => v.toUpperCase())
@@ -9,7 +21,7 @@ export function toCamelCase (value) {
9
21
 
10
22
  export function fromTsv (string) {
11
23
  return (
12
- string.replace(/\r/g, '\n').split('\n')
24
+ string.replace(/\r/g, LF).split(LF)
13
25
  .map((l) => l.split('\t'))
14
26
  .reduce((a, c, i, [head]) => (i) ? a.concat(Object.fromEntries(c.map((val, col) => [head[col], val]))) : a, [])
15
27
  )
@@ -49,12 +61,12 @@ function getCol (s) {
49
61
  switch (key) {
50
62
  case 'c': // will prefix the COMMAND or (Process Name) column
51
63
  return {
52
- command: value
64
+ [COMMAND]: value
53
65
  }
54
66
 
55
67
  case 'u': // will prefix the User column that the process is running under
56
68
  return {
57
- user: value
69
+ [USER]: value
58
70
  }
59
71
 
60
72
  case 'f': // will prefix the File Descriptor column
@@ -62,18 +74,18 @@ function getCol (s) {
62
74
  const fd = Number(value)
63
75
 
64
76
  return {
65
- fd: isNaN(fd) ? value : fd
77
+ [FD]: isNaN(fd) ? value : fd
66
78
  }
67
79
  }
68
80
 
69
81
  case 't': // will prefix the type column
70
82
  return {
71
- type: value
83
+ [TYPE]: value
72
84
  }
73
85
 
74
86
  case 'D': // will prefix the Device column
75
87
  return {
76
- device: value
88
+ [DEVICE]: value
77
89
  }
78
90
 
79
91
  case 's': // will prefix the SizeOff column
@@ -81,7 +93,7 @@ function getCol (s) {
81
93
  const sizeOff = Number(value)
82
94
 
83
95
  return {
84
- sizeOff: isNaN(sizeOff) ? value : sizeOff
96
+ [SIZEOFF]: isNaN(sizeOff) ? value : sizeOff
85
97
  }
86
98
  }
87
99
 
@@ -90,13 +102,13 @@ function getCol (s) {
90
102
  const node = Number(value)
91
103
 
92
104
  return {
93
- node: isNaN(node) ? value : node
105
+ [NODE]: isNaN(node) ? value : node
94
106
  }
95
107
  }
96
108
 
97
109
  case 'n': // will prefix the Name or (File Path)
98
110
  return {
99
- name: value
111
+ [NAME]: value
100
112
  }
101
113
 
102
114
  default:
@@ -115,7 +127,7 @@ function getCol (s) {
115
127
  * @returns {Set<Set<Record<string, string | number>>>}
116
128
  */
117
129
  export function getSet (value = '') {
118
- const array = value.split('\n')
130
+ const array = value.split(LF)
119
131
 
120
132
  /**
121
133
  * @type {Set<Set<Record<string, string | number>>>}
@@ -143,7 +155,7 @@ export function getSet (value = '') {
143
155
  * @returns {Array<Array<Record<string, string | number>>>}
144
156
  */
145
157
  export function getArray (value = '') {
146
- const array = value.split('\n')
158
+ const array = value.split(LF)
147
159
 
148
160
  /**
149
161
  * @type {Array<Array<Record<string, string | number>>>}
@@ -1,4 +1,7 @@
1
1
  import {
2
+ PID,
3
+ COMMAND,
4
+ USER,
2
5
  getArray
3
6
  } from './common.mjs'
4
7
 
@@ -10,21 +13,31 @@ import {
10
13
  function toEntries ({ pid, command, user }, ...args) {
11
14
  return [
12
15
  [
13
- 'pid',
16
+ PID,
14
17
  pid
15
18
  ],
16
19
  [
17
- 'command',
20
+ COMMAND,
18
21
  command
19
22
  ],
20
23
  [
21
- 'user',
24
+ USER,
22
25
  user
23
26
  ],
24
27
  ...args
25
28
  ]
26
29
  }
27
30
 
31
+ /**
32
+ * @param {Record<string, string | number>[]} inner
33
+ * @returns {Array<Record<string, string | number>>}
34
+ */
35
+ function map (inner) {
36
+ return (
37
+ inner.reduce(reduce, [{}])
38
+ )
39
+ }
40
+
28
41
  /**
29
42
  * @param {Array<Record<string, string | number>>} accumulator
30
43
  * @param {Record<string, string | number>} current
@@ -57,6 +70,6 @@ export default function toArray (value) {
57
70
  const outer = getArray(value)
58
71
 
59
72
  return (
60
- outer.map((inner) => inner.reduce(reduce, [{}]))
73
+ outer.map(map)
61
74
  )
62
75
  }
package/src/1/to-set.mjs CHANGED
@@ -1,65 +1,79 @@
1
1
  import {
2
+ PID,
3
+ COMMAND,
4
+ USER,
2
5
  getSet
3
6
  } from './common.mjs'
4
7
 
5
8
  /**
6
- * @param {string} value
7
- * @returns {Set<Set<Map<string, string | number>>>}
9
+ * @param {Map<string, string | number>} map
10
+ * @param {...Array<string | number>} args
11
+ * @returns {Array<Array<string | number>>}
8
12
  */
9
- export default function toSet (value) {
10
- const outer = getSet(value)
13
+ function toEntries (map, ...args) {
14
+ const pid = map.get(PID)
15
+ const command = map.get(COMMAND)
16
+ const user = map.get(USER)
17
+
18
+ return [
19
+ [
20
+ PID,
21
+ pid
22
+ ],
23
+ [
24
+ COMMAND,
25
+ command
26
+ ],
27
+ [
28
+ USER,
29
+ user
30
+ ],
31
+ ...args
32
+ ]
33
+ }
11
34
 
35
+ /**
36
+ * @param {Set<Map<string, string | number>>} inner
37
+ * @returns {Set<Set<Map<string, string | number>>>}
38
+ */
39
+ function map (inner) {
12
40
  return (
13
- new Set(
14
- Array
15
- .from(outer)
16
- .map((inner) => (
17
- new Set(
18
- Array
19
- .from(inner)
20
- .reduce((accumulator, current) => {
21
- const map = accumulator[accumulator.length - 1] // ?? {}
41
+ new Set(Array.from(inner).reduce(reduce, [new Map()]))
42
+ )
43
+ }
22
44
 
23
- const [
24
- [
25
- key,
26
- value
27
- ]
28
- ] = Object.entries(current)
45
+ /**
46
+ * @param {Array<Map<string, string | number>>} accumulator
47
+ * @param {Map<string, string | number>} current
48
+ * @returns {Array<Map<string, string | number>>}
49
+ */
50
+ function reduce (accumulator, current) {
51
+ const previous = accumulator[accumulator.length - 1] // ?? {}
52
+
53
+ const [
54
+ [
55
+ key,
56
+ value
57
+ ]
58
+ ] = Object.entries(current)
29
59
 
30
- if (map.has(key)) {
31
- const pid = map.get('pid')
32
- const command = map.get('command')
33
- const user = map.get('user')
60
+ if (previous.has(key)) {
61
+ accumulator.push(new Map(toEntries(previous, [key, value])))
62
+ } else {
63
+ previous.set(key, value)
64
+ }
34
65
 
35
- accumulator.push(
36
- new Map([
37
- [
38
- 'pid',
39
- pid
40
- ],
41
- [
42
- 'command',
43
- command
44
- ],
45
- [
46
- 'user',
47
- user
48
- ],
49
- [
50
- key,
51
- value
52
- ]
53
- ])
54
- )
55
- } else {
56
- map.set(key, value)
57
- }
66
+ return accumulator
67
+ }
58
68
 
59
- return accumulator
60
- }, [new Map()])
61
- )
62
- ))
63
- )
69
+ /**
70
+ * @param {string} value
71
+ * @returns {Set<Set<Map<string, string | number>>>}
72
+ */
73
+ export default function toSet (value) {
74
+ const outer = getSet(value)
75
+
76
+ return (
77
+ new Set(Array.from(outer).map(map))
64
78
  )
65
79
  }
package/src/2/common.mjs CHANGED
@@ -2,19 +2,29 @@ import {
2
2
  execFile
3
3
  } from 'node:child_process'
4
4
 
5
+ export const PID = 'pid'
6
+ export const COMMAND = 'command'
7
+ export const USER = 'user'
8
+ export const FD = 'fd'
9
+ export const TYPE = 'type'
10
+ export const DEVICE = 'device'
11
+ export const SIZEOFF = 'sizeOff'
12
+ export const NODE = 'node'
13
+ export const NAME = 'name'
14
+
5
15
  /**
6
16
  * @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
- */
17
+ * command: string,
18
+ * pid: number,
19
+ * user: string,
20
+ * fd: string | number,
21
+ * type: string,
22
+ * device: string,
23
+ * sizeOff: string | number,
24
+ * node: string | number,
25
+ * name: string
26
+ * }} LineType
27
+ */
18
28
 
19
29
  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
30
 
@@ -28,6 +38,20 @@ export function getArray (v) {
28
38
  return v.trim().split(LF).slice(1)
29
39
  }
30
40
 
41
+ /**
42
+ * @param {string} string
43
+ * @returns {number | string}
44
+ */
45
+ function transform (string) {
46
+ const number = Number(string)
47
+
48
+ return (
49
+ isNaN(number)
50
+ ? string.trim()
51
+ : number
52
+ )
53
+ }
54
+
31
55
  /**
32
56
  * @param {string} v
33
57
  * @returns {Record<string, string | number>}
@@ -49,30 +73,26 @@ export function toObject (v) {
49
73
 
50
74
  const {
51
75
  groups: {
52
- command,
53
- user,
54
- fd: FD,
55
- type,
56
- device,
57
- sizeOff: SIZEOFF,
58
- node: NODE,
59
- name
76
+ [COMMAND]: command,
77
+ [USER]: user,
78
+ [FD]: fd,
79
+ [TYPE]: type,
80
+ [DEVICE]: device,
81
+ [SIZEOFF]: sizeOff,
82
+ [NODE]: node,
83
+ [NAME]: name
60
84
  } = {}
61
85
  } = match
62
86
 
63
- const fd = Number(FD)
64
- const sizeOff = Number(SIZEOFF)
65
- const node = Number(NODE)
66
-
67
87
  return {
68
88
  pid,
69
89
  command,
70
90
  user,
71
- fd: isNaN(fd) ? FD.trim() : fd,
91
+ fd: transform(fd),
72
92
  type,
73
93
  device,
74
- sizeOff: isNaN(sizeOff) ? SIZEOFF.trim() : sizeOff,
75
- node: isNaN(node) ? NODE.trim() : node,
94
+ sizeOff: transform(sizeOff),
95
+ node: transform(node),
76
96
  name
77
97
  }
78
98
  }
@@ -3,16 +3,6 @@ import {
3
3
  toObject
4
4
  } from './common.mjs'
5
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
6
  /**
17
7
  * @param {string} value
18
8
  * @returns {Array<Record<string, string | number>>}
@@ -21,6 +11,6 @@ export default function toArray (value) {
21
11
  const array = getArray(value)
22
12
 
23
13
  return (
24
- array.map(mapObject)
14
+ array.map(toObject)
25
15
  )
26
16
  }
package/src/2/to-set.mjs CHANGED
@@ -7,9 +7,11 @@ import {
7
7
  * @param {string} value
8
8
  * @returns {Map<string, string | number>}
9
9
  */
10
- function mapMap (value) {
10
+ function toMap (value) {
11
+ const object = toObject(value)
12
+
11
13
  return (
12
- new Map(Object.entries(toObject(value)))
14
+ new Map(Object.entries(object))
13
15
  )
14
16
  }
15
17
 
@@ -21,6 +23,6 @@ export default function toSet (value) {
21
23
  const array = getArray(value)
22
24
 
23
25
  return (
24
- new Set(array.map(mapMap))
26
+ new Set(array.map(toMap))
25
27
  )
26
28
  }