11# Multipart.ts
22
3- A TypeScript library for multipart data parsing and creation.
3+ [ ![ Documentation] ( https://img.shields.io/badge/Documentation-blue )] ( https://zefir-git.github.io/Multipart.ts )
4+ [ ![ GitHub] ( https://img.shields.io/badge/GitHub-181717?logo=github )] ( https://github.com/zefir-git/Multipart.ts )
5+ [ ![ NPM] ( https://img.shields.io/npm/v/multipart-ts.svg )] ( https://www.npmjs.com/package/multipart-ts )
6+ [ ![ Downloads] ( https://img.shields.io/npm/d18m/multipart-ts.svg )] ( https://www.npmjs.com/package/multipart-ts )
7+ [ ![ Licence] ( https://img.shields.io/github/license/zefir-git/Multipart.ts.svg )] ( https://github.com/zefir-git/Multipart.ts/blob/main/LICENSE )
8+ [ ![ Test] ( https://github.com/zefir-git/Multipart.ts/actions/workflows/test.yml/badge.svg )] ( https://github.com/zefir-git/Multipart.ts/actions/workflows/test.yml )
9+
10+ A library for parsing and creating multipart data in Node.js and browsers, with built-in TypeScript support, zero
11+ dependencies, and no need for polyfills.
412
513[ ** Documentation** ] ( https://zefir-git.github.io/Multipart.ts )
614
715## Features
816
917- 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
18+ - Create multipart messages with arbitrary parts
19+ - Initialise a Multipart instance directly from FormData
20+ - Serialise a Multipart instance into bytes ([
21+ ` Uint8Array ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array ) )
22+ - Compatible with both Node.js and browsers, without requiring polyfills
23+ - Zero dependencies — lightweight and efficient
1524
1625## Getting started
1726
18- Install using NPM
27+ Install via NPM:
1928
2029``` shell
2130npm i multipart-ts
2231```
2332
24- Import as native ESM
33+ Import as native ESM:
2534
2635``` ts
2736import {Multipart } from " multipart-ts" ;
2837```
2938
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.
39+ This library exclusively uses ` Uint8Array ` , which ensures it works seamlessly in both Node.js and browsers.
40+
41+ In Node.js, [ ` Buffer ` ] ( https://nodejs.org/api/buffer.html ) can be used interchangeably with ` Uint8Array ` in most cases,
42+ as it inherits from it.
43+
44+ In the browser, use [ ` TextEncoder ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder ) and [
45+ ` TextDecoder ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder ) to convert between strings and
46+ ` Uint8Array ` .
3547
3648## Example
3749
3850### ` node:http `
3951
52+ This Node.js example sets up a simple HTTP server that parses multipart messages from POST requests and responds with an
53+ example multipart message to GET requests.
54+
4055``` ts
4156import http from " node:http" ;
4257import {Component , Multipart } from " multipart-ts" ;
4358
4459http .createServer (async (req , res ) => {
4560 // Parse multipart request body
4661 if (req .method === " POST" && req .headers [" content-type" ]) {
47-
48- // get the request body
62+ // Get the request body
4963 const body: Uint8Array [] = [];
5064 for await (const chunk of req )
5165 body .push (chunk );
5266
53- // create a blob to hold the Content-Type header (which includes the boundary) and the body
67+ // Create a Blob to hold the Content-Type header (which includes the boundary) and the body
5468 const blob = new Blob (body , {type: req .headers [" content-type" ]});
55- // parse multipart from the blob
69+ // Parse multipart from the blob
5670 const multipart = await Multipart .blob (blob );
5771 console .log (multipart );
5872
59- res .end (" ok " );
73+ res .end (" Parsed! " );
6074 }
6175 // Return a multipart response
6276 else if (req .method === " GET" ) {
@@ -88,18 +102,23 @@ http.createServer(async (req, res) => {
88102
89103### Browser
90104
105+ A minimal example demonstrating the creation, serialisation, and parsing of a simple multipart message. This can also be
106+ done the same way in Node.js.
107+
91108``` ts
92109import {Multipart , Component } from " multipart-ts" ;
93110
94- // create
111+ // Create
95112const multipart = new Multipart (
96113 [new Component ({" Content-Type" : " text/plain" }, new TextEncoder ().encode (" Hello world!" ))],
97114 " your-custom-boundary" , // or omit to generate a random one
98115 " multipart/mixed"
99116);
100117
101- // parse
118+ // Serialise
102119const data: Uint8Array = multipart .bytes ();
120+
121+ // Parse
103122const parsed: Multipart = Multipart .parse (data );
104123
105124console .log (new TextDecoder ().decode (parsed .parts [0 ].body )); // Hello world!
@@ -110,5 +129,6 @@ console.log(new TextDecoder().decode(parsed.parts[0].body)); // Hello world!
110129
111130Copyright © 2024–2025 Zefir Kirilov.
112131
113- This project is licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0). A copy of the licence text is
114- included in this repository. If not, see https://www.gnu.org/licenses/ .
132+ This project is licensed under
133+ the [ GNU Lesser General Public License v3.0 (LGPL-3.0)] ( https://www.gnu.org/licenses/lgpl-3.0.en.html ) . A copy of the
134+ licence text is included in the repository.
0 commit comments