@fastly/expressly 1.0.0-alpha.8 → 1.0.0-alpha.9

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,7 +1,5 @@
1
1
  import { addCommonMethods } from "../common";
2
2
  import { CookieMap } from "./cookie-map";
3
- // Will extend Request correctly after https://github.com/fastly/js-compute-runtime/pull/116 is merged
4
- // See: https://github.com/fastly/js-compute-runtime/issues/113
5
3
  class ERequestBase extends Request {
6
4
  constructor(config, event) {
7
5
  super(event.request, {
@@ -0,0 +1,9 @@
1
+ /// <reference types="@fastly/js-compute" />
2
+ export declare class CookieMap extends Map {
3
+ private headers;
4
+ constructor(headers: Headers);
5
+ clear(): void;
6
+ set(key: string, value: string): this;
7
+ delete(key: string): boolean;
8
+ private serialize;
9
+ }
@@ -0,0 +1,37 @@
1
+ import cookie from "cookie";
2
+ export class CookieMap extends Map {
3
+ constructor(headers) {
4
+ super();
5
+ this.headers = headers;
6
+ if (Boolean(this.headers.get("Cookie"))) {
7
+ for (const [key, value] of Object.entries(cookie.parse(this.headers.get("Cookie")))) {
8
+ if (typeof value === "string") {
9
+ super.set(key, value);
10
+ }
11
+ }
12
+ }
13
+ }
14
+ clear() {
15
+ this.headers.delete("Cookie");
16
+ super.clear();
17
+ }
18
+ set(key, value) {
19
+ super.set(key, value);
20
+ this.serialize();
21
+ return this;
22
+ }
23
+ delete(key) {
24
+ const deleteResult = super.delete(key);
25
+ this.serialize();
26
+ return deleteResult;
27
+ }
28
+ serialize() {
29
+ if (this.size) {
30
+ const cookies = [];
31
+ for (const [key, value] of this.entries()) {
32
+ cookies.push(cookie.serialize(key, value));
33
+ }
34
+ this.headers.set("Cookie", cookies.join("; "));
35
+ }
36
+ }
37
+ }
@@ -0,0 +1,9 @@
1
+ /// <reference types="@fastly/js-compute" />
2
+ export declare class EHeaders extends Headers {
3
+ cookies: Map<string, string>;
4
+ constructor();
5
+ private setCookie;
6
+ set(name: string, value: string): void;
7
+ append(name: string, value: string): void;
8
+ delete(name: string): void;
9
+ }
@@ -0,0 +1,29 @@
1
+ export class EHeaders extends Headers {
2
+ constructor() {
3
+ super();
4
+ this.cookies = new Map();
5
+ }
6
+ setCookie(value) {
7
+ const [cookieName] = value.split("=");
8
+ this.cookies.set(cookieName, value);
9
+ }
10
+ set(name, value) {
11
+ if (/^Set-Cookie$/i.test(name)) {
12
+ this.cookies.clear();
13
+ this.setCookie(value);
14
+ }
15
+ super.set(name, value);
16
+ }
17
+ append(name, value) {
18
+ if (/^Set-Cookie$/i.test(name)) {
19
+ this.setCookie(value);
20
+ }
21
+ super.append(name, value);
22
+ }
23
+ delete(name) {
24
+ if (/^Set-Cookie$/i.test(name)) {
25
+ this.cookies.clear();
26
+ }
27
+ super.delete(name);
28
+ }
29
+ }
@@ -1,9 +1,10 @@
1
1
  /// <reference types="@fastly/js-compute" />
2
2
  import { SurrogateKeys } from "./surrogate-keys";
3
+ import { EHeaders } from "./headers";
3
4
  import { CookieOptions, EConfig } from "..";
4
5
  declare class EResponseBase {
5
6
  private config;
6
- headers: Headers;
7
+ headers: EHeaders;
7
8
  status: number;
8
9
  body: BodyInit;
9
10
  hasEnded: boolean;
@@ -2,17 +2,15 @@ import cookie from "cookie";
2
2
  import { addCommonMethods } from "../common";
3
3
  import { statusText } from "./status-codes";
4
4
  import { SurrogateKeys } from "./surrogate-keys";
5
- // TODO: extends Response
6
- // See: https://github.com/fastly/js-compute-runtime/issues/113
5
+ import { EHeaders } from "./headers";
7
6
  class EResponseBase {
8
7
  constructor(config) {
9
8
  this.config = config;
10
- this.headers = new Headers();
9
+ this.headers = new EHeaders();
11
10
  this.status = 0;
12
11
  this.body = null;
13
12
  this.hasEnded = false;
14
13
  this.surrogateKeys = new SurrogateKeys(this.headers);
15
- // super();
16
14
  }
17
15
  // Header helpers.
18
16
  vary(field) {
@@ -27,7 +25,7 @@ class EResponseBase {
27
25
  clearCookie(key, options = {}) {
28
26
  if (this.hasEnded)
29
27
  return;
30
- this.headers.append("Set-Cookie", cookie.serialize(key, "", { ...options, expires: "Thu, 01 Jan 1970 00:00:00 GMT" }));
28
+ this.cookie(key, "", { ...options, expires: "Thu, 01 Jan 1970 00:00:00 GMT" });
31
29
  }
32
30
  // Response lifecycle methods.
33
31
  send(response) {
@@ -154,13 +154,16 @@ export class Router {
154
154
  };
155
155
  }
156
156
  static serializeResponse(res) {
157
- // Default to 200 / 204 if no status was set by middleware / route handler.
158
- if (res.status === 0) {
159
- res.status = Boolean(res.body) ? 200 : 204;
160
- }
161
- return new Response(res.body, {
157
+ const response = new Response(res.body, {
162
158
  headers: res.headers,
163
- status: res.status,
159
+ // Default to 200 / 204 if no status was set by middleware / route handler.
160
+ status: res.status ? res.status : Boolean(res.body) ? 200 : 204,
164
161
  });
162
+ if (res.headers.cookies.size) {
163
+ // Loop cookies manually to work around this issue: https://github.com/fastly/js-compute-runtime/issues/47
164
+ response.headers.delete("Set-Cookie");
165
+ res.headers.cookies.forEach((c) => response.headers.append("Set-Cookie", c));
166
+ }
167
+ return response;
165
168
  }
166
169
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastly/expressly",
3
- "version": "1.0.0-alpha.8",
3
+ "version": "1.0.0-alpha.9",
4
4
  "description": "Express-style router for Fastly's Compute@Edge.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",