Skip to content

Commit

Permalink
Revert "Refactor TCP and TLS code to remove toPromise() wrapper (f3…
Browse files Browse the repository at this point in the history
…05fd4)"

Seems to have some issues with TLS, getting error like:

`poll() error: Bad file descriptor`
TooTallNate committed Oct 28, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 8d86759 commit 3b0f156
Showing 8 changed files with 161 additions and 123 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-bags-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nx.js/runtime": patch
---

Revert "Refactor TCP and TLS code to remove `toPromise()` wrapper (f305fd4)"
25 changes: 19 additions & 6 deletions packages/runtime/src/$.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import type {
Versions,
} from './switch';
import type {
Callback,
Keys,
Opaque,
RGBA,
@@ -245,9 +246,9 @@ export interface Init {
swkbdUpdate(this: VirtualKeyboard): void;

// tcp.c
connect(ip: string, port: number): Promise<number>;
write(fd: number, data: ArrayBuffer): Promise<number>;
read(fd: number, buffer: ArrayBuffer): Promise<number>;
connect(cb: Callback<number>, ip: string, port: number): void;
write(cb: Callback<number>, fd: number, data: ArrayBuffer): void;
read(cb: Callback<number>, fd: number, buffer: ArrayBuffer): void;
close(fd: number): void;
tcpServerInit(c: any): void;
tcpServerNew(
@@ -257,9 +258,21 @@ export interface Init {
): Server;

// tls.c
tlsHandshake(fd: number, hostname: string): Promise<TlsContextOpaque>;
tlsWrite(ctx: TlsContextOpaque, data: ArrayBuffer): Promise<number>;
tlsRead(ctx: TlsContextOpaque, buffer: ArrayBuffer): Promise<number>;
tlsHandshake(
cb: Callback<TlsContextOpaque>,
fd: number,
hostname: string,
): void;
tlsWrite(
cb: Callback<number>,
ctx: TlsContextOpaque,
data: ArrayBuffer,
): void;
tlsRead(
cb: Callback<number>,
ctx: TlsContextOpaque,
buffer: ArrayBuffer,
): void;

// url.c
urlInit(c: ClassOf<URL>): void;
16 changes: 16 additions & 0 deletions packages/runtime/src/internal.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,22 @@ export type WasmModuleOpaque = Opaque<'WasmModuleOpaque'>;
export type WasmInstanceOpaque = Opaque<'WasmInstanceOpaque'>;
export type WasmGlobalOpaque = Opaque<'WasmGlobalOpaque'>;

export type Callback<T> = (err: Error | null, result: T) => void;

export type CallbackReturnType<T> = T extends (
fn: Callback<infer U>,
...args: any[]
) => any
? U
: never;

export type CallbackArguments<T> = T extends (
fn: Callback<any>,
...args: infer U
) => any
? U
: never;

export interface SocketOptionsInternal extends SocketOptions {
connect: typeof connect;
}
13 changes: 7 additions & 6 deletions packages/runtime/src/tcp.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import {
bufferSourceToArrayBuffer,
createInternal,
proto,
toPromise,
} from './utils';
import type { BufferSource } from './types';
import {
@@ -31,31 +32,31 @@ export async function connect(opts: SocketAddress) {
if (!ip) {
throw new Error(`Could not resolve "${hostname}" to an IP address`);
}
return $.connect(ip, port);
return toPromise($.connect, ip, port);
}

function read(fd: number, buffer: BufferSource) {
const ab = bufferSourceToArrayBuffer(buffer);
return $.read(fd, ab);
return toPromise($.read, fd, ab);
}

function write(fd: number, data: BufferSource) {
const ab = bufferSourceToArrayBuffer(data);
return $.write(fd, ab);
return toPromise($.write, fd, ab);
}

function tlsHandshake(fd: number, hostname: string) {
return $.tlsHandshake(fd, hostname);
return toPromise($.tlsHandshake, fd, hostname);
}

function tlsRead(ctx: TlsContextOpaque, buffer: BufferSource) {
const ab = bufferSourceToArrayBuffer(buffer);
return $.tlsRead(ctx, ab);
return toPromise($.tlsRead, ctx, ab);
}

function tlsWrite(ctx: TlsContextOpaque, data: BufferSource) {
const ab = bufferSourceToArrayBuffer(data);
return $.tlsWrite(ctx, ab);
return toPromise($.tlsWrite, ctx, ab);
}

interface SocketInternal {
26 changes: 25 additions & 1 deletion packages/runtime/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { PathLike } from './switch';
import type { BufferSource } from './types';
import { INTERNAL_SYMBOL, type RGBA } from './internal';
import {
INTERNAL_SYMBOL,
type Callback,
type CallbackArguments,
type CallbackReturnType,
type RGBA,
} from './internal';

export const proto = <T extends new (...args: any) => any>(
o: any,
@@ -52,6 +58,24 @@ export function asyncIteratorToStream<T>(it: AsyncIterableIterator<T>) {
});
}

export function toPromise<
Func extends (cb: Callback<any>, ...args: any[]) => any,
>(fn: Func, ...args: CallbackArguments<Func>) {
return new Promise<CallbackReturnType<Func>>((resolve, reject) => {
try {
fn(
(err, result) => {
if (err) return reject(err);
resolve(result);
},
...args,
);
} catch (err) {
reject(err);
}
});
}

export function assertInternalConstructor(a: ArrayLike<any>) {
if (a[0] !== INTERNAL_SYMBOL) throw new TypeError('Illegal constructor');
}
Loading

0 comments on commit 3b0f156

Please sign in to comment.