Skip to content

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

@IgorMinar

Description

@IgorMinar

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

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions