npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@admixltd/admix-multipart-parser

v1.0.4

Published

Parser of multipart/form-data requests for AWS Lambda

Downloads

89

Readme

Credit to:

This library was copied from https://www.npmjs.com/~anton.myshenin work on https://www.npmjs.com/package/aws-lambda-multipart-parser

The key difference is this version supports typescript

admix-multipart-parser

Introduction

Support of multipart/form-data requests is a relatively new feature for AWS Lambdas. Although, there is such feature, majority of libraries for parsing multipart/form-data requests is based on server technology, which can't be used in case of AWS Lambdas. That's why, AWS Lambda specific multipart/form-data parser was created.

Steps for integrating multipart/form-data to your lambda

1.Create project with serverless framework.

You can find all necessary information there:

  • at official github page: https://github.com/serverless/serverless
  • nice youtube playlist about serverless: https://www.youtube.com/watch?v=lUTGk64jppM&list=PLzvRQMJ9HDiT5b4OsmIBiMbsPjfp4kfg3

2. Add configuration for your AWS Lambda.

Go to serverless.yml file and add configurations for your AWS Lambda:

 method: POST
 integration: LAMBDA

method: POST - because of we are receiving multipart/form-data request. integration: LAMBDA - without that I constanly got 502 Error: Bad Gateway, but I don't know why.

3. Deploy a draft of your function

Execute command sls deploy in AWS Lambda folder. (for more information look for sources at step 1)

4. Configure API Gateway at AWS Console.

  1. Go to API Gateway.
  2. Select your API in API Gateway interface capture
  3. Go to Settings capture
  4. Add multipart/form-data binary media type capture
  5. Go to Resources -> POST method of your API -> Integration Request capture
  6. Check Use Lambda Proxy Integration capture
  7. Deploy your API changes capture

5. Enabling CORS

In majority of cases, while working with AWS Lambda, you will need to enable CORS. Unfortunately, standard way of doing it with AWS UI in API Gateway doesn't work in our case. To solve the problem, you need to send Access-Control-Allow-Origin header as a part of response from your lambda. For example:

const response = {
        statusCode: 200,
        headers: {
            "Access-Control-Allow-Origin": "*"
        },
        body: JSON.stringify(parse(event, true)),
    };

    callback(null, response);

6. You're ready to use the library.

  1. Import aws-lambda-multipart-parser with npm install --save aws-lambda-multipart-parser command.
  2. Require it in file const multipart = require('aws-lambda-multipart-parser');.
  3. Pass your event object to parse function like that multipart.parse(event, spotText), where event is a event object you get from lambda invocation, spotText - if it's true all text files are present in text for after parsing, if it's false all text files are represented as regular files with Buffer. Parse function will return object representing the request body.

7. Response body object

{
    "file": {
        "type": "file",
        "filename": "lorem.txt",
        "contentType": "text/plain",
        "content": {
            "type": "Buffer",
            "data": [ ... byte array ... ]
        } or String
    },
    "field": "value"
}

All fields are represented in request body object as a key-value pair. All files are represented as an object with these fields:

  • type - indicates that it's a file
  • filename - name of uploaded file (the first one)
  • contentType - mime-type of file
  • content - content of file in form of Buffer (it's planned in future to give a choise between Buffer and text)

8. Issue with malformed media files uploaded to S3.

Unfortunately, there is an issue with malformed media files uploaded to S3. Let me explain on example, I send an image, which is 50Kb. I get a buffer, which is 50Kb. That fact indicates buffer is formed well, but some encoding issue happens with s3.upload and s3.putObject functions. If you have a solution or a hint, contact me [email protected]