@1-/ipv4 0.1.0
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 +143 -0
- package/ipv4_u32.js +10 -0
- package/package.json +20 -0
- package/u32_ipv4.js +7 -0
package/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
[English](#en) | [中文](#zh)
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
<a id="en"></a>
|
|
6
|
+
# @1-/ipv4 : Convert IPv4 addresses to and from 32-bit integers
|
|
7
|
+
|
|
8
|
+
- [@1-/ipv4 : Convert IPv4 addresses to and from 32-bit integers](#1-ipv4-convert-ipv4-addresses-to-and-from-32-bit-integers)
|
|
9
|
+
- [Functionality](#functionality)
|
|
10
|
+
- [Usage demonstration](#usage-demonstration)
|
|
11
|
+
- [Design rationale](#design-rationale)
|
|
12
|
+
- [Technology stack](#technology-stack)
|
|
13
|
+
- [Code structure](#code-structure)
|
|
14
|
+
- [Historical context](#historical-context)
|
|
15
|
+
- [About](#about)
|
|
16
|
+
|
|
17
|
+
## Functionality
|
|
18
|
+
Convert IPv4 address strings to 32-bit unsigned integers and vice versa. Enables efficient IP address arithmetic, range calculations, and compact storage.
|
|
19
|
+
|
|
20
|
+
## Usage demonstration
|
|
21
|
+
Install the package:
|
|
22
|
+
```bash
|
|
23
|
+
npm install @1-/ipv4
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Use in JavaScript modules:
|
|
27
|
+
```javascript
|
|
28
|
+
import ipv4ToU32 from '@1-/ipv4/ipv4_u32';
|
|
29
|
+
import u32ToIpv4 from '@1-/ipv4/u32_ipv4';
|
|
30
|
+
|
|
31
|
+
// Convert IPv4 string to integer
|
|
32
|
+
const ipInt = ipv4ToU32('192.168.1.1'); // 3232235777
|
|
33
|
+
|
|
34
|
+
// Convert integer back to IPv4 string
|
|
35
|
+
const ipStr = u32ToIpv4(3232235777); // '192.168.1.1'
|
|
36
|
+
|
|
37
|
+
// Perform IP address arithmetic
|
|
38
|
+
const networkStart = ipv4ToU32('192.168.0.0');
|
|
39
|
+
const networkEnd = ipv4ToU32('192.168.255.255');
|
|
40
|
+
const totalAddresses = networkEnd - networkStart + 1; // 65536
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Design rationale
|
|
44
|
+
The library uses bit manipulation for optimal performance:
|
|
45
|
+
- IPv4 string parsing splits by dots and converts each octet
|
|
46
|
+
- Bit shifting combines octets into 32-bit integer representation
|
|
47
|
+
- Bitwise AND operations extract octets from integer representation
|
|
48
|
+
- Uses unsigned right shift (`>>>`) to handle 32-bit integer boundaries correctly
|
|
49
|
+
|
|
50
|
+

|
|
51
|
+
|
|
52
|
+
## Technology stack
|
|
53
|
+
- JavaScript ES modules
|
|
54
|
+
- Node.js runtime
|
|
55
|
+
- @3-/int dependency for integer conversion
|
|
56
|
+
|
|
57
|
+
## Code structure
|
|
58
|
+
```
|
|
59
|
+
src/
|
|
60
|
+
├── ipv4_u32.js # Converts IPv4 string → 32-bit integer
|
|
61
|
+
└── u32_ipv4.js # Converts 32-bit integer → IPv4 string
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Historical context
|
|
65
|
+
The IPv4 addressing scheme was standardized in 1981 with RFC 791. Its 32-bit design allowed approximately 4.3 billion unique addresses, which seemed abundant at the time. The conversion between dotted-decimal notation and integer representation became essential for network programming, enabling efficient subnet calculations and routing table implementations. This library implements the fundamental conversion operations that underpin modern network infrastructure.
|
|
66
|
+
|
|
67
|
+
## About
|
|
68
|
+
|
|
69
|
+
This library is developed by [WebC.site](https://webc.site).
|
|
70
|
+
|
|
71
|
+
[WebC.site](https://webc.site): A new paradigm of web development for AI
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
<a id="zh"></a>
|
|
77
|
+
# @1-/ipv4 : IPv4 地址与 32 位整数双向转换
|
|
78
|
+
|
|
79
|
+
- [@1-/ipv4 : IPv4 地址与 32 位整数双向转换](#1-ipv4-ipv4-地址与-32-位整数双向转换)
|
|
80
|
+
- [功能介绍](#功能介绍)
|
|
81
|
+
- [使用演示](#使用演示)
|
|
82
|
+
- [设计思路](#设计思路)
|
|
83
|
+
- [技术栈](#技术栈)
|
|
84
|
+
- [代码结构](#代码结构)
|
|
85
|
+
- [历史故事](#历史故事)
|
|
86
|
+
- [关于](#关于)
|
|
87
|
+
|
|
88
|
+
## 功能介绍
|
|
89
|
+
实现 IPv4 地址字符串与 32 位无符号整数之间的双向转换。支持高效 IP 地址算术运算、范围计算及紧凑存储。
|
|
90
|
+
|
|
91
|
+
## 使用演示
|
|
92
|
+
安装包:
|
|
93
|
+
```bash
|
|
94
|
+
npm install @1-/ipv4
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
在 JavaScript 模块中使用:
|
|
98
|
+
```javascript
|
|
99
|
+
import ipv4ToU32 from '@1-/ipv4/ipv4_u32';
|
|
100
|
+
import u32ToIpv4 from '@1-/ipv4/u32_ipv4';
|
|
101
|
+
|
|
102
|
+
// 将 IPv4 字符串转换为整数
|
|
103
|
+
const ipInt = ipv4ToU32('192.168.1.1'); // 3232235777
|
|
104
|
+
|
|
105
|
+
// 将整数转换回 IPv4 字符串
|
|
106
|
+
const ipStr = u32ToIpv4(3232235777); // '192.168.1.1'
|
|
107
|
+
|
|
108
|
+
// 执行 IP 地址算术运算
|
|
109
|
+
const networkStart = ipv4ToU32('192.168.0.0');
|
|
110
|
+
const networkEnd = ipv4ToU32('192.168.255.255');
|
|
111
|
+
const totalAddresses = networkEnd - networkStart + 1; // 65536
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## 设计思路
|
|
115
|
+
库采用位操作实现最优性能:
|
|
116
|
+
- IPv4 字符串解析通过点号分割并转换各八位组
|
|
117
|
+
- 位移操作将八位组组合为 32 位整数表示
|
|
118
|
+
- 位与操作从整数表示中提取八位组
|
|
119
|
+
- 使用无符号右移(`>>>`)正确处理 32 位整数边界
|
|
120
|
+
|
|
121
|
+

|
|
122
|
+
|
|
123
|
+
## 技术栈
|
|
124
|
+
- JavaScript ES 模块
|
|
125
|
+
- Node.js 运行时
|
|
126
|
+
- @3-/int 依赖项用于整数转换
|
|
127
|
+
|
|
128
|
+
## 代码结构
|
|
129
|
+
```
|
|
130
|
+
src/
|
|
131
|
+
├── ipv4_u32.js # IPv4 字符串 → 32 位整数转换
|
|
132
|
+
└── u32_ipv4.js # 32 位整数 → IPv4 字符串转换
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## 历史故事
|
|
136
|
+
IPv4 地址方案于 1981 年 RFC 791 标准化。其 32 位设计可提供约 43 亿个唯一地址,在当时看似充足。IPv4 点分十进制表示法与整数表示法之间的转换成为网络编程基础,支撑子网计算和路由表实现。本库实现了现代网络基础设施所依赖的核心转换操作。
|
|
137
|
+
|
|
138
|
+
## 关于
|
|
139
|
+
|
|
140
|
+
本库由 [WebC.site](https://webc.site) 开发。
|
|
141
|
+
|
|
142
|
+
[WebC.site](https://webc.site) : 面向人工智能的网站开发新范式
|
|
143
|
+
|
package/ipv4_u32.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@1-/ipv4",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Convert IPv4 addresses to and from 32-bit integers / 将 IPv4 地址与 32 位整数相互转换",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"homepage": "https://github.com/webc-site/npm/tree/main/ipv4",
|
|
7
|
+
"license": "MulanPSL-2.0",
|
|
8
|
+
"author": "x-at-01@googlegroups.com",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/webc-site/npm.git"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
"./*": "./*"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@3-/int": "^0.1.1"
|
|
19
|
+
}
|
|
20
|
+
}
|