creationix / http-parser-js

A pure JS HTTP parser for node.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is the API synchronous?

tommiv opened this issue · comments

I want to use this lib (thanks for it) on tcp socket – as standalone parser. I need to emulate some legacy protocol that can send to tcp socket http data as well as raw commands. So I need to know, is this guaranteed that all callbacks are called when I call .finish()/.close()? I used this code to check things out:

const parser = new HTTPParser(HTTPParser.REQUEST);
parser[0] = () => console.log('onHeaders');
parser[1] = headersRaw => console.log('onHeadersComplete', headersRaw);
parser[2] = body => console.log('onBody', body);
parser[3] = () => console.log('onMessageComplete');

parser.execute(this.buffer);
parser.finish();
parser.close();

console.log('complete');

in my experiments complete message always showed up after all callbacks, but it would be great to be sure about it and not wrap the code in promise.

Yes, you can look at the code yourself; callbacks are only called inside parser.execute(buffer) and parser.finish().

Great, thank you for blazing fast response!