@adamlui/geolocate 1.0.0 → 1.0.2
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 +222 -193
- package/dist/cli.min.js +4 -2
- package/dist/geolocate.min.js +1 -1
- package/docs/README.md +222 -0
- package/package.json +12 -4
package/README.md
CHANGED
|
@@ -1,193 +1,222 @@
|
|
|
1
|
-
# <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.com/adamlui/js-utils/blob/main/geolocate/media/images/icons/wire-globe/white/icon32.png"><img height=28 src="https://github.com/adamlui/js-utils/blob/main/geolocate/media/images/icons/wire-globe/black/icon32.png"></picture> geolocate
|
|
2
|
-
|
|
3
|
-
### Fetch IP geolocation data from the CLI.
|
|
4
|
-
|
|
5
|
-
<a href="https://github.com/adamlui/js-utils/releases/tag/geolocate-1.0.
|
|
6
|
-
<a href="https://www.npmjs.com/package/@adamlui/geolocate?activeTab=code"><img height=31 src="https://img.shields.io/npm/unpacked-size/%40adamlui%2Fgeolocate?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a>
|
|
7
|
-
<a href="https://
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const location = await geo.locate('
|
|
132
|
-
console.log(location);
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
geo.getOwnIP()
|
|
169
|
-
console.log(ip); // outputs your IP
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
<
|
|
193
|
-
|
|
1
|
+
# <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.com/adamlui/js-utils/blob/main/geolocate/media/images/icons/wire-globe/white/icon32.png"><img height=28 src="https://github.com/adamlui/js-utils/blob/main/geolocate/media/images/icons/wire-globe/black/icon32.png"></picture> geolocate
|
|
2
|
+
|
|
3
|
+
### Fetch IP geolocation data from the CLI.
|
|
4
|
+
|
|
5
|
+
<a href="https://github.com/adamlui/js-utils/releases/tag/geolocate-1.0.2"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.0.2-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
6
|
+
<a href="https://www.npmjs.com/package/@adamlui/geolocate?activeTab=code"><img height=31 src="https://img.shields.io/npm/unpacked-size/%40adamlui%2Fgeolocate?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a>
|
|
7
|
+
<a href="https://github.com/adamlui/js-utils/blob/geolocate-1.0.2/geolocate/dist/geolocate.min.js"><img height=31 src="https://img.shields.io/github/size/adamlui/js-utils/geolocate/dist/geolocate.min.js?branch=geolocate-1.0.2&label=Minified%20Size&logo=databricks&logoColor=white&labelColor=464646&color=ff69b4&style=for-the-badge"></a>
|
|
8
|
+
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:geolocate/src/geolocate.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%3Ageolocate%2Fsrc%2Fgeolocate.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 @adamlui/geolocate
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
As a **dev dependency**, from your project root:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
$ npm install -D @adamlui/geolocate
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
As a **runtime dependency**, from your project root:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
$ npm install @adamlui/geolocate
|
|
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
|
+
## 💻 Command line usage
|
|
39
|
+
|
|
40
|
+
The basic **global command** is:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
$ geolocate [ip]
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Sample output:
|
|
47
|
+
|
|
48
|
+
<img src="https://github.com/adamlui/js-utils/blob/main/geolocate/media/images/screenshots/cli-geolocate-8.8.8.8.jpg">
|
|
49
|
+
|
|
50
|
+
**💡 Note:** If no IPv4 address is passed, your own one will be used.
|
|
51
|
+
|
|
52
|
+
### Command line options
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
Boolean options:
|
|
56
|
+
-q, --quiet Suppress all logging except errors.
|
|
57
|
+
|
|
58
|
+
Info commands:
|
|
59
|
+
-h, --help Display help screen.
|
|
60
|
+
-v, --version Show version number.
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
<br>
|
|
64
|
+
|
|
65
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
66
|
+
|
|
67
|
+
## 🔌 Importing the API
|
|
68
|
+
|
|
69
|
+
You can also import **geolocate** into your app to use its API methods.
|
|
70
|
+
|
|
71
|
+
### <img height=18 src="https://i.imgur.com/JIeAdsr.png"> Node.js
|
|
72
|
+
|
|
73
|
+
#### ECMAScript*:
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
import * as geo from '@adamlui/geolocate';
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### CommonJS:
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
const geo = require('@adamlui/geolocate');
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
###### _*Node.js version 14 or higher required_
|
|
86
|
+
|
|
87
|
+
### <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
|
|
88
|
+
|
|
89
|
+
#### <> HTML script tag:
|
|
90
|
+
|
|
91
|
+
```html
|
|
92
|
+
<script src="https://cdn.jsdelivr.net/npm/@adamlui/geolocate@1.0.2/dist/geolocate.min.js"></script>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### ES6:
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
(async () => {
|
|
99
|
+
await import('https://cdn.jsdelivr.net/npm/@adamlui/geolocate@1.0.2/dist/geolocate.min.js');
|
|
100
|
+
// Your code here...
|
|
101
|
+
})();
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### <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
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
...
|
|
108
|
+
// @require https://cdn.jsdelivr.net/npm/@adamlui/geolocate@1.0.2/dist/geolocate.min.js
|
|
109
|
+
// ==/UserScript==
|
|
110
|
+
|
|
111
|
+
// Your code here...
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
<br>
|
|
115
|
+
|
|
116
|
+
📝 **Note:** To always import the latest version (not recommended in production!) remove the `@1.0.2` version tag from the jsDelivr URL: `https://cdn.jsdelivr.net/npm/@adamlui/geolocate/dist/geolocate.min.js`
|
|
117
|
+
|
|
118
|
+
<br>
|
|
119
|
+
|
|
120
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
121
|
+
|
|
122
|
+
## 📋 API usage
|
|
123
|
+
|
|
124
|
+
### `locate([ip])`
|
|
125
|
+
|
|
126
|
+
Asynchronous method to fetch geolocation data for the `ip` passed, returned as an object:
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
// Using await syntax
|
|
130
|
+
(async () => {
|
|
131
|
+
const location = await geo.locate('8.8.8.8');
|
|
132
|
+
console.log(location);
|
|
133
|
+
})();
|
|
134
|
+
|
|
135
|
+
// Using .then() syntax
|
|
136
|
+
geo.locate('8.8.8.8').then(location => {
|
|
137
|
+
console.log(location);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
/* outputs:
|
|
141
|
+
{
|
|
142
|
+
ip: '8.8.8.8',
|
|
143
|
+
country: 'United States',
|
|
144
|
+
countryCode: 'US',
|
|
145
|
+
region: 'VA',
|
|
146
|
+
regionName: 'Virginia',
|
|
147
|
+
city: 'Ashburn',
|
|
148
|
+
zip: '20149',
|
|
149
|
+
lat: 39.03,
|
|
150
|
+
lon: -77.5,
|
|
151
|
+
timezone: 'America/New_York',
|
|
152
|
+
isp: 'Google LLC'
|
|
153
|
+
}
|
|
154
|
+
*/
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**💡 Note:** If no `ip` is supplied, your own one will be used.
|
|
158
|
+
|
|
159
|
+
#
|
|
160
|
+
|
|
161
|
+
### `getOwnIP()`
|
|
162
|
+
|
|
163
|
+
Asynchronous method to fetch/return your own IP as a string:
|
|
164
|
+
|
|
165
|
+
```js
|
|
166
|
+
// Using await syntax
|
|
167
|
+
(async () => {
|
|
168
|
+
const ip = await geo.getOwnIP();
|
|
169
|
+
console.log(ip); // outputs your IP
|
|
170
|
+
})();
|
|
171
|
+
|
|
172
|
+
// Using .then() syntax
|
|
173
|
+
geo.getOwnIP().then(ip => {
|
|
174
|
+
console.log(ip); // outputs your IP
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
<br>
|
|
179
|
+
|
|
180
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
181
|
+
|
|
182
|
+
## 🏛️ MIT License
|
|
183
|
+
|
|
184
|
+
**Copyright © 2023 [Adam Lui](https://github.com/adamlui).**
|
|
185
|
+
|
|
186
|
+
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:
|
|
187
|
+
|
|
188
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
189
|
+
|
|
190
|
+
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.
|
|
191
|
+
|
|
192
|
+
<br>
|
|
193
|
+
|
|
194
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
195
|
+
|
|
196
|
+
## 🛠️ Related utilities
|
|
197
|
+
|
|
198
|
+
### <img height=21px src="https://i.imgur.com/kvf7fXm.png"> [generate-ip](https://js-utils.com/generate-ip) <a href="https://github.com/toolleeo/cli-apps#networking"><img height=18 src="https://awesome.re/mentioned-badge.svg"></a>
|
|
199
|
+
|
|
200
|
+
> Randomly generate, format, and validate IPv4/IPv6 addresses.
|
|
201
|
+
<br>[Install](https://github.com/adamlui/js-utils/tree/main/generate-ip#-installation) /
|
|
202
|
+
[Readme](https://github.com/adamlui/js-utils/tree/main/generate-ip#readme) /
|
|
203
|
+
[API usage](https://github.com/adamlui/js-utils/tree/main/generate-ip#-api-usage) /
|
|
204
|
+
[CLI usage](https://github.com/adamlui/js-utils/tree/main/generate-ip#-command-line-usage) /
|
|
205
|
+
[Discuss](https://js-utils.com/discussions)
|
|
206
|
+
|
|
207
|
+
### [🔒 generate-pw](../generate-pw)
|
|
208
|
+
|
|
209
|
+
> Randomly generate, strengthen, and validate cryptographically-secure passwords.
|
|
210
|
+
<br>[Install](https://github.com/adamlui/js-utils/tree/main/generate-pw#-installation) /
|
|
211
|
+
[Readme](https://github.com/adamlui/js-utils/tree/main/generate-pw#readme) /
|
|
212
|
+
[API usage](https://github.com/adamlui/js-utils/tree/main/generate-pw#-api-usage) /
|
|
213
|
+
[CLI usage](https://github.com/adamlui/js-utils/tree/main/generate-pw#-command-line-usage) /
|
|
214
|
+
[Discuss](https://js-utils.com/discussions)
|
|
215
|
+
|
|
216
|
+
<br>
|
|
217
|
+
|
|
218
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
219
|
+
|
|
220
|
+
<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> /
|
|
221
|
+
<a href="https://js-utils.com/discussions">Discuss</a> /
|
|
222
|
+
<a href="#-geolocate">Back to top ↑</a>
|
package/dist/cli.min.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
(async()=>{const e=require(__dirname.match(/src/)?"./geolocate":"./geolocate.min"),o=require("generate-ip")["ipv4"],s=require("child_process")["execSync"],r="[1;33m",t={},n={flags:{quietMode:/^--?q(?:uiet)?(?:-?mode)?$/},infoCmds:{help:/^--?h(?:elp)?$/,version:/^--?ve?r?s?i?o?n?$/}};if(process.argv.forEach(o=>{var e,s;o.startsWith("-")&&(e=Object.keys(n.flags).find(e=>n.flags[e].test(o)),s=Object.keys(n.infoCmds).find(e=>n.infoCmds[e].test(o)),e?t[e]=!0:s||(console.error(`
|
|
3
3
|
[1;91mERROR: Arg [${o}] not recognized.[0m`),console.info(`
|
|
4
|
-
${r}Valid arguments are below.[0m`),f(["flags","infoCmds"]),process.exit(1)))}),process.argv.some(e=>n.infoCmds.help.test(e)))f();else if(process.argv.some(e=>n.infoCmds.version.test(e)))console.info("v"+require("./package.json").version);else{var i,a=process.argv.slice(2),l=[];for(let e=0;e<a.length;e++)a[e].startsWith("-")||(o.validate(a[e],{verbose:!1})?l.push(a[e]):(i=0==e?"st":1==e?"nd":"th",console.error(`ERROR: ${e+1+i} arg '${a[e]}' is not a valid IPv4 address.`),
|
|
5
|
-
${r}geolocate [ip] [options|commands][0m`],flags:["\nBoolean options:"," -q, --quiet Suppress all logging except errors."],infoCmds:["\nInfo commands:"," -h, --help Display help screen."," -v, --version Show version number."]};e.forEach(e=>{o[e]?.forEach(e=>{{const r=process.stdout.columns||80,t=[],o=e.match(/\S+|\s+/g);let s="";o.forEach(e=>{var o=r-(0===t.length?0:29);s.length+e.length>o&&(t.push(0===t.length?s:s.trimStart()),s=""),s+=e}),t.push(0===t.length?s:s.trimStart()),t.forEach((e,o)=>console.info(0===o?e:" ".repeat(29)+e))}})})}function
|
|
4
|
+
${r}Valid arguments are below.[0m`),f(["flags","infoCmds"]),m(),process.exit(1)))}),process.argv.some(e=>n.infoCmds.help.test(e)))f();else if(process.argv.some(e=>n.infoCmds.version.test(e)))console.info("v"+require("./package.json").version);else{var i,a=process.argv.slice(2),l=[];for(let e=0;e<a.length;e++)a[e].startsWith("-")||(o.validate(a[e],{verbose:!1})?l.push(a[e]):(i=0==e?"st":1==e?"nd":"th",console.error(`ERROR: ${e+1+i} arg '${a[e]}' is not a valid IPv4 address.`),m(),process.exit(1)));0==l.length&&l.push(await e.getOwnIP()),l.forEach(e=>g(`Fetching geolocation data for ${e}...`));var c=[];for(const h of l)c.push(await e.locate(h));1==c.length&&g(c[0]),g("Copying to clipboard..."),p=(p=JSON.stringify(c)).replace(/\s+$/,"").replace(/"/g,'""'),"darwin"===process.platform?s(`printf "${p}" | pbcopy`):"linux"===process.platform?s(`printf "${p}" | xclip -selection clipboard`):"win32"===process.platform&&s(`Set-Clipboard -Value "${p}"`,{shell:"powershell"})}var p;function f(e=["cmdFormat","flags","infoCmds"]){const o={cmdFormat:[`
|
|
5
|
+
${r}geolocate [ip] [options|commands][0m`],flags:["\nBoolean options:"," -q, --quiet Suppress all logging except errors."],infoCmds:["\nInfo commands:"," -h, --help Display help screen."," -v, --version Show version number."]};e.forEach(e=>{o[e]?.forEach(e=>{{const r=process.stdout.columns||80,t=[],o=e.match(/\S+|\s+/g);let s="";o.forEach(e=>{var o=r-(0===t.length?0:29);s.length+e.length>o&&(t.push(0===t.length?s:s.trimStart()),s=""),s+=e}),t.push(0===t.length?s:s.trimStart()),t.forEach((e,o)=>console.info(0===o?e:" ".repeat(29)+e))}})})}function m(){console.info(`
|
|
6
|
+
${r}For more help, type 'minify-js --help' or visit
|
|
7
|
+
https://github.com/adamlui/js-utils/tree/main/geolocate#-command-line-usage[0m`)}function g(e){t.quietMode||console.info(e)}})();
|
package/dist/geolocate.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
async function e(t){let
|
|
1
|
+
async function e(t){let o;try{o=require("generate-ip").ipv4.validate}catch(e){await import("https://cdn.jsdelivr.net/npm/generate-ip/dist/generate-ip.min.js"),o=ipv4.validate}if(o&&!o(t,{verbose:!1}))console.error("geolocate() » ERROR: Invalid IP address passed."),console.info("geolocate() » For more help, please visit https://github.com/adamlui/js-utils/tree/main/geolocate#locateip");else try{let e;if("undefined"!=typeof fetch)e=await fetch("http://ip-api.com/json/"+t);else{if("function"!=typeof require)return console.error("geolocate() » ERROR: Environment not supported.");e=await require("axios").get("http://ip-api.com/json/"+t)}const{status:r,org:i,as:c,query:n,...a}=await e.json();return{ip:t,...a}}catch(e){console.error("geolocate() »",e)}}async function t(){return fetch("https://ifconfig.me/ip").then(e=>e.text()).catch(()=>fetch("http://ip-api.com/json/").then(e=>e.json()).then(e=>e.query)).catch(async()=>{try{var e=require("child_process")["exec"],t=require("util")["promisify"],{stdout:o,stderr:r}=await t(e)("curl -s ifconfig.me");return r?console.error("getOwnIP() »",r):o.trim()}catch(e){console.error("getOwnIP() »",e)}})}const o={geolocate:e,locate:e,getOwnIP:t};try{module.exports={...o}}catch(e){}try{window.geo={...o}}catch(e){}
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.com/adamlui/js-utils/blob/main/geolocate/media/images/icons/wire-globe/white/icon32.png"><img height=28 src="https://github.com/adamlui/js-utils/blob/main/geolocate/media/images/icons/wire-globe/black/icon32.png"></picture> geolocate
|
|
2
|
+
|
|
3
|
+
### Fetch IP geolocation data from the CLI.
|
|
4
|
+
|
|
5
|
+
<a href="https://github.com/adamlui/js-utils/releases/tag/geolocate-1.0.2"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.0.2-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
6
|
+
<a href="https://www.npmjs.com/package/@adamlui/geolocate?activeTab=code"><img height=31 src="https://img.shields.io/npm/unpacked-size/%40adamlui%2Fgeolocate?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a>
|
|
7
|
+
<a href="https://github.com/adamlui/js-utils/blob/geolocate-1.0.2/geolocate/dist/geolocate.min.js"><img height=31 src="https://img.shields.io/github/size/adamlui/js-utils/geolocate/dist/geolocate.min.js?branch=geolocate-1.0.2&label=Minified%20Size&logo=databricks&logoColor=white&labelColor=464646&color=ff69b4&style=for-the-badge"></a>
|
|
8
|
+
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:geolocate/src/geolocate.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%3Ageolocate%2Fsrc%2Fgeolocate.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 @adamlui/geolocate
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
As a **dev dependency**, from your project root:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
$ npm install -D @adamlui/geolocate
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
As a **runtime dependency**, from your project root:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
$ npm install @adamlui/geolocate
|
|
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
|
+
## 💻 Command line usage
|
|
39
|
+
|
|
40
|
+
The basic **global command** is:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
$ geolocate [ip]
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Sample output:
|
|
47
|
+
|
|
48
|
+
<img src="https://github.com/adamlui/js-utils/blob/main/geolocate/media/images/screenshots/cli-geolocate-8.8.8.8.jpg">
|
|
49
|
+
|
|
50
|
+
**💡 Note:** If no IPv4 address is passed, your own one will be used.
|
|
51
|
+
|
|
52
|
+
### Command line options
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
Boolean options:
|
|
56
|
+
-q, --quiet Suppress all logging except errors.
|
|
57
|
+
|
|
58
|
+
Info commands:
|
|
59
|
+
-h, --help Display help screen.
|
|
60
|
+
-v, --version Show version number.
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
<br>
|
|
64
|
+
|
|
65
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
66
|
+
|
|
67
|
+
## 🔌 Importing the API
|
|
68
|
+
|
|
69
|
+
You can also import **geolocate** into your app to use its API methods.
|
|
70
|
+
|
|
71
|
+
### <img height=18 src="https://i.imgur.com/JIeAdsr.png"> Node.js
|
|
72
|
+
|
|
73
|
+
#### ECMAScript*:
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
import * as geo from '@adamlui/geolocate';
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### CommonJS:
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
const geo = require('@adamlui/geolocate');
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
###### _*Node.js version 14 or higher required_
|
|
86
|
+
|
|
87
|
+
### <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
|
|
88
|
+
|
|
89
|
+
#### <> HTML script tag:
|
|
90
|
+
|
|
91
|
+
```html
|
|
92
|
+
<script src="https://cdn.jsdelivr.net/npm/@adamlui/geolocate@1.0.2/dist/geolocate.min.js"></script>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### ES6:
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
(async () => {
|
|
99
|
+
await import('https://cdn.jsdelivr.net/npm/@adamlui/geolocate@1.0.2/dist/geolocate.min.js');
|
|
100
|
+
// Your code here...
|
|
101
|
+
})();
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### <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
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
...
|
|
108
|
+
// @require https://cdn.jsdelivr.net/npm/@adamlui/geolocate@1.0.2/dist/geolocate.min.js
|
|
109
|
+
// ==/UserScript==
|
|
110
|
+
|
|
111
|
+
// Your code here...
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
<br>
|
|
115
|
+
|
|
116
|
+
📝 **Note:** To always import the latest version (not recommended in production!) remove the `@1.0.2` version tag from the jsDelivr URL: `https://cdn.jsdelivr.net/npm/@adamlui/geolocate/dist/geolocate.min.js`
|
|
117
|
+
|
|
118
|
+
<br>
|
|
119
|
+
|
|
120
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
121
|
+
|
|
122
|
+
## 📋 API usage
|
|
123
|
+
|
|
124
|
+
### `locate([ip])`
|
|
125
|
+
|
|
126
|
+
Asynchronous method to fetch geolocation data for the `ip` passed, returned as an object:
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
// Using await syntax
|
|
130
|
+
(async () => {
|
|
131
|
+
const location = await geo.locate('8.8.8.8');
|
|
132
|
+
console.log(location);
|
|
133
|
+
})();
|
|
134
|
+
|
|
135
|
+
// Using .then() syntax
|
|
136
|
+
geo.locate('8.8.8.8').then(location => {
|
|
137
|
+
console.log(location);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
/* outputs:
|
|
141
|
+
{
|
|
142
|
+
ip: '8.8.8.8',
|
|
143
|
+
country: 'United States',
|
|
144
|
+
countryCode: 'US',
|
|
145
|
+
region: 'VA',
|
|
146
|
+
regionName: 'Virginia',
|
|
147
|
+
city: 'Ashburn',
|
|
148
|
+
zip: '20149',
|
|
149
|
+
lat: 39.03,
|
|
150
|
+
lon: -77.5,
|
|
151
|
+
timezone: 'America/New_York',
|
|
152
|
+
isp: 'Google LLC'
|
|
153
|
+
}
|
|
154
|
+
*/
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**💡 Note:** If no `ip` is supplied, your own one will be used.
|
|
158
|
+
|
|
159
|
+
#
|
|
160
|
+
|
|
161
|
+
### `getOwnIP()`
|
|
162
|
+
|
|
163
|
+
Asynchronous method to fetch/return your own IP as a string:
|
|
164
|
+
|
|
165
|
+
```js
|
|
166
|
+
// Using await syntax
|
|
167
|
+
(async () => {
|
|
168
|
+
const ip = await geo.getOwnIP();
|
|
169
|
+
console.log(ip); // outputs your IP
|
|
170
|
+
})();
|
|
171
|
+
|
|
172
|
+
// Using .then() syntax
|
|
173
|
+
geo.getOwnIP().then(ip => {
|
|
174
|
+
console.log(ip); // outputs your IP
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
<br>
|
|
179
|
+
|
|
180
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
181
|
+
|
|
182
|
+
## 🏛️ MIT License
|
|
183
|
+
|
|
184
|
+
**Copyright © 2023 [Adam Lui](https://github.com/adamlui).**
|
|
185
|
+
|
|
186
|
+
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:
|
|
187
|
+
|
|
188
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
189
|
+
|
|
190
|
+
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.
|
|
191
|
+
|
|
192
|
+
<br>
|
|
193
|
+
|
|
194
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
195
|
+
|
|
196
|
+
## 🛠️ Related utilities
|
|
197
|
+
|
|
198
|
+
### <img height=21px src="https://i.imgur.com/kvf7fXm.png"> [generate-ip](https://js-utils.com/generate-ip) <a href="https://github.com/toolleeo/cli-apps#networking"><img height=18 src="https://awesome.re/mentioned-badge.svg"></a>
|
|
199
|
+
|
|
200
|
+
> Randomly generate, format, and validate IPv4/IPv6 addresses.
|
|
201
|
+
<br>[Install](https://github.com/adamlui/js-utils/tree/main/generate-ip#-installation) /
|
|
202
|
+
[Readme](https://github.com/adamlui/js-utils/tree/main/generate-ip#readme) /
|
|
203
|
+
[API usage](https://github.com/adamlui/js-utils/tree/main/generate-ip#-api-usage) /
|
|
204
|
+
[CLI usage](https://github.com/adamlui/js-utils/tree/main/generate-ip#-command-line-usage) /
|
|
205
|
+
[Discuss](https://js-utils.com/discussions)
|
|
206
|
+
|
|
207
|
+
### [🔒 generate-pw](../generate-pw)
|
|
208
|
+
|
|
209
|
+
> Randomly generate, strengthen, and validate cryptographically-secure passwords.
|
|
210
|
+
<br>[Install](https://github.com/adamlui/js-utils/tree/main/generate-pw#-installation) /
|
|
211
|
+
[Readme](https://github.com/adamlui/js-utils/tree/main/generate-pw#readme) /
|
|
212
|
+
[API usage](https://github.com/adamlui/js-utils/tree/main/generate-pw#-api-usage) /
|
|
213
|
+
[CLI usage](https://github.com/adamlui/js-utils/tree/main/generate-pw#-command-line-usage) /
|
|
214
|
+
[Discuss](https://js-utils.com/discussions)
|
|
215
|
+
|
|
216
|
+
<br>
|
|
217
|
+
|
|
218
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
219
|
+
|
|
220
|
+
<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> /
|
|
221
|
+
<a href="https://js-utils.com/discussions">Discuss</a> /
|
|
222
|
+
<a href="#-geolocate">Back to top ↑</a>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adamlui/geolocate",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Fetch IP geolocation data from the CLI.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
@@ -19,7 +19,13 @@
|
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
21
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
22
|
-
"build": "minify-js src dist"
|
|
22
|
+
"build": "minify-js src dist",
|
|
23
|
+
"bump:patch": "bash utils/bump.sh patch",
|
|
24
|
+
"bump:minor": "bash utils/bump.sh minor",
|
|
25
|
+
"bump:major": "bash utils/bump.sh major",
|
|
26
|
+
"publish:patch": "bash utils/bump.sh patch --publish",
|
|
27
|
+
"publish:minor": "bash utils/bump.sh minor --publish",
|
|
28
|
+
"publish:major": "bash utils/bump.sh major --publish"
|
|
23
29
|
},
|
|
24
30
|
"repository": {
|
|
25
31
|
"type": "git",
|
|
@@ -29,7 +35,9 @@
|
|
|
29
35
|
"geolocate",
|
|
30
36
|
"geolocation",
|
|
31
37
|
"ip",
|
|
32
|
-
"networking"
|
|
38
|
+
"networking",
|
|
39
|
+
"api",
|
|
40
|
+
"cli"
|
|
33
41
|
],
|
|
34
42
|
"bugs": {
|
|
35
43
|
"url": "https://github.com/adamlui/js-utils/issues"
|
|
@@ -38,6 +46,6 @@
|
|
|
38
46
|
"generate-ip": "^2.2.0"
|
|
39
47
|
},
|
|
40
48
|
"devDependencies": {
|
|
41
|
-
"@adamlui/minify.js": "^1.
|
|
49
|
+
"@adamlui/minify.js": "^1.5.0"
|
|
42
50
|
}
|
|
43
51
|
}
|