feat(node): node:net isIP checks and node:dns lookup()
Fedify checks a URL's host with both before fetching it.
Jason Schwarzenberger authored 1 parent 8702f6dcommit c23c5160f4b1b8b6d3174da4f9f4041b9f46d770Browse files
10 files changed+612−0
Files (10)
- HOMEFLARE.md+47−0
- crates/celld/js.rs+2−0
- crates/celld/js/dns.rs+78−0
- crates/celld/js/modules.rs+21−0
- crates/celld/js/node_dns.js+255−0
- crates/celld/js/node_net.js+64−0
- docs/cloudflare-compat.md+4−0
- homeflare/smoke/node-net-dns/index.js+81−0
- homeflare/smoke/node-net-dns/wrangler.jsonc+6−0
- homeflare/smoke/run.sh+54−0
addedHOMEFLARE.md+47−0View file
| @@ -1,0 +1,47 @@ | ||
| 1 | +# celld for Homeflare | |
| 2 | + | |
| 3 | +A fork of [denoland/celld](https://github.com/denoland/celld) with the | |
| 4 | +patches Homeflare's runtimes need. Upstream stopped at v0.6.3 when celld began | |
| 5 | +merging into workerd, so fixes for gaps Homeflare apps hit land here. | |
| 6 | + | |
| 7 | +## Branches | |
| 8 | + | |
| 9 | +`main` is upstream's v0.6.3 plus the patches below, one commit each, so a patch | |
| 10 | +can be dropped once the merged workerd covers it. Rebase onto upstream when it | |
| 11 | +moves: | |
| 12 | + | |
| 13 | +```sh | |
| 14 | +git remote add upstream https://github.com/denoland/celld.git | |
| 15 | +git fetch upstream && git rebase upstream/main | |
| 16 | +``` | |
| 17 | + | |
| 18 | +## Patches | |
| 19 | + | |
| 20 | +| Patch | Why | | |
| 21 | +|---|---| | |
| 22 | +| `node:net` `isIP` / `isIPv4` / `isIPv6`; `node:dns` and `node:dns/promises` `lookup()` | Fedify checks a URL's host with both before fetching it, so palm's remote lookups failed with `node:net.isIP is not implemented in celld`. `lookup()` uses the node's resolver through a new `__dns_lookup` op, with `connect()`'s egress rules. | | |
| 23 | + | |
| 24 | +## Building | |
| 25 | + | |
| 26 | +Homeflare builds this repo into its runtime image: set | |
| 27 | +`HOMEFLARE_CELLD_SOURCE=https://github.com/jason-s13r/celld.git` in | |
| 28 | +`homeflare.env` (see Homeflare's README). By hand, with Podman: | |
| 29 | + | |
| 30 | +```sh | |
| 31 | +podman build -t localhost/homeflare-celld:dev \ | |
| 32 | + --build-arg CELLD_COMMIT="$(git rev-parse HEAD)" . | |
| 33 | +``` | |
| 34 | + | |
| 35 | +The Dockerfile is upstream's: the image builds only if `cargo test` and | |
| 36 | +`cargo clippy -D warnings` pass. `--build-arg CELLD_PROFILE=lab` swaps fat LTO | |
| 37 | +for thin, which builds much faster. | |
| 38 | + | |
| 39 | +## Smoke tests | |
| 40 | + | |
| 41 | +Each folder in `homeflare/smoke/` is a Worker whose `GET /` reports its checks | |
| 42 | +as JSON; a `paths.txt` beside it adds requests that must answer 200 with the given text. With a built `celld` and `esbuild` on `PATH`: | |
| 43 | + | |
| 44 | +```sh | |
| 45 | +cargo build -p celld | |
| 46 | +homeflare/smoke/run.sh # target/debug/celld | |
| 47 | +``` |
modifiedcrates/celld/js.rs+2−0View file
| @@ -6742,6 +6742,7 @@ | ||
| 6742 | 6742 | "__tcp_shutdown" => tcp::op_tcp_shutdown, |
| 6743 | 6743 | "__tcp_close" => tcp::op_tcp_close, |
| 6744 | 6744 | "__tcp_starttls" => tcp::op_tcp_starttls, |
| 6745 | + "__dns_lookup" => dns::op_dns_lookup, | |
| 6745 | 6746 | "__container_running" => container::op_container_running, |
| 6746 | 6747 | "__container_address" => container::op_container_address, |
| 6747 | 6748 | "__container_start" => container::op_container_start, |
| @@ -9955,6 +9956,7 @@ | ||
| 9955 | 9956 | |
| 9956 | 9957 | mod container; |
| 9957 | 9958 | mod crypto; |
| 9959 | +mod dns; | |
| 9958 | 9960 | mod html_rewriter; |
| 9959 | 9961 | mod tcp; |
| 9960 | 9962 | #[cfg(celld_internal_tests)] |
addedcrates/celld/js/dns.rs+78−0View file
| @@ -1,0 +1,78 @@ | ||
| 1 | +//! Host op for `node:dns` `lookup()` (Homeflare patch). | |
| 2 | +//! | |
| 3 | +//! `lookup()` is getaddrinfo, so it resolves through the node's own resolver: | |
| 4 | +//! the container's `/etc/resolv.conf` and hosts file. On a homelab that's the | |
| 5 | +//! LAN or Podman network DNS, which is what the node's other outbound | |
| 6 | +//! connections (`fetch`, `connect()`) already use, so a Worker sees the same | |
| 7 | +//! answers it will connect to. | |
| 8 | +//! | |
| 9 | +//! Egress rules match `connect()`: a Worker denied the internet can't look | |
| 10 | +//! names up either, and a `globalOutbound` Fetcher has no way to broker a | |
| 11 | +//! lookup, so one is refused rather than leaking the hostname around it. | |
| 12 | + | |
| 13 | +use super::*; | |
| 14 | + | |
| 15 | +pub(super) fn op_dns_lookup( | |
| 16 | + scope: &mut v8::PinScope, | |
| 17 | + args: v8::FunctionCallbackArguments, | |
| 18 | + mut rv: v8::ReturnValue<v8::Value>, | |
| 19 | +) { | |
| 20 | + match &actor_runtime_state(scope).egress { | |
| 21 | + EgressPolicy::Allow => {} | |
| 22 | + EgressPolicy::Deny => { | |
| 23 | + return loader_throw( | |
| 24 | + scope, | |
| 25 | + "This worker is not permitted to access the internet via global functions.", | |
| 26 | + ); | |
| 27 | + } | |
| 28 | + EgressPolicy::Broker(_) => { | |
| 29 | + return loader_throw( | |
| 30 | + scope, | |
| 31 | + "A globalOutbound Fetcher cannot broker dns.lookup() in celld.", | |
| 32 | + ); | |
| 33 | + } | |
| 34 | + } | |
| 35 | + let hostname = args.get(0).to_rust_string_lossy(scope); | |
| 36 | + let gate = egress_gate_request(&event_context(scope), celld_logic::Channel::Tcp); | |
| 37 | + let async_id = asyncrt::enqueue(async move { | |
| 38 | + await_egress_gate(gate).await?; | |
| 39 | + let found = tokio::net::lookup_host((hostname.as_str(), 0)) | |
| 40 | + .await | |
| 41 | + .map_err(|error| lookup_error_code(&error).to_string())?; | |
| 42 | + // getaddrinfo can list an address once per socket type; keep the | |
| 43 | + // resolver's order and drop the repeats. | |
| 44 | + let mut addresses: Vec<std::net::IpAddr> = Vec::new(); | |
| 45 | + for address in found { | |
| 46 | + let ip = address.ip(); | |
| 47 | + if !addresses.contains(&ip) { | |
| 48 | + addresses.push(ip); | |
| 49 | + } | |
| 50 | + } | |
| 51 | + if addresses.is_empty() { | |
| 52 | + return Err("ENOTFOUND".to_string()); | |
| 53 | + } | |
| 54 | + let records: Vec<serde_json::Value> = addresses | |
| 55 | + .into_iter() | |
| 56 | + .map(|ip| { | |
| 57 | + serde_json::json!({ | |
| 58 | + "address": ip.to_string(), | |
| 59 | + "family": if ip.is_ipv4() { 4 } else { 6 }, | |
| 60 | + }) | |
| 61 | + }) | |
| 62 | + .collect(); | |
| 63 | + Ok::<String, String>(serde_json::Value::Array(records).to_string()) | |
| 64 | + }); | |
| 65 | + rv.set(promise_for(scope, async_id)); | |
| 66 | +} | |
| 67 | + | |
| 68 | +/// Node's error code for a failed getaddrinfo. Rust keeps only the message | |
| 69 | +/// (`gai_strerror`), so the temporary failure is told apart by its text; | |
| 70 | +/// anything else is a name that doesn't resolve. | |
| 71 | +fn lookup_error_code(error: &std::io::Error) -> &'static str { | |
| 72 | + let message = error.to_string(); | |
| 73 | + if message.contains("Temporary failure") || message.contains("try again") { | |
| 74 | + "EAI_AGAIN" | |
| 75 | + } else { | |
| 76 | + "ENOTFOUND" | |
| 77 | + } | |
| 78 | +} |
modifiedcrates/celld/js/modules.rs+21−0View file
| @@ -21,6 +21,8 @@ | ||
| 21 | 21 | static_source!(NODE_UTIL_SOURCE, "node_util.js"); |
| 22 | 22 | static_source!(NODE_EVENTS_SOURCE, "node_events.js"); |
| 23 | 23 | static_source!(NODE_OS_SOURCE, "node_os.js"); |
| 24 | +static_source!(NODE_NET_SOURCE, "node_net.js"); | |
| 25 | +static_source!(NODE_DNS_SOURCE, "node_dns.js"); | |
| 24 | 26 | static_source!(NODE_PATH_SOURCE, "node_path.js"); |
| 25 | 27 | static_source!(NODE_BUFFER_SOURCE, "node_buffer.js"); |
| 26 | 28 | static_source!(NODE_CRYPTO_SOURCE, "node_crypto.js"); |
| @@ -47,6 +49,8 @@ | ||
| 47 | 49 | ("node_util.js", &NODE_UTIL_SOURCE), |
| 48 | 50 | ("node_events.js", &NODE_EVENTS_SOURCE), |
| 49 | 51 | ("node_os.js", &NODE_OS_SOURCE), |
| 52 | + ("node_net.js", &NODE_NET_SOURCE), | |
| 53 | + ("node_dns.js", &NODE_DNS_SOURCE), | |
| 50 | 54 | ("node_path.js", &NODE_PATH_SOURCE), |
| 51 | 55 | ("node_buffer.js", &NODE_BUFFER_SOURCE), |
| 52 | 56 | ("node_crypto.js", &NODE_CRYPTO_SOURCE), |
| @@ -387,7 +391,24 @@ | ||
| 387 | 391 | global: "__osModule", |
| 388 | 392 | source: &NODE_OS_SOURCE, |
| 389 | 393 | }, |
| 394 | + // Homeflare patch: node:net's address helpers and node:dns lookup(). | |
| 390 | 395 | LazyModule { |
| 396 | + specs: &["net", "node:net"], | |
| 397 | + global: "__netModule", | |
| 398 | + source: &NODE_NET_SOURCE, | |
| 399 | + }, | |
| 400 | + // One source sets both globals; either entry's guard covers the other. | |
| 401 | + LazyModule { | |
| 402 | + specs: &["dns", "node:dns"], | |
| 403 | + global: "__dnsModule", | |
| 404 | + source: &NODE_DNS_SOURCE, | |
| 405 | + }, | |
| 406 | + LazyModule { | |
| 407 | + specs: &["dns/promises", "node:dns/promises"], | |
| 408 | + global: "__dnsPromisesModule", | |
| 409 | + source: &NODE_DNS_SOURCE, | |
| 410 | + }, | |
| 411 | + LazyModule { | |
| 391 | 412 | specs: &["path", "node:path", "path/posix", "node:path/posix"], |
| 392 | 413 | global: "__pathModule", |
| 393 | 414 | source: &NODE_PATH_SOURCE, |
addedcrates/celld/js/node_dns.js+255−0View file
| @@ -1,0 +1,255 @@ | ||
| 1 | +// node:dns and node:dns/promises for celld (Homeflare patch). | |
| 2 | +// | |
| 3 | +// lookup() only, in both styles. It's what libraries call before fetching a | |
| 4 | +// URL they've been handed, to check where the name points (Fedify's | |
| 5 | +// public-URL check, for one). The answer comes from the node's resolver | |
| 6 | +// through the __dns_lookup host op, with Node's options and result shapes: | |
| 7 | +// family (0, 4, 6, "IPv4", "IPv6"), all, order and the older verbatim flag. | |
| 8 | +// | |
| 9 | +// The resolve*() family queries DNS servers directly in Node; those throw | |
| 10 | +// with the member's name, as the generic stub did. | |
| 11 | +// | |
| 12 | +// Injected lazily, so an isolate that never imports node:dns pays nothing. | |
| 13 | +// One source backs both specifiers: whichever runs first sets both globals. | |
| 14 | +(() => { | |
| 15 | + "use strict"; | |
| 16 | + | |
| 17 | + const nodeError = (Type, code, message) => { | |
| 18 | + const error = new Type(message); | |
| 19 | + error.code = code; | |
| 20 | + return error; | |
| 21 | + }; | |
| 22 | + | |
| 23 | + // The same patterns as node:net, which may not be loaded. | |
| 24 | + const v4Seg = "(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])"; | |
| 25 | + const v4Str = `(?:${v4Seg}\\.){3}${v4Seg}`; | |
| 26 | + const IPv4Reg = new RegExp(`^${v4Str}$`); | |
| 27 | + const v6Seg = "(?:[0-9a-fA-F]{1,4})"; | |
| 28 | + const IPv6Reg = new RegExp( | |
| 29 | + "^(?:" + | |
| 30 | + `(?:${v6Seg}:){7}(?:${v6Seg}|:)|` + | |
| 31 | + `(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` + | |
| 32 | + `(?:${v6Seg}:){5}(?::${v4Str}|(?::${v6Seg}){1,2}|:)|` + | |
| 33 | + `(?:${v6Seg}:){4}(?:(?::${v6Seg}){0,1}:${v4Str}|(?::${v6Seg}){1,3}|:)|` + | |
| 34 | + `(?:${v6Seg}:){3}(?:(?::${v6Seg}){0,2}:${v4Str}|(?::${v6Seg}){1,4}|:)|` + | |
| 35 | + `(?:${v6Seg}:){2}(?:(?::${v6Seg}){0,3}:${v4Str}|(?::${v6Seg}){1,5}|:)|` + | |
| 36 | + `(?:${v6Seg}:){1}(?:(?::${v6Seg}){0,4}:${v4Str}|(?::${v6Seg}){1,6}|:)|` + | |
| 37 | + `(?::(?:(?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` + | |
| 38 | + ")(?:%[0-9a-zA-Z-.:]{1,})?$", | |
| 39 | + ); | |
| 40 | + const ipFamily = (input) => | |
| 41 | + IPv4Reg.test(input) ? 4 : IPv6Reg.test(input) ? 6 : 0; | |
| 42 | + | |
| 43 | + const parseFamily = (family) => { | |
| 44 | + if (family === undefined || family === null || family === 0) return 0; | |
| 45 | + if (family === 4 || family === "IPv4") return 4; | |
| 46 | + if (family === 6 || family === "IPv6") return 6; | |
| 47 | + throw nodeError( | |
| 48 | + TypeError, | |
| 49 | + "ERR_INVALID_ARG_VALUE", | |
| 50 | + `The argument 'family' must be one of: 0, 4, 6. Received ${String(family)}`, | |
| 51 | + ); | |
| 52 | + }; | |
| 53 | + | |
| 54 | + const parseOptions = (options) => { | |
| 55 | + if (options === undefined || options === null) { | |
| 56 | + return { family: 0, all: false, order: "verbatim" }; | |
| 57 | + } | |
| 58 | + if (typeof options === "number") { | |
| 59 | + return { family: parseFamily(options), all: false, order: "verbatim" }; | |
| 60 | + } | |
| 61 | + if (typeof options !== "object") { | |
| 62 | + throw nodeError( | |
| 63 | + TypeError, | |
| 64 | + "ERR_INVALID_ARG_TYPE", | |
| 65 | + 'The "options" argument must be of type object or number', | |
| 66 | + ); | |
| 67 | + } | |
| 68 | + let order = "verbatim"; | |
| 69 | + if (options.order !== undefined) { | |
| 70 | + if (!["verbatim", "ipv4first", "ipv6first"].includes(options.order)) { | |
| 71 | + throw nodeError( | |
| 72 | + TypeError, | |
| 73 | + "ERR_INVALID_ARG_VALUE", | |
| 74 | + `The property 'options.order' must be one of: 'verbatim', 'ipv4first', 'ipv6first'. Received ${String(options.order)}`, | |
| 75 | + ); | |
| 76 | + } | |
| 77 | + order = options.order; | |
| 78 | + } else if (options.verbatim === false) { | |
| 79 | + order = "ipv4first"; | |
| 80 | + } | |
| 81 | + return { family: parseFamily(options.family), all: options.all === true, order }; | |
| 82 | + }; | |
| 83 | + | |
| 84 | + const lookupError = (code, hostname) => { | |
| 85 | + const error = nodeError(Error, code, `getaddrinfo ${code} ${hostname}`); | |
| 86 | + error.errno = code === "EAI_AGAIN" ? -3001 : -3008; | |
| 87 | + error.syscall = "getaddrinfo"; | |
| 88 | + error.hostname = hostname; | |
| 89 | + return error; | |
| 90 | + }; | |
| 91 | + | |
| 92 | + // Every address the resolver gave, filtered and ordered as asked. | |
| 93 | + const resolveAll = async (hostname, { family, order }) => { | |
| 94 | + const literal = ipFamily(hostname); | |
| 95 | + let records; | |
| 96 | + if (literal !== 0) { | |
| 97 | + records = [{ address: hostname, family: literal }]; | |
| 98 | + } else { | |
| 99 | + try { | |
| 100 | + records = JSON.parse(await __dns_lookup(hostname)); | |
| 101 | + } catch (error) { | |
| 102 | + const message = String(error?.message ?? error); | |
| 103 | + if (message === "ENOTFOUND" || message === "EAI_AGAIN") { | |
| 104 | + throw lookupError(message, hostname); | |
| 105 | + } | |
| 106 | + throw error; | |
| 107 | + } | |
| 108 | + } | |
| 109 | + if (family !== 0) records = records.filter((r) => r.family === family); | |
| 110 | + if (records.length === 0) throw lookupError("ENOTFOUND", hostname); | |
| 111 | + if (order !== "verbatim") { | |
| 112 | + const first = order === "ipv4first" ? 4 : 6; | |
| 113 | + records = [ | |
| 114 | + ...records.filter((r) => r.family === first), | |
| 115 | + ...records.filter((r) => r.family !== first), | |
| 116 | + ]; | |
| 117 | + } | |
| 118 | + return records; | |
| 119 | + }; | |
| 120 | + | |
| 121 | + const lookupPromise = async (hostname, options) => { | |
| 122 | + if (typeof hostname !== "string") { | |
| 123 | + throw nodeError( | |
| 124 | + TypeError, | |
| 125 | + "ERR_INVALID_ARG_TYPE", | |
| 126 | + 'The "hostname" argument must be of type string', | |
| 127 | + ); | |
| 128 | + } | |
| 129 | + const parsed = parseOptions(options); | |
| 130 | + // Node answers an empty hostname without a lookup. | |
| 131 | + if (hostname === "") { | |
| 132 | + return parsed.all | |
| 133 | + ? [] | |
| 134 | + : { address: null, family: parsed.family === 6 ? 6 : 4 }; | |
| 135 | + } | |
| 136 | + const records = await resolveAll(hostname, parsed); | |
| 137 | + return parsed.all ? records : records[0]; | |
| 138 | + }; | |
| 139 | + | |
| 140 | + const lookup = (hostname, options, callback) => { | |
| 141 | + if (typeof options === "function") { | |
| 142 | + callback = options; | |
| 143 | + options = undefined; | |
| 144 | + } | |
| 145 | + if (typeof callback !== "function") { | |
| 146 | + throw nodeError( | |
| 147 | + TypeError, | |
| 148 | + "ERR_INVALID_ARG_TYPE", | |
| 149 | + 'The "callback" argument must be of type function', | |
| 150 | + ); | |
| 151 | + } | |
| 152 | + const all = options !== null && typeof options === "object" && options.all === true; | |
| 153 | + lookupPromise(hostname, options).then( | |
| 154 | + (result) => { | |
| 155 | + if (all) callback(null, result); | |
| 156 | + else callback(null, result.address, result.family); | |
| 157 | + }, | |
| 158 | + (error) => callback(error), | |
| 159 | + ); | |
| 160 | + return {}; | |
| 161 | + }; | |
| 162 | + | |
| 163 | + const notImplemented = (name) => { | |
| 164 | + const fn = function () { | |
| 165 | + throw new Error(`node:dns.${name} is not implemented in celld`); | |
| 166 | + }; | |
| 167 | + Object.defineProperty(fn, "name", { value: name }); | |
| 168 | + return fn; | |
| 169 | + }; | |
| 170 | + const unsupported = [ | |
| 171 | + "lookupService", | |
| 172 | + "resolve", | |
| 173 | + "resolve4", | |
| 174 | + "resolve6", | |
| 175 | + "resolveAny", | |
| 176 | + "resolveCaa", | |
| 177 | + "resolveCname", | |
| 178 | + "resolveMx", | |
| 179 | + "resolveNaptr", | |
| 180 | + "resolveNs", | |
| 181 | + "resolvePtr", | |
| 182 | + "resolveSoa", | |
| 183 | + "resolveSrv", | |
| 184 | + "resolveTxt", | |
| 185 | + "reverse", | |
| 186 | + "Resolver", | |
| 187 | + ]; | |
| 188 | + | |
| 189 | + const codes = { | |
| 190 | + NODATA: "ENODATA", | |
| 191 | + FORMERR: "EFORMERR", | |
| 192 | + SERVFAIL: "ESERVFAIL", | |
| 193 | + NOTFOUND: "ENOTFOUND", | |
| 194 | + NOTIMP: "ENOTIMP", | |
| 195 | + REFUSED: "EREFUSED", | |
| 196 | + BADQUERY: "EBADQUERY", | |
| 197 | + BADNAME: "EBADNAME", | |
| 198 | + BADFAMILY: "EBADFAMILY", | |
| 199 | + BADRESP: "EBADRESP", | |
| 200 | + CONNREFUSED: "ECONNREFUSED", | |
| 201 | + TIMEOUT: "ETIMEOUT", | |
| 202 | + EOF: "EOF", | |
| 203 | + FILE: "EFILE", | |
| 204 | + NOMEM: "ENOMEM", | |
| 205 | + DESTRUCTION: "EDESTRUCTION", | |
| 206 | + BADSTR: "EBADSTR", | |
| 207 | + BADFLAGS: "EBADFLAGS", | |
| 208 | + NONAME: "ENONAME", | |
| 209 | + BADHINTS: "EBADHINTS", | |
| 210 | + NOTINITIALIZED: "ENOTINITIALIZED", | |
| 211 | + LOADIPHLPAPI: "ELOADIPHLPAPI", | |
| 212 | + ADDRGETNETWORKPARAMS: "EADDRGETNETWORKPARAMS", | |
| 213 | + CANCELLED: "ECANCELLED", | |
| 214 | + }; | |
| 215 | + | |
| 216 | + let defaultOrder = "verbatim"; | |
| 217 | + const setDefaultResultOrder = (order) => { | |
| 218 | + parseOptions({ order }); | |
| 219 | + defaultOrder = order; | |
| 220 | + }; | |
| 221 | + const getDefaultResultOrder = () => defaultOrder; | |
| 222 | + const withDefaultOrder = (options) => { | |
| 223 | + if (defaultOrder === "verbatim") return options; | |
| 224 | + if (options === undefined || options === null) return { order: defaultOrder }; | |
| 225 | + if (typeof options === "number") return { family: options, order: defaultOrder }; | |
| 226 | + if (typeof options === "object" && options.order === undefined && options.verbatim === undefined) { | |
| 227 | + return { ...options, order: defaultOrder }; | |
| 228 | + } | |
| 229 | + return options; | |
| 230 | + }; | |
| 231 | + | |
| 232 | + const promises = { | |
| 233 | + ...codes, | |
| 234 | + lookup: (hostname, options) => lookupPromise(hostname, withDefaultOrder(options)), | |
| 235 | + setDefaultResultOrder, | |
| 236 | + getDefaultResultOrder, | |
| 237 | + }; | |
| 238 | + const dns = { | |
| 239 | + ...codes, | |
| 240 | + lookup: (hostname, options, callback) => { | |
| 241 | + if (typeof options === "function") return lookup(hostname, withDefaultOrder(undefined), options); | |
| 242 | + return lookup(hostname, withDefaultOrder(options), callback); | |
| 243 | + }, | |
| 244 | + setDefaultResultOrder, | |
| 245 | + getDefaultResultOrder, | |
| 246 | + promises, | |
| 247 | + }; | |
| 248 | + for (const name of unsupported) { | |
| 249 | + dns[name] = notImplemented(name); | |
| 250 | + promises[name] = notImplemented(`promises.${name}`); | |
| 251 | + } | |
| 252 | + | |
| 253 | + __celld.__dnsModule = dns; | |
| 254 | + __celld.__dnsPromisesModule = promises; | |
| 255 | +})(); |
addedcrates/celld/js/node_net.js+64−0View file
| @@ -1,0 +1,64 @@ | ||
| 1 | +// node:net for celld (Homeflare patch). | |
| 2 | +// | |
| 3 | +// The address helpers only: isIP, isIPv4 and isIPv6. They're what libraries | |
| 4 | +// in Workers reach for, usually to check a URL's host before fetching it | |
| 5 | +// (Fedify's public-URL check, for one). Sockets and servers aren't here; | |
| 6 | +// calling one throws with the member's name, as the generic stub did. | |
| 7 | +// | |
| 8 | +// The patterns are Node's own (lib/internal/net.js, MIT), so the answers | |
| 9 | +// match Node and workerd for the same input, zone IDs included. | |
| 10 | +// | |
| 11 | +// Injected lazily, so an isolate that never imports node:net pays nothing. | |
| 12 | +(() => { | |
| 13 | + "use strict"; | |
| 14 | + | |
| 15 | + const v4Seg = "(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])"; | |
| 16 | + const v4Str = `(?:${v4Seg}\\.){3}${v4Seg}`; | |
| 17 | + const IPv4Reg = new RegExp(`^${v4Str}$`); | |
| 18 | + | |
| 19 | + const v6Seg = "(?:[0-9a-fA-F]{1,4})"; | |
| 20 | + const IPv6Reg = new RegExp( | |
| 21 | + "^(?:" + | |
| 22 | + `(?:${v6Seg}:){7}(?:${v6Seg}|:)|` + | |
| 23 | + `(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` + | |
| 24 | + `(?:${v6Seg}:){5}(?::${v4Str}|(?::${v6Seg}){1,2}|:)|` + | |
| 25 | + `(?:${v6Seg}:){4}(?:(?::${v6Seg}){0,1}:${v4Str}|(?::${v6Seg}){1,3}|:)|` + | |
| 26 | + `(?:${v6Seg}:){3}(?:(?::${v6Seg}){0,2}:${v4Str}|(?::${v6Seg}){1,4}|:)|` + | |
| 27 | + `(?:${v6Seg}:){2}(?:(?::${v6Seg}){0,3}:${v4Str}|(?::${v6Seg}){1,5}|:)|` + | |
| 28 | + `(?:${v6Seg}:){1}(?:(?::${v6Seg}){0,4}:${v4Str}|(?::${v6Seg}){1,6}|:)|` + | |
| 29 | + `(?::(?:(?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` + | |
| 30 | + ")(?:%[0-9a-zA-Z-.:]{1,})?$", | |
| 31 | + ); | |
| 32 | + | |
| 33 | + // Node stringifies its argument, so isIP(null) is 0 rather than a throw. | |
| 34 | + const isIPv4 = (input) => IPv4Reg.test(`${input}`); | |
| 35 | + const isIPv6 = (input) => IPv6Reg.test(`${input}`); | |
| 36 | + const isIP = (input) => { | |
| 37 | + if (isIPv4(input)) return 4; | |
| 38 | + if (isIPv6(input)) return 6; | |
| 39 | + return 0; | |
| 40 | + }; | |
| 41 | + | |
| 42 | + const notImplemented = (name) => { | |
| 43 | + const fn = function () { | |
| 44 | + throw new Error(`node:net.${name} is not implemented in celld`); | |
| 45 | + }; | |
| 46 | + Object.defineProperty(fn, "name", { value: name }); | |
| 47 | + return fn; | |
| 48 | + }; | |
| 49 | + | |
| 50 | + const net = { | |
| 51 | + isIP, | |
| 52 | + isIPv4, | |
| 53 | + isIPv6, | |
| 54 | + BlockList: notImplemented("BlockList"), | |
| 55 | + connect: notImplemented("connect"), | |
| 56 | + createConnection: notImplemented("createConnection"), | |
| 57 | + createServer: notImplemented("createServer"), | |
| 58 | + Server: notImplemented("Server"), | |
| 59 | + Socket: notImplemented("Socket"), | |
| 60 | + SocketAddress: notImplemented("SocketAddress"), | |
| 61 | + Stream: notImplemented("Stream"), | |
| 62 | + }; | |
| 63 | + __celld.__netModule = net; | |
| 64 | +})(); |
modifieddocs/cloudflare-compat.md+4−0View file
| @@ -152,6 +152,10 @@ | ||
| 152 | 152 | - `KeyObject.toCryptoKey()` applies the requested algorithm, extractability, |
| 153 | 153 | and usages to an asymmetric key. |
| 154 | 154 | - `node:zlib` implements only the synchronous gzip and deflate functions. |
| 155 | +- `node:net` provides `isIP`, `isIPv4` and `isIPv6` (Homeflare patch). | |
| 156 | +- `node:dns` and `node:dns/promises` provide `lookup()`, which resolves | |
| 157 | + through the node's own resolver. The `resolve*()` functions are not | |
| 158 | + implemented (Homeflare patch). | |
| 155 | 159 | - `node:fs` provides `access`, `mkdir`, `realpath`, `stat`, `lstat`, and |
| 156 | 160 | `readFile`. It exposes an empty, request-local `/tmp` and a read-only |
| 157 | 161 | `/bundle` that contains the Worker modules. |
addedhomeflare/smoke/node-net-dns/index.js+81−0View file
| @@ -1,0 +1,81 @@ | ||
| 1 | +// Smoke test for the node:net and node:dns patch. Each import style is used | |
| 2 | +// once: named, default, namespace and dynamic. GET / answers a JSON report; | |
| 3 | +// every check is { name, ok, got }. | |
| 4 | +import { isIP, isIPv4, isIPv6 } from "node:net"; | |
| 5 | +import dns from "node:dns/promises"; | |
| 6 | +import * as dnsCallback from "node:dns"; | |
| 7 | + | |
| 8 | +const expectEqual = (name, got, want) => ({ | |
| 9 | + name, | |
| 10 | + ok: JSON.stringify(got) === JSON.stringify(want), | |
| 11 | + got, | |
| 12 | +}); | |
| 13 | + | |
| 14 | +const expectThrows = async (name, fn, code) => { | |
| 15 | + try { | |
| 16 | + await fn(); | |
| 17 | + return { name, ok: false, got: "no error" }; | |
| 18 | + } catch (error) { | |
| 19 | + return { name, ok: code ? error.code === code : true, got: `${error.code ?? ""} ${error.message}` }; | |
| 20 | + } | |
| 21 | +}; | |
| 22 | + | |
| 23 | +export default { | |
| 24 | + async fetch(request) { | |
| 25 | + const checks = [ | |
| 26 | + // node:net, cases from Node's test-net-isip.js | |
| 27 | + expectEqual("isIP 127.0.0.1", isIP("127.0.0.1"), 4), | |
| 28 | + expectEqual("isIP x127.0.0.1", isIP("x127.0.0.1"), 0), | |
| 29 | + expectEqual("isIP 0000:0000:0000:0000:0000:0000:0000:0000", isIP("0000:0000:0000:0000:0000:0000:0000:0000"), 6), | |
| 30 | + expectEqual("isIP ::1", isIP("::1"), 6), | |
| 31 | + expectEqual("isIP ::ffff:127.0.0.1", isIP("::ffff:127.0.0.1"), 6), | |
| 32 | + expectEqual("isIP fe80::1%eth0", isIP("fe80::1%eth0"), 6), | |
| 33 | + expectEqual("isIP 1050:0:0:0:5:600:300c:326b", isIP("1050:0:0:0:5:600:300c:326b"), 6), | |
| 34 | + expectEqual("isIP 2001:252:0:1::2008:6", isIP("2001:252:0:1::2008:6"), 6), | |
| 35 | + expectEqual("isIP 2001:dead:beef:1::2008:6", isIP("2001:dead:beef:1::2008:6"), 6), | |
| 36 | + expectEqual("isIP 256.0.0.0", isIP("256.0.0.0"), 0), | |
| 37 | + expectEqual("isIP 1:2:3::4:5:6:7:8:9", isIP("1:2:3::4:5:6:7:8:9"), 0), | |
| 38 | + expectEqual("isIP 1.2.3.4.5", isIP("1.2.3.4.5"), 0), | |
| 39 | + expectEqual("isIP example.com", isIP("example.com"), 0), | |
| 40 | + expectEqual("isIP empty", isIP(""), 0), | |
| 41 | + expectEqual("isIP null", isIP(null), 0), | |
| 42 | + expectEqual("isIPv4 127.0.0.1", isIPv4("127.0.0.1"), true), | |
| 43 | + expectEqual("isIPv4 ::1", isIPv4("::1"), false), | |
| 44 | + expectEqual("isIPv6 ::1", isIPv6("::1"), true), | |
| 45 | + expectEqual("isIPv6 127.0.0.1", isIPv6("127.0.0.1"), false), | |
| 46 | + | |
| 47 | + // node:dns/promises | |
| 48 | + expectEqual("lookup literal v4", await dns.lookup("10.1.2.3"), { address: "10.1.2.3", family: 4 }), | |
| 49 | + expectEqual("lookup literal v6 all", await dns.lookup("::1", { all: true }), [{ address: "::1", family: 6 }]), | |
| 50 | + expectEqual("lookup localhost has 127.0.0.1", | |
| 51 | + (await dns.lookup("localhost", { all: true })).some((r) => r.address === "127.0.0.1"), true), | |
| 52 | + expectEqual("lookup localhost family 4", | |
| 53 | + (await dns.lookup("localhost", 4)).family, 4), | |
| 54 | + await expectThrows("lookup .invalid is ENOTFOUND", | |
| 55 | + () => dns.lookup("does-not-exist.invalid"), "ENOTFOUND"), | |
| 56 | + await expectThrows("lookup bad family", () => dns.lookup("localhost", 5), "ERR_INVALID_ARG_VALUE"), | |
| 57 | + await expectThrows("resolve4 not implemented", () => dns.resolve4("localhost")), | |
| 58 | + | |
| 59 | + // node:dns callback style, through a namespace import | |
| 60 | + expectEqual("callback lookup literal", | |
| 61 | + await new Promise((resolve) => | |
| 62 | + dnsCallback.lookup("192.0.2.1", (error, address, family) => resolve([error, address, family]))), | |
| 63 | + [null, "192.0.2.1", 4]), | |
| 64 | + expectEqual("dns.promises is node:dns/promises", dnsCallback.promises.lookup === dns.lookup, true), | |
| 65 | + ]; | |
| 66 | + | |
| 67 | + // Dynamic import, which goes through the namespace path. | |
| 68 | + const net = await import("node:net"); | |
| 69 | + checks.push(expectEqual("dynamic import isIP", net.isIP("::1"), 6)); | |
| 70 | + | |
| 71 | + // Optional: a public name, when the node has DNS (GET /?public=example.com). | |
| 72 | + const publicName = new URL(request.url).searchParams.get("public"); | |
| 73 | + if (publicName) { | |
| 74 | + const found = await dns.lookup(publicName, { all: true }); | |
| 75 | + checks.push({ name: `lookup ${publicName}`, ok: found.length > 0 && found.every((r) => isIP(r.address) === r.family), got: found }); | |
| 76 | + } | |
| 77 | + | |
| 78 | + const failed = checks.filter((c) => !c.ok); | |
| 79 | + return Response.json({ ok: failed.length === 0, failed, checks }, { status: failed.length ? 500 : 200 }); | |
| 80 | + }, | |
| 81 | +}; |
addedhomeflare/smoke/node-net-dns/wrangler.jsonc+6−0View file
| @@ -1,0 +1,6 @@ | ||
| 1 | +{ | |
| 2 | + "name": "node-net-dns", | |
| 3 | + "main": "index.js", | |
| 4 | + "compatibility_date": "2026-01-01", | |
| 5 | + "compatibility_flags": ["nodejs_compat"] | |
| 6 | +} |
addedhomeflare/smoke/run.sh+54−0View file
| @@ -1,0 +1,54 @@ | ||
| 1 | +#!/usr/bin/env bash | |
| 2 | +# Run each smoke Worker under `celld dev` and fail if its report isn't ok. | |
| 3 | +# A Worker's paths.txt adds requests: "PATH<TAB>TEXT" lines, each passing when | |
| 4 | +# GET PATH answers 200 with TEXT in its body. | |
| 5 | +# homeflare/smoke/run.sh [path/to/celld] | |
| 6 | +# Defaults to target/debug/celld. Needs esbuild on PATH (celld bundles with it). | |
| 7 | +set -euo pipefail | |
| 8 | + | |
| 9 | +SMOKE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| 10 | +REPO_DIR="$(cd "$SMOKE_DIR/../.." && pwd)" | |
| 11 | +CELLD="${1:-$REPO_DIR/target/debug/celld}" | |
| 12 | +CELLD="$(cd "$(dirname "$CELLD")" && pwd)/$(basename "$CELLD")" | |
| 13 | +PORT="${SMOKE_PORT:-9876}" | |
| 14 | + | |
| 15 | +[ -x "$CELLD" ] || { echo "no celld at $CELLD" >&2; exit 1; } | |
| 16 | + | |
| 17 | +status=0 | |
| 18 | +for worker in "$SMOKE_DIR"/*/; do | |
| 19 | + name="$(basename "$worker")" | |
| 20 | + log="$(mktemp)" | |
| 21 | + (cd "$worker" && NO_COLOR=1 exec "$CELLD" dev --clean --port "$PORT") >"$log" 2>&1 & | |
| 22 | + pid=$! | |
| 23 | + report="" | |
| 24 | + for _ in $(seq 1 120); do | |
| 25 | + report="$(curl -sS "http://127.0.0.1:$PORT/${SMOKE_QUERY:-}" 2>/dev/null || true)" | |
| 26 | + [ -n "$report" ] && break | |
| 27 | + kill -0 "$pid" 2>/dev/null || break | |
| 28 | + sleep 0.5 | |
| 29 | + done | |
| 30 | + extra="" | |
| 31 | + if [ -f "$worker/paths.txt" ]; then | |
| 32 | + while IFS=$'\t' read -r path want; do | |
| 33 | + [ -n "$path" ] || continue | |
| 34 | + got="$(curl -sS -w '\n%{http_code}' "http://127.0.0.1:$PORT$path" 2>&1 || true)" | |
| 35 | + code="${got##*$'\n'}"; body="${got%$'\n'*}" | |
| 36 | + if [ "$code" != 200 ] || [[ "$body" != *"$want"* ]]; then | |
| 37 | + extra+="GET $path: $code $body"$'\n' | |
| 38 | + fi | |
| 39 | + done < "$worker/paths.txt" | |
| 40 | + fi | |
| 41 | + kill "$pid" 2>/dev/null || true | |
| 42 | + wait "$pid" 2>/dev/null || true | |
| 43 | + rm -rf "$worker/.celld" | |
| 44 | + if printf '%s' "$report" | grep -q '"ok":true' && [ -z "$extra" ]; then | |
| 45 | + echo "ok $name" | |
| 46 | + else | |
| 47 | + echo "FAIL $name" | |
| 48 | + if [ -n "$report" ]; then printf '%s\n' "$report"; else cat "$log"; fi | |
| 49 | + printf '%s' "$extra" | |
| 50 | + status=1 | |
| 51 | + fi | |
| 52 | + rm -f "$log" | |
| 53 | +done | |
| 54 | +exit "$status" |