txiki.js HTTP Parser Module - TypeScript Definitions
A thin wrapper around llhttp for high-performance, event-driven HTTP parsing. Supports both request and response parsing with streaming capabilities.
Complete example: Parsing a chunked response
const { Parser, RESPONSE } = import.meta.use('http');const parser = new Parser(RESPONSE);const chunks: string[] = [];const headers: Record<string, string> = {};let currentHeader = '';parser.onHeaderField = (buf, off, len) => { currentHeader = new TextDecoder().decode( buf.slice(off, off + len) ).toLowerCase();};parser.onHeaderValue = (buf, off, len) => { headers[currentHeader] = new TextDecoder().decode( buf.slice(off, off + len) );};parser.onChunkHeader = () => { console.log('New chunk detected');};parser.onBody = (buf, off, len) => { chunks.push(new TextDecoder().decode( buf.slice(off, off + len) ));};parser.onMessageComplete = () => { console.log('Message complete!');};// Simulated chunked HTTP responseconst data = new TextEncoder().encode( 'HTTP/1.1 200 OK\r\n' + 'Transfer-Encoding: chunked\r\n' + 'Content-Type: text/plain\r\n' + '\r\n' + '5\r\n' + 'Hello\r\n' + '6\r\n' + ' World\r\n' + '0\r\n' + '\r\n');const result = parser.execute(data);if (result.errno === 0) { console.log('Body:', chunks.join('')); // "Hello World" console.log('Headers:', headers);} Copy
const { Parser, RESPONSE } = import.meta.use('http');const parser = new Parser(RESPONSE);const chunks: string[] = [];const headers: Record<string, string> = {};let currentHeader = '';parser.onHeaderField = (buf, off, len) => { currentHeader = new TextDecoder().decode( buf.slice(off, off + len) ).toLowerCase();};parser.onHeaderValue = (buf, off, len) => { headers[currentHeader] = new TextDecoder().decode( buf.slice(off, off + len) );};parser.onChunkHeader = () => { console.log('New chunk detected');};parser.onBody = (buf, off, len) => { chunks.push(new TextDecoder().decode( buf.slice(off, off + len) ));};parser.onMessageComplete = () => { console.log('Message complete!');};// Simulated chunked HTTP responseconst data = new TextEncoder().encode( 'HTTP/1.1 200 OK\r\n' + 'Transfer-Encoding: chunked\r\n' + 'Content-Type: text/plain\r\n' + '\r\n' + '5\r\n' + 'Hello\r\n' + '6\r\n' + ' World\r\n' + '0\r\n' + '\r\n');const result = parser.execute(data);if (result.errno === 0) { console.log('Body:', chunks.join('')); // "Hello World" console.log('Headers:', headers);}
txiki.js HTTP Parser Module - TypeScript Definitions
A thin wrapper around llhttp for high-performance, event-driven HTTP parsing. Supports both request and response parsing with streaming capabilities.
Complete example: Parsing a chunked response
Example