Skip to content

Commit 0824a66

Browse files
fix: store handshake headers on the socket object
1 parent 23cf9a6 commit 0824a66

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-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: 10 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,22 @@ export class Socket extends EventEmitter<
5051
private pingIntervalTimer?: Timer;
5152
private pingTimeoutTimer?: Timer;
5253

53-
constructor(id: string, opts: ServerOptions, transport: Transport) {
54+
constructor(id: string, opts: ServerOptions, transport: Transport, request: Request) {
5455
super();
5556

5657
this.id = id;
5758
this.opts = opts;
5859

5960
this.transport = transport;
6061
this.bindTransport(transport);
62+
63+
// we store the headers of the handshake request, so that they are available in the `socket.handshake` attribute in
64+
// the `socket.io` library
65+
this.request = {
66+
headers: Object.fromEntries(request.headers.entries()),
67+
connection: {}
68+
}
69+
6170
this.onOpen();
6271
}
6372

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)