diff --git a/index.d.ts b/index.d.ts index 8d78cc2..d8d0644 100644 --- a/index.d.ts +++ b/index.d.ts @@ -9,6 +9,7 @@ declare module "lambda-multipart-parser" { contentType: string encoding: string fieldname: string + size: number } type MultipartRequest = { files: MultipartFile[] } & Record diff --git a/index.js b/index.js index 3189ad7..60efd1c 100644 --- a/index.js +++ b/index.js @@ -21,18 +21,22 @@ const Busboy = require('busboy'); } */ const parse = (event) => new Promise((resolve, reject) => { - const busboy = new Busboy({ + console.log("BUSBOyy---!!!! TESTIN TESTING123", JSON.stringify(event)); + + const bb = Busboy({ headers: { - 'content-type': event.headers['content-type'] || event.headers['Content-Type'] - } + 'Content-Type': event.headers['content-type'] || event.headers['Content-Type'] + }, + 'Content-Type': event.headers['content-type'] || event.headers['Content-Type'] }); const result = { files: [] }; - busboy.on('file', (fieldname, file, filename, encoding, mimetype) => { + bb.on('file', (fieldname, file, info) => { const uploadFile = {}; - + const { filename, encoding, mimeType } = info; + file.on('data', data => { uploadFile.content = data; }); @@ -40,30 +44,32 @@ const parse = (event) => new Promise((resolve, reject) => { file.on('end', () => { if (uploadFile.content) { uploadFile.filename = filename; - uploadFile.contentType = mimetype; + uploadFile.contentType = mimeType; uploadFile.encoding = encoding; uploadFile.fieldname = fieldname; + uploadFile.size = Buffer.byteLength(uploadFile.content); + result.files.push(uploadFile); } }); }); - busboy.on('field', (fieldname, value) => { + bb.on('field', (fieldname, value) => { result[fieldname] = value; }); - busboy.on('error', error => { + bb.on('error', error => { reject(error); }); - busboy.on('finish', () => { + bb.on('close', () => { resolve(result); }); const encoding = event.encoding || (event.isBase64Encoded ? "base64" : "binary"); - busboy.write(event.body, encoding); - busboy.end(); + bb.write(event.body, encoding); + bb.end(); }); module.exports.parse = parse; \ No newline at end of file diff --git a/package.json b/package.json index c0dd70f..7629e84 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "multipart/formdata" ], "dependencies": { - "busboy": "^0.3.0" + "busboy": "^1.6.0" }, "devDependencies": { "chai": "^3.5.0",