| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f8b2d7a commit 9151439
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,7 @@ const { | |||
| 10 | 10 | RegExpPrototypeExec, | |
| 11 | 11 | RegExpPrototypeSymbolReplace, | |
| 12 | 12 | ObjectCreate, | |
| 13 | + Symbol, | ||
| 13 | 14 | } = primordials; | |
| 14 | 15 | ||
| 15 | 16 | const errors = require('internal/errors'); | |
@@ -35,6 +36,12 @@ const { | |||
| 35 | 36 | ERR_INVALID_IP_ADDRESS, | |
| 36 | 37 | } = errors.codes; | |
| 37 | 38 | ||
| 39 | + const { | ||
| 40 | + addSerializeCallback, | ||
| 41 | + addDeserializeCallback, | ||
| 42 | + isBuildingSnapshot, | ||
| 43 | + } = require('v8').startupSnapshot; | ||
| 44 | + | ||
| 38 | 45 | function validateTimeout(options) { | |
| 39 | 46 | const { timeout = -1 } = { ...options }; | |
| 40 | 47 | validateInt32(timeout, 'options.timeout', -1); | |
@@ -47,12 +54,27 @@ function validateTries(options) { | |||
| 47 | 54 | return tries; | |
| 48 | 55 | } | |
| 49 | 56 | ||
| 57 | + const kSerializeResolver = Symbol('dns:resolver:serialize'); | ||
| 58 | + const kDeserializeResolver = Symbol('dns:resolver:deserialize'); | ||
| 59 | + const kSnapshotStates = Symbol('dns:resolver:config'); | ||
| 60 | + const kInitializeHandle = Symbol('dns:resolver:initializeHandle'); | ||
| 61 | + const kSetServersInteral = Symbol('dns:resolver:setServers'); | ||
| 62 | + | ||
| 50 | 63 | // Resolver instances correspond 1:1 to c-ares channels. | |
| 51 | 64 | ||
| 52 | 65 | class ResolverBase { | |
| 53 | 66 | constructor(options = undefined) { | |
| 54 | 67 | const timeout = validateTimeout(options); | |
| 55 | 68 | const tries = validateTries(options); | |
| 69 | + // If we are building snapshot, save the states of the resolver along | ||
| 70 | + // the way. | ||
| 71 | + if (isBuildingSnapshot()) { | ||
| 72 | + this[kSnapshotStates] = { timeout, tries }; | ||
| 73 | + } | ||
| 74 | + this[kInitializeHandle](timeout, tries); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + [kInitializeHandle](timeout, tries) { | ||
| 56 | 78 | const { ChannelWrap } = lazyBinding(); | |
| 57 | 79 | this._handle = new ChannelWrap(timeout, tries); | |
| 58 | 80 | } | |
@@ -77,9 +99,7 @@ class ResolverBase { | |||
| 77 | 99 | // Cache the original servers because in the event of an error while | |
| 78 | 100 | // setting the servers, c-ares won't have any servers available for | |
| 79 | 101 | // resolution. | |
| 80 | - const orig = this._handle.getServers() || []; | ||
| 81 | 102 | const newSet = []; | |
| 82 | - | ||
| 83 | 103 | ArrayPrototypeForEach(servers, (serv, index) => { | |
| 84 | 104 | validateString(serv, `servers[${index}]`); | |
| 85 | 105 | let ipVersion = isIP(serv); | |
@@ -118,6 +138,11 @@ class ResolverBase { | |||
| 118 | 138 | throw new ERR_INVALID_IP_ADDRESS(serv); | |
| 119 | 139 | }); | |
| 120 | 140 | ||
| 141 | + this[kSetServersInteral](newSet, servers); | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + [kSetServersInteral](newSet, servers) { | ||
| 145 | + const orig = this._handle.getServers() || []; | ||
| 121 | 146 | const errorNumber = this._handle.setServers(newSet); | |
| 122 | 147 | ||
| 123 | 148 | if (errorNumber !== 0) { | |
@@ -127,8 +152,13 @@ class ResolverBase { | |||
| 127 | 152 | const err = strerror(errorNumber); | |
| 128 | 153 | throw new ERR_DNS_SET_SERVERS_FAILED(err, servers); | |
| 129 | 154 | } | |
| 155 | + | ||
| 156 | + if (isBuildingSnapshot()) { | ||
| 157 | + this[kSnapshotStates].servers = newSet; | ||
| 158 | + } | ||
| 130 | 159 | } | |
| 131 | 160 | ||
| 161 | + | ||
| 132 | 162 | setLocalAddress(ipv4, ipv6) { | |
| 133 | 163 | validateString(ipv4, 'ipv4'); | |
| 134 | 164 | ||
@@ -137,6 +167,31 @@ class ResolverBase { | |||
| 137 | 167 | } | |
| 138 | 168 | ||
| 139 | 169 | this._handle.setLocalAddress(ipv4, ipv6); | |
| 170 | + | ||
| 171 | + if (isBuildingSnapshot()) { | ||
| 172 | + this[kSnapshotStates].localAddress = { ipv4, ipv6 }; | ||
| 173 | + } | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + // TODO(joyeecheung): consider exposing this if custom DNS resolvers | ||
| 177 | + // end up being useful for snapshot users. | ||
| 178 | + [kSerializeResolver]() { | ||
| 179 | + this._handle = null; // We'll restore it during deserialization. | ||
| 180 | + addDeserializeCallback(function deserializeResolver(resolver) { | ||
| 181 | + resolver[kDeserializeResolver](); | ||
| 182 | + }, this); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + [kDeserializeResolver]() { | ||
| 186 | + const { timeout, tries, localAddress, servers } = this[kSnapshotStates]; | ||
| 187 | + this[kInitializeHandle](timeout, tries); | ||
| 188 | + if (localAddress) { | ||
| 189 | + const { ipv4, ipv6 } = localAddress; | ||
| 190 | + this._handle.setLocalAddress(ipv4, ipv6); | ||
| 191 | + } | ||
| 192 | + if (servers) { | ||
| 193 | + this[kSetServersInteral](servers, servers); | ||
| 194 | + } | ||
| 140 | 195 | } | |
| 141 | 196 | } | |
| 142 | 197 | ||
@@ -151,6 +206,14 @@ function initializeDns() { | |||
| 151 | 206 | // Allow the deserialized application to override order from CLI. | |
| 152 | 207 | dnsOrder = orderFromCLI; | |
| 153 | 208 | } | |
| 209 | + | ||
| 210 | + if (!isBuildingSnapshot()) { | ||
| 211 | + return; | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + addSerializeCallback(() => { | ||
| 215 | + defaultResolver?.[kSerializeResolver](); | ||
| 216 | + }); | ||
| 154 | 217 | } | |
| 155 | 218 | ||
| 156 | 219 | const resolverKeys = [ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,7 +49,7 @@ const supportedModules = new SafeSet(new SafeArrayIterator([ | |||
| 49 | 49 | 'crypto', | |
| 50 | 50 | // 'dgram', | |
| 51 | 51 | // 'diagnostics_channel', | |
| 52 | - // 'dns', | ||
| 52 | + 'dns', | ||
| 53 | 53 | // 'dns/promises', | |
| 54 | 54 | // 'domain', | |
| 55 | 55 | 'events', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,18 +19,19 @@ | |||
| 19 | 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | 21 | ||
| 22 | + #include "cares_wrap.h" | ||
| 22 | 23 | #include "async_wrap-inl.h" | |
| 23 | - #include "base_object-inl.h" | ||
| 24 | 24 | #include "base64-inl.h" | |
| 25 | - #include "cares_wrap.h" | ||
| 25 | + #include "base_object-inl.h" | ||
| 26 | 26 | #include "env-inl.h" | |
| 27 | 27 | #include "memory_tracker-inl.h" | |
| 28 | 28 | #include "node.h" | |
| 29 | 29 | #include "node_errors.h" | |
| 30 | + #include "node_external_reference.h" | ||
| 30 | 31 | #include "req_wrap-inl.h" | |
| 31 | 32 | #include "util-inl.h" | |
| 32 | - #include "v8.h" | ||
| 33 | 33 | #include "uv.h" | |
| 34 | + #include "v8.h" | ||
| 34 | 35 | ||
| 35 | 36 | #include <cerrno> | |
| 36 | 37 | #include <cstring> | |
@@ -1955,7 +1956,36 @@ void Initialize(Local<Object> target, | |||
| 1955 | 1956 | SetConstructorFunction(context, target, "ChannelWrap", channel_wrap); | |
| 1956 | 1957 | } | |
| 1957 | 1958 | ||
| 1959 | + void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | ||
| 1960 | + registry->Register(GetAddrInfo); | ||
| 1961 | + registry->Register(GetNameInfo); | ||
| 1962 | + registry->Register(CanonicalizeIP); | ||
| 1963 | + registry->Register(StrError); | ||
| 1964 | + registry->Register(ChannelWrap::New); | ||
| 1965 | + | ||
| 1966 | + registry->Register(Query<QueryAnyWrap>); | ||
| 1967 | + registry->Register(Query<QueryAWrap>); | ||
| 1968 | + registry->Register(Query<QueryAaaaWrap>); | ||
| 1969 | + registry->Register(Query<QueryCaaWrap>); | ||
| 1970 | + registry->Register(Query<QueryCnameWrap>); | ||
| 1971 | + registry->Register(Query<QueryMxWrap>); | ||
| 1972 | + registry->Register(Query<QueryNsWrap>); | ||
| 1973 | + registry->Register(Query<QueryTxtWrap>); | ||
| 1974 | + registry->Register(Query<QuerySrvWrap>); | ||
| 1975 | + registry->Register(Query<QueryPtrWrap>); | ||
| 1976 | + registry->Register(Query<QueryNaptrWrap>); | ||
| 1977 | + registry->Register(Query<QuerySoaWrap>); | ||
| 1978 | + registry->Register(Query<GetHostByAddrWrap>); | ||
| 1979 | + | ||
| 1980 | + registry->Register(GetServers); | ||
| 1981 | + registry->Register(SetServers); | ||
| 1982 | + registry->Register(SetLocalAddress); | ||
| 1983 | + registry->Register(Cancel); | ||
| 1984 | + } | ||
| 1985 | + | ||
| 1958 | 1986 | } // namespace cares_wrap | |
| 1959 | 1987 | } // namespace node | |
| 1960 | 1988 | ||
| 1961 | 1989 | NODE_MODULE_CONTEXT_AWARE_INTERNAL(cares_wrap, node::cares_wrap::Initialize) | |
| 1990 | + NODE_MODULE_EXTERNAL_REFERENCE(cares_wrap, | ||
| 1991 | + node::cares_wrap::RegisterExternalReferences) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,8 +9,9 @@ | |||
| 9 | 9 | #include "base_object.h" | |
| 10 | 10 | #include "env.h" | |
| 11 | 11 | #include "memory_tracker.h" | |
| 12 | - #include "util.h" | ||
| 13 | 12 | #include "node.h" | |
| 13 | + #include "node_internals.h" | ||
| 14 | + #include "util.h" | ||
| 14 | 15 | ||
| 15 | 16 | #include "ares.h" | |
| 16 | 17 | #include "v8.h" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,6 +62,7 @@ class ExternalReferenceRegistry { | |||
| 62 | 62 | V(blob) \ | |
| 63 | 63 | V(buffer) \ | |
| 64 | 64 | V(builtins) \ | |
| 65 | + V(cares_wrap) \ | ||
| 65 | 66 | V(contextify) \ | |
| 66 | 67 | V(credentials) \ | |
| 67 | 68 | V(env_var) \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,66 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const tmpdir = require('../common/tmpdir'); | ||
| 4 | + const { spawnSync } = require('child_process'); | ||
| 5 | + const path = require('path'); | ||
| 6 | + const fs = require('fs'); | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + | ||
| 9 | + function buildSnapshot(entry, env) { | ||
| 10 | + const child = spawnSync(process.execPath, [ | ||
| 11 | + '--snapshot-blob', | ||
| 12 | + path.join(tmpdir.path, 'snapshot.blob'), | ||
| 13 | + '--build-snapshot', | ||
| 14 | + entry, | ||
| 15 | + ], { | ||
| 16 | + cwd: tmpdir.path, | ||
| 17 | + env: { | ||
| 18 | + ...process.env, | ||
| 19 | + ...env, | ||
| 20 | + }, | ||
| 21 | + }); | ||
| 22 | + | ||
| 23 | + const stderr = child.stderr.toString(); | ||
| 24 | + const stdout = child.stdout.toString(); | ||
| 25 | + console.log('[stderr]'); | ||
| 26 | + console.log(stderr); | ||
| 27 | + console.log('[stdout]'); | ||
| 28 | + console.log(stdout); | ||
| 29 | + | ||
| 30 | + assert.strictEqual(child.status, 0); | ||
| 31 | + | ||
| 32 | + const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob')); | ||
| 33 | + assert(stats.isFile()); | ||
| 34 | + | ||
| 35 | + return { child, stderr, stdout }; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + function runWithSnapshot(entry, env) { | ||
| 39 | + const args = ['--snapshot-blob', path.join(tmpdir.path, 'snapshot.blob')]; | ||
| 40 | + if (entry !== undefined) { | ||
| 41 | + args.push(entry); | ||
| 42 | + } | ||
| 43 | + const child = spawnSync(process.execPath, args, { | ||
| 44 | + cwd: tmpdir.path, | ||
| 45 | + env: { | ||
| 46 | + ...process.env, | ||
| 47 | + ...env, | ||
| 48 | + } | ||
| 49 | + }); | ||
| 50 | + | ||
| 51 | + const stderr = child.stderr.toString(); | ||
| 52 | + const stdout = child.stdout.toString(); | ||
| 53 | + console.log('[stderr]'); | ||
| 54 | + console.log(stderr); | ||
| 55 | + console.log('[stdout]'); | ||
| 56 | + console.log(stdout); | ||
| 57 | + | ||
| 58 | + assert.strictEqual(child.status, 0); | ||
| 59 | + | ||
| 60 | + return { child, stderr, stdout }; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + module.exports = { | ||
| 64 | + buildSnapshot, | ||
| 65 | + runWithSnapshot, | ||
| 66 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,39 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const dns = require('dns'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + | ||
| 5 | + assert(process.env.NODE_TEST_HOST); | ||
| 6 | + | ||
| 7 | + const { | ||
| 8 | + setDeserializeMainFunction, | ||
| 9 | + } = require('v8').startupSnapshot; | ||
| 10 | + | ||
| 11 | + function onError(err) { | ||
| 12 | + console.error('error:', err); | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + function onLookup(address, family) { | ||
| 16 | + console.log(`address: ${JSON.stringify(address)}`); | ||
| 17 | + console.log(`family: ${JSON.stringify(family)}`); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + function query() { | ||
| 21 | + const host = process.env.NODE_TEST_HOST; | ||
| 22 | + if (process.env.NODE_TEST_PROMISE === 'true') { | ||
| 23 | + dns.promises.lookup(host, { family: 4 }).then( | ||
| 24 | + ({address, family}) => onLookup(address, family), | ||
| 25 | + onError); | ||
| 26 | + } else { | ||
| 27 | + dns.lookup(host, { family: 4 }, (err, address, family) => { | ||
| 28 | + if (err) { | ||
| 29 | + onError(err); | ||
| 30 | + } else { | ||
| 31 | + onLookup(address, family); | ||
| 32 | + } | ||
| 33 | + }); | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + query(); | ||
| 38 | + | ||
| 39 | + setDeserializeMainFunction(query); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,59 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const dns = require('dns'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + | ||
| 5 | + assert(process.env.NODE_TEST_HOST); | ||
| 6 | + | ||
| 7 | + const { | ||
| 8 | + setDeserializeMainFunction, | ||
| 9 | + } = require('v8').startupSnapshot; | ||
| 10 | + | ||
| 11 | + function onError(err) { | ||
| 12 | + console.error('error:', err); | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + function onResolve(addresses) { | ||
| 16 | + console.log(`addresses: ${JSON.stringify(addresses)}`); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + function onReverse(hostnames) { | ||
| 20 | + console.log(`hostnames: ${JSON.stringify(hostnames)}`); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + function query() { | ||
| 24 | + if (process.env.NODE_TEST_DNS) { | ||
| 25 | + dns.setServers([process.env.NODE_TEST_DNS]) | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + const host = process.env.NODE_TEST_HOST; | ||
| 29 | + if (process.env.NODE_TEST_PROMISE === 'true') { | ||
| 30 | + dns.promises.resolve4(host).then(onResolve, onError); | ||
| 31 | + } else { | ||
| 32 | + dns.resolve4(host, (err, addresses) => { | ||
| 33 | + if (err) { | ||
| 34 | + onError(err); | ||
| 35 | + } else { | ||
| 36 | + onResolve(addresses); | ||
| 37 | + } | ||
| 38 | + }); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + const ip = process.env.NODE_TEST_IP; | ||
| 42 | + if (ip) { | ||
| 43 | + if (process.env.NODE_TEST_PROMISE === 'true') { | ||
| 44 | + dns.promises.reverse(ip).then(onReverse, onError); | ||
| 45 | + } else { | ||
| 46 | + dns.reverse(ip, (err, hostnames) => { | ||
| 47 | + if (err) { | ||
| 48 | + onError(err); | ||
| 49 | + } else { | ||
| 50 | + onReverse(hostnames); | ||
| 51 | + } | ||
| 52 | + }); | ||
| 53 | + } | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + query(); | ||
| 58 | + | ||
| 59 | + setDeserializeMainFunction(query); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments