forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket-io.adapter.ts
More file actions
62 lines (54 loc) · 1.95 KB
/
Copy pathsocket-io.adapter.ts
File metadata and controls
62 lines (54 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { IoAdapter } from '@nestjs/platform-socket.io';
import { INestApplicationContext } from '@nestjs/common';
type OriginValue = string | string[] | boolean | RegExp | undefined;
type OriginCallback = (err: Error | null, allow?: boolean) => void;
type OriginChecker = (requestOrigin: string, callback: OriginCallback) => void;
function toOriginChecker(origin: OriginValue | OriginChecker): OriginChecker {
if (typeof origin === 'function') {
return origin as OriginChecker;
}
return (requestOrigin: string, callback: OriginCallback) => {
// Non-browser clients (curl, server-to-server) send no Origin header at all —
// CORS doesn't apply to them; let them through.
if (!requestOrigin) {
return callback(null, true);
}
let allowed: boolean;
if (origin === true || origin === undefined) {
allowed = true;
} else if (origin === false) {
allowed = false;
} else if (origin instanceof RegExp) {
allowed = origin.test(requestOrigin);
} else if (Array.isArray(origin)) {
allowed = origin.includes(requestOrigin);
} else {
allowed = origin === requestOrigin;
}
return allowed
? callback(null, true)
: callback(new Error(`Origin ${requestOrigin} not allowed by CORS`));
};
}
export class SocketIoAdapter extends IoAdapter {
constructor(
app: INestApplicationContext,
private readonly options: Record<string, any>,
) {
super(app);
}
override createIOServer(port: number, options: Record<string, any> = {}) {
// this.options may itself be the cors block ({ origin, methods, credentials })
// or may already nest a `cors` key — normalize either shape.
const corsSource = this.options?.cors ?? this.options ?? options.cors ?? {};
const { origin, ...restCors } = corsSource;
return super.createIOServer(port, {
...options,
...this.options,
cors: {
...restCors,
origin: toOriginChecker(origin),
},
});
}
}