hono 2.2.5 → 2.3.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.
@@ -36,13 +36,12 @@ const basicAuth = (options, ...users) => {
36
36
  }
37
37
  }
38
38
  }
39
- ctx.res = new Response('Unauthorized', {
39
+ return new Response('Unauthorized', {
40
40
  status: 401,
41
41
  headers: {
42
42
  'WWW-Authenticate': 'Basic realm="' + options.realm?.replace(/"/g, '\\"') + '"',
43
43
  },
44
44
  });
45
- await next();
46
45
  };
47
46
  };
48
47
  exports.basicAuth = basicAuth;
@@ -19,7 +19,7 @@ const bearerAuth = (options) => {
19
19
  const headerToken = c.req.headers.get('Authorization');
20
20
  if (!headerToken) {
21
21
  // No Authorization header
22
- c.res = new Response('Unauthorized', {
22
+ return new Response('Unauthorized', {
23
23
  status: 401,
24
24
  headers: {
25
25
  'WWW-Authenticate': `${options.prefix} realm="` + realm + '"',
@@ -31,7 +31,7 @@ const bearerAuth = (options) => {
31
31
  const match = regexp.exec(headerToken);
32
32
  if (!match) {
33
33
  // Invalid Request
34
- c.res = new Response('Bad Request', {
34
+ return new Response('Bad Request', {
35
35
  status: 400,
36
36
  headers: {
37
37
  'WWW-Authenticate': `${options.prefix} error="invalid_request"`,
@@ -42,7 +42,7 @@ const bearerAuth = (options) => {
42
42
  const equal = await (0, buffer_1.timingSafeEqual)(options.token, match[1], options.hashFunction);
43
43
  if (!equal) {
44
44
  // Invalid Token
45
- c.res = new Response('Unauthorized', {
45
+ return new Response('Unauthorized', {
46
46
  status: 401,
47
47
  headers: {
48
48
  'WWW-Authenticate': `${options.prefix} error="invalid_token"`,
@@ -12,7 +12,7 @@ const compress = (options) => {
12
12
  }
13
13
  const encoding = match[0];
14
14
  const stream = new CompressionStream(encoding);
15
- ctx.res = new Response(ctx.res.body.pipeThrough(stream), ctx.res.clone());
15
+ ctx.res = new Response(ctx.res.body.pipeThrough(stream), ctx.res);
16
16
  ctx.res.headers.set('Content-Encoding', encoding);
17
17
  };
18
18
  };
@@ -15,13 +15,12 @@ const jwt = (options) => {
15
15
  if (credentials) {
16
16
  const parts = credentials.split(/\s+/);
17
17
  if (parts.length !== 2) {
18
- ctx.res = new Response('Unauthorized', {
18
+ return new Response('Unauthorized', {
19
19
  status: 401,
20
20
  headers: {
21
21
  'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="invalid credentials structure"`,
22
22
  },
23
23
  });
24
- return;
25
24
  }
26
25
  else {
27
26
  token = parts[1];
@@ -31,13 +30,12 @@ const jwt = (options) => {
31
30
  token = ctx.req.cookie(options.cookie);
32
31
  }
33
32
  if (!token) {
34
- ctx.res = new Response('Unauthorized', {
33
+ return new Response('Unauthorized', {
35
34
  status: 401,
36
35
  headers: {
37
36
  'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="no authorization included in request"`,
38
37
  },
39
38
  });
40
- return;
41
39
  }
42
40
  let authorized = false;
43
41
  let msg = '';
@@ -48,14 +46,13 @@ const jwt = (options) => {
48
46
  msg = `${e}`;
49
47
  }
50
48
  if (!authorized) {
51
- ctx.res = new Response('Unauthorized', {
49
+ return new Response('Unauthorized', {
52
50
  status: 401,
53
51
  statusText: msg,
54
52
  headers: {
55
53
  'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_token",error_description="token verification failure"`,
56
54
  },
57
55
  });
58
- return;
59
56
  }
60
57
  await next();
61
58
  };
@@ -14,6 +14,7 @@ const serveStatic = (options = { root: '' }) => {
14
14
  // Do nothing if Response is already set
15
15
  if (c.finalized) {
16
16
  await next();
17
+ return;
17
18
  }
18
19
  const url = new URL(c.req.url);
19
20
  let path = (0, filepath_1.getFilePath)({
@@ -11,6 +11,7 @@ const serveStatic = (options = { root: '' }) => {
11
11
  // Do nothing if Response is already set
12
12
  if (c.finalized) {
13
13
  await next();
14
+ return;
14
15
  }
15
16
  const url = new URL(c.req.url);
16
17
  const path = (0, filepath_1.getFilePath)({
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validatorMiddleware = void 0;
4
4
  const http_status_1 = require("../../utils/http-status");
5
+ const object_1 = require("../../utils/object");
5
6
  const validator_1 = require("./validator");
6
7
  const validatorMiddleware = (validationFunction, options) => {
7
8
  const v = new validator_1.Validator();
@@ -11,27 +12,47 @@ const validatorMiddleware = (validationFunction, options) => {
11
12
  messages: [],
12
13
  results: [],
13
14
  };
14
- const validatorList = getValidatorList(validationFunction(v, c));
15
+ const schema = validationFunction(v, c);
16
+ const validatorList = getValidatorList(schema);
17
+ let data = {};
15
18
  for (const [keys, validator] of validatorList) {
16
- let result;
19
+ let results;
17
20
  try {
18
- result = await validator.validate(c.req);
21
+ results = await validator.validate(c.req);
19
22
  }
20
23
  catch (e) {
21
24
  // Invalid JSON request
22
25
  return c.text((0, http_status_1.getStatusText)(400), 400);
23
26
  }
24
- if (result.isValid) {
25
- // Set data on request object
26
- c.req.valid(keys, result.value);
27
+ let isValid = true;
28
+ const value = results[0].value;
29
+ const jsonData = results[0].jsonData;
30
+ for (const result of results) {
31
+ if (!result.isValid) {
32
+ isValid = false;
33
+ resultSet.hasError = true;
34
+ if (result.ruleType === 'value' && result.message !== undefined) {
35
+ resultSet.messages.push(result.message);
36
+ }
37
+ }
38
+ resultSet.results.push(result);
27
39
  }
28
- else {
29
- resultSet.hasError = true;
30
- if (result.message !== undefined) {
31
- resultSet.messages.push(result.message);
40
+ // Set data on request object
41
+ if (isValid) {
42
+ // Set data on request object
43
+ if (jsonData) {
44
+ const dst = data;
45
+ data = (0, object_1.mergeObjects)(dst, jsonData);
46
+ }
47
+ else {
48
+ c.req.valid(keys, value);
32
49
  }
33
50
  }
34
- resultSet.results.push(result);
51
+ }
52
+ if (!resultSet.hasError) {
53
+ Object.keys(data).map((key) => {
54
+ c.req.valid(key, data[key]);
55
+ });
35
56
  }
36
57
  if (options && options.done) {
37
58
  const res = options.done(resultSet, c);
@@ -50,7 +71,13 @@ exports.validatorMiddleware = validatorMiddleware;
50
71
  function getValidatorList(schema) {
51
72
  const map = [];
52
73
  for (const [key, value] of Object.entries(schema)) {
53
- if (value instanceof validator_1.VBase) {
74
+ if (value instanceof validator_1.VObjectBase) {
75
+ const validators = value.getValidators();
76
+ for (const validator of validators) {
77
+ map.push([value.keys, validator]);
78
+ }
79
+ }
80
+ else if (value instanceof validator_1.VBase) {
54
81
  map.push([[key], value]);
55
82
  }
56
83
  else {