generate-ip 2.2.1 โ 2.2.3
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 +11 -11
- package/dist/cli.min.js +8 -5
- package/dist/generate-ip.min.js +1 -1
- package/docs/README.md +348 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
### Randomly generate, format, and validate IPv4/IPv6 addresses.
|
|
4
4
|
|
|
5
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.2.
|
|
6
|
+
<a href="https://github.com/adamlui/js-utils/releases/tag/generate-ip-2.2.3"><img height=31 src="https://img.shields.io/badge/Latest_Build-2.2.3-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
7
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://github.com/adamlui/js-utils/blob/generate-ip-2.2.
|
|
8
|
+
<a href="https://github.com/adamlui/js-utils/blob/generate-ip-2.2.3/generate-ip/dist/generate-ip.min.js"><img height=31 src="https://img.shields.io/github/size/adamlui/js-utils/generate-ip/dist/generate-ip.min.js?branch=generate-ip-2.2.3&label=Minified%20Size&logo=databricks&logoColor=white&labelColor=464646&color=ff69b4&style=for-the-badge"></a>
|
|
9
9
|
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:generate-ip/src/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%2Fsrc%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>
|
|
10
10
|
<a href="https://github.com/toolleeo/cli-apps#networking"><img height=31 src="https://img.shields.io/badge/Mentioned_in-Awesome-c4a2bd?logo=awesomelists&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
11
|
-
<a href="https://www.jsdelivr.com/package/npm/generate-ip?tab=stats"><img height=31 src="https://img.shields.io/jsdelivr/npm/
|
|
11
|
+
<a href="https://www.jsdelivr.com/package/npm/generate-ip?tab=stats"><img height=31 src="https://img.shields.io/jsdelivr/npm/hm/generate-ip?style=for-the-badge&logo=jsdelivr&logoColor=white&label=jsDelivr%20Hits&labelColor=464646&color=49c5d8"></a>
|
|
12
12
|
|
|
13
13
|
<br>
|
|
14
14
|
|
|
@@ -53,7 +53,7 @@ $ npm install generate-ip
|
|
|
53
53
|
|
|
54
54
|
### <img height=18 src="https://i.imgur.com/JIeAdsr.png"> Node.js
|
|
55
55
|
|
|
56
|
-
####
|
|
56
|
+
#### ECMAScript*:
|
|
57
57
|
|
|
58
58
|
```js
|
|
59
59
|
import { ipv4, ipv6 } from 'generate-ip';
|
|
@@ -72,14 +72,14 @@ const { ipv4, ipv6 } = require('generate-ip');
|
|
|
72
72
|
#### <> HTML script tag:
|
|
73
73
|
|
|
74
74
|
```html
|
|
75
|
-
<script src="https://cdn.jsdelivr.net/npm/generate-ip@2.2.
|
|
75
|
+
<script src="https://cdn.jsdelivr.net/npm/generate-ip@2.2.3/dist/generate-ip.min.js"></script>
|
|
76
76
|
```
|
|
77
77
|
|
|
78
78
|
#### ES6:
|
|
79
79
|
|
|
80
80
|
```js
|
|
81
81
|
(async () => {
|
|
82
|
-
await import('https://cdn.jsdelivr.net/npm/generate-ip@2.2.
|
|
82
|
+
await import('https://cdn.jsdelivr.net/npm/generate-ip@2.2.3/dist/generate-ip.min.js');
|
|
83
83
|
// Your code here...
|
|
84
84
|
})();
|
|
85
85
|
```
|
|
@@ -88,7 +88,7 @@ const { ipv4, ipv6 } = require('generate-ip');
|
|
|
88
88
|
|
|
89
89
|
```js
|
|
90
90
|
...
|
|
91
|
-
// @require https://cdn.jsdelivr.net/npm/generate-ip@2.2.
|
|
91
|
+
// @require https://cdn.jsdelivr.net/npm/generate-ip@2.2.3/dist/generate-ip.min.js
|
|
92
92
|
// ==/UserScript==
|
|
93
93
|
|
|
94
94
|
// Your code here...
|
|
@@ -96,7 +96,7 @@ const { ipv4, ipv6 } = require('generate-ip');
|
|
|
96
96
|
|
|
97
97
|
<br>
|
|
98
98
|
|
|
99
|
-
๐ **Note:** To always import the latest version (not recommended in production!) remove the `@2.2.
|
|
99
|
+
๐ **Note:** To always import the latest version (not recommended in production!) remove the `@2.2.3` version tag from the jsDelivr URL: `https://cdn.jsdelivr.net/npm/generate-ip/dist/generate-ip.min.js`
|
|
100
100
|
|
|
101
101
|
<br>
|
|
102
102
|
|
|
@@ -307,7 +307,7 @@ Info commands:
|
|
|
307
307
|
|
|
308
308
|
## ๐๏ธ MIT License
|
|
309
309
|
|
|
310
|
-
**Copyright ยฉ 2024 [Adam Lui](https://github.com/adamlui) & contributors
|
|
310
|
+
**Copyright ยฉ 2024 [Adam Lui](https://github.com/adamlui) & contributors.**
|
|
311
311
|
|
|
312
312
|
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:
|
|
313
313
|
|
|
@@ -330,7 +330,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|
|
330
330
|
[CLI usage](https://github.com/adamlui/js-utils/tree/main/generate-pw#-command-line-usage) /
|
|
331
331
|
[Discuss](https://js-utils.com/discussions)
|
|
332
332
|
|
|
333
|
-
### <
|
|
333
|
+
### <picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/geolocate/media/images/icons/wire-globe/white/icon32.png"><img height=22 src="https://raw.githubusercontent.com/adamlui/js-utils/main/geolocate/media/images/icons/wire-globe/black/icon32.png"></picture> [geolocate](https://js-utils.com/geolocate)
|
|
334
334
|
|
|
335
335
|
> Fetch IP geolocation data from the CLI.
|
|
336
336
|
<br>[Install](https://github.com/adamlui/js-utils/tree/main/geolocate#-installation) /
|
|
@@ -343,6 +343,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|
|
343
343
|
|
|
344
344
|
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
345
345
|
|
|
346
|
-
<picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/media/images/icons/home/white/icon32x27.png"><img height=
|
|
346
|
+
<picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/media/images/icons/home/white/icon32x27.png"><img height=13 src="https://raw.githubusercontent.com/adamlui/js-utils/main/media/images/icons/home/dark-gray/icon32x27.png"></picture> <a href="https://js-utils.com">**More JavaScript utilities**</a> /
|
|
347
347
|
<a href="https://js-utils.com/discussions">Discuss</a> /
|
|
348
348
|
<a href="#-generate-ip">Back to top โ</a>
|
package/dist/cli.min.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const
|
|
3
|
-
${
|
|
4
|
-
${
|
|
5
|
-
${
|
|
6
|
-
${
|
|
2
|
+
const docURL="https://github.com/adamlui/js-utils/tree/main/generate-ip#-command-line-usage",ipv4=require(__dirname.match(/src/)?"./generate-ip":"./generate-ip.min")["ipv4"],execSync=require("child_process")["execSync"],nc="[0m",br="[1;91m",by="[1;33m",config={},argRegex={paramOptions:{qty:/^--?qu?a?n?ti?t?y(?:=.*|$)/},flags:{quietMode:/^--?q(?:uiet)?(?:-?mode)?$/},infoCmds:{help:/^--?h(?:elp)?$/,version:/^--?ve?r?s?i?o?n?$/}};if(process.argv.forEach(arg=>{var matchedParamOption,matchedInfoCmd,matchedFlag;arg.startsWith("-")&&(matchedParamOption=Object.keys(argRegex.paramOptions).find(option=>argRegex.paramOptions[option].test(arg)),matchedFlag=Object.keys(argRegex.flags).find(flag=>argRegex.flags[flag].test(arg)),matchedInfoCmd=Object.keys(argRegex.infoCmds).find(cmd=>argRegex.infoCmds[cmd].test(arg)),matchedFlag?config[matchedFlag]=!0:matchedParamOption?(arg.includes("=")||(console.error(`
|
|
3
|
+
${br}ERROR: Arg [--${arg.replace(/-/g,"")}] requires '=' followed by a value.`+nc),printHelpCmdAndDocURL(),process.exit(1)),matchedFlag=arg.split("=")[1],config[matchedParamOption]=parseInt(matchedFlag)||matchedFlag):matchedInfoCmd||(console.error(`
|
|
4
|
+
${br}ERROR: Arg [${arg}] not recognized.`+nc),console.info(`
|
|
5
|
+
${by}Valid arguments are below.`+nc),printHelpSections(["paramOptions","flags","infoCmds"]),printHelpCmdAndDocURL(),process.exit(1)))}),process.argv.some(arg=>argRegex.infoCmds.help.test(arg)))printHelpSections();else if(process.argv.some(arg=>argRegex.infoCmds.version.test(arg)))console.info("v"+require("./package.json").version);else{config.qty&&(isNaN(config.qty)||config.qty<1)&&(console.error(`
|
|
6
|
+
${br}Error: [qty] argument can only be > 0.`+nc),printHelpCmdAndDocURL(),process.exit(1));const ipResult=ipv4.generate({qty:config.qty||1,verbose:!config.quietMode});config.quietMode||(1<config.qty&&console.info(`[ ${ipResult.join(", ")} ]`),console.info("\nCopying to clipboard...")),copyToClipboard(Array.isArray(ipResult)?ipResult.join("\n"):ipResult)}function printHelpSections(includeSections=["cmdFormat","paramOptions","flags","infoCmds"]){const helpSections={cmdFormat:[`
|
|
7
|
+
${by}generate-ip [options|commands]`+nc],paramOptions:["\nParameter options:"," --qty=n Generate n IP address(es)."],flags:["\nBoolean options:"," -q, --quiet Suppress all logging except errors."],infoCmds:["\nInfo commands:"," -h, --help Display help screen."," -v, --version Show version number."]};includeSections.forEach(section=>{helpSections[section]?.forEach(line=>{{const terminalWidth=process.stdout.columns||80,lines=[],words=line.match(/\S+|\s+/g);let currentLine="";words.forEach(word=>{var lineLength=terminalWidth-(0===lines.length?0:29);currentLine.length+word.length>lineLength&&(lines.push(0===lines.length?currentLine:currentLine.trimStart()),currentLine=""),currentLine+=word}),lines.push(0===lines.length?currentLine:currentLine.trimStart()),lines.forEach((line,index)=>console.info(0===index?line:" ".repeat(29)+line))}})})}function printHelpCmdAndDocURL(){console.info(`
|
|
8
|
+
${by}For more help, type 'generate-ip --help' or visit
|
|
9
|
+
`+(docURL+nc))}function copyToClipboard(data){data=data.replace(/\s+$/,"").replace(/"/g,'""'),"darwin"===process.platform?execSync(`printf "${data}" | pbcopy`):"linux"===process.platform?execSync(`printf "${data}" | xclip -selection clipboard`):"win32"===process.platform&&execSync(`Set-Clipboard -Value "${data}"`,{shell:"powershell"})}
|
package/dist/generate-ip.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let
|
|
1
|
+
let randomInt;try{({randomInt}=require("crypto"))}catch(err){const browserCrypto=window.crypto||window.msCrypto;randomInt=(min,max)=>{var randomVal=browserCrypto?.getRandomValues(new Uint32Array(1))[0]/4294967295||Math.random();return Math.floor(randomVal*(max-min))+min}}const ipv4={generate:function(options={}){var defaultOptions={verbose:!0,qty:1};if(validateOptions(options,defaultOptions,"https://github.com/adamlui/js-utils/tree/main/generate-ip#ipv4generateoptions","ipv4.generate({ verbose: false, qty: 3 })")){(options={...defaultOptions,...options}).verbose&&console.info(`ipv4.generate() ยป Generating IPv4 address${1<options.qty?"es":""}...`);var ips=[];if(1<options.qty)for(let i=0;i<options.qty;i++)ips.push(this.generate({...options,qty:1,verbose:!1}));else{var segments=[];for(let i=0;i<4;i++)segments.push(randomInt(0,256));ips.push(segments.join("."))}defaultOptions=1<options.qty?ips:ips[0];return options.verbose&&(console.info(`ipv4.generate() ยป IPv4 address${1<options.qty?"es":""} generated!`),1===options.qty?console.info("ipv4.generate() ยป "+defaultOptions):"undefined"!=typeof require&&require.main.filename.endsWith("cli.js")||console.info("ipv4.generate() ยป Check returned array.")),defaultOptions}},validate:function(address,options={}){var docURL="https://github.com/adamlui/js-utils/tree/main/generate-ip#ipv4validateaddress-options",defaultOptions={verbose:!0};if("string"!=typeof address)console.error("ipv4.validate() ยป ERROR: 1st arg <address> must be a string."),console.info("ipv4.validate() ยป For more help, please visit "+docURL);else if(validateOptions(options,defaultOptions,docURL,"ipv4.validate('0.0.255.255', { verbose: false })"))return(options={...defaultOptions,...options}).verbose&&console.info("ipv4.validate() ยป Validating IPv4 address..."),defaultOptions=!(4!==(docURL=address.split(".")).length||docURL.some(segment=>!/^\d+$/.test(segment)||parseInt(segment,10)<0||255<parseInt(segment,10))),options.verbose&&console.info(`ipv4.validate() ยป IP is ${defaultOptions?"":"in"}valid IPv4 address!`),defaultOptions}},ipv6={generate:function(options={}){var defaultOptions={verbose:!0,qty:1,leadingZeros:!1,doubleColon:!0};if(validateOptions(options,defaultOptions,"https://github.com/adamlui/js-utils/tree/main/generate-ip#ipv6generateoptions","ipv6.generate({ leadingZeros: true, qty: 5 })")){(options={...defaultOptions,...options}).verbose&&console.info(`ipv6.generate() ยป Generating IPv6 address${1<options.qty?"es":""}...`);var ips=[];if(1<options.qty)for(let i=0;i<options.qty;i++)ips.push(this.generate({...options,qty:1,verbose:!1}));else{const pieces=[],{qty,...nonQtyOptions}=options;for(let i=0;i<8;i++){let hex="";for(let j=0;j<4;j++)hex+=randomInt(0,16).toString(16);pieces.push(hex)}ips.push(this.format(pieces.join(":"),{...nonQtyOptions,verbose:!1}))}defaultOptions=1<options.qty?ips:ips[0];return options.verbose&&(console.info(`ipv6.generate() ยป IPv6 address${1<options.qty?"es":""} generated!`),console.info(1===options.qty?"ipv6.generate() ยป "+defaultOptions:"ipv6.generate() ยป Check returned array.")),defaultOptions}},format:function(address,options={}){var zeroSegment,docURL="https://github.com/adamlui/js-utils/tree/main/generate-ip#ipv6formataddress-options",defaultOptions={verbose:!0,leadingZeros:!1,doubleColon:!0};if("string"!=typeof address)console.error("ipv6.format() ยป ERROR: 1st arg <address> must be a string."),console.info("ipv6.format() ยป For more help, please visit "+docURL);else if(this.validate(address,{verbose:!1})){if(validateOptions(options,defaultOptions,docURL,"ipv6.format('0d::ffff:192.1.56.10/96', { leadingZeros: true, doubleColon: false })")){options={...defaultOptions,...options};let formattedAddress=address;if(formattedAddress=options.doubleColon?(options.verbose&&console.info("ipv6.format() ยป Replacing zero series w/ '::'..."),formattedAddress.replace(/:(?:0+:)+/,"::")):(options.verbose&&console.info("ipv6.format() ยป Expanding '::' into zero series..."),defaultOptions=formattedAddress.split(":").filter(Boolean).length,zeroSegment=options.leadingZeros?"0000":"0",defaultOptions=Array(8-defaultOptions).fill(zeroSegment).join(":"),formattedAddress.replace("::",`:${defaultOptions}:`)),options.leadingZeros){options.verbose&&console.info("ipv6.format() ยป Adding leading zeros...");var pieces=formattedAddress.split(":");for(let i=0;i<pieces.length;i++)for(;pieces[i].length<4;)pieces[i]="0"+pieces[i];formattedAddress=pieces.join(":")}else options.verbose&&console.info("ipv6.format() ยป Stripping leading zeros..."),formattedAddress=address.replace(/(^|(?<=:))0+(?!:)/g,"$1");return options.verbose&&(formattedAddress!==address?console.info("ipv6.format() ยป IP formatted successfully!"):console.info("ipv6.format() ยป IP already formatted to specs."),console.info("ipv6.format() ยป "+formattedAddress)),formattedAddress}}else console.error(`ipv6.format() ยป ERROR: ${address} is not a valid IPv6 address.`),console.info("ipv6.format() ยป For more help, please visit "+docURL)},validate:function(address,options={}){var docURL="https://github.com/adamlui/js-utils/tree/main/generate-ip#ipv6validateaddress-options",defaultOptions={verbose:!0};if("string"!=typeof address)console.error("ipv6.validate() ยป ERROR: 1st arg <address> must be a string."),console.info("ipv6.validate() ยป For more help, please visit "+docURL);else if(validateOptions(options,defaultOptions,docURL,"ipv6.validate('0:0:0:0:0:ffff:192.1.56.10/96', { verbose: false })")){(options={...defaultOptions,...options}).verbose&&console.info("ipv6.validate() ยป Validating IPv6 address...");const pieces=address.split(/::?/),lastPiece=pieces[pieces.length-1];docURL=!(address.includes("::")&&2<address.split("::").length||/:{3,}/g.test(address)||pieces.length<2||8<pieces.length||pieces.some(piece=>!(/^[\dA-Fa-f]{1,4}$/.test(piece)||piece===lastPiece&&ipv4.validate(lastPiece.replace(/\/(?:0|(?:[1-2]?\d)|32|96)$/,""),{verbose:!1}))));return options.verbose&&console.info(`ipv6.validate() ยป IP is ${docURL?"":"in"}valid IPv6 address!`),docURL}}};function validateOptions(options,defaultOptions,docURL,exampleCall){const strDefaultOptions=JSON.stringify(defaultOptions,null,2).replace(/"([^"]+)":/g,"$1:").replace(/"/g,"'").replace(/\n\s*/g," "),strValidOptions=Object.keys(defaultOptions).join(", "),booleanOptions=Object.keys(defaultOptions).filter(key=>"boolean"==typeof defaultOptions[key]),integerOptions=Object.keys(defaultOptions).filter(key=>Number.isInteger(defaultOptions[key])),logPrefix=(validateOptions.caller?.name||"validateOptions")+"() ยป ";var printValidOptions=()=>{console.info(logPrefix+`Valid options: [ ${strValidOptions} ]`),console.info(logPrefix+"If omitted, default settings are: "+strDefaultOptions)},printDocURL=()=>{console.info(logPrefix+"For more help, please visit "+docURL)};if("object"!=typeof options)return console.error(logPrefix+"ERROR: [options] can only be an object of key/values."),console.info(logPrefix+"Example valid call: "+exampleCall),printValidOptions(),printDocURL(),!1;for(const key in options){if(!Object.prototype.hasOwnProperty.call(defaultOptions,key))return console.error(logPrefix+`ERROR: \`${key}\` is an invalid option.`),printValidOptions(),printDocURL(),!1;if(booleanOptions.includes(key)&&"boolean"!=typeof options[key])return console.error(logPrefix+`ERROR: [${key}] option can only be \`true\` or \`false\`.`),printDocURL(),!1;if(integerOptions.includes(key)&&(options[key]=parseInt(options[key],10),isNaN(options[key])||options[key]<1))return console.error(logPrefix+`ERROR: [${key}] option can only be an integer > 0.`),printDocURL(),!1}return!0}try{module.exports={ipv4:ipv4,ipv6:ipv6}}catch(err){}try{window.ipv4=ipv4,window.ipv6=ipv6}catch(err){}
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
# > generate-ip
|
|
2
|
+
|
|
3
|
+
### Randomly generate, format, and validate IPv4/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.2.3"><img height=31 src="https://img.shields.io/badge/Latest_Build-2.2.3-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://github.com/adamlui/js-utils/blob/generate-ip-2.2.3/generate-ip/dist/generate-ip.min.js"><img height=31 src="https://img.shields.io/github/size/adamlui/js-utils/generate-ip/dist/generate-ip.min.js?branch=generate-ip-2.2.3&label=Minified%20Size&logo=databricks&logoColor=white&labelColor=464646&color=ff69b4&style=for-the-badge"></a>
|
|
9
|
+
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:generate-ip/src/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%2Fsrc%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>
|
|
10
|
+
<a href="https://github.com/toolleeo/cli-apps#networking"><img height=31 src="https://img.shields.io/badge/Mentioned_in-Awesome-c4a2bd?logo=awesomelists&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
11
|
+
<a href="https://www.jsdelivr.com/package/npm/generate-ip?tab=stats"><img height=31 src="https://img.shields.io/jsdelivr/npm/hm/generate-ip?style=for-the-badge&logo=jsdelivr&logoColor=white&label=jsDelivr%20Hits&labelColor=464646&color=49c5d8"></a>
|
|
12
|
+
|
|
13
|
+
<br>
|
|
14
|
+
|
|
15
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
16
|
+
|
|
17
|
+
## ๐ก About
|
|
18
|
+
|
|
19
|
+
**generate-ip** is a lightweight, easy-to-use library that allows you to randomly generate, format & validate IP address(es).
|
|
20
|
+
|
|
21
|
+
- **No external dependencies โ** Only built-in crypto methods used for secure randomization
|
|
22
|
+
- **Multi-protocol support โ** IPv4 + IPv6 addresses supported
|
|
23
|
+
- **Multi-environment support โ** Use in Node.js or the web browser
|
|
24
|
+
- **Command line usable โ** Just type `generate-ip`, that's it
|
|
25
|
+
|
|
26
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
27
|
+
|
|
28
|
+
## โก Installation
|
|
29
|
+
|
|
30
|
+
As a **global utility**:
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
$ npm install -g generate-ip
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
As a **dev dependency**, from your project root:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
$ npm install -D generate-ip
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
As a **runtime dependency**, from your project root:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
$ npm install generate-ip
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
<br>
|
|
49
|
+
|
|
50
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
51
|
+
|
|
52
|
+
## ๐ Importing the APIs
|
|
53
|
+
|
|
54
|
+
### <img height=18 src="https://i.imgur.com/JIeAdsr.png"> Node.js
|
|
55
|
+
|
|
56
|
+
#### ECMAScript*:
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
import { ipv4, ipv6 } from 'generate-ip';
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
#### CommonJS:
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
const { ipv4, ipv6 } = require('generate-ip');
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
###### _*Node.js version 14 or higher required_
|
|
69
|
+
|
|
70
|
+
### <picture><source media="(prefers-color-scheme: dark)" srcset="https://i.imgur.com/JSEb19A.png"><img width=16 src="https://i.imgur.com/5VPxf9y.png"></picture> Web
|
|
71
|
+
|
|
72
|
+
#### <> HTML script tag:
|
|
73
|
+
|
|
74
|
+
```html
|
|
75
|
+
<script src="https://cdn.jsdelivr.net/npm/generate-ip@2.2.3/dist/generate-ip.min.js"></script>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
#### ES6:
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
(async () => {
|
|
82
|
+
await import('https://cdn.jsdelivr.net/npm/generate-ip@2.2.3/dist/generate-ip.min.js');
|
|
83
|
+
// Your code here...
|
|
84
|
+
})();
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### <img height=17 src="https://raw.githubusercontent.com/KudoAI/chatgpt.js/main/starters/media/images/icons/tampermonkey-icon28.png"><img height=17.5 src="https://raw.githubusercontent.com/KudoAI/chatgpt.js/main/starters/media/images/icons/violentmonkey-icon100.png"> Greasemonkey
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
...
|
|
91
|
+
// @require https://cdn.jsdelivr.net/npm/generate-ip@2.2.3/dist/generate-ip.min.js
|
|
92
|
+
// ==/UserScript==
|
|
93
|
+
|
|
94
|
+
// Your code here...
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
<br>
|
|
98
|
+
|
|
99
|
+
๐ **Note:** To always import the latest version (not recommended in production!) remove the `@2.2.3` version tag from the jsDelivr URL: `https://cdn.jsdelivr.net/npm/generate-ip/dist/generate-ip.min.js`
|
|
100
|
+
|
|
101
|
+
<br>
|
|
102
|
+
|
|
103
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
104
|
+
|
|
105
|
+
## ๐ API usage
|
|
106
|
+
|
|
107
|
+
### `ipv4` methods
|
|
108
|
+
|
|
109
|
+
๐ก Use the `ipv4` methods to [**generate**](#ipv4generateoptions) and [**validate**](#ipv4validateaddress-options) IPv4 addresses.
|
|
110
|
+
|
|
111
|
+
#
|
|
112
|
+
|
|
113
|
+
#### `ipv4.generate([options])`
|
|
114
|
+
|
|
115
|
+
Generates **one** IPv4 address if `qty` option is not given, returning a string:
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
const ip = ipv4.generate();
|
|
119
|
+
console.log(ip); // sample output: '36.42.224.208'
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
...or **multiple** IPv4 addresses if `qty` option is given, returning an array of strings:
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
const ips = ipv4.generate({ qty: 3 });
|
|
126
|
+
console.log(ips);
|
|
127
|
+
|
|
128
|
+
/* sample output:
|
|
129
|
+
|
|
130
|
+
ipv4.generate() ยป Generating IPv4 addresses...
|
|
131
|
+
ipv4.generate() ยป IPv4 addresses generated!
|
|
132
|
+
ipv4.generate() ยป Check returned array.
|
|
133
|
+
[ '194.84.176.172', '192.186.53.120', '50.191.111.87' ]
|
|
134
|
+
*/
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Available options:
|
|
138
|
+
|
|
139
|
+
Name | Type | Description | Default Value
|
|
140
|
+
------------|---------|-------------------------------------|---------------
|
|
141
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
142
|
+
`qty` | Integer | Number of IP addresses to generate. | `1`
|
|
143
|
+
|
|
144
|
+
#
|
|
145
|
+
|
|
146
|
+
#### `ipv4.validate(address[, options])`
|
|
147
|
+
|
|
148
|
+
Checks if given `address` is a valid IPv4 address:
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
const ipIsValid = ipv4.validate('36.42.224.208');
|
|
152
|
+
console.log(ipIsValid);
|
|
153
|
+
|
|
154
|
+
/* outputs:
|
|
155
|
+
|
|
156
|
+
ipv4.validate() ยป Validating 36.42.224.208...
|
|
157
|
+
ipv4.validate() ยป IP is valid IPv4 address!
|
|
158
|
+
true
|
|
159
|
+
*/
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Available options (passed as object properties):
|
|
163
|
+
|
|
164
|
+
Name | Type | Description | Default Value
|
|
165
|
+
------------|---------|-----------------------------------|---------------
|
|
166
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
167
|
+
|
|
168
|
+
#
|
|
169
|
+
|
|
170
|
+
### `ipv6` methods
|
|
171
|
+
|
|
172
|
+
๐ก Use the `ipv6` methods to [**generate**](#ipv6generateoptions), [**format**](#ipv6formatipv6address-options), and [**validate**](#ipv6validateaddress-options) IPv6 addresses.
|
|
173
|
+
|
|
174
|
+
#
|
|
175
|
+
|
|
176
|
+
#### `ipv6.generate([options])`
|
|
177
|
+
|
|
178
|
+
Generates **one** IPv6 address if `qty` option is not given, returning a string:
|
|
179
|
+
|
|
180
|
+
```js
|
|
181
|
+
const ip = ipv6.generate();
|
|
182
|
+
console.log(ip); // sample output: '1379:6748:810c:5e16:b6c9:ae2:939f:8f2a'
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
...or **multiple** IPv6 addresses if `qty` option is given, returning an array of strings:
|
|
186
|
+
|
|
187
|
+
```js
|
|
188
|
+
const ips = ipv4.generate({ qty: 5 });
|
|
189
|
+
console.log(ips);
|
|
190
|
+
|
|
191
|
+
/* sample output:
|
|
192
|
+
|
|
193
|
+
ipv6.generate() ยป Generating IPv6 addresses...
|
|
194
|
+
ipv6.generate() ยป IPv6 addresses generated!
|
|
195
|
+
ipv6.generate() ยป Check returned array.
|
|
196
|
+
[
|
|
197
|
+
'8218:19b9:7709:4282:65e1:7ee:319e:32ef',
|
|
198
|
+
'e940:754d:ae46:ae18:94dd:b43c:583:68c2',
|
|
199
|
+
'b570:b4f8:68f:62e2:99cb:ad0f:6237:9d51',
|
|
200
|
+
'98a7:f4e5:2f4e:8a2d:56bb:dc28:f94a:46a8',
|
|
201
|
+
'ca59:590a:9b6c:ea25:94fa:37d6:9bac:7ff6'
|
|
202
|
+
]
|
|
203
|
+
*/
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Available options:
|
|
207
|
+
|
|
208
|
+
Name | Type | Description | Default Value
|
|
209
|
+
---------------|---------|--------------------------------------|---------------
|
|
210
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
211
|
+
`qty` | Integer | Number of IP addresses to generate. | `1`
|
|
212
|
+
`leadingZeros` | Boolean | Include leading zeros in hex pieces. | `false`
|
|
213
|
+
`doubleColon` | Boolean | Replace series of zeros w/ `::` | `true`
|
|
214
|
+
|
|
215
|
+
#
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
#### `ipv6.format(address[, options])`
|
|
219
|
+
|
|
220
|
+
Formats an IPv6 address according to `options` passed, returning a string:
|
|
221
|
+
|
|
222
|
+
```js
|
|
223
|
+
const address = '0d::ffff:192.1.56.10/96',
|
|
224
|
+
formattedAddress = ipv6.format(address, { leadingZeros: true, doubleColon: false });
|
|
225
|
+
|
|
226
|
+
console.log(formattedAddress);
|
|
227
|
+
|
|
228
|
+
/* outputs:
|
|
229
|
+
|
|
230
|
+
ipv6.format() ยป Expanding '::' into zero series...
|
|
231
|
+
ipv6.format() ยป Adding leading zeros...
|
|
232
|
+
ipv6.format() ยป IP formatted successfully!
|
|
233
|
+
ipv6.format() ยป 000d:0000:0000:0000:0000:0000:ffff:192.1.56.10/96
|
|
234
|
+
'000d:0000:0000:0000:0000:0000:ffff:192.1.56.10/96'
|
|
235
|
+
*/
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Available options:
|
|
239
|
+
|
|
240
|
+
Name | Type | Description | Default Value
|
|
241
|
+
---------------|---------|--------------------------------------|---------------
|
|
242
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
243
|
+
`leadingZeros` | Boolean | Include leading zeros in hex pieces. | `false`
|
|
244
|
+
`doubleColon` | Boolean | Replace series of zeros w/ `::` | `true`
|
|
245
|
+
|
|
246
|
+
#
|
|
247
|
+
|
|
248
|
+
#### `ipv6.validate(address[, options])`
|
|
249
|
+
|
|
250
|
+
Checks if given `address` is a valid IPv6 address:
|
|
251
|
+
|
|
252
|
+
```js
|
|
253
|
+
const ipIsValid = ipv6.validate('0:0:0:0:0:ffff:192.1.56.10/96');
|
|
254
|
+
console.log(ipIsValid);
|
|
255
|
+
|
|
256
|
+
/* outputs:
|
|
257
|
+
|
|
258
|
+
ipv6.validate() ยป Validating 0:0:0:0:0:ffff:192.1.56.10/96...
|
|
259
|
+
ipv6.validate() ยป IP is valid IPv6 address!
|
|
260
|
+
true
|
|
261
|
+
*/
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Available options (passed as object properties):
|
|
265
|
+
|
|
266
|
+
Name | Type | Description | Default Value
|
|
267
|
+
------------|---------|-----------------------------------|---------------
|
|
268
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
269
|
+
|
|
270
|
+
<br>
|
|
271
|
+
|
|
272
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
273
|
+
|
|
274
|
+
## ๐ป Command line usage
|
|
275
|
+
|
|
276
|
+
When installed [globally](#-installation), **generate-ip** can also be used from the command line. The basic command is:
|
|
277
|
+
|
|
278
|
+
```
|
|
279
|
+
$ generate-ip
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
Sample output:
|
|
283
|
+
|
|
284
|
+
<img src="https://github.com/adamlui/js-utils/blob/main/generate-ip/media/images/sample-cli-output.jpg">
|
|
285
|
+
|
|
286
|
+
๐ **Note:** To generate multiple IP addresses, pass `--qty=n` where `n` is the number of IPs to generate.
|
|
287
|
+
|
|
288
|
+
#
|
|
289
|
+
|
|
290
|
+
### Command line options
|
|
291
|
+
|
|
292
|
+
```
|
|
293
|
+
Parameter options:
|
|
294
|
+
--qty=n Generate n IP address(es).
|
|
295
|
+
|
|
296
|
+
Boolean options:
|
|
297
|
+
-q, --quiet Suppress all logging except errors.
|
|
298
|
+
|
|
299
|
+
Info commands:
|
|
300
|
+
-h, --help Display help screen.
|
|
301
|
+
-v, --version Show version number.
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
<br>
|
|
305
|
+
|
|
306
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
307
|
+
|
|
308
|
+
## ๐๏ธ MIT License
|
|
309
|
+
|
|
310
|
+
**Copyright ยฉ 2024 [Adam Lui](https://github.com/adamlui) & contributors.**
|
|
311
|
+
|
|
312
|
+
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:
|
|
313
|
+
|
|
314
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
315
|
+
|
|
316
|
+
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.
|
|
317
|
+
|
|
318
|
+
<br>
|
|
319
|
+
|
|
320
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
321
|
+
|
|
322
|
+
## ๐ ๏ธ Related utilities
|
|
323
|
+
|
|
324
|
+
### [๐ generate-pw](../generate-pw)
|
|
325
|
+
|
|
326
|
+
> Randomly generate, strengthen, and validate cryptographically-secure passwords.
|
|
327
|
+
<br>[Install](https://github.com/adamlui/js-utils/tree/main/generate-pw#-installation) /
|
|
328
|
+
[Readme](https://github.com/adamlui/js-utils/tree/main/generate-pw#readme) /
|
|
329
|
+
[API usage](https://github.com/adamlui/js-utils/tree/main/generate-pw#-api-usage) /
|
|
330
|
+
[CLI usage](https://github.com/adamlui/js-utils/tree/main/generate-pw#-command-line-usage) /
|
|
331
|
+
[Discuss](https://js-utils.com/discussions)
|
|
332
|
+
|
|
333
|
+
### <picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/geolocate/media/images/icons/wire-globe/white/icon32.png"><img height=22 src="https://raw.githubusercontent.com/adamlui/js-utils/main/geolocate/media/images/icons/wire-globe/black/icon32.png"></picture> [geolocate](https://js-utils.com/geolocate)
|
|
334
|
+
|
|
335
|
+
> Fetch IP geolocation data from the CLI.
|
|
336
|
+
<br>[Install](https://github.com/adamlui/js-utils/tree/main/geolocate#-installation) /
|
|
337
|
+
[Readme](https://github.com/adamlui/js-utils/tree/main/geolocate#readme) /
|
|
338
|
+
[CLI usage](https://github.com/adamlui/js-utils/tree/main/geolocate#-command-line-usage) /
|
|
339
|
+
[API usage](https://github.com/adamlui/js-utils/tree/main/geolocate#-api-usage) /
|
|
340
|
+
[Discuss](https://js-utils.com/discussions)
|
|
341
|
+
|
|
342
|
+
<br>
|
|
343
|
+
|
|
344
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
345
|
+
|
|
346
|
+
<picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/media/images/icons/home/white/icon32x27.png"><img height=13 src="https://raw.githubusercontent.com/adamlui/js-utils/main/media/images/icons/home/dark-gray/icon32x27.png"></picture> <a href="https://js-utils.com">**More JavaScript utilities**</a> /
|
|
347
|
+
<a href="https://js-utils.com/discussions">Discuss</a> /
|
|
348
|
+
<a href="#-generate-ip">Back to top โ</a>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "generate-ip",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.3",
|
|
4
4
|
"description": "Randomly generate, format, and validate IPv4/IPv6 addresses.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
27
|
-
"build": "minify-js src dist",
|
|
27
|
+
"build": "minify-js src dist --no-mangle",
|
|
28
28
|
"bump:patch": "bash utils/bump.sh patch",
|
|
29
29
|
"bump:minor": "bash utils/bump.sh minor",
|
|
30
30
|
"bump:major": "bash utils/bump.sh major",
|
|
@@ -56,6 +56,6 @@
|
|
|
56
56
|
"url": "https://github.com/sponsors/adamlui"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@adamlui/minify.js": "^1.
|
|
59
|
+
"@adamlui/minify.js": "^1.5.0"
|
|
60
60
|
}
|
|
61
61
|
}
|