Skip to content

Commit

Permalink
Merge pull request #47 from venables/revert-util-promisify
Browse files Browse the repository at this point in the history
Revert `util.promisify` usage to continue supporting Node 6
  • Loading branch information
venables committed Aug 12, 2019
2 parents 111391d + 712c84a commit 5e70d48
Show file tree
Hide file tree
Showing 6 changed files with 1,215 additions and 1,160 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -3,4 +3,5 @@ node_js:
- "12"
- "10"
- "8"
- "6"
after_success: yarn run coverage
2 changes: 1 addition & 1 deletion lib/koa-helmet.js
@@ -1,7 +1,7 @@
'use strict';

const helmet = require('helmet');
const { promisify } = require('util');
const promisify = require('./promisify');

const koaHelmet = function () {
const helmetPromise = promisify(helmet.apply(null, arguments));
Expand Down
24 changes: 24 additions & 0 deletions lib/promisify.js
@@ -0,0 +1,24 @@
'use strict';

/**
* Takes an express-style middleware and returns a promise-based version (returning a Promise,
* rather than calling the final argument as a callback) suitable for use with koa.
*
* @param {Function} middleware - The middleware to promisify
* @returns {Function} - The middleware function updated to return a Promise, not call a callback
*/
function koaHelmetPromisify (middleware) {
return function (req, res) {
return new Promise(function (resolve, reject) {
middleware(req, res, function (err) {
if (err) {
return reject(err);
}

return resolve();
});
});
};
}

module.exports = koaHelmetPromisify;
16 changes: 8 additions & 8 deletions package.json
Expand Up @@ -3,7 +3,7 @@
"author": "Matt Venables <mattvenables@gmail.com>",
"description": "Security header middleware collection for koa",
"license": "MIT",
"version": "4.2.0",
"version": "4.2.1",
"main": "lib/koa-helmet.js",
"scripts": {
"format": "eslint lib test --fix",
Expand All @@ -23,19 +23,19 @@
"url": "git://github.com/venables/koa-helmet.git"
},
"engines": {
"node": ">= 8.0.0"
"node": ">= 6.0.0"
},
"dependencies": {
"helmet": "^3.18.0"
"helmet": "^3.15.1"
},
"devDependencies": {
"ava": "^2.0.0",
"ava": "^1.3.1",
"coveralls": "^3.0.3",
"eslint": "^5.16.0",
"eslint-plugin-node": "^9.1.0",
"eslint": "^5.15.1",
"eslint-plugin-node": "^8.0.1",
"koa": "^2.7.0",
"nyc": "^14.1.1",
"supertest": "^4.0.2"
"nyc": "^13.3.0",
"supertest": "^4.0.0"
},
"nyc": {
"reporter": [
Expand Down
38 changes: 38 additions & 0 deletions test/promisify.spec.js
@@ -0,0 +1,38 @@
'use strict';

const promisify = require('../lib/promisify');
const test = require('ava');

function passingMiddleware (req, res, next) {
return next();
}

function failingMiddleware (req, res, next) {
return next(new Error('Expected Failure'));
}

test('returns a promisified version of the middleware which resolves the Promise on success', t => {
let middleware = promisify(passingMiddleware);

return middleware().then(
() => {
t.pass();
},
err => {
t.fail(err);
}
);
});

test('returns a promisified version of the middleware which rejects the Promise on failure', t => {
let middleware = promisify(failingMiddleware);

return middleware().then(
() => {
t.fail('Unexpected Success!');
},
() => {
t.pass();
}
);
});

0 comments on commit 5e70d48

Please sign in to comment.