@gjsify/net 0.0.2
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 +5 -0
- package/lib/cjs/index.js +55 -0
- package/lib/esm/index.js +26 -0
- package/package.json +49 -0
- package/src/index.spec.ts +84 -0
- package/src/index.ts +26 -0
- package/src/test.mts +6 -0
- package/test.gjs.mjs +34801 -0
- package/test.node.mjs +366 -0
- package/tsconfig.json +18 -0
- package/tsconfig.types.json +8 -0
package/README.md
ADDED
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var src_exports = {};
|
|
29
|
+
__export(src_exports, {
|
|
30
|
+
isIP: () => isIP,
|
|
31
|
+
isIPv4: () => isIPv4,
|
|
32
|
+
isIPv6: () => isIPv6
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(src_exports);
|
|
35
|
+
var import_gio_2 = __toESM(require("@girs/gio-2.0"), 1);
|
|
36
|
+
function isIP(input) {
|
|
37
|
+
const addr = import_gio_2.default.InetAddress.new_from_string(input);
|
|
38
|
+
if (!addr)
|
|
39
|
+
return 0;
|
|
40
|
+
const family = addr.get_family();
|
|
41
|
+
switch (family) {
|
|
42
|
+
case import_gio_2.default.SocketFamily.INVALID:
|
|
43
|
+
return 0;
|
|
44
|
+
case import_gio_2.default.SocketFamily.IPV4:
|
|
45
|
+
return 4;
|
|
46
|
+
case import_gio_2.default.SocketFamily.IPV6:
|
|
47
|
+
return 6;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function isIPv4(input) {
|
|
51
|
+
return isIP(input) === 4;
|
|
52
|
+
}
|
|
53
|
+
function isIPv6(input) {
|
|
54
|
+
return isIP(input) === 6;
|
|
55
|
+
}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Gio from "@girs/gio-2.0";
|
|
2
|
+
function isIP(input) {
|
|
3
|
+
const addr = Gio.InetAddress.new_from_string(input);
|
|
4
|
+
if (!addr)
|
|
5
|
+
return 0;
|
|
6
|
+
const family = addr.get_family();
|
|
7
|
+
switch (family) {
|
|
8
|
+
case Gio.SocketFamily.INVALID:
|
|
9
|
+
return 0;
|
|
10
|
+
case Gio.SocketFamily.IPV4:
|
|
11
|
+
return 4;
|
|
12
|
+
case Gio.SocketFamily.IPV6:
|
|
13
|
+
return 6;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function isIPv4(input) {
|
|
17
|
+
return isIP(input) === 4;
|
|
18
|
+
}
|
|
19
|
+
function isIPv6(input) {
|
|
20
|
+
return isIP(input) === 6;
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
isIP,
|
|
24
|
+
isIPv4,
|
|
25
|
+
isIPv6
|
|
26
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gjsify/net",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Node.js net module for Gjs",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/cjs/index.js",
|
|
7
|
+
"module": "lib/esm/index.js",
|
|
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
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo || exit 0",
|
|
22
|
+
"print:name": "echo '@gjsify/net'",
|
|
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:types": "tsc --project tsconfig.types.json || exit 0",
|
|
26
|
+
"build:test": "yarn build:test:gjs && yarn build:test:node",
|
|
27
|
+
"build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
|
|
28
|
+
"build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
|
|
29
|
+
"test": "yarn print:name && yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
|
|
30
|
+
"test:gjs": "gjs -m test.gjs.mjs",
|
|
31
|
+
"test:node": "node test.node.mjs"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"gjs",
|
|
35
|
+
"node",
|
|
36
|
+
"net"
|
|
37
|
+
],
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@gjsify/assert": "^0.0.2",
|
|
40
|
+
"@gjsify/cli": "^0.0.2",
|
|
41
|
+
"@gjsify/esbuild-plugin-gjsify": "^0.0.2",
|
|
42
|
+
"@gjsify/process": "^0.0.2",
|
|
43
|
+
"@gjsify/unit": "^0.0.2",
|
|
44
|
+
"@types/node": "^20.3.1"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@girs/gio-2.0": "2.76.1-3.1.0"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { describe, it, expect } from '@gjsify/unit';
|
|
2
|
+
|
|
3
|
+
import { isIP, isIPv4, isIPv6 } from 'net';
|
|
4
|
+
|
|
5
|
+
export default async () => {
|
|
6
|
+
|
|
7
|
+
const v6AddrArr = ['::1'];
|
|
8
|
+
const v4AddrArr = ['127.0.0.1'];
|
|
9
|
+
const invalidAddrArr = ['127.000.000.001', '127.0.0.1/24', 'fhqwhgads'];
|
|
10
|
+
|
|
11
|
+
await describe('net.isIP', async () => {
|
|
12
|
+
await it('should be a function', async () => {
|
|
13
|
+
expect(typeof isIP).toBe("function");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
for (const v6 of v6AddrArr) {
|
|
17
|
+
await it(`should return 6 for "${v6}"`, async () => {
|
|
18
|
+
expect(isIP(v6)).toBe(6);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
for (const v4 of v4AddrArr) {
|
|
23
|
+
await it(`should return 4 for "${v4}"`, async () => {
|
|
24
|
+
expect(isIP(v4)).toBe(4);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
for (const invalid of invalidAddrArr) {
|
|
29
|
+
await it(`should return 0 for "${invalid}"`, async () => {
|
|
30
|
+
expect(isIP(invalid)).toBe(0);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
await describe('net.isIPv4', async () => {
|
|
37
|
+
await it('should be a function', async () => {
|
|
38
|
+
expect(typeof isIPv4).toBe("function");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
for (const v6 of v6AddrArr) {
|
|
42
|
+
await it(`should return false for "${v6}"`, async () => {
|
|
43
|
+
expect(isIPv4(v6)).toBeFalsy();
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
for (const v4 of v4AddrArr) {
|
|
48
|
+
await it(`should return true for "${v4}"`, async () => {
|
|
49
|
+
expect(isIPv4(v4)).toBeTruthy();
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
for (const invalid of invalidAddrArr) {
|
|
54
|
+
await it(`should return false for "${invalid}"`, async () => {
|
|
55
|
+
expect(isIPv4(invalid)).toBeFalsy();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
await describe('net.isIPv6', async () => {
|
|
61
|
+
await it('should be a function', async () => {
|
|
62
|
+
expect(typeof isIPv6).toBe("function");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
for (const v6 of v6AddrArr) {
|
|
66
|
+
await it(`should return true for "${v6}"`, async () => {
|
|
67
|
+
expect(isIPv6(v6)).toBeTruthy();
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
for (const v4 of v4AddrArr) {
|
|
72
|
+
await it(`should return false for "${v4}"`, async () => {
|
|
73
|
+
expect(isIPv6(v4)).toBeFalsy();
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
for (const invalid of invalidAddrArr) {
|
|
78
|
+
await it(`should return false for "${invalid}"`, async () => {
|
|
79
|
+
expect(isIPv6(invalid)).toBeFalsy();
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Gio from '@girs/gio-2.0';
|
|
2
|
+
|
|
3
|
+
export function isIP(input: string): 0 | 4 | 6 {
|
|
4
|
+
const addr = Gio.InetAddress.new_from_string(input);
|
|
5
|
+
|
|
6
|
+
if (!addr) return 0;
|
|
7
|
+
|
|
8
|
+
const family = addr.get_family();
|
|
9
|
+
|
|
10
|
+
switch(family) {
|
|
11
|
+
case Gio.SocketFamily.INVALID:
|
|
12
|
+
return 0;
|
|
13
|
+
case Gio.SocketFamily.IPV4:
|
|
14
|
+
return 4;
|
|
15
|
+
case Gio.SocketFamily.IPV6:
|
|
16
|
+
return 6;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isIPv4(input: string): boolean {
|
|
21
|
+
return isIP(input) === 4;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function isIPv6(input: string): boolean {
|
|
25
|
+
return isIP(input) === 6;
|
|
26
|
+
}
|