Skip to content

Commit

Permalink
add stub implementation for node:dns (#3169)
Browse files Browse the repository at this point in the history
* add stub implementation for node:dns

* Update src/workerd/api/node/dns.h

Co-authored-by: James M Snell <[email protected]>

* simplify tests

---------

Co-authored-by: James M Snell <[email protected]>
  • Loading branch information
anonrig and jasnell authored Nov 25, 2024
1 parent ae0a8f4 commit 0a67595
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 2 deletions.
47 changes: 47 additions & 0 deletions src/node/dns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { default as dnsUtil } from 'node-internal:dns';

export const getServers = dnsUtil.getServers.bind(dnsUtil);
export const lookup = dnsUtil.lookup.bind(dnsUtil);
export const lookupService = dnsUtil.lookupService.bind(dnsUtil);
export const resolve = dnsUtil.resolve.bind(dnsUtil);
export const resolve4 = dnsUtil.resolve4.bind(dnsUtil);
export const resolve6 = dnsUtil.resolve6.bind(dnsUtil);
export const resolveAny = dnsUtil.resolveAny.bind(dnsUtil);
export const resolveCname = dnsUtil.resolveCname.bind(dnsUtil);
export const resolveCaa = dnsUtil.resolveCaa.bind(dnsUtil);
export const resolveMx = dnsUtil.resolveMx.bind(dnsUtil);
export const resolveNaptr = dnsUtil.resolveNaptr.bind(dnsUtil);
export const resolveNs = dnsUtil.resolveNs.bind(dnsUtil);
export const resolvePtr = dnsUtil.resolvePtr.bind(dnsUtil);
export const resolveSoa = dnsUtil.resolveSoa.bind(dnsUtil);
export const resolveSrv = dnsUtil.resolveSrv.bind(dnsUtil);
export const resolveTxt = dnsUtil.resolveTxt.bind(dnsUtil);
export const reverse = dnsUtil.reverse.bind(dnsUtil);
export const setDefaultResultOrder =
dnsUtil.setDefaultResultOrder.bind(dnsUtil);
export const getDefaultResultOrder =
dnsUtil.getDefaultResultOrder.bind(dnsUtil);
export const setServers = dnsUtil.setServers.bind(dnsUtil);

export default {
getServers,
lookup,
lookupService,
resolve,
resolve4,
resolve6,
resolveAny,
resolveCname,
resolveCaa,
resolveMx,
resolveNaptr,
resolveNs,
resolvePtr,
resolveSoa,
resolveSrv,
resolveTxt,
reverse,
setDefaultResultOrder,
getDefaultResultOrder,
setServers,
};
60 changes: 60 additions & 0 deletions src/node/internal/dns.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export function getServers(): string[];

export type LookupOptions = {
family: number | string;
hints: number;
all: boolean;
order: string;
verbatim: boolean;
};
export function lookup(
hostname: string,
options: LookupOptions,
callback: unknown
): void;

export function lookupService(
address: string,
port: number,
callback: unknown
): void;

export function resolve(
hostname: string,
rrtype: string,
callback: unknown
): void;

export type Resolve4Options = {
ttl: boolean;
};
export function resolve4(
hostname: string,
options: Resolve4Options,
callback: unknown
): void;

export type Resolve6Options = {
ttl: boolean;
};
export function resolve6(
hostname: string,
options: Resolve6Options,
callback: unknown
): void;

export function resolveAny(hostname: string, callback: unknown): void;
export function resolveCname(hostname: string, callback: unknown): void;
export function resolveCaa(hostname: string, callback: unknown): void;
export function resolveMx(hostname: string, callback: unknown): void;
export function resolveNaptr(hostname: string, callback: unknown): void;
export function resolveNs(hostname: string, callback: unknown): void;
export function resolvePtr(hostname: string, callback: unknown): void;
export function resolveSoa(hostname: string, callback: unknown): void;
export function resolveSrv(hostname: string, callback: unknown): void;
export function resolveTxt(hostname: string, callback: unknown): void;
export function reverse(ip: string, callback: unknown): void;

export function setDefaultResultOrder(order: string): void;
export function getDefaultResultOrder(): string;
export function setServers(servers: string[]): string;
8 changes: 8 additions & 0 deletions src/workerd/api/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ wd_cc_library(
"crypto.c++",
"crypto-keys.c++",
"diagnostics-channel.c++",
"dns.c++",
"zlib-util.c++",
],
hdrs = [
"crypto.h",
"diagnostics-channel.h",
"dns.h",
"node.h",
"zlib-util.h",
],
Expand Down Expand Up @@ -243,3 +245,9 @@ wd_test(
"//conditions:default": [],
}),
)

wd_test(
src = "tests/dns-nodejs-test.wd-test",
args = ["--experimental"],
data = ["tests/dns-nodejs-test.js"],
)
94 changes: 94 additions & 0 deletions src/workerd/api/node/dns.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
#include "dns.h"

namespace workerd::api::node {
kj::Array<kj::String> DnsUtil::getServers(jsg::Lock& js) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::lookup(jsg::Lock& js,
kj::String hostname,
jsg::Optional<LookupOptions> options,
LookupCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::lookupService(
jsg::Lock& js, kj::String address, kj::uint port, LookupServiceCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolve(
jsg::Lock& js, kj::String hostname, kj::String rrtype, ResolveCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolve4(
jsg::Lock& js, kj::String hostname, Resolve4Options options, Resolve6Callback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolve6(
jsg::Lock& js, kj::String hostname, Resolve6Options options, Resolve6Callback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolveAny(jsg::Lock& js, kj::String hostname, ResolveAnyCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolveCname(jsg::Lock& js, kj::String hostname, ResolveCnameCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolveCaa(jsg::Lock& js, kj::String hostname, ResolveCaaCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolveMx(jsg::Lock& js, kj::String hostname, ResolveMxCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolveNaptr(jsg::Lock& js, kj::String hostname, ResolveNaptrCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolveNs(jsg::Lock& js, kj::String hostname, ResolveNsCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolvePtr(jsg::Lock& js, kj::String hostname, ResolvePtrCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolveSoa(jsg::Lock& js, kj::String hostname, ResolveSoaCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolveSrv(jsg::Lock& js, kj::String hostname, ResolveSrvCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::resolveTxt(jsg::Lock& js, kj::String hostname, ResolveTxtCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::reverse(jsg::Lock& js, kj::String ip, ReverseCallback callback) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::setDefaultResultOrder(jsg::Lock& js, kj::String order) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

kj::StringPtr DnsUtil::getDefaultResultOrder(jsg::Lock& js) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

void DnsUtil::setServers(kj::Array<kj::String> servers) {
JSG_FAIL_REQUIRE(Error, "Not implemented"_kj);
}

} // namespace workerd::api::node
127 changes: 127 additions & 0 deletions src/workerd/api/node/dns.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
#pragma once

#include <workerd/jsg/jsg.h>

#include <kj/string.h>

namespace workerd::api::node {

class DnsUtil final: public jsg::Object {
public:
DnsUtil() = default;
DnsUtil(jsg::Lock&, const jsg::Url&) {}

kj::Array<kj::String> getServers(jsg::Lock& js);

struct LookupOptions {
kj::OneOf<kj::String, kj::uint> family = static_cast<kj::uint>(0);
kj::uint hints;
bool all = false;
jsg::Optional<kj::String> order;
bool verbatim = true;

JSG_STRUCT(family, hints, all, order, verbatim);
};
using LookupCallback = jsg::Function<void(void)>;
void lookup(jsg::Lock& js,
kj::String hostname,
jsg::Optional<LookupOptions> options,
LookupCallback callback);

using LookupServiceCallback = jsg::Function<void(void)>;
void lookupService(
jsg::Lock& js, kj::String address, kj::uint port, LookupServiceCallback callback);

using ResolveCallback = jsg::Function<void(void)>;
void resolve(jsg::Lock& js, kj::String hostname, kj::String rrtype, ResolveCallback callback);

struct Resolve4Options {
bool ttl;

JSG_STRUCT(ttl);
};
using Resolve4Callback = jsg::Function<void(void)>;
void resolve4(
jsg::Lock& js, kj::String hostname, Resolve4Options options, Resolve4Callback callback);

struct Resolve6Options {
bool ttl;

JSG_STRUCT(ttl);
};
using Resolve6Callback = jsg::Function<void(void)>;
void resolve6(
jsg::Lock& js, kj::String hostname, Resolve6Options options, Resolve6Callback callback);

using ResolveAnyCallback = jsg::Function<void(void)>;
void resolveAny(jsg::Lock& js, kj::String hostname, ResolveAnyCallback callback);

using ResolveCnameCallback = jsg::Function<void(void)>;
void resolveCname(jsg::Lock& js, kj::String hostname, ResolveCnameCallback callback);

using ResolveCaaCallback = jsg::Function<void(void)>;
void resolveCaa(jsg::Lock& js, kj::String hostname, ResolveCaaCallback callback);

using ResolveMxCallback = jsg::Function<void(void)>;
void resolveMx(jsg::Lock& js, kj::String hostname, ResolveMxCallback callback);

using ResolveNaptrCallback = jsg::Function<void(void)>;
void resolveNaptr(jsg::Lock& js, kj::String hostname, ResolveNaptrCallback callback);

using ResolveNsCallback = jsg::Function<void(void)>;
void resolveNs(jsg::Lock& js, kj::String hostname, ResolveNsCallback callback);

using ResolvePtrCallback = jsg::Function<void(void)>;
void resolvePtr(jsg::Lock& js, kj::String hostname, ResolvePtrCallback callback);

using ResolveSoaCallback = jsg::Function<void(void)>;
void resolveSoa(jsg::Lock& js, kj::String hostname, ResolveSoaCallback callback);

using ResolveSrvCallback = jsg::Function<void(void)>;
void resolveSrv(jsg::Lock& js, kj::String hostname, ResolveSrvCallback callback);

using ResolveTxtCallback = jsg::Function<void(void)>;
void resolveTxt(jsg::Lock& js, kj::String hostname, ResolveTxtCallback callback);

using ReverseCallback = jsg::Function<void(void)>;
void reverse(jsg::Lock& js, kj::String ip, ReverseCallback callback);

void setDefaultResultOrder(jsg::Lock& js, kj::String order);
kj::StringPtr getDefaultResultOrder(jsg::Lock& js);
void setServers(kj::Array<kj::String> servers);

JSG_RESOURCE_TYPE(DnsUtil) {
// Callback implementations
JSG_METHOD(getServers);
JSG_METHOD(lookup);
JSG_METHOD(lookupService);
JSG_METHOD(resolve);
JSG_METHOD(resolve4);
JSG_METHOD(resolve6);
JSG_METHOD(resolveAny);
JSG_METHOD(resolveCname);
JSG_METHOD(resolveCaa);
JSG_METHOD(resolveMx);
JSG_METHOD(resolveNaptr);
JSG_METHOD(resolveNs);
JSG_METHOD(resolvePtr);
JSG_METHOD(resolveSoa);
JSG_METHOD(resolveSrv);
JSG_METHOD(resolveTxt);
JSG_METHOD(reverse);

// Getter, setters
JSG_METHOD(setDefaultResultOrder);
JSG_METHOD(getDefaultResultOrder);
JSG_METHOD(setServers);
}
};

#define EW_NODE_DNS_ISOLATE_TYPES \
api::node::DnsUtil, api::node::DnsUtil::LookupOptions, api::node::DnsUtil::Resolve4Options, \
api::node::DnsUtil::Resolve6Options

} // namespace workerd::api::node
6 changes: 4 additions & 2 deletions src/workerd/api/node/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <workerd/api/node/async-hooks.h>
#include <workerd/api/node/buffer.h>
#include <workerd/api/node/dns.h>
#include <workerd/api/node/module.h>
#include <workerd/api/node/url.h>
#include <workerd/api/node/util.h>
Expand Down Expand Up @@ -50,7 +51,8 @@ class CompatibilityFlags: public jsg::Object {
V(UtilModule, "node-internal:util") \
V(DiagnosticsChannelModule, "node-internal:diagnostics_channel") \
V(ZlibUtil, "node-internal:zlib") \
V(UrlUtil, "node-internal:url")
V(UrlUtil, "node-internal:url") \
V(DnsUtil, "node-internal:dns")

// Add to the NODEJS_MODULES_EXPERIMENTAL list any currently in-development
// node.js compat C++ modules that should be guarded by the experimental compat
Expand Down Expand Up @@ -142,4 +144,4 @@ kj::Own<jsg::modules::ModuleBundle> getExternalNodeJsCompatModuleBundle(auto fea
api::node::CompatibilityFlags, EW_NODE_BUFFER_ISOLATE_TYPES, EW_NODE_CRYPTO_ISOLATE_TYPES, \
EW_NODE_DIAGNOSTICCHANNEL_ISOLATE_TYPES, EW_NODE_ASYNCHOOKS_ISOLATE_TYPES, \
EW_NODE_UTIL_ISOLATE_TYPES, EW_NODE_ZLIB_ISOLATE_TYPES, EW_NODE_URL_ISOLATE_TYPES, \
EW_NODE_MODULE_ISOLATE_TYPES\
EW_NODE_MODULE_ISOLATE_TYPES, EW_NODE_DNS_ISOLATE_TYPES\
Loading

0 comments on commit 0a67595

Please sign in to comment.