hono 4.6.19 → 4.7.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/adapter/lambda-edge/handler.js +6 -2
- package/dist/cjs/adapter/lambda-edge/handler.js +6 -2
- package/dist/cjs/helper/accepts/accepts.js +4 -26
- package/dist/cjs/helper/factory/index.js +6 -2
- package/dist/cjs/helper/proxy/index.js +75 -0
- package/dist/cjs/helper/streaming/sse.js +8 -5
- package/dist/cjs/helper/streaming/stream.js +8 -5
- package/dist/cjs/helper/streaming/utils.js +36 -0
- package/dist/cjs/middleware/etag/digest.js +2 -7
- package/dist/cjs/middleware/etag/index.js +7 -2
- package/dist/cjs/middleware/jwk/index.js +28 -0
- package/dist/cjs/middleware/jwk/jwk.js +124 -0
- package/dist/cjs/middleware/language/index.js +36 -0
- package/dist/cjs/middleware/language/language.js +210 -0
- package/dist/cjs/middleware/logger/index.js +2 -3
- package/dist/cjs/request.js +1 -1
- package/dist/cjs/router/linear-router/router.js +3 -1
- package/dist/cjs/router/trie-router/node.js +24 -11
- package/dist/cjs/utils/accept.js +86 -0
- package/dist/cjs/utils/jwt/index.js +1 -1
- package/dist/cjs/utils/jwt/jwt.js +56 -3
- package/dist/cjs/utils/jwt/types.js +8 -0
- package/dist/cjs/utils/url.js +6 -5
- package/dist/helper/accepts/accepts.js +2 -23
- package/dist/helper/factory/index.js +6 -2
- package/dist/helper/proxy/index.js +52 -0
- package/dist/helper/streaming/sse.js +8 -5
- package/dist/helper/streaming/stream.js +8 -5
- package/dist/helper/streaming/utils.js +13 -0
- package/dist/middleware/etag/digest.js +2 -7
- package/dist/middleware/etag/index.js +7 -2
- package/dist/middleware/jwk/index.js +5 -0
- package/dist/middleware/jwk/jwk.js +101 -0
- package/dist/middleware/language/index.js +15 -0
- package/dist/middleware/language/language.js +178 -0
- package/dist/middleware/logger/index.js +2 -3
- package/dist/request.js +1 -1
- package/dist/router/linear-router/router.js +3 -1
- package/dist/router/trie-router/node.js +24 -11
- package/dist/types/client/types.d.ts +1 -2
- package/dist/types/context.d.ts +2 -2
- package/dist/types/helper/accepts/accepts.d.ts +0 -1
- package/dist/types/helper/adapter/index.d.ts +1 -1
- package/dist/types/helper/factory/index.d.ts +4 -1
- package/dist/types/helper/proxy/index.d.ts +54 -0
- package/dist/types/helper/streaming/utils.d.ts +1 -0
- package/dist/types/middleware/etag/digest.d.ts +1 -1
- package/dist/types/middleware/etag/index.d.ts +4 -0
- package/dist/types/middleware/jwk/index.d.ts +1 -0
- package/dist/types/middleware/jwk/jwk.d.ts +40 -0
- package/dist/types/middleware/language/index.d.ts +7 -0
- package/dist/types/middleware/language/language.d.ts +102 -0
- package/dist/types/utils/accept.d.ts +11 -0
- package/dist/types/utils/jwt/index.d.ts +4 -0
- package/dist/types/utils/jwt/jws.d.ts +4 -1
- package/dist/types/utils/jwt/jwt.d.ts +7 -1
- package/dist/types/utils/jwt/types.d.ts +4 -0
- package/dist/types/utils/url.d.ts +1 -1
- package/dist/utils/accept.js +63 -0
- package/dist/utils/jwt/index.js +2 -2
- package/dist/utils/jwt/jwt.js +54 -2
- package/dist/utils/jwt/types.js +7 -0
- package/dist/utils/url.js +6 -5
- package/package.json +26 -5
- package/perf-measures/bundle-check/generated/.gitkeep +0 -0
- package/perf-measures/type-check/generated/.gitkeep +0 -0
|
@@ -5,7 +5,10 @@ globalThis.crypto ??= crypto;
|
|
|
5
5
|
var convertHeaders = (headers) => {
|
|
6
6
|
const cfHeaders = {};
|
|
7
7
|
headers.forEach((value, key) => {
|
|
8
|
-
cfHeaders[key.toLowerCase()] = [
|
|
8
|
+
cfHeaders[key.toLowerCase()] = [
|
|
9
|
+
...cfHeaders[key.toLowerCase()] || [],
|
|
10
|
+
{ key: key.toLowerCase(), value }
|
|
11
|
+
];
|
|
9
12
|
});
|
|
10
13
|
return cfHeaders;
|
|
11
14
|
};
|
|
@@ -36,7 +39,8 @@ var createResult = async (res) => {
|
|
|
36
39
|
};
|
|
37
40
|
var createRequest = (event) => {
|
|
38
41
|
const queryString = event.Records[0].cf.request.querystring;
|
|
39
|
-
const
|
|
42
|
+
const host = event.Records[0].cf.request.headers?.host?.[0]?.value || event.Records[0].cf.config.distributionDomainName;
|
|
43
|
+
const urlPath = `https://${host}${event.Records[0].cf.request.uri}`;
|
|
40
44
|
const url = queryString ? `${urlPath}?${queryString}` : urlPath;
|
|
41
45
|
const headers = new Headers();
|
|
42
46
|
Object.entries(event.Records[0].cf.request.headers).forEach(([k, v]) => {
|
|
@@ -35,7 +35,10 @@ globalThis.crypto ??= import_node_crypto.default;
|
|
|
35
35
|
const convertHeaders = (headers) => {
|
|
36
36
|
const cfHeaders = {};
|
|
37
37
|
headers.forEach((value, key) => {
|
|
38
|
-
cfHeaders[key.toLowerCase()] = [
|
|
38
|
+
cfHeaders[key.toLowerCase()] = [
|
|
39
|
+
...cfHeaders[key.toLowerCase()] || [],
|
|
40
|
+
{ key: key.toLowerCase(), value }
|
|
41
|
+
];
|
|
39
42
|
});
|
|
40
43
|
return cfHeaders;
|
|
41
44
|
};
|
|
@@ -66,7 +69,8 @@ const createResult = async (res) => {
|
|
|
66
69
|
};
|
|
67
70
|
const createRequest = (event) => {
|
|
68
71
|
const queryString = event.Records[0].cf.request.querystring;
|
|
69
|
-
const
|
|
72
|
+
const host = event.Records[0].cf.request.headers?.host?.[0]?.value || event.Records[0].cf.config.distributionDomainName;
|
|
73
|
+
const urlPath = `https://${host}${event.Records[0].cf.request.uri}`;
|
|
70
74
|
const url = queryString ? `${urlPath}?${queryString}` : urlPath;
|
|
71
75
|
const headers = new Headers();
|
|
72
76
|
Object.entries(event.Records[0].cf.request.headers).forEach(([k, v]) => {
|
|
@@ -19,31 +19,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var accepts_exports = {};
|
|
20
20
|
__export(accepts_exports, {
|
|
21
21
|
accepts: () => accepts,
|
|
22
|
-
defaultMatch: () => defaultMatch
|
|
23
|
-
parseAccept: () => parseAccept
|
|
22
|
+
defaultMatch: () => defaultMatch
|
|
24
23
|
});
|
|
25
24
|
module.exports = __toCommonJS(accepts_exports);
|
|
26
|
-
|
|
27
|
-
const accepts2 = acceptHeader.split(",");
|
|
28
|
-
return accepts2.map((accept) => {
|
|
29
|
-
const parts = accept.trim().split(";");
|
|
30
|
-
const type = parts[0];
|
|
31
|
-
const params = parts.slice(1);
|
|
32
|
-
const q = params.find((param) => param.startsWith("q="));
|
|
33
|
-
const paramsObject = params.reduce((acc, param) => {
|
|
34
|
-
const keyValue = param.split("=");
|
|
35
|
-
const key = keyValue[0].trim();
|
|
36
|
-
const value = keyValue[1].trim();
|
|
37
|
-
acc[key] = value;
|
|
38
|
-
return acc;
|
|
39
|
-
}, {});
|
|
40
|
-
return {
|
|
41
|
-
type,
|
|
42
|
-
params: paramsObject,
|
|
43
|
-
q: q ? parseFloat(q.split("=")[1]) : 1
|
|
44
|
-
};
|
|
45
|
-
});
|
|
46
|
-
};
|
|
25
|
+
var import_accept = require("../../utils/accept");
|
|
47
26
|
const defaultMatch = (accepts2, config) => {
|
|
48
27
|
const { supports, default: defaultSupport } = config;
|
|
49
28
|
const accept = accepts2.sort((a, b) => b.q - a.q).find((accept2) => supports.includes(accept2.type));
|
|
@@ -54,13 +33,12 @@ const accepts = (c, options) => {
|
|
|
54
33
|
if (!acceptHeader) {
|
|
55
34
|
return options.default;
|
|
56
35
|
}
|
|
57
|
-
const accepts2 = parseAccept(acceptHeader);
|
|
36
|
+
const accepts2 = (0, import_accept.parseAccept)(acceptHeader);
|
|
58
37
|
const match = options.match || defaultMatch;
|
|
59
38
|
return match(accepts2, options);
|
|
60
39
|
};
|
|
61
40
|
// Annotate the CommonJS export names for ESM import in node:
|
|
62
41
|
0 && (module.exports = {
|
|
63
42
|
accepts,
|
|
64
|
-
defaultMatch
|
|
65
|
-
parseAccept
|
|
43
|
+
defaultMatch
|
|
66
44
|
});
|
|
@@ -26,11 +26,15 @@ module.exports = __toCommonJS(factory_exports);
|
|
|
26
26
|
var import_hono = require("../../hono");
|
|
27
27
|
class Factory {
|
|
28
28
|
initApp;
|
|
29
|
+
#defaultAppOptions;
|
|
29
30
|
constructor(init) {
|
|
30
31
|
this.initApp = init?.initApp;
|
|
32
|
+
this.#defaultAppOptions = init?.defaultAppOptions;
|
|
31
33
|
}
|
|
32
|
-
createApp = () => {
|
|
33
|
-
const app = new import_hono.Hono(
|
|
34
|
+
createApp = (options) => {
|
|
35
|
+
const app = new import_hono.Hono(
|
|
36
|
+
options && this.#defaultAppOptions ? { ...this.#defaultAppOptions, ...options } : options ?? this.#defaultAppOptions
|
|
37
|
+
);
|
|
34
38
|
if (this.initApp) {
|
|
35
39
|
this.initApp(app);
|
|
36
40
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
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
|
+
var proxy_exports = {};
|
|
20
|
+
__export(proxy_exports, {
|
|
21
|
+
proxy: () => proxy
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(proxy_exports);
|
|
24
|
+
const hopByHopHeaders = [
|
|
25
|
+
"connection",
|
|
26
|
+
"keep-alive",
|
|
27
|
+
"proxy-authenticate",
|
|
28
|
+
"proxy-authorization",
|
|
29
|
+
"te",
|
|
30
|
+
"trailers",
|
|
31
|
+
"transfer-encoding"
|
|
32
|
+
];
|
|
33
|
+
const buildRequestInitFromRequest = (request) => {
|
|
34
|
+
if (!request) {
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
const headers = new Headers(request.headers);
|
|
38
|
+
hopByHopHeaders.forEach((header) => {
|
|
39
|
+
headers.delete(header);
|
|
40
|
+
});
|
|
41
|
+
return {
|
|
42
|
+
method: request.method,
|
|
43
|
+
body: request.body,
|
|
44
|
+
duplex: request.body ? "half" : void 0,
|
|
45
|
+
headers
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
const proxy = async (input, proxyInit) => {
|
|
49
|
+
const { raw, ...requestInit } = proxyInit ?? {};
|
|
50
|
+
const req = new Request(
|
|
51
|
+
input,
|
|
52
|
+
{
|
|
53
|
+
...buildRequestInitFromRequest(raw),
|
|
54
|
+
...requestInit
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
req.headers.delete("accept-encoding");
|
|
58
|
+
const res = await fetch(req);
|
|
59
|
+
const resHeaders = new Headers(res.headers);
|
|
60
|
+
hopByHopHeaders.forEach((header) => {
|
|
61
|
+
resHeaders.delete(header);
|
|
62
|
+
});
|
|
63
|
+
if (resHeaders.has("content-encoding")) {
|
|
64
|
+
resHeaders.delete("content-encoding");
|
|
65
|
+
resHeaders.delete("content-length");
|
|
66
|
+
}
|
|
67
|
+
return new Response(res.body, {
|
|
68
|
+
...res,
|
|
69
|
+
headers: resHeaders
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
73
|
+
0 && (module.exports = {
|
|
74
|
+
proxy
|
|
75
|
+
});
|
|
@@ -24,6 +24,7 @@ __export(sse_exports, {
|
|
|
24
24
|
module.exports = __toCommonJS(sse_exports);
|
|
25
25
|
var import_html = require("../../utils/html");
|
|
26
26
|
var import_stream = require("../../utils/stream");
|
|
27
|
+
var import_utils = require("./utils");
|
|
27
28
|
class SSEStreamingApi extends import_stream.StreamingApi {
|
|
28
29
|
constructor(writable, readable) {
|
|
29
30
|
super(writable, readable);
|
|
@@ -63,11 +64,13 @@ const contextStash = /* @__PURE__ */ new WeakMap();
|
|
|
63
64
|
const streamSSE = (c, cb, onError) => {
|
|
64
65
|
const { readable, writable } = new TransformStream();
|
|
65
66
|
const stream = new SSEStreamingApi(writable, readable);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
stream.
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
if ((0, import_utils.isOldBunVersion)()) {
|
|
68
|
+
c.req.raw.signal.addEventListener("abort", () => {
|
|
69
|
+
if (!stream.closed) {
|
|
70
|
+
stream.abort();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
71
74
|
contextStash.set(stream.responseReadable, c);
|
|
72
75
|
c.header("Transfer-Encoding", "chunked");
|
|
73
76
|
c.header("Content-Type", "text/event-stream");
|
|
@@ -22,15 +22,18 @@ __export(stream_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(stream_exports);
|
|
24
24
|
var import_stream = require("../../utils/stream");
|
|
25
|
+
var import_utils = require("./utils");
|
|
25
26
|
const contextStash = /* @__PURE__ */ new WeakMap();
|
|
26
27
|
const stream = (c, cb, onError) => {
|
|
27
28
|
const { readable, writable } = new TransformStream();
|
|
28
29
|
const stream2 = new import_stream.StreamingApi(writable, readable);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
stream2.
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
if ((0, import_utils.isOldBunVersion)()) {
|
|
31
|
+
c.req.raw.signal.addEventListener("abort", () => {
|
|
32
|
+
if (!stream2.closed) {
|
|
33
|
+
stream2.abort();
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
34
37
|
contextStash.set(stream2.responseReadable, c);
|
|
35
38
|
(async () => {
|
|
36
39
|
try {
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
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
|
+
var utils_exports = {};
|
|
20
|
+
__export(utils_exports, {
|
|
21
|
+
isOldBunVersion: () => isOldBunVersion
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(utils_exports);
|
|
24
|
+
let isOldBunVersion = () => {
|
|
25
|
+
const version = typeof Bun !== "undefined" ? Bun.version : void 0;
|
|
26
|
+
if (version === void 0) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
const result = version.startsWith("1.1") || version.startsWith("1.0") || version.startsWith("0.");
|
|
30
|
+
isOldBunVersion = () => result;
|
|
31
|
+
return result;
|
|
32
|
+
};
|
|
33
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
+
0 && (module.exports = {
|
|
35
|
+
isOldBunVersion
|
|
36
|
+
});
|
|
@@ -30,7 +30,7 @@ const mergeBuffers = (buffer1, buffer2) => {
|
|
|
30
30
|
merged.set(buffer2, buffer1.byteLength);
|
|
31
31
|
return merged;
|
|
32
32
|
};
|
|
33
|
-
const generateDigest = async (stream) => {
|
|
33
|
+
const generateDigest = async (stream, generator) => {
|
|
34
34
|
if (!stream || !crypto || !crypto.subtle) {
|
|
35
35
|
return null;
|
|
36
36
|
}
|
|
@@ -41,12 +41,7 @@ const generateDigest = async (stream) => {
|
|
|
41
41
|
if (done) {
|
|
42
42
|
break;
|
|
43
43
|
}
|
|
44
|
-
result = await
|
|
45
|
-
{
|
|
46
|
-
name: "SHA-1"
|
|
47
|
-
},
|
|
48
|
-
mergeBuffers(result, value)
|
|
49
|
-
);
|
|
44
|
+
result = await generator(mergeBuffers(result, value));
|
|
50
45
|
}
|
|
51
46
|
if (!result) {
|
|
52
47
|
return null;
|
|
@@ -37,20 +37,25 @@ function etagMatches(etag2, ifNoneMatch) {
|
|
|
37
37
|
const etag = (options) => {
|
|
38
38
|
const retainedHeaders = options?.retainedHeaders ?? RETAINED_304_HEADERS;
|
|
39
39
|
const weak = options?.weak ?? false;
|
|
40
|
+
const generator = options?.generateDigest ?? ((body) => crypto.subtle.digest(
|
|
41
|
+
{
|
|
42
|
+
name: "SHA-1"
|
|
43
|
+
},
|
|
44
|
+
body
|
|
45
|
+
));
|
|
40
46
|
return async function etag2(c, next) {
|
|
41
47
|
const ifNoneMatch = c.req.header("If-None-Match") ?? null;
|
|
42
48
|
await next();
|
|
43
49
|
const res = c.res;
|
|
44
50
|
let etag3 = res.headers.get("ETag");
|
|
45
51
|
if (!etag3) {
|
|
46
|
-
const hash = await (0, import_digest.generateDigest)(res.clone().body);
|
|
52
|
+
const hash = await (0, import_digest.generateDigest)(res.clone().body, generator);
|
|
47
53
|
if (hash === null) {
|
|
48
54
|
return;
|
|
49
55
|
}
|
|
50
56
|
etag3 = weak ? `W/"${hash}"` : `"${hash}"`;
|
|
51
57
|
}
|
|
52
58
|
if (etagMatches(etag3, ifNoneMatch)) {
|
|
53
|
-
await c.res.blob();
|
|
54
59
|
c.res = new Response(null, {
|
|
55
60
|
status: 304,
|
|
56
61
|
statusText: "Not Modified",
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
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
|
+
var jwk_exports = {};
|
|
20
|
+
__export(jwk_exports, {
|
|
21
|
+
jwk: () => import_jwk.jwk
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(jwk_exports);
|
|
24
|
+
var import_jwk = require("./jwk");
|
|
25
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
26
|
+
0 && (module.exports = {
|
|
27
|
+
jwk
|
|
28
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
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
|
+
var jwk_exports = {};
|
|
20
|
+
__export(jwk_exports, {
|
|
21
|
+
jwk: () => jwk
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(jwk_exports);
|
|
24
|
+
var import_cookie = require("../../helper/cookie");
|
|
25
|
+
var import_http_exception = require("../../http-exception");
|
|
26
|
+
var import_jwt = require("../../utils/jwt");
|
|
27
|
+
var import_context = require("../../context");
|
|
28
|
+
const jwk = (options, init) => {
|
|
29
|
+
if (!options || !(options.keys || options.jwks_uri)) {
|
|
30
|
+
throw new Error('JWK auth middleware requires options for either "keys" or "jwks_uri" or both');
|
|
31
|
+
}
|
|
32
|
+
if (!crypto.subtle || !crypto.subtle.importKey) {
|
|
33
|
+
throw new Error("`crypto.subtle.importKey` is undefined. JWK auth middleware requires it.");
|
|
34
|
+
}
|
|
35
|
+
return async function jwk2(ctx, next) {
|
|
36
|
+
const credentials = ctx.req.raw.headers.get("Authorization");
|
|
37
|
+
let token;
|
|
38
|
+
if (credentials) {
|
|
39
|
+
const parts = credentials.split(/\s+/);
|
|
40
|
+
if (parts.length !== 2) {
|
|
41
|
+
const errDescription = "invalid credentials structure";
|
|
42
|
+
throw new import_http_exception.HTTPException(401, {
|
|
43
|
+
message: errDescription,
|
|
44
|
+
res: unauthorizedResponse({
|
|
45
|
+
ctx,
|
|
46
|
+
error: "invalid_request",
|
|
47
|
+
errDescription
|
|
48
|
+
})
|
|
49
|
+
});
|
|
50
|
+
} else {
|
|
51
|
+
token = parts[1];
|
|
52
|
+
}
|
|
53
|
+
} else if (options.cookie) {
|
|
54
|
+
if (typeof options.cookie == "string") {
|
|
55
|
+
token = (0, import_cookie.getCookie)(ctx, options.cookie);
|
|
56
|
+
} else if (options.cookie.secret) {
|
|
57
|
+
if (options.cookie.prefixOptions) {
|
|
58
|
+
token = await (0, import_cookie.getSignedCookie)(
|
|
59
|
+
ctx,
|
|
60
|
+
options.cookie.secret,
|
|
61
|
+
options.cookie.key,
|
|
62
|
+
options.cookie.prefixOptions
|
|
63
|
+
);
|
|
64
|
+
} else {
|
|
65
|
+
token = await (0, import_cookie.getSignedCookie)(ctx, options.cookie.secret, options.cookie.key);
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
if (options.cookie.prefixOptions) {
|
|
69
|
+
token = (0, import_cookie.getCookie)(ctx, options.cookie.key, options.cookie.prefixOptions);
|
|
70
|
+
} else {
|
|
71
|
+
token = (0, import_cookie.getCookie)(ctx, options.cookie.key);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (!token) {
|
|
76
|
+
const errDescription = "no authorization included in request";
|
|
77
|
+
throw new import_http_exception.HTTPException(401, {
|
|
78
|
+
message: errDescription,
|
|
79
|
+
res: unauthorizedResponse({
|
|
80
|
+
ctx,
|
|
81
|
+
error: "invalid_request",
|
|
82
|
+
errDescription
|
|
83
|
+
})
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
let payload;
|
|
87
|
+
let cause;
|
|
88
|
+
try {
|
|
89
|
+
payload = await import_jwt.Jwt.verifyFromJwks(token, options, init);
|
|
90
|
+
} catch (e) {
|
|
91
|
+
cause = e;
|
|
92
|
+
}
|
|
93
|
+
if (!payload) {
|
|
94
|
+
if (cause instanceof Error && cause.constructor === Error) {
|
|
95
|
+
throw cause;
|
|
96
|
+
}
|
|
97
|
+
throw new import_http_exception.HTTPException(401, {
|
|
98
|
+
message: "Unauthorized",
|
|
99
|
+
res: unauthorizedResponse({
|
|
100
|
+
ctx,
|
|
101
|
+
error: "invalid_token",
|
|
102
|
+
statusText: "Unauthorized",
|
|
103
|
+
errDescription: "token verification failure"
|
|
104
|
+
}),
|
|
105
|
+
cause
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
ctx.set("jwtPayload", payload);
|
|
109
|
+
await next();
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
function unauthorizedResponse(opts) {
|
|
113
|
+
return new Response("Unauthorized", {
|
|
114
|
+
status: 401,
|
|
115
|
+
statusText: opts.statusText,
|
|
116
|
+
headers: {
|
|
117
|
+
"WWW-Authenticate": `Bearer realm="${opts.ctx.req.url}",error="${opts.error}",error_description="${opts.errDescription}"`
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
122
|
+
0 && (module.exports = {
|
|
123
|
+
jwk
|
|
124
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
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
|
+
var language_exports = {};
|
|
20
|
+
__export(language_exports, {
|
|
21
|
+
detectFromCookie: () => import_language.detectFromCookie,
|
|
22
|
+
detectFromHeader: () => import_language.detectFromHeader,
|
|
23
|
+
detectFromPath: () => import_language.detectFromPath,
|
|
24
|
+
detectFromQuery: () => import_language.detectFromQuery,
|
|
25
|
+
languageDetector: () => import_language.languageDetector
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(language_exports);
|
|
28
|
+
var import_language = require("./language");
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
detectFromCookie,
|
|
32
|
+
detectFromHeader,
|
|
33
|
+
detectFromPath,
|
|
34
|
+
detectFromQuery,
|
|
35
|
+
languageDetector
|
|
36
|
+
});
|