@@ -49,13 +49,6 @@ const rvs = new NodeCache({
49
49
useClones : false ,
50
50
} ) ;
51
51
52
- app . options ( "/" , cors ( {
53
- origin : "*" ,
54
- methods : [ "GET" , "POST" , "PUT" , "DELETE" , "OPTIONS" ] ,
55
- allowedHeaders : [ "X-Requested-With" , "Content-Type" , "Authorization" ] , // https://spec.matrix.org/v1.10/client-server-api/#web-browser-clients
56
- exposedHeaders : [ "ETag" ] ,
57
- } ) ) ;
58
-
59
52
function notFound ( res : express . Response ) : express . Response {
60
53
return res . status ( 404 ) . json ( { "errcode" : "M_NOT_FOUND" , "error" : "Rendezvous not found" } ) ;
61
54
}
@@ -74,7 +67,16 @@ function withContentType(req: express.Request, res: express.Response) {
74
67
return fn ( contentType ) ;
75
68
} ;
76
69
}
77
- app . post ( "/" , ( req , res ) => {
70
+
71
+ const postCors = cors ( {
72
+ origin : "*" ,
73
+ methods : [ "GET" , "POST" , "PUT" , "DELETE" , "OPTIONS" ] ,
74
+ allowedHeaders : [ "X-Requested-With" , "Content-Type" , "Authorization" ] , // https://spec.matrix.org/v1.10/client-server-api/#web-browser-clients
75
+ exposedHeaders : [ "ETag" ] ,
76
+ } ) ;
77
+
78
+ app . options ( "/" , postCors ) ;
79
+ app . post ( "/" , postCors , ( req , res ) => {
78
80
withContentType ( req , res ) ( ( contentType ) => {
79
81
80
82
let id : string | undefined ;
@@ -92,14 +94,15 @@ app.post("/", (req, res) => {
92
94
} ) ;
93
95
} ) ;
94
96
95
- app . options ( "/:id" , cors ( {
97
+ const sessionCors = cors ( {
96
98
origin : "*" ,
97
99
methods : [ "GET" , "PUT" , "DELETE" , "OPTIONS" ] ,
98
100
allowedHeaders : [ "If-Match" , "If-None-Match" ] ,
99
101
exposedHeaders : [ "ETag" ] ,
100
- } ) ) ;
102
+ } ) ;
101
103
102
- app . put ( "/:id" , ( req , res ) => {
104
+ app . options ( "/:id" , sessionCors ) ;
105
+ app . put ( "/:id" , sessionCors , ( req , res ) => {
103
106
withContentType ( req , res ) ( ( contentType ) => {
104
107
const { id } = req . params ;
105
108
const rv = rvs . get < Rendezvous > ( id ) ;
@@ -128,7 +131,7 @@ app.put("/:id", (req, res) => {
128
131
} ) ;
129
132
} ) ;
130
133
131
- app . get ( "/:id" , ( req , res ) => {
134
+ app . get ( "/:id" , sessionCors , ( req , res ) => {
132
135
const { id } = req . params ;
133
136
const rv = rvs . get < Rendezvous > ( id ) ;
134
137
@@ -150,7 +153,7 @@ app.get("/:id", (req, res) => {
150
153
return rv . sendData ( res ) ;
151
154
} ) ;
152
155
153
- app . delete ( "/:id" , ( req , res ) => {
156
+ app . delete ( "/:id" , sessionCors , ( req , res ) => {
154
157
const { id } = req . params ;
155
158
if ( ! rvs . has ( id ) ) {
156
159
return notFound ( res ) ;
0 commit comments