@redirhub/cloudflare-redirects 0.1.1 → 0.2.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.
- package/README.md +5 -2
- package/dist/core/redirects.d.ts +10 -0
- package/dist/core/redirects.js +3 -2
- package/dist/version.d.ts +2 -2
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -99,10 +99,12 @@ add your own logic, pass `origin: (request, env, ctx) => …`.
|
|
|
99
99
|
| Option | |
|
|
100
100
|
| --- | --- |
|
|
101
101
|
| `settings(env)` | The rules address and the key from the Worker's bindings. Empty address: off, every request goes to the origin. |
|
|
102
|
+
| `report` | `false`, or `(request) => boolean`: whether to report, over the key. Default: whenever there is a key. |
|
|
102
103
|
| `origin(request, env, ctx)` | Where requests no rule answers go; default `fetch(request)`. |
|
|
103
104
|
| `report404` | `rules` (default): 404s of hostnames with **Report 404 pages** on; `all`: every hostname the Worker serves. |
|
|
104
105
|
| `hostname(request)` | The hostname to look up in the rules; `null` leaves the request alone (straight to the origin, nothing reported). Default the `Host` header. |
|
|
105
|
-
| `path(request)` | The path
|
|
106
|
+
| `path(request)` | The path to look up in the rules, percent-encoded; `null` leaves the request alone. Default the request URL's path. |
|
|
107
|
+
| `location(location, request)` | The `Location` to send, from the one the rule gives; also the reported destination. |
|
|
106
108
|
| `clientIp(request)` | The visitor's address in reports; default `CF-Connecting-IP`. `null` writes `-`. |
|
|
107
109
|
| `userAgent(request)` | The User-Agent in reports; default the `User-Agent` header. `null` writes `-`. |
|
|
108
110
|
| `anonymizeIp` | Report IPv4 with the last byte zeroed, IPv6 cut to /48. |
|
|
@@ -127,7 +129,8 @@ export default createWorker<Env>({
|
|
|
127
129
|
const { pathname } = new URL(request.url)
|
|
128
130
|
return pathname.startsWith('/api/') ? null : pathname
|
|
129
131
|
},
|
|
130
|
-
|
|
132
|
+
// Testing on workers.dev redirects, but is not reported.
|
|
133
|
+
report: (request) => !new URL(request.url).hostname.endsWith('.workers.dev'),
|
|
131
134
|
anonymizeIp: true,
|
|
132
135
|
})
|
|
133
136
|
```
|
package/dist/core/redirects.d.ts
CHANGED
|
@@ -13,6 +13,11 @@ export interface RedirectsOptions {
|
|
|
13
13
|
rulesUrl: string | null | undefined;
|
|
14
14
|
/** The integration key. Without it redirects work and nothing is reported. */
|
|
15
15
|
key?: string | null;
|
|
16
|
+
/**
|
|
17
|
+
* Whether to report, over the key: `false` reports nothing, a function decides per request
|
|
18
|
+
* (e.g. not for preview deployments). Default: report whenever there is a key.
|
|
19
|
+
*/
|
|
20
|
+
report?: boolean | ((request: Request) => boolean);
|
|
16
21
|
/**
|
|
17
22
|
* Which 404 answers of the site are reported (where the site's answer is seen, `afterSite`):
|
|
18
23
|
* `rules` (default) for hostnames whose `@host` line has `report404`, `all` for every hostname.
|
|
@@ -32,6 +37,11 @@ export interface RedirectsOptions {
|
|
|
32
37
|
* your code sees it, e.g. a proxy that strips a prefix.
|
|
33
38
|
*/
|
|
34
39
|
path?: (request: Request) => string | null;
|
|
40
|
+
/**
|
|
41
|
+
* The `Location` to send, from the one the rule gives (e.g. a same-site target in the visitor's
|
|
42
|
+
* language). It is also what the report names as the destination. Default: as the rule gives it.
|
|
43
|
+
*/
|
|
44
|
+
location?: (location: string, request: Request) => string;
|
|
35
45
|
/**
|
|
36
46
|
* The visitor's address in reports: only a value the platform sets, never a header a client can
|
|
37
47
|
* forge. Null writes `-`.
|
package/dist/core/redirects.js
CHANGED
|
@@ -43,7 +43,7 @@ export function createRedirects(options) {
|
|
|
43
43
|
};
|
|
44
44
|
const keepAliveOf = (context) => (context ? (work) => context.waitUntil(work) : undefined);
|
|
45
45
|
const report = (request, url, host, status, size, keepAlive, redirect) => {
|
|
46
|
-
if (!reporter) {
|
|
46
|
+
if (!reporter || options.report === false || (typeof options.report === 'function' && !options.report(request))) {
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
49
|
const ip = options.clientIp?.(request) ?? null;
|
|
@@ -61,7 +61,8 @@ export function createRedirects(options) {
|
|
|
61
61
|
}, now()), keepAlive);
|
|
62
62
|
};
|
|
63
63
|
const redirect = (request, url, rules, host, rule, keepAlive) => {
|
|
64
|
-
const
|
|
64
|
+
const target = buildLocation(rule, url);
|
|
65
|
+
const location = options.location ? options.location(target, request) : target;
|
|
65
66
|
const headers = new Headers({ Location: byteString(location) });
|
|
66
67
|
if (rules.poweredBy) {
|
|
67
68
|
headers.set('X-Powered-By', rules.poweredBy);
|
package/dist/version.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** This package: names its log lines and the User-Agent of rules downloads and reports. */
|
|
2
2
|
export declare const PACKAGE = "cloudflare-redirects";
|
|
3
3
|
/** Equal to package.json's version (a test checks it). */
|
|
4
|
-
export declare const VERSION = "0.
|
|
5
|
-
export declare const USER_AGENT = "cloudflare-redirects/0.
|
|
4
|
+
export declare const VERSION = "0.2.0";
|
|
5
|
+
export declare const USER_AGENT = "cloudflare-redirects/0.2.0";
|
package/dist/version.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** This package: names its log lines and the User-Agent of rules downloads and reports. */
|
|
2
2
|
export const PACKAGE = 'cloudflare-redirects';
|
|
3
3
|
/** Equal to package.json's version (a test checks it). */
|
|
4
|
-
export const VERSION = '0.
|
|
4
|
+
export const VERSION = '0.2.0';
|
|
5
5
|
export const USER_AGENT = `${PACKAGE}/${VERSION}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@redirhub/cloudflare-redirects",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Bulk redirects and 404 monitoring for Cloudflare Workers from a remote rules file: thousands of 301/302 redirects, short links and website migrations in front of your origin, live within a minute without a redeploy. RedirHub Integration Mode.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|