From d100ba777c936870be81f7c1d5420e557eb1256a Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 5 Jul 2017 15:49:38 +0200 Subject: [PATCH 1/2] Support normal fields. --- README.md | 9 ++++++++- index.js | 2 ++ package.json | 8 ++++---- test.js | 10 ++++++++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2b286eb3..a136c84f 100644 --- a/README.md +++ b/README.md @@ -21,11 +21,18 @@ fastify.register(require('fastify-multipart'), err => { }) fastify.post('/', function (req, reply) { - req.multipart(handler, function (err) { + const mp = req.multipart(handler, function (err) { console.log('upload completed') reply.code(200).send() }) + // mp is an instance of + // https://www.npmjs.com/package/multipart-read-stream + + mp.on('field', function (key, value) { + console.log('form-data', key, value) + }) + function handler (field, file, filename, encoding, mimetype) { file.pipe(concat(function (buf) { console.log('received', filename, 'size', buf.length) diff --git a/index.js b/index.js index 4ad86f1e..4514747f 100644 --- a/index.js +++ b/index.js @@ -47,6 +47,8 @@ function fastifyMultipart (fastify, options, done) { log.debug({ field, filename, encoding, mimetype }, 'parsing part') handler(field, file, filename, encoding, mimetype) } + + return stream } function isMultipart () { diff --git a/package.json b/package.json index 7377ed30..bc7918a4 100644 --- a/package.json +++ b/package.json @@ -5,17 +5,17 @@ "main": "index.js", "dependencies": { "fastify-plugin": "^0.1.0", - "multipart-read-stream": "^3.0.0", + "multipart-read-stream": "^3.0.1", "pump": "^1.0.2" }, "devDependencies": { "concat-stream": "^1.6.0", "fastify": "^0.17.0", - "form-data": "^2.1.4", + "form-data": "^2.2.0", "pre-commit": "^1.2.2", "snazzy": "^7.0.0", - "standard": "^10.0.1", - "tap": "^10.3.2" + "standard": "^10.0.2", + "tap": "^10.7.0" }, "scripts": { "test": "standard | snazzy && tap test.js" diff --git a/test.js b/test.js index 447b998f..9486c4c6 100644 --- a/test.js +++ b/test.js @@ -13,7 +13,7 @@ const concat = require('concat-stream') const filePath = path.join(__dirname, 'README.md') test('should parse forms', function (t) { - t.plan(8) + t.plan(10) const fastify = Fastify() @@ -22,10 +22,15 @@ test('should parse forms', function (t) { fastify.post('/', function (req, reply) { t.ok(req.isMultipart()) - req.multipart(handler, function (err) { + const mp = req.multipart(handler, function (err) { t.error(err) }) + mp.on('field', function (name, value) { + t.equal(name, 'hello') + t.equal(value, 'world') + }) + function handler (field, file, filename, encoding, mimetype) { t.equal(filename, 'README.md') t.equal(field, 'upload') @@ -54,6 +59,7 @@ test('should parse forms', function (t) { var req = http.request(opts, fastify.close.bind(fastify)) var rs = fs.createReadStream(filePath) form.append('upload', rs) + form.append('hello', 'world') pump(form, req, function (err) { t.error(err, 'client pump: no err') }) From 050583484974e05525e4ce97cb1d9c6e75c1a5b1 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 5 Jul 2017 15:50:05 +0200 Subject: [PATCH 2/2] Added node 8 to travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 54a59fb8..136d1577 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: node_js node_js: + - "8" - "7" - "6" - "5"