-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
42 lines (36 loc) · 1.19 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
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
import { type Middleware, stringifyJfv, withHeader } from "./deps.ts";
import { assertValidNELPolicy } from "./utils.ts";
import { Header } from "./constants.ts";
import type { NELPolicy } from "./types.ts";
/** Create `NEL` header field middleware.
*
* @example
* ```ts
* import {
* type Handler,
* nel,
* } from "https://deno.land/x/nel_middleware@$VERSION/mod.ts";
* import { assert } from "https://deno.land/std/testing/asserts.ts";
*
* declare const request: Request;
* declare const handler: Handler;
*
* const middleware = nel({ report_to: "default", max_age: 86400 });
* const response = await middleware(request, handler);
*
* assert(response.headers.has("nel"));
* ```
*
* @throws {Error} If the endpoints include invalid member.
*/
export function nel(policy: NELPolicy): Middleware {
assertValidNELPolicy(policy);
const fieldValue = stringifyJfv([policy]);
return async (request, next) => {
const response = await next(request);
if (response.headers.has(Header.NEL)) return response;
return withHeader(response, Header.NEL, fieldValue);
};
}