|
| 1 | +# Multipart.ts |
| 2 | + |
| 3 | +A TypeScript library for multipart data parsing and creation. |
| 4 | + |
| 5 | +[**Documentation**](https://zefir-git.github.io/Multipart.ts) |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Parse any kind of `multipart/*`, e.g. `multipart/mixed`, `multipart/form-data`, `multipart/byteranges`, etc. |
| 10 | +- Create your own multipart with any parts |
| 11 | +- Create a Multipart instance from FormData |
| 12 | +- Serialise a Multipart instance into bytes (Uint8Array) |
| 13 | +- Works with Node.js and in the browser without any polyfills |
| 14 | +- Zero dependencies |
| 15 | + |
| 16 | +## Getting started |
| 17 | + |
| 18 | +Install using NPM |
| 19 | + |
| 20 | +```shell |
| 21 | +npm i multipart-ts |
| 22 | +``` |
| 23 | + |
| 24 | +Import as native ESM |
| 25 | + |
| 26 | +```ts |
| 27 | +import {Multipart} from "multipart-ts"; |
| 28 | +``` |
| 29 | + |
| 30 | +This library uses mainly `Uint8Array` so that it works in the browser. In Node.js, |
| 31 | +you can use [`Buffer`](https://nodejs.org/api/buffer.html) as it is a superset of `Uint8Array`. |
| 32 | +In the browser, to convert a `string` to `Uint8Array` and vice-versa, |
| 33 | +use [`TextEncoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) and |
| 34 | +[`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder) respectively. |
| 35 | + |
| 36 | +## Example |
| 37 | + |
| 38 | +### `node:http` |
| 39 | + |
| 40 | +```ts |
| 41 | +import http from "node:http"; |
| 42 | +import {Component, Multipart} from "multipart-ts"; |
| 43 | + |
| 44 | +http.createServer(async (req, res) => { |
| 45 | + // Parse multipart request body |
| 46 | + if (req.method === "POST" && req.headers["content-type"]) { |
| 47 | + |
| 48 | + // get the request body |
| 49 | + const chunks: Uint8Array[] = []; |
| 50 | + for await (const chunk of req) |
| 51 | + chunks.push(chunk); |
| 52 | + const body = Buffer.concat(chunks); |
| 53 | + |
| 54 | + // create a multipart component to hold the content-type header (which includes the boundary) and the body |
| 55 | + const component = new Component({ |
| 56 | + "Content-Type": req.headers["content-type"] |
| 57 | + }, body); |
| 58 | + // parse multipart from the component |
| 59 | + const multipart = Multipart.part(component); |
| 60 | + console.log(multipart); |
| 61 | + |
| 62 | + res.end("ok"); |
| 63 | + } |
| 64 | + // Return a multipart response |
| 65 | + else if (req.method === "GET") { |
| 66 | + const multipart = new Multipart( |
| 67 | + [ |
| 68 | + new Component({ |
| 69 | + "X-Foo": "Bar Baz", |
| 70 | + "Content-Type": "text/plain" |
| 71 | + }, Buffer.from("Hello world!")), |
| 72 | + |
| 73 | + // You can add nested multipart components |
| 74 | + new Multipart([ |
| 75 | + new Component({"Content-Type": "application/json"}, Buffer.from(JSON.stringify({foo: "bar"}))), |
| 76 | + new Component({}, [0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61, 0x72]) |
| 77 | + ]) |
| 78 | + ], |
| 79 | + "your-custom-boundary", // or omit to generate a random one |
| 80 | + "multipart/mixed" |
| 81 | + ); |
| 82 | + // use the content-type header generated by the multipart |
| 83 | + res.setHeader("Content-Type", multipart.headers.get("Content-Type")!); |
| 84 | + // send the multipart body bytes |
| 85 | + res.end(multipart.body); |
| 86 | + } |
| 87 | + else res.end(); |
| 88 | +}).listen(3000); |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | +### Browser |
| 93 | + |
| 94 | +```ts |
| 95 | +import {Multipart, Component} from "multipart-ts"; |
| 96 | + |
| 97 | +//create |
| 98 | +const multipart = new Multipart( |
| 99 | + [new Component({"Content-Type": "text/plain"}, new TextEncoder().encode("Hello world!"))], |
| 100 | + "your-custom-boundary", // or omit to generate a random one |
| 101 | + "multipart/mixed" |
| 102 | +); |
| 103 | + |
| 104 | +// parse |
| 105 | +const data: Uint8Array = multipart.bytes(); |
| 106 | +const parsed: Multipart = Multipart.parse(data); |
| 107 | + |
| 108 | +console.log(new TextDecoder().decode(parsed.parts[0].body)); // Hello world! |
| 109 | +``` |
0 commit comments