Registry / http-networking / http-parser-js

http-parser-js

JSON →
library0.5.10jsnpmunverified

http-parser-js is a JavaScript implementation of an HTTP protocol parser, designed to replace Node.js's internal C-based `http_parser.c` module. Its primary differentiator is its flexibility and tolerance for malformed or non-standard HTTP data, making it suitable for interacting with legacy services that do not strictly adhere to HTTP parsing rules, unlike Node.js's default stricter parser. The package is currently at version 0.5.10. It does not follow a strict release cadence but updates as needed for compatibility or bug fixes. While originally created to mitigate V8's C++ function call overhead, its current main benefit lies in its error tolerance. It can be used both as a monkey-patch replacement for Node.js's internal parser or as a standalone component for parsing raw HTTP buffers.

npm install http-parser-js
INSTALL
IMPORT
SIG · HTTP-PARSER-JS
H
http-parser-js
http-networkingjavascriptv0.5.10
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.

HTTPParser
import { HTTPParser } from 'http-parser-js';
const HTTPParser = require('http-parser-js').HTTPParser;
While Node.js uses CommonJS, this project also ships TypeScript types and can be imported via ESM syntax in environments supporting it, although its primary use case involves CommonJS `require` for monkey-patching.
monkey-patching entry
process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;
require('http-parser-js'); // This only imports the module, not patches.
This specific line must be executed *before* `require('http')` to effectively replace Node.js's internal parser.

This example demonstrates how to monkey-patch Node.js's internal HTTP parser with `http-parser-js` before initializing the native `http` module, then starts a basic HTTP server.

import { HTTPParser } from 'http-parser-js'; import http from 'http'; // Monkey patch before you require http for the first time. // Note: This specific usage pattern primarily targets CommonJS environments in Node.js. // For ESM, you might need a different approach or a build step to ensure `require` behavior. if (typeof process.binding === 'function' && process.binding('http_parser')) { process.binding('http_parser').HTTPParser = HTTPParser; // Assuming HTTPParser is already imported console.log('http-parser-js successfully monkey-patched into Node.js runtime.'); } // Now, `http` module will use http-parser-js const server = http.createServer((req, res) => { console.log(`Received request for: ${req.url}`); res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello from http-parser-js powered server!\n'); }); server.listen(3000, () => { console.log('Server running on port 3000'); }); // Example of making a request to demonstrate it's working // setTimeout(() => { // http.get('http://localhost:3000/', (res) => { // let data = ''; // res.on('data', (chunk) => { data += chunk; }); // res.on('end', () => { console.log('Client received:', data); server.close(); }); // }).on('error', (err) => { // console.error('Client error:', err.message); // server.close(); // }); // }, 1000);
Debug
Known issues
breakingNode.js v12.x renamed its internal HTTP parser and did not expose it for direct monkey-patching by default. Attempting to patch without a specific flag will fail.
fix
Run Node.js v12.x with `node --http-parser=legacy file.js` to opt into the older, patchable binding.
affects: =12.x
gotchaMonkey-patching must occur *before* the native Node.js `http` module is `require`d for the first time. Loading `http` prematurely will result in it using the default C++ parser.
fix
Ensure the `process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;` line is at the absolute top of your application's entry point, before any `require('http')` statements.
affects: >=0.5.0
gotchaThe API for standalone usage (not monkey-patching) is described as 'somewhat awkward' due to its adherence to Node.js internal compatibility. Directly using `HTTPParser` requires careful handling of raw buffers and state management.
fix
Refer to the `standalone-example.js` in the package's GitHub repository for guidance on how to use the raw `HTTPParser` class for manual parsing.
affects: >=0.5.0
gotchaThis library is designed to replace Node.js's internal C parser for specific use cases (tolerance, pure JS). For most standard applications, Node.js's default parser offers superior performance due to its C++ implementation.
fix
Only use this package if you specifically need its tolerant parsing behavior or have strong reasons to avoid C++ bindings; otherwise, rely on Node.js's default `http` module behavior.
affects: >=0.5.0
Errors
Common errors & fixes
TypeError: process.binding(...).HTTPParser is not a constructor
Attempting to monkey-patch `http-parser-js` on Node.js v12.x without enabling the legacy HTTP parser.
fix
Execute your Node.js application with the flag: `node --http-parser=legacy your-app.js`
Error: Cannot find module 'http-parser-js'
The package `http-parser-js` has not been installed as a dependency.
fix
Install the package: `npm install http-parser-js` or `yarn add http-parser-js`
The Node.js `http` module is still using the default parser, despite patching attempts.
The monkey-patching code was executed *after* the `http` module was first `require`d in the application lifecycle.
fix
Move the `process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;` line to the very top of your application's main entry file, ensuring it runs before any other code that might implicitly or explicitly load the `http` module.
Upgrade
Version history
0.5.10latest on npm
Audit
Dependencies

No dependency data recorded yet.

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