npm.io
1.0.0 • Published 3 years ago

multipart-browser-parser

Licence
MIT
Version
1.0.0
Deps
0
Size
20 kB
Vulns
0
Weekly
0
Stars
2

multipart-browser-parser

npm version MIT License

Got an obscure REST endpoint that returns a multipart response and need to parse it directly in browser? This is the package you are looking for! One response in, multiple responses out.

Installation

Install using npm

  npm install multipart-browser-parser

Example

import {parseMultipartResponse} from "multipart-browser-parser";

async function getMultipartData() {
  let response = await fetch("/api/multipart");
  let {bodyParts} = parseMultipartResponse(response);

  let firstPartAsText = await bodyParts[0].text();
  let secondPartAsJson = await bodyParts[1].json();

  return [firstPartAsText, secondPartAsJson];
}

API Reference

parseMultipartResponse

▸ parseMultipartResponse(response, userOptions?): Promise<ParseResult>

Parse a multipart Response object into separate Response objects

Parameters
Name Type Description
response Response Multipart Response object
userOptions? Partial<Options> User-supplied parser configuration
Returns

Promise<ParseResult>

Separate Response objects, together with any raw preamble and epilogue data


parse

▸ parse(body, boundary, userOptions?): ParseResult

Parse raw multipart data into separate Response objects

Parameters
Name Type Description
body ArrayBuffer | Uint8Array Multipart raw data
boundary string Multipart boundar string
userOptions? Partial<Options> User-supplied parser configuration
Returns

ParseResult Separate Response objects, together with any raw preamble and epilogue data


getBoundary

▸ getBoundary(headerWithBoundary): string | undefined

Parse a boundary string out of a header that contains it as a parameter

Parameters
Name Type Description
headerWithBoundary string Raw header value or whole raw header
Returns

string | undefined Multipart boundary string


ParseResult

Result of parsing of multipart data

• bodyParts: Response[]

Separate Response objects parsed from multipart data

• preamble: Uint8Array

Raw data of any preamble present in multipart body

• epilogue: Uint8Array

Raw data of any epilogue present in multipart body


Options

User-supplied parser configuration

• rfcViolationNoBoundaryPrefix: boolean

Flag to set if the Content-Type header already contains

• rfcViolationNoFirstBoundary: boolean

Flag to set if your response does not contain a preamble and starts with the first part immediately, without a boundary string


License

MIT

Keywords