nitro icon indicating copy to clipboard operation
nitro copied to clipboard

[Bug]: socket.remoteAddress is undefined when using Bun preset

Open nhj7 opened this issue 2 months ago • 2 comments

Environment

  • Nuxt: 4.1.3
  • Nitro preset: bun
  • Runtime: Bun (latest)
  • OS: macOS (Darwin 23.6.0)

Description

When using the Bun preset (nitro: { preset: 'bun' }), event.node.req.socket.remoteAddress and related socket properties return undefined, making it impossible to retrieve client IP addresses in server middleware or API routes.

Reproduction

1. Configure Bun preset

// nuxt.config.ts
export default defineNuxtConfig({
  nitro: {
    preset: 'bun'
  }
})
  1. Create middleware to log client IP
  // server/middleware/logging.ts
  export default defineEventHandler((event) => {
    const socket = event.node.req.socket;

    console.log('remoteAddress:', socket?.remoteAddress);
    console.log('remoteFamily:', socket?.remoteFamily);
    console.log('remotePort:', socket?.remotePort);
  });
  1. Build and run

bun run build bun run preview

Result with Bun preset:

remoteAddress: undefined remoteFamily: undefined remotePort: undefined

Result with Node preset:

remoteAddress: ::1 remoteFamily: IPv6 remotePort: 54241

Investigation

I investigated the socket object structure and found:

  1. Socket properties exist in prototype but values are undefined:
  // Checking prototype chain
  const proto = Object.getPrototypeOf(socket);
  const props = Object.getOwnPropertyNames(proto);
  // Depth 1: ['address', 'remoteAddress', 'remoteFamily', 'remotePort', 'localAddress']

  // But actual values are undefined
  console.log(socket.remoteAddress); // undefined
  console.log(proto.remoteAddress); // undefined
  1. Bun runtime is not available in the event handler:
  console.log(typeof Bun); // undefined (in built output)
  console.log(globalThis.Bun); // undefined
  1. No Bun-specific request object found: // All return undefined event._request event.web event.request req._bunRequest req[Symbol.for('bun.request')]

Expected Behavior

event.node.req.socket.remoteAddress should return the client IP address when using the Bun preset, just like it does with the Node preset.

Current Workaround

Switching to preset: 'node-server' works correctly, but we'd like to use Bun's performance benefits.

Additional Context

  • The issue only occurs with the Bun preset
  • Using getRequestIP() from h3 also returns undefined
  • X-Forwarded-For headers work, but direct socket access doesn't
  • This affects logging, rate limiting, and security features that rely on client IP

Questions

  1. Is this a known limitation of the Bun preset?
  2. Does Bun's networking layer not expose socket information the same way Node.js does?
  3. Is there a Bun-specific way to access client IP that we should use instead?

Would appreciate any guidance on how to properly retrieve client IP addresses when using the Bun preset. Thank you!


Describe the bug

Describe the bug

When using the Bun preset (nitro: { preset: 'bun' }), event.node.req.socket.remoteAddress and all related socket properties (remoteFamily, remotePort, etc.) return undefined, making it impossible to retrieve client IP addresses in server middleware, API routes, or Nitro plugins.

The same code works perfectly with preset: 'node-server', where all socket properties are properly populated.

Investigation findings:

  1. Socket properties exist in the prototype chain but have no values:

    • Object.getOwnPropertyNames(Object.getPrototypeOf(socket)) shows: ['address', 'remoteAddress', 'remoteFamily', 'remotePort', 'localAddress']
    • But socket.remoteAddress returns undefined
  2. Bun runtime not accessible in event handlers:

    • globalThis.Bun is undefined in the built output
    • No Bun-specific request objects found (event._request, req._bunRequest, etc.)
  3. Alternative methods also fail:

    • getRequestIP(event, { xForwardedFor: true }) returns undefined
    • Only X-Forwarded-For headers work (when set by a proxy)

Impact: This affects any feature requiring client IP: logging, rate limiting, geo-location, security measures, etc.

Workaround: Using preset: 'node-server' works, but we lose Bun's performance benefits.

Is this a known limitation of Bun's networking layer, or is there a Bun-specific API we should use instead?

nhj7 avatar Oct 09 '25 00:10 nhj7

For Nitro v2, I recommend using node preset and leveraging Bun's Node.js compatibility (which is quite fast)

pi0 avatar Oct 09 '25 07:10 pi0

Since address, family, and port are already available from server.requestIP(req) in Bun, but this behavior isn’t consistent across other runtimes, could we have a helper function like getRemoteInfo(event) in the Nitro context for a uniform interface?

Also, are the socket properties being undefined related to the Bun adapter in srvx?

Saeid-Za avatar Nov 06 '25 12:11 Saeid-Za