Skip to content

Commit d7555e3

Browse files
committed
feat(reactive-rpc): 🎸 enable CORS
1 parent eed1662 commit d7555e3

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/reactive-rpc/__demos__/ws.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import {createCaller} from '../common/rpc/__tests__/sample-api';
44
import {RpcServer} from '../server/http1/RpcServer';
55

6-
RpcServer.startWithDefaults({
6+
const server = RpcServer.startWithDefaults({
77
port: 3000,
88
caller: createCaller(),
99
logger: console,
1010
});
11+
12+
console.log(server + '');

src/reactive-rpc/server/http1/RpcServer.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ export class RpcServer implements Printable {
2828
return server;
2929
};
3030

31-
public static readonly startWithDefaults = (opts: RpcServerStartOpts) => {
31+
public static readonly startWithDefaults = (opts: RpcServerStartOpts): RpcServer => {
3232
const port = opts.port ?? 8080;
3333
const logger = opts.logger ?? console;
3434
const server = http.createServer();
3535
const http1Server = new Http1Server({
3636
server,
3737
});
38-
const rpcServer = RpcServer.create({
38+
const rpcServer = new RpcServer({
3939
caller: opts.caller,
4040
http1: http1Server,
4141
logger,
@@ -47,6 +47,7 @@ export class RpcServer implements Printable {
4747
if (typeof host === 'object') host = (host as any).address;
4848
logger.log({msg: 'SERVER_STARTED', host, port});
4949
});
50+
return rpcServer;
5051
};
5152

5253
public readonly http1: Http1Server;
@@ -71,6 +72,24 @@ export class RpcServer implements Printable {
7172
this.http1.enableHttpPing();
7273
}
7374

75+
public enableCors(): void {
76+
this.http1.route({
77+
method: 'OPTIONS',
78+
path: '/{::\n}',
79+
handler: (ctx) => {
80+
const res = ctx.res;
81+
res.writeHead(200, 'OK', {
82+
'Access-Control-Allow-Origin': '*',
83+
'Access-Control-Allow-Credentials': 'true',
84+
// 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
85+
// 'Access-Control-Allow-Headers': 'Content-Type',
86+
// 'Access-Control-Max-Age': '86400',
87+
});
88+
res.end();
89+
},
90+
});
91+
}
92+
7493
public enableHttpRpc(path: string = '/rpc'): void {
7594
const batchProcessor = this.batchProcessor;
7695
const logger = this.opts.logger ?? console;
@@ -161,16 +180,15 @@ export class RpcServer implements Printable {
161180
}
162181

163182
public enableDefaults(): void {
164-
// this.enableCors();
183+
this.enableCors();
165184
this.enableHttpPing();
166185
this.enableHttpRpc();
167-
// this.enableWsRpc();
168-
// this.startRouting();
186+
this.enableWsRpc();
169187
}
170188

171189
// ---------------------------------------------------------------- Printable
172190

173191
public toString(tab: string = ''): string {
174-
return `${this.constructor.name}` + printTree(tab, [(tab) => `HTTP/1.1 ${this.http1.toString(tab)}`]);
192+
return `${this.constructor.name}` + printTree(tab, [(tab) => this.http1.toString(tab)]);
175193
}
176194
}

0 commit comments

Comments
 (0)