Skip to content
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

investigate: can we polyfill node:net#Socket with cloudflare:socket? #2129

Open
IgorMinar opened this issue May 16, 2024 · 3 comments
Open
Assignees
Labels
api feature request Request for Workers team to add a feature nodejs compat

Comments

@IgorMinar
Copy link
Contributor

IgorMinar commented May 16, 2024

We need to investigate how hard is it to build a realistic node:net#Socket polyfill using what we currently have in workerd's cloudflare:socket and nodejs_compat layer.

This came out of a discusstion about brianc/node-postgres#3170 with @petebacondarwin this morning where we are trying to improve our pg specific node:net#Socket polyfill. This made me wonder if a better path wouldn't be to beef up our node:net#Socket polyfill so that things just work.

Looking at https://github.com/brianc/node-postgres/blob/master/packages/pg-cloudflare/src/index.ts it seems that we are essentially creating a bespoke polyfill for node Socket there. So why not upstream this to unenv (or eventually workerd)?

The unenv polyfill doesn't seem terribly complex: https://github.com/unjs/unenv/blob/main/src/runtime/node/net/socket.ts because it simply extends from node:stream#Duplex.

Note: workerd has native implementation of node:stream#Duplex but since this unenv polyfill exports it via a relative import, the unenv polyfill will end up ignoring our implementation. This is something that should be fixed in unenv as part of the hybrid polyfill effort: unjs/unenv#181

@jasnell
Copy link
Member

jasnell commented May 17, 2024

We ought to be able to get very close. There will likely be some edge cases or options that we're not able to support, but for the most part it should be doable

@IgorMinar
Copy link
Contributor Author

IgorMinar commented May 21, 2024

notes from a meeting:

Main concerns:

  • timing of events
  • low level options
  • low level buffer sizes

Estimate: 1 week of work

@hoodmane
Copy link
Contributor

This would be great from the point of view of our idea of shimming Python socket with our socket. If we make the Python socket library work on top of node sockets then we can upstream it, potentially all the way into Emscripten.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api feature request Request for Workers team to add a feature nodejs compat
Projects
None yet
Development

No branches or pull requests

3 participants