This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+49
View File
@@ -0,0 +1,49 @@
"use strict";
/*
* @adonisjs/shield
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.hstsFactory = void 0;
/// <reference path="../adonis-typings/index.ts" />
const helpers_1 = require("@poppinss/utils/build/helpers");
const noop_1 = require("./noop");
const DEFAULT_MAX_AGE = 180 * 24 * 60 * 60;
/**
* Normalizes the max age to a valid number
*/
function normalizeMaxAge(maxAge) {
if (maxAge === null || maxAge === undefined) {
return DEFAULT_MAX_AGE;
}
maxAge = (typeof maxAge === 'string' ? helpers_1.string.toMs(maxAge) : maxAge);
if (maxAge < 0) {
throw new Error('Max age for "shield.hsts" cannot be a negative value');
}
return maxAge;
}
/**
* Factory function that returns a new function to Add `Strict-Transport-Security`
* header based upon given user options.
*/
function hstsFactory(options) {
if (!options.enabled) {
return noop_1.noop;
}
const maxAge = normalizeMaxAge(options.maxAge);
let value = `max-age=${maxAge}`;
if (options.includeSubDomains) {
value += '; includeSubDomains';
}
if (options.preload) {
value += '; preload';
}
return function hsts({ response }) {
response.header('Strict-Transport-Security', value);
};
}
exports.hstsFactory = hstsFactory;