From bb203d55b7648b30f9842d04e6225b33da072589 Mon Sep 17 00:00:00 2001 From: Alessio Biancalana Date: Thu, 26 Oct 2017 14:08:29 +0200 Subject: [PATCH 1/5] Added a new async/await plugin example --- examples/async-await-plugin.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 examples/async-await-plugin.js diff --git a/examples/async-await-plugin.js b/examples/async-await-plugin.js new file mode 100644 index 00000000..b0629de7 --- /dev/null +++ b/examples/async-await-plugin.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = async function (fastify, options) { + fastify.get('/', async function (req, reply) { + return { hello: 'world' } + }) +} From 821d0b470788223a46f693968411a059a61421a4 Mon Sep 17 00:00:00 2001 From: Alessio Biancalana Date: Thu, 26 Oct 2017 14:09:59 +0200 Subject: [PATCH 2/5] Bump fastify version to 0.30.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0e9b7b35..65f658b5 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "update-notifier": "^2.1.0" }, "devDependencies": { - "fastify": "^0.28.2", + "fastify": "^0.30.2", "pre-commit": "^1.2.2", "request": "^2.81.0", "standard": "^10.0.2", From e920d2006dd1305dc1254efb260f8c6068f805e2 Mon Sep 17 00:00:00 2001 From: Alessio Biancalana Date: Thu, 26 Oct 2017 14:10:28 +0200 Subject: [PATCH 3/5] Added a new test to check async/await plugins support --- test.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test.js b/test.js index 6f6bc19f..4cade338 100644 --- a/test.js +++ b/test.js @@ -165,6 +165,36 @@ test('should throw on parsing error', t => { }) }) +test('should start the server with an async/await plugin', t => { + if (Number(process.versions.node[0]) < 7) { + t.pass('Skip because Node version < 7') + return t.end() + } + + t.plan(5) + + const fastify = cli.start({ + port: 3000, + _: ['./examples/async-await-plugin.js'] + }) + + t.tearDown(() => fastify.close()) + + fastify.ready(err => { + t.error(err) + + request({ + method: 'GET', + uri: 'http://localhost:3000' + }, (err, response, body) => { + t.error(err) + t.strictEqual(response.statusCode, 200) + t.strictEqual(response.headers['content-length'], '' + body.length) + t.deepEqual(JSON.parse(body), { hello: 'world' }) + }) + }) +}) + test('should exit without error on help', t => { t.plan(1) From 40e3ce118ec8896c352bc593e87be5015194069a Mon Sep 17 00:00:00 2001 From: Alessio Biancalana Date: Thu, 26 Oct 2017 14:10:57 +0200 Subject: [PATCH 4/5] Rewrote the arity check upon the plugin signature to support async/await plugins --- cli.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cli.js b/cli.js index 0bdb71a3..6d89a674 100755 --- a/cli.js +++ b/cli.js @@ -52,10 +52,16 @@ function runFastify (opts) { return module.exports.stop(e) } - if (file.length !== 3) { + console.log(file.constructor.name) + + if (file.length !== 3 && file.constructor.name === 'Function') { return module.exports.stop(new Error('Plugin function should contain 3 arguments. Refer to ' + 'documentation for more information.')) } + if (file.length !== 2 && file.constructor.name === 'AsyncFunction') { + return module.exports.stop(new Error('Async/Await plugin function should contain 2 arguments.' + + 'Refer to documentation for more information.')) + } const options = { logger: { From b03216cadb580d7e07352ba97dbf79153a66863d Mon Sep 17 00:00:00 2001 From: Alessio Biancalana Date: Thu, 26 Oct 2017 14:14:34 +0200 Subject: [PATCH 5/5] Included an async/await example in README --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 745e3530..e51f55d6 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,15 @@ You can easily run it with: ```bash $ fastify plugin.js ``` +You can use `async` functions too, and make your plugin more concise: +```js +// async-await-plugin.js +module.exports = async function (fastify, options) { + fastify.get('/', async function (req, reply) { + return { hello: 'world' } + }) +} +``` CLI options: ```bash