@cloudflare/pages-shared 0.3.2 → 0.3.3

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.
@@ -204,16 +204,26 @@ export async function generateHandler<
204
204
  }`;
205
205
  switch (status) {
206
206
  case 301:
207
- return new MovedPermanentlyResponse(location);
207
+ return new MovedPermanentlyResponse(location, undefined, {
208
+ preventLeadingDoubleSlash: false,
209
+ });
208
210
  case 303:
209
- return new SeeOtherResponse(location);
211
+ return new SeeOtherResponse(location, undefined, {
212
+ preventLeadingDoubleSlash: false,
213
+ });
210
214
  case 307:
211
- return new TemporaryRedirectResponse(location);
215
+ return new TemporaryRedirectResponse(location, undefined, {
216
+ preventLeadingDoubleSlash: false,
217
+ });
212
218
  case 308:
213
- return new PermanentRedirectResponse(location);
219
+ return new PermanentRedirectResponse(location, undefined, {
220
+ preventLeadingDoubleSlash: false,
221
+ });
214
222
  case 302:
215
223
  default:
216
- return new FoundResponse(location);
224
+ return new FoundResponse(location, undefined, {
225
+ preventLeadingDoubleSlash: false,
226
+ });
217
227
  }
218
228
  }
219
229
 
@@ -10,6 +10,10 @@ function mergeHeaders(base: HeadersInit, extra: HeadersInit) {
10
10
  });
11
11
  }
12
12
 
13
+ export function stripLeadingDoubleSlashes(location: string) {
14
+ return location.replace(/^(\/|%2F|%2f)+(.*)/, "/$2");
15
+ }
16
+
13
17
  export class OkResponse extends Response {
14
18
  constructor(...[body, init]: ConstructorParameters<typeof Response>) {
15
19
  super(body, {
@@ -23,8 +27,16 @@ export class OkResponse extends Response {
23
27
  export class MovedPermanentlyResponse extends Response {
24
28
  constructor(
25
29
  location: string,
26
- init?: ConstructorParameters<typeof Response>[1]
30
+ init?: ConstructorParameters<typeof Response>[1],
31
+ {
32
+ preventLeadingDoubleSlash = true,
33
+ }: { preventLeadingDoubleSlash: boolean } = {
34
+ preventLeadingDoubleSlash: true,
35
+ }
27
36
  ) {
37
+ location = preventLeadingDoubleSlash
38
+ ? stripLeadingDoubleSlashes(location)
39
+ : location;
28
40
  super(`Redirecting to ${location}`, {
29
41
  ...init,
30
42
  status: 301,
@@ -39,8 +51,16 @@ export class MovedPermanentlyResponse extends Response {
39
51
  export class FoundResponse extends Response {
40
52
  constructor(
41
53
  location: string,
42
- init?: ConstructorParameters<typeof Response>[1]
54
+ init?: ConstructorParameters<typeof Response>[1],
55
+ {
56
+ preventLeadingDoubleSlash = true,
57
+ }: { preventLeadingDoubleSlash: boolean } = {
58
+ preventLeadingDoubleSlash: true,
59
+ }
43
60
  ) {
61
+ location = preventLeadingDoubleSlash
62
+ ? stripLeadingDoubleSlashes(location)
63
+ : location;
44
64
  super(`Redirecting to ${location}`, {
45
65
  ...init,
46
66
  status: 302,
@@ -64,8 +84,16 @@ export class NotModifiedResponse extends Response {
64
84
  export class PermanentRedirectResponse extends Response {
65
85
  constructor(
66
86
  location: string,
67
- init?: ConstructorParameters<typeof Response>[1]
87
+ init?: ConstructorParameters<typeof Response>[1],
88
+ {
89
+ preventLeadingDoubleSlash = true,
90
+ }: { preventLeadingDoubleSlash: boolean } = {
91
+ preventLeadingDoubleSlash: true,
92
+ }
68
93
  ) {
94
+ location = preventLeadingDoubleSlash
95
+ ? stripLeadingDoubleSlashes(location)
96
+ : location;
69
97
  super(undefined, {
70
98
  ...init,
71
99
  status: 308,
@@ -126,8 +154,16 @@ export class InternalServerErrorResponse extends Response {
126
154
  export class SeeOtherResponse extends Response {
127
155
  constructor(
128
156
  location: string,
129
- init?: ConstructorParameters<typeof Response>[1]
157
+ init?: ConstructorParameters<typeof Response>[1],
158
+ {
159
+ preventLeadingDoubleSlash = true,
160
+ }: { preventLeadingDoubleSlash: boolean } = {
161
+ preventLeadingDoubleSlash: true,
162
+ }
130
163
  ) {
164
+ location = preventLeadingDoubleSlash
165
+ ? stripLeadingDoubleSlashes(location)
166
+ : location;
131
167
  super(`Redirecting to ${location}`, {
132
168
  ...init,
133
169
  status: 303,
@@ -140,8 +176,16 @@ export class SeeOtherResponse extends Response {
140
176
  export class TemporaryRedirectResponse extends Response {
141
177
  constructor(
142
178
  location: string,
143
- init?: ConstructorParameters<typeof Response>[1]
179
+ init?: ConstructorParameters<typeof Response>[1],
180
+ {
181
+ preventLeadingDoubleSlash = true,
182
+ }: { preventLeadingDoubleSlash: boolean } = {
183
+ preventLeadingDoubleSlash: true,
184
+ }
144
185
  ) {
186
+ location = preventLeadingDoubleSlash
187
+ ? stripLeadingDoubleSlashes(location)
188
+ : location;
145
189
  super(`Redirecting to ${location}`, {
146
190
  ...init,
147
191
  status: 307,
@@ -5,6 +5,7 @@ import {
5
5
  SPLAT_REGEX,
6
6
  PLACEHOLDER_REGEX,
7
7
  } from "./constants";
8
+ import type { MetadataStaticRedirects } from "../asset-server/metadata";
8
9
  import type {
9
10
  Metadata,
10
11
  MetadataRedirects,
@@ -67,13 +68,17 @@ function constructRedirects({
67
68
  return {};
68
69
  }
69
70
 
70
- const staticRedirects: MetadataRedirects = {};
71
+ const staticRedirects: MetadataStaticRedirects = {};
71
72
  const dynamicRedirects: MetadataRedirects = {};
72
73
  let canCreateStaticRule = true;
73
74
  for (const rule of redirects.rules) {
74
75
  if (!rule.from.match(SPLAT_REGEX) && !rule.from.match(PLACEHOLDER_REGEX)) {
75
76
  if (canCreateStaticRule) {
76
- staticRedirects[rule.from] = { status: rule.status, to: rule.to };
77
+ staticRedirects[rule.from] = {
78
+ status: rule.status,
79
+ to: rule.to,
80
+ lineNumber: rule.lineNumber,
81
+ };
77
82
  continue;
78
83
  } else {
79
84
  logger(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/pages-shared",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cloudflare/workers-sdk.git",