@ibiliaze/stringman 3.2.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -127,3 +127,14 @@ export declare const getRandomString: (length: number) => string;
127
127
  * @returns true if the URL is a video, false otherwise
128
128
  */
129
129
  export declare const isVideoUrl: (url: string) => boolean;
130
+ /**
131
+ * Returns the client's public IP address by querying external "IP echo" services.
132
+ *
133
+ * Strategy:
134
+ * - Try an endpoint that supports IPv6 first, then fall back to IPv4-only.
135
+ * - Use `cache: 'no-store'` to avoid any cached/stale responses from intermediaries.
136
+ * - If a request fails (network/CORS/HTTP error), move on to the next endpoint.
137
+ *
138
+ * @throws Error if no endpoint returns a valid IP.
139
+ */
140
+ export declare const getPublicIP: () => Promise<string>;
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isVideoUrl = exports.getRandomString = exports.extractImageSrcs = exports.select = exports.query = exports.getCloudinaryPublicId = exports.dp = exports.megaTrim = exports.superTrim = void 0;
3
+ exports.getPublicIP = exports.isVideoUrl = exports.getRandomString = exports.extractImageSrcs = exports.select = exports.query = exports.getCloudinaryPublicId = exports.dp = exports.megaTrim = exports.superTrim = void 0;
4
4
  /**
5
5
  * Clean up extra whitespace in a string.
6
6
  *
@@ -199,3 +199,32 @@ const isVideoUrl = (url) => {
199
199
  }
200
200
  };
201
201
  exports.isVideoUrl = isVideoUrl;
202
+ /**
203
+ * Returns the client's public IP address by querying external "IP echo" services.
204
+ *
205
+ * Strategy:
206
+ * - Try an endpoint that supports IPv6 first, then fall back to IPv4-only.
207
+ * - Use `cache: 'no-store'` to avoid any cached/stale responses from intermediaries.
208
+ * - If a request fails (network/CORS/HTTP error), move on to the next endpoint.
209
+ *
210
+ * @throws Error if no endpoint returns a valid IP.
211
+ */
212
+ const getPublicIP = async () => {
213
+ const urls = [
214
+ 'https://api64.ipify.org?format=json', // IPv6/IPv4
215
+ 'https://api.ipify.org?format=json', // IPv4 fallback
216
+ ];
217
+ for (const url of urls) {
218
+ try {
219
+ const r = await fetch(url, { cache: 'no-store' });
220
+ if (!r.ok)
221
+ continue;
222
+ const { ip } = await r.json();
223
+ if (ip)
224
+ return ip;
225
+ }
226
+ catch { }
227
+ }
228
+ throw new Error('Public IP unavailable');
229
+ };
230
+ exports.getPublicIP = getPublicIP;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@ibiliaze/stringman",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
9
  "pub": "npm publish --access public",
10
- "git": "git add .; git commit -m 'changes'; git tag -a v3.2.0 -m 'v3.2.0'; git push origin v3.2.0; git push",
10
+ "git": "git add .; git commit -m 'changes'; git tag -a v3.3.0 -m 'v3.3.0'; git push origin v3.3.0; git push",
11
11
  "push": "npm run build; npm run git; npm run pub"
12
12
  },
13
13
  "author": "Ibi Hasanli",
package/src/index.ts CHANGED
@@ -201,3 +201,29 @@ export const isVideoUrl = (url: string): boolean => {
201
201
  return false;
202
202
  }
203
203
  };
204
+
205
+ /**
206
+ * Returns the client's public IP address by querying external "IP echo" services.
207
+ *
208
+ * Strategy:
209
+ * - Try an endpoint that supports IPv6 first, then fall back to IPv4-only.
210
+ * - Use `cache: 'no-store'` to avoid any cached/stale responses from intermediaries.
211
+ * - If a request fails (network/CORS/HTTP error), move on to the next endpoint.
212
+ *
213
+ * @throws Error if no endpoint returns a valid IP.
214
+ */
215
+ export const getPublicIP = async (): Promise<string> => {
216
+ const urls = [
217
+ 'https://api64.ipify.org?format=json', // IPv6/IPv4
218
+ 'https://api.ipify.org?format=json', // IPv4 fallback
219
+ ];
220
+ for (const url of urls) {
221
+ try {
222
+ const r = await fetch(url, { cache: 'no-store' });
223
+ if (!r.ok) continue;
224
+ const { ip } = await r.json();
225
+ if (ip) return ip;
226
+ } catch {}
227
+ }
228
+ throw new Error('Public IP unavailable');
229
+ };