From 9e244bbe2b1d0e2a5a860110f6eb1178013b7afa Mon Sep 17 00:00:00 2001 From: jagnani73 Date: Sun, 26 Feb 2023 20:34:05 +0530 Subject: [PATCH 1/3] feat: add size --- index.d.ts | 1 + index.js | 4 +++- test/MultipartParserTest.js | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) 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..0f9e1c1 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,8 @@ const Busboy = require('busboy'); content: , contentType: 'application/pdf', encoding: '7bit', - fieldname: 'uploadFile1' + fieldname: 'uploadFile1', + size: 26000 } ], field1: 'VALUE1', @@ -43,6 +44,7 @@ const parse = (event) => new Promise((resolve, reject) => { uploadFile.contentType = mimetype; uploadFile.encoding = encoding; uploadFile.fieldname = fieldname; + uploadFile.size = Buffer.byteLength(uploadFile.content); result.files.push(uploadFile); } }); diff --git a/test/MultipartParserTest.js b/test/MultipartParserTest.js index a2e2614..bca1c4b 100644 --- a/test/MultipartParserTest.js +++ b/test/MultipartParserTest.js @@ -45,6 +45,7 @@ describe('MultipartParser', () => { assert.equal(file.contentType, "text/plain"); assert.equal(file.encoding, "7bit"); assert.equal(file.fieldname, "uploadFile1"); + assert.equal(file.size, 12); }); it('should parse the multipart form-data successfully given base64 encoded form data', async () => { @@ -72,6 +73,7 @@ describe('MultipartParser', () => { assert.equal(file.contentType, "text/plain"); assert.equal(file.encoding, "7bit"); assert.equal(file.fieldname, "uploadFile1"); + assert.equal(file.size, 12); }); it('should parse the multipart form-data successfully given utf8 encoded form data', async () => { From 7f897ddf1949d497ace28ab415b1340729cbb2c7 Mon Sep 17 00:00:00 2001 From: jagnani73 Date: Sun, 26 Feb 2023 21:01:03 +0530 Subject: [PATCH 2/3] feat: add checksum --- README.md | 7 ++++++- index.js | 5 ++++- package.json | 3 ++- test/MultipartParserTest.js | 2 ++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 979feb4..3d3db51 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,9 @@ This nodejs module will parse the multipart-form containing files and fields fro content: , contentType: 'application/pdf', encoding: '7bit', - fieldname: 'uploadFile1' + fieldname: 'uploadFile1', + size: 26000, + checksum: "387d3143b0baa6beb292eda4f81b2d33e55c6744" } ], field1: 'VALUE1', @@ -39,6 +41,9 @@ Please make sure to enable the "Use Lambda Proxy integration" in API Gateway met If decided not to enable it for some reason, make sure to pass the required Lambda event parameters in Integration Request -> Mapping Templates section, such as body, headers and isBase64Encoded flag. +The `size` of every file is in **bytes**. +The `checksum` of every file is calculated using **SHA1** hashing algorithm. + Sample Lambda and API Gateway implementation with Cloudformation can be found in [here](http://francismeynard.github.io/aws-upload-document-service). ## Test diff --git a/index.js b/index.js index 0f9e1c1..cad3260 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict'; const Busboy = require('busboy'); +const Crypto = require('crypto'); /* * This module will parse the multipart-form containing files and fields from the lambda event object. @@ -14,7 +15,8 @@ const Busboy = require('busboy'); contentType: 'application/pdf', encoding: '7bit', fieldname: 'uploadFile1', - size: 26000 + size: 26000, + checksum: "387d3143b0baa6beb292eda4f81b2d33e55c6744" } ], field1: 'VALUE1', @@ -45,6 +47,7 @@ const parse = (event) => new Promise((resolve, reject) => { uploadFile.encoding = encoding; uploadFile.fieldname = fieldname; uploadFile.size = Buffer.byteLength(uploadFile.content); + uploadFile.checksum = Crypto.createHash('sha1').update(uploadFile.content, "utf-8").digest('hex'); result.files.push(uploadFile); } }); diff --git a/package.json b/package.json index c0dd70f..70c648e 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "multipart/formdata" ], "dependencies": { - "busboy": "^0.3.0" + "busboy": "^0.3.0", + "crypto": "^1.0.1" }, "devDependencies": { "chai": "^3.5.0", diff --git a/test/MultipartParserTest.js b/test/MultipartParserTest.js index bca1c4b..62e7f53 100644 --- a/test/MultipartParserTest.js +++ b/test/MultipartParserTest.js @@ -46,6 +46,7 @@ describe('MultipartParser', () => { assert.equal(file.encoding, "7bit"); assert.equal(file.fieldname, "uploadFile1"); assert.equal(file.size, 12); + assert.equal(file.checksum, "2ef7bde608ce5404e97d5f042f95f89f1c232871"); }); it('should parse the multipart form-data successfully given base64 encoded form data', async () => { @@ -74,6 +75,7 @@ describe('MultipartParser', () => { assert.equal(file.encoding, "7bit"); assert.equal(file.fieldname, "uploadFile1"); assert.equal(file.size, 12); + assert.equal(file.checksum, "2ef7bde608ce5404e97d5f042f95f89f1c232871"); }); it('should parse the multipart form-data successfully given utf8 encoded form data', async () => { From ee28cfc8f254889171771fa51ca7bd820612ae42 Mon Sep 17 00:00:00 2001 From: jagnani73 Date: Sun, 26 Feb 2023 21:02:15 +0530 Subject: [PATCH 3/3] fix: update ts --- index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/index.d.ts b/index.d.ts index d8d0644..368c514 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,6 +10,7 @@ declare module "lambda-multipart-parser" { encoding: string fieldname: string size: number + checksum: string } type MultipartRequest = { files: MultipartFile[] } & Record