Skip to content

Commit

Permalink
Refactor TCP and TLS code to remove toPromise() wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 26, 2024
1 parent bb94e10 commit f305fd4
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 156 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-pandas-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nx.js/runtime": patch
---

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

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

// tls.c
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;
tlsHandshake(fd: number, hostname: string): Promise<TlsContextOpaque>;
tlsWrite(ctx: TlsContextOpaque, data: ArrayBuffer): Promise<number>;
tlsRead(ctx: TlsContextOpaque, buffer: ArrayBuffer): Promise<number>;

// url.c
urlInit(c: ClassOf<URL>): void;
Expand Down
16 changes: 0 additions & 16 deletions packages/runtime/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@ 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;
}
Expand Down
13 changes: 6 additions & 7 deletions packages/runtime/src/tcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
bufferSourceToArrayBuffer,
createInternal,
proto,
toPromise,
} from './utils';
import type { BufferSource } from './types';
import {
Expand All @@ -32,31 +31,31 @@ export async function connect(opts: SocketAddress) {
if (!ip) {
throw new Error(`Could not resolve "${hostname}" to an IP address`);
}
return toPromise($.connect, ip, port);
return $.connect(ip, port);
}

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

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

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

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

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

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

export const proto = <T extends new (...args: any) => any>(
o: any,
Expand Down Expand Up @@ -58,24 +52,6 @@ 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');
}
Expand Down
Loading

0 comments on commit f305fd4

Please sign in to comment.