Skip to content

Fix createServer to properly handle Node.js-compatible options paramete #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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Myestery
Copy link

@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.

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