@cloudflare/pages-shared 0.3.0 → 0.3.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.
@@ -12,15 +12,12 @@ import {
12
12
  TemporaryRedirectResponse,
13
13
  } from "./responses";
14
14
  import { generateRulesMatcher, replacer } from "./rulesEngine";
15
- import { stringifyURLToRootRelativePathname } from "./url";
16
15
  import type {
17
16
  Metadata,
18
17
  MetadataHeadersEntries,
19
18
  MetadataHeadersRulesV2,
20
19
  MetadataHeadersV1,
21
20
  MetadataHeadersV2,
22
- MetadataStaticRedirectEntry,
23
- MetadataRedirectEntry,
24
21
  } from "./metadata";
25
22
 
26
23
  type BodyEncoding = "manual" | "automatic";
@@ -100,16 +97,7 @@ type FullHandlerContext<AssetEntry, ContentNegotiation, Asset> = {
100
97
  waitUntil: (promise: Promise<unknown>) => void;
101
98
  };
102
99
 
103
- export type HandlerContext<
104
- AssetEntry,
105
- ContentNegotiation extends { encoding: string | null } = {
106
- encoding: string | null;
107
- },
108
- Asset extends { body: ReadableStream | null; contentType: string } = {
109
- body: ReadableStream | null;
110
- contentType: string;
111
- }
112
- > =
100
+ export type HandlerContext<AssetEntry, ContentNegotiation, Asset> =
113
101
  | FullHandlerContext<AssetEntry, ContentNegotiation, Asset>
114
102
  | (Omit<
115
103
  FullHandlerContext<AssetEntry, ContentNegotiation, Asset>,
@@ -204,15 +192,28 @@ export async function generateHandler<
204
192
  staticRedirectsMatcher() || generateRedirectsMatcher()({ request })[0];
205
193
 
206
194
  if (match) {
207
- if (match.status === 200) {
208
- // A 200 redirect means that we are proxying to a different asset, for example,
209
- // a request with url /users/12345 could be pointed to /users/id.html. In order to
210
- // do this, we overwrite the pathname, and instead match for assets with that url,
211
- // and importantly, do not use the regular redirect handler - as the url visible to
212
- // the user does not change
213
- pathname = new URL(match.to, request.url).pathname;
214
- } else {
215
- return getResponseFromMatch(match, url);
195
+ const { status, to } = match;
196
+ const destination = new URL(to, request.url);
197
+ const location =
198
+ destination.origin === new URL(request.url).origin
199
+ ? `${destination.pathname}${destination.search || search}${
200
+ destination.hash
201
+ }`
202
+ : `${destination.href}${destination.search ? "" : search}${
203
+ destination.hash
204
+ }`;
205
+ switch (status) {
206
+ case 301:
207
+ return new MovedPermanentlyResponse(location);
208
+ case 303:
209
+ return new SeeOtherResponse(location);
210
+ case 307:
211
+ return new TemporaryRedirectResponse(location);
212
+ case 308:
213
+ return new PermanentRedirectResponse(location);
214
+ case 302:
215
+ default:
216
+ return new FoundResponse(location);
216
217
  }
217
218
  }
218
219
 
@@ -581,49 +582,6 @@ export async function generateHandler<
581
582
  }
582
583
  }
583
584
 
584
- /**
585
- * Given a redirect match and the request URL, returns the response
586
- * for the redirect. This function will convert the Location header
587
- * to be a root-relative path URL if the request origin and destination
588
- * origins are the same. This is so that if the project is served
589
- * on multiple domains, the redirects won't take the client off of their current domain.
590
- */
591
- export function getResponseFromMatch(
592
- {
593
- status,
594
- to,
595
- }: Pick<MetadataStaticRedirectEntry | MetadataRedirectEntry, "status" | "to">,
596
- requestUrl: URL
597
- ) {
598
- // Inherit origin from the request URL if not specified in _redirects
599
- const destination = new URL(to, requestUrl);
600
- // If _redirects doesn't specify a search, inherit from the request
601
- destination.search = destination.search || requestUrl.search;
602
-
603
- // If the redirect destination origin matches the incoming request origin
604
- // we stringify destination to be a root-relative path, e.g.:
605
- // https://example.com/foo/bar?baz=1 -> /foo/bar/?baz=1
606
- // This way, the project can more easily be hosted on multiple domains
607
- const location =
608
- destination.origin === requestUrl.origin
609
- ? stringifyURLToRootRelativePathname(destination)
610
- : destination.toString();
611
-
612
- switch (status) {
613
- case 301:
614
- return new MovedPermanentlyResponse(location);
615
- case 303:
616
- return new SeeOtherResponse(location);
617
- case 307:
618
- return new TemporaryRedirectResponse(location);
619
- case 308:
620
- return new PermanentRedirectResponse(location);
621
- case 302:
622
- default:
623
- return new FoundResponse(location);
624
- }
625
- }
626
-
627
585
  // Parses a list such as "deflate, gzip;q=1.0, *;q=0.5" into
628
586
  // {deflate: 1, gzip: 1, *: 0.5}
629
587
  export function parseQualityWeightedList(list = "") {
@@ -3,7 +3,7 @@ export const HEADERS_VERSION = 2;
3
3
  export const ANALYTICS_VERSION = 1;
4
4
  export const ROUTES_JSON_VERSION = 1;
5
5
 
6
- export const PERMITTED_STATUS_CODES = new Set([200, 301, 302, 303, 307, 308]);
6
+ export const PERMITTED_STATUS_CODES = new Set([301, 302, 303, 307, 308]);
7
7
  export const HEADER_SEPARATOR = ":";
8
8
  export const MAX_LINE_LENGTH = 2000;
9
9
  export const MAX_HEADER_RULES = 100;
@@ -5,7 +5,6 @@ import {
5
5
  SPLAT_REGEX,
6
6
  PLACEHOLDER_REGEX,
7
7
  } from "./constants";
8
- import type { MetadataStaticRedirects } from "../asset-server/metadata";
9
8
  import type {
10
9
  Metadata,
11
10
  MetadataRedirects,
@@ -68,17 +67,13 @@ function constructRedirects({
68
67
  return {};
69
68
  }
70
69
 
71
- const staticRedirects: MetadataStaticRedirects = {};
70
+ const staticRedirects: MetadataRedirects = {};
72
71
  const dynamicRedirects: MetadataRedirects = {};
73
72
  let canCreateStaticRule = true;
74
73
  for (const rule of redirects.rules) {
75
74
  if (!rule.from.match(SPLAT_REGEX) && !rule.from.match(PLACEHOLDER_REGEX)) {
76
75
  if (canCreateStaticRule) {
77
- staticRedirects[rule.from] = {
78
- status: rule.status,
79
- to: rule.to,
80
- lineNumber: rule.lineNumber,
81
- };
76
+ staticRedirects[rule.from] = { status: rule.status, to: rule.to };
82
77
  continue;
83
78
  } else {
84
79
  logger(
@@ -104,7 +104,7 @@ export function parseRedirects(input: string): ParsedRedirects {
104
104
  invalid.push({
105
105
  line,
106
106
  lineNumber: i + 1,
107
- message: `Valid status codes are 200, 301, 302 (default), 303, 307, or 308. Got ${str_status}.`,
107
+ message: `Valid status codes are 301, 302 (default), 303, 307, or 308. Got ${str_status}.`,
108
108
  });
109
109
  continue;
110
110
  }
@@ -119,22 +119,6 @@ export function parseRedirects(input: string): ParsedRedirects {
119
119
  }
120
120
  seen_paths.add(from);
121
121
 
122
- if (status === 200) {
123
- // Error can only be that it's not relative - given validateUrl is called above without onlyRelative:true,
124
- // so if it's present, we can error to the user that proxying only expects relative urls
125
- const [proxyTo, error] = validateUrl(to, true, true, true);
126
- if (error) {
127
- invalid.push({
128
- line,
129
- lineNumber: i + 1,
130
- message: `Proxy (200) redirects can only point to relative paths. Got ${to}`,
131
- });
132
- continue;
133
- }
134
- rules.push({ from, to: proxyTo as string, status, lineNumber: i + 1 });
135
- continue;
136
- }
137
-
138
122
  rules.push({ from, to, status, lineNumber: i + 1 });
139
123
  }
140
124
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/pages-shared",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cloudflare/workers-sdk.git",
@@ -1,8 +0,0 @@
1
- /**
2
- * Stringifies a URL instance to a root-relative path string
3
- * @param url The URL to stringify
4
- * @returns A root-relative path string
5
- */
6
- export function stringifyURLToRootRelativePathname(url: URL): string {
7
- return `${url.pathname}${url.search}${url.hash}`;
8
- }