content-security-policy-builder
Advanced tools
Comparing version 0.2.0 to 1.0.0
30
index.js
@@ -1,32 +0,32 @@ | ||
var dashify = require("dashify"); | ||
var dashify = require('dashify') | ||
module.exports = function (options) { | ||
var directives = options.directives; | ||
var directives = options.directives | ||
var keysSeen = {}; | ||
var keysSeen = {} | ||
return Object.keys(directives).reduce(function (result, originalKey) { | ||
var directive = dashify(originalKey); | ||
var directive = dashify(originalKey) | ||
if (keysSeen[directive]) { | ||
throw new Error(originalKey + " is specified more than once"); | ||
throw new Error(originalKey + ' is specified more than once') | ||
} | ||
keysSeen[directive] = true; | ||
keysSeen[directive] = true | ||
var value = directives[originalKey]; | ||
var value = directives[originalKey] | ||
if (Array.isArray(value)) { | ||
value = value.join(" "); | ||
value = value.join(' ') | ||
} | ||
var combined; | ||
var combined | ||
if (value) { | ||
combined = directive + " " + value; | ||
combined = directive + ' ' + value | ||
} else { | ||
combined = directive; | ||
combined = directive | ||
} | ||
result.push(combined); | ||
result.push(combined) | ||
return result; | ||
}, []).join("; "); | ||
}; | ||
return result | ||
}, []).join('; ') | ||
} |
@@ -5,3 +5,3 @@ { | ||
"description": "Build Content Security Policy directives.", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
@@ -22,10 +22,17 @@ "keywords": [ | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "standard && mocha" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.3.0" | ||
"mocha": "^2.3.4", | ||
"standard": "^5.4.1" | ||
}, | ||
"dependencies": { | ||
"dashify": "^0.1.0" | ||
"dashify": "^0.2.0" | ||
}, | ||
"standard": { | ||
"globals": [ | ||
"describe", | ||
"it" | ||
] | ||
} | ||
} |
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) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) | ||
@@ -11,7 +13,7 @@ Take an object and turn it into a Content Security Policy string. Useful for building Content Security Policy libraries. | ||
```javascript | ||
var builder = require('content-security-policy-builder') | ||
var builder = require("content-security-policy-builder") | ||
// default-src 'self' default.com; script-src scripts.com; whatever-src something | ||
builder({ | ||
directive: { | ||
directives: { | ||
defaultSrc: ["'self'", "default.com"], | ||
@@ -18,0 +20,0 @@ scriptSrc: "scripts.com" |
@@ -1,66 +0,66 @@ | ||
var builder = require(".."); | ||
var builder = require('..') | ||
var assert = require("assert"); | ||
var assert = require('assert') | ||
describe("builder", function () { | ||
it("builds empty directives", function () { | ||
describe('builder', function () { | ||
it('builds no directives', function () { | ||
var result = builder({ | ||
directives: {} | ||
}); | ||
}) | ||
assert.equal(result, ""); | ||
}); | ||
assert.equal(result, '') | ||
}) | ||
it("builds directives with camelCased keys", function () { | ||
it('builds directives with camelCased keys', function () { | ||
var result = builder({ | ||
directives: { | ||
whatThe: "heck", | ||
whatThe: 'heck', | ||
defaultSrc: "'self'", | ||
playtimeIsOver: ["star", "fox"] | ||
playtimeIsOver: ['star', 'fox'] | ||
} | ||
}); | ||
}) | ||
var split = result.split("; ").sort(); | ||
var split = result.split('; ').sort() | ||
assert.equal(split.length, 3); | ||
assert.equal(split[0], "default-src 'self'"); | ||
assert.equal(split[1], "playtime-is-over star fox"); | ||
assert.equal(split[2], "what-the heck"); | ||
}); | ||
assert.equal(split.length, 3) | ||
assert.equal(split[0], "default-src 'self'") | ||
assert.equal(split[1], 'playtime-is-over star fox') | ||
assert.equal(split[2], 'what-the heck') | ||
}) | ||
it("builds directives with dash-separated keys", function () { | ||
it('builds directives with dash-separated keys', function () { | ||
var result = builder({ | ||
directives: { | ||
"do-a": "barrel roll", | ||
"default-src": "'self'", | ||
"andross-has-ordered-us": ["to", "take", "you", "down"] | ||
'do-a': 'barrel roll', | ||
'default-src': "'self'", | ||
'andross-has-ordered-us': ['to', 'take', 'you', 'down'] | ||
} | ||
}); | ||
}) | ||
var split = result.split("; ").sort(); | ||
var split = result.split('; ').sort() | ||
assert.equal(split.length, 3); | ||
assert.equal(split[0], "andross-has-ordered-us to take you down"); | ||
assert.equal(split[1], "default-src 'self'"); | ||
assert.equal(split[2], "do-a barrel roll"); | ||
}); | ||
assert.equal(split.length, 3) | ||
assert.equal(split[0], 'andross-has-ordered-us to take you down') | ||
assert.equal(split[1], "default-src 'self'") | ||
assert.equal(split[2], 'do-a barrel roll') | ||
}) | ||
it("builds directives with a mix of key types", function () { | ||
it('builds directives with a mix of key types', function () { | ||
var result = builder({ | ||
directives: { | ||
"hey-einstein": "i'm on your side", | ||
'hey-einstein': "i'm on your side", | ||
defaultSrc: "'self'", | ||
falco: ["lombardi"] | ||
falco: ['lombardi'] | ||
} | ||
}); | ||
}) | ||
var split = result.split("; ").sort(); | ||
var split = result.split('; ').sort() | ||
assert.equal(split.length, 3); | ||
assert.equal(split[0], "default-src 'self'"); | ||
assert.equal(split[1], "falco lombardi"); | ||
assert.equal(split[2], "hey-einstein i'm on your side"); | ||
}); | ||
assert.equal(split.length, 3) | ||
assert.equal(split[0], "default-src 'self'") | ||
assert.equal(split[1], 'falco lombardi') | ||
assert.equal(split[2], "hey-einstein i'm on your side") | ||
}) | ||
it("builds directives with empty values", function () { | ||
it('builds directives with empty values', function () { | ||
var result = builder({ | ||
@@ -72,13 +72,13 @@ directives: { | ||
} | ||
}); | ||
}) | ||
var split = result.split("; ").sort(); | ||
var split = result.split('; ').sort() | ||
assert.equal(split.length, 3); | ||
assert.equal(split[0], "cant"); | ||
assert.equal(split[1], "i"); | ||
assert.equal(split[2], "lose"); | ||
}); | ||
assert.equal(split.length, 3) | ||
assert.equal(split[0], 'cant') | ||
assert.equal(split[1], 'i') | ||
assert.equal(split[2], 'lose') | ||
}) | ||
it("throws errors when passed two keys of different types but the same names", function () { | ||
it('throws errors when passed two keys of different types but the same names', function () { | ||
assert.throws(function () { | ||
@@ -88,7 +88,7 @@ builder({ | ||
defaultSrc: "'self'", | ||
"default-src": "falco.biz" | ||
'default-src': 'falco.biz' | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}) | ||
}) | ||
}) | ||
}) |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5792
7
1
24
2
+ Addeddashify@0.2.2(transitive)
- Removeddashify@0.1.0(transitive)
Updateddashify@^0.2.0