Skip to content

Commit

Permalink
add node:net module
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jan 9, 2025
1 parent 9bc5f08 commit 4f60ae6
Show file tree
Hide file tree
Showing 8 changed files with 1,516 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/node/internal/compatibility-flags.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ export const workerdExperimental: boolean;
export const durableObjectGetExisting: boolean;
export const vectorizeQueryMetadataOptional: boolean;
export const nodeJsZlib: boolean;
export const nodeJsCompatNet: boolean;
49 changes: 49 additions & 0 deletions src/node/internal/internal_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,55 @@ export class DnsError extends NodeError {
}
}

export class ERR_OPTION_NOT_IMPLEMENTED extends NodeError {
constructor(name: string | symbol) {
if (typeof name === 'symbol') {
name = (name as symbol).description!;
}
super(
'ERR_OPTION_NOT_IMPLEMENTED',
`The ${name} option is not implemented`
);
}
}

export class ERR_SOCKET_BAD_PORT extends NodeError {
constructor(name: string, port: any, allowZero: boolean) {
const operator = allowZero ? '>=' : '>';
super(
'ERR_SOCKET_BAD_PORT',
`${name} should be ${operator} 0 and < 65536. Received ${typeof port}.`
);
}
}

export class EPIPE extends NodeError {
constructor() {
super('EPIPE', 'This socket has been ended by the other party');
}
}

export class ERR_SOCKET_CLOSED_BEFORE_CONNECTION extends NodeError {
constructor() {
super(
'ERR_SOCKET_CLOSED_BEFORE_CONNETION',
'Socket closed before connection established'
);
}
}

export class ERR_SOCKET_CLOSED extends NodeError {
constructor() {
super('ERR_SOCKET_CLOSED', 'Socket is closed');
}
}

export class ERR_SOCKET_CONNECTING extends NodeError {
constructor() {
super('ERR_SOCKET_CONNECTING', 'Socket is already connecting');
}
}

export function aggregateTwoErrors(innerError: any, outerError: any) {
if (innerError && outerError && innerError !== outerError) {
if (Array.isArray(outerError.errors)) {
Expand Down
25 changes: 25 additions & 0 deletions src/node/internal/sockets.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Buffer } from 'node-internal:internal_buffer';

declare namespace sockets {
function connect(
input: string,
options: Record<string, unknown>
): {
opened: Promise<void>;
closed: Promise<void>;
close(): Promise<void>;
readable: {
getReader(options: Record<string, string>): {
close(): Promise<void>;
read(value: unknown): Promise<{ value: Buffer; done: boolean }>;
};
};
writable: {
getWriter(): {
close(): Promise<void>;
write(data: string | ArrayBufferView): Promise<void>;
};
};
};
}
export default sockets;
7 changes: 7 additions & 0 deletions src/node/internal/streams_duplex.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {
Duplex,
Writable,
WritableOptions,
Readable,
ReadableOptions,
} from 'node:stream';
21 changes: 20 additions & 1 deletion src/node/internal/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ import { normalizeEncoding } from 'node-internal:internal_utils';
import {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_SOCKET_BAD_PORT,
ERR_OUT_OF_RANGE,
} from 'node-internal:internal_errors';
import { default as bufferUtil } from 'node-internal:buffer';

// TODO(someday): Not current implementing parseFileMode, validatePort
// TODO(someday): Not current implementing parseFileMode

export function isInt32(value: unknown): value is number {
// @ts-expect-error Due to value being unknown
Expand Down Expand Up @@ -301,6 +302,23 @@ export function checkRangesOrGetDefault(
return number;
}

export function validatePort(
port: unknown,
name = 'Port',
allowZero = true
): number {
if (
(typeof port !== 'number' && typeof port !== 'string') ||
(typeof port === 'string' && port.trim().length === 0) ||
+port !== +port >>> 0 ||
+port > 0xffff ||
(port === 0 && !allowZero)
) {
throw new ERR_SOCKET_BAD_PORT(name, port, allowZero);
}
return +port | 0;
}

export default {
isInt32,
isUint32,
Expand All @@ -316,6 +334,7 @@ export default {
validateOneOf,
validateString,
validateUint32,
validatePort,

// Zlib specific
checkFiniteNumber,
Expand Down
Loading

0 comments on commit 4f60ae6

Please sign in to comment.