max-mapper / websocket-stream

websockets with the node stream API
BSD 2-Clause "Simplified" License
667 stars 114 forks source link

Typescript createServer typing not correct #158

Open forkachild opened 4 years ago

forkachild commented 4 years ago

In index.d.ts

function createServer(opts?: WebSocket.ServerOptions, callback?: () => void): Server;

Should become

function createServer(opts?: WebSocket.ServerOptions, callback?: (socket: WebSocketDuplex) => void): Server;

Since the callback is missing the parameter, typescript isn't allowing to pass a handler function

max-carroll-sky commented 5 months ago
'use strict'

var WebSocketServer = require('ws').Server
var stream = require('./stream')

class Server extends WebSocketServer{
  constructor(opts, cb) {
    super(opts)

    var proxied = false
    this.on('newListener', function(event) {
      if (!proxied && event === 'stream') {
        proxied = true
        this.on('connection', function(conn, req) {
          this.emit('stream', stream(conn, opts), req) // <=== it should match up with this right?
        })
      }
    })

    if (cb) {
      this.on('stream', cb)
    }
  }
}

module.exports.Server = Server
module.exports.createServer = function(opts, cb) {
  return new Server(opts, cb)
}