@botiverse/porthole 0.14.7 → 0.14.9

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 (87) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/dist/bridge/bridge.d.ts +6 -0
  3. package/dist/bridge/bridge.js +5 -0
  4. package/dist/check/files.js +2 -0
  5. package/dist/check/lint.js +82 -3
  6. package/dist/check/page-source.js +1 -1
  7. package/dist/compile/policy.d.ts +17 -6
  8. package/dist/compile/policy.js +22 -7
  9. package/dist/config/capabilities.d.ts +16 -2
  10. package/dist/config/capabilities.js +8 -0
  11. package/dist/config/schema.d.ts +13 -0
  12. package/dist/config/validate.js +15 -0
  13. package/dist/conformance/harness-source.js +1 -1
  14. package/dist/conformance/kit.d.ts +5 -0
  15. package/dist/conformance/kit.js +21 -0
  16. package/dist/conformance/limits.js +10 -5
  17. package/dist/conformance/probes.js +139 -28
  18. package/dist/conformance/served.js +25 -4
  19. package/dist/find/types.d.ts +83 -0
  20. package/dist/find/types.js +29 -0
  21. package/dist/find/validate.d.ts +17 -0
  22. package/dist/find/validate.js +82 -0
  23. package/dist/guide/guide.js +2 -1
  24. package/dist/guide/limits.js +8 -1
  25. package/dist/host/create-host.js +249 -4
  26. package/dist/host/pass-on.js +3 -2
  27. package/dist/host/types.d.ts +51 -0
  28. package/dist/index.d.ts +4 -2
  29. package/dist/index.js +4 -1
  30. package/dist/presets/presets.d.ts +6 -0
  31. package/dist/presets/presets.js +6 -0
  32. package/dist/react/frame.d.ts +43 -0
  33. package/dist/react/frame.js +107 -3
  34. package/dist/react/view.js +2 -0
  35. package/dist/runtime/contract.d.ts +1 -1
  36. package/dist/runtime/contract.js +1 -1
  37. package/dist/server/files.js +4 -3
  38. package/dist/server/server.js +4 -1
  39. package/dist/shell/annotate.js +2 -95
  40. package/dist/shell/document.d.ts +3 -3
  41. package/dist/shell/document.js +5 -3
  42. package/dist/shell/find.d.ts +82 -0
  43. package/dist/shell/find.js +725 -0
  44. package/dist/shell/plan.d.ts +5 -0
  45. package/dist/shell/plan.js +2 -0
  46. package/dist/shell/runtime.d.ts +30 -1
  47. package/dist/shell/runtime.js +789 -105
  48. package/dist/shell/text.d.ts +17 -0
  49. package/dist/shell/text.js +140 -0
  50. package/docs/ADOPTING.md +103 -0
  51. package/docs/ARCHITECTURE.md +10 -3
  52. package/docs/DECISIONS.md +513 -1
  53. package/docs/DESIGN.md +40 -4
  54. package/docs/THREAT_MODEL.md +75 -6
  55. package/docs/insertion-entry-points.md +112 -0
  56. package/package.json +1 -1
  57. package/src/bridge/bridge.ts +7 -0
  58. package/src/check/files.ts +2 -0
  59. package/src/check/lint.ts +97 -8
  60. package/src/compile/policy.ts +23 -6
  61. package/src/config/capabilities.ts +13 -1
  62. package/src/config/schema.ts +13 -0
  63. package/src/config/validate.ts +16 -0
  64. package/src/conformance/kit.ts +22 -0
  65. package/src/conformance/limits.ts +10 -5
  66. package/src/conformance/probes.ts +149 -28
  67. package/src/conformance/served.ts +24 -4
  68. package/src/find/types.ts +86 -0
  69. package/src/find/validate.ts +101 -0
  70. package/src/guide/guide.ts +3 -1
  71. package/src/guide/limits.ts +9 -1
  72. package/src/host/create-host.ts +267 -4
  73. package/src/host/pass-on.ts +3 -2
  74. package/src/host/types.ts +52 -0
  75. package/src/index.ts +13 -0
  76. package/src/presets/presets.ts +15 -0
  77. package/src/react/frame.tsx +160 -1
  78. package/src/react/view.tsx +3 -0
  79. package/src/runtime/contract.ts +1 -1
  80. package/src/server/files.ts +8 -2
  81. package/src/server/server.ts +4 -1
  82. package/src/shell/annotate.ts +3 -95
  83. package/src/shell/document.ts +5 -3
  84. package/src/shell/find.ts +731 -0
  85. package/src/shell/plan.ts +7 -0
  86. package/src/shell/runtime.ts +792 -105
  87. package/src/shell/text.ts +140 -0
@@ -0,0 +1,140 @@
1
+ /**
2
+ * The document's text as annotations (P053) and find in page (P131) read it, spliced into each of
3
+ * their runtimes (inside its own function, after it took `createTreeWalker` and `createRange` from
4
+ * the DOM before content ran): the body's text nodes in order, without scripts, styles, noscript
5
+ * and templates, as one string with each node's start (`textIndex()`); an offset into it as a
6
+ * boundary point (`pointAt`), a boundary point as an offset (`offsetOf`), a span as a range
7
+ * (`rangeAt`). One source, so a match find highlights and an anchor's `TextPositionSelector` count
8
+ * the same text. Plain ES2020, no template literals (it sits in one).
9
+ *
10
+ * Every built-in it calls is taken before content runs and called through the shell's `callOf`
11
+ * (P129): the walker's step, a node's type, name and text (the `nodeType`, `localName` and
12
+ * `nodeValue` getters), the body getter, a range's boundaries, and the map of nodes. Its
13
+ * lists are the shell's prototype-less ones (`listOf`, `pushTo`), and the names it skips a lookup
14
+ * with no prototype, so nothing content puts on `Array.prototype`, `Object.prototype`, `Map` or a
15
+ * DOM prototype takes part in what it reads.
16
+ */
17
+ export const TEXT_INDEX = String.raw`// The document's text: the body's text nodes in order, without scripts, styles, templates.
18
+ const textNextNode = TreeWalker.prototype.nextNode;
19
+ const textTypeOf = getterOf(Node.prototype, "nodeType");
20
+ const textNameOf = getterOf(Element.prototype, "localName");
21
+ const textValueOf = getterOf(Node.prototype, "nodeValue");
22
+ const textBodyOf = getterOf(Document.prototype, "body");
23
+ const textSetStart = Range.prototype.setStart;
24
+ const textSetEnd = Range.prototype.setEnd;
25
+ const textCollapse = Range.prototype.collapse;
26
+ const textComparePoint = Range.prototype.comparePoint;
27
+ const TextMap = Map;
28
+ const textMapSet = Map.prototype.set;
29
+ const textMapGet = Map.prototype.get;
30
+ const textMapHas = Map.prototype.has;
31
+ const TEXT_SHOW = NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT;
32
+ const TEXT_ACCEPT = NodeFilter.FILTER_ACCEPT;
33
+ const TEXT_REJECT = NodeFilter.FILTER_REJECT;
34
+ const TEXT_SKIP = NodeFilter.FILTER_SKIP;
35
+ const SKIPPED = bare(null);
36
+ SKIPPED.script = 1;
37
+ SKIPPED.style = 1;
38
+ SKIPPED.noscript = 1;
39
+ SKIPPED.template = 1;
40
+ // A text node's text, read once through the kept getter; anything but a string is none.
41
+ const textOf = (node) => {
42
+ const value = textValueOf === null ? node.nodeValue : callOf(textValueOf, node, []);
43
+ return typeof value === "string" ? value : "";
44
+ };
45
+ const textKind = (node) => (textTypeOf === null ? node.nodeType : callOf(textTypeOf, node, []));
46
+ const textLesser = (a, b) => (a < b ? a : b);
47
+ const skippedElement = (node) => SKIPPED[textNameOf === null ? node.localName : callOf(textNameOf, node, [])] === 1;
48
+ const textIndex = () => {
49
+ const nodes = listOf();
50
+ const starts = listOf();
51
+ const at = new TextMap();
52
+ let text = "";
53
+ const body = textBodyOf === null ? document.body : callOf(textBodyOf, document, []);
54
+ if (body) {
55
+ const walker = callOf(createTreeWalker, document, [body, TEXT_SHOW, {
56
+ acceptNode: (node) => (textKind(node) === 1 ? (skippedElement(node) ? TEXT_REJECT : TEXT_SKIP) : TEXT_ACCEPT),
57
+ }]);
58
+ for (let node = callOf(textNextNode, walker, []); node !== null; node = callOf(textNextNode, walker, [])) {
59
+ callOf(textMapSet, at, [node, nodes.length]);
60
+ pushTo(nodes, node);
61
+ pushTo(starts, text.length);
62
+ text += textOf(node);
63
+ }
64
+ }
65
+ return { nodes, starts, at, text };
66
+ };
67
+ // A boundary point as an offset into the text: in a text node, where it is; elsewhere, the
68
+ // first text at or after it.
69
+ const offsetOf = (index, container, offset) => {
70
+ if (textKind(container) === 3 && callOf(textMapHas, index.at, [container])) {
71
+ return index.starts[callOf(textMapGet, index.at, [container])] + textLesser(offset, textOf(container).length);
72
+ }
73
+ const point = callOf(createRange, document, []);
74
+ try {
75
+ callOf(textSetStart, point, [container, offset]);
76
+ } catch (_) {
77
+ return index.text.length;
78
+ }
79
+ callOf(textCollapse, point, [true]);
80
+ let low = 0;
81
+ let high = index.nodes.length;
82
+ while (low < high) {
83
+ const middle = (low + high) >> 1;
84
+ if (callOf(textComparePoint, point, [index.nodes[middle], 0]) >= 0) {
85
+ high = middle;
86
+ } else {
87
+ low = middle + 1;
88
+ }
89
+ }
90
+ return low < index.nodes.length ? index.starts[low] : index.text.length;
91
+ };
92
+ const pointAt = (index, at, end) => {
93
+ let low = 0;
94
+ let high = index.nodes.length - 1;
95
+ let found = -1;
96
+ while (low <= high) {
97
+ const middle = (low + high) >> 1;
98
+ if (index.starts[middle] <= at) {
99
+ found = middle;
100
+ low = middle + 1;
101
+ } else {
102
+ high = middle - 1;
103
+ }
104
+ }
105
+ if (found === -1) {
106
+ return null;
107
+ }
108
+ let node = index.nodes[found];
109
+ let offset = at - index.starts[found];
110
+ if (end) {
111
+ // An end at a node's very start belongs to the text before it.
112
+ while (offset === 0 && found > 0) {
113
+ found -= 1;
114
+ node = index.nodes[found];
115
+ offset = textOf(node).length;
116
+ }
117
+ } else {
118
+ while (offset >= textOf(node).length && found + 1 < index.nodes.length) {
119
+ found += 1;
120
+ node = index.nodes[found];
121
+ offset = 0;
122
+ }
123
+ }
124
+ return { node, offset: textLesser(offset, textOf(node).length) };
125
+ };
126
+ const rangeAt = (index, start, end) => {
127
+ const from = pointAt(index, start, false);
128
+ const to = pointAt(index, end, true);
129
+ if (from === null || to === null) {
130
+ return null;
131
+ }
132
+ const range = callOf(createRange, document, []);
133
+ try {
134
+ callOf(textSetStart, range, [from.node, from.offset]);
135
+ callOf(textSetEnd, range, [to.node, to.offset]);
136
+ } catch (_) {
137
+ return null;
138
+ }
139
+ return range;
140
+ };`;