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

implement node:dns module for nodejs_compat #3183

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

anonrig
Copy link
Member

@anonrig anonrig commented Nov 27, 2024

Work in progress

Implements all functions in node:dns using Cloudflare DNS (1.1.1.1)

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few inline comments

throw new DnsError(name, errorCodes.BADRESP, syscall);
}

if (json.Answer?.at(0).name === '') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (json.Answer?.at(0).name === '') {
if (json.Answer?.[0]?.name === '') {

at -> [] is more concise
?.name not to throw if the array is empty

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On typescript [0] doesn't return undefined | ValueType but .at() does. I think we should encourage it more. And we should also enable noUncheckedIndexedAccess on tsconfig as well (ref: https://www.typescriptlang.org/tsconfig/#noUncheckedIndexedAccess)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On typescript [0] doesn't return undefined | ValueType but .at() does.

Did you mean to say "does not"?

If that's the case TS is lying to you:

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS doesn't lie on my machine

image

// hostname: 'google.com2'
// }
export class DnsError extends NodeError {
errno: undefined = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the following work?

Suggested change
errno: undefined = undefined;
errno = undefined;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes


constructor(hostname: string, code: string, syscall: string) {
super(code, `${syscall} ${code} ${hostname}`);
this.errno = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can drop this line

'One of the entries should have spf1'
);
ok(
results.at(0).at(0)[0] !== '"',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
results.at(0).at(0)[0] !== '"',
results[0][0][0] !== '"',

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess it should be results[0]?.[0]?.[0]

Comment on lines +63 to +73
// Callback API
const { promise, resolve, reject } = Promise.withResolvers();
dns.resolveTxt('cloudflare.com', (error, results) => {
if (error) {
reject(error);
return;
}
testResults(results);
resolve();
});
await promise;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Callback API
const { promise, resolve, reject } = Promise.withResolvers();
dns.resolveTxt('cloudflare.com', (error, results) => {
if (error) {
reject(error);
return;
}
testResults(results);
resolve();
});
await promise;
// Callback API
dns.resolveTxt('cloudflare.com', (error, results) => {
if (error) {
return fail(error);
}
testResults(results);
});

setServers,
} from 'node-internal:internal_dns';

function toCallback<A = unknown, ReturnValue = unknown>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know we implement callbackify? You can move its implementation into node/internal and reuse that here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL. Thanks

dnsUtil.getDefaultResultOrder.bind(dnsUtil);
export const setServers = dnsUtil.setServers.bind(dnsUtil);
export const reverse = toCallback(dns.reverse.bind(dns));
export const resolveTxt = toCallback(dns.resolveTxt.bind(dns));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're going from promises to callbacks here, how are you handling synchronously throwing on argument validation? By default the JSG wrapper should cause synchronously thrown errors at the c++ level to be returned into promise rejections. Are you certain this will do the right thing for the errors the Node.js API expects to be thrown synchronously?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove async from functions and just return a promise from the function, which would throw validation errors synchronously but the function will return Promise

) => void {
return (
input: A,
callback: (error: Error | null, value?: ReturnValue) => void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node.js will throw an error on these if callback is not a function, correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, definitely there is a validateFunction missing from here.


let json: SuccessResponse | FailedResponse;
try {
const response = await fetch(server, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting... it will be important to document to folks that DNS queries will count towards the subrequest limits and concurrent subrequest limits will apply.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an internal API to remove this sub request limit from this call?

): Promise<SuccessResponse> {
// We are using cloudflare-dns.com and not 1.1.1.1 because of certificate issues.
// TODO(soon): Replace this when KJ certificate issues are resolved.
const server = new URL('https://cloudflare-dns.com/dns-query');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this will route these requests as public internet requests I guess we should decide if using global fetch is really what we want vs. using a private fetch via a binding that would for sure route the request through internal infrastructure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for sure, we should find a solution to this before landing this pull-request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants