hono 2.2.5 → 2.3.1
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/cjs/middleware/basic-auth/index.js +1 -2
- package/dist/cjs/middleware/bearer-auth/index.js +3 -3
- package/dist/cjs/middleware/compress/index.js +1 -1
- package/dist/cjs/middleware/jwt/index.js +3 -6
- package/dist/cjs/middleware/serve-static/bun.js +1 -0
- package/dist/cjs/middleware/serve-static/serve-static.js +1 -0
- package/dist/cjs/middleware/validator/middleware.js +48 -12
- package/dist/cjs/middleware/validator/validator.js +239 -93
- package/dist/cjs/utils/json.js +67 -13
- package/dist/cjs/utils/object.js +40 -0
- package/dist/hono.d.ts +1 -1
- package/dist/middleware/basic-auth/index.js +1 -2
- package/dist/middleware/bearer-auth/index.js +3 -3
- package/dist/middleware/compress/index.js +1 -1
- package/dist/middleware/jwt/index.js +3 -6
- package/dist/middleware/serve-static/bun.js +1 -0
- package/dist/middleware/serve-static/serve-static.js +1 -0
- package/dist/middleware/validator/middleware.d.ts +5 -5
- package/dist/middleware/validator/middleware.js +49 -13
- package/dist/middleware/validator/validator.d.ts +48 -17
- package/dist/middleware/validator/validator.js +236 -92
- package/dist/utils/json.d.ts +1 -1
- package/dist/utils/json.js +65 -11
- package/dist/utils/object.d.ts +2 -0
- package/dist/utils/object.js +35 -0
- package/package.json +1 -1
|
@@ -36,13 +36,12 @@ const basicAuth = (options, ...users) => {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
};
|
|
@@ -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,56 @@ const validatorMiddleware = (validationFunction, options) => {
|
|
|
11
12
|
messages: [],
|
|
12
13
|
results: [],
|
|
13
14
|
};
|
|
14
|
-
const
|
|
15
|
+
const schema = validationFunction(v, c);
|
|
16
|
+
const validatorList = getValidatorList(schema);
|
|
17
|
+
let data = {};
|
|
15
18
|
for (const [keys, validator] of validatorList) {
|
|
16
|
-
let
|
|
19
|
+
let results;
|
|
17
20
|
try {
|
|
18
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
40
|
+
// If it's invalid but it has no "value" messages, have to set the "type" messages.
|
|
41
|
+
// This approach is verbose, but if do not so, the response body will be empty.
|
|
42
|
+
if (!isValid && resultSet.messages.length === 0) {
|
|
43
|
+
resultSet.results.map((r) => {
|
|
44
|
+
if (!r.isValid && r.ruleType === 'type' && r.message) {
|
|
45
|
+
resultSet.messages.push(r.message);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
// Set data on request object
|
|
50
|
+
if (isValid) {
|
|
51
|
+
// Set data on request object
|
|
52
|
+
if (jsonData) {
|
|
53
|
+
const dst = data;
|
|
54
|
+
data = (0, object_1.mergeObjects)(dst, jsonData);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
c.req.valid(keys, value);
|
|
32
58
|
}
|
|
33
59
|
}
|
|
34
|
-
|
|
60
|
+
}
|
|
61
|
+
if (!resultSet.hasError) {
|
|
62
|
+
Object.keys(data).map((key) => {
|
|
63
|
+
c.req.valid(key, data[key]);
|
|
64
|
+
});
|
|
35
65
|
}
|
|
36
66
|
if (options && options.done) {
|
|
37
67
|
const res = options.done(resultSet, c);
|
|
@@ -50,7 +80,13 @@ exports.validatorMiddleware = validatorMiddleware;
|
|
|
50
80
|
function getValidatorList(schema) {
|
|
51
81
|
const map = [];
|
|
52
82
|
for (const [key, value] of Object.entries(schema)) {
|
|
53
|
-
if (value instanceof validator_1.
|
|
83
|
+
if (value instanceof validator_1.VObjectBase) {
|
|
84
|
+
const validators = value.getValidators();
|
|
85
|
+
for (const validator of validators) {
|
|
86
|
+
map.push([value.keys, validator]);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else if (value instanceof validator_1.VBase) {
|
|
54
90
|
map.push([[key], value]);
|
|
55
91
|
}
|
|
56
92
|
else {
|