Giter Site home page Giter Site logo

aws-lambda-multipart-parser's People

Contributors

caiolrm avatar jalleyne avatar mamerisawesome avatar myshenin avatar tony-aq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

aws-lambda-multipart-parser's Issues

Getting a malformed buffer

I'm running in this situation where the buffer that I get back from this module, is not correct. For example images won't be displayed because of this. It seams to me that your code is parsing the binary data from the request incorrectly.

(HelpWanted) Reuse the binary to make a request

First of all, great job! this library help from having a hell of parsing multipart/form-data.
I'm now facing a very strange situation, my lambda function it's just an interface between a client and an API, I'm using your library to parse the incoming data and, make some operations and then post a different object to the API. My function looks like this

const axios = require('axios');
const multipart = require('aws-lambda-multipart-parser');
module.exports.run = async event => {
    try {
        /* sample of json
            {name: 'test_a', something: 'test_b', documents: ''}
        */
        const data = multipart.parse(event, false);

        /* data.documents has the file(s) */
        const result = await someOperation(data);
        const response = axios.post('http://example.com/results', result);
        return {
            statusCode: 200,
	    body: JSON.stringify(response.data),
        }
        } catch (e) {
            return {
                statusCode: 500,
	        body: JSON.stringify({
                    message: e.message,
                    stack: e,
            }),
        }
    }
};

My problem is that I need to reuse the files to make the last request. So far I'm failing on every try and I will appreciate your help a lot! Any idea is more than welcome.
Thanks in advance

Any progress on S3 uploading

Any progress or work arounds for the S3 uploading issue? I'm attempting to use this to upload to S3 and I am seeing the issue you mentioned (I hadn't seen that initially)

getValueIgnoringKeyCase causing issues with AWS parameter validation

Add the getValueIgnoringKeyCase method to the Object prototype breaks upload to S3 using the AWS SDK. This is because the SDK checks the params object against a strict schema.

{ InvalidParameterType: Expected params.getValueIgnoringKeyCase to be a string
at ParamValidator.fail (/var/task/node_modules/aws-sdk/lib/param_validator.js:50:37)
at ParamValidator.validateType (/var/task/node_modules/aws-sdk/lib/param_validator.js:222:10)
at ParamValidator.validateString (/var/task/node_modules/aws-sdk/lib/param_validator.js:154:32)
at ParamValidator.validateScalar (/var/task/node_modules/aws-sdk/lib/param_validator.js:130:21)
at ParamValidator.validateMember (/var/task/node_modules/aws-sdk/lib/param_validator.js:94:21)
at ParamValidator.validateStructure (/var/task/node_modules/aws-sdk/lib/param_validator.js:75:14)
at ParamValidator.validateMember (/var/task/node_modules/aws-sdk/lib/param_validator.js:88:21)
at ParamValidator.validate (/var/task/node_modules/aws-sdk/lib/param_validator.js:34:10)
at Request.VALIDATE_PARAMETERS (/var/task/node_modules/aws-sdk/lib/event_listeners.js:125:42)
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at callNextListener (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:95:12)
at /var/task/node_modules/aws-sdk/lib/event_listeners.js:85:9
at finish (/var/task/node_modules/aws-sdk/lib/config.js:320:7)
at /var/task/node_modules/aws-sdk/lib/config.js:338:9
at EnvironmentCredentials.get (/var/task/node_modules/aws-sdk/lib/credentials.js:126:7)
at getAsyncCredentials (/var/task/node_modules/aws-sdk/lib/config.js:332:24)
message: 'Expected params.getValueIgnoringKeyCase to be a string',
code: 'InvalidParameterType',
time: 2018-02-06T16:01:44.169Z }

[feature request]Have boundary and body as parameters instead of event

Issue I faced

Hi and really great work building a multipart parser for Lambda. Once issue I have with this library is that it only accepts 'event' as a parameter. While good for Lambda proxy integration, this is unsuitable for Lambda Custom integration.

Suggested feature

On looking at the code I see that your library just needs 2 parameters from the event object- 'Content-Type' header (for extracting boundary) and body(contains encoded string). Instead of asking for event, we could ask for these two parameters from the user. You can keep the existing function as it is, while adding a new function which incorporates these features.

For example

const multipart = require('aws-lambda-multipart-parser');
let body = event["body"];
let contentType = event["content-type"];
let output = multipart.parse(body, contentType, true);

If you don't mind, I can make a PR and you can add me as a collaborator for this library.

Not able to parse the Gzip form data

Hello

Am trying to POST the gzip form-data. After the library parses the data, Am not able to convert the buffer to string.

Below is the code am using for testing.

`const multipart = require('aws-lambda-multipart-parser');
var zlib = require('zlib');

exports.handler = function(event,context,callback){
console.log(event);

var buf = Buffer.from(event['body'], 'base64').toString();
console.log(buf)



var parsed = multipart.parse(event, true);

console.log(parsed);

var gbuf = parsed['payload']['content'];
var stringBuf = zlib.gunzipSync(buf);
console.log(stringBuf);
const response = {
    statusCode: 200,
    headers: {
        "Access-Control-Allow-Origin": "*"
    },
    body: JSON.stringify({ name : 'kalyan' })
};

callback(null,response);

}`

below is output of parser

{ name: 'kalyan',
payload: 
{ type: 'file',
filename: 'sample_payload.xml.gz',
contentType: 'application/x-gzip',
content: <Buffer 1f 8b 08 08 ef aa 47 5b 00 03 73 61 6d 70 6c 65 5f 70 61 79 6c 6f 61 64 2e 78 6d 6c 00 ec 5d 6b 73 e2 38 d6 fe be 55 ef 7f 48 f5 d7 61 07 c9 b2 7c 49 ... > } }

and below is error when i try to unzip the buffer in above content. It is failing at the below line.

var stringBuf = zlib.gunzipSync(buf);

Error: incorrect header check
at Zlib._handle.onerror (zlib.js:370:17)
at Gunzip.Zlib._processChunk (zlib.js:540:30)
at zlibBufferSync (zlib.js:239:17)
at Object.exports.gunzipSync (zlib.js:175:10)
at exports.handler (/var/task/index.js:19:26)

TypeScript

If anyone needs it as TypeScript:

// myshenin/aws-lambda-multipart-parser#30

import { APIGatewayProxyEvent } from 'aws-lambda'

type Fields = {
  [field: string]:
    | {
        type: string
        filename: string
        contentType: string
        content: unknown
      }
    | string
}

function getValueIgnoringKeyCase(
  object: APIGatewayProxyEvent['headers'],
  key: string
) {
  const foundKey = Object.keys(object).find(
    (currentKey) => currentKey.toLocaleLowerCase() === key.toLowerCase()
  )

  return foundKey && object[foundKey]
}

function getBoundary(event: APIGatewayProxyEvent) {
  return getValueIgnoringKeyCase(event.headers, 'Content-Type')?.split('=')[1]
}

function itemMatch(item: string, re: RegExp) {
  const match = item.match(re)

  if (match === null) {
    throw new Error(`${item} not found`)
  }

  return match[0]
}

export function parseEvent(event: APIGatewayProxyEvent): Fields {
  const boundary = getBoundary(event)
  if (!boundary) {
    throw new Error('boundary not found')
  }
  const fields = event.body?.split(boundary)
  if (!fields) {
    return {}
  }

  return fields.reduce((result, item) => {
    if (/filename=".+"/g.test(item)) {
      const key = itemMatch(item, /name=".+";/g).slice(6, -2)
      // @ts-ignore
      result[key] = {
        type: 'file',
        filename: itemMatch(item, /filename=".+"/g).slice(10, -1),
        contentType: itemMatch(item, /Content-Type:\s.+/g).slice(14),
        content: item.slice(
          item.search(/Content-Type:\s.+/g) +
            itemMatch(item, /Content-Type:\s.+/g).length +
            4,
          -4
        ),
      }
    } else if (/name=".+"/g.test(item)) {
      const key = itemMatch(item, /name=".+"/g).slice(6, -1)
      // @ts-ignore
      result[key] = item.slice(
        item.search(/name=".+"/g) + itemMatch(item, /name=".+"/g).length + 4,
        -4
      )
    }

    return result
  }, {})
}

Does not support multi line form values

Issue

When submitting a form with a multiline value such as:

Test line one,

Test Line Two

Anything after 'Test line one' gets removed.

After some debugging I figured out that this code (line 50 as of now):

.split(/\r\n\r\n/)[1]

is the issue. The input of the sample text above looks like so:

\r\n\r\nTest line one,\r\n\r\nTest Line Two\r\n

And the split() call is only taking the first element in the array, which is Test line one,.

Quick solution

For now, I just changed that line to this:

.split(/\r\n\r\n/).slice(1).join("\r\n")

Keep in mind that this changes any amount of new lines to a single new line. I don't have time to create a PR, but you could do something similar to support multi line inputs.

Getting a corrupted file after parsing event

Hey,

I was trying to set up the parser to handle multipart file uploads in my function, but I failed at my first try, maybe you guys can shed light at what am I doing wrong here:

This is my simplified event:

let fs = require('fs');
let parse = require('aws-lambda-multipart-parser');
let parsed = parse({
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d/fTTTyN91nGtk3fBOpHnZw4zu+yyyzcUiAZglUNH77333mbvvfdulltuubahnOy4447Ntddemzr13XffpfptZNCgX3755XQ/UdgChUWROYInP/ejXEA9uQhI36Jx5857WIUPcsvh2TxECPrUB30Igp1EMtqjb1deeWU6joBtPvjgg+HRUliiojfbX9U5WGM1b731VvP666+P6JVy0gn6AVzvsmEOkc5KQWvmgAMO+ILGL7zwwuGp32CV44GCaeiYY45pVl999XYQOVlzzTWbww8/PJV55ZVX2vOvvvpqSFoGUKsE7mX6I+VAyU8//XRLyFIERFkRcXPnI9x4443tWChXwjiRKyJILdAf47CBxKM0Vq7Rrp1ZVedLL73UHHHEEc0hhxwyolfr5IqqJR6BPnrJzeg///xzsn1L6BtuuGF4qewBESAUEYpBUs/OO+/cDqpLDjrooFlRANRGScES95133qkag58yhdz5CPS7ZiahPo1ZxIzG7eEJwv2UQ0rtcU2zVAmQSbry/aEOUhZmUduWHcsjjzwyPPsbIJuPqjlEeikhN6OT0uFcM9ttt92/uUHkYVAcd0WbLqAAdkbuv//+5sgjj2zWW2+9tuM5YbF5yimnNE899dRIlP0zQ9HHEzOKfjlYgpRmHAvKeP1R3hLTHvv+cF6OTT02QPixTAJbF6S0/YtAn6PgofHOrLbaav+lMk6AnAdMA3QGh2FHhUUki0kNJhK2DLfffvvm/PPPT7ka5SfFONM+QIGlqCgCWPjog45x8C+//DLdjwFK6QBgzIsWLUoOnms7go28An2w5NQx+Oqrr5qrr7667U/kEBZct1F4XL0C1fXZZ5+FTupnjghyvoGulyr8k08+SR2iYNf0OU2gCFKVM844o9l00007F5zk64OFbHPTTTclg/RRoidYH9hIhY5QsiV4RADaUHsYzLbP2qFGx88++2xb5p577hme/Q3U6YmbA3284IILUl18Vp8jvUQOYWH7bsuzWJwGd2ibvm255ZZt3ZK11lqrOfDAA9NY4I5tb3B9ZmTvua+hxwUeBSGkVLaWyNdQDtfYPx3k983666/f9q0kDPCKK65Izki9ipaQ74cffkjHnmDjIpr2iXBvv/12+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276rXdlRdoh0aonJwyHqVDlmLkFEFmxdvMYABp+XnkBQMh5BQyidAUSgA4g+c59taBKo0wyKJ2js+0KGvkbjXvqlaCdwnujIYlAkK9WNYqPUimNfpqQDteWJTDTydZfAvdMksIWIHEV9nE12t7bX5xwiO3iddpG8NFb1lWcFIP2fwtxTK17IFsHVociQcwVFhWkDBU5ar68jimroCSIQVa2OITJl2XP2ZaYB2u1jI2zLDK3/vwI5puk0NZyJAof0RfDJwTt4m0MzIDySqQyFqzIrRBdeduEr6kzdPAJm89++a5AT+wzfCuej+xG+p6ftLIQBQ5xIGFDtLDENR/F1RGkReuH1WumPHJky2ibsSqUgmv4FXBd4+MNCnXtzkZNr6AqCKMW0kVBCPX0BsawtLMYNIOoPe/YWcsAI2UUhA+0i+B9diIregZhx2DaTo0BMGQKphaZgwUYKFE47ti8s9ihDGxzTro8uFpZopF4l2LUAa4ccqNP+aA1Es9M9r3SWIjP9zV2DYOgTsXoB4wQQdKV+ffvtt8Ozo3qh7x7Vuxx0mAjJOwSqMBKiED9ZxX4q0VuDJG/lOIrobKOxjYXRRTz+4Sb12W8w8DsVCJ9/DyfzDmH7DInkBJoZIDKziq2DctbAtQtcSzReiC8BolGfJ2M0Y9l6la9COD/dR6AsY4/AGHMzpHf+CN5ZbD/50oAictT/39A0/wcIZWaaUTmF2QAAAABJRU5ErkJggg0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5eGd5dGkwUGxYR25xRDVGRi0tDQo=",
    "headers": {
        "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryxgyti0PlXGnqD5FF"
    },
    "isBase64Encoded": true
});
fs.writeFileSync('/tmp/file.png', parsed.files.content, 'utf-8');

after running it the parsed object gets the metadata right, but the contents (which gets written in /tmp/file.png seem to be corrupted or maybe I'm handling the encoding wrong?

Appreciate

Sorry for the long b64 btw, it was the smallest image I found

how to use with aws-serverless-express

Hi, I'm using passport.js to authenticate and the Okta SAML strategy that we are using is sending multipart/form-data to my express server where I am then using that data to authenticate and parse out user info. This require express in order to work, but as you know api-gateway doesn't support multipart/form-data. Do you know of a way to use this with aws-serverless-express?

Content-type header inside the actual content is not mandatory

Hi,

Sending a multipart that contains no Content-Type header inside breaks the parsing... Content-Type is used after the filename for matches, however it is not to be assumed there. Taken from the RFC1341

"A body part is NOT to be interpreted as actually being an RFC 822 message. To begin with, NO header fields are actually required in body parts. (...) the absence of a Content-Type header field implies that the encapsulation is plain US-ASCII text."

https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

We want to use this lib but we will have to use a fork instead and raise a PR if this is still being maintained?

Issue parsing data on multipart/form-data message containing a series of parts

The library seems to work very well when I use it to parse data from a form with only one part but I am experiencing issues when parsing data from a form with multiple parts.

E.g. if my form is submitted with the following payload (all payloads taken from hitting the endpoint via Postman):

------WebKitFormBoundaryHi34dGTTK0UbQ0td
Content-Disposition: form-data; name="file"; filename="myfile.csv"
Content-Type: text/csv
------WebKitFormBoundaryHi34dGTTK0UbQ0td--

then I get correctly get when parsing the event in my Lambda:

{
    "file": {
        "type": "file",
        "filename": "myfile.csv",
        "contentType": "text/csv",
        "content": "filecontents"
    }
}

If I submit with a file part and a boolean part like this:

------WebKitFormBoundaryHi34dGTTK0UbQ0td
Content-Disposition: form-data; name="file"; filename="myfile.csv"
Content-Type: text/csv


------WebKitFormBoundaryHi34dGTTK0UbQ0td
Content-Disposition: form-data; name="preview"

true
------WebKitFormBoundaryHi34dGTTK0UbQ0td--

then the result of parsing is:

{
    "file": {
        "type": "file",
        "filename": "myfile.csv",
        "contentType": "text/csv",
        "content": "filecontents"
    }
}

The "preview" value is missing.

If I submit with only a "preview" value:

------WebKitFormBoundaryBnXclFeabewM9qcN
Content-Disposition: form-data; name="preview"

true
------WebKitFormBoundaryBnXclFeabewM9qcN--

then I correctly get this back after parsing:

{
    "preview": "true"
}

If I submit the "preview" parameter as the first part of the form and the file as the second then things get a bit weirder:

------WebKitFormBoundaryAgA0CHTJt4WM8G60
Content-Disposition: form-data; name="preview"

true
------WebKitFormBoundaryAgA0CHTJt4WM8G60
Content-Disposition: form-data; name="file"; filename="myfile.csv"
Content-Type: text/csv


------WebKitFormBoundaryAgA0CHTJt4WM8G60--

Result from parsing:

{
    "preview": {
        "type": "file",
        "filename": "myfile.csv",
        "contentType": "text/csv",
        "content": "true\r\n------WebKitFormBoundaryAgA0CHTJt4WM8G60\r\nContent-Disposition: form-data; name=\"file\"; filename=\"myfile.csv\"\r\nContent-Type: text/csv"
    }
}

I can make the parsing work for all of the above cases if I change lines 10 and 11 from:

.split(boundary)
.filter(item => item.match(/Content-Disposition/))

to

.split('Content-Disposition')
.filter(item => item.match(/form-data/))

But this is quite a significant change. Is there something am I missing in terms of some setup somewhere (in API Gateway for example) in order to get the current version working with multiple parts or is it a genuine issue?

Cheers,
Chris

Repeated field names get overriden by the last one

I have a form where I have 3 input fields with the name attribute 'departureAirports'.
I only set the first one and leave the second and third empty.
When I inspect the parsed data from aws-lambda-multipart-parser, I see that departureAirports if empty and was overridden by the third departure airport.

I think the proper functionality should be, parse the first and if there are more fields with the same name, make the object attribute into a list, and push every subsequent value for the repeated fields.

So if I had the following multipart data:
departureAirports: 'JFK'
departureAirports: 'LAX'
departureAirports: ''

I should get this as the parsed data:

{
    departureAirports: ['JFK', 'LAX', '']
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.