counterfact 0.21.3 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/package.json +1 -1
- package/src/server/koa-middleware.js +35 -13
- package/test/server/koa-middleware.test.js +112 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# counterfact
|
|
2
2
|
|
|
3
|
+
## 0.22.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 4dcfa06: add CORS headers to support local development (Thanks, [dethell-jh](https://github.com/dethell-jh)!)
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- dec64f4: Add CORS headers
|
|
12
|
+
|
|
3
13
|
## 0.21.3
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -2,6 +2,21 @@ import koaProxy from "koa-proxy";
|
|
|
2
2
|
|
|
3
3
|
const HTTP_STATUS_CODE_OK = 200;
|
|
4
4
|
|
|
5
|
+
function addCors(ctx, headers) {
|
|
6
|
+
// Always append CORS headers, reflecting back the headers requested if any
|
|
7
|
+
ctx.set("Access-Control-Allow-Origin", headers?.origin || "*");
|
|
8
|
+
ctx.set("Access-Control-Allow-Methods", "GET,HEAD,PUT,POST,DELETE,PATCH");
|
|
9
|
+
ctx.set(
|
|
10
|
+
"Access-Control-Allow-Headers",
|
|
11
|
+
headers?.["access-control-request-headers"]
|
|
12
|
+
);
|
|
13
|
+
ctx.set(
|
|
14
|
+
"Access-Control-Expose-Headers",
|
|
15
|
+
headers?.["access-control-request-headers"]
|
|
16
|
+
);
|
|
17
|
+
ctx.set("Access-Control-Allow-Credentials", "true");
|
|
18
|
+
}
|
|
19
|
+
|
|
5
20
|
export function koaMiddleware(dispatcher, options = {}, proxy = koaProxy) {
|
|
6
21
|
return async function middleware(ctx, next) {
|
|
7
22
|
const { method, path, headers, body, query } = ctx.request;
|
|
@@ -10,19 +25,26 @@ export function koaMiddleware(dispatcher, options = {}, proxy = koaProxy) {
|
|
|
10
25
|
return proxy({ host: options.proxyUrl })(ctx, next);
|
|
11
26
|
}
|
|
12
27
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
addCors(ctx, headers);
|
|
29
|
+
|
|
30
|
+
// If the method is OPTIONS then always return OK
|
|
31
|
+
if (method === "OPTIONS") {
|
|
32
|
+
ctx.status = HTTP_STATUS_CODE_OK;
|
|
33
|
+
} else {
|
|
34
|
+
const response = await dispatcher.request({
|
|
35
|
+
method,
|
|
36
|
+
path,
|
|
37
|
+
headers,
|
|
38
|
+
body,
|
|
39
|
+
query,
|
|
40
|
+
req: ctx.req,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/* eslint-disable require-atomic-updates */
|
|
44
|
+
ctx.body = response.body;
|
|
45
|
+
ctx.status = response.status ?? HTTP_STATUS_CODE_OK;
|
|
46
|
+
/* eslint-enable require-atomic-updates */
|
|
47
|
+
}
|
|
26
48
|
|
|
27
49
|
return undefined;
|
|
28
50
|
};
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// eslint-disable-next-line import/no-extraneous-dependencies, n/no-extraneous-import, @typescript-eslint/no-shadow, no-shadow
|
|
2
|
+
import { jest } from "@jest/globals";
|
|
3
|
+
|
|
1
4
|
import { Registry } from "../../src/server/registry.js";
|
|
2
5
|
import { Dispatcher } from "../../src/server/dispatcher.js";
|
|
3
6
|
import { koaMiddleware } from "../../src/server/koa-middleware.js";
|
|
@@ -24,11 +27,17 @@ describe("koa middleware", () => {
|
|
|
24
27
|
const dispatcher = new Dispatcher(registry, new ContextRegistry());
|
|
25
28
|
const middleware = koaMiddleware(dispatcher);
|
|
26
29
|
const ctx = {
|
|
27
|
-
request: {
|
|
30
|
+
request: {
|
|
31
|
+
path: "/hello",
|
|
32
|
+
method: "POST",
|
|
33
|
+
body: { name: "Homer" },
|
|
34
|
+
},
|
|
28
35
|
|
|
29
36
|
req: {
|
|
30
37
|
path: "/hello",
|
|
31
38
|
},
|
|
39
|
+
|
|
40
|
+
set: jest.fn(),
|
|
32
41
|
};
|
|
33
42
|
|
|
34
43
|
await middleware(ctx);
|
|
@@ -52,6 +61,8 @@ describe("koa middleware", () => {
|
|
|
52
61
|
const middleware = koaMiddleware(dispatcher);
|
|
53
62
|
const ctx = {
|
|
54
63
|
request: { path: "/not-modified", method: "GET" },
|
|
64
|
+
// eslint-disable-next-line no-empty-function
|
|
65
|
+
set: () => {},
|
|
55
66
|
};
|
|
56
67
|
|
|
57
68
|
await middleware(ctx);
|
|
@@ -82,4 +93,104 @@ describe("koa middleware", () => {
|
|
|
82
93
|
|
|
83
94
|
expect(ctx.mockProxyHost).toBe("https://example.com");
|
|
84
95
|
});
|
|
96
|
+
|
|
97
|
+
it("adds default CORS headers if none are requested", async () => {
|
|
98
|
+
const registry = new Registry();
|
|
99
|
+
|
|
100
|
+
registry.add("/hello", {
|
|
101
|
+
POST({ body }) {
|
|
102
|
+
return {
|
|
103
|
+
body: `Hello, ${body.name}!`,
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const dispatcher = new Dispatcher(registry, new ContextRegistry());
|
|
109
|
+
const middleware = koaMiddleware(dispatcher);
|
|
110
|
+
const ctx = {
|
|
111
|
+
request: {
|
|
112
|
+
path: "/hello",
|
|
113
|
+
method: "POST",
|
|
114
|
+
body: { name: "Homer" },
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
req: {
|
|
118
|
+
path: "/hello",
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
set: jest.fn(),
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
await middleware(ctx);
|
|
125
|
+
|
|
126
|
+
expect(ctx.status).toBe(200);
|
|
127
|
+
expect(ctx.body).toBe("Hello, Homer!");
|
|
128
|
+
expect(ctx.set).toHaveBeenCalledWith("Access-Control-Allow-Origin", "*");
|
|
129
|
+
expect(ctx.set).toHaveBeenCalledWith(
|
|
130
|
+
"Access-Control-Allow-Methods",
|
|
131
|
+
"GET,HEAD,PUT,POST,DELETE,PATCH"
|
|
132
|
+
);
|
|
133
|
+
expect(ctx.set).toHaveBeenCalledWith(
|
|
134
|
+
"Access-Control-Allow-Headers",
|
|
135
|
+
undefined
|
|
136
|
+
);
|
|
137
|
+
expect(ctx.set).toHaveBeenCalledWith(
|
|
138
|
+
"Access-Control-Expose-Headers",
|
|
139
|
+
undefined
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("reflects desired CORS headers if specific headers are requested", async () => {
|
|
144
|
+
const registry = new Registry();
|
|
145
|
+
|
|
146
|
+
registry.add("/hello", {
|
|
147
|
+
POST({ body }) {
|
|
148
|
+
return {
|
|
149
|
+
body: `Hello, ${body.name}!`,
|
|
150
|
+
};
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const dispatcher = new Dispatcher(registry, new ContextRegistry());
|
|
155
|
+
const middleware = koaMiddleware(dispatcher);
|
|
156
|
+
const ctx = {
|
|
157
|
+
request: {
|
|
158
|
+
path: "/hello",
|
|
159
|
+
method: "POST",
|
|
160
|
+
body: { name: "Homer" },
|
|
161
|
+
|
|
162
|
+
headers: {
|
|
163
|
+
origin: "https://my.local.app:3000",
|
|
164
|
+
"access-control-request-headers": "X-My-Header,X-Another-Header",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
req: {
|
|
169
|
+
path: "/hello",
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
set: jest.fn(),
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
await middleware(ctx);
|
|
176
|
+
|
|
177
|
+
expect(ctx.status).toBe(200);
|
|
178
|
+
expect(ctx.body).toBe("Hello, Homer!");
|
|
179
|
+
expect(ctx.set).toHaveBeenCalledWith(
|
|
180
|
+
"Access-Control-Allow-Origin",
|
|
181
|
+
"https://my.local.app:3000"
|
|
182
|
+
);
|
|
183
|
+
expect(ctx.set).toHaveBeenCalledWith(
|
|
184
|
+
"Access-Control-Allow-Methods",
|
|
185
|
+
"GET,HEAD,PUT,POST,DELETE,PATCH"
|
|
186
|
+
);
|
|
187
|
+
expect(ctx.set).toHaveBeenCalledWith(
|
|
188
|
+
"Access-Control-Allow-Headers",
|
|
189
|
+
"X-My-Header,X-Another-Header"
|
|
190
|
+
);
|
|
191
|
+
expect(ctx.set).toHaveBeenCalledWith(
|
|
192
|
+
"Access-Control-Expose-Headers",
|
|
193
|
+
"X-My-Header,X-Another-Header"
|
|
194
|
+
);
|
|
195
|
+
});
|
|
85
196
|
});
|