-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Q: How to import the bindings in Node.js? #44
Comments
Hello! That stackoverflow page is referring to It is not clear what you'd like to do, however. Do you want to run old HTTP parser or new HTTP parser? Do you want to run it manually (i.e. outside of |
@indutny Thanks for the quick answer! You're right, I linked to the wrong snippet, I meant to link to the native bindings: https://stackoverflow.com/a/42003256 Yes, the idea would be to run the new HTTP parser that you wrote, llhttp, but manually in Node.js in a similar way than in the stackoverflow answer. I know that this is pretty much what the HTTP module does, but I'd like to be able to recreate something similar on my own. |
The |
Hmm, then I'm encountering a bug I believe in
I'm getting the following error:
|
Wow, it's actually even weirder than I thought.
sometimes with only the abort trap error:
and then a few times:
Even on the latest version, |
It is likely that there is some mandatory callback missing. Let me see. As for your case, wouldn't it be easier (and less dependent on the internals) to just create a server and do |
Ah, you haven't initialized the parser:
As I said before, it is not easy to use this outside of Node.js core... |
Thanks for the follow-up!
I understand that it's tricky but I would still like to try to call the parser manually because it was possible in the past and it let's you try out a lot of things on the parser directly. |
Could you try calling |
@indutny It seems to work now. Thank you very much for the support and sorry for asking silly questions. :) Have a nice evening. |
Np at all. Glad it worked! You have a good evening as well! |
For anyone else curious about a minimal way to utilize these parsers, as mentioned above, here's a code snippet. import { createServer, request } from 'http'
import { PassThrough } from 'node:stream'
const parseRequest = (data) => new Promise(resolve =>
createServer()
.on('request', resolve)
.on('connection', stream => stream.write(data))
.emit('connection', new PassThrough())
)
const parseResponse = (data) => new Promise(resolve =>
request({ createConnection: () => new PassThrough() })
.on('socket', stream => stream.write(data))
.on('response', resolve)
)
const parsedRequest = await parseRequest(Buffer.from('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n'))
const parsedResponse = await parseResponse(Buffer.from('HTTP/1.1 200 OK\r\n\r\n')) For other ways of accessing these parsers, see here: gr3p1p3/transparent-proxy#30 |
For those wondering, getting import { HTTPParser } from 'node:_http_common' |
is |
It's not a public API and there is no need for it to be. |
Hi @indutny,
It used to be possible to import the old HTTP parser by loading the bindings in Node.js.
I looked into llhttp and the parser generator project but couldn't find an immediate way to do this.
Would you have an example snippet that would let you run the HTTP parser manually in Node.js?
Something similar to: https://stackoverflow.com/a/30180574
Thank you a lot in advance!
The text was updated successfully, but these errors were encountered: