Skip to content
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

Bug: createWebSocker option prevents from using custom WebSocket types #38

Open
JulioMh opened this issue Sep 13, 2021 · 0 comments
Open

Comments

@JulioMh
Copy link

JulioMh commented Sep 13, 2021

When trying to create a new WebSocketAsPromised using TypeScript, compiler throws an error if your WebSocket object is coming from some library such WebSocket-Node

Code:

import WebSocketAsPromised from 'websocket-as-promised' 
import websocket from 'websocket'

const W3CWebSocket = websocket.w3cwebsocket

const ws = new WebSocketAsPromised('xxxx', {
  createWebSocket: url => new W3CWebSocket(url)
})

Error:

Type '(url: string) => w3cwebsocket' is not assignable to type '(url: string) => WebSocket'.
  Type 'w3cwebsocket' is missing the following properties from type 'WebSocket': addEventListener, removeEventListener, dispatchEvent

Cause (type/options.d.ts):

export = Options;

declare interface Options {
    createWebSocket?: (url: string) => WebSocket; <------------ maybe add | any (?)
    packMessage?: (data: any) => string | ArrayBuffer | Blob;
    unpackMessage?: (data: string | ArrayBuffer | Blob) => any;
    attachRequestId?: (data: any, requestId: string | number) => any;
    extractRequestId?: (data: any) => string | number | undefined;
    extractMessageData?: (event: any) => any;
    timeout?: number;
    connectionTimeout?: number;
}

Solution from client code

import WebSocketAsPromised from 'websocket-as-promised' 
import websocket from 'websocket'

const W3CWebSocket = websocket.w3cwebsocket

const ws = new WebSocketAsPromised('xxxx', {
  createWebSocket: url => new W3CWebSocket(url) as any
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant