@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.
@@ -0,0 +1,92 @@
1
+ import { cli, getPathSeparator, getOs } from "@gjsify/utils";
2
+ import * as linux from "./linux.js";
3
+ import * as darwin from "./darwin.js";
4
+ import GLib from "@girs/glib-2.0";
5
+ import constants from "./constants.js";
6
+ const EOL = getPathSeparator() === "/" ? "\n" : "\r\n";
7
+ const homedir = () => GLib.get_home_dir();
8
+ const hostname = () => GLib.get_host_name();
9
+ const release = () => cli("uname -r");
10
+ const tmpdir = () => GLib.get_tmp_dir();
11
+ const type = () => cli("uname");
12
+ const userInfo = () => ({
13
+ uid: 1e3,
14
+ gid: 100,
15
+ username: GLib.get_user_name(),
16
+ homedir: GLib.get_home_dir()
17
+ });
18
+ const cpus = () => {
19
+ const _os = getOs();
20
+ switch (_os) {
21
+ case "darwin":
22
+ return darwin.cpus();
23
+ case "linux":
24
+ return linux.cpus();
25
+ default:
26
+ console.warn(`${_os} is not supported!`);
27
+ break;
28
+ }
29
+ };
30
+ const endianness = () => {
31
+ const _os = getOs();
32
+ switch (_os) {
33
+ case "darwin":
34
+ return darwin.endianness();
35
+ case "linux":
36
+ return linux.endianness();
37
+ default:
38
+ console.warn(`${_os} is not supported!`);
39
+ break;
40
+ }
41
+ };
42
+ const freemem = () => {
43
+ const _os = getOs();
44
+ switch (_os) {
45
+ case "darwin":
46
+ return darwin.freemem();
47
+ case "linux":
48
+ return linux.freemem();
49
+ default:
50
+ console.warn(`${_os} is not supported!`);
51
+ break;
52
+ }
53
+ };
54
+ const loadavg = () => {
55
+ const _os = getOs();
56
+ switch (_os) {
57
+ case "darwin":
58
+ return darwin.loadavg();
59
+ case "linux":
60
+ return linux.loadavg();
61
+ default:
62
+ console.warn(`${_os} is not supported!`);
63
+ break;
64
+ }
65
+ };
66
+ const networkInterfaces = () => {
67
+ const _os = getOs();
68
+ switch (_os) {
69
+ case "darwin":
70
+ return darwin.networkInterfaces();
71
+ case "linux":
72
+ return linux.networkInterfaces();
73
+ default:
74
+ console.warn(`${_os} is not supported!`);
75
+ break;
76
+ }
77
+ };
78
+ export {
79
+ EOL,
80
+ constants,
81
+ cpus,
82
+ endianness,
83
+ freemem,
84
+ homedir,
85
+ hostname,
86
+ loadavg,
87
+ networkInterfaces,
88
+ release,
89
+ tmpdir,
90
+ type,
91
+ userInfo
92
+ };
@@ -0,0 +1,92 @@
1
+ import { createSubnet } from "./createSubnet.js";
2
+ import { cli } from "@gjsify/utils";
3
+ const EOL = /\r\n|\n/;
4
+ const getIPv4Subnet = createSubnet(32, 8, 10, ".");
5
+ const getIPv6Subnet = createSubnet(128, 16, 16, ":");
6
+ function parseInterfaces(info) {
7
+ info = info.trim();
8
+ if (info.length < 1)
9
+ return;
10
+ let iface = [], mac;
11
+ for (let line, lines = info.split(EOL), i = 0; i < lines.length; i++) {
12
+ line = lines[i];
13
+ switch (true) {
14
+ case /link\/\S+\s+((?:\S{2}:)+\S{2})/.test(line):
15
+ mac = RegExp.$1;
16
+ break;
17
+ case /inet(\d*)\s+(\S+)/.test(line):
18
+ let ip = RegExp.$2.split("/"), v = RegExp.$1 || "4";
19
+ iface.push({
20
+ address: ip[0],
21
+ netmask: (v === "4" ? getIPv4Subnet : getIPv6Subnet)(ip[1]),
22
+ family: "IPv" + v,
23
+ mac,
24
+ internal: ip[0] === "127.0.0.1"
25
+ });
26
+ break;
27
+ }
28
+ }
29
+ if (mac)
30
+ this[info.slice(0, info.indexOf(":"))] = iface;
31
+ }
32
+ ;
33
+ const cpus = () => {
34
+ const PROCESSOR = /^processor\s*:\s*(\d+)/i;
35
+ const NAME = /^model[\s_]+name\s*:([^\r\n]+)/i;
36
+ const FREQ = /^cpu[\s_]+MHz\s*:\s*(\d+)/i;
37
+ const cpus2 = [];
38
+ let cpu;
39
+ cli("cat /proc/cpuinfo").split(EOL).forEach((line) => {
40
+ switch (true) {
41
+ case PROCESSOR.test(line):
42
+ cpus2[RegExp.$1.trim()] = cpu = {
43
+ model: "",
44
+ speed: 0,
45
+ get times() {
46
+ return {};
47
+ }
48
+ };
49
+ break;
50
+ case NAME.test(line):
51
+ cpu.model = RegExp.$1.trim();
52
+ break;
53
+ case FREQ.test(line):
54
+ cpu.speed = parseFloat(RegExp.$1.trim());
55
+ break;
56
+ }
57
+ });
58
+ return cpus2;
59
+ };
60
+ const endianness = () => "LE";
61
+ const freemem = () => {
62
+ let I, mem = cli("free -b").split(EOL);
63
+ mem[0].split(/\s+/).some((info, i) => info === "free" && (I = i));
64
+ return parseFloat(mem[1].split(/\s+/)[I + 1]);
65
+ };
66
+ const loadavg = () => /(\d+(?:\.\d+))\s+(\d+(?:\.\d+))\s+(\d+(?:\.\d+))/.test(
67
+ cli("cat /proc/loadavg")
68
+ ) && [
69
+ parseFloat(RegExp.$1),
70
+ parseFloat(RegExp.$2),
71
+ parseFloat(RegExp.$3)
72
+ ];
73
+ const networkInterfaces = () => {
74
+ const ifaces = {};
75
+ cli("ip addr").split(/^\d+:\s+/m).forEach(parseInterfaces, ifaces);
76
+ return ifaces;
77
+ };
78
+ const totalmem = () => {
79
+ let I, mem = cli("free -b").split(EOL);
80
+ mem[0].split(/\s+/).some((info, i) => info === "total" && (I = i));
81
+ return parseFloat(mem[1].split(/\s+/)[I + 1]);
82
+ };
83
+ const uptime = () => (Date.now() - Date.parse(cli("uptime -s").replace(" ", "T"))) / 1e3;
84
+ export {
85
+ cpus,
86
+ endianness,
87
+ freemem,
88
+ loadavg,
89
+ networkInterfaces,
90
+ totalmem,
91
+ uptime
92
+ };
@@ -0,0 +1 @@
1
+ console.warn("os core module is not fully supported here");
package/package.json CHANGED
@@ -1,28 +1,44 @@
1
1
  {
2
2
  "name": "@gjsify/os",
3
- "version": "0.0.1",
4
- "description": "os core module for gjs",
3
+ "version": "0.0.3",
4
+ "description": "Node.js os module for Gjs",
5
+ "main": "lib/cjs/index.js",
6
+ "module": "lib/esm/index.js",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./lib/types/index.d.ts",
12
+ "default": "./lib/esm/index.js"
13
+ },
14
+ "require": {
15
+ "types": "./lib/types/index.d.ts",
16
+ "default": "./lib/cjs/index.js"
17
+ }
18
+ }
19
+ },
5
20
  "scripts": {
6
- "test": "cgjs ./test"
21
+ "clear": "rm -rf lib tsconfig.tsbuildinfo test.gjs.mjs test.node.mjs",
22
+ "print:name": "echo '@gjsify/os'",
23
+ "build": "yarn print:name && yarn build:gjsify",
24
+ "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
25
+ "build:test": "yarn build:test:gjs && yarn build:test:node",
26
+ "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
27
+ "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
28
+ "test": "yarn print:name && yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
29
+ "test:gjs": "gjs -m test.gjs.mjs",
30
+ "test:node": "node test.node.mjs"
7
31
  },
8
32
  "keywords": [
9
33
  "gjs",
10
- "require",
11
- "commonjs",
12
- "npm",
13
34
  "node",
14
- "gtk",
15
- "native",
16
- "cgjs",
17
35
  "os"
18
36
  ],
19
- "author": "Andrea Giammarchi",
20
- "license": "ISC",
21
- "main": "index.js",
22
- "repository": "github:cgjs/cgjs",
23
- "bugs": "https://github.com/gjsify/os/issues",
24
- "homepage": "https://github.com/gjsify/os/tree/master/packages/os#readme",
37
+ "dependencies": {
38
+ "@girs/gjs": "^3.1.0",
39
+ "@gjsify/utils": "^0.0.3"
40
+ },
25
41
  "devDependencies": {
26
- "cgjs": "^0.1.28"
42
+ "@gjsify/cli": "^0.0.3"
27
43
  }
28
- }
44
+ }
@@ -1,5 +1,5 @@
1
1
  // this is a copy and paste from node
2
- module.exports = {
2
+ export default {
3
3
  "UV_UDP_REUSEADDR": 4,
4
4
  "errno": {
5
5
  "E2BIG": 7,
@@ -1,4 +1,4 @@
1
- module.exports = function createSubnet(size, segment, base, sep) {
1
+ export function createSubnet(size, segment, base, sep) {
2
2
  const empty = '0'.repeat(size);
3
3
  return mask => {
4
4
  const str = ('1'.repeat(parseInt(mask, 10)) + empty).slice(0, size);
@@ -9,3 +9,5 @@ module.exports = function createSubnet(size, segment, base, sep) {
9
9
  return out.join(sep);
10
10
  };
11
11
  };
12
+
13
+ export default { createSubnet }
@@ -1,17 +1,60 @@
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
  const NOMAC = '00:00:00:00:00:00';
5
6
 
6
7
  const getIPv6Subnet = createSubnet(128, 16, 16, ':');
7
8
 
8
- this.cpus = () => {
9
- let cores = parseFloat(system('sysctl -n hw.ncpu'));
9
+ const parseInterfaces = function(info) {
10
+ info = info.trim();
11
+ if (info.length < 1 || !/\binet\b/.test(info)) return;
12
+ const lines = info.split('\n');
13
+ const iface = [];
14
+ const length = lines.length;
15
+ let mac = NOMAC;
16
+ for (let line, i = 0; i < length; i++) {
17
+ line = lines[i];
18
+ switch (true) {
19
+ case /ether\s+((?:\S{2}:)+\S{2})/.test(line):
20
+ mac = RegExp.$1;
21
+ break;
22
+ case /inet\s+(\d+\.\d+\.\d+\.\d+)\s+netmask\s+0x(.{2})(.{2})(.{2})(.{2})/.test(line):
23
+ iface.push({
24
+ address: RegExp.$1,
25
+ netmask: [
26
+ parseInt(RegExp.$2, 16),
27
+ parseInt(RegExp.$3, 16),
28
+ parseInt(RegExp.$4, 16),
29
+ parseInt(RegExp.$5, 16)
30
+ ].join('.'),
31
+ family: 'IPv4',
32
+ mac: mac,
33
+ internal: RegExp.$1 === '127.0.0.1'
34
+ });
35
+ break;
36
+ case /inet6\s+((?:\S{0,4}:)+\S{1,4}).+?prefixlen\s+(\d+)/.test(line):
37
+ iface.push({
38
+ address: RegExp.$1,
39
+ netmask: getIPv6Subnet(RegExp.$2),
40
+ family: 'IPv6',
41
+ mac: mac,
42
+ internal: mac !== NOMAC
43
+ });
44
+ break;
45
+ }
46
+ }
47
+ this[info.slice(0, info.indexOf(':'))] = iface;
48
+ };
49
+
50
+ // PORTED TO deno runtime
51
+ export const cpus = () => {
52
+ let cores = parseFloat(cli('sysctl -n hw.ncpu'));
10
53
  const cpus = [];
11
54
  while (cores--) {
12
55
  cpus.push({
13
- model: system('sysctl -n machdep.cpu.brand_string').replace(/\s+/g, ' '),
14
- speed: parseFloat(system('sysctl -n hw.cpufrequency')) / 1000 / 1000,
56
+ model: cli('sysctl -n machdep.cpu.brand_string').replace(/\s+/g, ' '),
57
+ speed: parseFloat(cli('sysctl -n hw.cpufrequency')) / 1000 / 1000,
15
58
  get times() {
16
59
  console.warn('cpus.times is not supported');
17
60
  return {};
@@ -21,23 +64,25 @@ this.cpus = () => {
21
64
  return cpus;
22
65
  };
23
66
 
24
- this.endianness = () => 'LE';
67
+ export const endianness = () => 'LE';
25
68
 
26
- this.freemem = () => parseFloat(system('sysctl -n hw.memsize')) -
27
- parseFloat(system('sysctl -n hw.physmem'));
69
+ // PORTED TO deno runtime
70
+ export const freemem = () => parseFloat(cli('sysctl -n hw.memsize')) -
71
+ parseFloat(cli('sysctl -n hw.physmem'));
28
72
 
29
- this.loadavg = () => /load\s+averages:\s+(\d+(?:\.\d+))\s+(\d+(?:\.\d+))\s+(\d+(?:\.\d+))/.test(
30
- system('uptime')
73
+ // PORTED TO deno runtime
74
+ export const loadavg = () => /load\s+averages:\s+(\d+(?:\.\d+))\s+(\d+(?:\.\d+))\s+(\d+(?:\.\d+))/.test(
75
+ cli('uptime')
31
76
  ) && [
32
77
  parseFloat(RegExp.$1),
33
78
  parseFloat(RegExp.$2),
34
79
  parseFloat(RegExp.$3)
35
80
  ];
36
81
 
37
- this.networkInterfaces = () => {
82
+ export const networkInterfaces = () => {
38
83
  const ifaces = {};
39
84
  const groups = [];
40
- const lines = system('ifconfig').split(EOL);
85
+ const lines = cli('ifconfig').split(EOL);
41
86
  const length = lines.length;
42
87
  for (let
43
88
  group = [],
@@ -52,20 +97,22 @@ this.networkInterfaces = () => {
52
97
  }
53
98
  --i;
54
99
  }
55
- groups.push(group.join(EOL));
100
+ groups.push(group.join(EOL as any)); // TODO check this
56
101
  }
57
102
  groups.forEach(parseInterfaces, ifaces);
58
103
  return ifaces;
59
104
  };
60
105
 
61
- this.totalmem = () => {
62
- let I, mem = system('free -b').split(EOL);
106
+ // PORTED TO deno runtime
107
+ export const totalmem = () => {
108
+ let I: number, mem = cli('free -b').split(EOL);
63
109
  mem[0].split(/\s+/).some((info, i) => info === 'total' && (I = i));
64
110
  return parseFloat(mem[1].split(/\s+/)[I + 1]);
65
111
  };
66
112
 
67
- this.uptime = () => {
68
- const uptime = system('uptime');
113
+ // PORTED TO deno runtime
114
+ export const uptime = () => {
115
+ const uptime = cli('uptime');
69
116
  const up = /up\s+([^,]+)?,/.test(uptime) && RegExp.$1;
70
117
  switch (true) {
71
118
  case /^(\d+):(\d+)$/.test(up):
@@ -82,44 +129,3 @@ this.uptime = () => {
82
129
  }
83
130
  return up;
84
131
  };
85
-
86
- function parseInterfaces(info) {
87
- info = info.trim();
88
- if (info.length < 1 || !/\binet\b/.test(info)) return;
89
- const lines = info.split('\n');
90
- const iface = [];
91
- const length = lines.length;
92
- let mac = NOMAC;
93
- for (let line, i = 0; i < length; i++) {
94
- line = lines[i];
95
- switch (true) {
96
- case /ether\s+((?:\S{2}:)+\S{2})/.test(line):
97
- mac = RegExp.$1;
98
- break;
99
- case /inet\s+(\d+\.\d+\.\d+\.\d+)\s+netmask\s+0x(.{2})(.{2})(.{2})(.{2})/.test(line):
100
- iface.push({
101
- address: RegExp.$1,
102
- netmask: [
103
- parseInt(RegExp.$2, 16),
104
- parseInt(RegExp.$3, 16),
105
- parseInt(RegExp.$4, 16),
106
- parseInt(RegExp.$5, 16)
107
- ].join('.'),
108
- family: 'IPv4',
109
- mac: mac,
110
- internal: RegExp.$1 === '127.0.0.1'
111
- });
112
- break;
113
- case /inet6\s+((?:\S{0,4}:)+\S{1,4}).+?prefixlen\s+(\d+)/.test(line):
114
- iface.push({
115
- address: RegExp.$1,
116
- netmask: getIPv6Subnet(RegExp.$2),
117
- family: 'IPv6',
118
- mac: mac,
119
- internal: mac !== NOMAC
120
- });
121
- break;
122
- }
123
- }
124
- this[info.slice(0, info.indexOf(':'))] = iface;
125
- };
@@ -0,0 +1,9 @@
1
+ import { describe, it, expect } from '@gjsify/unit';
2
+
3
+ export default async () => {
4
+ await describe('true', async () => {
5
+ await it('should be true', async () => {
6
+ expect(true).toBeTruthy();
7
+ });
8
+ });
9
+ }
package/src/index.ts ADDED
@@ -0,0 +1,103 @@
1
+ import { cli, getPathSeparator, getOs } from '@gjsify/utils';
2
+
3
+ export { constants }
4
+
5
+ import * as linux from './linux.js';
6
+ import * as darwin from './darwin.js';
7
+ import GLib from '@girs/glib-2.0';
8
+
9
+ import constants from './constants.js';
10
+
11
+ export const EOL = getPathSeparator() === '/' ? '\n' : '\r\n';
12
+
13
+ // Ported to packages/deno/std/node/os.ts
14
+ export const homedir = () => GLib.get_home_dir();
15
+
16
+ // Ported to deno runtime
17
+ export const hostname = () => GLib.get_host_name();
18
+
19
+ // Ported to deno runtime
20
+ export const release = () => cli('uname -r');
21
+
22
+ // Ported to packages/deno/std/node/os.ts
23
+ export const tmpdir = () => GLib.get_tmp_dir();
24
+
25
+ // Existing replacement in packages/deno/std/node/os.ts
26
+ export const type = () => cli('uname');
27
+
28
+ // Ported to packages/deno/std/node/os.ts
29
+ export const userInfo = () => ({
30
+ uid: 1000,
31
+ gid: 100,
32
+ username: GLib.get_user_name(),
33
+ homedir: GLib.get_home_dir()
34
+ });
35
+
36
+ // Ported to packages/deno/std/node/os.ts
37
+ export const cpus = () => {
38
+ const _os = getOs();
39
+ switch (_os) {
40
+ case "darwin":
41
+ return darwin.cpus();
42
+ case "linux":
43
+ return linux.cpus();
44
+ default:
45
+ console.warn(`${_os} is not supported!`);
46
+ break;
47
+ }
48
+ };
49
+
50
+ // Existing replacement in packages/deno/std/node/os.ts
51
+ export const endianness = () => {
52
+ const _os = getOs();
53
+ switch (_os) {
54
+ case "darwin":
55
+ return darwin.endianness();
56
+ case "linux":
57
+ return linux.endianness();
58
+ default:
59
+ console.warn(`${_os} is not supported!`);
60
+ break;
61
+ }
62
+ };
63
+
64
+ // Ported to packages/deno/std/node/os.ts
65
+ export const freemem = () => {
66
+ const _os = getOs();
67
+ switch (_os) {
68
+ case "darwin":
69
+ return darwin.freemem();
70
+ case "linux":
71
+ return linux.freemem();
72
+ default:
73
+ console.warn(`${_os} is not supported!`);
74
+ break;
75
+ }
76
+ };
77
+
78
+ // Ported to packages/deno/std/node/os.ts
79
+ export const loadavg = () => {
80
+ const _os = getOs();
81
+ switch (_os) {
82
+ case "darwin":
83
+ return darwin.loadavg();
84
+ case "linux":
85
+ return linux.loadavg();
86
+ default:
87
+ console.warn(`${_os} is not supported!`);
88
+ break;
89
+ }
90
+ }
91
+
92
+ export const networkInterfaces = () => {
93
+ const _os = getOs();
94
+ switch (_os) {
95
+ case "darwin":
96
+ return darwin.networkInterfaces();
97
+ case "linux":
98
+ return linux.networkInterfaces();
99
+ default:
100
+ console.warn(`${_os} is not supported!`);
101
+ break;
102
+ }
103
+ };