smartnetwork/readme.md

150 lines
5.6 KiB
Markdown
Raw Permalink Normal View History

2024-04-14 16:02:08 +00:00
# @push.rocks/smartnetwork
2019-04-16 08:21:11 +00:00
network diagnostics
2024-04-14 16:02:08 +00:00
## Install
To install `@push.rocks/smartnetwork`, run the following command in your terminal:
```bash
npm install @push.rocks/smartnetwork --save
```
This command will download `@push.rocks/smartnetwork` and add it to your project's `package.json` file.
2019-04-16 08:21:11 +00:00
## Usage
2024-04-14 16:02:08 +00:00
In this section, we will dive deep into the capabilities of the `@push.rocks/smartnetwork` package, exploring its various features through TypeScript examples. The package is designed to simplify network diagnostics tasks, including speed tests, port availability checks, ping operations, and more.
### Basic Setup
First, import the package into your project:
```typescript
import { SmartNetwork } from '@push.rocks/smartnetwork';
```
Then, create an instance of `SmartNetwork`:
```typescript
const myNetwork = new SmartNetwork();
```
### Performing a Speed Test
You can measure the network speed using the `getSpeed` method. This feature leverages Cloudflare's speed test capabilities to assess your internet connection's download and upload speeds.
```typescript
const speedTest = async () => {
const speedResult = await myNetwork.getSpeed();
console.log(`Download speed: ${speedResult.downloadSpeed} Mbps`);
console.log(`Upload speed: ${speedResult.uploadSpeed} Mbps`);
console.log(`Latency: ${speedResult.averageTime} ms`);
};
speedTest();
```
### Checking Port Availability Locally
The `isLocalPortUnused` method allows you to check if a specific port on your local machine is available for use.
```typescript
const checkLocalPort = async (port: number) => {
const isUnused = await myNetwork.isLocalPortUnused(port);
if (isUnused) {
console.log(`Port ${port} is available.`);
} else {
console.log(`Port ${port} is in use.`);
}
};
checkLocalPort(8080); // Example port number
```
### Checking Remote Port Availability
To verify if a certain port is available on a remote server, use `isRemotePortAvailable`. This can help determine if a service is up and reachable.
```typescript
const checkRemotePort = async (hostname: string, port: number) => {
const isAvailable = await myNetwork.isRemotePortAvailable(hostname, port);
if (isAvailable) {
console.log(`Port ${port} on ${hostname} is available.`);
} else {
console.log(`Port ${port} on ${hostname} is not available.`);
}
};
checkRemotePort('example.com', 443); // Checking HTTPS port on example.com
```
### Using Ping
The `ping` method allows you to send ICMP packets to a host to measure round-trip time and determine if the host is reachable.
```typescript
const pingHost = async (hostname: string) => {
const pingResult = await myNetwork.ping(hostname);
if (pingResult.alive) {
console.log(`${hostname} is reachable. RTT: ${pingResult.time} ms`);
} else {
console.log(`${hostname} is not reachable.`);
}
};
pingHost('google.com');
```
### Getting Network Gateways
You can also retrieve information about your network gateways, including the default gateway used by your machine.
2019-04-16 08:21:11 +00:00
```typescript
2024-04-14 16:02:08 +00:00
const showGateways = async () => {
const gateways = await myNetwork.getGateways();
console.log(gateways);
2019-04-17 18:05:07 +00:00
2024-04-14 16:02:08 +00:00
const defaultGateway = await myNetwork.getDefaultGateway();
console.log(`Default Gateway: `, defaultGateway);
};
2021-04-28 13:41:55 +00:00
2024-04-14 16:02:08 +00:00
showGateways();
```
2021-04-28 13:41:55 +00:00
2024-04-14 16:02:08 +00:00
### Discovering Public IP Addresses
To find out your public IPv4 and IPv6 addresses, the following method can be used:
```typescript
const showPublicIps = async () => {
const publicIps = await myNetwork.getPublicIps();
console.log(`Public IPv4: ${publicIps.v4}`);
console.log(`Public IPv6: ${publicIps.v6}`);
2019-09-06 16:26:32 +00:00
};
2024-04-14 16:02:08 +00:00
showPublicIps();
2019-04-16 08:21:11 +00:00
```
2024-04-14 16:02:08 +00:00
The `@push.rocks/smartnetwork` package provides an easy-to-use, comprehensive suite of tools for network diagnostics and monitoring, encapsulating complex network operations into simple asynchronous methods. By leveraging TypeScript, developers can benefit from type checking, ensuring that they can work with clear structures and expectations.
These examples offer a glimpse into the module's utility in real-world scenarios, demonstrating its versatility in handling common network tasks. Whether you're developing a network-sensitive application, diagnosing connectivity issues, or simply curious about your network performance, `@push.rocks/smartnetwork` equips you with the tools you need.
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
2019-11-19 23:00:37 +00:00
2024-04-14 16:02:08 +00:00
### Company Information
2019-11-19 23:00:37 +00:00
2024-04-14 16:02:08 +00:00
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2019-04-16 08:21:11 +00:00
2024-04-14 16:02:08 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
2019-04-16 08:21:11 +00:00
2024-04-14 16:02:08 +00:00
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.