-
Notifications
You must be signed in to change notification settings - Fork 3.8k
implement net.Socket
#1701
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
implement net.Socket
#1701
Conversation
We can change this manually ourselves but it's fine for a follow-up. We would call |
Is that consistent with what Jest does? |
AFAICT, yes. |
81b7104 to
658837d
Compare
| const self = socket.data; | ||
| self.bytesRead += buffer.length; | ||
| if (self.#readQueue.isEmpty()) { | ||
| if (self.push(buffer)) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we return Buffer objects in the stream class? We could add a flag to do that here because IIRC, node:stream will internally create a Buffer object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically Uint8Array (so basically Buffer from Node.js PoV) − will investigate if node:stream does extra allocation or not 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah it does in the streams code when you push a Uint8Array, it creates a new Buffer
- support TCP sockets for now, i.e. no IPC - extra features like keep-alive, no-delay etc. are absent due to limitations of uSockets - fix `jest` to treat `done(nullish)` as success
658837d to
372ee4e
Compare
| } | ||
|
|
||
| _write(chunk, encoding, callback) { | ||
| if (typeof chunk == "string" && encoding !== "utf8") chunk = Buffer.from(chunk, encoding); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we split this into two functions it might be faster:
- if (typeof chunk === "string") this.#writeString(chunk, callback)
- this.#writeBuffer(chunk, callback)
We could also add a DOMJIT binding for writeText / writeBytes, as we have done for ServerWebSocket and then we can have lower function call overhead in these functions. It would only shave off around 20ns though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify, the above code is there to handle things like socket.setEncoding("hex").write("deadbeef"), i.e. for correctness rather than performance
jestto treatdone(nullish)as success