@gjsify/os 0.0.1 → 0.0.3

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.
@@ -1,17 +1,51 @@
1
- const createSubnet = require('./createSubnet');
2
- const system = require('./system.js');
1
+ import { createSubnet } from './createSubnet.js';
2
+ import { cli } from '@gjsify/utils';
3
+
3
4
  const EOL = /\r\n|\n/;
4
5
 
5
6
  const getIPv4Subnet = createSubnet(32, 8, 10, '.');
6
7
  const getIPv6Subnet = createSubnet(128, 16, 16, ':');
7
8
 
8
- this.cpus = () => {
9
+ function parseInterfaces(info) {
10
+ info = info.trim();
11
+ if (info.length < 1) return;
12
+ let iface = [], mac;
13
+ for (let
14
+ line,
15
+ lines = info.split(EOL),
16
+ i = 0; i < lines.length; i++
17
+ ) {
18
+ line = lines[i];
19
+ switch (true) {
20
+ case /link\/\S+\s+((?:\S{2}:)+\S{2})/.test(line):
21
+ mac = RegExp.$1;
22
+ break;
23
+ case /inet(\d*)\s+(\S+)/.test(line):
24
+ let
25
+ ip = RegExp.$2.split('/'),
26
+ v = RegExp.$1 || '4'
27
+ ;
28
+ iface.push({
29
+ address: ip[0],
30
+ netmask: (v === '4' ? getIPv4Subnet : getIPv6Subnet)(ip[1]),
31
+ family: 'IPv' + v,
32
+ mac: mac,
33
+ internal: ip[0] === '127.0.0.1'
34
+ });
35
+ break;
36
+ }
37
+ }
38
+ if (mac) this[info.slice(0, info.indexOf(':'))] = iface;
39
+ };
40
+
41
+ // PORTED TO deno runtime
42
+ export const cpus = () => {
9
43
  const PROCESSOR = /^processor\s*:\s*(\d+)/i;
10
44
  const NAME = /^model[\s_]+name\s*:([^\r\n]+)/i;
11
45
  const FREQ = /^cpu[\s_]+MHz\s*:\s*(\d+)/i;
12
46
  const cpus = [];
13
- let cpu;
14
- system('cat /proc/cpuinfo').split(EOL).forEach(line => {
47
+ let cpu: { model: string, speed: number, times: {} };
48
+ cli('cat /proc/cpuinfo').split(EOL).forEach(line => {
15
49
  switch (true) {
16
50
  case PROCESSOR.test(line):
17
51
  cpus[RegExp.$1.trim()] = (cpu = {
@@ -31,69 +65,41 @@ this.cpus = () => {
31
65
  return cpus;
32
66
  };
33
67
 
34
- this.endianness = () => 'LE';
68
+ export const endianness = () => 'LE';
35
69
 
36
- this.freemem = () => {
37
- let I, mem = system('free -b').split(EOL);
70
+ // PORTED TO deno runtime
71
+ export const freemem = () => {
72
+ let I, mem = cli('free -b').split(EOL);
38
73
  mem[0].split(/\s+/).some((info, i) => info === 'free' && (I = i));
39
74
  return parseFloat(mem[1].split(/\s+/)[I + 1]);
40
75
  };
41
76
 
42
- this.loadavg = () =>
77
+ // PORTED TO deno runtime
78
+ export const loadavg = () =>
43
79
  /(\d+(?:\.\d+))\s+(\d+(?:\.\d+))\s+(\d+(?:\.\d+))/.test(
44
- system('cat /proc/loadavg')
80
+ cli('cat /proc/loadavg')
45
81
  ) && [
46
82
  parseFloat(RegExp.$1),
47
83
  parseFloat(RegExp.$2),
48
84
  parseFloat(RegExp.$3)
49
85
  ];
50
86
 
51
- this.networkInterfaces = () => {
87
+ export const networkInterfaces = () => {
52
88
  const ifaces = {};
53
- system('ip addr').split(/^\d+:\s+/m).forEach(parseInterfaces, ifaces);
89
+ cli('ip addr').split(/^\d+:\s+/m).forEach(parseInterfaces, ifaces);
54
90
  return ifaces;
55
91
  };
56
92
 
57
- function parseInterfaces(info) {
58
- info = info.trim();
59
- if (info.length < 1) return;
60
- let iface = [], mac;
61
- for (let
62
- line,
63
- lines = info.split(EOL),
64
- i = 0; i < lines.length; i++
65
- ) {
66
- line = lines[i];
67
- switch (true) {
68
- case /link\/\S+\s+((?:\S{2}:)+\S{2})/.test(line):
69
- mac = RegExp.$1;
70
- break;
71
- case /inet(\d*)\s+(\S+)/.test(line):
72
- let
73
- ip = RegExp.$2.split('/'),
74
- v = RegExp.$1 || '4'
75
- ;
76
- iface.push({
77
- address: ip[0],
78
- netmask: (v === '4' ? getIPv4Subnet : getIPv6Subnet)(ip[1]),
79
- family: 'IPv' + v,
80
- mac: mac,
81
- internal: ip[0] === '127.0.0.1'
82
- });
83
- break;
84
- }
85
- }
86
- if (mac) this[info.slice(0, info.indexOf(':'))] = iface;
87
- };
88
-
89
- this.totalmem = () => {
90
- let I, mem = system('free -b').split(EOL);
93
+ // PORTED TO deno runtime
94
+ export const totalmem = () => {
95
+ let I, mem = cli('free -b').split(EOL);
91
96
  mem[0].split(/\s+/).some((info, i) => info === 'total' && (I = i));
92
97
  return parseFloat(mem[1].split(/\s+/)[I + 1]);
93
98
  };
94
99
 
95
- this.uptime = () => (
100
+ // PORTED TO deno runtime
101
+ export const uptime = () => (
96
102
  Date.now() -
97
- Date.parse(system('uptime -s').replace(' ', 'T'))
103
+ Date.parse(cli('uptime -s').replace(' ', 'T'))
98
104
  ) / 1000;
99
105
 
package/src/test.mts ADDED
@@ -0,0 +1,4 @@
1
+
2
+ import { run } from '@gjsify/unit';
3
+ import testSuite from './index.spec.js';
4
+ run({testSuite});