Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node:dns): polyfill node:dns module #196

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/presets/nodeless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const nodeless: Preset & { alias: Map<string, string> } = {
"buffer",
"console",
"crypto",
"dns",
"dns/promises",
"events",
"fs",
"fs/promises",
Expand Down
13 changes: 13 additions & 0 deletions src/runtime/_internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ export function notImplemented<Fn extends (...args: any) => any>(
return Object.assign(fn, { __unenv__: true }) as unknown as Fn;
}

export interface Promisifiable {
(): any;
native: Promisifiable;
__promisify__: () => Promise<any>;
}

export function notImplementedAsync(name: string): Promisifiable {
const fn = notImplemented(name) as any;
fn.__promisify__ = () => notImplemented(name + ".__promisify__");
fn.native = fn;
return fn;
}

export function notImplementedClass<T = unknown>(name: string): T {
return class {
readonly __unenv__ = true;
Expand Down
30 changes: 30 additions & 0 deletions src/runtime/node/dns/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type dns from "node:dns";

export const ADDRCONFIG: typeof dns.ADDRCONFIG = 0;
export const ADDRGETNETWORKPARAMS: typeof dns.ADDRGETNETWORKPARAMS =
"ADDRGETNETWORKPARAMS";
export const ALL: typeof dns.ALL = 0;
export const BADFAMILY: typeof dns.BADFAMILY = "BADFAMILY";
export const BADFLAGS: typeof dns.BADFLAGS = "BADFLAGS";
export const BADHINTS: typeof dns.BADHINTS = "BADHINTS";
export const BADNAME: typeof dns.BADNAME = "BADNAME";
export const BADQUERY: typeof dns.BADQUERY = "BADQUERY";
export const BADRESP: typeof dns.BADRESP = "BADRESP";
export const BADSTR: typeof dns.BADSTR = "BADSTR";
export const CANCELLED: typeof dns.CANCELLED = "CANCELLED";
export const CONNREFUSED: typeof dns.CONNREFUSED = "TIMEOUT";
export const DESTRUCTION: typeof dns.DESTRUCTION = "DESTRUCTION";
export const EOF: typeof dns.EOF = "EOF";
export const FILE: typeof dns.FILE = "FILE";
export const FORMERR: typeof dns.FORMERR = "FORMERR";
export const LOADIPHLPAPI: typeof dns.LOADIPHLPAPI = "LOADIPHLPAPI";
export const NODATA: typeof dns.NODATA = "NODATA";
export const NOMEM: typeof dns.NOMEM = "NOMEM";
export const NONAME: typeof dns.NONAME = "NONAME";
export const NOTFOUND: typeof dns.NOTFOUND = "NOTFOUND";
export const NOTIMP: typeof dns.NOTIMP = "NOTIMP";
export const NOTINITIALIZED: typeof dns.NOTINITIALIZED = "NOTINITIALIZED";
export const REFUSED: typeof dns.REFUSED = "REFUSED";
export const SERVFAIL: typeof dns.SERVFAIL = "SERVFAIL";
export const TIMEOUT: typeof dns.TIMEOUT = "TIMEOUT";
export const V4MAPPED: typeof dns.V4MAPPED = 2048;
74 changes: 74 additions & 0 deletions src/runtime/node/dns/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import noop from "../../mock/noop";
import mock from "../../mock/proxy";
import {
notImplemented,
notImplementedAsync,
} from "src/runtime/_internal/utils";
import type dns from "node:dns";
import * as constants from "./constants";
import promises from "./promises";
export * from "./constants";

export const Resolver: typeof dns.Resolver =
mock.__createMock__("dns.Resolver");
export const getDefaultResultOrder: typeof dns.getDefaultResultOrder = () =>
"verbatim";
export const getServers: typeof dns.getServers = () => [];
export const lookup: typeof dns.lookup = notImplementedAsync("dns.lookup");
export const lookupService: typeof dns.lookupService =
notImplementedAsync("dns.lookupService");
export const resolve: typeof dns.resolve = notImplementedAsync("dns.resolve");
export const resolve4: typeof dns.resolve4 =
notImplementedAsync("dns.resolve4");
export const resolve6: typeof dns.resolve6 =
notImplementedAsync("dns.resolve6");
export const resolveAny: typeof dns.resolveAny =
notImplementedAsync("dns.resolveAny");
export const resolveCaa: typeof dns.resolveCaa =
notImplementedAsync("dns.resolveCaa");
export const resolveCname: typeof dns.resolveCname =
notImplementedAsync("dns.resolveCname");
export const resolveMx: typeof dns.resolveMx =
notImplementedAsync("dns.resolveMx");
export const resolveNaptr: typeof dns.resolveNaptr =
notImplementedAsync("dns.resolveNaptr");
export const resolveNs: typeof dns.resolveNs =
notImplementedAsync("dns.resolveNs");
export const resolvePtr: typeof dns.resolvePtr =
notImplementedAsync("dns.resolvePtr");
export const resolveSoa: typeof dns.resolveSoa =
notImplementedAsync("dns.resolveSoa");
export const resolveSrv: typeof dns.resolveSrv =
notImplementedAsync("dns.resolveSrv");
export const resolveTxt: typeof dns.resolveTxt =
notImplementedAsync("dns.resolveTxt");

export const reverse: typeof dns.reverse = notImplemented("dns.reverse");
export const setDefaultResultOrder: typeof dns.setDefaultResultOrder = noop;
export const setServers: typeof dns.setServers = noop;

export default <typeof dns>{
...constants,
Resolver,
getDefaultResultOrder,
getServers,
lookup,
lookupService,
promises,
resolve,
resolve4,
resolve6,
resolveAny,
resolveCaa,
resolveCname,
resolveMx,
resolveNaptr,
resolveNs,
resolvePtr,
resolveSoa,
resolveSrv,
resolveTxt,
reverse,
setDefaultResultOrder,
setServers,
};
72 changes: 72 additions & 0 deletions src/runtime/node/dns/promises/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import noop from "../../../mock/noop";
import mock from "../../../mock/proxy";
import {
notImplemented,
notImplementedAsync,
} from "src/runtime/_internal/utils";
import type dns from "node:dns/promises";
import * as constants from "../constants";
export * from "../constants";

export const Resolver: typeof dns.Resolver =
mock.__createMock__("dns.Resolver");
export const getDefaultResultOrder: typeof dns.getDefaultResultOrder = () =>
"verbatim";
export const getServers: typeof dns.getServers = () => [];
export const lookup: typeof dns.lookup = notImplementedAsync("dns.lookup");
export const lookupService: typeof dns.lookupService =
notImplementedAsync("dns.lookupService");
export const resolve: typeof dns.resolve = notImplementedAsync("dns.resolve");
export const resolve4: typeof dns.resolve4 =
notImplementedAsync("dns.resolve4");
export const resolve6: typeof dns.resolve6 =
notImplementedAsync("dns.resolve6");
export const resolveAny: typeof dns.resolveAny =
notImplementedAsync("dns.resolveAny");
export const resolveCaa: typeof dns.resolveCaa =
notImplementedAsync("dns.resolveCaa");
export const resolveCname: typeof dns.resolveCname =
notImplementedAsync("dns.resolveCname");
export const resolveMx: typeof dns.resolveMx =
notImplementedAsync("dns.resolveMx");
export const resolveNaptr: typeof dns.resolveNaptr =
notImplementedAsync("dns.resolveNaptr");
export const resolveNs: typeof dns.resolveNs =
notImplementedAsync("dns.resolveNs");
export const resolvePtr: typeof dns.resolvePtr =
notImplementedAsync("dns.resolvePtr");
export const resolveSoa: typeof dns.resolveSoa =
notImplementedAsync("dns.resolveSoa");
export const resolveSrv: typeof dns.resolveSrv =
notImplementedAsync("dns.resolveSrv");
export const resolveTxt: typeof dns.resolveTxt =
notImplementedAsync("dns.resolveTxt");

export const reverse: typeof dns.reverse = notImplemented("dns.reverse");
export const setDefaultResultOrder: typeof dns.setDefaultResultOrder = noop;
export const setServers: typeof dns.setServers = noop;

export default <typeof dns>{
...constants,
Resolver,
getDefaultResultOrder,
getServers,
lookup,
lookupService,
resolve,
resolve4,
resolve6,
resolveAny,
resolveCaa,
resolveCname,
resolveMx,
resolveNaptr,
resolveNs,
resolvePtr,
resolveSoa,
resolveSrv,
resolveTxt,
reverse,
setDefaultResultOrder,
setServers,
};
15 changes: 1 addition & 14 deletions src/runtime/node/fs/_fs.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
import type fs from "node:fs";
import { notImplemented } from "../../_internal/utils";
import { notImplemented, notImplementedAsync } from "../../_internal/utils";
import * as fsp from "./promises/_promises";

interface Promisifiable {
(): any;
native: Promisifiable;
__promisify__: () => Promise<any>;
}

function notImplementedAsync(name: string): Promisifiable {
const fn = notImplemented(name) as any;
fn.__promisify__ = () => notImplemented(name + ".__promisify__");
fn.native = fn;
return fn;
}

function callbackify(fn: (...args: any[]) => Promise<any>) {
const fnc = function (...args: any[]) {
const cb = args.pop();
Expand Down