node/test/fixtures/snapshot/dns-resolve.js
Joyee Cheung 8821860f11
dns: support dns module in the snapshot
For the initial iteration, only the default resolver can be
serialized/deserialized. If `dns.setServers()` has been
called, we'll preserve the configured DNS servers in the snapshot.
We can consider exposing the serialization method if it becomes
necessary for user-land snapshots.

PR-URL: https://github.com/nodejs/node/pull/44633
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-09-29 01:58:21 +08:00

60 lines
1.2 KiB
JavaScript

'use strict';
const dns = require('dns');
const assert = require('assert');
assert(process.env.NODE_TEST_HOST);
const {
setDeserializeMainFunction,
} = require('v8').startupSnapshot;
function onError(err) {
console.error('error:', err);
}
function onResolve(addresses) {
console.log(`addresses: ${JSON.stringify(addresses)}`);
}
function onReverse(hostnames) {
console.log(`hostnames: ${JSON.stringify(hostnames)}`);
}
function query() {
if (process.env.NODE_TEST_DNS) {
dns.setServers([process.env.NODE_TEST_DNS])
}
const host = process.env.NODE_TEST_HOST;
if (process.env.NODE_TEST_PROMISE === 'true') {
dns.promises.resolve4(host).then(onResolve, onError);
} else {
dns.resolve4(host, (err, addresses) => {
if (err) {
onError(err);
} else {
onResolve(addresses);
}
});
}
const ip = process.env.NODE_TEST_IP;
if (ip) {
if (process.env.NODE_TEST_PROMISE === 'true') {
dns.promises.reverse(ip).then(onReverse, onError);
} else {
dns.reverse(ip, (err, hostnames) => {
if (err) {
onError(err);
} else {
onReverse(hostnames);
}
});
}
}
}
query();
setDeserializeMainFunction(query);