@httpland/nel-middleware
Version:
HTTP network error logging(NEL) middleware
31 lines (30 loc) • 1.46 kB
JavaScript
;
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertUnitInterval = exports.assertNonNegativeInteger = exports.assertValidNELPolicy = void 0;
const deps_js_1 = require("./deps.js");
/** Assert the {@link NELPolicy} is valid.
* @throws {Error} If the policy include invalid field.
*/
function assertValidNELPolicy(policy) {
const { max_age, success_fraction, failure_fraction } = policy;
assertNonNegativeInteger(max_age, `max_age must be non-negative integer. ${max_age}`);
(0, deps_js_1.isNumber)(success_fraction) &&
assertUnitInterval(success_fraction, `success_fraction must be unit interval. ${success_fraction}`);
(0, deps_js_1.isNumber)(failure_fraction) &&
assertUnitInterval(failure_fraction, `failure_fraction must be unit interval. ${failure_fraction}`);
}
exports.assertValidNELPolicy = assertValidNELPolicy;
/** Assert the input is non-negative integer. */
function assertNonNegativeInteger(input, msg) {
if (!(0, deps_js_1.isNonNegativeInteger)(input))
throw Error(msg);
}
exports.assertNonNegativeInteger = assertNonNegativeInteger;
/** Assert the input is unit interval. */
function assertUnitInterval(input, msg) {
if (!(0, deps_js_1.isUnitInterval)(input))
throw Error(msg);
}
exports.assertUnitInterval = assertUnitInterval;