@jackdbd/eleventy-plugin-permissions-policy
Version:
Eleventy plugin that writes Permissions-Policy and Feature-Policy headers to a `_headers` file when Eleventy builds your site.
46 lines • 1.41 kB
JavaScript
import fs from 'node:fs';
import defDebug from 'debug';
import { DEBUG_PREFIX } from './constants.js';
const debug = defDebug(`${DEBUG_PREFIX}:utils`);
/**
* Converts a Feature-Policy directive to a string.
*
* @param {Directive} d
*/
export const featurePolicyDirectiveMapper = (d) => {
const allowlist = d.allowlist === undefined || d.allowlist.length === 0
? `'none'`
: d.allowlist
.map((s) => {
return s === '*' ? s : `'${s}'`;
})
.join(' ');
return `${d.feature} ${allowlist}`;
};
/**
* Converts a Permissions-Policy directive to a string.
*
* @param {Directive} d
*/
export const permissionsPolicyDirectiveMapper = (d) => {
const allowlist = d.allowlist === undefined || d.allowlist.length === 0
? ''
: d.allowlist
.map((s) => {
return s.includes('http') ? `"${s}"` : s;
})
.join(' ');
if (allowlist === '*') {
return `${d.feature}=*`;
}
else {
return `${d.feature}=(${allowlist})`;
}
};
export const appendToHeadersFile = (headerKey, headerValue, headersFilepath, patterns) => {
patterns.forEach((pattern) => {
debug(`add ${headerKey} header for resources matching ${pattern}`);
fs.appendFileSync(headersFilepath, `\n${pattern}\n ${headerKey}: ${headerValue}\n`);
});
};
//# sourceMappingURL=utils.js.map