Skip to content

Commit

Permalink
Merge pull request #6 from fastify/fix-5
Browse files Browse the repository at this point in the history
Adds support for fields
  • Loading branch information
mcollina committed Jul 5, 2017
2 parents cc04fd4 + 0505834 commit e6c367d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- "8"
- "7"
- "6"
- "5"
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 8 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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')
Expand Down Expand Up @@ -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')
})
Expand Down

0 comments on commit e6c367d

Please sign in to comment.