@w0s/report-same-referrer 4.0.0 → 5.0.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 +50 -62
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -5
- package/dist/index.js.map +1 -1
- package/dist/reportSameReferrer.d.ts +34 -0
- package/dist/reportSameReferrer.d.ts.map +1 -0
- package/dist/reportSameReferrer.js +92 -0
- package/dist/reportSameReferrer.js.map +1 -0
- package/package.json +6 -7
- package/dist/ReportSameReferrer.d.ts +0 -30
- package/dist/ReportSameReferrer.d.ts.map +0 -1
- package/dist/ReportSameReferrer.js +0 -119
- package/dist/ReportSameReferrer.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Send referrer error information to endpoints
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@w0s/report-same-referrer)
|
|
4
|
-
[](https://github.com/SaekiTominaga/js-library-browser/actions/workflows/package-report-same-referrer.yml)
|
|
5
5
|
|
|
6
6
|
If there are referrers from same site, that information will be sent to the endpoint as an error.
|
|
7
7
|
|
|
@@ -9,7 +9,7 @@ As a practical use case, this script put this script in error pages like 403, 40
|
|
|
9
9
|
|
|
10
10
|
## Demo
|
|
11
11
|
|
|
12
|
-
- [Demo page](https://saekitominaga.github.io/
|
|
12
|
+
- [Demo page](https://saekitominaga.github.io/js-library-browser/packages/report-same-referrer/demo/)
|
|
13
13
|
|
|
14
14
|
## Examples
|
|
15
15
|
|
|
@@ -24,23 +24,28 @@ As a practical use case, this script put this script in error pages like 403, 40
|
|
|
24
24
|
<script type="module">
|
|
25
25
|
import reportSameReferrer from '@w0s/report-same-referrer';
|
|
26
26
|
|
|
27
|
-
await reportSameReferrer(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
await reportSameReferrer({
|
|
28
|
+
fetch: {
|
|
29
|
+
endpoint: 'https://report.example.com/referrer',
|
|
30
|
+
param: {
|
|
31
|
+
documentURL: 'documentURL',
|
|
32
|
+
referrer: 'referrer',
|
|
33
|
+
},
|
|
34
|
+
contentType: 'application/json',
|
|
35
|
+
headers: {
|
|
36
|
+
'X-Requested-With': 'foo',
|
|
37
|
+
},
|
|
31
38
|
},
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
validate: {
|
|
40
|
+
referrer: {
|
|
41
|
+
comparePart: 'origin',
|
|
42
|
+
sames: [
|
|
43
|
+
'https://www1.example.com',
|
|
44
|
+
'https://www2.example.com',
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
ua: { denys: [/Googlebot\/2.1;/v] },
|
|
35
48
|
},
|
|
36
|
-
condition: 'origin',
|
|
37
|
-
same: [
|
|
38
|
-
'https://www1.example.com',
|
|
39
|
-
'https://www2.example.com',
|
|
40
|
-
],
|
|
41
|
-
denyUAs: [
|
|
42
|
-
/Googlebot\/2.1;/,
|
|
43
|
-
],
|
|
44
49
|
});
|
|
45
50
|
</script>
|
|
46
51
|
```
|
|
@@ -48,55 +53,38 @@ As a practical use case, this script put this script in error pages like 403, 40
|
|
|
48
53
|
## Default function
|
|
49
54
|
|
|
50
55
|
```TypeScript
|
|
51
|
-
async (
|
|
56
|
+
async (options: Readonly<Option>): Promise<Response | undefined>
|
|
52
57
|
```
|
|
53
58
|
|
|
54
|
-
### Parameters
|
|
55
|
-
|
|
56
|
-
<dl>
|
|
57
|
-
<dt><code>endpoint</code> [required]</dt>
|
|
58
|
-
<dd>URL of the endpoint</dd>
|
|
59
|
-
<dt><code>options</code> [required]</dt>
|
|
60
|
-
<dd>Information such as transmission conditions</dd>
|
|
61
|
-
</dl>
|
|
62
|
-
|
|
63
59
|
### Option
|
|
64
60
|
|
|
65
61
|
```TypeScript
|
|
66
|
-
interface Option {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
referrer: string;
|
|
70
|
-
};
|
|
71
|
-
fetchContentType?: 'application/x-www-form-urlencoded' | 'application/json';
|
|
72
|
-
fetchHeaders?: HeadersInit;
|
|
73
|
-
condition?: 'origin' | 'host' | 'hostname';
|
|
74
|
-
same?: string[];
|
|
75
|
-
denyUAs?: RegExp[];
|
|
76
|
-
allowUAs?: RegExp[];
|
|
62
|
+
export interface Option {
|
|
63
|
+
fetch: Readonly<FetchOption>;
|
|
64
|
+
validate?: Readonly<ValidateOption>;
|
|
77
65
|
}
|
|
78
|
-
```
|
|
79
66
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
<
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
<
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
<
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
67
|
+
export interface FetchOption {
|
|
68
|
+
endpoint: string | URL; // URL of the endpoint
|
|
69
|
+
param: Readonly<{
|
|
70
|
+
documentURL: string; // Field name when sending the URL of the document to an endpoint
|
|
71
|
+
referrer: string; // Field name when sending `document.referrer` to an endpoint
|
|
72
|
+
}>;
|
|
73
|
+
contentType?: 'application/x-www-form-urlencoded' | 'application/json'; // `Content-Type` header to be set in `fetch()` request
|
|
74
|
+
headers?: HeadersInit; // Header to add to the `fetch()` request <https://fetch.spec.whatwg.org/#typedefdef-headersinit>
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface ValidateOption {
|
|
78
|
+
/* Referrer */
|
|
79
|
+
referrer?: Readonly<{
|
|
80
|
+
comparePart?: 'origin' | 'host' | 'hostname'; // Which parts of the referrer to check (default: `origin`)
|
|
81
|
+
sames?: readonly string[]; // Domain information treated as the same site
|
|
82
|
+
}>;
|
|
83
|
+
|
|
84
|
+
/* User agent string */
|
|
85
|
+
ua?: Readonly<{
|
|
86
|
+
denys?: readonly RegExp[]; // If matches this regular expression, do not send report
|
|
87
|
+
allows?: readonly RegExp[]; // If matches this regular expression, send report
|
|
88
|
+
}>;
|
|
89
|
+
}
|
|
90
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
export default _default;
|
|
1
|
+
import reportSameReferrer from './reportSameReferrer.ts';
|
|
2
|
+
export default reportSameReferrer;
|
|
4
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,MAAM,yBAAyB,CAAC;AAEzD,eAAe,kBAAkB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
export default
|
|
3
|
-
const reportSameReferrer = new ReportSameReferrer(endpoint, options);
|
|
4
|
-
await reportSameReferrer.report();
|
|
5
|
-
};
|
|
1
|
+
import reportSameReferrer from "./reportSameReferrer.js";
|
|
2
|
+
export default reportSameReferrer;
|
|
6
3
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,MAAM,yBAAyB,CAAC;AAEzD,eAAe,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
type URLPart = 'origin' | 'host' | 'hostname';
|
|
2
|
+
export interface Option {
|
|
3
|
+
fetch: Readonly<FetchOption>;
|
|
4
|
+
validate?: Readonly<ValidateOption>;
|
|
5
|
+
}
|
|
6
|
+
export interface FetchOption {
|
|
7
|
+
endpoint: string | URL;
|
|
8
|
+
param: Readonly<{
|
|
9
|
+
documentURL: string;
|
|
10
|
+
referrer: string;
|
|
11
|
+
}>;
|
|
12
|
+
contentType?: 'application/x-www-form-urlencoded' | 'application/json';
|
|
13
|
+
headers?: HeadersInit;
|
|
14
|
+
}
|
|
15
|
+
export interface ValidateOption {
|
|
16
|
+
referrer?: Readonly<{
|
|
17
|
+
comparePart?: URLPart;
|
|
18
|
+
sames?: readonly string[];
|
|
19
|
+
}>;
|
|
20
|
+
ua?: Readonly<{
|
|
21
|
+
denys?: readonly RegExp[];
|
|
22
|
+
allows?: readonly RegExp[];
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Send referrer error information to endpoints
|
|
27
|
+
*
|
|
28
|
+
* @param options - Options
|
|
29
|
+
*
|
|
30
|
+
* @returns Fetch result
|
|
31
|
+
*/
|
|
32
|
+
declare const _default: (options: Readonly<Option>) => Promise<Response | undefined>;
|
|
33
|
+
export default _default;
|
|
34
|
+
//# sourceMappingURL=reportSameReferrer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reportSameReferrer.d.ts","sourceRoot":"","sources":["../src/reportSameReferrer.ts"],"names":[],"mappings":"AAAA,KAAK,OAAO,GAAG,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAC;AAE9C,MAAM,WAAW,MAAM;IACtB,KAAK,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;IAC7B,QAAQ,CAAC,EAAE,QAAQ,CAAC,cAAc,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,WAAW;IAC3B,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;IACvB,KAAK,EAAE,QAAQ,CAAC;QACf,WAAW,EAAE,MAAM,CAAC;QACpB,QAAQ,EAAE,MAAM,CAAC;KACjB,CAAC,CAAC;IACH,WAAW,CAAC,EAAE,mCAAmC,GAAG,kBAAkB,CAAC;IACvE,OAAO,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,cAAc;IAE9B,QAAQ,CAAC,EAAE,QAAQ,CAAC;QACnB,WAAW,CAAC,EAAE,OAAO,CAAC;QACtB,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;KAC1B,CAAC,CAAC;IAGH,EAAE,CAAC,EAAE,QAAQ,CAAC;QACb,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;QAC1B,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;KAC3B,CAAC,CAAC;CACH;AA+DD;;;;;;GAMG;yBACmB,SAAS,QAAQ,CAAC,MAAM,CAAC,KAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;AAA/E,wBAmCE"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation
|
|
3
|
+
*
|
|
4
|
+
* @param options - Options
|
|
5
|
+
*
|
|
6
|
+
* @returns If validation passes, it returns true
|
|
7
|
+
*/
|
|
8
|
+
const validate = (options) => {
|
|
9
|
+
/* Referrer */
|
|
10
|
+
const { referrer } = document;
|
|
11
|
+
if (referrer === '') {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
const referrerUrl = new URL(referrer);
|
|
15
|
+
const getUrlPart = (part) => {
|
|
16
|
+
switch (part) {
|
|
17
|
+
case undefined:
|
|
18
|
+
case 'origin': {
|
|
19
|
+
return { referrer: referrerUrl.origin, location: location.origin };
|
|
20
|
+
}
|
|
21
|
+
case 'host': {
|
|
22
|
+
return { referrer: referrerUrl.host, location: location.host };
|
|
23
|
+
}
|
|
24
|
+
case 'hostname': {
|
|
25
|
+
return { referrer: referrerUrl.hostname, location: location.hostname };
|
|
26
|
+
}
|
|
27
|
+
default:
|
|
28
|
+
throw new Error('An invalid value was specified for the argument `condition`.');
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const { referrer: referrerPart, location: locationPart } = getUrlPart(options?.referrer?.comparePart);
|
|
32
|
+
if (options?.referrer?.sames !== undefined) {
|
|
33
|
+
if (!options.referrer.sames.includes(referrerPart)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else if (referrerPart !== locationPart) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
/* User agent */
|
|
41
|
+
if (options?.ua !== undefined) {
|
|
42
|
+
const ua = navigator.userAgent;
|
|
43
|
+
const { denys, allows } = options.ua;
|
|
44
|
+
if (denys?.some((deny) => deny.test(ua))) {
|
|
45
|
+
console.info('No referrer error report will be sent because the user agent match the deny list.');
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
if (allows !== undefined && !allows.some((allow) => allow.test(ua))) {
|
|
49
|
+
console.info('No referrer error report will be sent because the user agent does not match the allow list.');
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Send referrer error information to endpoints
|
|
57
|
+
*
|
|
58
|
+
* @param options - Options
|
|
59
|
+
*
|
|
60
|
+
* @returns Fetch result
|
|
61
|
+
*/
|
|
62
|
+
export default async (options) => {
|
|
63
|
+
if (!validate(options.validate)) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
const { endpoint, param, contentType, headers: headersInit } = options.fetch;
|
|
67
|
+
const headers = new Headers(headersInit);
|
|
68
|
+
if (contentType !== undefined) {
|
|
69
|
+
headers.set('Content-Type', contentType);
|
|
70
|
+
}
|
|
71
|
+
const bodyObject = {
|
|
72
|
+
[param.documentURL]: location.toString(),
|
|
73
|
+
[param.referrer]: document.referrer,
|
|
74
|
+
};
|
|
75
|
+
let body;
|
|
76
|
+
if (contentType === 'application/json') {
|
|
77
|
+
body = JSON.stringify(bodyObject);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
body = new URLSearchParams(bodyObject);
|
|
81
|
+
}
|
|
82
|
+
const response = await fetch(endpoint, {
|
|
83
|
+
method: 'POST',
|
|
84
|
+
headers: headers,
|
|
85
|
+
body: body,
|
|
86
|
+
});
|
|
87
|
+
if (!response.ok) {
|
|
88
|
+
throw new Error(`\`${response.url}\` is ${String(response.status)} ${response.statusText}`);
|
|
89
|
+
}
|
|
90
|
+
return response;
|
|
91
|
+
};
|
|
92
|
+
//# sourceMappingURL=reportSameReferrer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reportSameReferrer.js","sourceRoot":"","sources":["../src/reportSameReferrer.ts"],"names":[],"mappings":"AA+BA;;;;;;GAMG;AACH,MAAM,QAAQ,GAAG,CAAC,OAAkC,EAAW,EAAE;IAChE,cAAc;IACd,MAAM,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAAC;IAC9B,IAAI,QAAQ,KAAK,EAAE,EAAE,CAAC;QACrB,OAAO,KAAK,CAAC;IACd,CAAC;IAED,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;IAEtC,MAAM,UAAU,GAAG,CAAC,IAAyB,EAA0C,EAAE;QACxF,QAAQ,IAAI,EAAE,CAAC;YACd,KAAK,SAAS,CAAC;YACf,KAAK,QAAQ,CAAC,CAAC,CAAC;gBACf,OAAO,EAAE,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,QAAQ,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC;YACpE,CAAC;YACD,KAAK,MAAM,CAAC,CAAC,CAAC;gBACb,OAAO,EAAE,QAAQ,EAAE,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC;YAChE,CAAC;YACD,KAAK,UAAU,CAAC,CAAC,CAAC;gBACjB,OAAO,EAAE,QAAQ,EAAE,WAAW,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,CAAC,QAAQ,EAAE,CA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 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@w0s/report-same-referrer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Send referrer error information to endpoints",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Report"
|
|
7
7
|
],
|
|
8
|
-
"homepage": "https://github.com/SaekiTominaga/
|
|
8
|
+
"homepage": "https://github.com/SaekiTominaga/js-library-browser#readme",
|
|
9
9
|
"bugs": {
|
|
10
|
-
"url": "https://github.com/SaekiTominaga/
|
|
10
|
+
"url": "https://github.com/SaekiTominaga/js-library-browser/issues"
|
|
11
11
|
},
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"author": "Saeki Tominaga",
|
|
@@ -27,14 +27,13 @@
|
|
|
27
27
|
"types": "dist/index.d.ts",
|
|
28
28
|
"repository": {
|
|
29
29
|
"type": "git",
|
|
30
|
-
"url": "git+https://github.com/SaekiTominaga/
|
|
30
|
+
"url": "git+https://github.com/SaekiTominaga/js-library-browser.git"
|
|
31
31
|
},
|
|
32
32
|
"scripts": {
|
|
33
|
-
"
|
|
33
|
+
"dev": "conc \"http-server -o demo -c-1\" \"tsc -w\"",
|
|
34
34
|
"prebuild": "rimraf dist/* -g",
|
|
35
35
|
"build": "tsc",
|
|
36
|
-
"
|
|
37
|
-
"lint": "eslint src/**/*.ts __tests__/**/*.js",
|
|
36
|
+
"lint": "eslint src/**/*.ts",
|
|
38
37
|
"pretest": "npm run build",
|
|
39
38
|
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest dist/.+\\.test\\.js -c ../../jest.config.mjs --roots packages/report-same-referrer"
|
|
40
39
|
},
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
interface FetchParam {
|
|
2
|
-
documentURL: string;
|
|
3
|
-
referrer: string;
|
|
4
|
-
}
|
|
5
|
-
export interface Option {
|
|
6
|
-
fetchParam: FetchParam;
|
|
7
|
-
fetchContentType?: 'application/x-www-form-urlencoded' | 'application/json';
|
|
8
|
-
fetchHeaders?: HeadersInit;
|
|
9
|
-
condition?: 'origin' | 'host' | 'hostname';
|
|
10
|
-
same?: string[];
|
|
11
|
-
denyUAs?: RegExp[];
|
|
12
|
-
allowUAs?: RegExp[];
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Send referrer error information to endpoints
|
|
16
|
-
*/
|
|
17
|
-
export default class {
|
|
18
|
-
#private;
|
|
19
|
-
/**
|
|
20
|
-
* @param endpoint - URL of the endpoint
|
|
21
|
-
* @param options - Information such as transmission conditions
|
|
22
|
-
*/
|
|
23
|
-
constructor(endpoint: string, options: Readonly<Option>);
|
|
24
|
-
/**
|
|
25
|
-
* Referrer check & report
|
|
26
|
-
*/
|
|
27
|
-
report(): Promise<void>;
|
|
28
|
-
}
|
|
29
|
-
export {};
|
|
30
|
-
//# sourceMappingURL=ReportSameReferrer.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ReportSameReferrer.d.ts","sourceRoot":"","sources":["../src/ReportSameReferrer.ts"],"names":[],"mappings":"AAAA,UAAU,UAAU;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,MAAM;IACtB,UAAU,EAAE,UAAU,CAAC;IACvB,gBAAgB,CAAC,EAAE,mCAAmC,GAAG,kBAAkB,CAAC;IAC5E,YAAY,CAAC,EAAE,WAAW,CAAC;IAC3B,SAAS,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAC;IAC3C,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,CAAC,OAAO;;IAKb;;;OAGG;gBACS,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC;IASvD;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CA+G7B"}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Send referrer error information to endpoints
|
|
3
|
-
*/
|
|
4
|
-
export default class {
|
|
5
|
-
#endpoint; // URL of the endpoint
|
|
6
|
-
#options; // Information such as transmission conditions
|
|
7
|
-
/**
|
|
8
|
-
* @param endpoint - URL of the endpoint
|
|
9
|
-
* @param options - Information such as transmission conditions
|
|
10
|
-
*/
|
|
11
|
-
constructor(endpoint, options) {
|
|
12
|
-
this.#endpoint = endpoint;
|
|
13
|
-
this.#options = options;
|
|
14
|
-
if (options.condition === undefined) {
|
|
15
|
-
this.#options.condition = 'origin';
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Referrer check & report
|
|
20
|
-
*/
|
|
21
|
-
async report() {
|
|
22
|
-
const { referrer } = document;
|
|
23
|
-
if (referrer === '') {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
if (!this.#checkUserAgent()) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
const referrerUrl = new URL(referrer);
|
|
30
|
-
switch (this.#options.condition) {
|
|
31
|
-
case 'origin': {
|
|
32
|
-
if (!this.#checkReferrer(referrerUrl.origin, location.origin)) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
case 'host': {
|
|
38
|
-
if (!this.#checkReferrer(referrerUrl.host, location.host)) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
break;
|
|
42
|
-
}
|
|
43
|
-
case 'hostname': {
|
|
44
|
-
if (!this.#checkReferrer(referrerUrl.hostname, location.hostname)) {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
default:
|
|
50
|
-
throw new Error('An invalid value was specified for the argument `condition`.');
|
|
51
|
-
}
|
|
52
|
-
await this.#fetch(referrerUrl);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* ユーザーエージェントがレポートを行う対象かどうかチェックする
|
|
56
|
-
*
|
|
57
|
-
* @returns 対象なら true
|
|
58
|
-
*/
|
|
59
|
-
#checkUserAgent() {
|
|
60
|
-
const ua = navigator.userAgent;
|
|
61
|
-
const { denyUAs, allowUAs } = this.#options;
|
|
62
|
-
if (denyUAs?.some((denyUA) => denyUA.test(ua))) {
|
|
63
|
-
console.info('No referrer error report will be sent because the user agent match the deny list.');
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
if (allowUAs !== undefined && !allowUAs.some((allowUA) => allowUA.test(ua))) {
|
|
67
|
-
console.info('No referrer error report will be sent because the user agent does not match the allow list.');
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* リファラーがレポートを行う対象かどうかチェックする
|
|
74
|
-
*
|
|
75
|
-
* @param referrerPart - リファラーの一部分
|
|
76
|
-
* @param locationPart - Location の一部分
|
|
77
|
-
*
|
|
78
|
-
* @returns 対象なら true
|
|
79
|
-
*/
|
|
80
|
-
#checkReferrer(referrerPart, locationPart) {
|
|
81
|
-
const { same } = this.#options;
|
|
82
|
-
if (same?.includes(referrerPart)) {
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
return referrerPart === locationPart;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* レポートを送信
|
|
89
|
-
*
|
|
90
|
-
* @param referrerUrl - リファラーのURL
|
|
91
|
-
*/
|
|
92
|
-
async #fetch(referrerUrl) {
|
|
93
|
-
const { fetchParam, fetchContentType, fetchHeaders } = this.#options;
|
|
94
|
-
const headers = new Headers(fetchHeaders);
|
|
95
|
-
if (fetchContentType !== undefined) {
|
|
96
|
-
headers.set('Content-Type', fetchContentType);
|
|
97
|
-
}
|
|
98
|
-
const bodyObject = {
|
|
99
|
-
[fetchParam.documentURL]: location.toString(),
|
|
100
|
-
[fetchParam.referrer]: referrerUrl.toString(),
|
|
101
|
-
};
|
|
102
|
-
let body;
|
|
103
|
-
if (fetchContentType === 'application/json') {
|
|
104
|
-
body = JSON.stringify(bodyObject);
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
body = new URLSearchParams(bodyObject);
|
|
108
|
-
}
|
|
109
|
-
const response = await fetch(this.#endpoint, {
|
|
110
|
-
method: 'POST',
|
|
111
|
-
headers: headers,
|
|
112
|
-
body: body,
|
|
113
|
-
});
|
|
114
|
-
if (!response.ok) {
|
|
115
|
-
throw new Error(`"${response.url}" is ${String(response.status)} ${response.statusText}`);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
//# sourceMappingURL=ReportSameReferrer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ReportSameReferrer.js","sourceRoot":"","sources":["../src/ReportSameReferrer.ts"],"names":[],"mappings":"AAeA;;GAEG;AACH,MAAM,CAAC,OAAO;IACJ,SAAS,CAAS,CAAC,sBAAsB;IAEzC,QAAQ,CAAS,CAAC,8CAA8C;IAEzE;;;OAGG;IACH,YAAY,QAAgB,EAAE,OAAyB;QACtD,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAE1B,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;YACrC,IAAI,CAAC,QAAQ,CAAC,SAAS,GAAG,QAAQ,CAAC;QACpC,CAAC;IACF,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,MAAM;QACX,MAAM,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAAC;QAC9B,IAAI,QAAQ,KAAK,EAAE,EAAE,CAAC;YACrB,OAAO;QACR,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,EAAE,CAAC;YAC7B,OAAO;QACR,CAAC;QAED,MAAM,WAAW,GAAG,IAAI,GAAG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