@halonicjs/testing 0.1.0 → 0.1.2

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 (228) hide show
  1. package/NOTICE +4 -0
  2. package/README.md +1 -1
  3. package/dist/adapters/http-kit/src/address-checking.d.ts +19 -0
  4. package/dist/adapters/http-kit/src/address-checking.js +1 -0
  5. package/dist/adapters/http-kit/src/connector-host-class.d.ts +14 -0
  6. package/dist/adapters/http-kit/src/connector-host-class.js +34 -0
  7. package/dist/adapters/http-kit/src/index.d.ts +3 -0
  8. package/dist/adapters/http-kit/src/index.js +2 -0
  9. package/dist/adapters/http-kit/src/ollama-tags.d.ts +20 -0
  10. package/dist/adapters/http-kit/src/ollama-tags.js +38 -0
  11. package/dist/adapters/integration-kit/src/auth-placement.d.ts +40 -0
  12. package/dist/adapters/integration-kit/src/auth-placement.js +102 -0
  13. package/dist/adapters/integration-kit/src/current-secret-forms.d.ts +28 -0
  14. package/dist/adapters/integration-kit/src/current-secret-forms.js +60 -0
  15. package/dist/adapters/integration-kit/src/declaration.d.ts +2 -1
  16. package/dist/adapters/integration-kit/src/declaration.js +4 -3
  17. package/dist/adapters/integration-kit/src/host.d.ts +7 -1
  18. package/dist/adapters/integration-kit/src/host.js +1 -0
  19. package/dist/adapters/integration-kit/src/index.d.ts +5 -2
  20. package/dist/adapters/integration-kit/src/index.js +2 -0
  21. package/dist/adapters/integration-kit/src/leak-attempt.d.ts +4 -2
  22. package/dist/adapters/integration-kit/src/leak-attempt.js +5 -2
  23. package/dist/adapters/integration-kit/src/limited-http.d.ts +7 -1
  24. package/dist/adapters/integration-kit/src/limited-http.js +53 -10
  25. package/dist/adapters/integration-kit/src/loader.d.ts +28 -11
  26. package/dist/adapters/integration-kit/src/loader.js +36 -21
  27. package/dist/adapters/integration-kit/src/own-request.d.ts +2 -2
  28. package/dist/adapters/integration-kit/src/own-request.js +1 -1
  29. package/dist/adapters/integration-kit/src/plugin-load.d.ts +3 -3
  30. package/dist/adapters/integration-kit/src/plugin-load.js +5 -5
  31. package/dist/adapters/integration-kit/src/policy.d.ts +28 -0
  32. package/dist/adapters/integration-kit/src/response-body.d.ts +6 -2
  33. package/dist/adapters/integration-kit/src/secret-filter.d.ts +3 -3
  34. package/dist/adapters/integration-kit/src/secret-filter.js +19 -4
  35. package/dist/adapters/integration-kit/src/secret-in-request.d.ts +3 -0
  36. package/dist/adapters/integration-kit/src/secret-in-request.js +8 -2
  37. package/dist/adapters/integration-kit/src/secret-placement.d.ts +35 -25
  38. package/dist/adapters/integration-kit/src/secret-placement.js +65 -95
  39. package/dist/adapters/integration-kit/src/sign-in-placement.d.ts +29 -0
  40. package/dist/adapters/integration-kit/src/sign-in-placement.js +92 -0
  41. package/dist/adapters/integration-kit/src/workspace-choices.d.ts +37 -0
  42. package/dist/adapters/integration-kit/src/workspace-choices.js +37 -0
  43. package/dist/core/events/src/blocked-sites.d.ts +57 -0
  44. package/dist/core/events/src/blocked-sites.js +89 -0
  45. package/dist/core/events/src/connector-listing-failed.d.ts +18 -0
  46. package/dist/core/events/src/connector-listing-failed.js +1 -0
  47. package/dist/core/events/src/home-path.d.ts +4 -1
  48. package/dist/core/events/src/home-path.js +14 -3
  49. package/dist/core/events/src/index.d.ts +8 -2
  50. package/dist/core/events/src/index.js +5 -0
  51. package/dist/core/events/src/provider-notes.d.ts +16 -0
  52. package/dist/core/events/src/provider-notes.js +24 -0
  53. package/dist/core/events/src/run-basics.d.ts +131 -10
  54. package/dist/core/events/src/run-event.d.ts +120 -11
  55. package/dist/core/events/src/run-reads.d.ts +25 -0
  56. package/dist/core/events/src/run-reads.js +44 -0
  57. package/dist/core/events/src/run-sends.d.ts +47 -0
  58. package/dist/core/events/src/run-sends.js +68 -0
  59. package/dist/core/events/src/run-summary.d.ts +11 -1
  60. package/dist/core/events/src/sandbox-opened-read.d.ts +2 -1
  61. package/dist/core/events/src/sandbox-opened-read.js +27 -2
  62. package/dist/core/events/src/sandbox-opened.d.ts +37 -4
  63. package/dist/core/events/src/sandbox-opened.js +13 -3
  64. package/dist/core/events/src/summarize.js +8 -1
  65. package/dist/core/events/src/timeline-entry.js +1 -0
  66. package/dist/core/events/src/tool-failure-cause.d.ts +25 -0
  67. package/dist/core/events/src/tool-failure-cause.js +66 -0
  68. package/dist/core/format/src/index.d.ts +9 -6
  69. package/dist/core/format/src/index.js +7 -3
  70. package/dist/core/format/src/integration/index.d.ts +3 -2
  71. package/dist/core/format/src/integration/index.js +2 -1
  72. package/dist/core/format/src/kinds/agent/harness-limits.d.ts +14 -0
  73. package/dist/core/format/src/kinds/agent/harness-limits.js +27 -0
  74. package/dist/core/format/src/kinds/agent/sandbox-fields.d.ts +15 -5
  75. package/dist/core/format/src/kinds/agent/sandbox-fields.js +29 -10
  76. package/dist/core/format/src/kinds/agent/schema.d.ts +5 -0
  77. package/dist/core/format/src/kinds/agent/schema.js +5 -2
  78. package/dist/core/format/src/kinds/connector/is-local.js +1 -1
  79. package/dist/core/format/src/kinds/connector/kind.js +122 -2
  80. package/dist/core/format/src/kinds/connector/schema.d.ts +38 -0
  81. package/dist/core/format/src/kinds/connector/schema.js +71 -2
  82. package/dist/core/format/src/kinds/deployments/kind.js +25 -1
  83. package/dist/core/format/src/kinds/deployments/schema.d.ts +40 -0
  84. package/dist/core/format/src/kinds/deployments/schema.js +52 -0
  85. package/dist/core/format/src/kinds/integration/approved-sign-ins.d.ts +62 -0
  86. package/dist/core/format/src/kinds/integration/approved-sign-ins.js +125 -0
  87. package/dist/core/format/src/kinds/integration/host-key.d.ts +7 -0
  88. package/dist/core/format/src/kinds/integration/host-key.js +8 -0
  89. package/dist/core/format/src/kinds/integration/kind.d.ts +4 -2
  90. package/dist/core/format/src/kinds/integration/kind.js +49 -12
  91. package/dist/core/format/src/kinds/integration/policy.d.ts +47 -2
  92. package/dist/core/format/src/kinds/integration/policy.js +61 -2
  93. package/dist/core/format/src/kinds/integration/schema.d.ts +180 -3
  94. package/dist/core/format/src/kinds/integration/schema.js +133 -7
  95. package/dist/core/format/src/kinds/integration/sign-in-checks.d.ts +14 -0
  96. package/dist/core/format/src/kinds/integration/sign-in-checks.js +150 -0
  97. package/dist/core/format/src/kinds/integration/sign-in-urls.d.ts +19 -0
  98. package/dist/core/format/src/kinds/integration/sign-in-urls.js +47 -0
  99. package/dist/core/format/src/kinds/plugin/checks.d.ts +3 -2
  100. package/dist/core/format/src/kinds/plugin/checks.js +24 -6
  101. package/dist/core/format/src/kinds/plugin/schema.d.ts +130 -0
  102. package/dist/core/format/src/kinds/plugin/schema.js +26 -6
  103. package/dist/core/format/src/kinds/plugin/ui-checks.d.ts +1 -1
  104. package/dist/core/format/src/kinds/plugin/ui-checks.js +1 -10
  105. package/dist/core/format/src/kinds/provider/kind.js +40 -1
  106. package/dist/core/format/src/kinds/provider/types.d.ts +19 -0
  107. package/dist/core/format/src/kinds/provider/types.js +22 -0
  108. package/dist/core/format/src/kinds/routine/kind.js +38 -1
  109. package/dist/core/format/src/kinds/routine/schema.d.ts +20 -5
  110. package/dist/core/format/src/kinds/routine/schema.js +42 -14
  111. package/dist/core/format/src/kinds/routine/webhook-headers.d.ts +9 -0
  112. package/dist/core/format/src/kinds/routine/webhook-headers.js +26 -0
  113. package/dist/core/format/src/kinds/workflow/schema.js +1 -1
  114. package/dist/core/format/src/kinds/workspace/kind.js +1 -1
  115. package/dist/core/format/src/kinds/workspace/schema.d.ts +2 -2
  116. package/dist/core/format/src/kinds/workspace/schema.js +6 -5
  117. package/dist/core/format/src/parse-file.js +3 -1
  118. package/dist/core/format/src/shared/fields.d.ts +12 -8
  119. package/dist/core/format/src/shared/fields.js +48 -19
  120. package/dist/core/format/src/shared/guard.d.ts +8 -0
  121. package/dist/core/format/src/shared/guard.js +50 -1
  122. package/dist/core/format/src/shared/missing-keys.d.ts +17 -0
  123. package/dist/core/format/src/shared/missing-keys.js +110 -0
  124. package/dist/core/ports/src/agent-runs-not-available.d.ts +21 -0
  125. package/dist/core/ports/src/agent-runs-not-available.js +25 -0
  126. package/dist/core/ports/src/blob-store.d.ts +12 -0
  127. package/dist/core/ports/src/blob-store.js +22 -0
  128. package/dist/core/ports/src/chat.d.ts +8 -0
  129. package/dist/core/ports/src/connector-client.d.ts +48 -4
  130. package/dist/core/ports/src/connector-client.js +17 -0
  131. package/dist/core/ports/src/connector-start-failure.d.ts +31 -0
  132. package/dist/core/ports/src/connector-start-failure.js +32 -0
  133. package/dist/core/ports/src/connector-tool-error.d.ts +33 -0
  134. package/dist/core/ports/src/connector-tool-error.js +40 -0
  135. package/dist/core/ports/src/definition-store-left-out.d.ts +46 -0
  136. package/dist/core/ports/src/definition-store-left-out.js +47 -0
  137. package/dist/core/ports/src/definition-store.d.ts +36 -5
  138. package/dist/core/ports/src/definition-store.js +1 -1
  139. package/dist/core/ports/src/deploy-target.d.ts +358 -0
  140. package/dist/core/ports/src/deploy-target.js +2 -0
  141. package/dist/core/ports/src/harness-bridge-server.d.ts +67 -7
  142. package/dist/core/ports/src/harness-sign-in-command.d.ts +27 -0
  143. package/dist/core/ports/src/harness-sign-in-command.js +31 -0
  144. package/dist/core/ports/src/harness.d.ts +79 -15
  145. package/dist/core/ports/src/harness.js +1 -1
  146. package/dist/core/ports/src/host-settings-store.d.ts +116 -0
  147. package/dist/core/ports/src/host-settings-store.js +106 -0
  148. package/dist/core/ports/src/host-too-old.d.ts +23 -0
  149. package/dist/core/ports/src/host-too-old.js +27 -0
  150. package/dist/core/ports/src/index.d.ts +32 -13
  151. package/dist/core/ports/src/index.js +28 -8
  152. package/dist/core/ports/src/instruction-trust-store.d.ts +81 -0
  153. package/dist/core/ports/src/instruction-trust-store.js +43 -0
  154. package/dist/core/ports/src/integration.d.ts +71 -7
  155. package/dist/core/ports/src/integration.js +22 -4
  156. package/dist/core/ports/src/llm-provider.d.ts +39 -6
  157. package/dist/core/ports/src/llm-provider.js +46 -3
  158. package/dist/core/ports/src/pairing-codes.d.ts +111 -0
  159. package/dist/core/ports/src/pairing-codes.js +336 -0
  160. package/dist/core/ports/src/pairing-qr-bytes.d.ts +32 -0
  161. package/dist/core/ports/src/pairing-qr-bytes.js +116 -0
  162. package/dist/core/ports/src/password-hasher.d.ts +36 -20
  163. package/dist/core/ports/src/port-versions.d.ts +13 -6
  164. package/dist/core/ports/src/port-versions.js +14 -0
  165. package/dist/core/ports/src/program-probe.d.ts +25 -2
  166. package/dist/core/ports/src/program-probe.js +1 -1
  167. package/dist/core/ports/src/provider-default-base-url.d.ts +10 -0
  168. package/dist/core/ports/src/provider-default-base-url.js +18 -0
  169. package/dist/core/ports/src/record-store.d.ts +17 -0
  170. package/dist/core/ports/src/record-store.js +70 -0
  171. package/dist/core/ports/src/remote-control.d.ts +159 -0
  172. package/dist/core/ports/src/remote-control.js +26 -0
  173. package/dist/core/ports/src/remote-gateway-protocol.d.ts +220 -0
  174. package/dist/core/ports/src/remote-gateway-protocol.js +62 -0
  175. package/dist/core/ports/src/remote-gateway-session.d.ts +194 -0
  176. package/dist/core/ports/src/remote-gateway-session.js +241 -0
  177. package/dist/core/ports/src/run-executor.d.ts +44 -7
  178. package/dist/core/ports/src/run-executor.js +1 -1
  179. package/dist/core/ports/src/run-target-not-found.d.ts +34 -0
  180. package/dist/core/ports/src/run-target-not-found.js +41 -0
  181. package/dist/core/ports/src/sandbox-setting-store.d.ts +68 -0
  182. package/dist/core/ports/src/sandbox-setting-store.js +33 -0
  183. package/dist/core/ports/src/sandbox.d.ts +149 -13
  184. package/dist/core/ports/src/sandbox.js +52 -2
  185. package/dist/core/ports/src/secrets.d.ts +105 -1
  186. package/dist/core/ports/src/secrets.js +75 -2
  187. package/dist/core/ports/src/tool-approval-store.d.ts +50 -8
  188. package/dist/core/ports/src/tool-approval-store.js +59 -0
  189. package/dist/core/ports/src/tunnel-provider.d.ts +117 -0
  190. package/dist/core/ports/src/tunnel-provider.js +65 -0
  191. package/dist/core/ports/src/tunnel-resolver.d.ts +83 -0
  192. package/dist/core/ports/src/tunnel-resolver.js +36 -0
  193. package/dist/core/ports/src/vcs-status.d.ts +8 -0
  194. package/dist/core/ports/src/vcs-status.js +90 -0
  195. package/dist/core/ports/src/vcs.d.ts +30 -0
  196. package/dist/core/schema-kit/src/builders.d.ts +5 -0
  197. package/dist/core/schema-kit/src/builders.js +5 -0
  198. package/dist/core/std/src/amount.d.ts +13 -0
  199. package/dist/core/std/src/amount.js +22 -0
  200. package/dist/core/std/src/hidden-characters.d.ts +28 -0
  201. package/dist/core/std/src/hidden-characters.js +72 -0
  202. package/dist/core/std/src/index.d.ts +7 -0
  203. package/dist/core/std/src/index.js +7 -0
  204. package/dist/core/std/src/path-overlap.d.ts +22 -0
  205. package/dist/core/std/src/path-overlap.js +57 -0
  206. package/dist/core/std/src/public-suffixes.generated.d.ts +9 -0
  207. package/dist/core/std/src/public-suffixes.generated.js +10428 -0
  208. package/dist/core/std/src/registrable-domain.d.ts +10 -0
  209. package/dist/core/std/src/registrable-domain.js +62 -0
  210. package/dist/core/std/src/secret-variable-name.d.ts +12 -0
  211. package/dist/core/std/src/secret-variable-name.js +34 -0
  212. package/dist/core/std/src/site-name.d.ts +35 -0
  213. package/dist/core/std/src/site-name.js +111 -0
  214. package/dist/core/std/src/url-host.d.ts +35 -0
  215. package/dist/core/std/src/url-host.js +170 -0
  216. package/dist/core/std/src/word-diff.d.ts +28 -0
  217. package/dist/core/std/src/word-diff.js +77 -0
  218. package/dist/core/yaml/src/edit-data.d.ts +7 -0
  219. package/dist/core/yaml/src/edit-data.js +62 -3
  220. package/dist/core/yaml/src/edit.d.ts +7 -0
  221. package/dist/core/yaml/src/edit.js +34 -3
  222. package/dist/core/yaml/src/splice-node.d.ts +6 -0
  223. package/dist/core/yaml/src/splice-node.js +10 -0
  224. package/dist/core/yaml/src/splice.js +20 -10
  225. package/dist/testing/integration/src/checks.d.ts +5 -3
  226. package/dist/testing/integration/src/checks.js +31 -3
  227. package/dist/testing/integration/src/load.js +3 -1
  228. package/package.json +9 -2
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The registrable domain of `host`: its public suffix and one label before
3
+ * it. A host that is a public suffix itself, or has a single label, is its
4
+ * own domain. A suffix the list doesn't know is the last label, as the list
5
+ * says. Labels are compared as they are, so pass a lowercase ASCII host.
6
+ *
7
+ * @param host - a lowercase host name in ASCII form, without a trailing dot
8
+ * @returns the registrable domain, at the end of `host`
9
+ */
10
+ export declare function registrableDomain(host: string): string;
@@ -0,0 +1,62 @@
1
+ // Finds a host's registrable domain with our pinned copy of the public suffix
2
+ // list (P7 on HAL-1782): the part a person or company registered, such as
3
+ // `example.co.uk` in `api.example.co.uk`. The list's own algorithm is at
4
+ // https://github.com/publicsuffix/list/wiki/Format.
5
+ import { publicSuffixRules } from "./public-suffixes.generated.js";
6
+ // Split on first use, not when the package loads: most programs that use std
7
+ // never show a site. The rules never change while a program runs.
8
+ let rules;
9
+ /**
10
+ * The registrable domain of `host`: its public suffix and one label before
11
+ * it. A host that is a public suffix itself, or has a single label, is its
12
+ * own domain. A suffix the list doesn't know is the last label, as the list
13
+ * says. Labels are compared as they are, so pass a lowercase ASCII host.
14
+ *
15
+ * @param host - a lowercase host name in ASCII form, without a trailing dot
16
+ * @returns the registrable domain, at the end of `host`
17
+ */
18
+ export function registrableDomain(host) {
19
+ const labels = host.split(".");
20
+ const suffixLength = publicSuffixLength(labels);
21
+ if (labels.length <= suffixLength)
22
+ return host;
23
+ return labels.slice(-(suffixLength + 1)).join(".");
24
+ }
25
+ /** How many labels at the end of `labels` are the public suffix. */
26
+ function publicSuffixLength(labels) {
27
+ const { exact, wildcard, exception } = suffixRules();
28
+ // From the longest candidate to the shortest, so the first rule that
29
+ // matches is the longest. An exception rule wins over the wildcard it
30
+ // undoes: its suffix is the candidate without its first label.
31
+ for (let start = 0; start < labels.length; start += 1) {
32
+ const candidate = labels.slice(start).join(".");
33
+ if (exception.has(candidate))
34
+ return labels.length - start - 1;
35
+ if (exact.has(candidate))
36
+ return labels.length - start;
37
+ const parent = labels.slice(start + 1).join(".");
38
+ if (start + 1 < labels.length && wildcard.has(parent))
39
+ return labels.length - start;
40
+ }
41
+ return 1;
42
+ }
43
+ function suffixRules() {
44
+ rules ??= splitRules(publicSuffixRules);
45
+ return rules;
46
+ }
47
+ function splitRules(text) {
48
+ const exact = new Set();
49
+ const wildcard = new Set();
50
+ const exception = new Set();
51
+ for (const rule of text.split("\n")) {
52
+ if (rule === "")
53
+ continue;
54
+ if (rule.startsWith("!"))
55
+ exception.add(rule.slice(1));
56
+ else if (rule.startsWith("*."))
57
+ wildcard.add(rule.slice(2));
58
+ else
59
+ exact.add(rule);
60
+ }
61
+ return { exact, wildcard, exception };
62
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Whether an environment variable's name says its value is a secret: one
3
+ * of its words, in any letter case, is a word such as `TOKEN`, `KEY` or
4
+ * `PASSWORD`. Words are split at each character that is not a letter or
5
+ * digit and before a capital letter that follows a small letter or digit.
6
+ *
7
+ * @example
8
+ * isSecretVariableName("ANTHROPIC_API_KEY"); // true
9
+ * isSecretVariableName("authToken"); // true
10
+ * isSecretVariableName("KEYMAP"); // false
11
+ */
12
+ export declare function isSecretVariableName(name: string): boolean;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Words in a variable's name that say its value is a secret, such as
3
+ * `API_KEY`, `NODE_AUTH_TOKEN` or `npm_config__authToken`. Only whole words
4
+ * count, so `KEYMAP` or `AUTHOR` don't.
5
+ */
6
+ const secretWords = new Set([
7
+ "APIKEY",
8
+ "AUTH",
9
+ "COOKIE",
10
+ "CREDENTIAL",
11
+ "CREDENTIALS",
12
+ "KEY",
13
+ "PASSPHRASE",
14
+ "PASSWD",
15
+ "PASSWORD",
16
+ "PRIVATE",
17
+ "SECRET",
18
+ "TOKEN",
19
+ ]);
20
+ /**
21
+ * Whether an environment variable's name says its value is a secret: one
22
+ * of its words, in any letter case, is a word such as `TOKEN`, `KEY` or
23
+ * `PASSWORD`. Words are split at each character that is not a letter or
24
+ * digit and before a capital letter that follows a small letter or digit.
25
+ *
26
+ * @example
27
+ * isSecretVariableName("ANTHROPIC_API_KEY"); // true
28
+ * isSecretVariableName("authToken"); // true
29
+ * isSecretVariableName("KEYMAP"); // false
30
+ */
31
+ export function isSecretVariableName(name) {
32
+ const words = name.split(/[^A-Za-z0-9]+|(?<=[a-z0-9])(?=[A-Z])/);
33
+ return words.some((word) => secretWords.has(word.toUpperCase()));
34
+ }
@@ -0,0 +1,35 @@
1
+ /** A site's name, split so a screen can show the registrable domain apart. */
2
+ export interface SiteName {
3
+ /** The labels before the domain, with their last dot, such as `registry.`; empty when there are none. */
4
+ readonly prefix: string;
5
+ /** The registrable domain, such as `npmjs.org`, or the whole address of an IP host. */
6
+ readonly domain: string;
7
+ /** Whether the name was shortened to fit {@link siteNameMaxLength}; the text then holds `…`. */
8
+ readonly cut: boolean;
9
+ }
10
+ /** The most characters `prefix` and `domain` hold together, `…` and escapes included. */
11
+ export declare const siteNameMaxLength = 64;
12
+ /**
13
+ * Writes a host name the way a screen shows a site (requirement
14
+ * `std.site-name`). It is lowercased and loses a trailing dot. Any character
15
+ * other than `a`–`z`, `0`–`9`, `.`, `-`, `:`, `[` and `]` is written as
16
+ * `\u{…}`, so a Cyrillic letter, a full-width dot or a bidi mark can't look
17
+ * like the real thing. Punycode (`xn--`) labels stay as they are, never
18
+ * decoded. `domain` is the registrable domain from our pinned copy of the
19
+ * public suffix list, private section included, looked up on the host's
20
+ * ASCII form so a suffix written in Unicode (`公司.cn`) is found; an IP
21
+ * address is all domain.
22
+ *
23
+ * A name longer than {@link siteNameMaxLength} keeps its domain whole and
24
+ * loses the middle of its prefix, or all of it when the domain alone fills
25
+ * the cap. A domain too long by itself loses its start.
26
+ * Either way `cut` is set and the text holds `…`.
27
+ *
28
+ * @param host - the host as the sandbox reported it, without a port
29
+ * @returns the plain-text parts to show; never a link
30
+ *
31
+ * @example
32
+ * siteName("registry.npmjs.org.evil.example");
33
+ * // => { prefix: "registry.npmjs.org.", domain: "evil.example", cut: false }
34
+ */
35
+ export declare function siteName(host: string): SiteName;
@@ -0,0 +1,111 @@
1
+ // A site's name as a screen shows it: plain text, capped in length, with the
2
+ // registrable domain apart so it can stand out. Julian's conditions for
3
+ // showing blocked and allowed sites (HAL-1673): a look-alike name must never
4
+ // pass for the real one, and a long name must never push the real domain out
5
+ // of sight.
6
+ import { asciiHost } from "./ascii-host.js";
7
+ import { registrableDomain } from "./registrable-domain.js";
8
+ /** The most characters `prefix` and `domain` hold together, `…` and escapes included. */
9
+ export const siteNameMaxLength = 64;
10
+ const ellipsis = "…";
11
+ /** The characters shown as they are; any other is escaped. */
12
+ const plainCharacter = /^[a-z0-9.\-:[\]]$/;
13
+ const ipv4 = /^\d{1,3}(?:\.\d{1,3}){3}$/;
14
+ /**
15
+ * Writes a host name the way a screen shows a site (requirement
16
+ * `std.site-name`). It is lowercased and loses a trailing dot. Any character
17
+ * other than `a`–`z`, `0`–`9`, `.`, `-`, `:`, `[` and `]` is written as
18
+ * `\u{…}`, so a Cyrillic letter, a full-width dot or a bidi mark can't look
19
+ * like the real thing. Punycode (`xn--`) labels stay as they are, never
20
+ * decoded. `domain` is the registrable domain from our pinned copy of the
21
+ * public suffix list, private section included, looked up on the host's
22
+ * ASCII form so a suffix written in Unicode (`公司.cn`) is found; an IP
23
+ * address is all domain.
24
+ *
25
+ * A name longer than {@link siteNameMaxLength} keeps its domain whole and
26
+ * loses the middle of its prefix, or all of it when the domain alone fills
27
+ * the cap. A domain too long by itself loses its start.
28
+ * Either way `cut` is set and the text holds `…`.
29
+ *
30
+ * @param host - the host as the sandbox reported it, without a port
31
+ * @returns the plain-text parts to show; never a link
32
+ *
33
+ * @example
34
+ * siteName("registry.npmjs.org.evil.example");
35
+ * // => { prefix: "registry.npmjs.org.", domain: "evil.example", cut: false }
36
+ */
37
+ export function siteName(host) {
38
+ const plainHost = host.endsWith(".") ? host.slice(0, -1) : host;
39
+ const tokens = shownCharacters(plainHost);
40
+ const text = tokens.join("");
41
+ const domainStart = isIpAddress(text) ? 0 : domainStartOf(tokens, plainHost);
42
+ const prefixTokens = tokens.slice(0, domainStart);
43
+ const domainTokens = tokens.slice(domainStart);
44
+ const domain = domainTokens.join("");
45
+ if (text.length <= siteNameMaxLength) {
46
+ return { prefix: prefixTokens.join(""), domain, cut: false };
47
+ }
48
+ if (domain.length === siteNameMaxLength)
49
+ return { prefix: "", domain, cut: true };
50
+ if (domain.length < siteNameMaxLength) {
51
+ return {
52
+ prefix: middleCut(prefixTokens, siteNameMaxLength - domain.length),
53
+ domain,
54
+ cut: true,
55
+ };
56
+ }
57
+ const kept = lastTokens(domainTokens, siteNameMaxLength - ellipsis.length);
58
+ return { prefix: "", domain: ellipsis + kept.join(""), cut: true };
59
+ }
60
+ /** The host's characters as shown: ASCII letters lowercased, others plain or escaped. */
61
+ function shownCharacters(host) {
62
+ return Array.from(host, (character) => {
63
+ const lower = /^[A-Z]$/.test(character) ? character.toLowerCase() : character;
64
+ if (plainCharacter.test(lower))
65
+ return lower;
66
+ const codePoint = lower.codePointAt(0) ?? 0;
67
+ return `\\u{${codePoint.toString(16)}}`;
68
+ });
69
+ }
70
+ function isIpAddress(text) {
71
+ return ipv4.test(text) || text.includes(":");
72
+ }
73
+ /**
74
+ * Where the registrable domain starts in the shown tokens. The list is read
75
+ * on the ASCII form, which has the same labels: only a plain `.` separates
76
+ * them in both, and an escaped character is never one.
77
+ */
78
+ function domainStartOf(tokens, host) {
79
+ const domainLabels = registrableDomain(asciiHost(host)).split(".").length;
80
+ let dots = 0;
81
+ for (let index = tokens.length - 1; index >= 0; index -= 1) {
82
+ if (tokens[index] !== ".")
83
+ continue;
84
+ dots += 1;
85
+ if (dots === domainLabels)
86
+ return index + 1;
87
+ }
88
+ return 0;
89
+ }
90
+ /** `tokens` in at most `budget` characters, with `…` in place of the middle. Never splits an escape. */
91
+ function middleCut(tokens, budget) {
92
+ const room = budget - ellipsis.length;
93
+ // The end gets the odd character, so a prefix keeps its last dot.
94
+ const tail = lastTokens(tokens, Math.ceil(room / 2));
95
+ const head = firstTokens(tokens, room - Math.ceil(room / 2));
96
+ return head.join("") + ellipsis + tail.join("");
97
+ }
98
+ function firstTokens(tokens, budget) {
99
+ const kept = [];
100
+ let characters = 0;
101
+ for (const token of tokens) {
102
+ if (characters + token.length > budget)
103
+ break;
104
+ kept.push(token);
105
+ characters += token.length;
106
+ }
107
+ return kept;
108
+ }
109
+ function lastTokens(tokens, budget) {
110
+ return firstTokens([...tokens].reverse(), budget).reverse();
111
+ }
@@ -0,0 +1,35 @@
1
+ /** The host of a URL, as a name or an address. */
2
+ export type UrlHost = {
3
+ readonly kind: "domain";
4
+ readonly name: string;
5
+ }
6
+ /** Four numbers from 0 to 255. */
7
+ | {
8
+ readonly kind: "ipv4";
9
+ readonly octets: readonly number[];
10
+ }
11
+ /** Eight numbers from 0 to 65535, the groups of the address in order. */
12
+ | {
13
+ readonly kind: "ipv6";
14
+ readonly groups: readonly number[];
15
+ };
16
+ /**
17
+ * Reads the host of an `http` or `https` URL the way a browser would, for
18
+ * the cases that decide where a request goes: user info before an `@`, a
19
+ * port, a trailing dot, an IPv4 address written as `127.1`, `2130706433` or
20
+ * `0x7f.0.0.1`, and an IPv6 address in brackets. The core has no `URL`
21
+ * class, so it is read by hand.
22
+ *
23
+ * It does not give the full IDNA mapping, so a host with any non-ASCII
24
+ * character, a percent-encoded host, other schemes and anything it doesn't
25
+ * fully understand give `undefined`.
26
+ * Callers that need a safe answer treat that as "unknown".
27
+ *
28
+ * @param url - the URL as written
29
+ * @returns the host, or `undefined` when it is not an `http(s)` URL with a host this reader understands
30
+ *
31
+ * @example
32
+ * urlHost("http://127.1:8080"); // { kind: "ipv4", octets: [127, 0, 0, 1] }
33
+ * urlHost("http://localhost@evil.example"); // { kind: "domain", name: "evil.example" }
34
+ */
35
+ export declare function urlHost(url: string): UrlHost | undefined;
@@ -0,0 +1,170 @@
1
+ import { asciiHost } from "./ascii-host.js";
2
+ const urlShape = /^(https?):\/\/([^/?#\\]*)/i;
3
+ const maxPort = 65535;
4
+ const forbiddenInDomain = /[\s#%/:<>?@[\\\]^|]/;
5
+ /**
6
+ * Reads the host of an `http` or `https` URL the way a browser would, for
7
+ * the cases that decide where a request goes: user info before an `@`, a
8
+ * port, a trailing dot, an IPv4 address written as `127.1`, `2130706433` or
9
+ * `0x7f.0.0.1`, and an IPv6 address in brackets. The core has no `URL`
10
+ * class, so it is read by hand.
11
+ *
12
+ * It does not give the full IDNA mapping, so a host with any non-ASCII
13
+ * character, a percent-encoded host, other schemes and anything it doesn't
14
+ * fully understand give `undefined`.
15
+ * Callers that need a safe answer treat that as "unknown".
16
+ *
17
+ * @param url - the URL as written
18
+ * @returns the host, or `undefined` when it is not an `http(s)` URL with a host this reader understands
19
+ *
20
+ * @example
21
+ * urlHost("http://127.1:8080"); // { kind: "ipv4", octets: [127, 0, 0, 1] }
22
+ * urlHost("http://localhost@evil.example"); // { kind: "domain", name: "evil.example" }
23
+ */
24
+ export function urlHost(url) {
25
+ const match = urlShape.exec(trimEdges(url));
26
+ if (match === null) {
27
+ return undefined;
28
+ }
29
+ const authority = (match[2] ?? "").slice((match[2] ?? "").lastIndexOf("@") + 1);
30
+ if (authority.startsWith("[")) {
31
+ return ipv6Host(authority);
32
+ }
33
+ const colon = authority.indexOf(":");
34
+ const text = colon === -1 ? authority : authority.slice(0, colon);
35
+ if (colon !== -1 && !isPort(authority.slice(colon + 1))) {
36
+ return undefined;
37
+ }
38
+ return nameOrIpv4(text);
39
+ }
40
+ /** A port is digits only, or empty, and fits in 16 bits. */
41
+ function isPort(text) {
42
+ return /^\d*$/.test(text) && (text === "" || Number(text) <= maxPort);
43
+ }
44
+ function ipv6Host(authority) {
45
+ const end = authority.indexOf("]");
46
+ const after = authority.slice(end + 1);
47
+ if (end === -1 || !(after === "" || (after.startsWith(":") && isPort(after.slice(1))))) {
48
+ return undefined;
49
+ }
50
+ const groups = parseIpv6(authority.slice(1, end));
51
+ return groups === undefined ? undefined : { kind: "ipv6", groups };
52
+ }
53
+ function nameOrIpv4(text) {
54
+ // Non-ASCII hosts would need the full IDNA mapping, so a wrong guess is worse than none.
55
+ if (text === "" ||
56
+ hasNonAscii(text) ||
57
+ forbiddenInDomain.test(text) ||
58
+ hasControlCharacter(text)) {
59
+ return undefined;
60
+ }
61
+ const name = asciiHost(text.endsWith(".") ? text.slice(0, -1) : text);
62
+ const labels = name.split(".");
63
+ if (labels.some((label) => label === "")) {
64
+ return undefined;
65
+ }
66
+ if (!endsInNumber(labels)) {
67
+ return { kind: "domain", name };
68
+ }
69
+ const octets = parseIpv4(labels);
70
+ return octets === undefined ? undefined : { kind: "ipv4", octets };
71
+ }
72
+ /** A host whose last label is a number is an IPv4 address or nothing (WHATWG "ends in a number"). */
73
+ function endsInNumber(labels) {
74
+ const last = labels[labels.length - 1] ?? "";
75
+ return /^(\d+|0x[0-9a-f]*)$/.test(last);
76
+ }
77
+ function parseIpv4(labels) {
78
+ if (labels.length > 4) {
79
+ return undefined;
80
+ }
81
+ const numbers = labels.map(ipv4Number);
82
+ if (numbers.some((number) => number === undefined)) {
83
+ return undefined;
84
+ }
85
+ const values = numbers;
86
+ const last = values[values.length - 1];
87
+ const head = values.slice(0, -1);
88
+ if (head.some((value) => value > 255) || last >= 256 ** (5 - values.length)) {
89
+ return undefined;
90
+ }
91
+ let address = last;
92
+ head.forEach((value, index) => {
93
+ address += value * 256 ** (3 - index);
94
+ });
95
+ return [24, 16, 8, 0].map((shift) => Math.floor(address / 2 ** shift) % 256);
96
+ }
97
+ function ipv4Number(label) {
98
+ if (/^0x[0-9a-f]*$/.test(label)) {
99
+ return label.length === 2 ? 0 : Number.parseInt(label.slice(2), 16);
100
+ }
101
+ if (/^0[0-7]+$/.test(label)) {
102
+ return Number.parseInt(label, 8);
103
+ }
104
+ // A leading 0 means octal, so `09` is not a number.
105
+ return /^(0|[1-9]\d*)$/.test(label) ? Number.parseInt(label, 10) : undefined;
106
+ }
107
+ function parseIpv6(text) {
108
+ const sides = text.split("::");
109
+ if (sides.length > 2) {
110
+ return undefined;
111
+ }
112
+ const head = groupsOf(sides[0] ?? "", sides.length === 1);
113
+ const tail = sides.length === 2 ? groupsOf(sides[1] ?? "", true) : [];
114
+ if (head === undefined || tail === undefined) {
115
+ return undefined;
116
+ }
117
+ if (sides.length === 1) {
118
+ return head.length === 8 ? head : undefined;
119
+ }
120
+ const missing = 8 - head.length - tail.length;
121
+ return missing < 1 ? undefined : [...head, ...new Array(missing).fill(0), ...tail];
122
+ }
123
+ /** The groups of one side of an IPv6 address; the end of the address may be an IPv4 address that makes two. */
124
+ function groupsOf(side, endsAddress) {
125
+ if (side === "") {
126
+ return [];
127
+ }
128
+ const parts = side.split(":");
129
+ const groups = [];
130
+ for (const [index, part] of parts.entries()) {
131
+ if (index === parts.length - 1 && part.includes(".")) {
132
+ const octets = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/
133
+ .exec(part)
134
+ ?.slice(1)
135
+ .map(Number);
136
+ if (!endsAddress || octets === undefined || octets.some((octet) => octet > 255)) {
137
+ return undefined;
138
+ }
139
+ groups.push(octets[0] * 256 + octets[1], octets[2] * 256 + octets[3]);
140
+ }
141
+ else if (/^[0-9a-f]{1,4}$/i.test(part)) {
142
+ groups.push(Number.parseInt(part, 16));
143
+ }
144
+ else {
145
+ return undefined;
146
+ }
147
+ }
148
+ return groups;
149
+ }
150
+ function hasControlCharacter(text) {
151
+ return [...text].some((character) => {
152
+ const code = character.codePointAt(0) ?? 0;
153
+ return code < 0x20 || code === 0x7f;
154
+ });
155
+ }
156
+ /** What the URL rules strip from both ends: control characters and the space, not Unicode spaces. */
157
+ function trimEdges(text) {
158
+ let start = 0;
159
+ let end = text.length;
160
+ while (start < end && text.charCodeAt(start) <= 0x20) {
161
+ start += 1;
162
+ }
163
+ while (end > start && text.charCodeAt(end - 1) <= 0x20) {
164
+ end -= 1;
165
+ }
166
+ return text.slice(start, end);
167
+ }
168
+ function hasNonAscii(text) {
169
+ return [...text].some((character) => (character.codePointAt(0) ?? 0) > 0x7f);
170
+ }
@@ -0,0 +1,28 @@
1
+ /** A run of a text in a word diff: its `text`, and whether the other text doesn't have it. */
2
+ export interface WordPiece {
3
+ readonly text: string;
4
+ readonly changed: boolean;
5
+ }
6
+ /** Both texts of a word diff, each cut into pieces that join back to the text. */
7
+ export interface WordDiff {
8
+ readonly before: readonly WordPiece[];
9
+ readonly after: readonly WordPiece[];
10
+ }
11
+ /**
12
+ * The words that differ between two texts, so a page can mark them inside
13
+ * the text rather than show two whole texts to compare by eye.
14
+ *
15
+ * Words, spaces and punctuation are compared as tokens, keeping the longest
16
+ * run of common tokens (as `diffLines` does for lines). The spaces at either
17
+ * end of a changed run are left unmarked, so the marks sit on the words.
18
+ *
19
+ * @param before - the text as it was
20
+ * @param after - the text as it is now
21
+ * @returns the pieces of each text, joining back to `before` and `after`
22
+ *
23
+ * @example
24
+ * diffWords("Write short sentences.", "Write short, warm sentences.").after;
25
+ * // [{ text: "Write short", changed: false }, { text: ", warm", changed: true },
26
+ * // { text: " sentences.", changed: false }]
27
+ */
28
+ export declare function diffWords(before: string, after: string): WordDiff;
@@ -0,0 +1,77 @@
1
+ import { diffLines } from "./line-diff.js";
2
+ /**
3
+ * Words, runs of spaces and single other characters (punctuation, symbols).
4
+ * Every character falls in one of them, so the tokens join back to the text.
5
+ */
6
+ const tokenPattern = /[\p{L}\p{N}_]+|\s+|[^\p{L}\p{N}_\s]/gu;
7
+ /**
8
+ * The words that differ between two texts, so a page can mark them inside
9
+ * the text rather than show two whole texts to compare by eye.
10
+ *
11
+ * Words, spaces and punctuation are compared as tokens, keeping the longest
12
+ * run of common tokens (as `diffLines` does for lines). The spaces at either
13
+ * end of a changed run are left unmarked, so the marks sit on the words.
14
+ *
15
+ * @param before - the text as it was
16
+ * @param after - the text as it is now
17
+ * @returns the pieces of each text, joining back to `before` and `after`
18
+ *
19
+ * @example
20
+ * diffWords("Write short sentences.", "Write short, warm sentences.").after;
21
+ * // [{ text: "Write short", changed: false }, { text: ", warm", changed: true },
22
+ * // { text: " sentences.", changed: false }]
23
+ */
24
+ export function diffWords(before, after) {
25
+ const beforeTokens = before.match(tokenPattern) ?? [];
26
+ const afterTokens = after.match(tokenPattern) ?? [];
27
+ // diffLines compares lines, so each distinct token becomes a line of its own number.
28
+ const numbers = new Map();
29
+ const asLines = (tokens) => tokens
30
+ .map((token) => {
31
+ const known = numbers.get(token);
32
+ if (known !== undefined)
33
+ return `${known}\n`;
34
+ numbers.set(token, numbers.size);
35
+ return `${numbers.size - 1}\n`;
36
+ })
37
+ .join("");
38
+ const changes = diffLines(asLines(beforeTokens), asLines(afterTokens));
39
+ const beforeChanged = new Array(beforeTokens.length).fill(false);
40
+ const afterChanged = new Array(afterTokens.length).fill(false);
41
+ for (const change of changes) {
42
+ markRun(beforeTokens, beforeChanged, change.beforeStart, change.beforeEnd);
43
+ markRun(afterTokens, afterChanged, change.afterStart, change.afterStart + change.added.length);
44
+ }
45
+ return {
46
+ before: piecesOf(beforeTokens, beforeChanged),
47
+ after: piecesOf(afterTokens, afterChanged),
48
+ };
49
+ }
50
+ /** Marks the tokens from `start` up to `end` as changed, but not the spaces at either end. */
51
+ function markRun(tokens, changed, start, end) {
52
+ let first = start;
53
+ let last = end;
54
+ while (first < last && isSpace(tokens[first]))
55
+ first++;
56
+ while (last > first && isSpace(tokens[last - 1]))
57
+ last--;
58
+ changed.fill(true, first, last);
59
+ }
60
+ function isSpace(token) {
61
+ return token !== undefined && /^\s+$/u.test(token);
62
+ }
63
+ /** The tokens joined into pieces, a new piece wherever `changed` flips. */
64
+ function piecesOf(tokens, changed) {
65
+ const pieces = [];
66
+ tokens.forEach((token, index) => {
67
+ const isChanged = changed[index] === true;
68
+ const last = pieces.at(-1);
69
+ if (last?.changed === isChanged) {
70
+ last.text += token;
71
+ }
72
+ else {
73
+ pieces.push({ text: token, changed: isChanged });
74
+ }
75
+ });
76
+ return pieces;
77
+ }
@@ -9,6 +9,9 @@ import type { YamlPath } from "./read.js";
9
9
  * nothing is there.
10
10
  * - `insert`: adds `value` to a list before the item at the index that ends
11
11
  * `path`; the list's length adds it at the end.
12
+ * - `rename`: gives the map key that ends `path` the name `to`, in the same
13
+ * place in its map and with the same value. The key must be there and `to`
14
+ * must not be a key of that map yet; `to` equal to the old key changes nothing.
12
15
  *
13
16
  * A `set` that adds a new key to a map puts it at the end of that map, or
14
17
  * right after the key `after` names; `after: null` puts it first. This is
@@ -29,6 +32,10 @@ export type YamlEdit = {
29
32
  readonly kind: "insert";
30
33
  readonly path: YamlPath;
31
34
  readonly value: unknown;
35
+ } | {
36
+ readonly kind: "rename";
37
+ readonly path: YamlPath;
38
+ readonly to: string;
32
39
  };
33
40
  /**
34
41
  * Applies an edit to plain data, the way `editYaml` must change what the