Skip to content

Commit

Permalink
fix(node/net): add missing ip utils and SocketAddress class
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 7, 2023
1 parent cf45410 commit 7abe54e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/runtime/_internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ export function notImplemented(name: string) {
throw new Error(`[unenv] ${name} is not implemented yet!`);
};
}

export function notImplementedClass(name: string) {
return class {
constructor() {
throw new Error(`[unenv] ${name} is not implemented yet!`);
}
}
}
34 changes: 29 additions & 5 deletions src/runtime/node/net/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
// https://nodejs.org/api/net.html
import type net from "node:net";
import { notImplemented } from "../../_internal/utils";
import * as socket from "./socket";
import { notImplemented, notImplementedClass } from "../../_internal/utils";
import { Socket, SocketAddress } from "./socket";

export * from "./socket";
export { Socket, SocketAddress } from "./socket";

export const createServer = notImplemented(
"net.createServer",
) as typeof net.createServer;

export const Server = notImplementedClass("net.Server") as typeof net.Server;

export const BlockList = notImplementedClass("net.BlockList") as typeof net.BlockList;

export const connect = notImplemented("net.connect") as typeof net.connect;

export const createConnection = notImplemented(
"net.createConnection",
) as typeof net.createConnection;

export default <typeof net>{
...socket,
const IPV4Regex = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;

Check failure on line 22 in src/runtime/node/net/index.ts

View workflow job for this annotation

GitHub Actions / ci

/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ can be optimized to /^(?:\d{1,3}\.){3}\d{1,3}$/
export const isIPv4: typeof net.isIPv4 = ((host: string) => IPV4Regex.test(host));

Check failure on line 23 in src/runtime/node/net/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unnecessary parentheses around expression

const IPV6Regex = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;

Check failure on line 25 in src/runtime/node/net/index.ts

View workflow job for this annotation

GitHub Actions / ci

/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/ can be optimized to /^([\dA-Fa-f]{1,4}:){7}[\dA-Fa-f]{1,4}$/
export const isIPv6: typeof net.isIPv6 = ((host: string) => IPV6Regex.test(host));

Check failure on line 26 in src/runtime/node/net/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unnecessary parentheses around expression

export const isIP: typeof net.isIP = (host: string) => {
if (isIPv4(host)) return 4;

Check failure on line 29 in src/runtime/node/net/index.ts

View workflow job for this annotation

GitHub Actions / ci

Expected { after 'if' condition
if (isIPv6(host)) return 6;

Check failure on line 30 in src/runtime/node/net/index.ts

View workflow job for this annotation

GitHub Actions / ci

Expected { after 'if' condition
return 0;
}

export const exports: typeof net = {
Socket: Socket as any, // TODO
Server,
BlockList,
SocketAddress,
createServer,
connect,
createConnection,
isIPv4,
isIPv6,
isIP,
};

export default exports;
13 changes: 13 additions & 0 deletions src/runtime/node/net/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,16 @@ export class Socket extends Duplex implements net.Socket {
return this;
}
}

export class SocketAddress implements net.SocketAddress {
address: string;
family: 'ipv4' | 'ipv6';
port: number;
flowlabel: number;
constructor(options: net.SocketAddress) {
this.address = options.address;
this.family = options.family ;
this.port = options.port;
this.flowlabel = options.flowlabel;
}
}

0 comments on commit 7abe54e

Please sign in to comment.