athanclark / purescript-websocket-moderate

A different websocket binding for purescript
BSD 3-Clause "New" or "Revised" License
3 stars 3 forks source link

WebSocket error JSON.stringify may fail #1

Closed djoyner closed 5 years ago

djoyner commented 5 years ago

While doing some negative testing I produced a crash here: https://github.com/athanclark/purescript-websocket-moderate/blob/master/src/WebSocket.js#L28

TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at WebSocket.<anonymous> (/Users/djoyner/repo/purescript-chrome-devtools/output/WebSocket/foreign.js:30:23)
    at WebSocket.onError (/Users/djoyner/node_modules/ws/lib/EventTarget.js:109:16)
    at emitOne (events.js:116:13)
    at WebSocket.emit (events.js:211:7)
    at WebSocket.finalize (/Users/djoyner/node_modules/ws/lib/WebSocket.js:182:41)
    at ClientRequest._req.on (/Users/djoyner/node_modules/ws/lib/WebSocket.js:653:12)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:543:21)

Here's the output of console.log(e):

{ Error: unexpected server response (500)
    at ClientRequest._req.on (/Users/djoyner/node_modules/ws/lib/WebSocket.js:653:21)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:543:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:112:17)
    at Socket.socketOnData (_http_client.js:440:20)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
  type: 'error',
  target:
   WebSocket {
     domain: null,
     _events:
      { close: [Object],
        error: [Object],
        message: [Object],
        open: [Object] },
     _eventsCount: 4,
     _maxListeners: undefined,
     readyState: 2,
     bytesReceived: 0,
     extensions: {},
     protocol: '',
     _binaryType: 'nodebuffer',
     _finalize: [Function: bound finalize],
     _closeFrameReceived: false,
     _closeFrameSent: false,
     _closeMessage: '',
     _closeTimer: null,
     _finalized: true,
     _closeCode: 1006,
     _receiver: null,
     _sender: null,
     _socket: null,
     _ultron: null,
     protocolVersion: 13,
     _isServer: false,
     url: 'ws://localhost:9222/devtools/page/61D46411B7962502293BEA05CE72B4CBfoo',
     _req:
      ClientRequest {
        domain: null,
        _events: [Object],
        _eventsCount: 4,
        _maxListeners: undefined,
        output: [],
        outputEncodings: [],
        outputCallbacks: [],
        outputSize: 0,
        writable: true,
        _last: true,
        upgrading: true,
        chunkedEncoding: false,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: 0,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Object],
        connection: [Object],
        _header: 'GET /devtools/page/61D46411B7962502293BEA05CE72B4CBfoo HTTP/1.1\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: yJzOTzoDQsbX7g/dc+lyjA==\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\nHost: localhost:9222\r\n\r\n',
        _onPendingData: [Function: noopPendingOutput],
        agent: [Object],
        socketPath: undefined,
        timeout: undefined,
        method: 'GET',
        path: '/devtools/page/61D46411B7962502293BEA05CE72B4CBfoo',
        _ended: false,
        res: [Object],
        aborted: 1542897059592,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: [Object],
        maxHeadersCount: null,
        [Symbol(outHeadersKey)]: [Object] } } }
athanclark commented 5 years ago

Agh jeepers, I'll fix this right away, sorry about this!

djoyner commented 5 years ago

@athanclark No need to apologize at all! I'm new to PureScript but it has been a pleasure to use your libraries (this one and purescript-queue). Thank you for producing them.

athanclark commented 5 years ago

Hah no problem. I've released a new version v3.1.0 that should address this issue cosmetically - you'll still need to re-throw the exception with Effect.Exception (throwException) or something to help relay the Error, so you can get a better look at what's throwing it. Kinda silly of me to try and serialize it though lol, this library is a bit old.