{"id":13579,"library":"multipasta","title":"multipasta - Cross-platform Multipart Parser","description":"multipasta is a cross-platform parser specifically designed for `multipart/form-data` payloads, commonly used for file uploads in web applications. It provides a stream-based API for handling incoming data, making it suitable for both Node.js environments (where it leverages Node's `Buffer` capabilities efficiently) and browser-like runtimes. The current stable version is 0.2.7, with frequent patch releases addressing bug fixes and minor improvements, as seen in recent changelogs. Minor version bumps (e.g., v0.2.0) introduce new features or behavior changes. Key differentiators include its cross-platform compatibility, TypeScript type definitions, and focus on efficient, stream-based parsing of multipart bodies.","status":"active","version":"0.2.7","language":"javascript","source_language":"en","source_url":"https://github.com/tim-smart/multipasta","tags":["javascript","typescript"],"install":[{"cmd":"npm install multipasta","lang":"bash","label":"npm"},{"cmd":"yarn add multipasta","lang":"bash","label":"yarn"},{"cmd":"pnpm add multipasta","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Primarily designed for ESM. CommonJS `require` might work in some Node.js setups but ESM is recommended for full compatibility and type inference.","wrong":"const { MultipartParser } = require('multipasta');","symbol":"MultipartParser","correct":"import { MultipartParser } from 'multipasta';"},{"note":"FileStream is a named export, not a default export.","wrong":"import FileStream from 'multipasta';","symbol":"FileStream","correct":"import { FileStream } from 'multipasta';"},{"note":"When importing types in TypeScript, use `import type` for clarity and to ensure it's removed during transpilation if not needed at runtime.","wrong":"import { MultipartParserOptions } from 'multipasta';","symbol":"MultipartParserOptions","correct":"import type { MultipartParserOptions } from 'multipasta';"}],"quickstart":{"code":"import { MultipartParser } from 'multipasta';\nimport { Readable } from 'stream';\n\nconst boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';\nconst multipartBody = `--${boundary}\\r\\n` +\n  'Content-Disposition: form-data; name=\"text_field\"\\r\\n\\r\\n' +\n  'Some text value\\r\\n' +\n  `--${boundary}\\r\\n` +\n  'Content-Disposition: form-data; name=\"file_field\"; filename=\"hello.txt\"\\r\\n' +\n  'Content-Type: text/plain\\r\\n\\r\\n' +\n  'Hello, world!\\nThis is a test file.\\r\\n' +\n  `--${boundary}--\\r\\n`;\n\nconst parser = new MultipartParser({\n  boundary: Buffer.from(boundary),\n});\n\nconst readableStream = Readable.from(multipartBody);\n\nreadableStream.on('data', (chunk) => {\n  parser.write(chunk);\n});\n\nparser.on('field', (field) => {\n  console.log(`Field: ${field.name.toString()} = ${field.value.toString()}`);\n});\n\nparser.on('file', (file) => {\n  console.log(`File received: Name=${file.name.toString()}, Filename=${file.filename?.toString() ?? 'N/A'}, Content-Type=${file.contentType?.toString() ?? 'N/A'}`);\n  let fileContent = Buffer.alloc(0);\n  file.on('data', (chunk) => {\n    fileContent = Buffer.concat([fileContent, chunk]);\n  });\n  file.on('end', () => {\n    console.log(`File content for ${file.filename?.toString()}:\\n${fileContent.toString()}`);\n  });\n});\n\nparser.on('end', () => {\n  console.log('Multipart parsing complete.');\n});\n\nreadableStream.on('end', () => {\n  parser.end();\n});\n\nreadableStream.on('error', (err) => {\n  console.error('Stream error:', err);\n});\n\nparser.on('error', (err) => {\n  console.error('Parser error:', err);\n});\n","lang":"typescript","description":"Demonstrates parsing a multipart/form-data request body from a Node.js Readable stream, extracting both text fields and file content."},"warnings":[{"fix":"Ensure robust error handling in your application logic. Any processing of emitted parts should consider that `error` events might occur after `field` or `file` events, and that subsequent parts will not be emitted.","message":"As of v0.2.7, multipasta will no longer emit parts (fields or files) when an error occurs during parsing. This changes previous behavior where partial data might have been emitted before an error halted processing.","severity":"gotcha","affected_versions":">=0.2.7"},{"fix":"Applications handling file uploads should attach error listeners to `file` objects (the `FileStream` instances) in addition to the main `MultipartParser` instance to catch stream-specific errors.","message":"In v0.2.6, error propagation within the Node.js parser was improved to correctly relay errors to file streams. This ensures that issues encountered while parsing file data are surfaced appropriately through the associated `FileStream` instance.","severity":"gotcha","affected_versions":">=0.2.6"},{"fix":"Review how your application expects duplicate headers to be processed. Implement explicit logic to handle arrays of header values if needed, or ensure your `Content-Type` headers are unambiguous.","message":"Version 0.2.0 introduced changes to handle duplicate header values more robustly. While not a breaking API change, this might alter how applications that rely on specific behavior for duplicate headers (e.g., only the first or last value being kept) process their data.","severity":"gotcha","affected_versions":">=0.2.0"}],"env_vars":null,"search_vec":"'0.2.7':63 'address':68 'api':33 'applic':26 'base':32,107 'behavior':88 'bodi':111 'browser':55 'browser-lik':54 'buffer':50 'bug':69 'bump':81 'capabl':51 'chang':89 'changelog':78 'common':19 'compat':97 'cross':3,11,95 'cross-platform':2,10,94 'current':59 'data':37 'definit':100 'design':15 'differenti':91 'e.g':82 'effici':52,104 'environ':44 'featur':86 'file':22 'fix':70 'focus':102 'frequent':65 'handl':35 'improv':73 'includ':92 'incom':36 'introduc':84 'javascript':112 'key':90 'leverag':47 'like':56 'make':38 'minor':72,79 'multipart':5,110 'multipart/form-data':17 'multipasta':1,7 'new':85 'node':48 'node.js':43 'pars':108 'parser':6,13 'patch':66 'payload':18 'platform':4,12,96 'provid':28 'recent':77 'releas':67 'runtim':57 'seen':75 'specif':14 'stabl':60 'stream':31,106 'stream-bas':30,105 'suitabl':40 'type':99 'typescript':98,113 'upload':23 'use':20 'v0.2.0':83 'version':61,80 'web':25","created_at":"2026-04-20T01:55:13.680250+00:00","updated_at":"2026-04-20T01:55:13.680250+00:00","problems":[{"fix":"Ensure the `Content-Type` header (e.g., `multipart/form-data; boundary=xxxx`) is correctly extracted and the `boundary` option for `MultipartParser` is set to the correct `Buffer` representation of the boundary string.","cause":"The `Content-Type` header provided to the parser (or implicitly from the request) does not contain a `boundary` directive, or the boundary string passed to the parser options is incorrect.","error":"Error: Multipart boundary not found in Content-Type header."},{"fix":"Verify that `parser.on('file', (file) => { /* ... */ })` and `parser.on('field', (field) => { /* ... */ })` are correctly defined before data starts flowing into the parser, and that `file` and `field` objects are accessed within their respective event handlers.","cause":"This typically occurs when trying to attach an event listener to a `file` or `field` object that is `undefined` or not correctly returned/handled. It might happen if `file` or `field` events are not correctly wired up or if data is processed outside the event loop.","error":"TypeError: Cannot read properties of undefined (reading 'on')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/tim-smart/multipasta","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/multipasta","openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}