Skip to content

Commit 941b0c4

Browse files
fix: store handshake headers on the socket object
Related: socketio/socket.io#5394 We could also have used the "connection" event: ```js - engine.on("connection", (socket, { remoteAddress, headers }) => {}); + engine.on("connection", (socket, request, server) => {}); ``` In order to abstract the way each runtime gives access to the headers and the remote address of the client: - Node.js: `IncomingMessage` object with `remoteAddress` - Deno: `Request` native object and `Deno.ServeHandlerInfo` - Bun: `Request` native object and `server.requestIP()` That would be a breaking change though.
1 parent 23cf9a6 commit 941b0c4

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

lib/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ export class Server extends EventEmitter<
347347

348348
debug(`new socket ${id}`);
349349

350-
const socket = new Socket(id, this.opts, transport);
350+
const socket = new Socket(id, this.opts, transport, req);
351351
this.clients.set(id, socket);
352352

353353
socket.once("close", (reason) => {

lib/socket.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class Socket extends EventEmitter<
4040
public readonly id: string;
4141
public readyState: ReadyState = "opening";
4242
public transport: Transport;
43+
public readonly request;
4344

4445
private readonly opts: ServerOptions;
4546
private upgradeState: UpgradeState = "not_upgraded";
@@ -50,14 +51,27 @@ export class Socket extends EventEmitter<
5051
private pingIntervalTimer?: Timer;
5152
private pingTimeoutTimer?: Timer;
5253

53-
constructor(id: string, opts: ServerOptions, transport: Transport) {
54+
constructor(
55+
id: string,
56+
opts: ServerOptions,
57+
transport: Transport,
58+
req: Request,
59+
) {
5460
super();
5561

5662
this.id = id;
5763
this.opts = opts;
5864

5965
this.transport = transport;
6066
this.bindTransport(transport);
67+
68+
// we store the headers of the handshake request, so that they are available in the `socket.handshake` attribute in
69+
// the `socket.io` library
70+
this.request = {
71+
headers: Object.fromEntries(req.headers.entries()),
72+
connection: {},
73+
};
74+
6175
this.onOpen();
6276
}
6377

test/socket.test.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,46 @@ async function initSocketIOConnection() {
2525
return socket;
2626
}
2727

28-
// imported from https://github.com/socketio/socket.io/tree/main/docs/socket.io-protocol/v5-test-suite
29-
describe("Socket.IO protocol", () => {
30-
beforeAll(() => {
31-
const io = new Server();
28+
function setup() {
29+
const io = new Server();
3230

33-
const engine = new Engine({
34-
path: "/socket.io/",
35-
pingInterval: PING_INTERVAL,
36-
pingTimeout: PING_TIMEOUT,
37-
});
31+
const engine = new Engine({
32+
path: "/socket.io/",
33+
pingInterval: PING_INTERVAL,
34+
pingTimeout: PING_TIMEOUT,
35+
});
3836

39-
io.bind(engine);
37+
io.bind(engine);
4038

41-
io.on("connection", (socket) => {
42-
socket.emit("auth", socket.handshake.auth);
39+
io.on("connection", (socket) => {
40+
expect(socket.handshake.headers).toContainKey("host");
4341

44-
socket.on("message", (...args) => {
45-
socket.emit.apply(socket, ["message-back", ...args]);
46-
});
42+
socket.emit("auth", socket.handshake.auth);
4743

48-
socket.on("message-with-ack", (...args) => {
49-
const ack = args.pop();
50-
ack(...args);
51-
});
44+
socket.on("message", (...args) => {
45+
socket.emit.apply(socket, ["message-back", ...args]);
5246
});
5347

54-
io.of("/custom").on("connection", (socket) => {
55-
socket.emit("auth", socket.handshake.auth);
48+
socket.on("message-with-ack", (...args) => {
49+
const ack = args.pop();
50+
ack(...args);
5651
});
52+
});
5753

58-
Bun.serve({
59-
port: 3001,
60-
...engine.handler(),
61-
});
54+
io.of("/custom").on("connection", (socket) => {
55+
socket.emit("auth", socket.handshake.auth);
6256
});
6357

58+
Bun.serve({
59+
port: 3001,
60+
...engine.handler(),
61+
});
62+
}
63+
64+
// imported from https://github.com/socketio/socket.io/tree/main/docs/socket.io-protocol/v5-test-suite
65+
describe("Socket.IO protocol", () => {
66+
beforeAll(() => setup());
67+
6468
describe("connect", () => {
6569
it("should allow connection to the main namespace", async () => {
6670
const socket = createWebSocket(

0 commit comments

Comments
 (0)