generate-ip 1.0.0 → 2.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.
- package/README.md +223 -112
- package/dist/generate-ip.min.js +11 -0
- package/package.json +20 -6
- package/generate-ip.js +0 -107
package/README.md
CHANGED
|
@@ -1,112 +1,223 @@
|
|
|
1
|
-
# > generate-ip
|
|
2
|
-
|
|
3
|
-
### Randomly generate
|
|
4
|
-
|
|
5
|
-
<a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
6
|
-
<a href="https://
|
|
7
|
-
<a href="https://www.npmjs.com/package/generate-ip?activeTab=code"><img height=31 src="https://img.shields.io/npm/unpacked-size/generate-ip?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a>
|
|
8
|
-
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:generate-ip/generate-ip.js"><img height=31 src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fsonarcloud.io%2Fapi%2Fmeasures%2Fcomponent%3Fcomponent%3Dadamlui_js-utils%3Agenerate-ip%2Fgenerate-ip.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonarcloud&logoColor=white&labelColor=464646&label=Vulnerabilities&color=gold"></a>
|
|
9
|
-
|
|
10
|
-
<br>
|
|
11
|
-
|
|
12
|
-
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
13
|
-
|
|
14
|
-
## ⚡ Installation
|
|
15
|
-
|
|
16
|
-
As a **global utility**:
|
|
17
|
-
|
|
18
|
-
```
|
|
19
|
-
$ npm install -g generate-ip
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
As a **
|
|
23
|
-
|
|
24
|
-
```
|
|
25
|
-
$ npm install generate-ip
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
####
|
|
43
|
-
|
|
44
|
-
```js
|
|
45
|
-
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
1
|
+
# > generate-ip
|
|
2
|
+
|
|
3
|
+
### Randomly generate IPv4 and IPv6 addresses.
|
|
4
|
+
|
|
5
|
+
<a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
6
|
+
<a href="https://github.com/adamlui/js-utils/releases/tag/generate-ip-2.0.1"><img height=31 src="https://img.shields.io/badge/Latest_Build-2.0.1-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/generate-ip?activeTab=code"><img height=31 src="https://img.shields.io/npm/unpacked-size/generate-ip?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a>
|
|
8
|
+
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:generate-ip/generate-ip.js"><img height=31 src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fsonarcloud.io%2Fapi%2Fmeasures%2Fcomponent%3Fcomponent%3Dadamlui_js-utils%3Agenerate-ip%2Fgenerate-ip.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonarcloud&logoColor=white&labelColor=464646&label=Vulnerabilities&color=gold"></a>
|
|
9
|
+
|
|
10
|
+
<br>
|
|
11
|
+
|
|
12
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
13
|
+
|
|
14
|
+
## ⚡ Installation
|
|
15
|
+
|
|
16
|
+
As a **global utility**:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
$ npm install -g generate-ip
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
As a **dev dependency**, from your project root:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
$ npm install -D generate-ip
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
As a **runtime dependency**, from your project root:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
$ npm install generate-ip
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
<br>
|
|
35
|
+
|
|
36
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
37
|
+
|
|
38
|
+
## 🔌 API usage
|
|
39
|
+
|
|
40
|
+
**generate-ip** provides the [**`ipv4`**](#ipv4-methods) and [**`ipv6`**](#ipv6-methods) APIs that can be imported into your app as ECMAScript modules or CommonJS modules.
|
|
41
|
+
|
|
42
|
+
#### ESM:
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
import { ipv4, ipv6 } from 'generate-ip';
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### CJS:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
const { ipv4, ipv6 } = require('generate-ip');
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
#
|
|
55
|
+
|
|
56
|
+
### `ipv4` methods
|
|
57
|
+
|
|
58
|
+
Use the `ipv4` methods to [**generate**](#ipv4generateoptions) and [**validate**](#ipv4validateaddress-options) IPv4 addresses.
|
|
59
|
+
|
|
60
|
+
#
|
|
61
|
+
|
|
62
|
+
#### `ipv4.generate([options])`
|
|
63
|
+
|
|
64
|
+
Generates **one** IPv4 address:
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
const ip = ipv4.generate();
|
|
68
|
+
console.log(ip); // sample output: 36.42.224.208
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Available options (passed as object properties):
|
|
72
|
+
|
|
73
|
+
Name | Type | Description | Default Value
|
|
74
|
+
------------|---------|--------------------------------------------------------------------------------|---------------
|
|
75
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
76
|
+
|
|
77
|
+
#
|
|
78
|
+
|
|
79
|
+
#### `ipv4.validate(address[, options])`
|
|
80
|
+
|
|
81
|
+
Checks if given `address` is a valid IPv4 address:
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
const ipIsValid = ipv4.validate('36.42.224.208');
|
|
85
|
+
console.log(ipIsValid);
|
|
86
|
+
|
|
87
|
+
/* outputs:
|
|
88
|
+
|
|
89
|
+
ipv4.validate() » Validating 36.42.224.208...
|
|
90
|
+
ipv4.validate() » IP is valid IPv4 address!
|
|
91
|
+
true
|
|
92
|
+
*/
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Available options (passed as object properties):
|
|
96
|
+
|
|
97
|
+
Name | Type | Description | Default Value
|
|
98
|
+
------------|---------|--------------------------------------------------------------------------------|---------------
|
|
99
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
100
|
+
|
|
101
|
+
#
|
|
102
|
+
|
|
103
|
+
### `ipv6` methods
|
|
104
|
+
|
|
105
|
+
Use the `ipv6` methods to [**generate**](#ipv6generateoptions), [**format**](#ipv6formatipv6address-options), and [**validate**](#ipv6validateaddress-options) IPv6 addresses.
|
|
106
|
+
|
|
107
|
+
#
|
|
108
|
+
|
|
109
|
+
#### `ipv6.generate([options])`
|
|
110
|
+
|
|
111
|
+
Generates **one** IPv6 address, returned as a string:
|
|
112
|
+
|
|
113
|
+
```js
|
|
114
|
+
const ip = ipv6.generate();
|
|
115
|
+
console.log(ip); // sample output: 1379:6748:810c:5e16:b6c9:ae2:939f:8f2a
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Available options (passed as object properties):
|
|
119
|
+
|
|
120
|
+
Name | Type | Description | Default Value
|
|
121
|
+
---------------|---------|--------------------------------------------------------------------------------|---------------
|
|
122
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
123
|
+
`leadingZeros` | Boolean | Include leading zeros in hex pieces. | `false`
|
|
124
|
+
`doubleColon` | Boolean | Replace series of zeros w/ `::` | `true`
|
|
125
|
+
|
|
126
|
+
#
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
#### `ipv6.format(address[, options])`
|
|
130
|
+
|
|
131
|
+
Formats an IPv6 address according to `options` passed, returning a string:
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
const address = '0d::ffff:192.1.56.10/96'
|
|
135
|
+
formattedAddress = ipv6.format(address, { leadingZeros: true, doubleColon: false });
|
|
136
|
+
|
|
137
|
+
/* outputs:
|
|
138
|
+
|
|
139
|
+
ipv6.format() » Formatting 0d::ffff:192.1.56.10/96...
|
|
140
|
+
ipv6.format() » Expanding '::' into zero series...
|
|
141
|
+
ipv6.format() » Adding leading zeros...
|
|
142
|
+
|
|
143
|
+
000d:0000:0000:0000:0000:0000:ffff:192.1.56.10/96
|
|
144
|
+
*/
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Available options:
|
|
148
|
+
|
|
149
|
+
Name | Type | Description | Default Value
|
|
150
|
+
---------------|---------|--------------------------------------------------------------------------------|---------------
|
|
151
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
152
|
+
`leadingZeros` | Boolean | Include leading zeros in hex pieces. | `false`
|
|
153
|
+
`doubleColon` | Boolean | Replace series of zeros w/ `::` | `true`
|
|
154
|
+
|
|
155
|
+
#
|
|
156
|
+
|
|
157
|
+
#### `ipv6.validate(address[, options])`
|
|
158
|
+
|
|
159
|
+
Checks if given `address` is a valid IPv6 address:
|
|
160
|
+
|
|
161
|
+
```js
|
|
162
|
+
const ipIsValid = ipv6.validate('0:0:0:0:0:ffff:192.1.56.10/96');
|
|
163
|
+
console.log(ipIsValid);
|
|
164
|
+
|
|
165
|
+
/* outputs:
|
|
166
|
+
|
|
167
|
+
ipv6.validate() » Validating 0:0:0:0:0:ffff:192.1.56.10/96...
|
|
168
|
+
ipv6.validate() » IP is valid IPv6 address!
|
|
169
|
+
true
|
|
170
|
+
*/
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Available options (passed as object properties):
|
|
174
|
+
|
|
175
|
+
Name | Type | Description | Default Value
|
|
176
|
+
------------|---------|--------------------------------------------------------------------------------|---------------
|
|
177
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
178
|
+
|
|
179
|
+
<br>
|
|
180
|
+
|
|
181
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
182
|
+
|
|
183
|
+
## 💻 Command line usage
|
|
184
|
+
|
|
185
|
+
When installed [globally](#-installation), **generate-ip** can also be used from the command line. The basic command is:
|
|
186
|
+
|
|
187
|
+
```
|
|
188
|
+
$ generate-ip
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Generated IP is logged to the console + copied to the clipboard.
|
|
192
|
+
|
|
193
|
+
#
|
|
194
|
+
|
|
195
|
+
### Command line options
|
|
196
|
+
|
|
197
|
+
```
|
|
198
|
+
Info commands:
|
|
199
|
+
-h, --help Display help screen.
|
|
200
|
+
-v, --version Show version number.
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
<br>
|
|
204
|
+
|
|
205
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
206
|
+
|
|
207
|
+
## 🏛️ MIT License
|
|
208
|
+
|
|
209
|
+
**Copyright © 2024 [Adam Lui](https://github.com/adamlui)**
|
|
210
|
+
|
|
211
|
+
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:
|
|
212
|
+
|
|
213
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
214
|
+
|
|
215
|
+
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.
|
|
216
|
+
|
|
217
|
+
<br>
|
|
218
|
+
|
|
219
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
220
|
+
|
|
221
|
+
<a href="https://github.com/adamlui/js-utils">**Home**</a> /
|
|
222
|
+
<a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
|
|
223
|
+
<a href="#-generate-ip">Back to top ↑</a>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const s=require("crypto")["randomInt"],n=require("child_process")["execSync"],i={generate:function(e={}){var o={verbose:!0};e={...o,...e};for(const t of Object.keys(e))if(!Object.prototype.hasOwnProperty.call(o,t))return console.error(`ipv4.generate() » ERROR: \`${t}\` is an invalid option.`+`
|
|
3
|
+
ipv4.generate() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof e.verbose)return console.error("ipv4.generate() » ERROR: [verbose] option can only be `true` or `false`.");e.verbose&&console.info("ipv4.generate() » Generating IPv4 address...");var r=[];for(let e=0;e<4;e++)r.push(s(0,256));var n=r.join(".");return e.verbose&&console.log("\n"+n),n},validate:function(e,o={}){var r={verbose:!0};if(o={...r,...o},"string"!=typeof e)return console.error("ipv4.validate() » ERROR: 1st arg `address` must be a string.");for(const n of Object.keys(o))if(!Object.prototype.hasOwnProperty.call(r,n))return console.error(`ipv4.validate() » ERROR: \`${n}\` is an invalid option.`+`
|
|
4
|
+
ipv4.validate() » Valid options: [ ${Object.keys(r).join(", ")} ]`);if("boolean"!=typeof o.verbose)return console.error("ipv4.validate() » ERROR: [verbose] option can only be `true` or `false`.");o.verbose&&console.info(`ipv4.validate() » Validating ${e}...`);e=e.split("."),e=!(4!==e.length||e.some(e=>!/^\d+$/.test(e)||parseInt(e,10)<0||255<parseInt(e,10)));return o.verbose&&console.info(`ipv4.validate() » IP is ${e?"":"in"}valid IPv4 address!`),e}},e={generate:function(e={}){var o={verbose:!0,leadingZeros:!1,doubleColon:!0};for(const t in e={...o,...e}){if(!Object.prototype.hasOwnProperty.call(o,t))return console.error(`ipv6.generate() » ERROR: \`${t}\` is an invalid option.`+`
|
|
5
|
+
ipv6.generate() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof e[t])return console.error(`ipv6.generate() » ERROR: [${t}] option can only be \`true\` or \`false\`.`)}e.verbose&&console.info("ipv6.generate() » Generating IPv6 address...");var r=[];for(let e=0;e<8;e++){let o="";for(let e=0;e<4;e++)o+=s(0,16).toString(16);r.push(o)}var n=this.format(r.join(":"),{...e,verbose:!1});return e.verbose&&console.log("\n"+n),n},format:function(e,o={}){var r,n,t={verbose:!0,leadingZeros:!1,doubleColon:!0};if(o={...t,...o},"string"!=typeof e)return console.error("ipv6.format() » ERROR: 1st arg `address` must be a string.");for(const a in o){if(!Object.prototype.hasOwnProperty.call(t,a))return console.error(`ipv6.format() » ERROR: \`${a}\` is an invalid option.`+`
|
|
6
|
+
ipv6.format() » Valid options: [ ${Object.keys(t).join(", ")} ]`);if("boolean"!=typeof o[a])return console.error(`ipv6.format() » ERROR: [${a}] option can only be \`true\` or \`false\`.`)}if(!this.validate(e,{verbose:!1}))return console.error(`ipv6.format() » ERROR:
|
|
7
|
+
- ${e} is not a valid IPv6 address.`);o.verbose&&console.info(`ipv6.format() » Formatting ${e}...`);let s=e;if(s=o.doubleColon?(o.verbose&&console.info("ipv6.format() » Replacing zero series w/ '::'..."),s.replace(/:(?:0+:)+/,"::")):(o.verbose&&console.info("ipv6.format() » Expanding '::' into zero series..."),n=s.split(":").filter(Boolean).length,r=o.leadingZeros?"0000":"0",n=Array(8-n).fill(r).join(":"),s.replace("::",`:${n}:`)),o.leadingZeros){o.verbose&&console.info("ipv6.format() » Adding leading zeros...");var i=s.split(":");for(let e=0;e<i.length;e++)for(;i[e].length<4;)i[e]="0"+i[e];s=i.join(":")}else o.verbose&&console.info("ipv6.format() » Stripping leading zeros..."),s=e.replace(/(^|(?<=:))0+(?!:)/g,"$1");return o.verbose&&console.info(s!==e?s:"ipv6.format() » Address cannot be formatted!\n"),s},validate:function(e,o={}){var r={verbose:!0};if(o={...r,...o},"string"!=typeof e)return console.error("ipv6.validate() » ERROR: 1st arg `address` must be a string.");for(const s of Object.keys(o))if(!Object.prototype.hasOwnProperty.call(r,s))return console.error(`ipv6.validate() » ERROR: \`${s}\` is an invalid option.`+`
|
|
8
|
+
ipv6.validate() » Valid options: [ ${Object.keys(r).join(", ")} ]`);if("boolean"!=typeof o.verbose)return console.error("ipv6.validate() » ERROR: [verbose] option can only be `true` or `false`.");o.verbose&&console.info(`ipv6.validate() » Validating ${e}...`);const n=e.split(/::?/),t=n[n.length-1];e=!(e.includes("::")&&2<e.split("::").length||/:{3,}/g.test(e)||n.length<2||8<n.length||n.some(e=>!(/^[\dA-Fa-f]{1,4}$/.test(e)||e===t&&i.validate(t.replace(/\/(?:0|(?:[1-2]?\d)|32|96)$/,""),{verbose:!1}))));return o.verbose&&console.info(`ipv6.validate() » IP is ${e?"":"in"}valid IPv6 address!`),e}};if(require.main!==module)module.exports={ipv4:i,ipv6:e};else{const l="[0m",p="[1;91m",c="[1;33m",v="[1;97m",f={infoCmds:{help:/^--?h(?:elp)?$/,version:/^--?ve?r?s?i?o?n?$/}};let o=!1,r;for(const d of Object.keys(f))for(const b of Object.keys(f[d]))o||process.argv.forEach(e=>{e.startsWith("-")&&(r=f[d][b].test(e)?void(o=!0):e)});if(r&&(console.error(`
|
|
9
|
+
${p}ERROR: Arg [${r}] not recognized.`+l),console.info(`
|
|
10
|
+
${c}Valid arguments are below.`+l),t(["infoCmds"]),process.exit(1)),process.argv.some(e=>/^--?h(?:elp)?$/.test(e)))t();else if(process.argv.some(e=>/^--?ve?r?s?i?o?n?$/.test(e)))console.info("v"+require("./package.json").version);else{const g=i.generate({verbose:!1});a(g),console.log(v+g+l)}function t(e=["cmdFormat","formatOptions","infoCmds"]){const o={cmdFormat:[`
|
|
11
|
+
${c}generate-ip [commands]`+l],infoCmds:["\nInfo commands:"," -h, --help Display help screen."," -v, --version Show version number."]};e.forEach(e=>{o[e]?.forEach(e=>{{const n=process.stdout.columns||80,t=[],o=e.match(/\S+|\s+/g);let r="";o.forEach(e=>{var o=n-(0===t.length?0:29);r.length+e.length>o&&(t.push(0===t.length?r:r.trimStart()),r=""),r+=e}),t.push(0===t.length?r:r.trimStart()),t.forEach((e,o)=>console.info(0===o?e:" ".repeat(29)+e))}})})}function a(e){e=e.replace(/\s+$/,"").replace(/"/g,'""'),"darwin"===process.platform?n(`printf "${e}" | pbcopy`):"linux"===process.platform?n(`printf "${e}" | xclip -selection clipboard`):"win32"===process.platform&&n(`Set-Clipboard -Value "${e}"`,{shell:"powershell"})}}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "generate-ip",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Randomly generate
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "Randomly generate IPv4 and IPv6 addresses.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
7
7
|
"email": "adam@kudoai.com",
|
|
@@ -9,13 +9,24 @@
|
|
|
9
9
|
},
|
|
10
10
|
"homepage": "https://github.com/adamlui/js-utils",
|
|
11
11
|
"license": "MIT",
|
|
12
|
-
"main": "generate-ip.js",
|
|
12
|
+
"main": "dist/generate-ip.min.js",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"docs"
|
|
16
|
+
],
|
|
13
17
|
"bin": {
|
|
14
|
-
"generateip": "generate-ip.js",
|
|
15
|
-
"generate-ip": "generate-ip.js"
|
|
18
|
+
"generateip": "dist/generate-ip.min.js",
|
|
19
|
+
"generate-ip": "dist/generate-ip.min.js"
|
|
16
20
|
},
|
|
17
21
|
"scripts": {
|
|
18
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
22
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
23
|
+
"build": "minify-js generate-ip.js dist",
|
|
24
|
+
"bump:patch": "bash utils/bump.sh patch",
|
|
25
|
+
"bump:minor": "bash utils/bump.sh minor",
|
|
26
|
+
"bump:major": "bash utils/bump.sh major",
|
|
27
|
+
"publish:patch": "bash utils/bump.sh patch --publish",
|
|
28
|
+
"publish:minor": "bash utils/bump.sh minor --publish",
|
|
29
|
+
"publish:major": "bash utils/bump.sh major --publish"
|
|
19
30
|
},
|
|
20
31
|
"repository": {
|
|
21
32
|
"type": "git",
|
|
@@ -39,5 +50,8 @@
|
|
|
39
50
|
"funding": {
|
|
40
51
|
"type": "github",
|
|
41
52
|
"url": "https://github.com/sponsors/adamlui"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@adamlui/minify.js": "^1.4.5"
|
|
42
56
|
}
|
|
43
57
|
}
|
package/generate-ip.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
// Import crypto.randomInt() for secure RNG
|
|
4
|
-
const { randomInt } = require('crypto');
|
|
5
|
-
|
|
6
|
-
// Define MAIN functions
|
|
7
|
-
|
|
8
|
-
function generateIPv4() {
|
|
9
|
-
console.info('Generating IPv4 address...');
|
|
10
|
-
const segments = [];
|
|
11
|
-
for (let i = 0; i < 4; i++) segments.push(randomInt(0, 256));
|
|
12
|
-
return segments.join('.');
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function isValidIPv4(address) {
|
|
16
|
-
console.info(`Validating IPv4 address ${ address }...`);
|
|
17
|
-
const segments = address.split('.');
|
|
18
|
-
if (segments.length !== 4) return false;
|
|
19
|
-
for (const segment of segments) {
|
|
20
|
-
const num = parseInt(segment);
|
|
21
|
-
if (isNaN(num) || num < 0 || num > 255) return false;
|
|
22
|
-
}
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// EXPORT functions if script was required
|
|
27
|
-
if (require.main !== module) module.exports = { generateIPv4, isValidIPv4 };
|
|
28
|
-
|
|
29
|
-
else { // run as CLI utility
|
|
30
|
-
|
|
31
|
-
// Init UI colors
|
|
32
|
-
const nc = '\x1b[0m', // no color
|
|
33
|
-
br = '\x1b[1;91m', // bright red
|
|
34
|
-
by = '\x1b[1;33m', // bright yellow
|
|
35
|
-
bw = '\x1b[1;97m'; // bright white
|
|
36
|
-
|
|
37
|
-
// Load settings from ARGS
|
|
38
|
-
const argRegex = {
|
|
39
|
-
infoCmds: {
|
|
40
|
-
'help': /^--?h(?:elp)?$/,
|
|
41
|
-
'version': /^--?ve?r?s?i?o?n?$/
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
let matchedArg = false, unrecognizedArg = undefined;
|
|
45
|
-
for (const argType of Object.keys(argRegex))
|
|
46
|
-
for (const option of Object.keys(argRegex[argType]))
|
|
47
|
-
if (!matchedArg) process.argv.forEach(arg => {
|
|
48
|
-
if (!arg.startsWith('-')) return;
|
|
49
|
-
if (argRegex[argType][option].test(arg)) {
|
|
50
|
-
matchedArg = true; unrecognizedArg = undefined; return;
|
|
51
|
-
} else unrecognizedArg = arg;
|
|
52
|
-
});
|
|
53
|
-
if (unrecognizedArg) {
|
|
54
|
-
console.error(`\n${br}ERROR: Arg [${unrecognizedArg}] not recognized.${nc}`);
|
|
55
|
-
console.info(`\n${by}Valid arguments are below.${nc}`);
|
|
56
|
-
printHelpScreen(['infoCmds']);
|
|
57
|
-
process.exit(1);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Show HELP screen if -h or --help passed
|
|
61
|
-
if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) printHelpScreen();
|
|
62
|
-
|
|
63
|
-
// Show VERSION number if -v or --version passed
|
|
64
|
-
else if (process.argv.some(arg => /^--?ve?r?s?i?o?n?$/.test(arg)))
|
|
65
|
-
console.info('v' + require('./package.json').version);
|
|
66
|
-
|
|
67
|
-
else // log RESULT
|
|
68
|
-
console.log(bw + generateIPv4() + nc);
|
|
69
|
-
|
|
70
|
-
function printHelpMsg(msg) { // wrap msg + indent 2nd+ lines (for --help screen)
|
|
71
|
-
const terminalWidth = process.stdout.columns || 80,
|
|
72
|
-
indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
|
|
73
|
-
|
|
74
|
-
// Split msg into lines of appropriate lengths
|
|
75
|
-
let currentLine = '';
|
|
76
|
-
words.forEach(word => {
|
|
77
|
-
const lineLength = terminalWidth - ( lines.length === 0 ? 0 : indentation );
|
|
78
|
-
if (currentLine.length + word.length > lineLength) { // cap/store it
|
|
79
|
-
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
80
|
-
currentLine = '';
|
|
81
|
-
}
|
|
82
|
-
currentLine += word;
|
|
83
|
-
});
|
|
84
|
-
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
85
|
-
|
|
86
|
-
// Print formatted msg
|
|
87
|
-
lines.forEach((line, index) => console.info(
|
|
88
|
-
index === 0 ? line // print 1st line unindented
|
|
89
|
-
: ' '.repeat(indentation) + line // print subsequent lines indented
|
|
90
|
-
));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function printHelpScreen(includeSections = ['cmdFormat', 'formatOptions', 'infoCmds']) {
|
|
94
|
-
const sections = {
|
|
95
|
-
'cmdFormat': [
|
|
96
|
-
`\n${by}generate-ip [commands]${nc}`
|
|
97
|
-
],
|
|
98
|
-
'infoCmds': [
|
|
99
|
-
'\nInfo commands:',
|
|
100
|
-
' -h, --help Display help screen.',
|
|
101
|
-
' -v, --version Show version number.'
|
|
102
|
-
]
|
|
103
|
-
};
|
|
104
|
-
includeSections.forEach(section => { // print valid arg elems
|
|
105
|
-
sections[section]?.forEach(line => printHelpMsg(line)); });
|
|
106
|
-
}
|
|
107
|
-
}
|