Skip to content

Commit 4f60ae6

Browse files
committed
add node:net module
1 parent 9bc5f08 commit 4f60ae6

File tree

8 files changed

+1516
-2
lines changed

8 files changed

+1516
-2
lines changed

src/node/internal/compatibility-flags.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ export const workerdExperimental: boolean;
3131
export const durableObjectGetExisting: boolean;
3232
export const vectorizeQueryMetadataOptional: boolean;
3333
export const nodeJsZlib: boolean;
34+
export const nodeJsCompatNet: boolean;

src/node/internal/internal_errors.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,55 @@ export class DnsError extends NodeError {
609609
}
610610
}
611611

612+
export class ERR_OPTION_NOT_IMPLEMENTED extends NodeError {
613+
constructor(name: string | symbol) {
614+
if (typeof name === 'symbol') {
615+
name = (name as symbol).description!;
616+
}
617+
super(
618+
'ERR_OPTION_NOT_IMPLEMENTED',
619+
`The ${name} option is not implemented`
620+
);
621+
}
622+
}
623+
624+
export class ERR_SOCKET_BAD_PORT extends NodeError {
625+
constructor(name: string, port: any, allowZero: boolean) {
626+
const operator = allowZero ? '>=' : '>';
627+
super(
628+
'ERR_SOCKET_BAD_PORT',
629+
`${name} should be ${operator} 0 and < 65536. Received ${typeof port}.`
630+
);
631+
}
632+
}
633+
634+
export class EPIPE extends NodeError {
635+
constructor() {
636+
super('EPIPE', 'This socket has been ended by the other party');
637+
}
638+
}
639+
640+
export class ERR_SOCKET_CLOSED_BEFORE_CONNECTION extends NodeError {
641+
constructor() {
642+
super(
643+
'ERR_SOCKET_CLOSED_BEFORE_CONNETION',
644+
'Socket closed before connection established'
645+
);
646+
}
647+
}
648+
649+
export class ERR_SOCKET_CLOSED extends NodeError {
650+
constructor() {
651+
super('ERR_SOCKET_CLOSED', 'Socket is closed');
652+
}
653+
}
654+
655+
export class ERR_SOCKET_CONNECTING extends NodeError {
656+
constructor() {
657+
super('ERR_SOCKET_CONNECTING', 'Socket is already connecting');
658+
}
659+
}
660+
612661
export function aggregateTwoErrors(innerError: any, outerError: any) {
613662
if (innerError && outerError && innerError !== outerError) {
614663
if (Array.isArray(outerError.errors)) {

src/node/internal/sockets.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Buffer } from 'node-internal:internal_buffer';
2+
3+
declare namespace sockets {
4+
function connect(
5+
input: string,
6+
options: Record<string, unknown>
7+
): {
8+
opened: Promise<void>;
9+
closed: Promise<void>;
10+
close(): Promise<void>;
11+
readable: {
12+
getReader(options: Record<string, string>): {
13+
close(): Promise<void>;
14+
read(value: unknown): Promise<{ value: Buffer; done: boolean }>;
15+
};
16+
};
17+
writable: {
18+
getWriter(): {
19+
close(): Promise<void>;
20+
write(data: string | ArrayBufferView): Promise<void>;
21+
};
22+
};
23+
};
24+
}
25+
export default sockets;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export {
2+
Duplex,
3+
Writable,
4+
WritableOptions,
5+
Readable,
6+
ReadableOptions,
7+
} from 'node:stream';

src/node/internal/validators.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ import { normalizeEncoding } from 'node-internal:internal_utils';
3030
import {
3131
ERR_INVALID_ARG_TYPE,
3232
ERR_INVALID_ARG_VALUE,
33+
ERR_SOCKET_BAD_PORT,
3334
ERR_OUT_OF_RANGE,
3435
} from 'node-internal:internal_errors';
3536
import { default as bufferUtil } from 'node-internal:buffer';
3637

37-
// TODO(someday): Not current implementing parseFileMode, validatePort
38+
// TODO(someday): Not current implementing parseFileMode
3839

3940
export function isInt32(value: unknown): value is number {
4041
// @ts-expect-error Due to value being unknown
@@ -301,6 +302,23 @@ export function checkRangesOrGetDefault(
301302
return number;
302303
}
303304

305+
export function validatePort(
306+
port: unknown,
307+
name = 'Port',
308+
allowZero = true
309+
): number {
310+
if (
311+
(typeof port !== 'number' && typeof port !== 'string') ||
312+
(typeof port === 'string' && port.trim().length === 0) ||
313+
+port !== +port >>> 0 ||
314+
+port > 0xffff ||
315+
(port === 0 && !allowZero)
316+
) {
317+
throw new ERR_SOCKET_BAD_PORT(name, port, allowZero);
318+
}
319+
return +port | 0;
320+
}
321+
304322
export default {
305323
isInt32,
306324
isUint32,
@@ -316,6 +334,7 @@ export default {
316334
validateOneOf,
317335
validateString,
318336
validateUint32,
337+
validatePort,
319338

320339
// Zlib specific
321340
checkFiniteNumber,

0 commit comments

Comments
 (0)