@engenty/wizards-runtime 0.17.0 → 0.17.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 (149) hide show
  1. package/apps/runtime/dist/app.js +2 -0
  2. package/apps/runtime/dist/broker/account.js +20 -1
  3. package/apps/runtime/dist/broker/call.js +116 -20
  4. package/apps/runtime/dist/broker/host.js +8 -2
  5. package/apps/runtime/dist/broker/index.js +3 -2
  6. package/apps/runtime/dist/broker/log.js +149 -14
  7. package/apps/runtime/dist/broker/over.js +38 -0
  8. package/apps/runtime/dist/broker/rights.js +192 -0
  9. package/apps/runtime/dist/broker/rwx.js +295 -51
  10. package/apps/runtime/dist/broker/send.js +25 -12
  11. package/apps/runtime/dist/bundles/discover.js +133 -0
  12. package/apps/runtime/dist/bundles/fetch.js +119 -0
  13. package/apps/runtime/dist/bundles/index.js +621 -0
  14. package/apps/runtime/dist/bundles/read.js +607 -0
  15. package/apps/runtime/dist/connectors/external.js +540 -61
  16. package/apps/runtime/dist/connectors/person-sign-ins.js +1 -0
  17. package/apps/runtime/dist/connectors/reload.js +73 -0
  18. package/apps/runtime/dist/connectors/space-connections.js +73 -6
  19. package/apps/runtime/dist/db/migrations/tenant/0058_mcp_state_across_roles.sql +26 -0
  20. package/apps/runtime/dist/db/migrations/tenant/0059_space_connection_paused.sql +2 -0
  21. package/apps/runtime/dist/db/migrations/tenant/0060_space_bundle.sql +21 -0
  22. package/apps/runtime/dist/db/migrations/tenant/0061_space_connection_limits.sql +2 -0
  23. package/apps/runtime/dist/db/migrations/tenant/meta/0058_snapshot.json +6696 -0
  24. package/apps/runtime/dist/db/migrations/tenant/meta/0059_snapshot.json +6710 -0
  25. package/apps/runtime/dist/db/migrations/tenant/meta/0060_snapshot.json +6857 -0
  26. package/apps/runtime/dist/db/migrations/tenant/meta/0061_snapshot.json +6871 -0
  27. package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +28 -0
  28. package/apps/runtime/dist/db/schema.js +81 -3
  29. package/apps/runtime/dist/docs/dev/broker.md +312 -32
  30. package/apps/runtime/dist/docs/dev/skills.md +8 -1
  31. package/apps/runtime/dist/docs/user-guide/capabilities.md +127 -5
  32. package/apps/runtime/dist/docs/user-guide/connectors.md +103 -5
  33. package/apps/runtime/dist/docs/user-guide/protocol.md +15 -4
  34. package/apps/runtime/dist/docs/user-guide/skills.md +11 -1
  35. package/apps/runtime/dist/docs/user-guide/test-and-share.md +1 -1
  36. package/apps/runtime/dist/elements/caller.js +2 -2
  37. package/apps/runtime/dist/engenty/connections-external/build-connector.js +23 -8
  38. package/apps/runtime/dist/engenty/connections-external/import-service.js +18 -9
  39. package/apps/runtime/dist/engenty/connections-external/importer/classify.js +12 -0
  40. package/apps/runtime/dist/engenty/connections-external/invoke/http-invoker.js +1 -0
  41. package/apps/runtime/dist/engenty/connections-external/net/guarded-fetch.js +6 -3
  42. package/apps/runtime/dist/engenty/connections-sdk/oauth2.js +2 -2
  43. package/apps/runtime/dist/engenty/shims/web-ingest.js +64 -0
  44. package/apps/runtime/dist/engine/resources.js +15 -6
  45. package/apps/runtime/dist/engine/runner.js +2 -1
  46. package/apps/runtime/dist/env.js +2 -0
  47. package/apps/runtime/dist/index.js +11 -2
  48. package/apps/runtime/dist/mcp/handler.js +2 -2
  49. package/apps/runtime/dist/mcp/seen.js +71 -51
  50. package/apps/runtime/dist/mcp/tools.js +5 -1
  51. package/apps/runtime/dist/prompts/mcp.md +1 -1
  52. package/apps/runtime/dist/prompts/tools.md +16 -0
  53. package/apps/runtime/dist/routes/broker.js +1 -1
  54. package/apps/runtime/dist/routes/bundles.js +91 -0
  55. package/apps/runtime/dist/routes/connections.js +14 -3
  56. package/apps/runtime/dist/routes/discover.js +1 -0
  57. package/apps/runtime/dist/routes/rules.js +53 -9
  58. package/apps/runtime/dist/routes/studio.js +23 -18
  59. package/apps/runtime/dist/services/cloud.js +2 -0
  60. package/apps/runtime/dist/services/discover.js +9 -3
  61. package/apps/runtime/dist/services/draft-events.js +79 -6
  62. package/apps/runtime/dist/services/files.js +2 -2
  63. package/apps/runtime/dist/services/skills.js +61 -5
  64. package/apps/runtime/dist/services/space-index.js +113 -11
  65. package/apps/runtime/dist/services/synced-spaces.js +5 -1
  66. package/apps/runtime/dist/services/wizards.js +48 -2
  67. package/apps/runtime/dist/sharing/jobs.js +6 -2
  68. package/apps/runtime/dist/sync/records.js +6 -1
  69. package/apps/runtime/dist/texts.js +36 -1
  70. package/apps/runtime/dist/tools/connections.js +8 -0
  71. package/apps/runtime/dist/tools/connector.js +13 -1
  72. package/apps/runtime/dist/tools/index.js +8 -3
  73. package/apps/runtime/dist/tools/skills.js +11 -1
  74. package/apps/web/dist/assets/{AboutDialog-B2Xmxyj4.js → AboutDialog-NzB-GEj_.js} +1 -1
  75. package/apps/web/dist/assets/{BlockEditor-D0HoUSih.js → BlockEditor-HyDf8fOb.js} +1 -1
  76. package/apps/web/dist/assets/Bundle-B0Bhs0By.js +1 -0
  77. package/apps/web/dist/assets/CapabilitiesPage-CQSaMsWK.js +2 -0
  78. package/apps/web/dist/assets/{ChangelogDialog-BIrJbAKi.js → ChangelogDialog-Ngxuqubx.js} +1 -1
  79. package/apps/web/dist/assets/{CodeEditor-DUVcXI5P.js → CodeEditor-C2LBf4pS.js} +1 -1
  80. package/apps/web/dist/assets/EditorPage-Dysvvadu.js +3 -0
  81. package/apps/web/dist/assets/{FlowPreview-BIag8_Be.js → FlowPreview-CGfSaeYu.js} +1 -1
  82. package/apps/web/dist/assets/LangSwitch-B7UQlGh5.js +1 -0
  83. package/apps/web/dist/assets/Marketplace-BkIxd1PW.js +2 -0
  84. package/apps/web/dist/assets/{NewWizardChannels-DI3SQ4iS.js → NewWizardChannels-BWLaVFld.js} +1 -1
  85. package/apps/web/dist/assets/NewWizardPage-BpRd8tDF.js +7 -0
  86. package/apps/web/dist/assets/OnboardingPage-ScEcQcR0.js +1 -0
  87. package/apps/web/dist/assets/PreviewPane-CaC1FYxZ.js +7 -0
  88. package/apps/web/dist/assets/ProtocolSection-CILRCYLD.js +1 -0
  89. package/apps/web/dist/assets/PublicRunner-DHX431Pr.js +1 -0
  90. package/apps/web/dist/assets/{ReadOnly-CWf5x74a.js → ReadOnly-DqS1UDQs.js} +1 -1
  91. package/apps/web/dist/assets/SettingsPage-Dp2-rHXu.js +2 -0
  92. package/apps/web/dist/assets/SetupPage-mzMLQcD5.js +1 -0
  93. package/apps/web/dist/assets/SharePage-CBDgqEH7.js +1 -0
  94. package/apps/web/dist/assets/SkillEditor-Cf6FqkkU.js +1 -0
  95. package/apps/web/dist/assets/{Skills-CN4QE7Kx.js → Skills-DqV5Oqqu.js} +5 -5
  96. package/apps/web/dist/assets/SpacePage-C3SXe2hs.js +2 -0
  97. package/apps/web/dist/assets/{UpdateCheck-QhBI3rlQ.js → UpdateCheck-B51jMIhp.js} +2 -2
  98. package/apps/web/dist/assets/brand-CG35IRgr.js +2 -0
  99. package/apps/web/dist/assets/changelog-DN6sziTl.js +1 -0
  100. package/apps/web/dist/assets/{code-editor-DkPo1C-L.js → code-editor-Bsyy7cuT.js} +2 -2
  101. package/apps/web/dist/assets/{host-D6H6Z9Jp.js → host-DaDyLfqT.js} +16 -16
  102. package/apps/web/dist/assets/i18n-CyrHWeGk.js +55 -0
  103. package/apps/web/dist/assets/index-Db9JthZr.css +1 -0
  104. package/apps/web/dist/assets/index-LjwVRoki.js +10 -0
  105. package/apps/web/dist/assets/minus-BpYkOkYw.js +1 -0
  106. package/apps/web/dist/assets/plug-De3mHDFV.js +1 -0
  107. package/apps/web/dist/assets/{read-aloud-CnFwDSGD.js → read-aloud-DCTouK7J.js} +1 -1
  108. package/apps/web/dist/assets/{runners-0sTPbWzr.js → runners-DtiBHvG7.js} +1 -1
  109. package/apps/web/dist/assets/session-DKGZUy6h.js +1 -0
  110. package/apps/web/dist/assets/{theme-CkNTe-_Z.js → theme-Bcj8TXQr.js} +2 -2
  111. package/apps/web/dist/assets/{ui-DAk_vjrL.js → ui-C6vSmisV.js} +1 -1
  112. package/apps/web/dist/assets/{useMutation-isHfB33W.js → useMutation-B8hJ5Z6N.js} +1 -1
  113. package/apps/web/dist/assets/{viewers-ijNZHJFG.js → viewers-b9FdC42r.js} +2 -2
  114. package/apps/web/dist/assets/{volume-2-BqlBtCIJ.js → volume-2-B1roRk6x.js} +1 -1
  115. package/apps/web/dist/index.html +15 -13
  116. package/apps/web/dist/mcp/flow.html +2 -2
  117. package/node_modules/@engenty/wizards-shared/dist/bundles.d.ts +219 -0
  118. package/node_modules/@engenty/wizards-shared/dist/bundles.js +19 -0
  119. package/node_modules/@engenty/wizards-shared/dist/connectors.d.ts +105 -4
  120. package/node_modules/@engenty/wizards-shared/dist/connectors.js +6 -0
  121. package/node_modules/@engenty/wizards-shared/dist/discover.d.ts +28 -1
  122. package/node_modules/@engenty/wizards-shared/dist/run.d.ts +8 -0
  123. package/node_modules/@engenty/wizards-shared/dist/run.js +16 -0
  124. package/node_modules/@engenty/wizards-shared/dist/rwx.d.ts +84 -1
  125. package/node_modules/@engenty/wizards-shared/dist/rwx.js +69 -1
  126. package/node_modules/@engenty/wizards-shared/dist/skills.d.ts +20 -3
  127. package/node_modules/@engenty/wizards-shared/dist/skills.js +5 -2
  128. package/package.json +1 -1
  129. package/plugin/.claude-plugin/plugin.json +1 -1
  130. package/plugin/plugin.json +1 -1
  131. package/apps/web/dist/assets/CapabilitiesPage-C1th2cZE.js +0 -2
  132. package/apps/web/dist/assets/EditorPage-56sIqEfF.js +0 -3
  133. package/apps/web/dist/assets/LangSwitch-CJ00IgaO.js +0 -1
  134. package/apps/web/dist/assets/Marketplace-D7-ot9Dz.js +0 -2
  135. package/apps/web/dist/assets/NewWizardPage-CytheHxX.js +0 -7
  136. package/apps/web/dist/assets/OnboardingPage-_91Pa8Ad.js +0 -1
  137. package/apps/web/dist/assets/PreviewPane-BDsYm-_K.js +0 -7
  138. package/apps/web/dist/assets/ProtocolSection-D_zaxHEL.js +0 -1
  139. package/apps/web/dist/assets/PublicRunner-CEgXSk3r.js +0 -1
  140. package/apps/web/dist/assets/SettingsPage-Dr8SDk-L.js +0 -2
  141. package/apps/web/dist/assets/SetupPage-B68n0l4I.js +0 -1
  142. package/apps/web/dist/assets/SharePage-DfznN7ar.js +0 -1
  143. package/apps/web/dist/assets/SkillEditor-BIcIczRc.js +0 -1
  144. package/apps/web/dist/assets/SpacePage-DiVTwhnJ.js +0 -2
  145. package/apps/web/dist/assets/changelog-a_CuplMX.js +0 -1
  146. package/apps/web/dist/assets/i18n-BaPKbQ1s.js +0 -55
  147. package/apps/web/dist/assets/index-CCUG6Vnh.css +0 -1
  148. package/apps/web/dist/assets/index-De0Wb42y.js +0 -10
  149. package/apps/web/dist/assets/session-BMiUrqHg.js +0 -2
@@ -1,7 +1,8 @@
1
1
  import { and, eq, gte, inArray, isNull, notLike, or } from "drizzle-orm";
2
2
  import { db, schema } from "../db/client.js";
3
3
  import { channelInGrant, grantOf } from "./grant.js";
4
- import { summarizeInput, writeLog } from "./log.js";
4
+ import { startOfToday, summarizeInput, writeLog } from "./log.js";
5
+ import { runOver } from "./over.js";
5
6
  /*
6
7
  * Sends from a guest's run (docs/plans/capabilities.md, A6; rule 7 of the connections plan): a
7
8
  * mail leaves only for an address the run itself collected — what the person typed into a page
@@ -45,8 +46,13 @@ export function guestPostCheck(post) {
45
46
  : { ok: false, refused: "channel_not_fixed", reason: FIXED_CHANNEL_ONLY };
46
47
  }
47
48
  const ADDRESS = /^[^\s@<>,;]+@[^\s@<>,;]+\.[^\s@<>,;]+$/;
48
- /** The addresses a send's input names: `to`, `cc`, `bcc`, `recipients`, as strings, lists or objects. */
49
- export function recipientsOf(input) {
49
+ /**
50
+ * The addresses a send's input names: the field the connector's action names as the recipient's
51
+ * (an imported connector: what an admin said, `body.to` for one inside an HTTP body), and `to`,
52
+ * `cc`, `bcc`, `recipients`, as strings, lists or objects. The named field comes on top of the
53
+ * guess, never in place of it: an address in `cc` is still one the send goes to.
54
+ */
55
+ export function recipientsOf(input, field) {
50
56
  if (!input || typeof input !== "object") {
51
57
  return [];
52
58
  }
@@ -71,6 +77,13 @@ export function recipientsOf(input) {
71
77
  take(o.email ?? o.address ?? o.emailAddress);
72
78
  }
73
79
  };
80
+ if (field) {
81
+ let value = input;
82
+ for (const part of field.split(".")) {
83
+ value = value && typeof value === "object" ? value[part] : null;
84
+ }
85
+ take(value);
86
+ }
74
87
  for (const key of ["to", "cc", "bcc", "recipients", "recipient"]) {
75
88
  take(input[key]);
76
89
  }
@@ -106,11 +119,6 @@ export function inMailDomains(address, domains) {
106
119
  return own && (domain === own || domain.endsWith(`.${own}`));
107
120
  }));
108
121
  }
109
- /** Today's first moment, as the per-day count sees it. */
110
- const startOfToday = () => {
111
- const now = new Date();
112
- return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
113
- };
114
122
  /**
115
123
  * How often guests sent through this run, and through this wizard today: mails and posts, a
116
124
  * guest's own and those of the fixed steps of guests' runs.
@@ -194,13 +202,13 @@ export async function guestSendCheck(where, send) {
194
202
  }
195
203
  /**
196
204
  * A mail from the server's own address, which no account holds: the one send that is not a
197
- * connector's action. A guest's run is bound by the rules above; the call is logged like any
198
- * other, under the connection `mail:server`.
205
+ * connector's action. A run that is over sends nothing (over.ts), asked first; a guest's run is
206
+ * bound by the rules above; the call is logged like any other, under the connection `mail:server`.
199
207
  */
200
208
  export async function brokerSend(call) {
201
209
  const started = Date.now();
202
210
  const { who, where } = call;
203
- const line = (outcome, reason, result) => writeLog({
211
+ const line = (outcome, reason, result, rule = "guest_send") => writeLog({
204
212
  at: new Date(started),
205
213
  durationMs: Date.now() - started,
206
214
  whoKind: who.kind,
@@ -215,11 +223,16 @@ export async function brokerSend(call) {
215
223
  kind: "send",
216
224
  inputSummary: summarizeInput(call.input),
217
225
  outcome,
218
- rule: "guest_send",
226
+ rule,
219
227
  reason,
220
228
  result,
221
229
  resultSize: null,
222
230
  });
231
+ const over = await runOver(where);
232
+ if (over) {
233
+ await line("deny", over.reason, "run_ended", over.rule);
234
+ return { ok: false, refused: "run_ended", reason: over.reason };
235
+ }
223
236
  let reason = "The server's address sends for members.";
224
237
  if (where.guest) {
225
238
  const check = await guestSendCheck(where, { recipients: call.recipients });
@@ -0,0 +1,133 @@
1
+ import { officialSkill } from "@engenty/wizards-shared/skills";
2
+ import { eq } from "drizzle-orm";
3
+ import { strToU8, zipSync } from "fflate";
4
+ import { db, schema } from "../db/client.js";
5
+ import { createGuardedFetch } from "../engenty/connections-external/net/guarded-fetch.js";
6
+ import { Archive, readPlugin } from "./read.js";
7
+ /**
8
+ * Entdecken's one hit for a Connector that brings skills (docs/plans/capabilities-next.md,
9
+ * piece 4). skills.sh names the repository each skill lives in. A repository whose root is a
10
+ * plugin for Claude Code or Cursor with an MCP server over http holds both: its skills and the
11
+ * service's connector are one thing to set up, not two. Only the plugin's manifest and MCP file
12
+ * are read, from GitHub's raw host through the guarded fetch, and kept for an hour.
13
+ *
14
+ * Not found this way: a plugin in a folder of its repository, and one whose manifest keeps its
15
+ * MCP servers in another file.
16
+ */
17
+ const RAW = (process.env.GITHUB_RAW_URL ?? "https://raw.githubusercontent.com").replace(/\/+$/, "");
18
+ /** The files that tell a plugin with a server, at a repository's root. */
19
+ const FILES = [
20
+ ".claude-plugin/plugin.json",
21
+ ".cursor-plugin/plugin.json",
22
+ ".mcp.json",
23
+ "mcp.json",
24
+ ];
25
+ /** How many repositories one search looks into. */
26
+ const REPOS = 6;
27
+ const WAIT_MS = 4000;
28
+ const KEEP_MS = 60 * 60_000;
29
+ const MAX_BYTES = 300_000;
30
+ const known = new Map();
31
+ async function fileOf(guarded, repo, path) {
32
+ try {
33
+ const res = await guarded(`${RAW}/${repo}/HEAD/${path}`, {
34
+ headers: { "user-agent": "engenty-wizards" },
35
+ signal: AbortSignal.timeout(WAIT_MS),
36
+ });
37
+ if (!res.ok) {
38
+ return null;
39
+ }
40
+ const text = await res.text();
41
+ return text.length > MAX_BYTES ? null : text;
42
+ }
43
+ catch {
44
+ return null;
45
+ }
46
+ }
47
+ /** The plugin at a repository's root, as far as its manifest and MCP file tell; null: none. */
48
+ async function repoPlugin(repo) {
49
+ const key = repo.toLowerCase();
50
+ const hit = known.get(key);
51
+ if (hit && Date.now() - hit.at < KEEP_MS) {
52
+ return hit.plugin;
53
+ }
54
+ const guarded = createGuardedFetch();
55
+ const texts = await Promise.all(FILES.map((path) => fileOf(guarded, repo, path)));
56
+ const files = {};
57
+ FILES.forEach((path, i) => {
58
+ const text = texts[i];
59
+ if (text !== null) {
60
+ files[path] = strToU8(text);
61
+ }
62
+ });
63
+ let plugin = null;
64
+ if (Object.keys(files).length) {
65
+ try {
66
+ const read = readPlugin(new Archive(zipSync(files)), {
67
+ fallbackName: repo.split("/")[1] ?? repo,
68
+ say: () => "",
69
+ });
70
+ plugin = "plugin" in read ? read.plugin : null;
71
+ }
72
+ catch {
73
+ // A manifest nobody can read is no plugin to offer.
74
+ }
75
+ }
76
+ if (known.size > 500) {
77
+ known.clear();
78
+ }
79
+ known.set(key, { at: Date.now(), plugin });
80
+ return plugin;
81
+ }
82
+ /** `owner/name` of a skill's address on skills.sh (`owner/name/skill`). */
83
+ const repoOf = (ref) => ref.split("/").slice(0, 2).join("/");
84
+ /**
85
+ * The repositories among a search's skills that are a plugin with a server over http, each with
86
+ * the hits it stands for: its skills, and the connectors of the service its server belongs to.
87
+ */
88
+ export async function discoverBundles(spaceId, skills, connectors) {
89
+ const repos = [...new Set(skills.map((s) => repoOf(s.ref)))]
90
+ .filter((r) => /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(r))
91
+ .slice(0, REPOS);
92
+ if (!repos.length) {
93
+ return [];
94
+ }
95
+ const [plugins, have] = await Promise.all([
96
+ Promise.all(repos.map(repoPlugin)),
97
+ db
98
+ .select({ origin: schema.spaceBundle.origin })
99
+ .from(schema.spaceBundle)
100
+ .where(eq(schema.spaceBundle.spaceId, spaceId)),
101
+ ]);
102
+ const added = new Set(have.flatMap((b) => (b.origin.kind === "github" ? [b.origin.repo.toLowerCase()] : [])));
103
+ const found = [];
104
+ repos.forEach((repo, i) => {
105
+ const plugin = plugins[i];
106
+ if (!plugin?.servers.length) {
107
+ return;
108
+ }
109
+ const hosts = [...new Set(plugin.servers.map((s) => new URL(s.url).hostname.toLowerCase()))];
110
+ found.push({
111
+ repo,
112
+ name: plugin.name,
113
+ description: plugin.description,
114
+ format: plugin.format,
115
+ hosts,
116
+ official: officialSkill(`${repo}/x`),
117
+ added: added.has(repo.toLowerCase()),
118
+ covers: {
119
+ skills: skills.filter((s) => repoOf(s.ref) === repo).map((s) => s.ref),
120
+ connectors: connectors
121
+ .map((c) => c.domain)
122
+ .filter((domain) => {
123
+ const d = domain
124
+ .trim()
125
+ .toLowerCase()
126
+ .replace(/^www\./, "");
127
+ return hosts.some((host) => host === d || host.endsWith(`.${d}`));
128
+ }),
129
+ },
130
+ });
131
+ });
132
+ return found;
133
+ }
@@ -0,0 +1,119 @@
1
+ import { createGuardedFetch } from "../engenty/connections-external/net/guarded-fetch.js";
2
+ import { ServiceError } from "../services/errors.js";
3
+ import { MAX_ARCHIVE_MB } from "./read.js";
4
+ /**
5
+ * A public repository on GitHub as a zip (docs/plans/capabilities-next.md, piece 4). The address
6
+ * is one a person copies from the browser: the repository, or a folder of it on a branch or tag.
7
+ * The archive comes from GitHub's own download host through the guarded fetch, which follows no
8
+ * redirect into a private network, and is refused before it is read whole where it is too large.
9
+ */
10
+ const refuse = (server, params) => new ServiceError("invalid", { server, params });
11
+ const PART = /^[A-Za-z0-9_.-]{1,100}$/;
12
+ /**
13
+ * A repository's address on GitHub: `https://github.com/owner/name`, with `.git` or without, and
14
+ * a folder as `…/tree/<branch>/<folder>`. A branch whose name has a slash cannot be told from a
15
+ * folder: its first part is read as the branch. Null for any other address.
16
+ */
17
+ export function parseRepoAddress(address) {
18
+ const typed = address.trim();
19
+ let url;
20
+ try {
21
+ url = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(typed) ? typed : `https://${typed}`);
22
+ }
23
+ catch {
24
+ return null;
25
+ }
26
+ if (!/^(www\.)?github\.com$/i.test(url.hostname) || !/^https?:$/.test(url.protocol)) {
27
+ return null;
28
+ }
29
+ const [owner, rawName, kind, ref, ...rest] = url.pathname.split("/").filter(Boolean);
30
+ const name = rawName?.replace(/\.git$/i, "");
31
+ if (!(owner && name && PART.test(owner) && PART.test(name))) {
32
+ return null;
33
+ }
34
+ const folder = kind === "tree" || kind === "blob";
35
+ let path;
36
+ try {
37
+ path = folder ? rest.map((part) => decodeURIComponent(part)) : [];
38
+ }
39
+ catch {
40
+ return null;
41
+ }
42
+ if (path.some((part) => part === ".." || part === "." || part.includes("\\"))) {
43
+ return null;
44
+ }
45
+ return {
46
+ repo: `${owner}/${name}`,
47
+ ref: folder && ref ? decodeURIComponent(ref) : null,
48
+ path: path.join("/"),
49
+ };
50
+ }
51
+ /** The address a person opens to look at the origin. */
52
+ export function repoUrl(address) {
53
+ const tail = address.ref || address.path
54
+ ? `/tree/${address.ref ?? "HEAD"}${address.path ? `/${address.path}` : ""}`
55
+ : "";
56
+ return `https://github.com/${address.repo}${tail}`;
57
+ }
58
+ /** The repository as a zip, as it is on its branch or tag now. */
59
+ export async function fetchRepoZip(address) {
60
+ const guarded = createGuardedFetch();
61
+ const url = `https://codeload.github.com/${address.repo}/zip/${encodeURIComponent(address.ref ?? "HEAD")}`;
62
+ let res;
63
+ try {
64
+ res = await guarded(url, {
65
+ headers: { accept: "application/zip", "user-agent": "engenty-wizards" },
66
+ signal: AbortSignal.timeout(45_000),
67
+ });
68
+ }
69
+ catch {
70
+ throw new ServiceError("refused", { server: "bundleRepoDown" });
71
+ }
72
+ if (res.status === 404) {
73
+ throw refuse("bundleRepoNotFound", { repo: address.repo });
74
+ }
75
+ if (!res.ok) {
76
+ throw new ServiceError("refused", { server: "bundleRepoDown" });
77
+ }
78
+ const max = MAX_ARCHIVE_MB * 1_000_000;
79
+ const tooLarge = () => refuse("bundleTooLarge", { mb: MAX_ARCHIVE_MB });
80
+ if (Number(res.headers.get("content-length")) > max) {
81
+ throw tooLarge();
82
+ }
83
+ const reader = res.body?.getReader();
84
+ if (!reader) {
85
+ const all = new Uint8Array(await res.arrayBuffer());
86
+ if (all.length > max) {
87
+ throw tooLarge();
88
+ }
89
+ return all;
90
+ }
91
+ const chunks = [];
92
+ let total = 0;
93
+ try {
94
+ while (true) {
95
+ const { done, value } = await reader.read();
96
+ if (done) {
97
+ break;
98
+ }
99
+ total += value.byteLength;
100
+ if (total > max) {
101
+ throw tooLarge();
102
+ }
103
+ chunks.push(value);
104
+ }
105
+ }
106
+ catch (err) {
107
+ await reader.cancel().catch(() => undefined);
108
+ throw err instanceof ServiceError
109
+ ? err
110
+ : new ServiceError("refused", { server: "bundleRepoDown" });
111
+ }
112
+ const all = new Uint8Array(total);
113
+ let at = 0;
114
+ for (const chunk of chunks) {
115
+ all.set(chunk, at);
116
+ at += chunk.byteLength;
117
+ }
118
+ return all;
119
+ }