@joystick.js/cli-canary 0.0.0-canary.10 → 0.0.0-canary.12

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.
@@ -4,8 +4,8 @@ import util from "util";
4
4
  import commandExists from "command-exists";
5
5
  import child_process from "child_process";
6
6
  import { kill as killPortProcess } from "cross-port-killer";
7
- import getProcessIdFromPort from "../../getProcessIdFromPort.js";
8
- import CLILog from "../../../../lib/CLILog.js";
7
+ import getProcessIdFromPort from "../../../getProcessIdFromPort.js";
8
+ import CLILog from "../../../CLILog.js";
9
9
  const exec = util.promisify(child_process.exec);
10
10
  const getPostgreSQLProcessId = async (port = 2610) => {
11
11
  const pids = await getProcessIdFromPort(port);
@@ -1,4 +1,4 @@
1
- import connectMongoDB from "./mongodb/connect.js.js";
1
+ import connectMongoDB from "./mongodb/connect.js";
2
2
  import connectPostgreSQL from "./postgresql/connect.js";
3
3
  var providerMap_default = {
4
4
  mongodb: {
@@ -0,0 +1,60 @@
1
+ import condense from "selective-whitespace";
2
+ import netstats from "netstats";
3
+ const platform = process.platform;
4
+ function pushTo(target, item) {
5
+ if (item !== "" && typeof item === "number" && item !== 0 && target.indexOf(item) === -1) {
6
+ target.push(item);
7
+ }
8
+ }
9
+ async function processNetStats(arr) {
10
+ const pidindex = 1;
11
+ let items = arr.slice(1);
12
+ if (platform === "win32") {
13
+ items = arr;
14
+ }
15
+ const pids = {
16
+ all: [],
17
+ tcp: [],
18
+ udp: []
19
+ };
20
+ await Promise.all(items.map((item) => {
21
+ const values = condense(item).split(" ");
22
+ let pid = parseInt(values[pidindex], 10);
23
+ if (platform === "win32") {
24
+ pid = parseInt(values.pop(), 10);
25
+ }
26
+ if (values.length > 1) {
27
+ if (values.indexOf("TCP") !== -1) {
28
+ pushTo(pids.tcp, pid);
29
+ pushTo(pids.all, pid);
30
+ } else if (values.indexOf("UDP") !== -1) {
31
+ pushTo(pids.udp, pid);
32
+ pushTo(pids.all, pid);
33
+ }
34
+ }
35
+ return Promise.resolve();
36
+ }));
37
+ return pids;
38
+ }
39
+ function getProcessIdFromPort(port) {
40
+ if (typeof port !== "number") {
41
+ throw new TypeError("Expected a port number");
42
+ }
43
+ return new Promise((resolve) => {
44
+ netstats(port).then((stats) => {
45
+ processNetStats(stats).then((ps) => {
46
+ resolve(ps);
47
+ });
48
+ }).catch(() => {
49
+ resolve({
50
+ all: [],
51
+ tcp: [],
52
+ udp: []
53
+ });
54
+ });
55
+ });
56
+ }
57
+ var getProcessIdFromPort_default = getProcessIdFromPort;
58
+ export {
59
+ getProcessIdFromPort_default as default
60
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@joystick.js/cli-canary",
3
- "version": "0.0.0-canary.10",
3
+ "version": "0.0.0-canary.12",
4
4
  "type": "module",
5
5
  "description": "CLI for the Joystick JavaScript framework.",
6
6
  "main": "development.js",
@@ -4,8 +4,8 @@ import util from "util";
4
4
  import commandExists from "command-exists";
5
5
  import child_process from "child_process";
6
6
  import { kill as killPortProcess } from 'cross-port-killer';
7
- import getProcessIdFromPort from "../../getProcessIdFromPort.js";
8
- import CLILog from "../../../../lib/CLILog.js";
7
+ import getProcessIdFromPort from "../../../getProcessIdFromPort.js";
8
+ import CLILog from "../../../CLILog.js";
9
9
 
10
10
  const exec = util.promisify(child_process.exec);
11
11
 
@@ -1,4 +1,4 @@
1
- import connectMongoDB from './mongodb/connect.js.js';
1
+ import connectMongoDB from './mongodb/connect.js';
2
2
  import connectPostgreSQL from './postgresql/connect.js';
3
3
 
4
4
  export default {
@@ -0,0 +1,92 @@
1
+ import condense from 'selective-whitespace';
2
+ import netstats from 'netstats';
3
+
4
+ const platform = process.platform;
5
+
6
+ function pushTo(target, item) {
7
+ if (item !== '' && typeof item === 'number' && item !== 0 && target.indexOf(item) === -1) {
8
+ target.push(item);
9
+ }
10
+ }
11
+
12
+ // Example output to parse (macOS):
13
+ // $ lsof -i :8017
14
+ /*
15
+ COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
16
+ nc 20661 michael 3u IPv4 0x3b190d9d07c2c3db 0t0 TCP *:8017 (LISTEN)
17
+ nc 21145 michael 3u IPv4 0x3b190d9d054773db 0t0 TCP *:8017 (LISTEN)
18
+ Python 21221 michael 3u IPv4 0x3b190d9ceb8dfd7b 0t0 UDP localhost:8017
19
+ */
20
+
21
+ // [WIN] $ netstat.exe -a -n -o | findstr :9000
22
+
23
+ /*
24
+ TCP 0.0.0.0:9000 0.0.0.0:0 LISTENING 5220
25
+ TCP 127.0.0.1:9000 127.0.0.1:62376 ESTABLISHED 5220
26
+ TCP 127.0.0.1:9000 127.0.0.1:62379 ESTABLISHED 5220
27
+ TCP 127.0.0.1:62288 127.0.0.1:9000 TIME_WAIT 0
28
+ TCP 127.0.0.1:62299 127.0.0.1:9000 TIME_WAIT 0
29
+ TCP 127.0.0.1:62376 127.0.0.1:9000 ESTABLISHED 7604
30
+ TCP 127.0.0.1:62378 127.0.0.1:9000 ESTABLISHED 7604
31
+ UDP 127.0.0.1:9000 *:* 1240
32
+ */
33
+
34
+ async function processNetStats(arr) {
35
+ const pidindex = 1;
36
+ let items = arr.slice(1);
37
+
38
+ if (platform === 'win32') {
39
+ items = arr;
40
+ }
41
+
42
+ const pids = {
43
+ all: [],
44
+ tcp: [],
45
+ udp: [],
46
+ };
47
+
48
+ await Promise.all(items.map((item) => {
49
+ const values = condense(item).split(' ');
50
+ let pid = parseInt(values[pidindex], 10);
51
+
52
+ if (platform === 'win32') {
53
+ pid = parseInt(values.pop(), 10);
54
+ }
55
+
56
+ if (values.length > 1) {
57
+ if (values.indexOf('TCP') !== -1) {
58
+ pushTo(pids.tcp, pid);
59
+ pushTo(pids.all, pid);
60
+ } else if (values.indexOf('UDP') !== -1) {
61
+ pushTo(pids.udp, pid);
62
+ pushTo(pids.all, pid);
63
+ }
64
+ }
65
+
66
+ return Promise.resolve();
67
+ }));
68
+
69
+ return pids;
70
+ }
71
+
72
+ function getProcessIdFromPort(port) {
73
+ if (typeof port !== 'number') {
74
+ throw new TypeError('Expected a port number');
75
+ }
76
+
77
+ return new Promise(((resolve) => {
78
+ netstats(port).then((stats) => {
79
+ processNetStats(stats).then((ps) => {
80
+ resolve(ps);
81
+ });
82
+ }).catch(() => {
83
+ resolve({
84
+ all: [],
85
+ tcp: [],
86
+ udp: [],
87
+ });
88
+ });
89
+ }));
90
+ }
91
+
92
+ export default getProcessIdFromPort;