HTTP and RFC822 Message Object Model

raw JSON →
7.0.0 verified Thu Apr 23 auth: no javascript maintenance

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.

error TypeError: Cannot read properties of undefined (reading 'headers')
cause Attempting to create a message object (e.g., HttpRequest, Rfc822Message) without providing the required `headers` property in the constructor options object.
fix
Ensure the constructor is called with an object containing at least an empty headers property: new HttpRequest({ headers: {} }).
error Error: Invalid message format: Missing required header components
cause Parsing a malformed or incomplete raw message string using `HttpRequest.parse()` or `HttpResponse.parse()`. This error typically indicates fundamental structural issues like missing the status line or a malformed header section.
fix
Validate the input raw message string against HTTP/RFC822 specifications before attempting to parse it. Ensure it has at least a valid start line (e.g., HTTP/1.1 200 OK) and a correctly delimited header section.
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.
fix Consider checking the GitHub repository for recent activity or forks if active development is a strict requirement for your project.
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.
fix Thoroughly review the package's changelog or migration guide for version 7.x, if available on its GitHub repository, before upgrading from 6.x. Test existing codebases extensively after upgrading.
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.
fix Always use a canonical casing (e.g., 'Title-Case') for header names when programmatically accessing or modifying them, or use helper functions if the library provides case-insensitive accessors.
npm install messy
yarn add messy
pnpm add messy

This quickstart demonstrates creating HTTP request and RFC822 email message objects, setting headers, and parsing a raw HTTP response string to extract its status, headers, and body. It showcases the core functionalities of the `messy` library.

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')}`);
});