Skip to content

Comments

feat(dns): added support for a custom lookup function; (#5339)#6

Open
MitchLewis930 wants to merge 1 commit intopr_026_beforefrom
pr_026_after
Open

feat(dns): added support for a custom lookup function; (#5339)#6
MitchLewis930 wants to merge 1 commit intopr_026_beforefrom
pr_026_after

Conversation

@MitchLewis930
Copy link

@MitchLewis930 MitchLewis930 commented Jan 30, 2026

User description

PR_026


PR Type

Enhancement


Description

  • Added support for custom DNS lookup functions in HTTP adapter

  • Supports both callback-based and async lookup functions

  • Implemented callbackify helper to convert async functions to callbacks

  • Added family option for IPv4/IPv6 selection

  • Comprehensive test coverage and TypeScript type definitions


Diagram Walkthrough

flowchart LR
  A["HTTP Adapter Config"] -->|lookup option| B["Callbackify Helper"]
  B -->|converts async| C["Callback Function"]
  C -->|passes to| D["Node.js HTTP Options"]
  A -->|family option| D
  E["Utils Functions"] -->|isAsyncFn detection| B
Loading

File Walkthrough

Relevant files
Enhancement
http.js
Integrated custom DNS lookup support                                         

lib/adapters/http.js

  • Imported callbackify helper for async function conversion
  • Extracted lookup and family from config parameters
  • Added logic to detect and convert async lookup functions to callbacks
  • Passed lookup and family to Node.js HTTP request options
+15/-1   
callbackify.js
New async-to-callback converter utility                                   

lib/helpers/callbackify.js

  • New helper file that converts async functions to callback-based
    functions
  • Supports optional reducer function for transforming return values
  • Handles error propagation from async functions to callbacks
+16/-0   
utils.js
Added function type detection utilities                                   

lib/utils.js

  • Added isAsyncFn function to detect async functions
  • Added isGeneratorFn and isAsyncGeneratorFn for generator detection
  • Added isPlainFunction to identify regular functions
  • Added isThenable to detect promise-like objects
  • Exported all new utility functions
+15/-1   
Tests
http.js
Added comprehensive DNS lookup tests                                         

test/unit/adapters/http.js

  • Added SERVER_HANDLER_STREAM_ECHO constant for test server
  • Added DNS test suite with three test cases
  • Tests cover callback-based lookup, async lookup, and async lookup
    returning only IP
  • Verifies lookup function is called and request succeeds
+61/-0   
Documentation
index.d.ts
Added DNS lookup TypeScript definitions                                   

index.d.ts

  • Added family option typed as 4 | 6 | undefined for IPv4/IPv6 selection
  • Added lookup option with dual signatures for callback and async
    functions
  • Fixed formatting in beforeRedirect type definition
+4/-1     
index.d.cts
Added DNS lookup CommonJS type definitions                             

index.d.cts

  • Added family option typed as 4 | 6 | undefined
  • Added lookup option with callback and async function signatures
+3/-0     
index.ts
Added DNS lookup TypeScript usage examples                             

test/module/typings/esm/index.ts

  • Added example usage of callback-based lookup function
  • Added example usage of async lookup function
  • Demonstrates both function signatures for TypeScript validation
+14/-0   
Formatting
CHANGELOG.md
Fixed file formatting                                                                       

CHANGELOG.md

  • Fixed newline at end of file
+1/-1     

@qodo-code-review
Copy link

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status:
Lookup return validation: The async lookup normalization validates the return is an array but does not validate
array length/value types (e.g., [address, family]), which may lead to unclear downstream
failures for malformed returns.

Referred Code
let {data, lookup, family} = config;
const {responseType, responseEncoding} = config;
const method = config.method.toUpperCase();
let isDone;
let rejected = false;
let req;

if (lookup && utils.isAsyncFn(lookup)) {
  lookup = callbackify(lookup, (entry) => {
    if(utils.isString(entry)) {
      entry = [entry, entry.indexOf('.') < 0 ? 6 : 4]
    } else if (!utils.isArray(entry)) {
      throw new TypeError('lookup async function must return an array [ip: string, family: number]]')
    }
    return entry;
  })
}

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status:
Unvalidated family value: The family option from config is forwarded into Node request options without runtime
validation (expected 4 | 6), which could cause unexpected behavior or errors if
user-provided input is malformed.

Referred Code
let {data, lookup, family} = config;
const {responseType, responseEncoding} = config;

Learn more about managing compliance generic rules or creating your own custom rules

Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Use net.isIP() for IP validation

Replace the naive IP family detection logic with Node.js's net.isIP() function
for accurate and reliable validation of IP addresses returned by the lookup
function.

lib/adapters/http.js [159-163]

 if(utils.isString(entry)) {
-  entry = [entry, entry.indexOf('.') < 0 ? 6 : 4]
+  const family = net.isIP(entry);
+  if (family === 0) {
+    throw new TypeError('lookup async function must return a valid IP address string');
+  }
+  entry = [entry, family];
 } else if (!utils.isArray(entry)) {
-  throw new TypeError('lookup async function must return an array [ip: string, family: number]]')
+  throw new TypeError('lookup async function must return an array [ip: string, family: number] or an IP address string.')
 }
  • Apply / Chat
Suggestion importance[1-10]: 8

__

Why: The suggestion correctly identifies that the IP family detection logic is flawed and proposes using the robust net.isIP() function, which fixes a correctness bug and improves validation.

Medium
General
Fix typo in error message

Fix a typo in the TypeError message by removing an extra closing bracket.

lib/adapters/http.js [162]

-throw new TypeError('lookup async function must return an array [ip: string, family: number]]')
+throw new TypeError('lookup async function must return an array [ip: string, family: number]')
  • Apply / Chat
Suggestion importance[1-10]: 2

__

Why: This suggestion corrects a minor typo in an error message, which improves developer experience but has no impact on runtime behavior.

Low
  • More

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants