socket.io.patch.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var io = require("socket.io");
  2. if (io.version === "0.9.16") {
  3. console.log("patching socket.io hybi-16 transport frame prototype");
  4. var transports = require("socket.io/lib/transports/websocket/hybi-16.js");
  5. transports.prototype.frame = patchedFrameHandler;
  6. // file hybi-07-12 has the same problem but no browsers are using that protocol now
  7. }
  8. function patchedFrameHandler(opcode, str) {
  9. var dataBuffer = new Buffer(str),
  10. dataLength = dataBuffer.length,
  11. startOffset = 2,
  12. secondByte = dataLength;
  13. if (dataLength === 65536) {
  14. console.log("fixing invalid frame length in socket.io");
  15. }
  16. if (dataLength > 65535) {
  17. // original code had > 65536
  18. startOffset = 10;
  19. secondByte = 127;
  20. } else if (dataLength > 125) {
  21. startOffset = 4;
  22. secondByte = 126;
  23. }
  24. var outputBuffer = new Buffer(dataLength + startOffset);
  25. outputBuffer[0] = opcode;
  26. outputBuffer[1] = secondByte;
  27. dataBuffer.copy(outputBuffer, startOffset);
  28. switch (secondByte) {
  29. case 126:
  30. outputBuffer[2] = dataLength >>> 8;
  31. outputBuffer[3] = dataLength % 256;
  32. break;
  33. case 127:
  34. var l = dataLength;
  35. for (var i = 1; i <= 8; ++i) {
  36. outputBuffer[startOffset - i] = l & 0xff;
  37. l >>>= 8;
  38. }
  39. }
  40. return outputBuffer;
  41. }