-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: #44633 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
- Loading branch information
1 parent
f8b2d7a
commit 9151439
Showing
14 changed files
with
474 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
const { spawnSync } = require('child_process'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
|
||
function buildSnapshot(entry, env) { | ||
const child = spawnSync(process.execPath, [ | ||
'--snapshot-blob', | ||
path.join(tmpdir.path, 'snapshot.blob'), | ||
'--build-snapshot', | ||
entry, | ||
], { | ||
cwd: tmpdir.path, | ||
env: { | ||
...process.env, | ||
...env, | ||
}, | ||
}); | ||
|
||
const stderr = child.stderr.toString(); | ||
const stdout = child.stdout.toString(); | ||
console.log('[stderr]'); | ||
console.log(stderr); | ||
console.log('[stdout]'); | ||
console.log(stdout); | ||
|
||
assert.strictEqual(child.status, 0); | ||
|
||
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob')); | ||
assert(stats.isFile()); | ||
|
||
return { child, stderr, stdout }; | ||
} | ||
|
||
function runWithSnapshot(entry, env) { | ||
const args = ['--snapshot-blob', path.join(tmpdir.path, 'snapshot.blob')]; | ||
if (entry !== undefined) { | ||
args.push(entry); | ||
} | ||
const child = spawnSync(process.execPath, args, { | ||
cwd: tmpdir.path, | ||
env: { | ||
...process.env, | ||
...env, | ||
} | ||
}); | ||
|
||
const stderr = child.stderr.toString(); | ||
const stdout = child.stdout.toString(); | ||
console.log('[stderr]'); | ||
console.log(stderr); | ||
console.log('[stdout]'); | ||
console.log(stdout); | ||
|
||
assert.strictEqual(child.status, 0); | ||
|
||
return { child, stderr, stdout }; | ||
} | ||
|
||
module.exports = { | ||
buildSnapshot, | ||
runWithSnapshot, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'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); |
Oops, something went wrong.