@fastly/expressly 1.0.0-alpha.9 → 1.0.0-beta.1
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/dist/lib/routing/request/index.js +1 -5
- package/dist/lib/routing/response/index.js +4 -1
- package/dist/lib/routing/router.js +36 -1
- package/package.json +8 -9
- package/tsconfig.json +4 -1
- package/yarn-error.log +2484 -0
- package/dist/lib/routing/response/cookie-map.d.ts +0 -9
- package/dist/lib/routing/response/cookie-map.js +0 -37
|
@@ -2,11 +2,7 @@ import { addCommonMethods } from "../common";
|
|
|
2
2
|
import { CookieMap } from "./cookie-map";
|
|
3
3
|
class ERequestBase extends Request {
|
|
4
4
|
constructor(config, event) {
|
|
5
|
-
super(event.request
|
|
6
|
-
headers: event.request.headers,
|
|
7
|
-
method: event.request.method,
|
|
8
|
-
body: event.request.body,
|
|
9
|
-
});
|
|
5
|
+
super(event.request);
|
|
10
6
|
this.config = config;
|
|
11
7
|
this.event = event;
|
|
12
8
|
this.params = {};
|
|
@@ -25,7 +25,7 @@ class EResponseBase {
|
|
|
25
25
|
clearCookie(key, options = {}) {
|
|
26
26
|
if (this.hasEnded)
|
|
27
27
|
return;
|
|
28
|
-
this.cookie(key, "", { ...options, expires: "Thu, 01 Jan 1970 00:00:00 GMT" });
|
|
28
|
+
this.cookie(key, "", { ...options, expires: new Date("Thu, 01 Jan 1970 00:00:00 GMT") });
|
|
29
29
|
}
|
|
30
30
|
// Response lifecycle methods.
|
|
31
31
|
send(response) {
|
|
@@ -60,6 +60,9 @@ class EResponseBase {
|
|
|
60
60
|
end(body) {
|
|
61
61
|
if (this.hasEnded)
|
|
62
62
|
return;
|
|
63
|
+
// if(typeof body !== "string") {
|
|
64
|
+
// body = body.toString()
|
|
65
|
+
// }
|
|
63
66
|
this.send(body);
|
|
64
67
|
}
|
|
65
68
|
// Send a HTTP status code response and end the response process.
|
|
@@ -16,6 +16,38 @@ const defaultErrorHandler = (auto405) => async (err, req, res) => {
|
|
|
16
16
|
console.error(err);
|
|
17
17
|
res.withStatus(500).json({ error: err.message });
|
|
18
18
|
};
|
|
19
|
+
/**
|
|
20
|
+
* Handles preflight requests from trusted origins configured by the user when initializing a router.
|
|
21
|
+
* Note that the wildcard value "*" will fail if the request is sent with credentials (see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin#directives)
|
|
22
|
+
* @param autoCorsPreflight the object containing CORS preflight option of trustedOrigins, an array of trusted origins.
|
|
23
|
+
* @returns 200 if the preflight request succeeds, 403 if it fails
|
|
24
|
+
*/
|
|
25
|
+
const preflightHandler = (autoCorsPreflight) => async (req, res) => {
|
|
26
|
+
if (autoCorsPreflight.trustedOrigins.length === 0) {
|
|
27
|
+
return res.sendStatus(403);
|
|
28
|
+
}
|
|
29
|
+
let originHeaderValue = null;
|
|
30
|
+
if (autoCorsPreflight.trustedOrigins.length === 1 && autoCorsPreflight.trustedOrigins[0] === "*") {
|
|
31
|
+
originHeaderValue = "*";
|
|
32
|
+
}
|
|
33
|
+
else if (req.headers.has("origin")) {
|
|
34
|
+
const origin = req.headers.get("origin").toLowerCase();
|
|
35
|
+
if (autoCorsPreflight.trustedOrigins.some((trustedOrigin) => trustedOrigin.toLowerCase() === origin)) {
|
|
36
|
+
originHeaderValue = origin;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (!originHeaderValue) {
|
|
40
|
+
return res.sendStatus(403);
|
|
41
|
+
}
|
|
42
|
+
if (req.headers.has("access-control-request-method")) {
|
|
43
|
+
res.headers.set("access-control-allow-methods", req.headers.get("access-control-request-method"));
|
|
44
|
+
}
|
|
45
|
+
if (req.headers.has("access-control-request-headers")) {
|
|
46
|
+
res.headers.set("access-control-allow-headers", req.headers.get("access-control-request-headers"));
|
|
47
|
+
}
|
|
48
|
+
res.headers.set("access-control-allow-origin", originHeaderValue);
|
|
49
|
+
return res.sendStatus(200);
|
|
50
|
+
};
|
|
19
51
|
export class Router {
|
|
20
52
|
constructor(config) {
|
|
21
53
|
this.requestHandlers = [];
|
|
@@ -24,12 +56,15 @@ export class Router {
|
|
|
24
56
|
parseCookie: true,
|
|
25
57
|
auto405: true,
|
|
26
58
|
extractRequestParameters: true,
|
|
27
|
-
autoContentType: false
|
|
59
|
+
autoContentType: false,
|
|
28
60
|
};
|
|
29
61
|
this.config = {
|
|
30
62
|
...this.config,
|
|
31
63
|
...config
|
|
32
64
|
};
|
|
65
|
+
if (this.config.autoCorsPreflight) {
|
|
66
|
+
this.options("*", preflightHandler(this.config.autoCorsPreflight));
|
|
67
|
+
}
|
|
33
68
|
}
|
|
34
69
|
listen() {
|
|
35
70
|
addEventListener("fetch", (event) => event.respondWith(this.handler(event)));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastly/expressly",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
4
|
"description": "Express-style router for Fastly's Compute@Edge.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"scripts": {
|
|
15
15
|
"build": "tsc",
|
|
16
16
|
"prepublish": "tsc",
|
|
17
|
-
"dev": "
|
|
17
|
+
"dev": "tsc --watch"
|
|
18
18
|
},
|
|
19
19
|
"keywords": [
|
|
20
20
|
"fastly",
|
|
@@ -30,20 +30,19 @@
|
|
|
30
30
|
"license": "MIT",
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/node": "^17.0.40",
|
|
33
|
-
"auto": "^10.37.
|
|
33
|
+
"auto": "^10.37.6",
|
|
34
34
|
"husky": "^8.0.1",
|
|
35
|
-
"nodemon": "^2.0.18",
|
|
36
35
|
"prettier": "^2.7.1",
|
|
37
36
|
"pretty-quick": "^3.1.3",
|
|
38
|
-
"ts-loader": "^9.
|
|
39
|
-
"typescript": "^4.
|
|
40
|
-
"webpack": "^5.
|
|
37
|
+
"ts-loader": "^9.4.1",
|
|
38
|
+
"typescript": "^4.8.4",
|
|
39
|
+
"webpack": "^5.74.0",
|
|
41
40
|
"webpack-cli": "^4.10.0"
|
|
42
41
|
},
|
|
43
42
|
"dependencies": {
|
|
44
|
-
"@fastly/js-compute": "^0.
|
|
43
|
+
"@fastly/js-compute": "^0.5.4",
|
|
45
44
|
"cookie": "^0.5.0",
|
|
46
|
-
"core-js": "^3.
|
|
45
|
+
"core-js": "^3.25.5",
|
|
47
46
|
"path-to-regexp": "^6.2.1"
|
|
48
47
|
},
|
|
49
48
|
"auto": {
|