hono-utils 0.3.8 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -12,30 +12,14 @@ var logger = (details, parentEventIdHeader) => _factory.createMiddleware.call(vo
12
12
  details,
13
13
  withParentEventId: !!parentEventIdHeader,
14
14
  parentEventId: parentEventIdHeader ? _nullishCoalesce(c.req.raw.headers.get(parentEventIdHeader), () => ( void 0)) : void 0,
15
- defaultArea: new URL(c.req.raw.url).pathname
15
+ defaultArea: new URL(c.req.raw.url).pathname,
16
+ path: new URL(c.req.raw.url).pathname
16
17
  });
17
18
  c.set("logger", logger2);
18
19
  c.set("eventId", logger2.eventId);
19
20
  await next();
20
21
  });
21
22
 
22
- // src/middleware/withLogger.ts
23
-
24
- var withLogger = (name, middleware) => _factory.createMiddleware.call(void 0, 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
23
  // src/middleware/validator.ts
40
24
  var _httpexception = require('hono/http-exception');
41
25
  var _validator = require('hono/validator');
@@ -13822,19 +13806,31 @@ var jsonValidator = (schema) => _validator.validator.call(void 0, "json", (value
13822
13806
  // src/middleware/isBot.ts
13823
13807
 
13824
13808
 
13825
- var isBot = ({
13826
- threshold = 49,
13827
- allowVerifiedBot
13809
+
13810
+ var isBot = ({ threshold, allowVerifiedBot, useLogger } = {
13811
+ threshold: 49
13828
13812
  }) => _factory.createMiddleware.call(void 0, async (c, next) => {
13829
- const botManagement = _optionalChain([c, 'access', _183 => _183.req, 'access', _184 => _184.raw, 'access', _185 => _185.cf, 'optionalAccess', _186 => _186.botManagement]);
13813
+ const logger2 = useLogger ? (_nullishCoalesce(c.get("logger"), () => ( new (0, _hierarchicalarealogger.Logger)({
13814
+ details: {
13815
+ service: "dummy"
13816
+ }
13817
+ })))).getArea("middeware:isBot") : void 0;
13818
+ _optionalChain([logger2, 'optionalAccess', _183 => _183.debug, 'call', _184 => _184("Retrieving bot management data from cloudflare data")]);
13819
+ const botManagement = _optionalChain([c, 'access', _185 => _185.req, 'access', _186 => _186.raw, 'access', _187 => _187.cf, 'optionalAccess', _188 => _188.botManagement]);
13830
13820
  if (!botManagement) {
13831
13821
  throw new Error("botManagement is not available");
13832
13822
  }
13833
- const score = _nullishCoalesce(_optionalChain([botManagement, 'optionalAccess', _187 => _187.score]), () => ( 54));
13834
- const verifiedBot = _nullishCoalesce(_optionalChain([botManagement, 'optionalAccess', _188 => _188.verifiedBot]), () => ( false));
13823
+ const score = _nullishCoalesce(_optionalChain([botManagement, 'optionalAccess', _189 => _189.score]), () => ( 54));
13824
+ const verifiedBot = _nullishCoalesce(_optionalChain([botManagement, 'optionalAccess', _190 => _190.verifiedBot]), () => ( false));
13835
13825
  const isBotByScore = score < threshold;
13836
13826
  const isVerifiedAllowed = !!(allowVerifiedBot && verifiedBot);
13837
13827
  const isBot2 = isBotByScore;
13828
+ _optionalChain([logger2, 'optionalAccess', _191 => _191.debug, 'call', _192 => _192("Setting isBot variable in context", {
13829
+ isBotByScore,
13830
+ isVerifiedAllowed,
13831
+ isBot: isBot2
13832
+ })]);
13833
+ _optionalChain([logger2, 'optionalAccess', _193 => _193.info, 'call', _194 => _194("isBot variable set")]);
13838
13834
  c.set("isBot", isBot2);
13839
13835
  if (isBotByScore && !isVerifiedAllowed) {
13840
13836
  throw new (0, _httpexception.HTTPException)(401, { message: "Bot detected" });
@@ -13894,18 +13890,18 @@ var queue = ({
13894
13890
  }) => _factory.createMiddleware.call(void 0, async ({ env, set: set2, get }, next) => {
13895
13891
  const queueEnv = env[name];
13896
13892
  const language = get("language");
13897
- const logger2 = get("logger");
13898
13893
  if (!queueEnv) {
13899
13894
  throw new Error(
13900
13895
  `Queue environment variable ${name} is not defined`
13901
13896
  );
13902
13897
  }
13898
+ const logger2 = get("logger");
13903
13899
  let parentEventId;
13904
13900
  if (finalizeLogHandler) {
13905
13901
  if (!logger2) {
13906
13902
  throw new Error("Logger should be initialized before queue middleware");
13907
13903
  }
13908
- parentEventId = logger2.eventId;
13904
+ parentEventId = _optionalChain([logger2, 'optionalAccess', _195 => _195.eventId]);
13909
13905
  }
13910
13906
  const producer = queueHandler.getProducer(queueEnv, {
13911
13907
  parentEventId,
@@ -14045,11 +14041,18 @@ async function hash3({
14045
14041
  }
14046
14042
 
14047
14043
  // src/middleware/clientInfo.ts
14048
- var clientInfo = (config2) => _factory.createMiddleware.call(void 0, async ({ env, req, set: set2 }, next) => {
14044
+
14045
+ var clientInfo = (config2) => _factory.createMiddleware.call(void 0, async ({ env, req, set: set2, get }, next) => {
14046
+ const logger2 = _optionalChain([config2, 'optionalAccess', _196 => _196.useLogger]) ? (_nullishCoalesce(get("logger"), () => ( new (0, _hierarchicalarealogger.Logger)({
14047
+ details: {
14048
+ service: "dummy"
14049
+ }
14050
+ })))).getArea("middeware:clientInfo") : void 0;
14049
14051
  if (!req.raw.cf) {
14050
14052
  throw new Error("Cloudflare data is not available");
14051
14053
  }
14052
- const hashSecret = env[_nullishCoalesce(_optionalChain([config2, 'optionalAccess', _189 => _189.hashSecretBinding]), () => ( "HASH_SECRET"))];
14054
+ _optionalChain([logger2, 'optionalAccess', _197 => _197.debug, 'call', _198 => _198("Retrieving hash secret from environment")]);
14055
+ const hashSecret = env[_nullishCoalesce(_optionalChain([config2, 'optionalAccess', _199 => _199.hashSecretBinding]), () => ( "HASH_SECRET"))];
14053
14056
  if (!hashSecret) {
14054
14057
  throw new Error("Hash secret is not available");
14055
14058
  }
@@ -14067,6 +14070,7 @@ var clientInfo = (config2) => _factory.createMiddleware.call(void 0, async ({ en
14067
14070
  postalCode,
14068
14071
  timezone
14069
14072
  } = req.raw.cf;
14073
+ _optionalChain([logger2, 'optionalAccess', _200 => _200.debug, 'call', _201 => _201("Forming cloudflare client data object")]);
14070
14074
  const cf = {
14071
14075
  latitude,
14072
14076
  longitude,
@@ -14081,6 +14085,7 @@ var clientInfo = (config2) => _factory.createMiddleware.call(void 0, async ({ en
14081
14085
  postalCode,
14082
14086
  timezone
14083
14087
  };
14088
+ _optionalChain([logger2, 'optionalAccess', _202 => _202.debug, 'call', _203 => _203("Parsing user agent")]);
14084
14089
  const userAgent = _nullishCoalesce(req.raw.headers.get("user-agent"), () => ( "Unknown"));
14085
14090
  const parser = new (0, _uaparserjs.UAParser)(userAgent);
14086
14091
  const userAgentParsed = parser.getResult();
@@ -14105,27 +14110,20 @@ var clientInfo = (config2) => _factory.createMiddleware.call(void 0, async ({ en
14105
14110
  },
14106
14111
  cpu: _nullishCoalesce(cpu.architecture, () => ( "Unknown"))
14107
14112
  };
14113
+ _optionalChain([logger2, 'optionalAccess', _204 => _204.debug, 'call', _205 => _205("Getting ip address")]);
14108
14114
  const ip = _nullishCoalesce(req.raw.headers.get("CF-Connecting-IP"), () => ( "127.0.0.1"));
14109
14115
  const clientContent = { ...cf, ...ua, ip };
14116
+ _optionalChain([logger2, 'optionalAccess', _206 => _206.debug, 'call', _207 => _207("Setting client data into context")]);
14110
14117
  set2("client", {
14111
14118
  ...clientContent,
14112
14119
  userAgent,
14113
14120
  securityHash: await sha_exports.hash({
14114
- input: _nullishCoalesce(_optionalChain([config2, 'optionalAccess', _190 => _190.securityHashString, 'optionalCall', _191 => _191(clientContent)]), () => ( `${ip}${city}${country}${userAgent}`)),
14121
+ input: _nullishCoalesce(_optionalChain([config2, 'optionalAccess', _208 => _208.securityHashString, 'optionalCall', _209 => _209(clientContent)]), () => ( `${ip}${city}${country}${userAgent}`)),
14115
14122
  algorithm: "SHA-512",
14116
14123
  pepper: hashSecret
14117
14124
  })
14118
14125
  });
14119
- next();
14120
- });
14121
-
14122
- // src/middleware/hydrateVariable.ts
14123
-
14124
- var hydrateVariable = ({
14125
- variableName,
14126
- hydrate
14127
- }) => _factory.createMiddleware.call(void 0, async ({ set: set2, env, get }, next) => {
14128
- set2(variableName, hydrate({ ...env, get }));
14126
+ _optionalChain([logger2, 'optionalAccess', _210 => _210.info, 'call', _211 => _211("Client data variable set")]);
14129
14127
  await next();
14130
14128
  });
14131
14129
 
@@ -14247,30 +14245,31 @@ var createTypedClient = () => {
14247
14245
  headers: options.headers,
14248
14246
  fetch: options.fetch
14249
14247
  });
14250
- _optionalChain([callbacks, 'optionalAccess', _192 => _192.onStart, 'optionalCall', _193 => _193()]);
14248
+ _optionalChain([callbacks, 'optionalAccess', _212 => _212.onStart, 'optionalCall', _213 => _213()]);
14251
14249
  let responseHeaders = new Headers();
14252
14250
  try {
14253
14251
  const response2 = await fn(client);
14254
14252
  responseHeaders = response2.headers;
14255
14253
  const data = await _client.parseResponse.call(void 0, response2);
14256
- _optionalChain([callbacks, 'optionalAccess', _194 => _194.onSuccess, 'optionalCall', _195 => _195(data, responseHeaders)]);
14254
+ _optionalChain([callbacks, 'optionalAccess', _214 => _214.onSuccess, 'optionalCall', _215 => _215(data, responseHeaders)]);
14257
14255
  return data;
14258
14256
  } catch (err) {
14259
14257
  let status = 500;
14260
14258
  const { detail, statusCode } = err;
14261
14259
  status = _nullishCoalesce(statusCode, () => ( 500));
14262
14260
  if (!detail) {
14263
- _optionalChain([options, 'access', _196 => _196.errorHandler, 'optionalCall', _197 => _197(status, {
14261
+ _optionalChain([options, 'access', _216 => _216.errorHandler, 'optionalCall', _217 => _217(status, {
14264
14262
  message: "Fetch malformed"
14265
14263
  })]);
14266
14264
  throw new (0, _httpexception.HTTPException)(status, { message: "Fetch malformed" });
14267
14265
  }
14268
14266
  const { message } = detail;
14269
- _optionalChain([options, 'access', _198 => _198.errorHandler, 'optionalCall', _199 => _199(status, { ...detail, status })]);
14270
- _optionalChain([callbacks, 'optionalAccess', _200 => _200.onError, 'optionalCall', _201 => _201(detail, responseHeaders)]);
14267
+ const eventId = _nullishCoalesce(responseHeaders.get("x-event-id"), () => ( void 0));
14268
+ _optionalChain([options, 'access', _218 => _218.errorHandler, 'optionalCall', _219 => _219(status, { ...detail, eventId, status })]);
14269
+ _optionalChain([callbacks, 'optionalAccess', _220 => _220.onError, 'optionalCall', _221 => _221(detail, responseHeaders)]);
14271
14270
  throw new (0, _httpexception.HTTPException)(status, { message });
14272
14271
  } finally {
14273
- _optionalChain([callbacks, 'optionalAccess', _202 => _202.onEnd, 'optionalCall', _203 => _203()]);
14272
+ _optionalChain([callbacks, 'optionalAccess', _222 => _222.onEnd, 'optionalCall', _223 => _223()]);
14274
14273
  }
14275
14274
  };
14276
14275
  };
@@ -14288,7 +14287,5 @@ var createTypedClient = () => {
14288
14287
 
14289
14288
 
14290
14289
 
14291
-
14292
-
14293
- exports.PBKDF2 = pbkdf2_exports; exports.QueueHandler = QueueHandler; exports.SHA = sha_exports; exports.clientInfo = clientInfo; exports.createTypedClient = createTypedClient; exports.hydrateVariable = hydrateVariable; exports.i18n = i18n; exports.isBot = isBot; exports.jsonValidator = jsonValidator; exports.logger = logger; exports.onError = onError; exports.onNotFound = onNotFound; exports.queue = queue; exports.response = response; exports.withLogger = withLogger;
14290
+ exports.PBKDF2 = pbkdf2_exports; exports.QueueHandler = QueueHandler; exports.SHA = sha_exports; exports.clientInfo = clientInfo; exports.createTypedClient = createTypedClient; exports.i18n = i18n; exports.isBot = isBot; exports.jsonValidator = jsonValidator; exports.logger = logger; exports.onError = onError; exports.onNotFound = onNotFound; exports.queue = queue; exports.response = response;
14294
14291
  //# sourceMappingURL=index.cjs.map