-
Notifications
You must be signed in to change notification settings - Fork 386
[PHP] gethostbyname – native DNS resolution in Node.js builds #2988
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
Conversation
| */ | ||
| async gethostbyname(hostname: string): Promise<string> { | ||
| const { address } = await lookup(hostname, { | ||
| family: 4, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps we could support ipv6 while we're at it. Or merge this as is and add v6 support in a follow-up pr
Resolved conflicts by prioritizing trunk's wasm files and removing syscalls-related code that was removed in trunk.
7fc9ada to
4332dd2
Compare
Removed all references to SyscallsForNode that were deleted in trunk during the merge. This includes: - Removed SyscallsForNode type from load-runtime.ts - Deleted syscalls-test-worker.ts test file - Removed syscalls field and useSyscalls method from worker classes - Removed syscallsPort parameters from handler functions
|
@brandonpayton this may impact your work on the filesystem as it also introduces a central syscalls handler like yours. Let's merge the two. |
@adamziel sounds good! I plan to account for that in the big rebase I'm doing today. |
Motivation
Before this PR, the
gethostbyname()PHP function always returns either an internal IP address on success, or an unchanged hostname on failure. That's because Emscripten's default DNS implementation doesn't perform real lookups.This PR enables actual DNS resolution for PHP-WASM running in Node.js via Node's
dnsmodule. When PHP code calls
gethostbyname('wordpress.org'), it now resolves and returnsthe real IP address.
Implementation details
The implementation works similarly to
FileLockManager:phpwasm-emscripten-library-dns-lookup-for-node.jsdefines a new build-time JavaScript library linked with PHP.wasm. It overrides the__emscripten_lookup_nameshipped by Emscripten with a custom callback that callsPHPLoader.syscalls.gethostbyname()to resolve the IP.PHPLoader.syscallsis an object passed toloadNodeRuntime()viaoptions.emscriptenOptions. It's an object that, for now, defines a single method:gethostbyname().Even though you may pass an actual object instance, we create the instance in the parent worker and call
gethostbyname()using a Comlink proxy. This is becauserequire('dns').lookup()is asynchronous in Node. JSPI builds can easilyawaitthe returned value, but Asyncify builds can't. Not without spending a dozen hours tweaking theASYNCIFY_ONLYlist in the PHP compile Dockerfile. Therefore, we use a synchronous Comlink proxy to turn an asynchronousgethostbyname()call into a synchronous one in Asyncify builds.Test plan
CI. See the tests shipped with this PR, confirm they pass and make sense.
cc @bcotrim @fredrikekelund