@braidlabs/gateway 0.7.1 → 0.8.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 +28 -0
- package/package.json +1 -1
- package/src/cookie-scope.js +4 -1
- package/src/cookie-scope.js.map +1 -1
- package/src/gateway.d.ts +33 -1
- package/src/gateway.js +147 -35
- package/src/gateway.js.map +1 -1
- package/src/index.d.ts +2 -0
- package/src/index.js +1 -0
- package/src/index.js.map +1 -1
- package/src/node.d.ts +2 -0
- package/src/node.js +8 -1
- package/src/node.js.map +1 -1
- package/src/preferences-file.d.ts +15 -0
- package/src/preferences-file.js +76 -0
- package/src/preferences-file.js.map +1 -0
- package/src/preferences.d.ts +73 -0
- package/src/preferences.js +206 -0
- package/src/preferences.js.map +1 -0
- package/src/protocol.d.ts +2 -0
- package/src/protocol.js +2 -0
- package/src/protocol.js.map +1 -1
- package/src/registry.d.ts +20 -1
- package/src/registry.js +4 -0
- package/src/registry.js.map +1 -1
- package/src/rewriter/transforms.d.ts +9 -2
- package/src/rewriter/transforms.js +39 -14
- package/src/rewriter/transforms.js.map +1 -1
- package/src/speculation.d.ts +56 -0
- package/src/speculation.js +85 -0
- package/src/speculation.js.map +1 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Links never speculated, whatever they point at: ones the page marks as not to be followed (as
|
|
3
|
+
* user-generated content often is), and ones it opts out by name.
|
|
4
|
+
*/
|
|
5
|
+
export const SPECULATION_EXCLUDED_LINKS = '[rel~="nofollow"], [data-braid-no-speculate]';
|
|
6
|
+
/**
|
|
7
|
+
* The rule set for these manifests: every pierce pattern of a bound fragment that sets
|
|
8
|
+
* `speculate`.
|
|
9
|
+
*
|
|
10
|
+
* Two kinds are left out, because their patterns describe pages the fragment does not own:
|
|
11
|
+
*
|
|
12
|
+
* - **An unbound fragment** is chrome — a header, a notifications panel — pierced into pages
|
|
13
|
+
* that belong to someone else.
|
|
14
|
+
* - **A pattern that matches any path** (`/*`, `/:rest*`) would make every same-origin link on
|
|
15
|
+
* every page speculative, `/logout` included.
|
|
16
|
+
*/
|
|
17
|
+
export function speculationRulesFor(manifests) {
|
|
18
|
+
const rules = {};
|
|
19
|
+
for (const action of ['prefetch', 'prerender']) {
|
|
20
|
+
const patterns = new Set();
|
|
21
|
+
for (const manifest of manifests) {
|
|
22
|
+
if (manifest.speculate !== action || manifest.bound === false)
|
|
23
|
+
continue;
|
|
24
|
+
for (const pattern of manifest.pierce ?? []) {
|
|
25
|
+
if (matchesAnyPath(pattern))
|
|
26
|
+
continue;
|
|
27
|
+
patterns.add(pattern);
|
|
28
|
+
const toggled = slashToggled(pattern);
|
|
29
|
+
if (toggled)
|
|
30
|
+
patterns.add(toggled);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (patterns.size === 0)
|
|
34
|
+
continue;
|
|
35
|
+
rules[action] = [
|
|
36
|
+
{
|
|
37
|
+
source: 'document',
|
|
38
|
+
where: {
|
|
39
|
+
and: [
|
|
40
|
+
{ or: [...patterns].map((pathname) => ({ href_matches: { pathname }, relative_to: 'document' })) },
|
|
41
|
+
{ not: { selector_matches: SPECULATION_EXCLUDED_LINKS } },
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
// on hover or pointerdown: early enough to feel instant, late enough not to fetch every link
|
|
45
|
+
eagerness: 'moderate',
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
}
|
|
49
|
+
return rules;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Whether a pierce pattern covers every page of the site rather than some of them: anything that
|
|
53
|
+
* is not a literal first segment (`/*`, `/:page`, `/(.*)`), or that matches a path nobody wrote.
|
|
54
|
+
* The home page alone (`/`) is a page like any other. Registry analysis judges it the same way.
|
|
55
|
+
*/
|
|
56
|
+
export function matchesAnyPath(pattern) {
|
|
57
|
+
if (pattern === '/')
|
|
58
|
+
return false;
|
|
59
|
+
if (!/^\/[^:*({?+\\]/.test(pattern))
|
|
60
|
+
return true;
|
|
61
|
+
try {
|
|
62
|
+
const compiled = new URLPattern({ pathname: pattern });
|
|
63
|
+
return ['/__braid_probe', '/__braid_probe/deeper'].some((pathname) => compiled.test({ pathname }));
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The pattern that matches the trailing-slash twin of a URL this one matches.
|
|
71
|
+
*
|
|
72
|
+
* Pierce matching is slash-insensitive (`/checkout/*` also pierces `/checkout`), and a link the
|
|
73
|
+
* gateway would compose should be speculated too. URLPattern itself is strict, so the twin is
|
|
74
|
+
* listed alongside.
|
|
75
|
+
*/
|
|
76
|
+
function slashToggled(pattern) {
|
|
77
|
+
if (pattern === '/' || pattern === '/*')
|
|
78
|
+
return null;
|
|
79
|
+
if (pattern.endsWith('/*'))
|
|
80
|
+
return pattern.slice(0, -2);
|
|
81
|
+
if (pattern.endsWith('*'))
|
|
82
|
+
return null;
|
|
83
|
+
return pattern.endsWith('/') ? pattern.slice(0, -1) : `${pattern}/`;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=speculation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"speculation.js","sourceRoot":"","sources":["../../../../libs/braid-gateway/src/speculation.ts"],"names":[],"mappings":"AAqBA;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,8CAA8C,CAAC;AAOzF;;;;;;;;;;GAUG;AACH,MAAM,UAAU,mBAAmB,CAAC,SAAsC;IACxE,MAAM,KAAK,GAAqB,EAAE,CAAC;IACnC,KAAK,MAAM,MAAM,IAAI,CAAC,UAAU,EAAE,WAAW,CAAU,EAAE,CAAC;QACxD,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;QACnC,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YACjC,IAAI,QAAQ,CAAC,SAAS,KAAK,MAAM,I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