@shware/http 1.2.3 → 1.2.6

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.
@@ -25,36 +25,35 @@ __export(geolocation_exports, {
25
25
  module.exports = __toCommonJS(geolocation_exports);
26
26
  var import_adapter = require("hono/adapter");
27
27
  var import_ip = require("../utils/ip.cjs");
28
- var runtime = (0, import_adapter.getRuntimeKey)();
29
- function geolocation(c) {
30
- if (runtime === "workerd") {
31
- return {
32
- ip_address: c.req.header("true-client-ip") ?? c.req.header("cf-connecting-ip") ?? null,
33
- city: c.req.header("cf-ipcity") ?? null,
34
- country: c.req.header("cf-ipcountry") ?? null,
35
- continent: c.req.header("cf-ipcontinent") ?? null,
36
- longitude: c.req.header("cf-iplongitude") ? Number(c.req.header("cf-iplongitude")) : null,
37
- latitude: c.req.header("cf-iplatitude") ? Number(c.req.header("cf-iplatitude")) : null,
38
- region: c.req.header("cf-region-code") ?? null,
39
- metro_code: c.req.header("cf-metro-code") ?? null,
40
- postal_code: c.req.header("cf-postal-code") ?? null,
41
- time_zone: c.req.header("cf-timezone") ?? null
42
- };
43
- }
44
- if (c.req.header("x-vercel-id")) {
45
- return {
46
- ip_address: c.req.header("x-real-ip") ?? null,
47
- city: c.req.header("x-vercel-ip-city") ?? null,
48
- country: c.req.header("x-vercel-ip-country") ?? null,
49
- continent: c.req.header("x-vercel-ip-continent") ?? null,
50
- longitude: c.req.header("x-vercel-ip-longitude") ? Number(c.req.header("x-vercel-ip-longitude")) : null,
51
- latitude: c.req.header("x-vercel-ip-latitude") ? Number(c.req.header("x-vercel-ip-latitude")) : null,
52
- region: c.req.header("x-vercel-ip-country-region") ?? null,
53
- metro_code: null,
54
- postal_code: c.req.header("x-vercel-ip-postal-code") ?? null,
55
- time_zone: null
56
- };
57
- }
28
+ function getGeolocationFromCloudflareWorker(c) {
29
+ return {
30
+ ip_address: c.req.header("true-client-ip") ?? c.req.header("cf-connecting-ip") ?? null,
31
+ city: c.req.header("cf-ipcity") ?? null,
32
+ country: c.req.header("cf-ipcountry") ?? null,
33
+ continent: c.req.header("cf-ipcontinent") ?? null,
34
+ longitude: c.req.header("cf-iplongitude") ? Number(c.req.header("cf-iplongitude")) : null,
35
+ latitude: c.req.header("cf-iplatitude") ? Number(c.req.header("cf-iplatitude")) : null,
36
+ region: c.req.header("cf-region-code") ?? null,
37
+ metro_code: c.req.header("cf-metro-code") ?? null,
38
+ postal_code: c.req.header("cf-postal-code") ?? null,
39
+ time_zone: c.req.header("cf-timezone") ?? null
40
+ };
41
+ }
42
+ function getGeolocationFromVercel(c) {
43
+ return {
44
+ ip_address: c.req.header("x-real-ip") ?? null,
45
+ city: c.req.header("x-vercel-ip-city") ?? null,
46
+ country: c.req.header("x-vercel-ip-country") ?? null,
47
+ continent: c.req.header("x-vercel-ip-continent") ?? null,
48
+ longitude: c.req.header("x-vercel-ip-longitude") ? Number(c.req.header("x-vercel-ip-longitude")) : null,
49
+ latitude: c.req.header("x-vercel-ip-latitude") ? Number(c.req.header("x-vercel-ip-latitude")) : null,
50
+ region: c.req.header("x-vercel-ip-country-region") ?? null,
51
+ metro_code: null,
52
+ postal_code: c.req.header("x-vercel-ip-postal-code") ?? null,
53
+ time_zone: null
54
+ };
55
+ }
56
+ function getGeolocationFromCloudfront(c) {
58
57
  return {
59
58
  ip_address: (0, import_ip.extractIpAddress)(c.req.header("CloudFront-Viewer-Address")),
60
59
  city: c.req.header("CloudFront-Viewer-City") ?? null,
@@ -68,6 +67,11 @@ function geolocation(c) {
68
67
  time_zone: c.req.header("CloudFront-Viewer-Time-Zone") ?? null
69
68
  };
70
69
  }
70
+ function geolocation(c) {
71
+ if ((0, import_adapter.getRuntimeKey)() === "workerd") return getGeolocationFromCloudflareWorker(c);
72
+ if (c.req.header("x-vercel-id")) return getGeolocationFromVercel(c);
73
+ return getGeolocationFromCloudfront(c);
74
+ }
71
75
  // Annotate the CommonJS export names for ESM import in node:
72
76
  0 && (module.exports = {
73
77
  geolocation
@@ -1 +1 @@
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;AAgBjC,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;AAAA,MACrF,UAAU,EAAE,IAAI,OAAO,eAAe,IAAI,OAAO,EAAE,IAAI,OAAO,eAAe,CAAC,IAAI;AAAA,MAClF,QAAQ,EAAE,IAAI,OAAO,gBAAgB,KAAK;AAAA,MAC1C,YAAY,EA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BAA8B,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\n/** reference: https://developers.cloudflare.com/rules/transform/managed-transforms/reference/#add-visitor-location-headers */\nfunction getGeolocationFromCloudflareWorker(c: Context): Geolocation {\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 */\nfunction getGeolocationFromVercel(c: Context): Geolocation {\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/** ref: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/adding-cloudfront-headers.html#cloudfront-headers-viewer-location */\nfunction getGeolocationFromCloudfront(c: Context): Geolocation {\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\nexport function geolocation(c: Context): Geolocation {\n if (getRuntimeKey() === 'workerd') return getGeolocationFromCloudflareWorker(c);\n if (c.req.header('x-vercel-id')) return getGeolocationFromVercel(c);\n return getGeolocationFromCloudfront(c);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAA8B;AAC9B,gBAAiC;AAiBjC,SAAS,mCAAmC,GAAyB;AACnE,SAAO;AAAA,IACL,YAAY,EAAE,IAAI,OAAO,gBAAgB,KAAK,EAAE,IAAI,OAAO,kBAAkB,KAAK;AAAA,IAClF,MAAM,EAAE,IAAI,OAAO,WAAW,KAAK;AAAA,IACnC,SAAS,EAAE,IAAI,OAAO,cAAc,KAAK;AAAA,IACzC,WAAW,EAAE,IAAI,OAAO,gBAAgB,KAAK;AAAA,IAC7C,WAAW,E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6B,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;AAEO,SAAS,YAAY,GAAyB;AACnD,UAAI,8BAAc,MAAM,UAAW,QAAO,mCAAmC,CAAC;AAC9E,MAAI,EAAE,IAAI,OAAO,aAAa,EAAG,QAAO,yBAAyB,CAAC;AAClE,SAAO,6BAA6B,CAAC;AACvC;","names":[]}
@@ -12,6 +12,6 @@ type Geolocation = {
12
12
  postal_code: string | null;
13
13
  time_zone: string | null;
14
14
  };
15
- declare function geolocation(c: Context): Geolocation | null;
15
+ declare function geolocation(c: Context): Geolocation;
16
16
 
17
17
  export { type Geolocation, geolocation };
@@ -12,6 +12,6 @@ type Geolocation = {
12
12
  postal_code: string | null;
13
13
  time_zone: string | null;
14
14
  };
15
- declare function geolocation(c: Context): Geolocation | null;
15
+ declare function geolocation(c: Context): Geolocation;
16
16
 
17
17
  export { type Geolocation, geolocation };
@@ -1,36 +1,35 @@
1
1
  // src/hono/geolocation.ts
2
2
  import { getRuntimeKey } from "hono/adapter";
3
3
  import { extractIpAddress } from "../utils/ip.mjs";
4
- var runtime = getRuntimeKey();
5
- function geolocation(c) {
6
- if (runtime === "workerd") {
7
- return {
8
- ip_address: c.req.header("true-client-ip") ?? c.req.header("cf-connecting-ip") ?? null,
9
- city: c.req.header("cf-ipcity") ?? null,
10
- country: c.req.header("cf-ipcountry") ?? null,
11
- continent: c.req.header("cf-ipcontinent") ?? null,
12
- longitude: c.req.header("cf-iplongitude") ? Number(c.req.header("cf-iplongitude")) : null,
13
- latitude: c.req.header("cf-iplatitude") ? Number(c.req.header("cf-iplatitude")) : null,
14
- region: c.req.header("cf-region-code") ?? null,
15
- metro_code: c.req.header("cf-metro-code") ?? null,
16
- postal_code: c.req.header("cf-postal-code") ?? null,
17
- time_zone: c.req.header("cf-timezone") ?? null
18
- };
19
- }
20
- if (c.req.header("x-vercel-id")) {
21
- return {
22
- ip_address: c.req.header("x-real-ip") ?? null,
23
- city: c.req.header("x-vercel-ip-city") ?? null,
24
- country: c.req.header("x-vercel-ip-country") ?? null,
25
- continent: c.req.header("x-vercel-ip-continent") ?? null,
26
- longitude: c.req.header("x-vercel-ip-longitude") ? Number(c.req.header("x-vercel-ip-longitude")) : null,
27
- latitude: c.req.header("x-vercel-ip-latitude") ? Number(c.req.header("x-vercel-ip-latitude")) : null,
28
- region: c.req.header("x-vercel-ip-country-region") ?? null,
29
- metro_code: null,
30
- postal_code: c.req.header("x-vercel-ip-postal-code") ?? null,
31
- time_zone: null
32
- };
33
- }
4
+ function getGeolocationFromCloudflareWorker(c) {
5
+ return {
6
+ ip_address: c.req.header("true-client-ip") ?? c.req.header("cf-connecting-ip") ?? null,
7
+ city: c.req.header("cf-ipcity") ?? null,
8
+ country: c.req.header("cf-ipcountry") ?? null,
9
+ continent: c.req.header("cf-ipcontinent") ?? null,
10
+ longitude: c.req.header("cf-iplongitude") ? Number(c.req.header("cf-iplongitude")) : null,
11
+ latitude: c.req.header("cf-iplatitude") ? Number(c.req.header("cf-iplatitude")) : null,
12
+ region: c.req.header("cf-region-code") ?? null,
13
+ metro_code: c.req.header("cf-metro-code") ?? null,
14
+ postal_code: c.req.header("cf-postal-code") ?? null,
15
+ time_zone: c.req.header("cf-timezone") ?? null
16
+ };
17
+ }
18
+ function getGeolocationFromVercel(c) {
19
+ return {
20
+ ip_address: c.req.header("x-real-ip") ?? null,
21
+ city: c.req.header("x-vercel-ip-city") ?? null,
22
+ country: c.req.header("x-vercel-ip-country") ?? null,
23
+ continent: c.req.header("x-vercel-ip-continent") ?? null,
24
+ longitude: c.req.header("x-vercel-ip-longitude") ? Number(c.req.header("x-vercel-ip-longitude")) : null,
25
+ latitude: c.req.header("x-vercel-ip-latitude") ? Number(c.req.header("x-vercel-ip-latitude")) : null,
26
+ region: c.req.header("x-vercel-ip-country-region") ?? null,
27
+ metro_code: null,
28
+ postal_code: c.req.header("x-vercel-ip-postal-code") ?? null,
29
+ time_zone: null
30
+ };
31
+ }
32
+ function getGeolocationFromCloudfront(c) {
34
33
  return {
35
34
  ip_address: extractIpAddress(c.req.header("CloudFront-Viewer-Address")),
36
35
  city: c.req.header("CloudFront-Viewer-City") ?? null,
@@ -44,6 +43,11 @@ function geolocation(c) {
44
43
  time_zone: c.req.header("CloudFront-Viewer-Time-Zone") ?? null
45
44
  };
46
45
  }
46
+ function geolocation(c) {
47
+ if (getRuntimeKey() === "workerd") return getGeolocationFromCloudflareWorker(c);
48
+ if (c.req.header("x-vercel-id")) return getGeolocationFromVercel(c);
49
+ return getGeolocationFromCloudfront(c);
50
+ }
47
51
  export {
48
52
  geolocation
49
53
  };
@@ -1 +1 @@
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-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,sBAAsB,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":[]}
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\n/** reference: https://developers.cloudflare.com/rules/transform/managed-transforms/reference/#add-visitor-location-headers */\nfunction getGeolocationFromCloudflareWorker(c: Context): Geolocation {\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 */\nfunction getGeolocationFromVercel(c: Context): Geolocation {\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/** ref: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/adding-cloudfront-headers.html#cloudfront-headers-viewer-location */\nfunction getGeolocationFromCloudfront(c: Context): Geolocation {\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\nexport function geolocation(c: Context): Geolocation {\n if (getRuntimeKey() === 'workerd') return getGeolocationFromCloudflareWorker(c);\n if (c.req.header('x-vercel-id')) return getGeolocationFromVercel(c);\n return getGeolocationFromCloudfront(c);\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AAiBjC,SAAS,mCAAmC,GAAyB;AACnE,SAAO;AAAA,IACL,YAAY,EAAE,IAAI,OAAO,gBAAgB,KAAK,EAAE,IAAI,OAAO,kBAAkB,KAAK;AAAA,IAClF,MAAM,EAAE,IAAI,OAAO,WAAW,KAAK;AAAA,IACnC,SAAS,EAAE,IAAI,OAAO,cAAc,KAAK;AAAA,IACzC,WAAW,EAAE,IAAI,OAAO,gBAAgB,KAAK;AAAA,IAC7C,WAAW,EAAE,IAAI,OAAO,gBAAgB,IAAI,OAAO,EAAE,IAAI,OAAO,gBAAgB,CAAC,IAAI;AAAA,IACrF,UAAU,EAAE,IAAI,OAAO,eAAe,IAAI,OAAO,EAAE,IAAI,OAAO,eAAe,CAAC,IAAI;AAAA,IAClF,QAAQ,EAAE,IAAI,OAAO,gBAAgB,KAAK;AAAA,IAC1C,YAAY,EAAE,IAAI,OAAO,eAAe,KAAK;AAAA,IAC7C,aAAa,EAAE,IAAI,OAAO,gBAAgB,KAAK;AAAA,IAC/C,WAAW,EAAE,IAAI,OAAO,aAAa,KAAK;AAAA,EAC5C;AACF;AAGA,SAAS,yBAAyB,GAAyB;AACzD,SAAO;AAAA,IACL,YAAY,EAAE,IAAI,OAAO,WAAW,KAAK;AAAA,IACzC,MAAM,EAAE,IAAI,OAAO,kBAAkB,KAAK;AAAA,IAC1C,SAAS,EAAE,IAAI,OAAO,qBAAqB,KAAK;AAAA,IAChD,WAAW,EAAE,IAAI,OAAO,uBAAuB,K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H: () => MAX_LENGTH,
40
40
  PurchaseError: () => import_error.PurchaseError,
41
41
  TokenBucket: () => import_token_bucket.TokenBucket,
42
+ base62: () => import_base62.base62,
42
43
  getErrorMessage: () => import_parse.getErrorMessage,
43
44
  getNextPageParam: () => import_response.getNextPageParam,
44
45
  getPreviousPageParam: () => import_response.getPreviousPageParam,
@@ -58,6 +59,7 @@ var import_response = require("./response.cjs");
58
59
  __reExport(index_exports, require("./vaild.cjs"), module.exports);
59
60
  __reExport(index_exports, require("./snowflake.cjs"), module.exports);
60
61
  var MAX_LENGTH = __toESM(require("./max-length/index.cjs"), 1);
62
+ var import_base62 = require("./utils/base62.cjs");
61
63
  var import_string = require("./utils/string.cjs");
62
64
  var import_timing = require("./utils/timing.cjs");
63
65
  var import_promise = require("./utils/promise.cjs");
@@ -74,6 +76,7 @@ var import_iso_3601_1 = require("./iso/iso_3601_1.cjs");
74
76
  MAX_LENGTH,
75
77
  PurchaseError,
76
78
  TokenBucket,
79
+ base62,
77
80
  getErrorMessage,
78
81
  getNextPageParam,
79
82
  getPreviousPageParam,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport {\n LoginTimeoutError,\n LoginCanceledError,\n CheckoutCreateError,\n PurchaseError,\n} from './error/index';\nexport type {\n NetworkErrorReason,\n StatusErrorReason,\n AuthenticationErrorReason,\n ModerationErrorReason,\n MultipartErrorReason,\n AppErrorReason,\n ErrorReason,\n} from './error/reason';\nexport * from './error/detail';\nexport * from './error/status';\nexport { getErrorMessage } from './error/parse';\nexport {\n pageParamsSchema,\n Cursor,\n initialPageParam,\n getPreviousPageParam,\n getNextPageParam,\n} from './response';\nexport type {\n Empty,\n EntityId,\n Entity,\n Response,\n InitParams,\n NextParams,\n PrevParams,\n PageParams,\n ParentPageParams,\n Page,\n} from './response';\n\nexport * from './vaild';\nexport * from './snowflake';\n\nexport * as MAX_LENGTH from './max-length/index';\nexport { hasText } from './utils/string';\nexport { timing } from './utils/timing';\nexport { once } from './utils/promise';\nexport { invariant } from './utils/invariant';\nexport { TokenBucket, type TokenBucketOptions } from './utils/token-bucket';\n\nexport { ISO_3601_1, type ISO3166CountryCode } from './iso/iso_3601_1';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaA,mBAKO;AAUP,0BAAc,+BA5Bd;AA6BA,0BAAc,+BA7Bd;AA8BA,mBAAgC;AAChC,sBAMO;AAcP,0BAAc,wBAnDd;AAoDA,0BAAc,4BApDd;AAsDA,iBAA4B;AAC5B,oBAAwB;AACxB,oBAAuB;AACvB,qBAAqB;AACrB,uBAA0B;AAC1B,0BAAqD;AAErD,wBAAoD;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport {\n LoginTimeoutError,\n LoginCanceledError,\n CheckoutCreateError,\n PurchaseError,\n} from './error/index';\nexport type {\n NetworkErrorReason,\n StatusErrorReason,\n AuthenticationErrorReason,\n ModerationErrorReason,\n MultipartErrorReason,\n AppErrorReason,\n ErrorReason,\n} from './error/reason';\nexport * from './error/detail';\nexport * from './error/status';\nexport { getErrorMessage } from './error/parse';\nexport {\n pageParamsSchema,\n Cursor,\n initialPageParam,\n getPreviousPageParam,\n getNextPageParam,\n} from './response';\nexport type {\n Empty,\n EntityId,\n Entity,\n Response,\n InitParams,\n NextParams,\n PrevParams,\n PageParams,\n ParentPageParams,\n Page,\n} from './response';\n\nexport * from './vaild';\nexport * from './snowflake';\n\nexport * as MAX_LENGTH from './max-length/index';\nexport { base62 } from './utils/base62';\nexport { hasText } from './utils/string';\nexport { timing } from './utils/timing';\nexport { once } from './utils/promise';\nexport { invariant } from './utils/invariant';\nexport { TokenBucket, type TokenBucketOptions } from './utils/token-bucket';\n\nexport { ISO_3601_1, type ISO3166CountryCode } from './iso/iso_3601_1';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaA,mBAKO;AAUP,0BAAc,+BA5Bd;AA6BA,0BAAc,+BA7Bd;AA8BA,mBAAgC;AAChC,sBAMO;AAcP,0BAAc,wBAnDd;AAoDA,0BAAc,4BApDd;AAsDA,iBAA4B;AAC5B,oBAAuB;AACvB,oBAAwB;AACxB,oBAAuB;AACvB,qBAAqB;AACrB,uBAA0B;AAC1B,0BAAqD;AAErD,wBAAoD;","names":[]}
package/dist/index.d.cts CHANGED
@@ -7,6 +7,7 @@ export { Cursor, Empty, Entity, EntityId, InitParams, NextParams, Page, PagePara
7
7
  export { Result, valid } from './vaild.cjs';
8
8
  export { UidGenerator, uid } from './snowflake.cjs';
9
9
  export { i as MAX_LENGTH } from './index-BnPgRQDl.cjs';
10
+ export { base62 } from './utils/base62.cjs';
10
11
  export { hasText } from './utils/string.cjs';
11
12
  export { timing } from './utils/timing.cjs';
12
13
  export { once } from './utils/promise.cjs';
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export { Cursor, Empty, Entity, EntityId, InitParams, NextParams, Page, PagePara
7
7
  export { Result, valid } from './vaild.js';
8
8
  export { UidGenerator, uid } from './snowflake.js';
9
9
  export { i as MAX_LENGTH } from './index-BnPgRQDl.js';
10
+ export { base62 } from './utils/base62.js';
10
11
  export { hasText } from './utils/string.js';
11
12
  export { timing } from './utils/timing.js';
12
13
  export { once } from './utils/promise.js';
package/dist/index.mjs CHANGED
@@ -18,6 +18,7 @@ import {
18
18
  export * from "./vaild.mjs";
19
19
  export * from "./snowflake.mjs";
20
20
  import * as MAX_LENGTH from "./max-length/index.mjs";
21
+ import { base62 } from "./utils/base62.mjs";
21
22
  import { hasText } from "./utils/string.mjs";
22
23
  import { timing } from "./utils/timing.mjs";
23
24
  import { once } from "./utils/promise.mjs";
@@ -33,6 +34,7 @@ export {
33
34
  MAX_LENGTH,
34
35
  PurchaseError,
35
36
  TokenBucket,
37
+ base62,
36
38
  getErrorMessage,
37
39
  getNextPageParam,
38
40
  getPreviousPageParam,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport {\n LoginTimeoutError,\n LoginCanceledError,\n CheckoutCreateError,\n PurchaseError,\n} from './error/index';\nexport type {\n NetworkErrorReason,\n StatusErrorReason,\n AuthenticationErrorReason,\n ModerationErrorReason,\n MultipartErrorReason,\n AppErrorReason,\n ErrorReason,\n} from './error/reason';\nexport * from './error/detail';\nexport * from './error/status';\nexport { getErrorMessage } from './error/parse';\nexport {\n pageParamsSchema,\n Cursor,\n initialPageParam,\n getPreviousPageParam,\n getNextPageParam,\n} from './response';\nexport type {\n Empty,\n EntityId,\n Entity,\n Response,\n InitParams,\n NextParams,\n PrevParams,\n PageParams,\n ParentPageParams,\n Page,\n} from './response';\n\nexport * from './vaild';\nexport * from './snowflake';\n\nexport * as MAX_LENGTH from './max-length/index';\nexport { hasText } from './utils/string';\nexport { timing } from './utils/timing';\nexport { once } from './utils/promise';\nexport { invariant } from './utils/invariant';\nexport { TokenBucket, type TokenBucketOptions } from './utils/token-bucket';\n\nexport { ISO_3601_1, type ISO3166CountryCode } from './iso/iso_3601_1';\n"],"mappings":";AAaA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAUP,cAAc;AACd,cAAc;AACd,SAAS,uBAAuB;AAChC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAcP,cAAc;AACd,cAAc;AAEd,YAAY,gBAAgB;AAC5B,SAAS,eAAe;AACxB,SAAS,cAAc;AACvB,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAC1B,SAAS,mBAA4C;AAErD,SAAS,kBAA2C;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport {\n LoginTimeoutError,\n LoginCanceledError,\n CheckoutCreateError,\n PurchaseError,\n} from './error/index';\nexport type {\n NetworkErrorReason,\n StatusErrorReason,\n AuthenticationErrorReason,\n ModerationErrorReason,\n MultipartErrorReason,\n AppErrorReason,\n ErrorReason,\n} from './error/reason';\nexport * from './error/detail';\nexport * from './error/status';\nexport { getErrorMessage } from './error/parse';\nexport {\n pageParamsSchema,\n Cursor,\n initialPageParam,\n getPreviousPageParam,\n getNextPageParam,\n} from './response';\nexport type {\n Empty,\n EntityId,\n Entity,\n Response,\n InitParams,\n NextParams,\n PrevParams,\n PageParams,\n ParentPageParams,\n Page,\n} from './response';\n\nexport * from './vaild';\nexport * from './snowflake';\n\nexport * as MAX_LENGTH from './max-length/index';\nexport { base62 } from './utils/base62';\nexport { hasText } from './utils/string';\nexport { timing } from './utils/timing';\nexport { once } from './utils/promise';\nexport { invariant } from './utils/invariant';\nexport { TokenBucket, type TokenBucketOptions } from './utils/token-bucket';\n\nexport { ISO_3601_1, type ISO3166CountryCode } from './iso/iso_3601_1';\n"],"mappings":";AAaA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAUP,cAAc;AACd,cAAc;AACd,SAAS,uBAAuB;AAChC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAcP,cAAc;AACd,cAAc;AAEd,YAAY,gBAAgB;AAC5B,SAAS,cAAc;AACvB,SAAS,eAAe;AACxB,SAAS,cAAc;AACvB,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAC1B,SAAS,mBAA4C;AAErD,SAAS,kBAA2C;","names":[]}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ // src/utils/__tests__/base62.test.ts
4
+ var import_base62 = require("../base62.cjs");
5
+ describe("base62", () => {
6
+ it("should encode", () => {
7
+ const buffer = Buffer.from([1]);
8
+ const encoded = import_base62.base62.encode(buffer);
9
+ expect(encoded).toEqual("1");
10
+ });
11
+ it("should decode", () => {
12
+ const encoded = "1";
13
+ const decoded = import_base62.base62.decode(encoded);
14
+ expect(decoded).toEqual(Buffer.from([1]));
15
+ });
16
+ it("should encode and decode", () => {
17
+ const buffer = Buffer.from("hello", "utf-8");
18
+ const encoded = import_base62.base62.encode(buffer);
19
+ const decoded = import_base62.base62.decode(encoded, buffer.length);
20
+ expect(decoded).toEqual(buffer);
21
+ });
22
+ it("should encode and decode with leading zeros", () => {
23
+ const buffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
24
+ const encoded = import_base62.base62.encode(buffer);
25
+ const decoded = import_base62.base62.decode(encoded, buffer.length);
26
+ expect(decoded).toEqual(buffer);
27
+ });
28
+ });
29
+ //# sourceMappingURL=base62.test.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/__tests__/base62.test.ts"],"sourcesContent":["import { base62 } from '../base62';\n\ndescribe('base62', () => {\n it('should encode', () => {\n const buffer = Buffer.from([1]);\n const encoded = base62.encode(buffer);\n expect(encoded).toEqual('1');\n });\n\n it('should decode', () => {\n const encoded = '1';\n const decoded = base62.decode(encoded);\n expect(decoded).toEqual(Buffer.from([1]));\n });\n\n it('should encode and decode', () => {\n const buffer = Buffer.from('hello', 'utf-8');\n const encoded = base62.encode(buffer);\n const decoded = base62.decode(encoded, buffer.length);\n expect(decoded).toEqual(buffer);\n });\n\n it('should encode and decode with leading zeros', () => {\n const buffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);\n const encoded = base62.encode(buffer);\n const decoded = base62.decode(encoded, buffer.length);\n expect(decoded).toEqual(buffer);\n });\n});\n"],"mappings":";;;AAAA,oBAAuB;AAEvB,SAAS,UAAU,MAAM;AACvB,KAAG,iBAAiB,MAAM;AACxB,UAAM,SAAS,OAAO,KAAK,CAAC,CAAC,CAAC;AAC9B,UAAM,UAAU,qBAAO,OAAO,MAAM;AACpC,WAAO,OAAO,EAAE,QAAQ,GAAG;AAAA,EAC7B,CAAC;AAED,KAAG,iBAAiB,MAAM;AACxB,UAAM,UAAU;AAChB,UAAM,UAAU,qBAAO,OAAO,OAAO;AACrC,WAAO,OAAO,EAAE,QAAQ,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;AAAA,EAC1C,CAAC;AAED,KAAG,4BAA4B,MAAM;AACnC,UAAM,SAAS,OAAO,KAAK,SAAS,OAAO;AAC3C,UAAM,UAAU,qBAAO,OAAO,MAAM;AACpC,UAAM,UAAU,qBAAO,OAAO,SAAS,OAAO,MAAM;AACpD,WAAO,OAAO,EAAE,QAAQ,MAAM;AAAA,EAChC,CAAC;AAED,KAAG,+CAA+C,MAAM;AACtD,UAAM,SAAS,OAAO,KAAK,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC;AAC3E,UAAM,UAAU,qBAAO,OAAO,MAAM;AACpC,UAAM,UAAU,qBAAO,OAAO,SAAS,OAAO,MAAM;AACpD,WAAO,OAAO,EAAE,QAAQ,MAAM;AAAA,EAChC,CAAC;AACH,CAAC;","names":[]}
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,27 @@
1
+ // src/utils/__tests__/base62.test.ts
2
+ import { base62 } from "../base62.mjs";
3
+ describe("base62", () => {
4
+ it("should encode", () => {
5
+ const buffer = Buffer.from([1]);
6
+ const encoded = base62.encode(buffer);
7
+ expect(encoded).toEqual("1");
8
+ });
9
+ it("should decode", () => {
10
+ const encoded = "1";
11
+ const decoded = base62.decode(encoded);
12
+ expect(decoded).toEqual(Buffer.from([1]));
13
+ });
14
+ it("should encode and decode", () => {
15
+ const buffer = Buffer.from("hello", "utf-8");
16
+ const encoded = base62.encode(buffer);
17
+ const decoded = base62.decode(encoded, buffer.length);
18
+ expect(decoded).toEqual(buffer);
19
+ });
20
+ it("should encode and decode with leading zeros", () => {
21
+ const buffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
22
+ const encoded = base62.encode(buffer);
23
+ const decoded = base62.decode(encoded, buffer.length);
24
+ expect(decoded).toEqual(buffer);
25
+ });
26
+ });
27
+ //# sourceMappingURL=base62.test.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/__tests__/base62.test.ts"],"sourcesContent":["import { base62 } from '../base62';\n\ndescribe('base62', () => {\n it('should encode', () => {\n const buffer = Buffer.from([1]);\n const encoded = base62.encode(buffer);\n expect(encoded).toEqual('1');\n });\n\n it('should decode', () => {\n const encoded = '1';\n const decoded = base62.decode(encoded);\n expect(decoded).toEqual(Buffer.from([1]));\n });\n\n it('should encode and decode', () => {\n const buffer = Buffer.from('hello', 'utf-8');\n const encoded = base62.encode(buffer);\n const decoded = base62.decode(encoded, buffer.length);\n expect(decoded).toEqual(buffer);\n });\n\n it('should encode and decode with leading zeros', () => {\n const buffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);\n const encoded = base62.encode(buffer);\n const decoded = base62.decode(encoded, buffer.length);\n expect(decoded).toEqual(buffer);\n });\n});\n"],"mappings":";AAAA,SAAS,cAAc;AAEvB,SAAS,UAAU,MAAM;AACvB,KAAG,iBAAiB,MAAM;AACxB,UAAM,SAAS,OAAO,KAAK,CAAC,CAAC,CAAC;AAC9B,UAAM,UAAU,OAAO,OAAO,MAAM;AACpC,WAAO,OAAO,EAAE,QAAQ,GAAG;AAAA,EAC7B,CAAC;AAED,KAAG,iBAAiB,MAAM;AACxB,UAAM,UAAU;AAChB,UAAM,UAAU,OAAO,OAAO,OAAO;AACrC,WAAO,OAAO,EAAE,QAAQ,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;AAAA,EAC1C,CAAC;AAED,KAAG,4BAA4B,MAAM;AACnC,UAAM,SAAS,OAAO,KAAK,SAAS,OAAO;AAC3C,UAAM,UAAU,OAAO,OAAO,MAAM;AACpC,UAAM,UAAU,OAAO,OAAO,SAAS,OAAO,MAAM;AACpD,WAAO,OAAO,EAAE,QAAQ,MAAM;AAAA,EAChC,CAAC;AAED,KAAG,+CAA+C,MAAM;AACtD,UAAM,SAAS,OAAO,KAAK,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC;AAC3E,UAAM,UAAU,OAAO,OAAO,MAAM;AACpC,UAAM,UAAU,OAAO,OAAO,SAAS,OAAO,MAAM;AACpD,WAAO,OAAO,EAAE,QAAQ,MAAM;AAAA,EAChC,CAAC;AACH,CAAC;","names":[]}
@@ -0,0 +1,85 @@
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/base62.ts
21
+ var base62_exports = {};
22
+ __export(base62_exports, {
23
+ base62: () => base62
24
+ });
25
+ module.exports = __toCommonJS(base62_exports);
26
+ var base62 = {
27
+ encode: (buffer) => {
28
+ const base = 62n;
29
+ const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
30
+ let leadingZeros = 0;
31
+ for (const byte of buffer) {
32
+ if (byte === 0) leadingZeros++;
33
+ else break;
34
+ }
35
+ let num = BigInt("0x" + buffer.toString("hex"));
36
+ if (num === 0n) return "0".repeat(buffer.length);
37
+ const chars = [];
38
+ while (num > 0n) {
39
+ const remainder = num % base;
40
+ chars.push(charset[Number(remainder)]);
41
+ num /= base;
42
+ }
43
+ return "0".repeat(leadingZeros) + chars.reverse().join("");
44
+ },
45
+ decode: (string, expectedLength) => {
46
+ const base = 62n;
47
+ const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
48
+ let leadingZeros = 0;
49
+ for (const ch of string) {
50
+ if (ch === "0") leadingZeros++;
51
+ else break;
52
+ }
53
+ if (leadingZeros === string.length) {
54
+ return Buffer.alloc(expectedLength ?? leadingZeros);
55
+ }
56
+ let num = 0n;
57
+ for (let i = leadingZeros; i < string.length; i++) {
58
+ const ch = string[i];
59
+ const idx = charset.indexOf(ch);
60
+ if (idx === -1) {
61
+ throw new Error(`Invalid base62 character: ${ch}`);
62
+ }
63
+ num = num * base + BigInt(idx);
64
+ }
65
+ let hex = num.toString(16);
66
+ if (hex.length % 2 !== 0) hex = "0" + hex;
67
+ let buf = Buffer.from(hex, "hex");
68
+ if (leadingZeros > 0) {
69
+ buf = Buffer.concat([Buffer.alloc(leadingZeros), buf]);
70
+ }
71
+ if (expectedLength !== void 0) {
72
+ if (buf.length < expectedLength) {
73
+ buf = Buffer.concat([Buffer.alloc(expectedLength - buf.length), buf]);
74
+ } else if (buf.length > expectedLength) {
75
+ buf = buf.subarray(buf.length - expectedLength);
76
+ }
77
+ }
78
+ return buf;
79
+ }
80
+ };
81
+ // Annotate the CommonJS export names for ESM import in node:
82
+ 0 && (module.exports = {
83
+ base62
84
+ });
85
+ //# sourceMappingURL=base62.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/base62.ts"],"sourcesContent":["export const base62 = {\n encode: (buffer: Buffer): string => {\n const base = 62n;\n const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n let leadingZeros = 0;\n for (const byte of buffer) {\n if (byte === 0) leadingZeros++;\n else break;\n }\n\n let num = BigInt('0x' + buffer.toString('hex'));\n if (num === 0n) return '0'.repeat(buffer.length);\n\n const chars: string[] = [];\n while (num > 0n) {\n const remainder = num % base;\n chars.push(charset[Number(remainder)]!);\n num /= base;\n }\n\n return '0'.repeat(leadingZeros) + chars.reverse().join('');\n },\n decode: (string: string, expectedLength?: number): Buffer => {\n const base = 62n;\n const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n\n let leadingZeros = 0;\n for (const ch of string) {\n if (ch === '0') leadingZeros++;\n else break;\n }\n\n if (leadingZeros === string.length) {\n return Buffer.alloc(expectedLength ?? leadingZeros);\n }\n\n let num = 0n;\n for (let i = leadingZeros; i < string.length; i++) {\n const ch = string[i];\n const idx = charset.indexOf(ch);\n if (idx === -1) {\n throw new Error(`Invalid base62 character: ${ch}`);\n }\n num = num * base + BigInt(idx);\n }\n\n let hex = num.toString(16);\n if (hex.length % 2 !== 0) hex = '0' + hex;\n let buf = Buffer.from(hex, 'hex');\n\n if (leadingZeros > 0) {\n buf = Buffer.concat([Buffer.alloc(leadingZeros), buf]);\n }\n\n if (expectedLength !== undefined) {\n if (buf.length < expectedLength) {\n buf = Buffer.concat([Buffer.alloc(expectedLength - buf.length), buf]);\n } else if (buf.length > expectedLength) {\n buf = buf.subarray(buf.length - expectedLength);\n }\n }\n\n return buf;\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,IAAM,SAAS;AAAA,EACpB,QAAQ,CAAC,WAA2B;AAClC,UAAM,OAAO;AACb,UAAM,UAAU;AAChB,QAAI,eAAe;AACnB,eAAW,QAAQ,QAAQ;AACzB,UAAI,SAAS,EAAG;AAAA,UACX;AAAA,IACP;AAEA,QAAI,MAAM,OAAO,OAAO,OAAO,SAAS,KAAK,CAAC;AAC9C,QAAI,QAAQ,GAAI,QAAO,IAAI,OAAO,OAAO,MAAM;AAE/C,UAAM,QAAkB,CAAC;AACzB,WAAO,MAAM,IAAI;AACf,YAAM,YAAY,MAAM;AACxB,YAAM,KAAK,QAAQ,OAAO,SAAS,CAAC,CAAE;AACtC,aAAO;AAAA,IACT;AAEA,WAAO,IAAI,OAAO,YAAY,IAAI,MAAM,QAAQ,EAAE,KAAK,EAAE;AAAA,EAC3D;AAAA,EACA,QAAQ,CAAC,QAAgB,mBAAoC;AAC3D,UAAM,OAAO;AACb,UAAM,UAAU;AAEhB,QAAI,eAAe;AACnB,eAAW,MAAM,QAAQ;AACvB,UAAI,OAAO,IAAK;AAAA,UACX;AAAA,IACP;AAEA,QAAI,iBAAiB,OAAO,QAAQ;AAClC,aAAO,OAAO,MAAM,kBAAkB,YAAY;AAAA,IACpD;AAEA,QAAI,MAAM;AACV,aAAS,IAAI,cAAc,IAAI,OAAO,QAAQ,KAAK;AACjD,YAAM,KAAK,OAAO,CAAC;AACnB,YAAM,MAAM,QAAQ,QAAQ,EAAE;AAC9B,UAAI,QAAQ,IAAI;AACd,cAAM,IAAI,MAAM,6BAA6B,EAAE,EAAE;AAAA,MACnD;AACA,YAAM,MAAM,OAAO,OAAO,GAAG;AAAA,IAC/B;AAEA,QAAI,MAAM,IAAI,SAAS,EAAE;AACzB,QAAI,IAAI,SAAS,MAAM,EAAG,OAAM,MAAM;AACtC,QAAI,MAAM,OAAO,KAAK,KAAK,KAAK;AAEhC,QAAI,eAAe,GAAG;AACpB,YAAM,OAAO,OAAO,CAAC,OAAO,MAAM,YAAY,GAAG,GAAG,CAAC;AAAA,IACvD;AAEA,QAAI,mBAAmB,QAAW;AAChC,UAAI,IAAI,SAAS,gBAAgB;AAC/B,cAAM,OAAO,OAAO,CAAC,OAAO,MAAM,iBAAiB,IAAI,MAAM,GAAG,GAAG,CAAC;AAAA,MACtE,WAAW,IAAI,SAAS,gBAAgB;AACtC,cAAM,IAAI,SAAS,IAAI,SAAS,cAAc;AAAA,MAChD;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -0,0 +1,6 @@
1
+ declare const base62: {
2
+ encode: (buffer: Buffer) => string;
3
+ decode: (string: string, expectedLength?: number) => Buffer;
4
+ };
5
+
6
+ export { base62 };
@@ -0,0 +1,6 @@
1
+ declare const base62: {
2
+ encode: (buffer: Buffer) => string;
3
+ decode: (string: string, expectedLength?: number) => Buffer;
4
+ };
5
+
6
+ export { base62 };
@@ -0,0 +1,60 @@
1
+ // src/utils/base62.ts
2
+ var base62 = {
3
+ encode: (buffer) => {
4
+ const base = 62n;
5
+ const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
6
+ let leadingZeros = 0;
7
+ for (const byte of buffer) {
8
+ if (byte === 0) leadingZeros++;
9
+ else break;
10
+ }
11
+ let num = BigInt("0x" + buffer.toString("hex"));
12
+ if (num === 0n) return "0".repeat(buffer.length);
13
+ const chars = [];
14
+ while (num > 0n) {
15
+ const remainder = num % base;
16
+ chars.push(charset[Number(remainder)]);
17
+ num /= base;
18
+ }
19
+ return "0".repeat(leadingZeros) + chars.reverse().join("");
20
+ },
21
+ decode: (string, expectedLength) => {
22
+ const base = 62n;
23
+ const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
24
+ let leadingZeros = 0;
25
+ for (const ch of string) {
26
+ if (ch === "0") leadingZeros++;
27
+ else break;
28
+ }
29
+ if (leadingZeros === string.length) {
30
+ return Buffer.alloc(expectedLength ?? leadingZeros);
31
+ }
32
+ let num = 0n;
33
+ for (let i = leadingZeros; i < string.length; i++) {
34
+ const ch = string[i];
35
+ const idx = charset.indexOf(ch);
36
+ if (idx === -1) {
37
+ throw new Error(`Invalid base62 character: ${ch}`);
38
+ }
39
+ num = num * base + BigInt(idx);
40
+ }
41
+ let hex = num.toString(16);
42
+ if (hex.length % 2 !== 0) hex = "0" + hex;
43
+ let buf = Buffer.from(hex, "hex");
44
+ if (leadingZeros > 0) {
45
+ buf = Buffer.concat([Buffer.alloc(leadingZeros), buf]);
46
+ }
47
+ if (expectedLength !== void 0) {
48
+ if (buf.length < expectedLength) {
49
+ buf = Buffer.concat([Buffer.alloc(expectedLength - buf.length), buf]);
50
+ } else if (buf.length > expectedLength) {
51
+ buf = buf.subarray(buf.length - expectedLength);
52
+ }
53
+ }
54
+ return buf;
55
+ }
56
+ };
57
+ export {
58
+ base62
59
+ };
60
+ //# sourceMappingURL=base62.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/base62.ts"],"sourcesContent":["export const base62 = {\n encode: (buffer: Buffer): string => {\n const base = 62n;\n const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n let leadingZeros = 0;\n for (const byte of buffer) {\n if (byte === 0) leadingZeros++;\n else break;\n }\n\n let num = BigInt('0x' + buffer.toString('hex'));\n if (num === 0n) return '0'.repeat(buffer.length);\n\n const chars: string[] = [];\n while (num > 0n) {\n const remainder = num % base;\n chars.push(charset[Number(remainder)]!);\n num /= base;\n }\n\n return '0'.repeat(leadingZeros) + chars.reverse().join('');\n },\n decode: (string: string, expectedLength?: number): Buffer => {\n const base = 62n;\n const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n\n let leadingZeros = 0;\n for (const ch of string) {\n if (ch === '0') leadingZeros++;\n else break;\n }\n\n if (leadingZeros === string.length) {\n return Buffer.alloc(expectedLength ?? leadingZeros);\n }\n\n let num = 0n;\n for (let i = leadingZeros; i < string.length; i++) {\n const ch = string[i];\n const idx = charset.indexOf(ch);\n if (idx === -1) {\n throw new Error(`Invalid base62 character: ${ch}`);\n }\n num = num * base + BigInt(idx);\n }\n\n let hex = num.toString(16);\n if (hex.length % 2 !== 0) hex = '0' + hex;\n let buf = Buffer.from(hex, 'hex');\n\n if (leadingZeros > 0) {\n buf = Buffer.concat([Buffer.alloc(leadingZeros), buf]);\n }\n\n if (expectedLength !== undefined) {\n if (buf.length < expectedLength) {\n buf = Buffer.concat([Buffer.alloc(expectedLength - buf.length), buf]);\n } else if (buf.length > expectedLength) {\n buf = buf.subarray(buf.length - expectedLength);\n }\n }\n\n return buf;\n },\n};\n"],"mappings":";AAAO,IAAM,SAAS;AAAA,EACpB,QAAQ,CAAC,WAA2B;AAClC,UAAM,OAAO;AACb,UAAM,UAAU;AAChB,QAAI,eAAe;AACnB,eAAW,QAAQ,QAAQ;AACz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B,IAAI,MAAM,GAAG,GAAG,CAAC;AAAA,MACtE,WAAW,IAAI,SAAS,gBAAgB;AACtC,cAAM,IAAI,SAAS,IAAI,SAAS,cAAc;AAAA,MAChD;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shware/http",
3
- "version": "1.2.3",
3
+ "version": "1.2.6",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "repository": {
@@ -39,16 +39,16 @@
39
39
  ],
40
40
  "devDependencies": {
41
41
  "@types/jest": "^30.0.0",
42
- "@types/node": "^24.3.3",
43
- "@types/react": "^19.1.13",
44
- "jest": "^30.1.3",
45
- "ts-jest": "^29.4.1",
46
- "typescript": "^5.9.2",
47
- "@repo/eslint-config": "0.0.4",
42
+ "@types/node": "^24.7.2",
43
+ "@types/react": "^19.2.2",
44
+ "jest": "^30.2.0",
45
+ "ts-jest": "^29.4.5",
46
+ "typescript": "^5.9.3",
47
+ "@repo/eslint-config": "0.0.6",
48
48
  "@repo/typescript-config": "0.0.0"
49
49
  },
50
50
  "dependencies": {
51
- "zod": "^4.1.8"
51
+ "zod": "^4.1.12"
52
52
  },
53
53
  "peerDependencies": {
54
54
  "axios": "^1.10.0",