@netlify/redirects 4.0.4 → 4.1.0

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.
Files changed (2) hide show
  1. package/dist/main.js +21 -33
  2. package/package.json +3 -3
package/dist/main.js CHANGED
@@ -5,7 +5,7 @@ import process from "process";
5
5
  import path from "path";
6
6
  import { toMultiValueHeaders } from "@netlify/dev-utils";
7
7
  import { parseCookie } from "cookie";
8
- import redirector from "netlify-redirector";
8
+ import { createMatcher } from "@netlify/redirect-matcher";
9
9
 
10
10
  // src/lib/parser.ts
11
11
  import { parseAllRedirects } from "@netlify/redirect-parser";
@@ -78,17 +78,15 @@ var createRewriter = async function({
78
78
  }
79
79
  const getMatcher = async () => {
80
80
  if (matcher) return matcher;
81
- if (redirects.length !== 0) {
82
- return matcher = await redirector.parseJSON(JSON.stringify(redirects), {
83
- jwtSecret,
84
- jwtRoleClaim
85
- });
81
+ if (redirects.length === 0) {
82
+ return { match: () => null };
86
83
  }
87
- return {
88
- match() {
89
- return null;
90
- }
91
- };
84
+ matcher = await createMatcher(redirects, { jwtSecret, jwtRoleClaim });
85
+ if (matcher.parseErrors.length !== 0) {
86
+ console.log(`Redirects matcher errors:
87
+ ${matcher.parseErrors.map(({ message }) => message).join("\n\n")}`);
88
+ }
89
+ return matcher;
92
90
  };
93
91
  return async function rewriter(req) {
94
92
  const matcherFunc = await getMatcher();
@@ -99,23 +97,14 @@ var createRewriter = async function({
99
97
  "x-country": cookieValues.nf_country || geoCountry || "us",
100
98
  ...toMultiValueHeaders(req.headers)
101
99
  };
102
- const matchReq = {
100
+ return matcherFunc.match({
103
101
  scheme: reqUrl.protocol.replace(/:.*$/, ""),
104
102
  host: reqUrl.hostname,
105
103
  path: decodeURIComponent(reqUrl.pathname),
106
104
  query: reqUrl.search.slice(1),
107
105
  headers,
108
- cookieValues,
109
- getHeader: (name) => {
110
- const val = headers[name.toLowerCase()];
111
- if (Array.isArray(val)) {
112
- return val[0];
113
- }
114
- return val || "";
115
- },
116
- getCookie: (key) => cookieValues[key] || ""
117
- };
118
- return matcherFunc.match(matchReq);
106
+ cookies: cookieValues
107
+ });
119
108
  };
120
109
  };
121
110
 
@@ -172,7 +161,7 @@ var RedirectsHandler = class {
172
161
  if (!rule) {
173
162
  return;
174
163
  }
175
- if (rule.force404) {
164
+ if (rule.type === "forcedNotFound") {
176
165
  return {
177
166
  external: false,
178
167
  force: true,
@@ -180,24 +169,22 @@ var RedirectsHandler = class {
180
169
  hiddenProxy: false,
181
170
  redirect: false,
182
171
  statusCode: 404,
183
- target: new URL(""),
172
+ target: new URL(request.url),
184
173
  targetRelative: ""
185
174
  };
186
175
  }
187
176
  const requestURL = new URL(request.url);
188
- const headers = {
189
- ...rule.force404 ? {} : rule.proxyHeaders
190
- };
177
+ const headers = { ...rule.proxyHeaders };
191
178
  const hiddenProxy = Object.entries(headers).some(
192
179
  ([key, val]) => key.toLowerCase() === "x-nf-hidden-proxy" && val === "true"
193
180
  );
194
181
  const target = new URL(rule.to, request.url);
195
182
  const match = {
196
- external: "to" in rule && /^https?:\/\//.exec(rule.to) != null,
183
+ external: /^https?:\/\//.exec(rule.to) != null,
197
184
  force: rule.force,
198
185
  headers,
199
186
  hiddenProxy,
200
- redirect: "status" in rule && rule.status != null && rule.status >= 300 && rule.status <= 400,
187
+ redirect: rule.status >= 300 && rule.status <= 400,
201
188
  statusCode: rule.status,
202
189
  target,
203
190
  targetRelative: `${rule.to}${requestURL.search}${requestURL.hash}`
@@ -207,8 +194,9 @@ var RedirectsHandler = class {
207
194
  target.searchParams.append(key, val);
208
195
  });
209
196
  }
210
- if (rule.signingSecret) {
211
- const signingSecretVar = process.env[rule.signingSecret];
197
+ const signingSecretName = rule.signer?.jwtSecret;
198
+ if (signingSecretName) {
199
+ const signingSecretVar = process.env[signingSecretName];
212
200
  if (signingSecretVar) {
213
201
  match.headers["x-nf-sign"] = signRedirect({
214
202
  deployContext: "dev",
@@ -217,7 +205,7 @@ var RedirectsHandler = class {
217
205
  siteURL: this.siteURL
218
206
  });
219
207
  } else {
220
- match.error = new Error(`Could not sign redirect because environment variable ${rule.signingSecret} is not set`);
208
+ match.error = new Error(`Could not sign redirect because environment variable ${signingSecretName} is not set`);
221
209
  }
222
210
  }
223
211
  return match;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/redirects",
3
- "version": "4.0.4",
3
+ "version": "4.1.0",
4
4
  "description": "TypeScript implementation of Netlify's rewrites and redirects engine",
5
5
  "type": "module",
6
6
  "engines": {
@@ -43,9 +43,9 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "@netlify/dev-utils": "6.0.3",
46
+ "@netlify/redirect-matcher": "^0.4.2",
46
47
  "@netlify/redirect-parser": "^16.1.1",
47
48
  "cookie": "^2.0.0",
48
- "jsonwebtoken": "9.0.3",
49
- "netlify-redirector": "^0.5.0"
49
+ "jsonwebtoken": "9.0.3"
50
50
  }
51
51
  }