@shware/http 2.11.3 → 2.12.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/_virtual/_rolldown/runtime.cjs +13 -0
- package/dist/_virtual/_rolldown/runtime.mjs +13 -0
- package/dist/cache/index.cjs +5 -30
- package/dist/cache/index.cjs.map +1 -1
- package/dist/cache/index.d.cts +7 -5
- package/dist/cache/index.d.cts.map +1 -0
- package/dist/cache/index.d.mts +11 -0
- package/dist/cache/index.d.mts.map +1 -0
- package/dist/cache/index.mjs +5 -6
- package/dist/cache/index.mjs.map +1 -1
- package/dist/error/detail.cjs +100 -90
- package/dist/error/detail.cjs.map +1 -1
- package/dist/error/detail.d.cts +77 -78
- package/dist/error/detail.d.cts.map +1 -0
- package/dist/error/detail.d.mts +108 -0
- package/dist/error/detail.d.mts.map +1 -0
- package/dist/error/detail.mjs +99 -65
- package/dist/error/detail.mjs.map +1 -1
- package/dist/error/i18n/en-us.cjs +5 -29
- package/dist/error/i18n/en-us.cjs.map +1 -1
- package/dist/error/i18n/en-us.d.cts +3 -1
- package/dist/error/i18n/en-us.d.cts.map +1 -0
- package/dist/error/i18n/en-us.d.mts +5 -0
- package/dist/error/i18n/en-us.d.mts.map +1 -0
- package/dist/error/i18n/en-us.mjs +5 -5
- package/dist/error/i18n/en-us.mjs.map +1 -1
- package/dist/error/index.cjs +29 -56
- package/dist/error/index.cjs.map +1 -1
- package/dist/error/index.d.cts +12 -10
- package/dist/error/index.d.cts.map +1 -0
- package/dist/error/index.d.mts +21 -0
- package/dist/error/index.d.mts.map +1 -0
- package/dist/error/index.mjs +25 -28
- package/dist/error/index.mjs.map +1 -1
- package/dist/error/parse.cjs +63 -61
- package/dist/error/parse.cjs.map +1 -1
- package/dist/error/parse.d.cts +8 -7
- package/dist/error/parse.d.cts.map +1 -0
- package/dist/error/{parse.d.ts → parse.d.mts} +8 -7
- package/dist/error/parse.d.mts.map +1 -0
- package/dist/error/parse.mjs +59 -33
- package/dist/error/parse.mjs.map +1 -1
- package/dist/error/reason.cjs +0 -19
- package/dist/error/reason.d.cts +5 -4
- package/dist/error/reason.d.cts.map +1 -0
- package/dist/error/reason.d.mts +6 -0
- package/dist/error/reason.d.mts.map +1 -0
- package/dist/error/reason.mjs +1 -1
- package/dist/error/status.cjs +103 -256
- package/dist/error/status.cjs.map +1 -1
- package/dist/error/status.d.cts +58 -58
- package/dist/error/status.d.cts.map +1 -0
- package/dist/error/status.d.mts +69 -0
- package/dist/error/status.d.mts.map +1 -0
- package/dist/error/status.mjs +98 -227
- package/dist/error/status.mjs.map +1 -1
- package/dist/google-one-tap/index.cjs +135 -138
- package/dist/google-one-tap/index.cjs.map +1 -1
- package/dist/google-one-tap/index.d.cts +49 -48
- package/dist/google-one-tap/index.d.cts.map +1 -0
- package/dist/google-one-tap/index.d.mts +82 -0
- package/dist/google-one-tap/index.d.mts.map +1 -0
- package/dist/google-one-tap/index.mjs +133 -112
- package/dist/google-one-tap/index.mjs.map +1 -1
- package/dist/google-one-tap/types.cjs +0 -19
- package/dist/google-one-tap/types.d.cts +124 -122
- package/dist/google-one-tap/types.d.cts.map +1 -0
- package/dist/google-one-tap/types.d.mts +157 -0
- package/dist/google-one-tap/types.d.mts.map +1 -0
- package/dist/google-one-tap/types.mjs +1 -1
- package/dist/hono/authorizer.cjs +28 -65
- package/dist/hono/authorizer.cjs.map +1 -1
- package/dist/hono/authorizer.d.cts +12 -11
- package/dist/hono/authorizer.d.cts.map +1 -0
- package/dist/hono/authorizer.d.mts +19 -0
- package/dist/hono/authorizer.d.mts.map +1 -0
- package/dist/hono/authorizer.mjs +23 -36
- package/dist/hono/authorizer.mjs.map +1 -1
- package/dist/hono/csrf.cjs +81 -92
- package/dist/hono/csrf.cjs.map +1 -1
- package/dist/hono/csrf.d.cts +46 -45
- package/dist/hono/csrf.d.cts.map +1 -0
- package/dist/hono/csrf.d.mts +76 -0
- package/dist/hono/csrf.d.mts.map +1 -0
- package/dist/hono/csrf.mjs +76 -63
- package/dist/hono/csrf.mjs.map +1 -1
- package/dist/hono/geolocation.cjs +10 -34
- package/dist/hono/geolocation.cjs.map +1 -1
- package/dist/hono/geolocation.d.cts +5 -4
- package/dist/hono/geolocation.d.cts.map +1 -0
- package/dist/hono/geolocation.d.mts +7 -0
- package/dist/hono/geolocation.d.mts.map +1 -0
- package/dist/hono/geolocation.mjs +8 -12
- package/dist/hono/geolocation.mjs.map +1 -1
- package/dist/hono/handler.cjs +37 -61
- package/dist/hono/handler.cjs.map +1 -1
- package/dist/hono/handler.d.cts +26 -25
- package/dist/hono/handler.d.cts.map +1 -0
- package/dist/hono/handler.d.mts +32 -0
- package/dist/hono/handler.d.mts.map +1 -0
- package/dist/hono/handler.mjs +34 -34
- package/dist/hono/handler.mjs.map +1 -1
- package/dist/hono/index.cjs +14 -48
- package/dist/hono/index.d.cts +7 -15
- package/dist/hono/index.d.mts +7 -0
- package/dist/hono/index.mjs +4 -14
- package/dist/hono/rate-limit.cjs +65 -110
- package/dist/hono/rate-limit.cjs.map +1 -1
- package/dist/hono/rate-limit.d.cts +27 -26
- package/dist/hono/rate-limit.d.cts.map +1 -0
- package/dist/hono/rate-limit.d.mts +36 -0
- package/dist/hono/rate-limit.d.mts.map +1 -0
- package/dist/hono/rate-limit.mjs +60 -81
- package/dist/hono/rate-limit.mjs.map +1 -1
- package/dist/hono/validator.cjs +47 -66
- package/dist/hono/validator.cjs.map +1 -1
- package/dist/hono/validator.d.cts +25 -30
- package/dist/hono/validator.d.cts.map +1 -0
- package/dist/hono/validator.d.mts +26 -0
- package/dist/hono/validator.d.mts.map +1 -0
- package/dist/hono/validator.mjs +43 -43
- package/dist/hono/validator.mjs.map +1 -1
- package/dist/index.cjs +39 -95
- package/dist/index.d.cts +12 -14
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +5 -48
- package/dist/iso/iso_3601_1.cjs +1512 -290
- package/dist/iso/iso_3601_1.cjs.map +1 -1
- package/dist/iso/iso_3601_1.d.cts +8 -6
- package/dist/iso/iso_3601_1.d.cts.map +1 -0
- package/dist/iso/iso_3601_1.d.mts +11 -0
- package/dist/iso/iso_3601_1.d.mts.map +1 -0
- package/dist/iso/iso_3601_1.mjs +1511 -265
- package/dist/iso/iso_3601_1.mjs.map +1 -1
- package/dist/max-length/index.cjs +158 -91
- package/dist/max-length/index.cjs.map +1 -1
- package/dist/max-length/index.d.cts +110 -1
- package/dist/max-length/index.d.cts.map +1 -0
- package/dist/{index-BhgdyigB.d.ts → max-length/index.d.mts} +9 -28
- package/dist/max-length/index.d.mts.map +1 -0
- package/dist/max-length/index.mjs +133 -47
- package/dist/max-length/index.mjs.map +1 -1
- package/dist/message.cjs +5 -30
- package/dist/message.cjs.map +1 -1
- package/dist/message.d.cts +3 -1
- package/dist/message.d.cts.map +1 -0
- package/dist/{message.d.ts → message.d.mts} +3 -1
- package/dist/message.d.mts.map +1 -0
- package/dist/message.mjs +5 -6
- package/dist/message.mjs.map +1 -1
- package/dist/polyfills/index.cjs +4 -4
- package/dist/polyfills/index.cjs.map +1 -1
- package/dist/polyfills/index.d.cts +1 -2
- package/dist/polyfills/index.d.mts +1 -0
- package/dist/polyfills/index.mjs +5 -2
- package/dist/polyfills/index.mjs.map +1 -1
- package/dist/response.cjs +191 -208
- package/dist/response.cjs.map +1 -1
- package/dist/response.d.cts +53 -54
- package/dist/response.d.cts.map +1 -0
- package/dist/response.d.mts +82 -0
- package/dist/response.d.mts.map +1 -0
- package/dist/response.mjs +182 -175
- package/dist/response.mjs.map +1 -1
- package/dist/snowflake.cjs +48 -70
- package/dist/snowflake.cjs.map +1 -1
- package/dist/snowflake.d.cts +16 -14
- package/dist/snowflake.d.cts.map +1 -0
- package/dist/snowflake.d.mts +31 -0
- package/dist/snowflake.d.mts.map +1 -0
- package/dist/snowflake.mjs +46 -44
- package/dist/snowflake.mjs.map +1 -1
- package/dist/utils/geolocation.cjs +51 -75
- package/dist/utils/geolocation.cjs.map +1 -1
- package/dist/utils/geolocation.d.cts +14 -12
- package/dist/utils/geolocation.d.cts.map +1 -0
- package/dist/utils/{geolocation.d.ts → geolocation.d.mts} +14 -12
- package/dist/utils/geolocation.d.mts.map +1 -0
- package/dist/utils/geolocation.mjs +47 -47
- package/dist/utils/geolocation.mjs.map +1 -1
- package/dist/utils/google-tag-gateway.cjs +35 -56
- package/dist/utils/google-tag-gateway.cjs.map +1 -1
- package/dist/utils/google-tag-gateway.d.cts +3 -1
- package/dist/utils/google-tag-gateway.d.cts.map +1 -0
- package/dist/utils/{google-tag-gateway.d.ts → google-tag-gateway.d.mts} +3 -1
- package/dist/utils/google-tag-gateway.d.mts.map +1 -0
- package/dist/utils/google-tag-gateway.mjs +33 -30
- package/dist/utils/google-tag-gateway.mjs.map +1 -1
- package/dist/utils/timing.cjs +50 -71
- package/dist/utils/timing.cjs.map +1 -1
- package/dist/utils/timing.d.cts +11 -9
- package/dist/utils/timing.d.cts.map +1 -0
- package/dist/utils/timing.d.mts +15 -0
- package/dist/utils/timing.d.mts.map +1 -0
- package/dist/utils/timing.mjs +49 -46
- package/dist/utils/timing.mjs.map +1 -1
- package/dist/webhook/index.cjs +43 -72
- package/dist/webhook/index.cjs.map +1 -1
- package/dist/webhook/index.d.cts +3 -1
- package/dist/webhook/index.d.cts.map +1 -0
- package/dist/webhook/{index.d.ts → index.d.mts} +3 -1
- package/dist/webhook/index.d.mts.map +1 -0
- package/dist/webhook/index.mjs +41 -46
- package/dist/webhook/index.mjs.map +1 -1
- package/package.json +50 -25
- package/dist/cache/index.d.ts +0 -9
- package/dist/error/detail.d.ts +0 -109
- package/dist/error/i18n/en-us.d.ts +0 -3
- package/dist/error/index.d.ts +0 -19
- package/dist/error/reason.cjs.map +0 -1
- package/dist/error/reason.d.ts +0 -5
- package/dist/error/reason.mjs.map +0 -1
- package/dist/error/status.d.ts +0 -69
- package/dist/google-one-tap/index.d.ts +0 -81
- package/dist/google-one-tap/types.cjs.map +0 -1
- package/dist/google-one-tap/types.d.ts +0 -155
- package/dist/google-one-tap/types.mjs.map +0 -1
- package/dist/hono/authorizer.d.ts +0 -18
- package/dist/hono/csrf.d.ts +0 -75
- package/dist/hono/geolocation.d.ts +0 -6
- package/dist/hono/handler.d.ts +0 -31
- package/dist/hono/index.cjs.map +0 -1
- package/dist/hono/index.d.ts +0 -15
- package/dist/hono/index.mjs.map +0 -1
- package/dist/hono/rate-limit.d.ts +0 -35
- package/dist/hono/validator.d.ts +0 -31
- package/dist/index-BhgdyigB.d.cts +0 -129
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts +0 -14
- package/dist/index.mjs.map +0 -1
- package/dist/iso/iso_3601_1.d.ts +0 -9
- package/dist/max-length/index.d.ts +0 -1
- package/dist/polyfills/index.d.ts +0 -2
- package/dist/response.d.ts +0 -83
- package/dist/snowflake.d.ts +0 -29
- package/dist/utils/timing.d.ts +0 -13
package/dist/hono/handler.cjs
CHANGED
|
@@ -1,66 +1,42 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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/hono/handler.ts
|
|
21
|
-
var handler_exports = {};
|
|
22
|
-
__export(handler_exports, {
|
|
23
|
-
errorHandler: () => errorHandler,
|
|
24
|
-
isAxiosError: () => isAxiosError
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(handler_exports);
|
|
27
|
-
var import_detail = require("../error/detail.cjs");
|
|
28
|
-
var import_status = require("../error/status.cjs");
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_error_detail = require("../error/detail.cjs");
|
|
3
|
+
const require_error_status = require("../error/status.cjs");
|
|
4
|
+
//#region src/hono/handler.ts
|
|
29
5
|
function isAxiosError(payload) {
|
|
30
|
-
|
|
6
|
+
return payload !== null && typeof payload === "object" && "isAxiosError" in payload && payload.isAxiosError === true;
|
|
31
7
|
}
|
|
32
8
|
function errorHandler(error, c) {
|
|
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
|
-
|
|
9
|
+
const requestId = c.get("requestId");
|
|
10
|
+
const servingData = `${c.req.method}: ${c.req.path}`;
|
|
11
|
+
const details = require_error_detail.Details.new().requestInfo({
|
|
12
|
+
requestId,
|
|
13
|
+
servingData
|
|
14
|
+
});
|
|
15
|
+
if (error instanceof require_error_status.StatusError) {
|
|
16
|
+
error.body?.error?.details?.push(...details.list);
|
|
17
|
+
const badRequest = error.body?.error?.details.find((d) => d["@type"] === "type.googleapis.com/google.rpc.BadRequest");
|
|
18
|
+
if (badRequest) console.warn(servingData, badRequest);
|
|
19
|
+
console.error(servingData, JSON.stringify(error.body?.error ?? {}, null, 2));
|
|
20
|
+
return c.json(error.body, error.status);
|
|
21
|
+
}
|
|
22
|
+
if (isAxiosError(error)) {
|
|
23
|
+
console.error({
|
|
24
|
+
status: error.status,
|
|
25
|
+
message: error.message,
|
|
26
|
+
request: {
|
|
27
|
+
method: error.config?.method,
|
|
28
|
+
url: error.config?.url,
|
|
29
|
+
data: error.config?.data
|
|
30
|
+
},
|
|
31
|
+
response: { data: error.response?.data }
|
|
32
|
+
});
|
|
33
|
+
return require_error_status.Status.internal("Axios error").response(details);
|
|
34
|
+
}
|
|
35
|
+
console.error(`Unknown error: ${servingData}`, error);
|
|
36
|
+
return require_error_status.Status.internal("Unknown error").response(details);
|
|
60
37
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
});
|
|
38
|
+
//#endregion
|
|
39
|
+
exports.errorHandler = errorHandler;
|
|
40
|
+
exports.isAxiosError = isAxiosError;
|
|
41
|
+
|
|
66
42
|
//# sourceMappingURL=handler.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hono/handler.ts"],"sourcesContent":["import type { Context } from 'hono';\nimport type { RequestIdVariables } from 'hono/request-id';\nimport type { Bindings, HTTPResponseError } from 'hono/types';\nimport type { ContentfulStatusCode } from 'hono/utils/http-status';\nimport { DetailType, Details } from '../error/detail';\nimport { Status, StatusError } from '../error/status';\n\nexport type Env = {\n Variables: RequestIdVariables;\n Bindings?: Bindings;\n};\n\ntype AxiosError = {\n code?: string;\n cause?: unknown;\n status?: number;\n message?: string;\n isAxiosError: boolean;\n response?: {\n data: unknown;\n status: number;\n statusText: string;\n headers: Record<string, string>;\n };\n config?: { url?: string; data?: unknown; method?: string; headers?: Record<string, string> };\n};\n\nexport function isAxiosError(payload: unknown): payload is AxiosError {\n return (\n payload !== null &&\n typeof payload === 'object' &&\n 'isAxiosError' in payload &&\n payload.isAxiosError === true\n );\n}\n\nexport function errorHandler<E extends Env = never>(\n error: Error | HTTPResponseError,\n c: Context<E>\n): Response | Promise<Response> {\n const requestId = c.get('requestId');\n const servingData = `${c.req.method}: ${c.req.path}`;\n const details = Details.new().requestInfo({ requestId, servingData });\n\n if (error instanceof StatusError) {\n error.body?.error?.details?.push(...details.list);\n const badRequest = error.body?.error?.details.find(\n (d) => d['@type'] === DetailType.BAD_REQUEST\n );\n if (badRequest) console.warn(servingData, badRequest);\n console.error(servingData, JSON.stringify(error.body?.error ?? {}, null, 2));\n return c.json(error.body, error.status as ContentfulStatusCode);\n }\n\n if (isAxiosError(error)) {\n console.error({\n status: error.status,\n message: error.message,\n request: {\n method: error.config?.method,\n url: error.config?.url,\n data: error.config?.data,\n },\n response: { data: error.response?.data },\n });\n return Status.internal('Axios error').response(details);\n }\n\n console.error(`Unknown error: ${servingData}`, error);\n return Status.internal('Unknown error').response(details);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"handler.cjs","names":["Details","StatusError","Status"],"sources":["../../src/hono/handler.ts"],"sourcesContent":["import type { Context } from 'hono';\nimport type { RequestIdVariables } from 'hono/request-id';\nimport type { Bindings, HTTPResponseError } from 'hono/types';\nimport type { ContentfulStatusCode } from 'hono/utils/http-status';\nimport { DetailType, Details } from '../error/detail';\nimport { Status, StatusError } from '../error/status';\n\nexport type Env = {\n Variables: RequestIdVariables;\n Bindings?: Bindings;\n};\n\ntype AxiosError = {\n code?: string;\n cause?: unknown;\n status?: number;\n message?: string;\n isAxiosError: boolean;\n response?: {\n data: unknown;\n status: number;\n statusText: string;\n headers: Record<string, string>;\n };\n config?: { url?: string; data?: unknown; method?: string; headers?: Record<string, string> };\n};\n\nexport function isAxiosError(payload: unknown): payload is AxiosError {\n return (\n payload !== null &&\n typeof payload === 'object' &&\n 'isAxiosError' in payload &&\n payload.isAxiosError === true\n );\n}\n\nexport function errorHandler<E extends Env = never>(\n error: Error | HTTPResponseError,\n c: Context<E>\n): Response | Promise<Response> {\n const requestId = c.get('requestId');\n const servingData = `${c.req.method}: ${c.req.path}`;\n const details = Details.new().requestInfo({ requestId, servingData });\n\n if (error instanceof StatusError) {\n error.body?.error?.details?.push(...details.list);\n const badRequest = error.body?.error?.details.find(\n (d) => d['@type'] === DetailType.BAD_REQUEST\n );\n if (badRequest) console.warn(servingData, badRequest);\n console.error(servingData, JSON.stringify(error.body?.error ?? {}, null, 2));\n return c.json(error.body, error.status as ContentfulStatusCode);\n }\n\n if (isAxiosError(error)) {\n console.error({\n status: error.status,\n message: error.message,\n request: {\n method: error.config?.method,\n url: error.config?.url,\n data: error.config?.data,\n },\n response: { data: error.response?.data },\n });\n return Status.internal('Axios error').response(details);\n }\n\n console.error(`Unknown error: ${servingData}`, error);\n return Status.internal('Unknown error').response(details);\n}\n"],"mappings":";;;;AA2BA,SAAgB,aAAa,SAAyC;CACpE,OACE,YAAY,QACZ,OAAO,YAAY,YACnB,kBAAkB,WAClB,QAAQ,iBAAiB;AAE7B;AAEA,SAAgB,aACd,OACA,GAC8B;CAC9B,MAAM,YAAY,EAAE,IAAI,WAAW;CACnC,MAAM,cAAc,GAAG,EAAE,IAAI,OAAO,IAAI,EAAE,IAAI;CAC9C,MAAM,UAAUA,qBAAAA,QAAQ,IAAI,CAAC,CAAC,YAAY;EAAE;EAAW;CAAY,CAAC;CAEpE,IAAI,iBAAiBC,qBAAAA,aAAa;EAChC,MAAM,MAAM,OAAO,SAAS,KAAK,GAAG,QAAQ,IAAI;EAChD,MAAM,aAAa,MAAM,MAAM,OAAO,QAAQ,MAC3C,MAAM,EAAE,aAAA,2CACX;EACA,IAAI,YAAY,QAAQ,KAAK,aAAa,UAAU;EACpD,QAAQ,MAAM,aAAa,KAAK,UAAU,MAAM,MAAM,SAAS,CAAC,GAAG,MAAM,CAAC,CAAC;EAC3E,OAAO,EAAE,KAAK,MAAM,MAAM,MAAM,MAA8B;CAChE;CAEA,IAAI,aAAa,KAAK,GAAG;EACvB,QAAQ,MAAM;GACZ,QAAQ,MAAM;GACd,SAAS,MAAM;GACf,SAAS;IACP,QAAQ,MAAM,QAAQ;IACtB,KAAK,MAAM,QAAQ;IACnB,MAAM,MAAM,QAAQ;GACtB;GACA,UAAU,EAAE,MAAM,MAAM,UAAU,KAAK;EACzC,CAAC;EACD,OAAOC,qBAAAA,OAAO,SAAS,aAAa,CAAC,CAAC,SAAS,OAAO;CACxD;CAEA,QAAQ,MAAM,kBAAkB,eAAe,KAAK;CACpD,OAAOA,qBAAAA,OAAO,SAAS,eAAe,CAAC,CAAC,SAAS,OAAO;AAC1D"}
|
package/dist/hono/handler.d.cts
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
|
-
import { Context } from
|
|
2
|
-
import { RequestIdVariables } from
|
|
3
|
-
import { Bindings, HTTPResponseError } from
|
|
4
|
-
|
|
1
|
+
import { Context } from "hono";
|
|
2
|
+
import { RequestIdVariables } from "hono/request-id";
|
|
3
|
+
import { Bindings, HTTPResponseError } from "hono/types";
|
|
4
|
+
//#region src/hono/handler.d.ts
|
|
5
5
|
type Env = {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
Variables: RequestIdVariables;
|
|
7
|
+
Bindings?: Bindings;
|
|
8
8
|
};
|
|
9
9
|
type AxiosError = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
10
|
+
code?: string;
|
|
11
|
+
cause?: unknown;
|
|
12
|
+
status?: number;
|
|
13
|
+
message?: string;
|
|
14
|
+
isAxiosError: boolean;
|
|
15
|
+
response?: {
|
|
16
|
+
data: unknown;
|
|
17
|
+
status: number;
|
|
18
|
+
statusText: string;
|
|
19
|
+
headers: Record<string, string>;
|
|
20
|
+
};
|
|
21
|
+
config?: {
|
|
22
|
+
url?: string;
|
|
23
|
+
data?: unknown;
|
|
24
|
+
method?: string;
|
|
25
|
+
headers?: Record<string, string>;
|
|
26
|
+
};
|
|
27
27
|
};
|
|
28
28
|
declare function isAxiosError(payload: unknown): payload is AxiosError;
|
|
29
29
|
declare function errorHandler<E extends Env = never>(error: Error | HTTPResponseError, c: Context<E>): Response | Promise<Response>;
|
|
30
|
-
|
|
31
|
-
export {
|
|
30
|
+
//#endregion
|
|
31
|
+
export { Env, errorHandler, isAxiosError };
|
|
32
|
+
//# sourceMappingURL=handler.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.d.cts","names":[],"sources":["../../src/hono/handler.ts"],"mappings":";;;;KAOY;EACV,WAAW;EACX,WAAW;;KAGR;EACH;EACA;EACA;EACA;EACA;EACA;IACE;IACA;IACA;IACA,SAAS;;EAEX;IAAW;IAAc;IAAgB;IAAiB,UAAU;;;iBAGtD,aAAa,mBAAmB,WAAW;iBAS3C,aAAa,UAAU,aACrC,OAAO,QAAQ,mBACf,GAAG,QAAQ,KACV,WAAW,QAAQ"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Context } from "hono";
|
|
2
|
+
import { RequestIdVariables } from "hono/request-id";
|
|
3
|
+
import { Bindings, HTTPResponseError } from "hono/types";
|
|
4
|
+
//#region src/hono/handler.d.ts
|
|
5
|
+
type Env = {
|
|
6
|
+
Variables: RequestIdVariables;
|
|
7
|
+
Bindings?: Bindings;
|
|
8
|
+
};
|
|
9
|
+
type AxiosError = {
|
|
10
|
+
code?: string;
|
|
11
|
+
cause?: unknown;
|
|
12
|
+
status?: number;
|
|
13
|
+
message?: string;
|
|
14
|
+
isAxiosError: boolean;
|
|
15
|
+
response?: {
|
|
16
|
+
data: unknown;
|
|
17
|
+
status: number;
|
|
18
|
+
statusText: string;
|
|
19
|
+
headers: Record<string, string>;
|
|
20
|
+
};
|
|
21
|
+
config?: {
|
|
22
|
+
url?: string;
|
|
23
|
+
data?: unknown;
|
|
24
|
+
method?: string;
|
|
25
|
+
headers?: Record<string, string>;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
declare function isAxiosError(payload: unknown): payload is AxiosError;
|
|
29
|
+
declare function errorHandler<E extends Env = never>(error: Error | HTTPResponseError, c: Context<E>): Response | Promise<Response>;
|
|
30
|
+
//#endregion
|
|
31
|
+
export { Env, errorHandler, isAxiosError };
|
|
32
|
+
//# sourceMappingURL=handler.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.d.mts","names":[],"sources":["../../src/hono/handler.ts"],"mappings":";;;;KAOY;EACV,WAAW;EACX,WAAW;;KAGR;EACH;EACA;EACA;EACA;EACA;EACA;IACE;IACA;IACA;IACA,SAAS;;EAEX;IAAW;IAAc;IAAgB;IAAiB,UAAU;;;iBAGtD,aAAa,mBAAmB,WAAW;iBAS3C,aAAa,UAAU,aACrC,OAAO,QAAQ,mBACf,GAAG,QAAQ,KACV,WAAW,QAAQ"}
|
package/dist/hono/handler.mjs
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
import { DetailType, Details } from "../error/detail.mjs";
|
|
1
|
+
import { Details } from "../error/detail.mjs";
|
|
3
2
|
import { Status, StatusError } from "../error/status.mjs";
|
|
3
|
+
//#region src/hono/handler.ts
|
|
4
4
|
function isAxiosError(payload) {
|
|
5
|
-
|
|
5
|
+
return payload !== null && typeof payload === "object" && "isAxiosError" in payload && payload.isAxiosError === true;
|
|
6
6
|
}
|
|
7
7
|
function errorHandler(error, c) {
|
|
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
|
-
|
|
8
|
+
const requestId = c.get("requestId");
|
|
9
|
+
const servingData = `${c.req.method}: ${c.req.path}`;
|
|
10
|
+
const details = Details.new().requestInfo({
|
|
11
|
+
requestId,
|
|
12
|
+
servingData
|
|
13
|
+
});
|
|
14
|
+
if (error instanceof StatusError) {
|
|
15
|
+
error.body?.error?.details?.push(...details.list);
|
|
16
|
+
const badRequest = error.body?.error?.details.find((d) => d["@type"] === "type.googleapis.com/google.rpc.BadRequest");
|
|
17
|
+
if (badRequest) console.warn(servingData, badRequest);
|
|
18
|
+
console.error(servingData, JSON.stringify(error.body?.error ?? {}, null, 2));
|
|
19
|
+
return c.json(error.body, error.status);
|
|
20
|
+
}
|
|
21
|
+
if (isAxiosError(error)) {
|
|
22
|
+
console.error({
|
|
23
|
+
status: error.status,
|
|
24
|
+
message: error.message,
|
|
25
|
+
request: {
|
|
26
|
+
method: error.config?.method,
|
|
27
|
+
url: error.config?.url,
|
|
28
|
+
data: error.config?.data
|
|
29
|
+
},
|
|
30
|
+
response: { data: error.response?.data }
|
|
31
|
+
});
|
|
32
|
+
return Status.internal("Axios error").response(details);
|
|
33
|
+
}
|
|
34
|
+
console.error(`Unknown error: ${servingData}`, error);
|
|
35
|
+
return Status.internal("Unknown error").response(details);
|
|
35
36
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
};
|
|
37
|
+
//#endregion
|
|
38
|
+
export { errorHandler, isAxiosError };
|
|
39
|
+
|
|
40
40
|
//# sourceMappingURL=handler.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hono/handler.ts"],"sourcesContent":["import type { Context } from 'hono';\nimport type { RequestIdVariables } from 'hono/request-id';\nimport type { Bindings, HTTPResponseError } from 'hono/types';\nimport type { ContentfulStatusCode } from 'hono/utils/http-status';\nimport { DetailType, Details } from '../error/detail';\nimport { Status, StatusError } from '../error/status';\n\nexport type Env = {\n Variables: RequestIdVariables;\n Bindings?: Bindings;\n};\n\ntype AxiosError = {\n code?: string;\n cause?: unknown;\n status?: number;\n message?: string;\n isAxiosError: boolean;\n response?: {\n data: unknown;\n status: number;\n statusText: string;\n headers: Record<string, string>;\n };\n config?: { url?: string; data?: unknown; method?: string; headers?: Record<string, string> };\n};\n\nexport function isAxiosError(payload: unknown): payload is AxiosError {\n return (\n payload !== null &&\n typeof payload === 'object' &&\n 'isAxiosError' in payload &&\n payload.isAxiosError === true\n );\n}\n\nexport function errorHandler<E extends Env = never>(\n error: Error | HTTPResponseError,\n c: Context<E>\n): Response | Promise<Response> {\n const requestId = c.get('requestId');\n const servingData = `${c.req.method}: ${c.req.path}`;\n const details = Details.new().requestInfo({ requestId, servingData });\n\n if (error instanceof StatusError) {\n error.body?.error?.details?.push(...details.list);\n const badRequest = error.body?.error?.details.find(\n (d) => d['@type'] === DetailType.BAD_REQUEST\n );\n if (badRequest) console.warn(servingData, badRequest);\n console.error(servingData, JSON.stringify(error.body?.error ?? {}, null, 2));\n return c.json(error.body, error.status as ContentfulStatusCode);\n }\n\n if (isAxiosError(error)) {\n console.error({\n status: error.status,\n message: error.message,\n request: {\n method: error.config?.method,\n url: error.config?.url,\n data: error.config?.data,\n },\n response: { data: error.response?.data },\n });\n return Status.internal('Axios error').response(details);\n }\n\n console.error(`Unknown error: ${servingData}`, error);\n return Status.internal('Unknown error').response(details);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"handler.mjs","names":[],"sources":["../../src/hono/handler.ts"],"sourcesContent":["import type { Context } from 'hono';\nimport type { RequestIdVariables } from 'hono/request-id';\nimport type { Bindings, HTTPResponseError } from 'hono/types';\nimport type { ContentfulStatusCode } from 'hono/utils/http-status';\nimport { DetailType, Details } from '../error/detail';\nimport { Status, StatusError } from '../error/status';\n\nexport type Env = {\n Variables: RequestIdVariables;\n Bindings?: Bindings;\n};\n\ntype AxiosError = {\n code?: string;\n cause?: unknown;\n status?: number;\n message?: string;\n isAxiosError: boolean;\n response?: {\n data: unknown;\n status: number;\n statusText: string;\n headers: Record<string, string>;\n };\n config?: { url?: string; data?: unknown; method?: string; headers?: Record<string, string> };\n};\n\nexport function isAxiosError(payload: unknown): payload is AxiosError {\n return (\n payload !== null &&\n typeof payload === 'object' &&\n 'isAxiosError' in payload &&\n payload.isAxiosError === true\n );\n}\n\nexport function errorHandler<E extends Env = never>(\n error: Error | HTTPResponseError,\n c: Context<E>\n): Response | Promise<Response> {\n const requestId = c.get('requestId');\n const servingData = `${c.req.method}: ${c.req.path}`;\n const details = Details.new().requestInfo({ requestId, servingData });\n\n if (error instanceof StatusError) {\n error.body?.error?.details?.push(...details.list);\n const badRequest = error.body?.error?.details.find(\n (d) => d['@type'] === DetailType.BAD_REQUEST\n );\n if (badRequest) console.warn(servingData, badRequest);\n console.error(servingData, JSON.stringify(error.body?.error ?? {}, null, 2));\n return c.json(error.body, error.status as ContentfulStatusCode);\n }\n\n if (isAxiosError(error)) {\n console.error({\n status: error.status,\n message: error.message,\n request: {\n method: error.config?.method,\n url: error.config?.url,\n data: error.config?.data,\n },\n response: { data: error.response?.data },\n });\n return Status.internal('Axios error').response(details);\n }\n\n console.error(`Unknown error: ${servingData}`, error);\n return Status.internal('Unknown error').response(details);\n}\n"],"mappings":";;;AA2BA,SAAgB,aAAa,SAAyC;CACpE,OACE,YAAY,QACZ,OAAO,YAAY,YACnB,kBAAkB,WAClB,QAAQ,iBAAiB;AAE7B;AAEA,SAAgB,aACd,OACA,GAC8B;CAC9B,MAAM,YAAY,EAAE,IAAI,WAAW;CACnC,MAAM,cAAc,GAAG,EAAE,IAAI,OAAO,IAAI,EAAE,IAAI;CAC9C,MAAM,UAAU,QAAQ,IAAI,CAAC,CAAC,YAAY;EAAE;EAAW;CAAY,CAAC;CAEpE,IAAI,iBAAiB,aAAa;EAChC,MAAM,MAAM,OAAO,SAAS,KAAK,GAAG,QAAQ,IAAI;EAChD,MAAM,aAAa,MAAM,MAAM,OAAO,QAAQ,MAC3C,MAAM,EAAE,aAAA,2CACX;EACA,IAAI,YAAY,QAAQ,KAAK,aAAa,UAAU;EACpD,QAAQ,MAAM,aAAa,KAAK,UAAU,MAAM,MAAM,SAAS,CAAC,GAAG,MAAM,CAAC,CAAC;EAC3E,OAAO,EAAE,KAAK,MAAM,MAAM,MAAM,MAA8B;CAChE;CAEA,IAAI,aAAa,KAAK,GAAG;EACvB,QAAQ,MAAM;GACZ,QAAQ,MAAM;GACd,SAAS,MAAM;GACf,SAAS;IACP,QAAQ,MAAM,QAAQ;IACtB,KAAK,MAAM,QAAQ;IACnB,MAAM,MAAM,QAAQ;GACtB;GACA,UAAU,EAAE,MAAM,MAAM,UAAU,KAAK;EACzC,CAAC;EACD,OAAO,OAAO,SAAS,aAAa,CAAC,CAAC,SAAS,OAAO;CACxD;CAEA,QAAQ,MAAM,kBAAkB,eAAe,KAAK;CACpD,OAAO,OAAO,SAAS,eAAe,CAAC,CAAC,SAAS,OAAO;AAC1D"}
|
package/dist/hono/index.cjs
CHANGED
|
@@ -1,48 +1,14 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/hono/index.ts
|
|
21
|
-
var hono_exports = {};
|
|
22
|
-
__export(hono_exports, {
|
|
23
|
-
authorizer: () => import_authorizer.authorizer,
|
|
24
|
-
bigintId: () => import_validator.bigintId,
|
|
25
|
-
csrf: () => import_csrf.csrf,
|
|
26
|
-
errorHandler: () => import_handler.errorHandler,
|
|
27
|
-
geolocation: () => import_geolocation.geolocation,
|
|
28
|
-
rateLimit: () => import_rate_limit.rateLimit,
|
|
29
|
-
zValidator: () => import_validator.zValidator
|
|
30
|
-
});
|
|
31
|
-
module.exports = __toCommonJS(hono_exports);
|
|
32
|
-
var import_validator = require("./validator.cjs");
|
|
33
|
-
var import_handler = require("./handler.cjs");
|
|
34
|
-
var import_geolocation = require("./geolocation.cjs");
|
|
35
|
-
var import_authorizer = require("./authorizer.cjs");
|
|
36
|
-
var import_csrf = require("./csrf.cjs");
|
|
37
|
-
var import_rate_limit = require("./rate-limit.cjs");
|
|
38
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
39
|
-
0 && (module.exports = {
|
|
40
|
-
authorizer,
|
|
41
|
-
bigintId,
|
|
42
|
-
csrf,
|
|
43
|
-
errorHandler,
|
|
44
|
-
geolocation,
|
|
45
|
-
rateLimit,
|
|
46
|
-
zValidator
|
|
47
|
-
});
|
|
48
|
-
//# sourceMappingURL=index.cjs.map
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_hono_authorizer = require("./authorizer.cjs");
|
|
3
|
+
const require_hono_csrf = require("./csrf.cjs");
|
|
4
|
+
const require_hono_geolocation = require("./geolocation.cjs");
|
|
5
|
+
const require_hono_handler = require("./handler.cjs");
|
|
6
|
+
const require_hono_validator = require("./validator.cjs");
|
|
7
|
+
const require_hono_rate_limit = require("./rate-limit.cjs");
|
|
8
|
+
exports.authorizer = require_hono_authorizer.authorizer;
|
|
9
|
+
exports.bigintId = require_hono_validator.bigintId;
|
|
10
|
+
exports.csrf = require_hono_csrf.csrf;
|
|
11
|
+
exports.errorHandler = require_hono_handler.errorHandler;
|
|
12
|
+
exports.geolocation = require_hono_geolocation.geolocation;
|
|
13
|
+
exports.rateLimit = require_hono_rate_limit.rateLimit;
|
|
14
|
+
exports.zValidator = require_hono_validator.zValidator;
|
package/dist/hono/index.d.cts
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import 'hono/utils/http-status';
|
|
9
|
-
import 'hono/validator';
|
|
10
|
-
import 'hono/types';
|
|
11
|
-
import 'hono';
|
|
12
|
-
import 'zod/v4/core';
|
|
13
|
-
import 'zod';
|
|
14
|
-
import 'hono/request-id';
|
|
15
|
-
import '../utils/geolocation.cjs';
|
|
1
|
+
import { AuthorizerConfig, authorizer } from "./authorizer.cjs";
|
|
2
|
+
import { CSRFConfig, CSRFIgnoreRule, csrf } from "./csrf.cjs";
|
|
3
|
+
import { geolocation } from "./geolocation.cjs";
|
|
4
|
+
import { errorHandler } from "./handler.cjs";
|
|
5
|
+
import { bigintId, zValidator } from "./validator.cjs";
|
|
6
|
+
import { RateLimitOptions, rateLimit } from "./rate-limit.cjs";
|
|
7
|
+
export { type AuthorizerConfig, type CSRFConfig, type CSRFIgnoreRule, type RateLimitOptions, authorizer, bigintId, csrf, errorHandler, geolocation, rateLimit, zValidator };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AuthorizerConfig, authorizer } from "./authorizer.mjs";
|
|
2
|
+
import { CSRFConfig, CSRFIgnoreRule, csrf } from "./csrf.mjs";
|
|
3
|
+
import { geolocation } from "./geolocation.mjs";
|
|
4
|
+
import { errorHandler } from "./handler.mjs";
|
|
5
|
+
import { bigintId, zValidator } from "./validator.mjs";
|
|
6
|
+
import { RateLimitOptions, rateLimit } from "./rate-limit.mjs";
|
|
7
|
+
export { type AuthorizerConfig, type CSRFConfig, type CSRFIgnoreRule, type RateLimitOptions, authorizer, bigintId, csrf, errorHandler, geolocation, rateLimit, zValidator };
|
package/dist/hono/index.mjs
CHANGED
|
@@ -1,17 +1,7 @@
|
|
|
1
|
-
// src/hono/index.ts
|
|
2
|
-
import { zValidator, bigintId } from "./validator.mjs";
|
|
3
|
-
import { errorHandler } from "./handler.mjs";
|
|
4
|
-
import { geolocation } from "./geolocation.mjs";
|
|
5
1
|
import { authorizer } from "./authorizer.mjs";
|
|
6
2
|
import { csrf } from "./csrf.mjs";
|
|
3
|
+
import { geolocation } from "./geolocation.mjs";
|
|
4
|
+
import { errorHandler } from "./handler.mjs";
|
|
5
|
+
import { bigintId, zValidator } from "./validator.mjs";
|
|
7
6
|
import { rateLimit } from "./rate-limit.mjs";
|
|
8
|
-
export {
|
|
9
|
-
authorizer,
|
|
10
|
-
bigintId,
|
|
11
|
-
csrf,
|
|
12
|
-
errorHandler,
|
|
13
|
-
geolocation,
|
|
14
|
-
rateLimit,
|
|
15
|
-
zValidator
|
|
16
|
-
};
|
|
17
|
-
//# sourceMappingURL=index.mjs.map
|
|
7
|
+
export { authorizer, bigintId, csrf, errorHandler, geolocation, rateLimit, zValidator };
|
package/dist/hono/rate-limit.cjs
CHANGED
|
@@ -1,121 +1,76 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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/hono/rate-limit.ts
|
|
21
|
-
var rate_limit_exports = {};
|
|
22
|
-
__export(rate_limit_exports, {
|
|
23
|
-
checkRateLimit: () => checkRateLimit,
|
|
24
|
-
rateLimit: () => rateLimit
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(rate_limit_exports);
|
|
27
|
-
var import_detail = require("../error/detail.cjs");
|
|
28
|
-
var import_status = require("../error/status.cjs");
|
|
29
|
-
var import_geolocation = require("./geolocation.cjs");
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_error_detail = require("../error/detail.cjs");
|
|
3
|
+
const require_error_status = require("../error/status.cjs");
|
|
4
|
+
const require_hono_geolocation = require("./geolocation.cjs");
|
|
5
|
+
//#region src/hono/rate-limit.ts
|
|
30
6
|
function calculateTokens(state, capacity, rate, now) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
};
|
|
7
|
+
if (!state) return {
|
|
8
|
+
tokens: capacity,
|
|
9
|
+
lastRefillTime: now
|
|
10
|
+
};
|
|
11
|
+
const tokensToAdd = (now - state.lastRefillTime) / 1e3 * rate;
|
|
12
|
+
return {
|
|
13
|
+
tokens: Math.min(capacity, state.tokens + tokensToAdd),
|
|
14
|
+
lastRefillTime: now
|
|
15
|
+
};
|
|
41
16
|
}
|
|
42
17
|
function calculateResetTime(currentTokens, capacity, rate) {
|
|
43
|
-
|
|
44
|
-
|
|
18
|
+
if (currentTokens >= capacity) return 0;
|
|
19
|
+
return Math.ceil((capacity - currentTokens) / rate);
|
|
45
20
|
}
|
|
46
21
|
function calculateRetryAfter(currentTokens, requested, rate) {
|
|
47
|
-
|
|
48
|
-
|
|
22
|
+
if (currentTokens >= requested) return 0;
|
|
23
|
+
return Math.ceil((requested - currentTokens) / rate);
|
|
49
24
|
}
|
|
50
25
|
async function checkRateLimit(kv, identifier, options) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
reset: calculateResetTime(newTokens, capacity, rate)
|
|
72
|
-
};
|
|
73
|
-
if (!allowed) {
|
|
74
|
-
result.retryAfter = calculateRetryAfter(currentTokens, requested, rate);
|
|
75
|
-
}
|
|
76
|
-
return result;
|
|
26
|
+
const { rate, capacity, requested = 1, keyPrefix = "rate-limit:", expiresIn = Math.max(3600, Math.ceil(capacity / rate * 2)) } = options;
|
|
27
|
+
const key = `${keyPrefix}${identifier}`;
|
|
28
|
+
const now = Date.now();
|
|
29
|
+
const stateStr = await kv.getItem(key);
|
|
30
|
+
const { tokens: currentTokens, lastRefillTime } = calculateTokens(stateStr ? JSON.parse(stateStr) : null, capacity, rate, now);
|
|
31
|
+
const allowed = currentTokens >= requested;
|
|
32
|
+
const newTokens = allowed ? currentTokens - requested : currentTokens;
|
|
33
|
+
const newState = {
|
|
34
|
+
tokens: newTokens,
|
|
35
|
+
lastRefillTime
|
|
36
|
+
};
|
|
37
|
+
await kv.setItem(key, JSON.stringify(newState), expiresIn);
|
|
38
|
+
const result = {
|
|
39
|
+
allowed,
|
|
40
|
+
remaining: Math.floor(Math.max(0, newTokens)),
|
|
41
|
+
limit: capacity,
|
|
42
|
+
reset: calculateResetTime(newTokens, capacity, rate)
|
|
43
|
+
};
|
|
44
|
+
if (!allowed) result.retryAfter = calculateRetryAfter(currentTokens, requested, rate);
|
|
45
|
+
return result;
|
|
77
46
|
}
|
|
78
47
|
function rateLimit(options) {
|
|
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
|
-
c.header("X-RateLimit-Limit", String(result.limit));
|
|
104
|
-
c.header("X-RateLimit-Remaining", String(result.remaining));
|
|
105
|
-
c.header("X-RateLimit-Reset", String(result.reset));
|
|
106
|
-
if (!result.allowed) {
|
|
107
|
-
c.header("Retry-After", String(result.retryAfter ?? 1));
|
|
108
|
-
if (onRateLimited) return onRateLimited(c, result.retryAfter ?? 1);
|
|
109
|
-
const details = import_detail.Details.new().errorInfo({ reason: "RATE_LIMIT_EXCEEDED" }).retryInfo({ retryDelay: result.retryAfter ?? 1 });
|
|
110
|
-
const message = "Rate limit exceeded. Please try again later.";
|
|
111
|
-
return import_status.Status.resourceExhausted(message).response(details);
|
|
112
|
-
}
|
|
113
|
-
return next();
|
|
114
|
-
};
|
|
48
|
+
const { kv, rate, capacity, requested = 1, keyPrefix = "rate-limit:", expiresIn = Math.max(3600, Math.ceil(capacity / rate * 2)), getIdentifier, onRateLimited, skip } = options;
|
|
49
|
+
return async (c, next) => {
|
|
50
|
+
if (skip && await skip(c)) return next();
|
|
51
|
+
const identifier = getIdentifier ? getIdentifier(c) : require_hono_geolocation.geolocation(c).ip_address;
|
|
52
|
+
if (!identifier) return next();
|
|
53
|
+
const result = await checkRateLimit(kv, identifier, {
|
|
54
|
+
rate,
|
|
55
|
+
capacity,
|
|
56
|
+
requested,
|
|
57
|
+
keyPrefix,
|
|
58
|
+
expiresIn
|
|
59
|
+
});
|
|
60
|
+
c.header("X-RateLimit-Limit", String(result.limit));
|
|
61
|
+
c.header("X-RateLimit-Remaining", String(result.remaining));
|
|
62
|
+
c.header("X-RateLimit-Reset", String(result.reset));
|
|
63
|
+
if (!result.allowed) {
|
|
64
|
+
c.header("Retry-After", String(result.retryAfter ?? 1));
|
|
65
|
+
if (onRateLimited) return onRateLimited(c, result.retryAfter ?? 1);
|
|
66
|
+
const details = require_error_detail.Details.new().errorInfo({ reason: "RATE_LIMIT_EXCEEDED" }).retryInfo({ retryDelay: result.retryAfter ?? 1 });
|
|
67
|
+
return require_error_status.Status.resourceExhausted("Rate limit exceeded. Please try again later.").response(details);
|
|
68
|
+
}
|
|
69
|
+
return next();
|
|
70
|
+
};
|
|
115
71
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
});
|
|
72
|
+
//#endregion
|
|
73
|
+
exports.checkRateLimit = checkRateLimit;
|
|
74
|
+
exports.rateLimit = rateLimit;
|
|
75
|
+
|
|
121
76
|
//# sourceMappingURL=rate-limit.cjs.map
|