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

refactor: Avoid accessing prototype in custom_connect #522

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/custom_connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import net from 'net';
import type http from 'http';
import { promisify } from 'util';

const asyncWrite = promisify(net.Socket.prototype.write);

export const customConnect = async (socket: net.Socket, server: http.Server): Promise<void> => {
// `countTargetBytes(socket, socket)` is incorrect here since `socket` is not a target.
// We would have to create a new stream and pipe traffic through that,
// however this would also increase CPU usage.
// Also, counting bytes here is not correct since we don't know how the response is generated
// (whether any additional sockets are used).

const asyncWrite = promisify(socket.write).bind(socket);
await asyncWrite.call(socket, 'HTTP/1.1 200 Connection Established\r\n\r\n');
JuroOravec marked this conversation as resolved.
Show resolved Hide resolved
server.emit('connection', socket);

Expand Down