csp-builder
Version:
A builder tool to help generate Content Security Policies in a type-safe way
21 lines (20 loc) • 649 B
JavaScript
export class Builder {
constructor(level = 3) {
this.directives = new Map();
this.level = level;
}
addDirective(directive) {
const directiveName = directive.getDirectiveName();
if (directive.getMinimumCspVersion() > this.level) {
throw new Error(`Directive ${directiveName} is not supported by CSP level of ${this.level}`);
}
this.directives.set(directiveName, directive);
return this;
}
stringify() {
return Array.from(this.directives.values())
.map((directive) => directive.serialize())
.join(' ')
.trim();
}
}