content-security-policy-builder
Advanced tools
Comparing version 2.1.0 to 2.1.1
# Changelog | ||
## 2.1.1 - 2022-03-23 | ||
### Changed | ||
- Fixed bug where names on `Object.prototype` didn't work | ||
- Marked inputs as `Readonly` (TypeScript-only) | ||
- Shrink package size a bit | ||
## 2.1.0 - 2019-06-13 | ||
### Added | ||
- Added TypeScript type definitions. See [#6](https://github.com/helmetjs/content-security-policy-builder/issues/6) | ||
@@ -9,4 +19,5 @@ - Created a changelog | ||
### Changed | ||
- Excluded useless files from npm package | ||
This changelog was started after the release of version 2.1.0. |
interface PolicyBuilderOptions { | ||
directives: { | ||
[directive: string]: string[] | string | boolean; | ||
}; | ||
directives: Readonly<Record<string, string[] | string | boolean>>; | ||
} | ||
declare const _default: ({ directives }: PolicyBuilderOptions) => string; | ||
declare const _default: ({ directives }: Readonly<PolicyBuilderOptions>) => string; | ||
export = _default; |
"use strict"; | ||
function dashify(str) { | ||
return str | ||
.replace(/([a-z])([A-Z])/g, '$1-$2') | ||
.toLowerCase(); | ||
} | ||
module.exports = function (_a) { | ||
var directives = _a.directives; | ||
var keysSeen = {}; | ||
return Object.keys(directives).reduce(function (result, originalKey) { | ||
var directive = dashify(originalKey); | ||
if (keysSeen[directive]) { | ||
throw new Error(originalKey + " is specified more than once"); | ||
var namesSeen = new Set(); | ||
var result = []; | ||
Object.keys(directives).forEach(function (originalName) { | ||
var name = originalName.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(); | ||
if (namesSeen.has(name)) { | ||
throw new Error("".concat(originalName, " is specified more than once")); | ||
} | ||
keysSeen[directive] = true; | ||
var value = directives[originalKey]; | ||
namesSeen.add(name); | ||
var value = directives[originalName]; | ||
if (Array.isArray(value)) { | ||
value = value.join(' '); | ||
value = value.join(" "); | ||
} | ||
else if (value === true) { | ||
value = ''; | ||
value = ""; | ||
} | ||
else if (value === false) { | ||
return result; | ||
} | ||
if (value) { | ||
return result.concat(directive + " " + value); | ||
result.push("".concat(name, " ").concat(value)); | ||
} | ||
else { | ||
return result.concat(directive); | ||
else if (value !== false) { | ||
result.push(name); | ||
} | ||
}, []).join('; '); | ||
}); | ||
return result.join("; "); | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "Build Content Security Policy directives.", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"license": "MIT", | ||
@@ -40,17 +40,20 @@ "keywords": [ | ||
"prepublishOnly": "npm run build", | ||
"lint": "eslint --fix '**/*.ts'", | ||
"test": "jest --config test/jest-config.json", | ||
"lint": "npm run lint:eslint && npm run lint:prettier", | ||
"lint:eslint": "eslint .", | ||
"lint:prettier": "prettier --check .", | ||
"format": "prettier --write .", | ||
"clean": "rm -rf dist", | ||
"build": "npm run clean && tsc" | ||
"build": "npm run clean && tsc", | ||
"test": "jest --config test/jest-config.json" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^24.0.14", | ||
"@typescript-eslint/eslint-plugin": "^1.9.0", | ||
"@typescript-eslint/parser": "^1.9.0", | ||
"eslint": "^5.16.0", | ||
"eslint-config-helmet": "^0.2.0", | ||
"jest": "^24.8.0", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "^3.5.2" | ||
"@types/jest": "^27.4.1", | ||
"@typescript-eslint/eslint-plugin": "^5.16.0", | ||
"@typescript-eslint/parser": "^5.16.0", | ||
"eslint": "^8.11.0", | ||
"jest": "^27.5.1", | ||
"prettier": "^2.6.0", | ||
"ts-jest": "^27.1.3", | ||
"typescript": "^4.6.2" | ||
} | ||
} |
@@ -1,6 +0,6 @@ | ||
Content Security Policy builder | ||
=============================== | ||
# Content Security Policy builder | ||
[![Build Status](https://travis-ci.org/helmetjs/content-security-policy-builder.svg?branch=master)](https://travis-ci.org/helmetjs/content-security-policy-builder) | ||
Take an object and turn it into a Content Security Policy string. Useful for building Content Security Policy libraries. | ||
Take an object and turn it into a Content Security Policy string. | ||
@@ -12,3 +12,3 @@ It can handle a lot of things you can you throw at it; `camelCased` or `dash-separated` directives, arrays or strings, et cetera. | ||
```javascript | ||
const builder = require('content-security-policy-builder') | ||
const builder = require("content-security-policy-builder"); | ||
@@ -18,8 +18,10 @@ // default-src 'self' default.com; script-src scripts.com; whatever-src something; object-src | ||
directives: { | ||
defaultSrc: ["'self'", 'default.com'], | ||
scriptSrc: 'scripts.com', | ||
'whatever-src': 'something', | ||
objectSrc: true | ||
} | ||
}) | ||
defaultSrc: ["'self'", "default.com"], | ||
scriptSrc: "scripts.com", | ||
"whatever-src": "something", | ||
objectSrc: true, | ||
}, | ||
}); | ||
``` | ||
This module is considered "complete". I expect to continue maintenance if needed, but I don't plan to add features or make breaking changes. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5018
26
32