@shware/http 0.2.16 → 0.2.18

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.
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ // src/__tests__/ip.test.ts
4
+ var import_ip = require("../utils/ip.cjs");
5
+ describe("extractIpAddress", () => {
6
+ it("should extract the ipv4 address from the request", () => {
7
+ expect((0, import_ip.extractIpAddress)("127.0.0.1")).toBe("127.0.0.1");
8
+ expect((0, import_ip.extractIpAddress)("192.168.0.1")).toBe("192.168.0.1");
9
+ expect((0, import_ip.extractIpAddress)("127.0.0.1:8080")).toBe("127.0.0.1");
10
+ expect((0, import_ip.extractIpAddress)("192.168.0.1:8080")).toBe("192.168.0.1");
11
+ expect((0, import_ip.extractIpAddress)("127.0.0.1:8080/path")).toBe("127.0.0.1");
12
+ });
13
+ it("should extract the ipv6 address from the request", () => {
14
+ expect((0, import_ip.extractIpAddress)("::1")).toBe("::1");
15
+ expect((0, import_ip.extractIpAddress)("[::1]:8080")).toBe("::1");
16
+ expect((0, import_ip.extractIpAddress)("2001:0db8:85a3:0000:0000:8a2e:0370:7334")).toBe(
17
+ "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
18
+ );
19
+ expect((0, import_ip.extractIpAddress)("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80")).toBe(
20
+ "2001:db8:85a3::8a2e:370:7334"
21
+ // remove leading zeros
22
+ );
23
+ expect((0, import_ip.extractIpAddress)("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80/path")).toBe(
24
+ "2001:db8:85a3::8a2e:370:7334"
25
+ // remove leading zeros
26
+ );
27
+ expect((0, import_ip.extractIpAddress)("2404:7ac0:614d:bba7:cf41:992e:98e:9186:60704")).toBe(
28
+ "2404:7ac0:614d:bba7:cf41:992e:98e:9186"
29
+ );
30
+ });
31
+ it("should return null if the ip address is not valid", () => {
32
+ expect((0, import_ip.extractIpAddress)(null)).toBeNull();
33
+ expect((0, import_ip.extractIpAddress)(void 0)).toBeNull();
34
+ expect((0, import_ip.extractIpAddress)("invalid")).toBeNull();
35
+ expect((0, import_ip.extractIpAddress)("example.com")).toBeNull();
36
+ });
37
+ });
38
+ //# sourceMappingURL=ip.test.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/__tests__/ip.test.ts"],"sourcesContent":["import { extractIpAddress } from '../utils/ip';\n\ndescribe('extractIpAddress', () => {\n it('should extract the ipv4 address from the request', () => {\n expect(extractIpAddress('127.0.0.1')).toBe('127.0.0.1');\n expect(extractIpAddress('192.168.0.1')).toBe('192.168.0.1');\n expect(extractIpAddress('127.0.0.1:8080')).toBe('127.0.0.1');\n expect(extractIpAddress('192.168.0.1:8080')).toBe('192.168.0.1');\n expect(extractIpAddress('127.0.0.1:8080/path')).toBe('127.0.0.1');\n });\n\n it('should extract the ipv6 address from the request', () => {\n expect(extractIpAddress('::1')).toBe('::1');\n expect(extractIpAddress('[::1]:8080')).toBe('::1');\n\n expect(extractIpAddress('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe(\n '2001:0db8:85a3:0000:0000:8a2e:0370:7334'\n );\n expect(extractIpAddress('[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80')).toBe(\n '2001:db8:85a3::8a2e:370:7334' // remove leading zeros\n );\n expect(extractIpAddress('[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80/path')).toBe(\n '2001:db8:85a3::8a2e:370:7334' // remove leading zeros\n );\n\n expect(extractIpAddress('2404:7ac0:614d:bba7:cf41:992e:98e:9186:60704')).toBe(\n '2404:7ac0:614d:bba7:cf41:992e:98e:9186'\n );\n });\n\n it('should return null if the ip address is not valid', () => {\n expect(extractIpAddress(null)).toBeNull();\n expect(extractIpAddress(undefined)).toBeNull();\n expect(extractIpAddress('invalid')).toBeNull();\n expect(extractIpAddress('example.com')).toBeNull();\n });\n});\n"],"mappings":";;;AAAA,gBAAiC;AAEjC,SAAS,oBAAoB,MAAM;AACjC,KAAG,oDAAoD,MAAM;AAC3D,eAAO,4BAAiB,WAAW,CAAC,EAAE,KAAK,WAAW;AACtD,eAAO,4BAAiB,aAAa,CAAC,EAAE,KAAK,aAAa;AAC1D,eAAO,4BAAiB,gBAAgB,CAAC,EAAE,KAAK,WAAW;AAC3D,eAAO,4BAAiB,kBAAkB,CAAC,EAAE,KAAK,aAAa;AAC/D,eAAO,4BAAiB,qBAAqB,CAAC,EAAE,KAAK,WAAW;AAAA,EAClE,CAAC;AAED,KAAG,oDAAoD,MAAM;AAC3D,eAAO,4BAAiB,KAAK,CAAC,EAAE,KAAK,KAAK;AAC1C,eAAO,4BAAiB,YAAY,CAAC,EAAE,KAAK,KAAK;AAEjD,eAAO,4BAAiB,yCAAyC,CAAC,EAAE;AAAA,MAClE;AAAA,IACF;AACA,eAAO,4BAAiB,8CAA8C,CAAC,EAAE;AAAA,MACvE;AAAA;AAAA,IACF;AACA,eAAO,4BAAiB,mDAAmD,CAAC,EAAE;AAAA,MAC5E;AAAA;AAAA,IACF;AAEA,eAAO,4BAAiB,8CAA8C,CAAC,EAAE;AAAA,MACvE;AAAA,IACF;AAAA,EACF,CAAC;AAED,KAAG,qDAAqD,MAAM;AAC5D,eAAO,4BAAiB,IAAI,CAAC,EAAE,SAAS;AACxC,eAAO,4BAAiB,MAAS,CAAC,EAAE,SAAS;AAC7C,eAAO,4BAAiB,SAAS,CAAC,EAAE,SAAS;AAC7C,eAAO,4BAAiB,aAAa,CAAC,EAAE,SAAS;AAAA,EACnD,CAAC;AACH,CAAC;","names":[]}
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,36 @@
1
+ // src/__tests__/ip.test.ts
2
+ import { extractIpAddress } from "../utils/ip.mjs";
3
+ describe("extractIpAddress", () => {
4
+ it("should extract the ipv4 address from the request", () => {
5
+ expect(extractIpAddress("127.0.0.1")).toBe("127.0.0.1");
6
+ expect(extractIpAddress("192.168.0.1")).toBe("192.168.0.1");
7
+ expect(extractIpAddress("127.0.0.1:8080")).toBe("127.0.0.1");
8
+ expect(extractIpAddress("192.168.0.1:8080")).toBe("192.168.0.1");
9
+ expect(extractIpAddress("127.0.0.1:8080/path")).toBe("127.0.0.1");
10
+ });
11
+ it("should extract the ipv6 address from the request", () => {
12
+ expect(extractIpAddress("::1")).toBe("::1");
13
+ expect(extractIpAddress("[::1]:8080")).toBe("::1");
14
+ expect(extractIpAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334")).toBe(
15
+ "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
16
+ );
17
+ expect(extractIpAddress("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80")).toBe(
18
+ "2001:db8:85a3::8a2e:370:7334"
19
+ // remove leading zeros
20
+ );
21
+ expect(extractIpAddress("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80/path")).toBe(
22
+ "2001:db8:85a3::8a2e:370:7334"
23
+ // remove leading zeros
24
+ );
25
+ expect(extractIpAddress("2404:7ac0:614d:bba7:cf41:992e:98e:9186:60704")).toBe(
26
+ "2404:7ac0:614d:bba7:cf41:992e:98e:9186"
27
+ );
28
+ });
29
+ it("should return null if the ip address is not valid", () => {
30
+ expect(extractIpAddress(null)).toBeNull();
31
+ expect(extractIpAddress(void 0)).toBeNull();
32
+ expect(extractIpAddress("invalid")).toBeNull();
33
+ expect(extractIpAddress("example.com")).toBeNull();
34
+ });
35
+ });
36
+ //# sourceMappingURL=ip.test.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/__tests__/ip.test.ts"],"sourcesContent":["import { extractIpAddress } from '../utils/ip';\n\ndescribe('extractIpAddress', () => {\n it('should extract the ipv4 address from the request', () => {\n expect(extractIpAddress('127.0.0.1')).toBe('127.0.0.1');\n expect(extractIpAddress('192.168.0.1')).toBe('192.168.0.1');\n expect(extractIpAddress('127.0.0.1:8080')).toBe('127.0.0.1');\n expect(extractIpAddress('192.168.0.1:8080')).toBe('192.168.0.1');\n expect(extractIpAddress('127.0.0.1:8080/path')).toBe('127.0.0.1');\n });\n\n it('should extract the ipv6 address from the request', () => {\n expect(extractIpAddress('::1')).toBe('::1');\n expect(extractIpAddress('[::1]:8080')).toBe('::1');\n\n expect(extractIpAddress('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe(\n '2001:0db8:85a3:0000:0000:8a2e:0370:7334'\n );\n expect(extractIpAddress('[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80')).toBe(\n '2001:db8:85a3::8a2e:370:7334' // remove leading zeros\n );\n expect(extractIpAddress('[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80/path')).toBe(\n '2001:db8:85a3::8a2e:370:7334' // remove leading zeros\n );\n\n expect(extractIpAddress('2404:7ac0:614d:bba7:cf41:992e:98e:9186:60704')).toBe(\n '2404:7ac0:614d:bba7:cf41:992e:98e:9186'\n );\n });\n\n it('should return null if the ip address is not valid', () => {\n expect(extractIpAddress(null)).toBeNull();\n expect(extractIpAddress(undefined)).toBeNull();\n expect(extractIpAddress('invalid')).toBeNull();\n expect(extractIpAddress('example.com')).toBeNull();\n });\n});\n"],"mappings":";AAAA,SAAS,wBAAwB;AAEjC,SAAS,oBAAoB,MAAM;AACjC,KAAG,oDAAoD,MAAM;AAC3D,WAAO,iBAAiB,WAAW,CAAC,EAAE,KAAK,WAAW;AACtD,WAAO,iBAAiB,aAAa,CAAC,EAAE,KAAK,aAAa;AAC1D,WAAO,iBAAiB,gBAAgB,CAAC,EAAE,KAAK,WAAW;AAC3D,WAAO,iBAAiB,kBAAkB,CAAC,EAAE,KAAK,aAAa;AAC/D,WAAO,iBAAiB,qBAAqB,CAAC,EAAE,KAAK,WAAW;AAAA,EAClE,CAAC;AAED,KAAG,oDAAoD,MAAM;AAC3D,WAAO,iBAAiB,KAAK,CAAC,EAAE,KAAK,KAAK;AAC1C,WAAO,iBAAiB,YAAY,CAAC,EAAE,KAAK,KAAK;AAEjD,WAAO,iBAAiB,yCAAyC,CAAC,EAAE;AAAA,MAClE;AAAA,IACF;AACA,WAAO,iBAAiB,8CAA8C,CAAC,EAAE;AAAA,MACvE;AAAA;AAAA,IACF;AACA,WAAO,iBAAiB,mDAAmD,CAAC,EAAE;AAAA,MAC5E;AAAA;AAAA,IACF;AAEA,WAAO,iBAAiB,8CAA8C,CAAC,EAAE;AAAA,MACvE;AAAA,IACF;AAAA,EACF,CAAC;AAED,KAAG,qDAAqD,MAAM;AAC5D,WAAO,iBAAiB,IAAI,CAAC,EAAE,SAAS;AACxC,WAAO,iBAAiB,MAAS,CAAC,EAAE,SAAS;AAC7C,WAAO,iBAAiB,SAAS,CAAC,EAAE,SAAS;AAC7C,WAAO,iBAAiB,aAAa,CAAC,EAAE,SAAS;AAAA,EACnD,CAAC;AACH,CAAC;","names":[]}
@@ -24,7 +24,7 @@ __export(geolocation_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(geolocation_exports);
26
26
  var import_adapter = require("hono/adapter");
27
- var import_net = require("net");
27
+ var import_ip = require("../utils/ip.cjs");
28
28
  var runtime = (0, import_adapter.getRuntimeKey)();
29
29
  function geolocation(c) {
30
30
  if (runtime === "workerd") {
@@ -55,20 +55,8 @@ function geolocation(c) {
55
55
  time_zone: null
56
56
  };
57
57
  }
58
- let ip_address = null;
59
- const viewerAddress = c.req.header("CloudFront-Viewer-Address");
60
- if (viewerAddress) {
61
- try {
62
- const url = new URL(`https://${viewerAddress}`);
63
- ip_address = url.hostname.replace("[", "").replace("]", "");
64
- } catch (_) {
65
- if ((0, import_net.isIPv4)(viewerAddress) || (0, import_net.isIPv6)(viewerAddress)) {
66
- ip_address = viewerAddress;
67
- }
68
- }
69
- }
70
58
  return {
71
- ip_address,
59
+ ip_address: (0, import_ip.extractIpAddress)(c.req.header("CloudFront-Viewer-Address")),
72
60
  city: c.req.header("CloudFront-Viewer-City") ?? null,
73
61
  country: c.req.header("CloudFront-Viewer-Country") ?? null,
74
62
  continent: null,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hono/geolocation.ts"],"sourcesContent":["import { getRuntimeKey } from 'hono/adapter';\nimport { isIPv4, isIPv6 } from 'net';\nimport type { Context } from 'hono';\n\nexport type Geolocation = {\n ip_address: string | null;\n city: string | null;\n country: string | null; // ISO 3166-1 alpha-2\n continent: string | null;\n longitude: number | null;\n latitude: number | null;\n region: string | null; // ISO 3166-2\n metro_code: string | null;\n postal_code: string | null;\n time_zone: string | null;\n};\n\nconst runtime = getRuntimeKey();\nexport function geolocation(c: Context): Geolocation | null {\n /** reference: https://developers.cloudflare.com/rules/transform/managed-transforms/reference/#add-visitor-location-headers */\n if (runtime === 'workerd') {\n return {\n ip_address: c.req.header('true-client-ip') ?? c.req.header('cf-connecting-ip') ?? null,\n city: c.req.header('cf-ipcity') ?? null,\n country: c.req.header('cf-ipcountry') ?? null,\n continent: c.req.header('cf-ipcontinent') ?? null,\n longitude: c.req.header('cf-iplongitude') ? Number(c.req.header('cf-iplongitude')) : null,\n latitude: c.req.header('cf-iplatitude') ? Number(c.req.header('cf-iplatitude')) : null,\n region: c.req.header('cf-region-code') ?? null,\n metro_code: c.req.header('cf-metro-code') ?? null,\n postal_code: c.req.header('cf-postal-code') ?? null,\n time_zone: c.req.header('cf-timezone') ?? null,\n };\n }\n\n /** https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts */\n if (c.req.header('x-vercel-id')) {\n return {\n ip_address: c.req.header('x-real-ip') ?? null,\n city: c.req.header('x-vercel-ip-city') ?? null,\n country: c.req.header('x-vercel-ip-country') ?? null,\n continent: c.req.header('x-vercel-ip-continent') ?? null,\n longitude: c.req.header('x-vercel-ip-longitude')\n ? Number(c.req.header('x-vercel-ip-longitude'))\n : null,\n latitude: c.req.header('x-vercel-ip-latitude')\n ? Number(c.req.header('x-vercel-ip-latitude'))\n : null,\n region: c.req.header('x-vercel-ip-country-region') ?? null,\n metro_code: null,\n postal_code: c.req.header('x-vercel-ip-postal-code') ?? null,\n time_zone: null,\n };\n }\n\n // cloudfront\n // https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/adding-cloudfront-headers.html#cloudfront-headers-viewer-location\n let ip_address: string | null = null;\n const viewerAddress = c.req.header('CloudFront-Viewer-Address');\n if (viewerAddress) {\n try {\n const url = new URL(`https://${viewerAddress}`);\n ip_address = url.hostname.replace('[', '').replace(']', '');\n } catch (_) {\n if (isIPv4(viewerAddress) || isIPv6(viewerAddress)) {\n ip_address = viewerAddress;\n }\n }\n }\n\n return {\n ip_address,\n city: c.req.header('CloudFront-Viewer-City') ?? null,\n country: c.req.header('CloudFront-Viewer-Country') ?? null,\n continent: null,\n longitude: c.req.header('CloudFront-Viewer-Longitude')\n ? Number(c.req.header('CloudFront-Viewer-Longitude'))\n : null,\n latitude: c.req.header('CloudFront-Viewer-Latitude')\n ? Number(c.req.header('CloudFront-Viewer-Latitude'))\n : null,\n region: c.req.header('CloudFront-Viewer-Country-Region') ?? null,\n metro_code: c.req.header('CloudFront-Viewer-Metro-Code') ?? null,\n postal_code: c.req.header('CloudFront-Viewer-Postal-Code') ?? null,\n time_zone: c.req.header('CloudFront-Viewer-Time-Zone') ?? null,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAA8B;AAC9B,iBAA+B;AAgB/B,IAAM,cAAU,8BAAc;AACvB,SAAS,YAAY,GAAgC;AAE1D,MAAI,YAAY,WAAW;AACzB,WAAO;AAAA,MACL,YAAY,EAAE,IAAI,OAAO,gBAAgB,KAAK,EAAE,IAAI,OAAO,kBAAkB,KAAK;AAAA,MAClF,MAAM,EAAE,IAAI,OAAO,WAAW,KAAK;AAAA,MACnC,SAAS,EAAE,IAAI,OAAO,cAAc,KAAK;AAAA,MACzC,WAAW,EAAE,IAAI,OAAO,gBAAgB,KAAK;AAAA,MAC7C,WAAW,EAAE,IAAI,OAAO,gBAAgB,IAAI,OAAO,EAAE,IAAI,OAAO,gBAAgB,CAAC,IAAI;AAA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ACf;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA,MAAM,EAAE,IAAI,OAAO,wBAAwB,KAAK;AAAA,IAChD,SAAS,EAAE,IAAI,OAAO,2BAA2B,KAAK;AAAA,IACtD,WAAW;AAAA,IACX,WAAW,EAAE,IAAI,OAAO,6BAA6B,IACjD,OAAO,EAAE,IAAI,OAAO,6BAA6B,CAAC,IAClD;AAAA,IACJ,UAAU,EAAE,IAAI,OAAO,4BAA4B,IAC/C,OAAO,EAAE,IAAI,OAAO,4BAA4B,CAAC,IACjD;AAAA,IACJ,QAAQ,EAAE,IAAI,OAAO,kCAAkC,KAAK;AAAA,IAC5D,YAAY,EAAE,IAAI,OAAO,8BAA8B,KAAK;AAAA,IAC5D,aAAa,EAAE,IAAI,OAAO,+BAA+B,KAAK;AAAA,IAC9D,WAAW,EAAE,IAAI,OAAO,6BAA6B,KAAK;AAAA,EAC5D;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/hono/geolocation.ts"],"sourcesContent":["import { getRuntimeKey } from 'hono/adapter';\nimport { extractIpAddress } from '../utils/ip';\nimport type { Context } from 'hono';\n\nexport type Geolocation = {\n ip_address: string | null;\n city: string | null;\n country: string | null; // ISO 3166-1 alpha-2\n continent: string | null;\n longitude: number | null;\n latitude: number | null;\n region: string | null; // ISO 3166-2\n metro_code: string | null;\n postal_code: string | null;\n time_zone: string | null;\n};\n\nconst runtime = getRuntimeKey();\nexport function geolocation(c: Context): Geolocation | null {\n /** reference: https://developers.cloudflare.com/rules/transform/managed-transforms/reference/#add-visitor-location-headers */\n if (runtime === 'workerd') {\n return {\n ip_address: c.req.header('true-client-ip') ?? c.req.header('cf-connecting-ip') ?? null,\n city: c.req.header('cf-ipcity') ?? null,\n country: c.req.header('cf-ipcountry') ?? null,\n continent: c.req.header('cf-ipcontinent') ?? null,\n longitude: c.req.header('cf-iplongitude') ? Number(c.req.header('cf-iplongitude')) : null,\n latitude: c.req.header('cf-iplatitude') ? Number(c.req.header('cf-iplatitude')) : null,\n region: c.req.header('cf-region-code') ?? null,\n metro_code: c.req.header('cf-metro-code') ?? null,\n postal_code: c.req.header('cf-postal-code') ?? null,\n time_zone: c.req.header('cf-timezone') ?? null,\n };\n }\n\n /** https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts */\n if (c.req.header('x-vercel-id')) {\n return {\n ip_address: c.req.header('x-real-ip') ?? null,\n city: c.req.header('x-vercel-ip-city') ?? null,\n country: c.req.header('x-vercel-ip-country') ?? null,\n continent: c.req.header('x-vercel-ip-continent') ?? null,\n longitude: c.req.header('x-vercel-ip-longitude')\n ? Number(c.req.header('x-vercel-ip-longitude'))\n : null,\n latitude: c.req.header('x-vercel-ip-latitude')\n ? Number(c.req.header('x-vercel-ip-latitude'))\n : null,\n region: c.req.header('x-vercel-ip-country-region') ?? null,\n metro_code: null,\n postal_code: c.req.header('x-vercel-ip-postal-code') ?? null,\n time_zone: null,\n };\n }\n\n // cloudfront\n // https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/adding-cloudfront-headers.html#cloudfront-headers-viewer-location\n return {\n ip_address: extractIpAddress(c.req.header('CloudFront-Viewer-Address')),\n city: c.req.header('CloudFront-Viewer-City') ?? null,\n country: c.req.header('CloudFront-Viewer-Country') ?? null,\n continent: null,\n longitude: c.req.header('CloudFront-Viewer-Longitude')\n ? Number(c.req.header('CloudFront-Viewer-Longitude'))\n : null,\n latitude: c.req.header('CloudFront-Viewer-Latitude')\n ? Number(c.req.header('CloudFront-Viewer-Latitude'))\n : null,\n region: c.req.header('CloudFront-Viewer-Country-Region') ?? null,\n metro_code: c.req.header('CloudFront-Viewer-Metro-Code') ?? null,\n postal_code: c.req.header('CloudFront-Viewer-Postal-Code') ?? null,\n time_zone: c.req.header('CloudFront-Viewer-Time-Zone') ?? null,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAA8B;AAC9B,gBAAiC;AAg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A,MACtD,YAAY;AAAA,MACZ,aAAa,EAAE,IAAI,OAAO,yBAAyB,KAAK;AAAA,MACxD,WAAW;AAAA,IACb;AAAA,EACF;AAIA,SAAO;AAAA,IACL,gBAAY,4BAAiB,EAAE,IAAI,OAAO,2BAA2B,CAAC;AAAA,IACtE,MAAM,EAAE,IAAI,OAAO,wBAAwB,KAAK;AAAA,IAChD,SAAS,EAAE,IAAI,OAAO,2BAA2B,KAAK;AAAA,IACtD,WAAW;AAAA,IACX,WAAW,EAAE,IAAI,OAAO,6BAA6B,IACjD,OAAO,EAAE,IAAI,OAAO,6BAA6B,CAAC,IAClD;AAAA,IACJ,UAAU,EAAE,IAAI,OAAO,4BAA4B,IAC/C,OAAO,EAAE,IAAI,OAAO,4BAA4B,CAAC,IACjD;AAAA,IACJ,QAAQ,EAAE,IAAI,OAAO,kCAAkC,KAAK;AAAA,IAC5D,YAAY,EAAE,IAAI,OAAO,8BAA8B,KAAK;AAAA,IAC5D,aAAa,EAAE,IAAI,OAAO,+BAA+B,KAAK;AAAA,IAC9D,WAAW,EAAE,IAAI,OAAO,6BAA6B,KAAK;AAAA,EAC5D;AACF;","names":[]}
@@ -1,6 +1,6 @@
1
1
  // src/hono/geolocation.ts
2
2
  import { getRuntimeKey } from "hono/adapter";
3
- import { isIPv4, isIPv6 } from "net";
3
+ import { extractIpAddress } from "../utils/ip.mjs";
4
4
  var runtime = getRuntimeKey();
5
5
  function geolocation(c) {
6
6
  if (runtime === "workerd") {
@@ -31,20 +31,8 @@ function geolocation(c) {
31
31
  time_zone: null
32
32
  };
33
33
  }
34
- let ip_address = null;
35
- const viewerAddress = c.req.header("CloudFront-Viewer-Address");
36
- if (viewerAddress) {
37
- try {
38
- const url = new URL(`https://${viewerAddress}`);
39
- ip_address = url.hostname.replace("[", "").replace("]", "");
40
- } catch (_) {
41
- if (isIPv4(viewerAddress) || isIPv6(viewerAddress)) {
42
- ip_address = viewerAddress;
43
- }
44
- }
45
- }
46
34
  return {
47
- ip_address,
35
+ ip_address: extractIpAddress(c.req.header("CloudFront-Viewer-Address")),
48
36
  city: c.req.header("CloudFront-Viewer-City") ?? null,
49
37
  country: c.req.header("CloudFront-Viewer-Country") ?? null,
50
38
  continent: null,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hono/geolocation.ts"],"sourcesContent":["import { getRuntimeKey } from 'hono/adapter';\nimport { isIPv4, isIPv6 } from 'net';\nimport type { Context } from 'hono';\n\nexport type Geolocation = {\n ip_address: string | null;\n city: string | null;\n country: string | null; // ISO 3166-1 alpha-2\n continent: string | null;\n longitude: number | null;\n latitude: number | null;\n region: string | null; // ISO 3166-2\n metro_code: string | null;\n postal_code: string | null;\n time_zone: string | null;\n};\n\nconst runtime = getRuntimeKey();\nexport function geolocation(c: Context): Geolocation | null {\n /** reference: https://developers.cloudflare.com/rules/transform/managed-transforms/reference/#add-visitor-location-headers */\n if (runtime === 'workerd') {\n return {\n ip_address: c.req.header('true-client-ip') ?? c.req.header('cf-connecting-ip') ?? null,\n city: c.req.header('cf-ipcity') ?? null,\n country: c.req.header('cf-ipcountry') ?? null,\n continent: c.req.header('cf-ipcontinent') ?? null,\n longitude: c.req.header('cf-iplongitude') ? Number(c.req.header('cf-iplongitude')) : null,\n latitude: c.req.header('cf-iplatitude') ? Number(c.req.header('cf-iplatitude')) : null,\n region: c.req.header('cf-region-code') ?? null,\n metro_code: c.req.header('cf-metro-code') ?? null,\n postal_code: c.req.header('cf-postal-code') ?? null,\n time_zone: c.req.header('cf-timezone') ?? null,\n };\n }\n\n /** https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts */\n if (c.req.header('x-vercel-id')) {\n return {\n ip_address: c.req.header('x-real-ip') ?? null,\n city: c.req.header('x-vercel-ip-city') ?? null,\n country: c.req.header('x-vercel-ip-country') ?? null,\n continent: c.req.header('x-vercel-ip-continent') ?? null,\n longitude: c.req.header('x-vercel-ip-longitude')\n ? Number(c.req.header('x-vercel-ip-longitude'))\n : null,\n latitude: c.req.header('x-vercel-ip-latitude')\n ? Number(c.req.header('x-vercel-ip-latitude'))\n : null,\n region: c.req.header('x-vercel-ip-country-region') ?? null,\n metro_code: null,\n postal_code: c.req.header('x-vercel-ip-postal-code') ?? null,\n time_zone: null,\n };\n }\n\n // cloudfront\n // https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/adding-cloudfront-headers.html#cloudfront-headers-viewer-location\n let ip_address: string | null = null;\n const viewerAddress = c.req.header('CloudFront-Viewer-Address');\n if (viewerAddress) {\n try {\n const url = new URL(`https://${viewerAddress}`);\n ip_address = url.hostname.replace('[', '').replace(']', '');\n } catch (_) {\n if (isIPv4(viewerAddress) || isIPv6(viewerAddress)) {\n ip_address = viewerAddress;\n }\n }\n }\n\n return {\n ip_address,\n city: c.req.header('CloudFront-Viewer-City') ?? null,\n country: c.req.header('CloudFront-Viewer-Country') ?? null,\n continent: null,\n longitude: c.req.header('CloudFront-Viewer-Longitude')\n ? Number(c.req.header('CloudFront-Viewer-Longitude'))\n : null,\n latitude: c.req.header('CloudFront-Viewer-Latitude')\n ? Number(c.req.header('CloudFront-Viewer-Latitude'))\n : null,\n region: c.req.header('CloudFront-Viewer-Country-Region') ?? null,\n metro_code: c.req.header('CloudFront-Viewer-Metro-Code') ?? null,\n postal_code: c.req.header('CloudFront-Viewer-Postal-Code') ?? null,\n time_zone: c.req.header('CloudFront-Viewer-Time-Zone') ?? null,\n };\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AAC9B,SAAS,QAAQ,cAAc;AAgB/B,IAAM,UAAU,cAAc;AACvB,SAAS,YAAY,GAAgC;AAE1D,MAAI,YAAY,WAAW;AACzB,WAAO;AAAA,MACL,YAAY,EAAE,IAAI,OAAO,gBAAgB,KAAK,EAAE,IAAI,OAAO,kBAAkB,KAAK;AAAA,MAClF,MAAM,EAAE,IAAI,OAAO,WAAW,KAAK;AAAA,MACnC,SAAS,EAAE,IAAI,OAAO,cAAc,KAAK;AAAA,MACzC,WAAW,EAAE,IAAI,O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AAE,QAAQ,KAAK,EAAE;AAAA,IAC5D,SAAS,GAAG;AACV,UAAI,OAAO,aAAa,KAAK,OAAO,aAAa,GAAG;AAClD,qBAAa;AAAA,MACf;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA,MAAM,EAAE,IAAI,OAAO,wBAAwB,KAAK;AAAA,IAChD,SAAS,EAAE,IAAI,OAAO,2BAA2B,KAAK;AAAA,IACtD,WAAW;AAAA,IACX,WAAW,EAAE,IAAI,OAAO,6BAA6B,IACjD,OAAO,EAAE,IAAI,OAAO,6BAA6B,CAAC,IAClD;AAAA,IACJ,UAAU,EAAE,IAAI,OAAO,4BAA4B,IAC/C,OAAO,EAAE,IAAI,OAAO,4BAA4B,CAAC,IACjD;AAAA,IACJ,QAAQ,EAAE,IAAI,OAAO,kCAAkC,KAAK;AAAA,IAC5D,YAAY,EAAE,IAAI,OAAO,8BAA8B,KAAK;AAAA,IAC5D,aAAa,EAAE,IAAI,OAAO,+BAA+B,KAAK;AAAA,IAC9D,WAAW,EAAE,IAAI,OAAO,6BAA6B,KAAK;AAAA,EAC5D;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/hono/geolocation.ts"],"sourcesContent":["import { getRuntimeKey } from 'hono/adapter';\nimport { extractIpAddress } from '../utils/ip';\nimport type { Context } from 'hono';\n\nexport type Geolocation = {\n ip_address: string | null;\n city: string | null;\n country: string | null; // ISO 3166-1 alpha-2\n continent: string | null;\n longitude: number | null;\n latitude: number | null;\n region: string | null; // ISO 3166-2\n metro_code: string | null;\n postal_code: string | null;\n time_zone: string | null;\n};\n\nconst runtime = getRuntimeKey();\nexport function geolocation(c: Context): Geolocation | null {\n /** reference: https://developers.cloudflare.com/rules/transform/managed-transforms/reference/#add-visitor-location-headers */\n if (runtime === 'workerd') {\n return {\n ip_address: c.req.header('true-client-ip') ?? c.req.header('cf-connecting-ip') ?? null,\n city: c.req.header('cf-ipcity') ?? null,\n country: c.req.header('cf-ipcountry') ?? null,\n continent: c.req.header('cf-ipcontinent') ?? null,\n longitude: c.req.header('cf-iplongitude') ? Number(c.req.header('cf-iplongitude')) : null,\n latitude: c.req.header('cf-iplatitude') ? Number(c.req.header('cf-iplatitude')) : null,\n region: c.req.header('cf-region-code') ?? null,\n metro_code: c.req.header('cf-metro-code') ?? null,\n postal_code: c.req.header('cf-postal-code') ?? null,\n time_zone: c.req.header('cf-timezone') ?? null,\n };\n }\n\n /** https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts */\n if (c.req.header('x-vercel-id')) {\n return {\n ip_address: c.req.header('x-real-ip') ?? null,\n city: c.req.header('x-vercel-ip-city') ?? null,\n country: c.req.header('x-vercel-ip-country') ?? null,\n continent: c.req.header('x-vercel-ip-continent') ?? null,\n longitude: c.req.header('x-vercel-ip-longitude')\n ? Number(c.req.header('x-vercel-ip-longitude'))\n : null,\n latitude: c.req.header('x-vercel-ip-latitude')\n ? Number(c.req.header('x-vercel-ip-latitude'))\n : null,\n region: c.req.header('x-vercel-ip-country-region') ?? null,\n metro_code: null,\n postal_code: c.req.header('x-vercel-ip-postal-code') ?? null,\n time_zone: null,\n };\n }\n\n // cloudfront\n // https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/adding-cloudfront-headers.html#cloudfront-headers-viewer-location\n return {\n ip_address: extractIpAddress(c.req.header('CloudFront-Viewer-Address')),\n city: c.req.header('CloudFront-Viewer-City') ?? null,\n country: c.req.header('CloudFront-Viewer-Country') ?? null,\n continent: null,\n longitude: c.req.header('CloudFront-Viewer-Longitude')\n ? Number(c.req.header('CloudFront-Viewer-Longitude'))\n : null,\n latitude: c.req.header('CloudFront-Viewer-Latitude')\n ? Number(c.req.header('CloudFront-Viewer-Latitude'))\n : null,\n region: c.req.header('CloudFront-Viewer-Country-Region') ?? null,\n metro_code: c.req.header('CloudFront-Viewer-Metro-Code') ?? null,\n postal_code: c.req.header('CloudFront-Viewer-Postal-Code') ?? null,\n time_zone: c.req.header('CloudFront-Viewer-Time-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AAsB,CAAC,IAC3C;AAAA,MACJ,QAAQ,EAAE,IAAI,OAAO,4BAA4B,KAAK;AAAA,MACtD,YAAY;AAAA,MACZ,aAAa,EAAE,IAAI,OAAO,yBAAyB,KAAK;AAAA,MACxD,WAAW;AAAA,IACb;AAAA,EACF;AAIA,SAAO;AAAA,IACL,YAAY,iBAAiB,EAAE,IAAI,OAAO,2BAA2B,CAAC;AAAA,IACtE,MAAM,EAAE,IAAI,OAAO,wBAAwB,KAAK;AAAA,IAChD,SAAS,EAAE,IAAI,OAAO,2BAA2B,KAAK;AAAA,IACtD,WAAW;AAAA,IACX,WAAW,EAAE,IAAI,OAAO,6BAA6B,IACjD,OAAO,EAAE,IAAI,OAAO,6BAA6B,CAAC,IAClD;AAAA,IACJ,UAAU,EAAE,IAAI,OAAO,4BAA4B,IAC/C,OAAO,EAAE,IAAI,OAAO,4BAA4B,CAAC,IACjD;AAAA,IACJ,QAAQ,EAAE,IAAI,OAAO,kCAAkC,KAAK;AAAA,IAC5D,YAAY,EAAE,IAAI,OAAO,8BAA8B,KAAK;AAAA,IAC5D,aAAa,EAAE,IAAI,OAAO,+BAA+B,KAAK;AAAA,IAC9D,WAAW,EAAE,IAAI,OAAO,6BAA6B,KAAK;AAAA,EAC5D;AACF;","names":[]}
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/utils/ip.ts
21
+ var ip_exports = {};
22
+ __export(ip_exports, {
23
+ extractIpAddress: () => extractIpAddress
24
+ });
25
+ module.exports = __toCommonJS(ip_exports);
26
+ var import_net = require("net");
27
+ function extractIpAddress(ip) {
28
+ if (!ip) return null;
29
+ if ((0, import_net.isIPv4)(ip) || (0, import_net.isIPv6)(ip)) return ip;
30
+ if (ip.includes(".") && ip.includes(":")) {
31
+ const [address] = ip.split(":");
32
+ if ((0, import_net.isIPv4)(address)) return address;
33
+ }
34
+ if (!ip.includes("[") && !ip.includes("]") && !ip.includes(".") && ip.includes(":")) {
35
+ const parts = ip.split(":");
36
+ parts.pop();
37
+ const address = parts.join(":");
38
+ if ((0, import_net.isIPv6)(address)) return address;
39
+ }
40
+ try {
41
+ const url = new URL(`https://${ip}`);
42
+ const hostname = url.hostname.replace("[", "").replace("]", "");
43
+ if ((0, import_net.isIPv4)(hostname) || (0, import_net.isIPv6)(hostname)) return hostname;
44
+ return null;
45
+ } catch (_) {
46
+ return null;
47
+ }
48
+ }
49
+ // Annotate the CommonJS export names for ESM import in node:
50
+ 0 && (module.exports = {
51
+ extractIpAddress
52
+ });
53
+ //# sourceMappingURL=ip.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/ip.ts"],"sourcesContent":["import { isIPv4, isIPv6 } from 'net';\n\nexport function extractIpAddress(ip: string | undefined | null): string | null {\n if (!ip) return null;\n if (isIPv4(ip) || isIPv6(ip)) return ip;\n if (ip.includes('.') && ip.includes(':')) {\n const [address] = ip.split(':');\n if (isIPv4(address)) return address;\n }\n if (!ip.includes('[') && !ip.includes(']') && !ip.includes('.') && ip.includes(':')) {\n const parts = ip.split(':');\n parts.pop();\n const address = parts.join(':');\n if (isIPv6(address)) return address;\n }\n\n try {\n const url = new URL(`https://${ip}`);\n const hostname = url.hostname.replace('[', '').replace(']', '');\n if (isIPv4(hostname) || isIPv6(hostname)) return hostname;\n return null;\n } catch (_) {\n return null;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAA+B;AAExB,SAAS,iBAAiB,IAA8C;AAC7E,MAAI,CAAC,GAAI,QAAO;AAChB,UAAI,mBAAO,EAAE,SAAK,mBAAO,EAAE,EAAG,QAAO;AACrC,MAAI,GAAG,SAAS,GAAG,KAAK,GAAG,SAAS,GAAG,GAAG;AACxC,UAAM,CAAC,OAAO,IAAI,GAAG,MAAM,GAAG;AAC9B,YAAI,mBAAO,OAAO,EAAG,QAAO;AAAA,EAC9B;AACA,MAAI,CAAC,GAAG,SAAS,GAAG,KAAK,CAAC,GAAG,SAAS,GAAG,KAAK,CAAC,GAAG,SAAS,GAAG,KAAK,GAAG,SAAS,GAAG,GAAG;AACnF,UAAM,QAAQ,GAAG,MAAM,GAAG;AAC1B,UAAM,IAAI;AACV,UAAM,UAAU,MAAM,KAAK,GAAG;AAC9B,YAAI,mBAAO,OAAO,EAAG,QAAO;AAAA,EAC9B;AAEA,MAAI;AACF,UAAM,MAAM,IAAI,IAAI,WAAW,EAAE,EAAE;AACnC,UAAM,WAAW,IAAI,SAAS,QAAQ,KAAK,EAAE,EAAE,QAAQ,KAAK,EAAE;AAC9D,YAAI,mBAAO,QAAQ,SAAK,mBAAO,QAAQ,EAAG,QAAO;AACjD,WAAO;AAAA,EACT,SAAS,GAAG;AACV,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -0,0 +1,3 @@
1
+ declare function extractIpAddress(ip: string | undefined | null): string | null;
2
+
3
+ export { extractIpAddress };
@@ -0,0 +1,3 @@
1
+ declare function extractIpAddress(ip: string | undefined | null): string | null;
2
+
3
+ export { extractIpAddress };
@@ -0,0 +1,28 @@
1
+ // src/utils/ip.ts
2
+ import { isIPv4, isIPv6 } from "net";
3
+ function extractIpAddress(ip) {
4
+ if (!ip) return null;
5
+ if (isIPv4(ip) || isIPv6(ip)) return ip;
6
+ if (ip.includes(".") && ip.includes(":")) {
7
+ const [address] = ip.split(":");
8
+ if (isIPv4(address)) return address;
9
+ }
10
+ if (!ip.includes("[") && !ip.includes("]") && !ip.includes(".") && ip.includes(":")) {
11
+ const parts = ip.split(":");
12
+ parts.pop();
13
+ const address = parts.join(":");
14
+ if (isIPv6(address)) return address;
15
+ }
16
+ try {
17
+ const url = new URL(`https://${ip}`);
18
+ const hostname = url.hostname.replace("[", "").replace("]", "");
19
+ if (isIPv4(hostname) || isIPv6(hostname)) return hostname;
20
+ return null;
21
+ } catch (_) {
22
+ return null;
23
+ }
24
+ }
25
+ export {
26
+ extractIpAddress
27
+ };
28
+ //# sourceMappingURL=ip.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/ip.ts"],"sourcesContent":["import { isIPv4, isIPv6 } from 'net';\n\nexport function extractIpAddress(ip: string | undefined | null): string | null {\n if (!ip) return null;\n if (isIPv4(ip) || isIPv6(ip)) return ip;\n if (ip.includes('.') && ip.includes(':')) {\n const [address] = ip.split(':');\n if (isIPv4(address)) return address;\n }\n if (!ip.includes('[') && !ip.includes(']') && !ip.includes('.') && ip.includes(':')) {\n const parts = ip.split(':');\n parts.pop();\n const address = parts.join(':');\n if (isIPv6(address)) return address;\n }\n\n try {\n const url = new URL(`https://${ip}`);\n const hostname = url.hostname.replace('[', '').replace(']', '');\n if (isIPv4(hostname) || isIPv6(hostname)) return hostname;\n return null;\n } catch (_) {\n return null;\n }\n}\n"],"mappings":";AAAA,SAAS,QAAQ,cAAc;AAExB,SAAS,iBAAiB,IAA8C;AAC7E,MAAI,CAAC,GAAI,QAAO;AAChB,MAAI,OAAO,EAAE,KAAK,OAAO,EAAE,EAAG,QAAO;AACrC,MAAI,GAAG,SAAS,GAAG,KAAK,GAAG,SAAS,GAAG,GAAG;AACxC,UAAM,CAAC,OAAO,IAAI,GAAG,MAAM,GAAG;AAC9B,QAAI,OAAO,OAAO,EAAG,QAAO;AAAA,EAC9B;AACA,MAAI,CAAC,GAAG,SAAS,GAAG,KAAK,CAAC,GAAG,SAAS,GAAG,KAAK,CAAC,GAAG,SAAS,GAAG,KAAK,GAAG,SAAS,GAAG,GAAG;AACnF,UAAM,QAAQ,GAAG,MAAM,GAAG;AAC1B,UAAM,IAAI;AACV,UAAM,UAAU,MAAM,KAAK,GAAG;AAC9B,QAAI,OAAO,OAAO,EAAG,QAAO;AAAA,EAC9B;AAEA,MAAI;AACF,UAAM,MAAM,IAAI,IAAI,WAAW,EAAE,EAAE;AACnC,UAAM,WAAW,IAAI,SAAS,QAAQ,KAAK,EAAE,EAAE,QAAQ,KAAK,EAAE;AAC9D,QAAI,OAAO,QAAQ,KAAK,OAAO,QAAQ,EAAG,QAAO;AACjD,WAAO;AAAA,EACT,SAAS,GAAG;AACV,WAAO;AAAA,EACT;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shware/http",
3
- "version": "0.2.16",
3
+ "version": "0.2.18",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "scripts": {