Skip to content

Commit

Permalink
Update code now that Node 18+ is required
Browse files Browse the repository at this point in the history
We can now use ES6 features.
  • Loading branch information
EvanHahn committed Oct 10, 2024
1 parent 4e2bc06 commit aa3a16a
Show file tree
Hide file tree
Showing 8 changed files with 664 additions and 766 deletions.
11 changes: 0 additions & 11 deletions .eslintrc.json

This file was deleted.

3 changes: 0 additions & 3 deletions .prettierrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ app.use(
setIf(req, res) {
return req.secure;
},
})
}),
);
```

Expand Down
18 changes: 18 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import globals from "globals";
import pluginJs from "@eslint/js";

export default [
{ files: ["**/*.js"], languageOptions: { sourceType: "commonjs" } },
{ languageOptions: { globals: globals.node } },
pluginJs.configs.recommended,
{
rules: {
"array-callback-return": "error",
"no-useless-rename": "error",
"no-var": "error",
"prefer-arrow-callback": "error",
"prefer-const": "error",
"prefer-destructuring": "error",
},
},
];
58 changes: 25 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = function hpkp(passedOptions) {
var options = parseOptions(passedOptions);
var headerName = getHeaderName(options);
var headerValue = getHeaderValue(options);
const options = parseOptions(passedOptions);
const headerName = getHeaderName(options);
const headerValue = getHeaderValue(options);

return function hpkp(req, res, next) {
if (options.setIf(req, res)) {
Expand All @@ -12,8 +12,8 @@ module.exports = function hpkp(passedOptions) {
};

function parseOptions(options) {
var badArgumentsError = new Error(
"hpkp must be called with a maxAge and at least two SHA-256s (one actually used and another kept as a backup)."
const badArgumentsError = new Error(
"hpkp must be called with a maxAge and at least two SHA-256s (one actually used and another kept as a backup).",
);

if (
Expand All @@ -24,13 +24,13 @@ function parseOptions(options) {
throw badArgumentsError;
}

var maxAge = options.maxAge;
var sha256s = options.sha256s;
var setIf =
options.setIf ||
function () {
return true;
};
const {
maxAge,
sha256s,
setIf = () => true,
reportUri,
reportOnly,
} = options;

if (!maxAge || maxAge <= 0 || !sha256s || sha256s.length < 2) {
throw badArgumentsError;
Expand All @@ -40,33 +40,25 @@ function parseOptions(options) {
}

return {
maxAge: maxAge,
sha256s: sha256s,
maxAge,
sha256s,
includeSubDomains: options.includeSubDomains || options.includeSubdomains,
reportUri: options.reportUri,
reportOnly: options.reportOnly,
setIf: setIf,
reportUri,
reportOnly,
setIf,
};
}

function getHeaderName(options) {
var result = "Public-Key-Pins";
if (options.reportOnly) {
result += "-Report-Only";
}
function getHeaderName({ reportOnly }) {
const result = "Public-Key-Pins";
if (reportOnly) return result + "-Report-Only";
return result;
}

function getHeaderValue(options) {
var result = options.sha256s.map(function (sha) {
return 'pin-sha256="' + sha + '"';
});
result.push("max-age=" + Math.round(options.maxAge));
if (options.includeSubDomains) {
result.push("includeSubDomains");
}
if (options.reportUri) {
result.push('report-uri="' + options.reportUri + '"');
}
function getHeaderValue({ sha256s, maxAge, includeSubDomains, reportUri }) {
const result = sha256s.map((sha) => 'pin-sha256="' + sha + '"');
result.push("max-age=" + Math.round(maxAge));
if (includeSubDomains) result.push("includeSubDomains");
if (reportUri) result.push('report-uri="' + reportUri + '"');
return result.join("; ");
}
Loading

0 comments on commit aa3a16a

Please sign in to comment.