node/test/fixtures/snapshot/dns-lookup.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

39 lines
860 B
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 onLookup(address, family) {
console.log(`address: ${JSON.stringify(address)}`);
console.log(`family: ${JSON.stringify(family)}`);
}
function query() {
const host = process.env.NODE_TEST_HOST;
if (process.env.NODE_TEST_PROMISE === 'true') {
dns.promises.lookup(host, { family: 4 }).then(
({address, family}) => onLookup(address, family),
onError);
} else {
dns.lookup(host, { family: 4 }, (err, address, family) => {
if (err) {
onError(err);
} else {
onLookup(address, family);
}
});
}
}
query();
setDeserializeMainFunction(query);