Skip to content

Commit 9ef028f

Browse files
author
Francis Pasoquen
committed
Initial commit
0 parents  commit 9ef028f

File tree

7 files changed

+576
-0
lines changed

7 files changed

+576
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
**/node_modules/**
2+
**/target/**
3+
.project
4+
.jsbeautifyrc

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Francis Pasoquen
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# lambda-multipart-parser
2+
3+
## Introduction
4+
This module will parse the multipart-form containing files and fields from the lambda event object.
5+
6+
## Description
7+
```
8+
@param {event} - an event containing the multipart-form in the body
9+
@return {object} - a JSON object containing array of files and fields, sample below.
10+
11+
{
12+
files: [
13+
{
14+
filename: 'test.pdf',
15+
content: <Buffer 25 50 6f 62 ... >,
16+
contentType: 'application/pdf',
17+
encoding: '7bit',
18+
fieldname: 'uploadFile1'
19+
}
20+
],
21+
field1: 'VALUE1',
22+
field2: 'VALUE2',
23+
}
24+
```
25+
26+
## Usage

index.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
3+
const Busboy = require('busboy');
4+
5+
/*
6+
* This module will parse the multipart-form containing files and fields from the lambda event object.
7+
* @param {event} - an event containing the multipart-form in the body
8+
* @return {object} - a JSON object containing array of files and fields, sample below.
9+
{
10+
files: [
11+
{
12+
filename: 'test.pdf',
13+
content: <Buffer 25 50 6f 62 ... >,
14+
contentType: 'application/pdf',
15+
encoding: '7bit',
16+
fieldname: 'uploadFile1'
17+
}
18+
],
19+
field1: 'VALUE1',
20+
field2: 'VALUE2',
21+
}
22+
*/
23+
const parse = (event) => new Promise((resolve, reject) => {
24+
const busboy = new Busboy({
25+
headers: {
26+
'content-type': event.headers['content-type'] || event.headers['Content-Type']
27+
}
28+
});
29+
const result = {
30+
files: []
31+
};
32+
33+
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
34+
const uploadFile = {};
35+
36+
file.on('data', data => {
37+
uploadFile.content = data;
38+
});
39+
40+
file.on('end', () => {
41+
if (uploadFile.content) {
42+
uploadFile.filename = filename;
43+
uploadFile.contentType = mimetype;
44+
uploadFile.encoding = encoding;
45+
uploadFile.fieldname = fieldname;
46+
result.files.push(uploadFile);
47+
}
48+
});
49+
});
50+
51+
busboy.on('field', (fieldname, value) => {
52+
result[fieldname] = value;
53+
});
54+
55+
busboy.on('error', error => {
56+
reject(error);
57+
});
58+
59+
busboy.on('finish', () => {
60+
resolve(result);
61+
});
62+
63+
busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'binary');
64+
busboy.end();
65+
});
66+
67+
module.exports.parse = parse;

0 commit comments

Comments
 (0)