hono-utils 0.3.8 → 0.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +43 -47
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -61
- package/dist/index.d.ts +16 -61
- package/dist/index.js +42 -46
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as hono from 'hono';
|
|
2
|
-
import {
|
|
2
|
+
import { MiddlewareHandler, Context, Env, ErrorHandler, NotFoundHandler, Hono } from 'hono';
|
|
3
3
|
import { Logger, Details } from 'hierarchical-area-logger';
|
|
4
4
|
import * as hono_utils_http_status from 'hono/utils/http-status';
|
|
5
5
|
import { ContentfulStatusCode, SuccessStatusCode, ClientErrorStatusCode, StatusCode } from 'hono/utils/http-status';
|
|
@@ -36,29 +36,6 @@ declare const logger: (details: Details, parentEventIdHeader?: string) => hono.M
|
|
|
36
36
|
Variables: HonoLoggerVariables;
|
|
37
37
|
}, string, {}, Response>;
|
|
38
38
|
|
|
39
|
-
type WrappableMiddleware<V extends Env> = (c: Context<V>, loggerFunctions: ReturnType<Logger['getArea']> & Pick<Logger, 'eventId' | 'parentEventId' | 'dump'>) => Promise<void>;
|
|
40
|
-
/**
|
|
41
|
-
* Wraps middleware with logger. Logger should be initialized before middleware.
|
|
42
|
-
*
|
|
43
|
-
* @param name - Name of the middleware
|
|
44
|
-
* @param middleware - Middleware function
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```ts
|
|
48
|
-
* app.use('*', logger(
|
|
49
|
-
* {
|
|
50
|
-
* service: 'logger',
|
|
51
|
-
* },
|
|
52
|
-
* parentEventIdHeader: 'parent-event-id',
|
|
53
|
-
* ));
|
|
54
|
-
*
|
|
55
|
-
* app.use('*', withLogger('middlewareName', (c, { info }) => {
|
|
56
|
-
* info('Middleware initialized');
|
|
57
|
-
* }));
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
declare const withLogger: <V extends Env>(name: string, middleware: WrappableMiddleware<V>) => hono.MiddlewareHandler<any, string, {}, Response>;
|
|
61
|
-
|
|
62
39
|
/**
|
|
63
40
|
* @description
|
|
64
41
|
* Validator middleware for JSON data.
|
|
@@ -85,6 +62,13 @@ declare const jsonValidator: <T extends z.ZodRawShape>(schema: z.ZodObject<T>) =
|
|
|
85
62
|
};
|
|
86
63
|
}, z.core.$InferObjectOutput<T, {}> extends infer T_3 ? T_3 extends z.core.$InferObjectOutput<T, {}> ? T_3 extends Promise<infer PR_1> ? PR_1 extends hono.TypedResponse<infer T_4, infer S extends hono_utils_http_status.StatusCode, infer F extends string> ? hono.TypedResponse<T_4, S, F> : PR_1 extends Response ? PR_1 : PR_1 extends undefined ? never : never : T_3 extends hono.TypedResponse<infer T_5, infer S_1 extends hono_utils_http_status.StatusCode, infer F_1 extends string> ? hono.TypedResponse<T_5, S_1, F_1> : T_3 extends Response ? T_3 : T_3 extends undefined ? never : never : never : never>;
|
|
87
64
|
|
|
65
|
+
type MiddlewareWithLoggingCapabilityProps<RestProps extends Record<string, unknown>> = {
|
|
66
|
+
useLogger?: boolean;
|
|
67
|
+
} & RestProps;
|
|
68
|
+
interface MiddlewareWithLoggingCapability<RestProps extends Record<string, unknown>> {
|
|
69
|
+
(props?: MiddlewareWithLoggingCapabilityProps<RestProps>): MiddlewareHandler;
|
|
70
|
+
}
|
|
71
|
+
|
|
88
72
|
type HonoIsBotVariables = {
|
|
89
73
|
isBot: boolean;
|
|
90
74
|
};
|
|
@@ -107,12 +91,10 @@ type HonoIsBotVariables = {
|
|
|
107
91
|
* app.use('/api/*', isBot({ threshold: 50, allowVerifiedBot: true }));
|
|
108
92
|
* ```
|
|
109
93
|
*/
|
|
110
|
-
declare const isBot:
|
|
94
|
+
declare const isBot: MiddlewareWithLoggingCapability<{
|
|
111
95
|
threshold: number;
|
|
112
96
|
allowVerifiedBot?: boolean;
|
|
113
|
-
}
|
|
114
|
-
Variables: HonoIsBotVariables;
|
|
115
|
-
}, string, {}, Response>;
|
|
97
|
+
}>;
|
|
116
98
|
|
|
117
99
|
type HonoLanguageVariables = {
|
|
118
100
|
language: string;
|
|
@@ -384,7 +366,9 @@ type HonoClientInfoVariables = {
|
|
|
384
366
|
* ```ts
|
|
385
367
|
* const app = new Hono<{ Variables: HonoClientInfoVariables }>();
|
|
386
368
|
*
|
|
387
|
-
* app.use('*', clientInfo(
|
|
369
|
+
* app.use('*', clientInfo({
|
|
370
|
+
* useLogger: true // for debugging purposes
|
|
371
|
+
* }));
|
|
388
372
|
*
|
|
389
373
|
* app.get('/me', (c) => {
|
|
390
374
|
* const client = c.get('client');
|
|
@@ -395,41 +379,12 @@ type HonoClientInfoVariables = {
|
|
|
395
379
|
* });
|
|
396
380
|
* ```
|
|
397
381
|
*/
|
|
398
|
-
declare const clientInfo:
|
|
382
|
+
declare const clientInfo: MiddlewareWithLoggingCapability<{
|
|
399
383
|
hashSecretBinding?: string;
|
|
400
384
|
securityHashString?: (content: CFData & UserAgentData & {
|
|
401
385
|
ip: string;
|
|
402
386
|
}) => string;
|
|
403
|
-
}
|
|
404
|
-
Bindings: Record<string, string>;
|
|
405
|
-
Variables: HonoClientInfoVariables;
|
|
406
|
-
}, string, {}, Response>;
|
|
407
|
-
|
|
408
|
-
/**
|
|
409
|
-
* A Hono middleware factory that hydrates a context variable based on environment bindings or other variables.
|
|
410
|
-
* @template {Record<string, unknown>} Bindings - The environment bindings (e.g., Cloudflare Env).
|
|
411
|
-
* @template {Record<string, unknown>} Variables - The Hono context variables.
|
|
412
|
-
* @template HidratedResult - The resulting type of the hydrated variable.
|
|
413
|
-
* @param {Object} options - The configuration options for hydration.
|
|
414
|
-
* @param {keyof Variables} options.variableName - The name of the variable in the context to be set.
|
|
415
|
-
* @param {(c: Bindings & { get: <T>(key: keyof Variables) => T }) => HidratedResult} options.hydrate -
|
|
416
|
-
* A function that takes the environment and a getter, returning the hydrated value.
|
|
417
|
-
* @returns {import("hono").MiddlewareHandler} A Hono middleware that performs the hydration and calls next().
|
|
418
|
-
* @example
|
|
419
|
-
* const middleware = hydrateVariable({
|
|
420
|
-
* variableName: 'userProfile',
|
|
421
|
-
* hydrate: (c) => new UserProfile(c.USER_ID)
|
|
422
|
-
* });
|
|
423
|
-
*/
|
|
424
|
-
declare const hydrateVariable: <Environment extends Env, HidratedResult>({ variableName, hydrate, }: {
|
|
425
|
-
variableName: keyof Environment["Variables"];
|
|
426
|
-
hydrate: (c: Environment["Bindings"] & {
|
|
427
|
-
get: <T>(key: keyof Environment["Variables"]) => T;
|
|
428
|
-
}) => HidratedResult;
|
|
429
|
-
}) => hono.MiddlewareHandler<{
|
|
430
|
-
Bindings: Environment["Bindings"];
|
|
431
|
-
Variables: Record<string, HidratedResult>;
|
|
432
|
-
}, string, {}, Response>;
|
|
387
|
+
}>;
|
|
433
388
|
|
|
434
389
|
/**
|
|
435
390
|
* Derives a secure hex-encoded hash from an input string using PBKDF2-HMAC-SHA256.
|
|
@@ -501,4 +456,4 @@ interface CreateTypedClientOptions {
|
|
|
501
456
|
}
|
|
502
457
|
declare const createTypedClient: <TApp extends Hono<any, any, any>>() => (options: CreateTypedClientOptions) => <TSuccessData>(fn: (c: ReturnType<typeof hc<TApp>>) => Promise<ClientResponse<TSuccessData>>, callbacks?: TypedClientCallbacks) => Promise<TSuccessData>;
|
|
503
458
|
|
|
504
|
-
export { type ContextFn, type CreateTypedClientOptions, type HonoClientInfoVariables, type HonoI18nVariables, type HonoIsBotVariables, type HonoLanguageVariables, type HonoLoggerVariables, type HonoResponseVariables, type MessageHandlers, pbkdf2 as PBKDF2, QueueHandler, sha as SHA, type TypedClientCallbacks,
|
|
459
|
+
export { type ContextFn, type CreateTypedClientOptions, type HonoClientInfoVariables, type HonoI18nVariables, type HonoIsBotVariables, type HonoLanguageVariables, type HonoLoggerVariables, type HonoResponseVariables, type MessageHandlers, type MiddlewareWithLoggingCapability, pbkdf2 as PBKDF2, QueueHandler, sha as SHA, type TypedClientCallbacks, clientInfo, createTypedClient, i18n, isBot, jsonValidator, logger, onError, onNotFound, queue, response };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as hono from 'hono';
|
|
2
|
-
import {
|
|
2
|
+
import { MiddlewareHandler, Context, Env, ErrorHandler, NotFoundHandler, Hono } from 'hono';
|
|
3
3
|
import { Logger, Details } from 'hierarchical-area-logger';
|
|
4
4
|
import * as hono_utils_http_status from 'hono/utils/http-status';
|
|
5
5
|
import { ContentfulStatusCode, SuccessStatusCode, ClientErrorStatusCode, StatusCode } from 'hono/utils/http-status';
|
|
@@ -36,29 +36,6 @@ declare const logger: (details: Details, parentEventIdHeader?: string) => hono.M
|
|
|
36
36
|
Variables: HonoLoggerVariables;
|
|
37
37
|
}, string, {}, Response>;
|
|
38
38
|
|
|
39
|
-
type WrappableMiddleware<V extends Env> = (c: Context<V>, loggerFunctions: ReturnType<Logger['getArea']> & Pick<Logger, 'eventId' | 'parentEventId' | 'dump'>) => Promise<void>;
|
|
40
|
-
/**
|
|
41
|
-
* Wraps middleware with logger. Logger should be initialized before middleware.
|
|
42
|
-
*
|
|
43
|
-
* @param name - Name of the middleware
|
|
44
|
-
* @param middleware - Middleware function
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```ts
|
|
48
|
-
* app.use('*', logger(
|
|
49
|
-
* {
|
|
50
|
-
* service: 'logger',
|
|
51
|
-
* },
|
|
52
|
-
* parentEventIdHeader: 'parent-event-id',
|
|
53
|
-
* ));
|
|
54
|
-
*
|
|
55
|
-
* app.use('*', withLogger('middlewareName', (c, { info }) => {
|
|
56
|
-
* info('Middleware initialized');
|
|
57
|
-
* }));
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
declare const withLogger: <V extends Env>(name: string, middleware: WrappableMiddleware<V>) => hono.MiddlewareHandler<any, string, {}, Response>;
|
|
61
|
-
|
|
62
39
|
/**
|
|
63
40
|
* @description
|
|
64
41
|
* Validator middleware for JSON data.
|
|
@@ -85,6 +62,13 @@ declare const jsonValidator: <T extends z.ZodRawShape>(schema: z.ZodObject<T>) =
|
|
|
85
62
|
};
|
|
86
63
|
}, z.core.$InferObjectOutput<T, {}> extends infer T_3 ? T_3 extends z.core.$InferObjectOutput<T, {}> ? T_3 extends Promise<infer PR_1> ? PR_1 extends hono.TypedResponse<infer T_4, infer S extends hono_utils_http_status.StatusCode, infer F extends string> ? hono.TypedResponse<T_4, S, F> : PR_1 extends Response ? PR_1 : PR_1 extends undefined ? never : never : T_3 extends hono.TypedResponse<infer T_5, infer S_1 extends hono_utils_http_status.StatusCode, infer F_1 extends string> ? hono.TypedResponse<T_5, S_1, F_1> : T_3 extends Response ? T_3 : T_3 extends undefined ? never : never : never : never>;
|
|
87
64
|
|
|
65
|
+
type MiddlewareWithLoggingCapabilityProps<RestProps extends Record<string, unknown>> = {
|
|
66
|
+
useLogger?: boolean;
|
|
67
|
+
} & RestProps;
|
|
68
|
+
interface MiddlewareWithLoggingCapability<RestProps extends Record<string, unknown>> {
|
|
69
|
+
(props?: MiddlewareWithLoggingCapabilityProps<RestProps>): MiddlewareHandler;
|
|
70
|
+
}
|
|
71
|
+
|
|
88
72
|
type HonoIsBotVariables = {
|
|
89
73
|
isBot: boolean;
|
|
90
74
|
};
|
|
@@ -107,12 +91,10 @@ type HonoIsBotVariables = {
|
|
|
107
91
|
* app.use('/api/*', isBot({ threshold: 50, allowVerifiedBot: true }));
|
|
108
92
|
* ```
|
|
109
93
|
*/
|
|
110
|
-
declare const isBot:
|
|
94
|
+
declare const isBot: MiddlewareWithLoggingCapability<{
|
|
111
95
|
threshold: number;
|
|
112
96
|
allowVerifiedBot?: boolean;
|
|
113
|
-
}
|
|
114
|
-
Variables: HonoIsBotVariables;
|
|
115
|
-
}, string, {}, Response>;
|
|
97
|
+
}>;
|
|
116
98
|
|
|
117
99
|
type HonoLanguageVariables = {
|
|
118
100
|
language: string;
|
|
@@ -384,7 +366,9 @@ type HonoClientInfoVariables = {
|
|
|
384
366
|
* ```ts
|
|
385
367
|
* const app = new Hono<{ Variables: HonoClientInfoVariables }>();
|
|
386
368
|
*
|
|
387
|
-
* app.use('*', clientInfo(
|
|
369
|
+
* app.use('*', clientInfo({
|
|
370
|
+
* useLogger: true // for debugging purposes
|
|
371
|
+
* }));
|
|
388
372
|
*
|
|
389
373
|
* app.get('/me', (c) => {
|
|
390
374
|
* const client = c.get('client');
|
|
@@ -395,41 +379,12 @@ type HonoClientInfoVariables = {
|
|
|
395
379
|
* });
|
|
396
380
|
* ```
|
|
397
381
|
*/
|
|
398
|
-
declare const clientInfo:
|
|
382
|
+
declare const clientInfo: MiddlewareWithLoggingCapability<{
|
|
399
383
|
hashSecretBinding?: string;
|
|
400
384
|
securityHashString?: (content: CFData & UserAgentData & {
|
|
401
385
|
ip: string;
|
|
402
386
|
}) => string;
|
|
403
|
-
}
|
|
404
|
-
Bindings: Record<string, string>;
|
|
405
|
-
Variables: HonoClientInfoVariables;
|
|
406
|
-
}, string, {}, Response>;
|
|
407
|
-
|
|
408
|
-
/**
|
|
409
|
-
* A Hono middleware factory that hydrates a context variable based on environment bindings or other variables.
|
|
410
|
-
* @template {Record<string, unknown>} Bindings - The environment bindings (e.g., Cloudflare Env).
|
|
411
|
-
* @template {Record<string, unknown>} Variables - The Hono context variables.
|
|
412
|
-
* @template HidratedResult - The resulting type of the hydrated variable.
|
|
413
|
-
* @param {Object} options - The configuration options for hydration.
|
|
414
|
-
* @param {keyof Variables} options.variableName - The name of the variable in the context to be set.
|
|
415
|
-
* @param {(c: Bindings & { get: <T>(key: keyof Variables) => T }) => HidratedResult} options.hydrate -
|
|
416
|
-
* A function that takes the environment and a getter, returning the hydrated value.
|
|
417
|
-
* @returns {import("hono").MiddlewareHandler} A Hono middleware that performs the hydration and calls next().
|
|
418
|
-
* @example
|
|
419
|
-
* const middleware = hydrateVariable({
|
|
420
|
-
* variableName: 'userProfile',
|
|
421
|
-
* hydrate: (c) => new UserProfile(c.USER_ID)
|
|
422
|
-
* });
|
|
423
|
-
*/
|
|
424
|
-
declare const hydrateVariable: <Environment extends Env, HidratedResult>({ variableName, hydrate, }: {
|
|
425
|
-
variableName: keyof Environment["Variables"];
|
|
426
|
-
hydrate: (c: Environment["Bindings"] & {
|
|
427
|
-
get: <T>(key: keyof Environment["Variables"]) => T;
|
|
428
|
-
}) => HidratedResult;
|
|
429
|
-
}) => hono.MiddlewareHandler<{
|
|
430
|
-
Bindings: Environment["Bindings"];
|
|
431
|
-
Variables: Record<string, HidratedResult>;
|
|
432
|
-
}, string, {}, Response>;
|
|
387
|
+
}>;
|
|
433
388
|
|
|
434
389
|
/**
|
|
435
390
|
* Derives a secure hex-encoded hash from an input string using PBKDF2-HMAC-SHA256.
|
|
@@ -501,4 +456,4 @@ interface CreateTypedClientOptions {
|
|
|
501
456
|
}
|
|
502
457
|
declare const createTypedClient: <TApp extends Hono<any, any, any>>() => (options: CreateTypedClientOptions) => <TSuccessData>(fn: (c: ReturnType<typeof hc<TApp>>) => Promise<ClientResponse<TSuccessData>>, callbacks?: TypedClientCallbacks) => Promise<TSuccessData>;
|
|
503
458
|
|
|
504
|
-
export { type ContextFn, type CreateTypedClientOptions, type HonoClientInfoVariables, type HonoI18nVariables, type HonoIsBotVariables, type HonoLanguageVariables, type HonoLoggerVariables, type HonoResponseVariables, type MessageHandlers, pbkdf2 as PBKDF2, QueueHandler, sha as SHA, type TypedClientCallbacks,
|
|
459
|
+
export { type ContextFn, type CreateTypedClientOptions, type HonoClientInfoVariables, type HonoI18nVariables, type HonoIsBotVariables, type HonoLanguageVariables, type HonoLoggerVariables, type HonoResponseVariables, type MessageHandlers, type MiddlewareWithLoggingCapability, pbkdf2 as PBKDF2, QueueHandler, sha as SHA, type TypedClientCallbacks, clientInfo, createTypedClient, i18n, isBot, jsonValidator, logger, onError, onNotFound, queue, response };
|
package/dist/index.js
CHANGED
|
@@ -19,23 +19,6 @@ var logger = (details, parentEventIdHeader) => createMiddleware(async (c, next)
|
|
|
19
19
|
await next();
|
|
20
20
|
});
|
|
21
21
|
|
|
22
|
-
// src/middleware/withLogger.ts
|
|
23
|
-
import { createMiddleware as createMiddleware2 } from "hono/factory";
|
|
24
|
-
var withLogger = (name, middleware) => createMiddleware2(async (c, next) => {
|
|
25
|
-
const logger2 = c.get("logger");
|
|
26
|
-
if (!logger2) {
|
|
27
|
-
throw new Error("Logger should be initialized");
|
|
28
|
-
}
|
|
29
|
-
const loggerFunctions = logger2.getArea(`middleware:${name}`);
|
|
30
|
-
await middleware(c, {
|
|
31
|
-
...loggerFunctions,
|
|
32
|
-
eventId: logger2.eventId,
|
|
33
|
-
parentEventId: logger2.parentEventId,
|
|
34
|
-
dump: logger2.dump
|
|
35
|
-
});
|
|
36
|
-
await next();
|
|
37
|
-
});
|
|
38
|
-
|
|
39
22
|
// src/middleware/validator.ts
|
|
40
23
|
import { HTTPException } from "hono/http-exception";
|
|
41
24
|
import { validator } from "hono/validator";
|
|
@@ -13821,11 +13804,17 @@ var jsonValidator = (schema) => validator("json", (value) => {
|
|
|
13821
13804
|
|
|
13822
13805
|
// src/middleware/isBot.ts
|
|
13823
13806
|
import { HTTPException as HTTPException2 } from "hono/http-exception";
|
|
13824
|
-
import {
|
|
13825
|
-
|
|
13826
|
-
|
|
13827
|
-
|
|
13828
|
-
}) =>
|
|
13807
|
+
import { Logger as Logger2 } from "hierarchical-area-logger";
|
|
13808
|
+
import { createMiddleware as createMiddleware2 } from "hono/factory";
|
|
13809
|
+
var isBot = ({ threshold, allowVerifiedBot, useLogger } = {
|
|
13810
|
+
threshold: 49
|
|
13811
|
+
}) => createMiddleware2(async (c, next) => {
|
|
13812
|
+
const logger2 = useLogger ? (c.get("logger") ?? new Logger2({
|
|
13813
|
+
details: {
|
|
13814
|
+
service: "dummy"
|
|
13815
|
+
}
|
|
13816
|
+
})).getArea("middeware:isBot") : void 0;
|
|
13817
|
+
logger2?.debug("Retrieving bot management data from cloudflare data");
|
|
13829
13818
|
const botManagement = c.req.raw.cf?.botManagement;
|
|
13830
13819
|
if (!botManagement) {
|
|
13831
13820
|
throw new Error("botManagement is not available");
|
|
@@ -13835,6 +13824,12 @@ var isBot = ({
|
|
|
13835
13824
|
const isBotByScore = score < threshold;
|
|
13836
13825
|
const isVerifiedAllowed = !!(allowVerifiedBot && verifiedBot);
|
|
13837
13826
|
const isBot2 = isBotByScore;
|
|
13827
|
+
logger2?.debug("Setting isBot variable in context", {
|
|
13828
|
+
isBotByScore,
|
|
13829
|
+
isVerifiedAllowed,
|
|
13830
|
+
isBot: isBot2
|
|
13831
|
+
});
|
|
13832
|
+
logger2?.info("isBot variable set");
|
|
13838
13833
|
c.set("isBot", isBot2);
|
|
13839
13834
|
if (isBotByScore && !isVerifiedAllowed) {
|
|
13840
13835
|
throw new HTTPException2(401, { message: "Bot detected" });
|
|
@@ -13843,7 +13838,7 @@ var isBot = ({
|
|
|
13843
13838
|
});
|
|
13844
13839
|
|
|
13845
13840
|
// src/middleware/i18n.ts
|
|
13846
|
-
import { createMiddleware as
|
|
13841
|
+
import { createMiddleware as createMiddleware3 } from "hono/factory";
|
|
13847
13842
|
var setI18n = (translations, language, defaultLanguage) => (input, params, overrideLanguage) => {
|
|
13848
13843
|
const result = input.split(".").reduce(
|
|
13849
13844
|
(acc, key) => {
|
|
@@ -13873,7 +13868,7 @@ var setI18n = (translations, language, defaultLanguage) => (input, params, overr
|
|
|
13873
13868
|
return typeof result === "string" ? result : input;
|
|
13874
13869
|
};
|
|
13875
13870
|
var i18n = (translations, defaultLanguage) => ({
|
|
13876
|
-
middleware:
|
|
13871
|
+
middleware: createMiddleware3(async ({ set: set2, var: { language } }, next) => {
|
|
13877
13872
|
if (!language) {
|
|
13878
13873
|
throw new Error(
|
|
13879
13874
|
"Language variable should be initialized before translations middleware"
|
|
@@ -13886,26 +13881,26 @@ var i18n = (translations, defaultLanguage) => ({
|
|
|
13886
13881
|
});
|
|
13887
13882
|
|
|
13888
13883
|
// src/middleware/queue.ts
|
|
13889
|
-
import { createMiddleware as
|
|
13884
|
+
import { createMiddleware as createMiddleware4 } from "hono/factory";
|
|
13890
13885
|
var queue = ({
|
|
13891
13886
|
name,
|
|
13892
13887
|
queueHandler,
|
|
13893
13888
|
finalizeLogHandler
|
|
13894
|
-
}) =>
|
|
13889
|
+
}) => createMiddleware4(async ({ env, set: set2, get }, next) => {
|
|
13895
13890
|
const queueEnv = env[name];
|
|
13896
13891
|
const language = get("language");
|
|
13897
|
-
const logger2 = get("logger");
|
|
13898
13892
|
if (!queueEnv) {
|
|
13899
13893
|
throw new Error(
|
|
13900
13894
|
`Queue environment variable ${name} is not defined`
|
|
13901
13895
|
);
|
|
13902
13896
|
}
|
|
13897
|
+
const logger2 = get("logger");
|
|
13903
13898
|
let parentEventId;
|
|
13904
13899
|
if (finalizeLogHandler) {
|
|
13905
13900
|
if (!logger2) {
|
|
13906
13901
|
throw new Error("Logger should be initialized before queue middleware");
|
|
13907
13902
|
}
|
|
13908
|
-
parentEventId = logger2
|
|
13903
|
+
parentEventId = logger2?.eventId;
|
|
13909
13904
|
}
|
|
13910
13905
|
const producer = queueHandler.getProducer(queueEnv, {
|
|
13911
13906
|
parentEventId,
|
|
@@ -13922,7 +13917,7 @@ var queue = ({
|
|
|
13922
13917
|
});
|
|
13923
13918
|
|
|
13924
13919
|
// src/middleware/response.ts
|
|
13925
|
-
import { createMiddleware as
|
|
13920
|
+
import { createMiddleware as createMiddleware5 } from "hono/factory";
|
|
13926
13921
|
import { HTTPException as HTTPException3 } from "hono/http-exception";
|
|
13927
13922
|
var setResponseHandlers = (c) => {
|
|
13928
13923
|
return {
|
|
@@ -13951,7 +13946,7 @@ var setResponseHandlers = (c) => {
|
|
|
13951
13946
|
}
|
|
13952
13947
|
};
|
|
13953
13948
|
};
|
|
13954
|
-
var response =
|
|
13949
|
+
var response = createMiddleware5(async (c, next) => {
|
|
13955
13950
|
c.set("res", setResponseHandlers(c));
|
|
13956
13951
|
if (c.var.eventId) {
|
|
13957
13952
|
c.header("x-event-id", c.var.eventId);
|
|
@@ -13960,7 +13955,7 @@ var response = createMiddleware6(async (c, next) => {
|
|
|
13960
13955
|
});
|
|
13961
13956
|
|
|
13962
13957
|
// src/middleware/clientInfo.ts
|
|
13963
|
-
import { createMiddleware as
|
|
13958
|
+
import { createMiddleware as createMiddleware6 } from "hono/factory";
|
|
13964
13959
|
import { UAParser } from "ua-parser-js";
|
|
13965
13960
|
|
|
13966
13961
|
// src/crypto/pbkdf2.ts
|
|
@@ -14045,10 +14040,17 @@ async function hash3({
|
|
|
14045
14040
|
}
|
|
14046
14041
|
|
|
14047
14042
|
// src/middleware/clientInfo.ts
|
|
14048
|
-
|
|
14043
|
+
import { Logger as Logger3 } from "hierarchical-area-logger";
|
|
14044
|
+
var clientInfo = (config2) => createMiddleware6(async ({ env, req, set: set2, get }, next) => {
|
|
14045
|
+
const logger2 = config2?.useLogger ? (get("logger") ?? new Logger3({
|
|
14046
|
+
details: {
|
|
14047
|
+
service: "dummy"
|
|
14048
|
+
}
|
|
14049
|
+
})).getArea("middeware:clientInfo") : void 0;
|
|
14049
14050
|
if (!req.raw.cf) {
|
|
14050
14051
|
throw new Error("Cloudflare data is not available");
|
|
14051
14052
|
}
|
|
14053
|
+
logger2?.debug("Retrieving hash secret from environment");
|
|
14052
14054
|
const hashSecret = env[config2?.hashSecretBinding ?? "HASH_SECRET"];
|
|
14053
14055
|
if (!hashSecret) {
|
|
14054
14056
|
throw new Error("Hash secret is not available");
|
|
@@ -14067,6 +14069,7 @@ var clientInfo = (config2) => createMiddleware7(async ({ env, req, set: set2 },
|
|
|
14067
14069
|
postalCode,
|
|
14068
14070
|
timezone
|
|
14069
14071
|
} = req.raw.cf;
|
|
14072
|
+
logger2?.debug("Forming cloudflare client data object");
|
|
14070
14073
|
const cf = {
|
|
14071
14074
|
latitude,
|
|
14072
14075
|
longitude,
|
|
@@ -14081,6 +14084,7 @@ var clientInfo = (config2) => createMiddleware7(async ({ env, req, set: set2 },
|
|
|
14081
14084
|
postalCode,
|
|
14082
14085
|
timezone
|
|
14083
14086
|
};
|
|
14087
|
+
logger2?.debug("Parsing user agent");
|
|
14084
14088
|
const userAgent = req.raw.headers.get("user-agent") ?? "Unknown";
|
|
14085
14089
|
const parser = new UAParser(userAgent);
|
|
14086
14090
|
const userAgentParsed = parser.getResult();
|
|
@@ -14105,8 +14109,10 @@ var clientInfo = (config2) => createMiddleware7(async ({ env, req, set: set2 },
|
|
|
14105
14109
|
},
|
|
14106
14110
|
cpu: cpu.architecture ?? "Unknown"
|
|
14107
14111
|
};
|
|
14112
|
+
logger2?.debug("Getting ip address");
|
|
14108
14113
|
const ip = req.raw.headers.get("CF-Connecting-IP") ?? "127.0.0.1";
|
|
14109
14114
|
const clientContent = { ...cf, ...ua, ip };
|
|
14115
|
+
logger2?.debug("Setting client data into context");
|
|
14110
14116
|
set2("client", {
|
|
14111
14117
|
...clientContent,
|
|
14112
14118
|
userAgent,
|
|
@@ -14116,16 +14122,7 @@ var clientInfo = (config2) => createMiddleware7(async ({ env, req, set: set2 },
|
|
|
14116
14122
|
pepper: hashSecret
|
|
14117
14123
|
})
|
|
14118
14124
|
});
|
|
14119
|
-
|
|
14120
|
-
});
|
|
14121
|
-
|
|
14122
|
-
// src/middleware/hydrateVariable.ts
|
|
14123
|
-
import { createMiddleware as createMiddleware8 } from "hono/factory";
|
|
14124
|
-
var hydrateVariable = ({
|
|
14125
|
-
variableName,
|
|
14126
|
-
hydrate
|
|
14127
|
-
}) => createMiddleware8(async ({ set: set2, env, get }, next) => {
|
|
14128
|
-
set2(variableName, hydrate({ ...env, get }));
|
|
14125
|
+
logger2?.info("Client data variable set");
|
|
14129
14126
|
await next();
|
|
14130
14127
|
});
|
|
14131
14128
|
|
|
@@ -14266,7 +14263,8 @@ var createTypedClient = () => {
|
|
|
14266
14263
|
throw new HTTPException4(status, { message: "Fetch malformed" });
|
|
14267
14264
|
}
|
|
14268
14265
|
const { message } = detail;
|
|
14269
|
-
|
|
14266
|
+
const eventId = responseHeaders.get("x-event-id") ?? void 0;
|
|
14267
|
+
options.errorHandler?.(status, { ...detail, eventId, status });
|
|
14270
14268
|
callbacks?.onError?.(detail, responseHeaders);
|
|
14271
14269
|
throw new HTTPException4(status, { message });
|
|
14272
14270
|
} finally {
|
|
@@ -14280,7 +14278,6 @@ export {
|
|
|
14280
14278
|
sha_exports as SHA,
|
|
14281
14279
|
clientInfo,
|
|
14282
14280
|
createTypedClient,
|
|
14283
|
-
hydrateVariable,
|
|
14284
14281
|
i18n,
|
|
14285
14282
|
isBot,
|
|
14286
14283
|
jsonValidator,
|
|
@@ -14288,7 +14285,6 @@ export {
|
|
|
14288
14285
|
onError,
|
|
14289
14286
|
onNotFound,
|
|
14290
14287
|
queue,
|
|
14291
|
-
response
|
|
14292
|
-
withLogger
|
|
14288
|
+
response
|
|
14293
14289
|
};
|
|
14294
14290
|
//# sourceMappingURL=index.js.map
|