Skip to content

Commit 5772122

Browse files
committed
1.Strict_Transport_Security.2.中间件不再处理websocket
1 parent ced4987 commit 5772122

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

deno.json

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"importMap": "./import_map.json",
23
"lock": false,
34
"tasks": {
45
"check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx",
@@ -26,17 +27,6 @@
2627
"exclude": [
2728
"**/_fresh/*"
2829
],
29-
"imports": {
30-
"$fresh/": "https://deno.land/x/[email protected]/",
31-
"preact": "https://esm.sh/[email protected]",
32-
"preact/": "https://esm.sh/[email protected]/",
33-
"@preact/signals": "https://esm.sh/*@preact/[email protected]",
34-
"@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
35-
"tailwindcss": "npm:[email protected]",
36-
"tailwindcss/": "npm:/[email protected]/",
37-
"tailwindcss/plugin": "npm:/[email protected]/plugin.js",
38-
"$std/": "https://deno.land/[email protected]/"
39-
},
4030
"compilerOptions": {
4131
"jsx": "react-jsx",
4232
"jsxImportSource": "preact"

import_map.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"imports": {
3+
"$fresh/": "https://deno.land/x/[email protected]/",
4+
"preact": "https://esm.sh/[email protected]",
5+
"preact/": "https://esm.sh/[email protected]/",
6+
"@preact/signals": "https://esm.sh/*@preact/[email protected]",
7+
"@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
8+
"tailwindcss": "npm:[email protected]",
9+
"tailwindcss/": "npm:/[email protected]/",
10+
"tailwindcss/plugin": "npm:/[email protected]/plugin.js",
11+
"$std/": "https://deno.land/[email protected]/",
12+
"@hattip/response": "npm:@hattip/[email protected]"
13+
}
14+
}

middleware.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
bodyToBuffer,
3+
} from "https://cdn.jsdelivr.net/gh/masx200/[email protected]/mod.ts";
4+
import { DenoMiddleWare } from "./DenoMiddleWare.ts";
15
/**
26
* 定义一个适用于Deno服务器中间件的类型。
37
*
@@ -147,7 +151,6 @@ export async function middlewareMain(
147151
);
148152
return resp;
149153
}
150-
import { DenoMiddleWare } from "./DenoMiddleWare.ts";
151154
/**
152155
* 主要中间件处理函数
153156
*
@@ -224,9 +227,14 @@ export async function reverse_proxy(
224227
export default async function (
225228
...[request, info, next]: Parameters<DenoMiddleWare>
226229
): Promise<Response> {
230+
if (request.headers.get("upgrade") == "websocket") {
231+
return await next();
232+
}
227233
try {
228-
return await middlewareLogger(request, info, async () => {
229-
return await middlewareMain(request, info, next);
234+
return await Strict_Transport_Security(request, info, async () => {
235+
return await middlewareLogger(request, info, async () => {
236+
return await middlewareMain(request, info, next);
237+
});
230238
});
231239
} catch (error) {
232240
console.error(error);
@@ -238,3 +246,21 @@ export default async function (
238246
);
239247
}
240248
}
249+
export async function Strict_Transport_Security(
250+
...[_request, _info, next]: Parameters<DenoMiddleWare>
251+
): Promise<Response> {
252+
// console.log(2);
253+
const response = await next();
254+
const headers = new Headers(response.headers);
255+
256+
headers.set("Strict-Transport-Security", "max-age=31536000");
257+
// console.log(ctx.response.body);
258+
// 必须把响应的主体转换为Uint8Array才行
259+
const body = response.body && (await bodyToBuffer(response.body));
260+
// headers.delete("content-length");
261+
const res = new Response(body, {
262+
status: response.status,
263+
headers,
264+
});
265+
return res;
266+
}

0 commit comments

Comments
 (0)