@fastly/expressly 1.0.0-alpha.9 → 1.0.0-beta.2

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.
@@ -1,6 +1,6 @@
1
1
  import { EReq } from "./request";
2
2
  import { ERes } from "./response";
3
- export declare type ErrorMiddlewareCallback = (err: Error, req: EReq, res: ERes) => Promise<any>;
3
+ export type ErrorMiddlewareCallback = (err: Error, req: EReq, res: ERes) => Promise<any>;
4
4
  export declare class ErrorMiddleware {
5
5
  private matchFn;
6
6
  private callback;
@@ -30,5 +30,5 @@ export declare const ERequest: {
30
30
  }, value?: string | string[]): void;
31
31
  };
32
32
  } & typeof ERequestBase;
33
- export declare type EReq = InstanceType<typeof ERequest>;
33
+ export type EReq = InstanceType<typeof ERequest>;
34
34
  export {};
@@ -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 = {};
@@ -1,6 +1,6 @@
1
1
  import { EReq } from "./request";
2
2
  import { ERes } from "./response";
3
- export declare type RequestHandlerCallback = (req: EReq, res: ERes) => Promise<any>;
3
+ export type RequestHandlerCallback = (req: EReq, res: ERes) => Promise<any>;
4
4
  export declare class RequestHandler {
5
5
  private matchFn;
6
6
  private callback;
@@ -33,5 +33,5 @@ export declare const EResponse: {
33
33
  }, value?: string | string[]): void;
34
34
  };
35
35
  } & typeof EResponseBase;
36
- export declare type ERes = InstanceType<typeof EResponse>;
36
+ export type ERes = InstanceType<typeof EResponse>;
37
37
  export {};
@@ -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) {
@@ -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-alpha.9",
3
+ "version": "1.0.0-beta.2",
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": "nodemon --exec \"tsc\""
17
+ "dev": "tsc --watch"
18
18
  },
19
19
  "keywords": [
20
20
  "fastly",
@@ -24,26 +24,25 @@
24
24
  "router"
25
25
  ],
26
26
  "engines": {
27
- "node": "^16"
27
+ "node": ">=16.0.0"
28
28
  },
29
29
  "author": "oss@fastly.com",
30
30
  "license": "MIT",
31
31
  "devDependencies": {
32
32
  "@types/node": "^17.0.40",
33
- "auto": "^10.37.1",
34
- "husky": "^8.0.1",
35
- "nodemon": "^2.0.18",
36
- "prettier": "^2.7.1",
33
+ "auto": "^10.37.6",
34
+ "husky": "^8.0.2",
35
+ "prettier": "^2.8.1",
37
36
  "pretty-quick": "^3.1.3",
38
- "ts-loader": "^9.3.1",
39
- "typescript": "^4.7.4",
40
- "webpack": "^5.73.0",
41
- "webpack-cli": "^4.10.0"
37
+ "ts-loader": "^9.4.2",
38
+ "typescript": "^4.9.4",
39
+ "webpack": "^5.75.0",
40
+ "webpack-cli": "^5.0.1"
42
41
  },
43
42
  "dependencies": {
44
- "@fastly/js-compute": "^0.3.0",
43
+ "@fastly/js-compute": "^0.7.0",
45
44
  "cookie": "^0.5.0",
46
- "core-js": "^3.23.3",
45
+ "core-js": "^3.26.1",
47
46
  "path-to-regexp": "^6.2.1"
48
47
  },
49
48
  "auto": {
package/tsconfig.json CHANGED
@@ -16,5 +16,8 @@
16
16
  "forceConsistentCasingInFileNames": true,
17
17
  "declaration": true
18
18
  },
19
- "exclude": ["dist/", ".dora", "docs/"]
19
+ "exclude": ["node_modules"],
20
+ "include": [
21
+ "src/**/*.ts"
22
+ ]
20
23
  }