Pure JavaScript HTTP Parser
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.
Common errors
-
TypeError: process.binding(...).HTTPParser is not a constructor
cause Attempting to monkey-patch `http-parser-js` on Node.js v12.x without enabling the legacy HTTP parser.fixExecute your Node.js application with the flag: `node --http-parser=legacy your-app.js` -
Error: Cannot find module 'http-parser-js'
cause The package `http-parser-js` has not been installed as a dependency.fixInstall 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.
cause The monkey-patching code was executed *after* the `http` module was first `require`d in the application lifecycle.fixMove 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.
Warnings
- breaking Node.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.
- gotcha Monkey-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.
- gotcha The 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.
- gotcha This 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.
Install
-
npm install http-parser-js -
yarn add http-parser-js -
pnpm add http-parser-js
Imports
- HTTPParser
const HTTPParser = require('http-parser-js').HTTPParser;import { HTTPParser } from 'http-parser-js'; - monkey-patching entry
require('http-parser-js'); // This only imports the module, not patches.process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;
Quickstart
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);