-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
43 lines (39 loc) · 1.24 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
import { Middleware, withHeader } from "./deps.ts";
import { CrossOriginResourcePolicy, PolicyHeader } from "./constants.ts";
/** Create `Cross-Origin-Resource-Policy` header middleware.
*
* Add `Cross-Origin-Resource-Policy` header field to `Response`.
* ```http
* Cross-Origin-Resource-Policy: same-origin
* ```
*
* @example
* ```ts
* import {
* corp,
* type Handler,
* } from "https://deno.land/x/corp_middleware@$VERSION/mod.ts";
* import { assert } from "https://deno.land/std/testing/asserts.ts";
*
* declare const request: Request;
* declare const handler: Handler;
*
* const middleware = corp();
* const response = await middleware(request, handler);
*
* assert(response.headers.has("cross-origin-resource-policy"));
* ```
*/
export function corp(
policy: `${CrossOriginResourcePolicy}` = CrossOriginResourcePolicy.SameOrigin,
): Middleware {
return async (request, next) => {
const response = await next(request);
if (response.headers.has(PolicyHeader.CrossOriginResourcePolicy)) {
return response;
}
return withHeader(response, PolicyHeader.CrossOriginResourcePolicy, policy);
};
}