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

fix(ext/net): node compatibility issue missing fd in createServer callback socket object #27789

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 11 additions & 6 deletions ext/net/01_net.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
BadResourcePrototype,
InterruptedPrototype,
internalRidSymbol,
internalFdSymbol,
createCancelHandle,
} = core;
import {
Expand Down Expand Up @@ -99,13 +100,17 @@ class Conn {

#readable;
#writable;

constructor(rid, remoteAddr, localAddr) {
constructor(rid, remoteAddr, localAddr, fd) {
ObjectDefineProperty(this, internalRidSymbol, {
__proto__: null,
enumerable: false,
value: rid,
});
ObjectDefineProperty(this, internalFdSymbol, {
__proto__: null,
enumerable: false,
value: fd,
});
this.#rid = rid;
this.#remoteAddr = remoteAddr;
this.#localAddr = localAddr;
Expand Down Expand Up @@ -211,8 +216,8 @@ class UpgradedConn extends Conn {
class TcpConn extends Conn {
#rid = 0;

constructor(rid, remoteAddr, localAddr) {
super(rid, remoteAddr, localAddr);
constructor(rid, remoteAddr, localAddr, fd) {
super(rid, remoteAddr, localAddr, fd);
ObjectDefineProperty(this, internalRidSymbol, {
__proto__: null,
enumerable: false,
Expand Down Expand Up @@ -278,12 +283,12 @@ class Listener {
}
this.#promise = promise;
if (this.#unref) core.unrefOpPromise(promise);
const { 0: rid, 1: localAddr, 2: remoteAddr } = await promise;
const { 0: rid, 1: localAddr, 2: remoteAddr, 3: fd } = await promise;
this.#promise = null;
if (this.addr.transport == "tcp") {
localAddr.transport = "tcp";
remoteAddr.transport = "tcp";
return new TcpConn(rid, remoteAddr, localAddr);
return new TcpConn(rid, remoteAddr, localAddr, fd);
} else if (this.addr.transport == "unix") {
return new UnixConn(
rid,
Expand Down
15 changes: 13 additions & 2 deletions ext/net/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::cell::RefCell;
use std::net::Ipv4Addr;
use std::net::Ipv6Addr;
use std::net::SocketAddr;
use std::os::fd::AsFd;
use std::os::fd::AsRawFd;
use std::rc::Rc;
use std::str::FromStr;

Expand Down Expand Up @@ -46,6 +48,8 @@ use crate::resolve_addr::resolve_addr_sync;
use crate::tcp::TcpListener;
use crate::NetPermissions;

pub type Fd = u32;

#[derive(Serialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TlsHandshakeInfo {
Expand Down Expand Up @@ -165,7 +169,7 @@ pub(crate) fn accept_err(e: std::io::Error) -> NetError {
pub async fn op_net_accept_tcp(
state: Rc<RefCell<OpState>>,
#[smi] rid: ResourceId,
) -> Result<(ResourceId, IpAddr, IpAddr), NetError> {
) -> Result<(ResourceId, IpAddr, IpAddr, Fd), NetError> {
let resource = state
.borrow()
.resource_table
Expand All @@ -180,14 +184,21 @@ pub async fn op_net_accept_tcp(
.try_or_cancel(cancel)
.await
.map_err(accept_err)?;
let fd = tcp_stream.as_fd();
let fd_raw = fd.as_raw_fd() as u32;
let local_addr = tcp_stream.local_addr()?;
let remote_addr = tcp_stream.peer_addr()?;

let mut state = state.borrow_mut();
let rid = state
.resource_table
.add(TcpStreamResource::new(tcp_stream.into_split()));
Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr)))
Ok((
rid,
IpAddr::from(local_addr),
IpAddr::from(remote_addr),
fd_raw,
))
}

#[op2(async)]
Expand Down
14 changes: 14 additions & 0 deletions ext/node/polyfills/internal_binding/tcp_wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials

import { core } from "ext:core/mod.js";
const { internalFdSymbol } = core;
import { notImplemented } from "ext:deno_node/_utils.ts";
import { unreachable } from "ext:deno_node/_util/asserts.ts";
import { ConnectionWrap } from "ext:deno_node/internal_binding/connection_wrap.ts";
Expand All @@ -47,6 +49,8 @@ import {
} from "ext:deno_node/internal_binding/_listen.ts";
import { nextTick } from "ext:deno_node/_next_tick.ts";

const fdSymbol: unique symbol = Symbol("fdSymbol");

/** The type of TCP socket. */
enum socketType {
SOCKET,
Expand Down Expand Up @@ -101,6 +105,8 @@ export class TCP extends ConnectionWrap {
#closed = false;
#acceptBackoffDelay?: number;

[fdSymbol]: number = -1;

/**
* Creates a new TCP class instance.
* @param type The socket type.
Expand All @@ -127,6 +133,10 @@ export class TCP extends ConnectionWrap {

super(provider, conn);

if (this[kStreamBaseField]) {
this[fdSymbol] = this[kStreamBaseField][internalFdSymbol];
}

// TODO(cmorten): the handling of new connections and construction feels
// a little off. Suspect duplicating in some fashion.
if (conn && provider === providerType.TCPWRAP) {
Expand All @@ -141,6 +151,10 @@ export class TCP extends ConnectionWrap {
}
}

get fd() {
return this[fdSymbol];
}

/**
* Opens a file descriptor.
* @param fd The file descriptor to open.
Expand Down
Loading