Skip to content

indefinite behavior of nodejs/events.once() on WebSocket #1795

Closed
@zimtsui

Description

@zimtsui

hello guys.

the function once of nodejs v12 LTS makes different behaviors between EventEmitter or EventTarget. REF

This method is intentionally generic and works with the web platform EventTarget interface, which has no special 'error' event semantics and does not listen to the 'error' event.

once makes judgement about whether an emitter is an EventEmitter or an EventTarget according to whether emitter.addEventListener is a function. REF

since WebSocket implements addEventListener in

const { addEventListener, removeEventListener } = require('./event-target');

once will misjudge the emitter to be an EventTarget and it will not listen to error event. thus, users cannot do:

async function connect(){
    ws = new WebSocket('xxx');
    await once(ws, 'open');
}

this will be waiting forever if it fails to connect.

since it's said in docs that

This class represents a WebSocket server. It extends the EventEmitter.

, this will mislead users.

but this problem does not exist with nodejs v14, because nodejs v14 makes that judgement via whether emitter.on is a function. REF

i don't think it's a bug, but it'd be better if this problem should be warning in docs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions