@cloudflare/pages-shared 0.3.1 → 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,7 +192,29 @@ export async function generateHandler<
204
192
  staticRedirectsMatcher() || generateRedirectsMatcher()({ request })[0];
205
193
 
206
194
  if (match) {
207
- 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);
217
+ }
208
218
  }
209
219
 
210
220
  if (!request.method.match(/^(get|head)$/i)) {
@@ -572,49 +582,6 @@ export async function generateHandler<
572
582
  }
573
583
  }
574
584
 
575
- /**
576
- * Given a redirect match and the request URL, returns the response
577
- * for the redirect. This function will convert the Location header
578
- * to be a root-relative path URL if the request origin and destination
579
- * origins are the same. This is so that if the project is served
580
- * on multiple domains, the redirects won't take the client off of their current domain.
581
- */
582
- export function getResponseFromMatch(
583
- {
584
- status,
585
- to,
586
- }: Pick<MetadataStaticRedirectEntry | MetadataRedirectEntry, "status" | "to">,
587
- requestUrl: URL
588
- ) {
589
- // Inherit origin from the request URL if not specified in _redirects
590
- const destination = new URL(to, requestUrl);
591
- // If _redirects doesn't specify a search, inherit from the request
592
- destination.search = destination.search || requestUrl.search;
593
-
594
- // If the redirect destination origin matches the incoming request origin
595
- // we stringify destination to be a root-relative path, e.g.:
596
- // https://example.com/foo/bar?baz=1 -> /foo/bar/?baz=1
597
- // This way, the project can more easily be hosted on multiple domains
598
- const location =
599
- destination.origin === requestUrl.origin
600
- ? stringifyURLToRootRelativePathname(destination)
601
- : destination.toString();
602
-
603
- switch (status) {
604
- case 301:
605
- return new MovedPermanentlyResponse(location);
606
- case 303:
607
- return new SeeOtherResponse(location);
608
- case 307:
609
- return new TemporaryRedirectResponse(location);
610
- case 308:
611
- return new PermanentRedirectResponse(location);
612
- case 302:
613
- default:
614
- return new FoundResponse(location);
615
- }
616
- }
617
-
618
585
  // Parses a list such as "deflate, gzip;q=1.0, *;q=0.5" into
619
586
  // {deflate: 1, gzip: 1, *: 0.5}
620
587
  export function parseQualityWeightedList(list = "") {
@@ -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(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/pages-shared",
3
- "version": "0.3.1",
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
- }