{"record":{"id":"42874bbdcdaefed3","repo":"nodejs/node","slug":"invalid-affinity-must-be-either-4-or-6","errorCode":null,"errorMessage":"Invalid affinity. Must be either 4 or 6","messagePattern":"Invalid affinity\\. Must be either 4 or 6","errorType":"exception","errorClass":"InvalidArgumentError","httpStatus":null,"severity":"error","filePath":"deps/undici/src/lib/interceptor/dns.js","lineNumber":481,"sourceCode":"    throw new InvalidArgumentError('Invalid maxTTL. Must be a positive number')\n  }\n\n  if (\n    interceptorOpts?.maxItems != null &&\n    (typeof interceptorOpts?.maxItems !== 'number' ||\n      interceptorOpts?.maxItems < 1)\n  ) {\n    throw new InvalidArgumentError(\n      'Invalid maxItems. Must be a positive number and greater than zero'\n    )\n  }\n\n  if (\n    interceptorOpts?.affinity != null &&\n    interceptorOpts?.affinity !== 4 &&\n    interceptorOpts?.affinity !== 6\n  ) {\n    throw new InvalidArgumentError('Invalid affinity. Must be either 4 or 6')\n  }\n\n  if (\n    interceptorOpts?.dualStack != null &&\n    typeof interceptorOpts?.dualStack !== 'boolean'\n  ) {\n    throw new InvalidArgumentError('Invalid dualStack. Must be a boolean')\n  }\n\n  if (\n    interceptorOpts?.lookup != null &&\n    typeof interceptorOpts?.lookup !== 'function'\n  ) {\n    throw new InvalidArgumentError('Invalid lookup. Must be a function')\n  }\n\n  if (\n    interceptorOpts?.pick != null &&","sourceCodeStart":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/undici/src/lib/interceptor/dns.js#L463-L499","documentation":"Thrown by the DNS interceptor factory (`InvalidArgumentError`, code `UND_ERR_INVALID_ARG`) when `affinity` is provided and is neither `4` (IPv4) nor `6` (IPv6). `affinity` biases connection attempts toward one address family in dual-stack setups so the connect race prefers the desired family. Only the two literal integers are accepted — no strings, booleans, or other numbers.","triggerScenarios":"Passing `affinity: '4'` (string), `affinity: 0`, `affinity: 'ipv4'`, `affinity: true`, or any number other than 4 or 6. The guard is `affinity != null && affinity !== 4 && affinity !== 6`.","commonSituations":"Loading `affinity` from env/config as a string; using an enum-like string token from another library; passing a stringified family from JSON config. When `dualStack` is false the default affinity becomes 4, so omitting the option is usually what you want.","solutions":["Pass either `affinity: 4` or `affinity: 6` as a literal integer.","If the value comes from config, coerce: `Number(affinity) === 4 ? 4 : Number(affinity) === 6 ? 6 : undefined`.","If you have no preference, omit `affinity` entirely — the interceptor picks a sensible default based on `dualStack`."],"exampleFix":"// before\ndns({ affinity: '4' })\ndns({ affinity: process.env.IP_FAMILY })\n\n// after\ndns({ affinity: 4 })\n// or omit it:\ndns({ dualStack: true })","handlingStrategy":"type-guard","validationCode":"function validateAffinity(v) {\n  if (v == null) return undefined\n  const n = Number(v)\n  if (n !== 4 && n !== 6) throw new Error('affinity must be 4 or 6')\n  return n\n}","typeGuard":"function isAffinity(v) {\n  return v === 4 || v === 6\n}","tryCatchPattern":null,"preventionTips":["Pass affinity as a literal integer 4 or 6.","Omit it if you have no family preference."],"tags":["undici","interceptor","dns","ipv4","ipv6","validation","config"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}