uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
8.09k stars 577 forks source link

Cannot perform %TypedArray%.prototype.set on a detached ArrayBuffer when using with Socket.io #705

Closed tahayk closed 2 years ago

tahayk commented 2 years ago

Hey I'm trying to use uWebSockets with Socket.io, but I keep getting this error, and I noticed that it only happens when I use Safari:

node:buffer:254
  TypedArrayPrototypeSet(target, source, targetStart);
  ^

TypeError: Cannot perform %TypedArray%.prototype.set on a detached ArrayBuffer
    at Buffer.set (<anonymous>)
    at _copyActual (node:buffer:254:3)
    at Function.concat (node:buffer:562:12)
    at onEnd (/Users/.../node_modules/engine.io/build/transports-uws/polling.js:126:32)
    at /Users/tahayk/.../node_modules/engine.io/build/transports-uws/polling.js:143:17

Node.js v17.0.1

Here is the example I used:

server.js:

const { App } = require("uWebSockets.js");
const { Server } = require("socket.io");

const app = new App();
const io = new Server({ cors: {
        origin: "*",
        methods: ["GET", "POST", "PUT"]
    }});

io.attachApp(app);

io.on("connection", (socket) => {
    // ...
});

app.listen(9000, (token) => {
    if (!token) {
        console.warn("port already in use");
    }
});

demo.html

<!doctype html>
<html>
<head>
    <title>Socket.IO uWS</title>
</head>
<script src="node_modules/socket.io/client-dist/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
    $(function () {
        var socket = io(
            'ws://127.0.0.1:9000',
            {
                // transports: ["websocket"] // ----> it works if you add this
            });

        socket.onAny((eventName, ...args) => {
            console.log("received event: " + eventName);
        });
    });
</script>
<body>
</body>
</html>

Any idea about this issue ? Thanks.

e3dio commented 2 years ago

You can see the error is in engine.io/build/transports-uws/polling.js so you need to open issue in engine.io library

e3dio commented 2 years ago

@tahayk They are doing this wrong on this line https://github.com/socketio/engine.io/blob/a463d268ed90064e7863679bda423951de108c36/lib/transports-uws/polling.ts#L158 They are trying to concat Buffer views of arrayBuffers that are already gone, need to make copy

e3dio commented 2 years ago
Buffer.from(arrayBuffer).copy(allocatedBuffer, offset)
ghost commented 2 years ago

That's kind of interesting that they've actually built polling on uws http and are using the pub sub features. That means we've won, they have adopted our features and our API.

Compare this with 2016 when we adopted their interfaces and acted like a drop in module for their shitty API. That's a milestone since the NPM shitfest days.

e3dio commented 2 years ago

I added PR to fix Socket.io issue here https://github.com/socketio/engine.io/pull/642 as described here https://github.com/socketio/socket.io/discussions/4281#discussioncomment-2193206

e3dio commented 2 years ago

@tahayk they just merged PR and released new version, that should be fixed for you

tahayk commented 2 years ago

@e3dio thank you very much for your help