Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export const corsOptions = {
credentials: true,
};

if (corsOptions.origins.length > 1 && process.env.NODE_ENV === "production") {
console.warn(
"WARNING: socket.io only supports one cors origin, therefore only first origin will be registered.",
);
}

app.use(cors(corsOptions));
app.use(csrf(corsOptions));

Expand Down
4 changes: 2 additions & 2 deletions server/src/lib/cross-origin/share.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Config = {
export type Config = {
origins: string[]; // allowed origins
methods?: string[]; // Access-Control-Allow-Methods
credentials?: boolean; // Access-Control-Allow-Credentials
Expand Down Expand Up @@ -47,4 +47,4 @@ function assertValidConfig(config: Config) {
}
}

export { validateConfig, type Config };
export { validateConfig };
9 changes: 7 additions & 2 deletions server/src/lib/socket/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import type { Message, UserID } from "common/types";
import type { CorsOptions } from "cors";
import { type Socket, Server as SocketIOServer } from "socket.io";
import { getUserIdFromToken } from "../../firebase/auth/db";
import type { Config as CorsConfig } from "../cross-origin/share";

const users = new Map<UserID, Socket>();

export function initializeSocket(server: Server, corsOptions: CorsOptions) {
export function initializeSocket(server: Server, corsOptions: CorsConfig) {
const cors: CorsOptions = {
origin: corsOptions.origins[0],
...corsOptions,
};
const io = new SocketIOServer(server, {
cors: corsOptions,
cors,
connectionStateRecovery: {},
});

Expand Down
Loading