UNPKG

@httpland/nel-middleware

Version:

HTTP network error logging(NEL) middleware

41 lines (40 loc) 1.34 kB
"use strict"; // Copyright 2023-latest the httpland authors. All rights reserved. MIT license. // This module is browser compatible. Object.defineProperty(exports, "__esModule", { value: true }); exports.nel = void 0; const deps_js_1 = require("./deps.js"); const utils_js_1 = require("./utils.js"); const constants_js_1 = require("./constants.js"); /** 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. */ function nel(policy) { (0, utils_js_1.assertValidNELPolicy)(policy); const fieldValue = (0, deps_js_1.stringifyJfv)([policy]); return async (request, next) => { const response = await next(request); if (response.headers.has(constants_js_1.Header.NEL)) return response; return (0, deps_js_1.withHeader)(response, constants_js_1.Header.NEL, fieldValue); }; } exports.nel = nel;