Skip to content

fix: Fix createServer() to properly handle Node.js-compatible options parameter #210

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

Merged
merged 3 commits into from
Apr 23, 2025

Conversation

Myestery
Copy link
Contributor

@Myestery Myestery commented Feb 25, 2025

Fix createServer to properly handle Node.js-compatible options parameter

fix #183 fix #209

Issue

Currently, when using createServer with options similar to Node.js, it causes a TypeError:

// This works with no issues
const server = net.connect({
  noDelay: true,
  keepAlive: true
}, () => {
  console.info('server started')
})

// This causes TypeError: The listener must be a function
const server = net.createServer({
  noDelay: true,
  keepAlive: true
}, () => {
  console.info('server started')
})

The signature of createServer needs to be consistent with Node.js, where options can be passed as the first argument.

Changes

This PR implements:

  1. Updated Server.js constructor to properly handle options vs. connectionListener arguments
  2. Added proper ServerOptions type definition with support for:
    • noDelay
    • keepAlive
    • keepAliveInitialDelay
    • allowHalfOpen
    • pauseOnConnect
  3. Enhanced the listen() method to match Node.js signature patterns:
    • listen(port, [host], [callback])
    • listen(options, [callback])
  4. Fixed TypeScript definitions to correctly type all parameters and eliminate type errors
  5. Added _applySocketOptions method to apply server options to new socket connections

Testing

The following patterns now work without errors:

// With options and listener
const server1 = net.createServer({
  noDelay: true,
  keepAlive: true
}, () => {
  console.info('server started')
});

// Just with listener
const server2 = net.createServer(() => {
  console.info('connection received')
});

// Setting options with just the object
const server3 = net.createServer({
  noDelay: true,
  keepAlive: true
});
server3.on('connection', () => {
  console.info('connection received');
});

These changes improve compatibility with code written for Node.js and make the library more intuitive to use for developers familiar with the Node.js API.

@Rapsssito Rapsssito changed the title Fix createServer to properly handle Node.js-compatible options paramete fix: Fix createServer() to properly handle Node.js-compatible options parameter Apr 23, 2025
@Rapsssito Rapsssito merged commit ace0e1c into Rapsssito:master Apr 23, 2025
github-actions bot pushed a commit that referenced this pull request Apr 23, 2025
# [6.3.0](v6.2.0...v6.3.0) (2025-04-23)

### Bug Fixes

* Fix createServer() to properly handle Node.js-compatible options parameter ([#210](#210)) ([ace0e1c](ace0e1c))
* Upgrade bouncycastle dependency to 1.78.1 ([#205](#205)) ([f33522e](f33522e))

### Features

* **iOS:** Add more NodeJS TLS options ([#208](#208)) ([6d2fcae](6d2fcae))
* Added compatibility for concurrenct connections for Android 15 ([#206](#206)) ([4284f91](4284f91))
Copy link

🎉 This PR is included in version 6.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants