@fuman/net 0.0.1

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.
Files changed (59) hide show
  1. package/LICENSE +8 -0
  2. package/errors.cjs +14 -0
  3. package/errors.d.ts +6 -0
  4. package/errors.js +14 -0
  5. package/fake.cjs +42 -0
  6. package/fake.d.ts +16 -0
  7. package/fake.js +42 -0
  8. package/index.cjs +30 -0
  9. package/index.d.ts +7 -0
  10. package/index.js +30 -0
  11. package/ip/bundle.cjs +19 -0
  12. package/ip/bundle.d.ts +3 -0
  13. package/ip/bundle.js +19 -0
  14. package/ip/index.d.ts +8 -0
  15. package/ip/parse.cjs +34 -0
  16. package/ip/parse.d.ts +5 -0
  17. package/ip/parse.js +34 -0
  18. package/ip/types.d.ts +10 -0
  19. package/ip/v4.bench.d.ts +1 -0
  20. package/ip/v4.cjs +55 -0
  21. package/ip/v4.d.ts +4 -0
  22. package/ip/v4.js +55 -0
  23. package/ip/v6.bench.d.ts +1 -0
  24. package/ip/v6.cjs +217 -0
  25. package/ip/v6.d.ts +18 -0
  26. package/ip/v6.js +217 -0
  27. package/package.json +30 -0
  28. package/proxy/http/_protocol.cjs +31 -0
  29. package/proxy/http/_protocol.d.ts +3 -0
  30. package/proxy/http/_protocol.js +31 -0
  31. package/proxy/http/connect.cjs +34 -0
  32. package/proxy/http/connect.d.ts +4 -0
  33. package/proxy/http/connect.js +34 -0
  34. package/proxy/http/index.cjs +15 -0
  35. package/proxy/http/index.d.ts +5 -0
  36. package/proxy/http/index.js +15 -0
  37. package/proxy/http/types.cjs +10 -0
  38. package/proxy/http/types.d.ts +32 -0
  39. package/proxy/http/types.js +10 -0
  40. package/proxy/index.d.ts +2 -0
  41. package/proxy/socks/_protocol.cjs +111 -0
  42. package/proxy/socks/_protocol.d.ts +7 -0
  43. package/proxy/socks/_protocol.js +111 -0
  44. package/proxy/socks/connect.cjs +78 -0
  45. package/proxy/socks/connect.d.ts +4 -0
  46. package/proxy/socks/connect.js +78 -0
  47. package/proxy/socks/index.cjs +15 -0
  48. package/proxy/socks/index.d.ts +5 -0
  49. package/proxy/socks/index.js +15 -0
  50. package/proxy/socks/types.cjs +10 -0
  51. package/proxy/socks/types.d.ts +34 -0
  52. package/proxy/socks/types.js +10 -0
  53. package/reconnection.cjs +151 -0
  54. package/reconnection.d.ts +82 -0
  55. package/reconnection.js +151 -0
  56. package/types.d.ts +41 -0
  57. package/websocket.cjs +157 -0
  58. package/websocket.d.ts +44 -0
  59. package/websocket.js +157 -0
package/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright 2024 alina sireneva
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
package/errors.cjs ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ class ConnectionClosedError extends Error {
4
+ constructor(message) {
5
+ super(`Connection closed${message != null ? `: ${message}` : ""}`);
6
+ }
7
+ }
8
+ class ListenerClosedError extends Error {
9
+ constructor() {
10
+ super("Listener closed");
11
+ }
12
+ }
13
+ exports.ConnectionClosedError = ConnectionClosedError;
14
+ exports.ListenerClosedError = ListenerClosedError;
package/errors.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export declare class ConnectionClosedError extends Error {
2
+ constructor(message?: string);
3
+ }
4
+ export declare class ListenerClosedError extends Error {
5
+ constructor();
6
+ }
package/errors.js ADDED
@@ -0,0 +1,14 @@
1
+ class ConnectionClosedError extends Error {
2
+ constructor(message) {
3
+ super(`Connection closed${message != null ? `: ${message}` : ""}`);
4
+ }
5
+ }
6
+ class ListenerClosedError extends Error {
7
+ constructor() {
8
+ super("Listener closed");
9
+ }
10
+ }
11
+ export {
12
+ ConnectionClosedError,
13
+ ListenerClosedError
14
+ };
package/fake.cjs ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const io = require("@fuman/io");
4
+ const utils = require("@fuman/utils");
5
+ const errors = require("./errors.cjs");
6
+ class FakeConnection {
7
+ constructor(address) {
8
+ this.address = address;
9
+ }
10
+ rx = io.Bytes.alloc();
11
+ tx = io.Bytes.alloc();
12
+ closed = false;
13
+ cv = new utils.ConditionVariable();
14
+ get localAddress() {
15
+ return this.address;
16
+ }
17
+ get remoteAddress() {
18
+ return this.address;
19
+ }
20
+ close() {
21
+ this.closed = true;
22
+ this.cv.notify();
23
+ }
24
+ async read(into) {
25
+ if (this.closed) throw new errors.ConnectionClosedError();
26
+ if (!this.rx.available) await this.cv.wait();
27
+ if (this.closed) throw new errors.ConnectionClosedError();
28
+ return this.rx.read(into);
29
+ }
30
+ async write(bytes) {
31
+ await this.tx.write(bytes);
32
+ this.cv.notify();
33
+ }
34
+ writeIntoRx(bytes) {
35
+ this.rx.writeSync(bytes.length).set(bytes);
36
+ this.cv.notify();
37
+ }
38
+ getTx() {
39
+ return this.tx.result();
40
+ }
41
+ }
42
+ exports.FakeConnection = FakeConnection;
package/fake.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { IConnection } from './types.js';
2
+ export declare class FakeConnection<Address = string> implements IConnection<Address, Address> {
3
+ readonly address: Address;
4
+ constructor(address: Address);
5
+ private rx;
6
+ private tx;
7
+ private closed;
8
+ private cv;
9
+ get localAddress(): Address;
10
+ get remoteAddress(): Address;
11
+ close(): void;
12
+ read(into: Uint8Array): Promise<number>;
13
+ write(bytes: Uint8Array): Promise<void>;
14
+ writeIntoRx(bytes: Uint8Array): void;
15
+ getTx(): Uint8Array;
16
+ }
package/fake.js ADDED
@@ -0,0 +1,42 @@
1
+ import { Bytes } from "@fuman/io";
2
+ import { ConditionVariable } from "@fuman/utils";
3
+ import { ConnectionClosedError } from "./errors.js";
4
+ class FakeConnection {
5
+ constructor(address) {
6
+ this.address = address;
7
+ }
8
+ rx = Bytes.alloc();
9
+ tx = Bytes.alloc();
10
+ closed = false;
11
+ cv = new ConditionVariable();
12
+ get localAddress() {
13
+ return this.address;
14
+ }
15
+ get remoteAddress() {
16
+ return this.address;
17
+ }
18
+ close() {
19
+ this.closed = true;
20
+ this.cv.notify();
21
+ }
22
+ async read(into) {
23
+ if (this.closed) throw new ConnectionClosedError();
24
+ if (!this.rx.available) await this.cv.wait();
25
+ if (this.closed) throw new ConnectionClosedError();
26
+ return this.rx.read(into);
27
+ }
28
+ async write(bytes) {
29
+ await this.tx.write(bytes);
30
+ this.cv.notify();
31
+ }
32
+ writeIntoRx(bytes) {
33
+ this.rx.writeSync(bytes.length).set(bytes);
34
+ this.cv.notify();
35
+ }
36
+ getTx() {
37
+ return this.tx.result();
38
+ }
39
+ }
40
+ export {
41
+ FakeConnection
42
+ };
package/index.cjs ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const errors = require("./errors.cjs");
4
+ const fake = require("./fake.cjs");
5
+ const reconnection = require("./reconnection.cjs");
6
+ const websocket = require("./websocket.cjs");
7
+ const bundle = require("./ip/bundle.cjs");
8
+ const connect = require("./proxy/http/connect.cjs");
9
+ const types = require("./proxy/http/types.cjs");
10
+ const index = require("./proxy/http/index.cjs");
11
+ const connect$1 = require("./proxy/socks/connect.cjs");
12
+ const types$1 = require("./proxy/socks/types.cjs");
13
+ const index$1 = require("./proxy/socks/index.cjs");
14
+ exports.ConnectionClosedError = errors.ConnectionClosedError;
15
+ exports.ListenerClosedError = errors.ListenerClosedError;
16
+ exports.FakeConnection = fake.FakeConnection;
17
+ exports.PersistentConnection = reconnection.PersistentConnection;
18
+ exports.defaultReconnectionStrategy = reconnection.defaultReconnectionStrategy;
19
+ exports.WebSocketConnection = websocket.WebSocketConnection;
20
+ exports.WebSocketConnectionClosedError = websocket.WebSocketConnectionClosedError;
21
+ exports.WebSocketConnectionFramed = websocket.WebSocketConnectionFramed;
22
+ exports.connectWs = websocket.connectWs;
23
+ exports.connectWsFramed = websocket.connectWsFramed;
24
+ exports.ip = bundle;
25
+ exports.performHttpProxyHandshake = connect.performHttpProxyHandshake;
26
+ exports.HttpProxyConnectionError = types.HttpProxyConnectionError;
27
+ exports.withHttpProxy = index.withHttpProxy;
28
+ exports.performSocksHandshake = connect$1.performSocksHandshake;
29
+ exports.SocksProxyConnectionError = types$1.SocksProxyConnectionError;
30
+ exports.withSocksProxy = index$1.withSocksProxy;
package/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from './errors.js';
2
+ export * from './fake.js';
3
+ export * from './ip/index.js';
4
+ export * from './proxy/index.js';
5
+ export * from './reconnection.js';
6
+ export * from './types.js';
7
+ export * from './websocket.js';
package/index.js ADDED
@@ -0,0 +1,30 @@
1
+ import { ConnectionClosedError, ListenerClosedError } from "./errors.js";
2
+ import { FakeConnection } from "./fake.js";
3
+ import { PersistentConnection, defaultReconnectionStrategy } from "./reconnection.js";
4
+ import { WebSocketConnection, WebSocketConnectionClosedError, WebSocketConnectionFramed, connectWs, connectWsFramed } from "./websocket.js";
5
+ import * as bundle from "./ip/bundle.js";
6
+ import { performHttpProxyHandshake } from "./proxy/http/connect.js";
7
+ import { HttpProxyConnectionError } from "./proxy/http/types.js";
8
+ import { withHttpProxy } from "./proxy/http/index.js";
9
+ import { performSocksHandshake } from "./proxy/socks/connect.js";
10
+ import { SocksProxyConnectionError } from "./proxy/socks/types.js";
11
+ import { withSocksProxy } from "./proxy/socks/index.js";
12
+ export {
13
+ ConnectionClosedError,
14
+ FakeConnection,
15
+ HttpProxyConnectionError,
16
+ ListenerClosedError,
17
+ PersistentConnection,
18
+ SocksProxyConnectionError,
19
+ WebSocketConnection,
20
+ WebSocketConnectionClosedError,
21
+ WebSocketConnectionFramed,
22
+ connectWs,
23
+ connectWsFramed,
24
+ defaultReconnectionStrategy,
25
+ bundle as ip,
26
+ performHttpProxyHandshake,
27
+ performSocksHandshake,
28
+ withHttpProxy,
29
+ withSocksProxy
30
+ };
package/ip/bundle.cjs ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const parse = require("./parse.cjs");
4
+ const v4 = require("./v4.cjs");
5
+ const v6 = require("./v6.cjs");
6
+ exports.parse = parse.parse;
7
+ exports.parseWithPort = parse.parseWithPort;
8
+ exports.stringify = parse.stringify;
9
+ exports.stringifyWithPort = parse.stringifyWithPort;
10
+ exports.normalizeV4 = v4.normalizeV4;
11
+ exports.parseV4 = v4.parseV4;
12
+ exports.stringifyV4 = v4.stringifyV4;
13
+ exports.expandV6 = v6.expandV6;
14
+ exports.fromBytesV6 = v6.fromBytesV6;
15
+ exports.parseV6 = v6.parseV6;
16
+ exports.readV6 = v6.readV6;
17
+ exports.stringifyV6 = v6.stringifyV6;
18
+ exports.toBytesV6 = v6.toBytesV6;
19
+ exports.writeV6 = v6.writeV6;
package/ip/bundle.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './parse.js';
2
+ export * from './v4.js';
3
+ export * from './v6.js';
package/ip/bundle.js ADDED
@@ -0,0 +1,19 @@
1
+ import { parse, parseWithPort, stringify, stringifyWithPort } from "./parse.js";
2
+ import { normalizeV4, parseV4, stringifyV4 } from "./v4.js";
3
+ import { expandV6, fromBytesV6, parseV6, readV6, stringifyV6, toBytesV6, writeV6 } from "./v6.js";
4
+ export {
5
+ expandV6,
6
+ fromBytesV6,
7
+ normalizeV4,
8
+ parse,
9
+ parseV4,
10
+ parseV6,
11
+ parseWithPort,
12
+ readV6,
13
+ stringify,
14
+ stringifyV4,
15
+ stringifyV6,
16
+ stringifyWithPort,
17
+ toBytesV6,
18
+ writeV6
19
+ };
package/ip/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Utilities for parsing and manipulating IP addresses.
3
+ *
4
+ * @module
5
+ */
6
+ import * as ip from './bundle.js';
7
+ export * from './types.js';
8
+ export { ip };
package/ip/parse.cjs ADDED
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const v4 = require("./v4.cjs");
4
+ const v6 = require("./v6.cjs");
5
+ function parse(ip) {
6
+ if (ip.includes(":")) {
7
+ return v6.parseV6(ip);
8
+ } else {
9
+ return v4.parseV4(ip);
10
+ }
11
+ }
12
+ function parseWithPort(ip) {
13
+ const lastColon = ip.lastIndexOf(":");
14
+ const addr = ip.slice(0, lastColon);
15
+ const port = Number(ip.slice(lastColon + 1));
16
+ if (Number.isNaN(port) || port < 0 || port > 65535) {
17
+ throw new Error(`Invalid port: ${ip} (in ${ip})`);
18
+ }
19
+ return [parse(addr), port];
20
+ }
21
+ function stringify(parsed) {
22
+ if (parsed.type === "ipv4") {
23
+ return v4.stringifyV4(parsed);
24
+ } else {
25
+ return v6.stringifyV6(parsed);
26
+ }
27
+ }
28
+ function stringifyWithPort(parsed, port) {
29
+ return `${stringify(parsed)}:${port}`;
30
+ }
31
+ exports.parse = parse;
32
+ exports.parseWithPort = parseWithPort;
33
+ exports.stringify = stringify;
34
+ exports.stringifyWithPort = stringifyWithPort;
package/ip/parse.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { Ipv4Address, Ipv6Address } from './types.js';
2
+ export declare function parse(ip: string): Ipv4Address | Ipv6Address;
3
+ export declare function parseWithPort(ip: string): [Ipv4Address | Ipv6Address, number];
4
+ export declare function stringify(parsed: Ipv4Address | Ipv6Address): string;
5
+ export declare function stringifyWithPort(parsed: Ipv4Address | Ipv6Address, port: number): string;
package/ip/parse.js ADDED
@@ -0,0 +1,34 @@
1
+ import { parseV4, stringifyV4 } from "./v4.js";
2
+ import { parseV6, stringifyV6 } from "./v6.js";
3
+ function parse(ip) {
4
+ if (ip.includes(":")) {
5
+ return parseV6(ip);
6
+ } else {
7
+ return parseV4(ip);
8
+ }
9
+ }
10
+ function parseWithPort(ip) {
11
+ const lastColon = ip.lastIndexOf(":");
12
+ const addr = ip.slice(0, lastColon);
13
+ const port = Number(ip.slice(lastColon + 1));
14
+ if (Number.isNaN(port) || port < 0 || port > 65535) {
15
+ throw new Error(`Invalid port: ${ip} (in ${ip})`);
16
+ }
17
+ return [parse(addr), port];
18
+ }
19
+ function stringify(parsed) {
20
+ if (parsed.type === "ipv4") {
21
+ return stringifyV4(parsed);
22
+ } else {
23
+ return stringifyV6(parsed);
24
+ }
25
+ }
26
+ function stringifyWithPort(parsed, port) {
27
+ return `${stringify(parsed)}:${port}`;
28
+ }
29
+ export {
30
+ parse,
31
+ parseWithPort,
32
+ stringify,
33
+ stringifyWithPort
34
+ };
package/ip/types.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export interface Ipv4Address {
2
+ type: 'ipv4';
3
+ parts: Uint8Array;
4
+ }
5
+ export interface Ipv6Address {
6
+ type: 'ipv6';
7
+ parts: Uint16Array;
8
+ zoneId?: string;
9
+ }
10
+ export type IpAddress = Ipv4Address | Ipv6Address;
@@ -0,0 +1 @@
1
+ export {};
package/ip/v4.cjs ADDED
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const utils = require("@fuman/utils");
4
+ function parseV4(input) {
5
+ const parts = utils.u8.alloc(4);
6
+ const octets = input.split(".");
7
+ const octetsCount = octets.length;
8
+ if (octetsCount === 1) {
9
+ const num = Number(octets[0]);
10
+ if (Number.isNaN(num) || num < 0 || num > 4294967295) {
11
+ throw new Error(`Invalid IPv4 address: ${input}`);
12
+ }
13
+ parts[0] = num >> 24 & 255;
14
+ parts[1] = num >> 16 & 255;
15
+ parts[2] = num >> 8 & 255;
16
+ parts[3] = num & 255;
17
+ return {
18
+ type: "ipv4",
19
+ parts
20
+ };
21
+ }
22
+ if (octetsCount > 4) {
23
+ throw new Error(`Invalid IPv4 address: ${input}`);
24
+ }
25
+ let pos = 0;
26
+ for (let i = 0; i < octetsCount; i++) {
27
+ const part = Number(octets[i]);
28
+ if (Number.isNaN(part) || part < 0 || part > 255) {
29
+ throw new Error(`Invalid IPv4 address: ${input}`);
30
+ }
31
+ parts[pos] = part;
32
+ if (octetsCount === 2 && pos === 0) {
33
+ pos += 2;
34
+ } else if (octetsCount === 3 && pos === 1) {
35
+ pos += 1;
36
+ }
37
+ pos += 1;
38
+ }
39
+ return {
40
+ type: "ipv4",
41
+ parts
42
+ };
43
+ }
44
+ function stringifyV4(ip) {
45
+ if (ip.parts.length !== 4) {
46
+ throw new Error("Invalid IPv4 address");
47
+ }
48
+ return ip.parts.join(".");
49
+ }
50
+ function normalizeV4(input) {
51
+ return stringifyV4(parseV4(input));
52
+ }
53
+ exports.normalizeV4 = normalizeV4;
54
+ exports.parseV4 = parseV4;
55
+ exports.stringifyV4 = stringifyV4;
package/ip/v4.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { Ipv4Address } from './types.js';
2
+ export declare function parseV4(input: string): Ipv4Address;
3
+ export declare function stringifyV4(ip: Ipv4Address): string;
4
+ export declare function normalizeV4(input: string): string;
package/ip/v4.js ADDED
@@ -0,0 +1,55 @@
1
+ import { u8 } from "@fuman/utils";
2
+ function parseV4(input) {
3
+ const parts = u8.alloc(4);
4
+ const octets = input.split(".");
5
+ const octetsCount = octets.length;
6
+ if (octetsCount === 1) {
7
+ const num = Number(octets[0]);
8
+ if (Number.isNaN(num) || num < 0 || num > 4294967295) {
9
+ throw new Error(`Invalid IPv4 address: ${input}`);
10
+ }
11
+ parts[0] = num >> 24 & 255;
12
+ parts[1] = num >> 16 & 255;
13
+ parts[2] = num >> 8 & 255;
14
+ parts[3] = num & 255;
15
+ return {
16
+ type: "ipv4",
17
+ parts
18
+ };
19
+ }
20
+ if (octetsCount > 4) {
21
+ throw new Error(`Invalid IPv4 address: ${input}`);
22
+ }
23
+ let pos = 0;
24
+ for (let i = 0; i < octetsCount; i++) {
25
+ const part = Number(octets[i]);
26
+ if (Number.isNaN(part) || part < 0 || part > 255) {
27
+ throw new Error(`Invalid IPv4 address: ${input}`);
28
+ }
29
+ parts[pos] = part;
30
+ if (octetsCount === 2 && pos === 0) {
31
+ pos += 2;
32
+ } else if (octetsCount === 3 && pos === 1) {
33
+ pos += 1;
34
+ }
35
+ pos += 1;
36
+ }
37
+ return {
38
+ type: "ipv4",
39
+ parts
40
+ };
41
+ }
42
+ function stringifyV4(ip) {
43
+ if (ip.parts.length !== 4) {
44
+ throw new Error("Invalid IPv4 address");
45
+ }
46
+ return ip.parts.join(".");
47
+ }
48
+ function normalizeV4(input) {
49
+ return stringifyV4(parseV4(input));
50
+ }
51
+ export {
52
+ normalizeV4,
53
+ parseV4,
54
+ stringifyV4
55
+ };
@@ -0,0 +1 @@
1
+ export {};