HTTP and RFC822 Message Object Model
raw JSON →messy is a JavaScript library providing a robust object model for parsing, manipulating, and serializing HTTP messages (requests and responses) and RFC822 (email) messages. It aims to simplify interaction with these complex message formats by abstracting header parsing, body handling, and common operations into a coherent API. The current stable version, 7.0.0, was released in November 2020. Given the last release date, the package is likely in a maintenance state, indicating stability rather than active feature development. It differentiates itself by offering a unified approach to both HTTP and email message structures, which often share similar header-based formats, making it suitable for applications requiring deep inspection or modification of network and mail protocols.
Common errors
error TypeError: Cannot read properties of undefined (reading 'headers') ↓
headers property: new HttpRequest({ headers: {} }). error Error: Invalid message format: Missing required header components ↓
HTTP/1.1 200 OK) and a correctly delimited header section. Warnings
gotcha The `messy` package, version 7.0.0, was last published in November 2020. While stable for its defined purpose, this means it may not receive active feature updates or rapid bug fixes. Users should evaluate its suitability for projects requiring modern protocol features or very active maintenance. ↓
breaking As a major version release, `messy` v7.0.0 likely introduced breaking changes from previous 6.x versions, common in JavaScript libraries of that era. These often include API cleanups, removal of deprecated methods, or adjustments for newer Node.js versions or JavaScript features. Specific details require consulting the changelog, which is not provided in the prompt. ↓
gotcha Header names in HTTP and RFC822 messages are case-insensitive, but when interacting with `messy`'s API (e.g., `setHeader`, `getHeader`), consistent casing (e.g., 'Content-Type' vs 'content-type') is crucial for predictable behavior, even if the underlying parsing normalizes it. Inconsistencies can lead to missed headers or unexpected results. ↓
Install
npm install messy yarn add messy pnpm add messy Imports
- HttpRequest wrong
const HttpRequest = require('messy').HttpRequestcorrectimport { HttpRequest } from 'messy' - HttpResponse wrong
const HttpResponse = require('messy').HttpResponsecorrectimport { HttpResponse } from 'messy' - Rfc822Message wrong
const Rfc822Message = require('messy').Rfc822Messagecorrectimport { Rfc822Message } from 'messy'
Quickstart
import { HttpRequest, HttpResponse, Rfc822Message } from 'messy';
// Example 1: Create and manipulate an HTTP Request
const httpRequest = new HttpRequest({
method: 'GET',
url: '/api/users?id=123',
headers: {
'Host': 'example.com',
'Accept': 'application/json',
'User-Agent': 'MessyClient/1.0'
}
});
httpRequest.setHeader('Authorization', 'Bearer token123');
console.log(`HTTP Request Method: ${httpRequest.method}`);
console.log(`HTTP Request URL: ${httpRequest.url}`);
console.log(`HTTP Request Auth Header: ${httpRequest.getHeader('Authorization')}`);
// Example 2: Parse a raw HTTP Response string
const rawHttpResponse = [
'HTTP/1.1 200 OK',
'Content-Type: application/json',
'Content-Length: 28',
'',
'{"message": "Hello, World!"}'
].join('\r\n');
const httpResponse = HttpResponse.parse(rawHttpResponse);
console.log(`HTTP Response Status Code: ${httpResponse.statusCode}`);
console.log(`HTTP Response Content-Type: ${httpResponse.getHeader('Content-Type')}`);
httpResponse.body.then(bodyBuffer => {
console.log(`HTTP Response Body: ${bodyBuffer.toString('utf-8')}`);
});
// Example 3: Create an RFC822 (Email) Message
const emailMessage = new Rfc822Message({
headers: {
'From': 'sender@example.com',
'To': 'recipient@example.com',
'Subject': 'Hello from Messy!',
'Content-Type': 'text/plain; charset="utf-8"'
},
body: 'This is the plain text body of the email.'
});
console.log(`Email Subject: ${emailMessage.getHeader('Subject')}`);
emailMessage.body.then(bodyBuffer => {
console.log(`Email Body: ${bodyBuffer.toString('utf-8')}`);
});