h3poteto / megalodon

Fediverse API client library for node.js and browser
https://h3poteto.github.io/megalodon/
MIT License
250 stars 33 forks source link

Streaming throws on 301 response from an instance #2183

Open not7cd opened 5 months ago

not7cd commented 5 months ago

As I'm trying to connect to my instance with client.publicStreaming(), I'm getting this exception

Error: Unexpected server response: 301
    at ClientRequest.<anonymous> (/data/node_modules/node-red-contrib-megalodon/node_modules/ws/lib/websocket.js:912:7)
    at ClientRequest.emit (node:events:513:28)
    at HTTPParser.parserOnIncomingClient (node:_http_client:693:27)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
    at TLSSocket.socketOnData (node:_http_client:534:22)
    at TLSSocket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at TLSSocket.Readable.push (node:internal/streams/readable:228:10)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23)

It may be connected to a default nginx config that forces TLS? I'm not sure how to debug this. I'm using a slightly modified streaming example from readme. https://github.com/not7cd/node-red-contrib-megalodon/blob/fa9c3b49f51208cffb59d793108b79ab563de272/src/streaming.js

not7cd commented 5 months ago

Quick search suggests changing the url from ws:// to wss://.

not7cd commented 5 months ago

I was able to hack a solution in transpiled JS. Two fixes were needed in my case. Append / to /api/v1/streaming. This solved 301 redirect to /api/v1/streaming/. Not sure where this came from.

I also had to add .trim() in Streaming, as for some reason, space %20 was being added here. It may be a bug coming from my implementation in Node-RED. But it may be worth to add sanitization in the lib.

parameter.push("access_token=".concat(accessToken.trim()));
h3poteto commented 5 months ago

Which server did you use? Please provide a sample code to reproduce this.