Registry / http-networking / http-message-parser

http-message-parser

JSON →
library0.0.34jsnpmunverified

http-message-parser is a JavaScript library designed to parse raw HTTP message strings or Buffers into structured JavaScript objects. It handles both request and response messages, including multipart bodies, and aims to retain original encodings by returning body content as Node.js Buffer objects. The current stable version is 0.0.34, released in 2018. Due to its age, it primarily targets older Node.js environments (engines >=0.10.0) and uses CommonJS modules. A key differentiator is its focus on binary data integrity, performing offset slices on input buffers to avoid stringifying binary content, making it suitable for parsing responses with non-textual data.

npm install http-message-parser
INSTALL
IMPORT
SIG · HTTP-MESSAGE-PARSE
H
http-message-parser
http-networkingjavascriptv0.0.34
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

httpMessageParser
const httpMessageParser = require('http-message-parser');
import httpMessageParser from 'http-message-parser';
This library is primarily CommonJS and does not officially support ESM. Direct ESM import might fail or require a CommonJS wrapper.
parseHttpMessage
const parseHttpMessage = require('http-message-parser');
The default export (or module.exports) is the parsing function itself.
httpMessageParserCli
# CLI usage as shown in documentation
The package also exposes a command-line interface, not typically imported as a symbol but used via `http-message-parser <file>` or piping input.

This quickstart demonstrates parsing a multi-part HTTP response from a Buffer and then inspecting its structured output, including decoding one of the multipart bodies.

const httpMessageParser = require('http-message-parser'); const fs = require('fs'); const path = require('path'); const exampleMessage = [ 'HTTP/1.1 200 OK', 'MIME-Version: 1.0', 'Content-Type: multipart/mixed; boundary=frontier', '', 'This is a message with multiple parts in MIME format.', '--frontier', 'Content-Type: text/plain', '', 'This is the body of the message.', '--frontier', 'Content-Type: application/octet-stream', 'Content-Transfer-Encoding: base64', '', 'PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg', 'Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==', '--frontier--' ].join('\r\n'); // Simulate reading from a file or network stream with a Buffer const messageBuffer = Buffer.from(exampleMessage, 'binary'); console.log('Parsing HTTP message...'); const parsedMessage = httpMessageParser(messageBuffer); console.log(JSON.stringify(parsedMessage, (key, value) => { // Convert Buffer to string for display, or indicate its presence if (value && typeof value === 'object' && value.type === 'Buffer') { return `<Buffer(${value.data.length} bytes)>`; } return value; }, 2)); // Accessing specific parts if (parsedMessage.multipart && parsedMessage.multipart.length > 0) { const firstPartBody = parsedMessage.multipart[0].body; console.log('\nFirst multipart body (decoded):', firstPartBody.toString('utf8')); }
Debug
Known issues
breakingThe library primarily targets Node.js versions >=0.10.0 (circa 2012) and has not been updated since 2018. It may not be fully compatible with modern Node.js features, ES modules, or recent changes in HTTP standards or Buffer API behavior.
fix
Consider alternative, actively maintained HTTP parsing libraries for modern Node.js applications or thoroughly test compatibility before production deployment. Ensure a CommonJS environment.
affects: >=0.0.35 (hypothetical), or running on Node.js >=12
gotchaAll message bodies (main body and multipart bodies) are returned as Node.js Buffer objects to preserve original encoding. This requires explicit conversion to string (e.g., `body.toString('utf8')`) if text is expected, or special handling for binary data.
fix
Always check `typeof body === 'object' && body.type === 'Buffer'` and use `body.toString('encoding')` or other Buffer methods as appropriate. In browser environments, a `Buffer` polyfill like `feross/buffer` is required for binary data handling.
affects: >=0.0.1
gotchaThe package uses `require()` and does not explicitly support ES Modules (ESM). Attempting to `import` it directly in an ESM context might lead to errors or unexpected behavior unless specific Node.js interop flags are used.
fix
Use CommonJS `require()` for importing the library. If absolutely necessary in an ESM project, use dynamic `import()` or review Node.js's CJS-ESM interop documentation.
affects: >=0.0.1
Errors
Common errors & fixes
httpMessageParser is not a function
Attempting to import the module incorrectly in an ESM context or trying to destructure a non-object export.
fix
Ensure you are using `const httpMessageParser = require('http-message-parser');` in a CommonJS module or environment.
Buffer is not defined
Using the library in a browser environment without providing a `Buffer` polyfill.
fix
Install and configure a `Buffer` polyfill (e.g., `npm install buffer` and import it) for browser environments when dealing with binary data.
TypeError: Cannot read properties of undefined (reading 'headers')
Attempting to access properties like 'headers' or 'body' on a `parsedMessage` object that might be `null` or have an unexpected structure if the input message was malformed or empty.
fix
Always check the `parsedMessage` object and its sub-properties (e.g., `parsedMessage && parsedMessage.headers`) for existence before accessing them, especially when handling arbitrary or potentially invalid input.
Upgrade
Version history
0.0.34latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
http-message-parser — npm install http-message-parser · libregistry