@stigmer/plugin-package 0.1.0-dev.20261017020314.g644422724c27

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 (273) hide show
  1. package/LICENSE +194 -0
  2. package/README.md +72 -0
  3. package/client/archive.d.ts +27 -0
  4. package/client/archive.d.ts.map +1 -0
  5. package/client/archive.js +38 -0
  6. package/client/archive.js.map +1 -0
  7. package/client/builtin.d.ts +37 -0
  8. package/client/builtin.d.ts.map +1 -0
  9. package/client/builtin.js +49 -0
  10. package/client/builtin.js.map +1 -0
  11. package/client/ignore/defaults.d.ts +2 -0
  12. package/client/ignore/defaults.d.ts.map +1 -0
  13. package/client/ignore/defaults.js +140 -0
  14. package/client/ignore/defaults.js.map +1 -0
  15. package/client/ignore/match.d.ts +3 -0
  16. package/client/ignore/match.d.ts.map +1 -0
  17. package/client/ignore/match.js +166 -0
  18. package/client/ignore/match.js.map +1 -0
  19. package/client/ignore/matcher.d.ts +50 -0
  20. package/client/ignore/matcher.d.ts.map +1 -0
  21. package/client/ignore/matcher.js +116 -0
  22. package/client/ignore/matcher.js.map +1 -0
  23. package/client/ignore/pattern.d.ts +13 -0
  24. package/client/ignore/pattern.d.ts.map +1 -0
  25. package/client/ignore/pattern.js +119 -0
  26. package/client/ignore/pattern.js.map +1 -0
  27. package/client/prepare-eval.d.ts +62 -0
  28. package/client/prepare-eval.d.ts.map +1 -0
  29. package/client/prepare-eval.js +60 -0
  30. package/client/prepare-eval.js.map +1 -0
  31. package/client/prepare.d.ts +99 -0
  32. package/client/prepare.d.ts.map +1 -0
  33. package/client/prepare.js +89 -0
  34. package/client/prepare.js.map +1 -0
  35. package/client/presentation.d.ts +19 -0
  36. package/client/presentation.d.ts.map +1 -0
  37. package/client/presentation.js +44 -0
  38. package/client/presentation.js.map +1 -0
  39. package/client/refs.d.ts +76 -0
  40. package/client/refs.d.ts.map +1 -0
  41. package/client/refs.js +123 -0
  42. package/client/refs.js.map +1 -0
  43. package/client/release.d.ts +20 -0
  44. package/client/release.d.ts.map +1 -0
  45. package/client/release.js +28 -0
  46. package/client/release.js.map +1 -0
  47. package/client/reroot.d.ts +21 -0
  48. package/client/reroot.d.ts.map +1 -0
  49. package/client/reroot.js +37 -0
  50. package/client/reroot.js.map +1 -0
  51. package/client/select.d.ts +74 -0
  52. package/client/select.d.ts.map +1 -0
  53. package/client/select.js +121 -0
  54. package/client/select.js.map +1 -0
  55. package/client/vocabulary.d.ts +13 -0
  56. package/client/vocabulary.d.ts.map +1 -0
  57. package/client/vocabulary.js +17 -0
  58. package/client/vocabulary.js.map +1 -0
  59. package/client.d.ts +51 -0
  60. package/client.d.ts.map +1 -0
  61. package/client.js +51 -0
  62. package/client.js.map +1 -0
  63. package/detect.d.ts +83 -0
  64. package/detect.d.ts.map +1 -0
  65. package/detect.js +210 -0
  66. package/detect.js.map +1 -0
  67. package/dialects/claude.d.ts +34 -0
  68. package/dialects/claude.d.ts.map +1 -0
  69. package/dialects/claude.js +84 -0
  70. package/dialects/claude.js.map +1 -0
  71. package/dialects/codex.d.ts +20 -0
  72. package/dialects/codex.d.ts.map +1 -0
  73. package/dialects/codex.js +21 -0
  74. package/dialects/codex.js.map +1 -0
  75. package/dialects/cursor.d.ts +25 -0
  76. package/dialects/cursor.d.ts.map +1 -0
  77. package/dialects/cursor.js +66 -0
  78. package/dialects/cursor.js.map +1 -0
  79. package/dialects/manifest.d.ts +164 -0
  80. package/dialects/manifest.d.ts.map +1 -0
  81. package/dialects/manifest.js +256 -0
  82. package/dialects/manifest.js.map +1 -0
  83. package/dialects/open.d.ts +19 -0
  84. package/dialects/open.d.ts.map +1 -0
  85. package/dialects/open.js +49 -0
  86. package/dialects/open.js.map +1 -0
  87. package/documents.d.ts +58 -0
  88. package/documents.d.ts.map +1 -0
  89. package/documents.js +121 -0
  90. package/documents.js.map +1 -0
  91. package/evals/eval-dir.d.ts +59 -0
  92. package/evals/eval-dir.d.ts.map +1 -0
  93. package/evals/eval-dir.js +84 -0
  94. package/evals/eval-dir.js.map +1 -0
  95. package/evals/fields.d.ts +78 -0
  96. package/evals/fields.d.ts.map +1 -0
  97. package/evals/fields.js +156 -0
  98. package/evals/fields.js.map +1 -0
  99. package/evals/glob.d.ts +40 -0
  100. package/evals/glob.d.ts.map +1 -0
  101. package/evals/glob.js +146 -0
  102. package/evals/glob.js.map +1 -0
  103. package/evals/graders.d.ts +52 -0
  104. package/evals/graders.d.ts.map +1 -0
  105. package/evals/graders.js +279 -0
  106. package/evals/graders.js.map +1 -0
  107. package/evals/markdown.d.ts +24 -0
  108. package/evals/markdown.d.ts.map +1 -0
  109. package/evals/markdown.js +48 -0
  110. package/evals/markdown.js.map +1 -0
  111. package/evals/mocks.d.ts +50 -0
  112. package/evals/mocks.d.ts.map +1 -0
  113. package/evals/mocks.js +182 -0
  114. package/evals/mocks.js.map +1 -0
  115. package/evals/read-eval-suite.d.ts +110 -0
  116. package/evals/read-eval-suite.d.ts.map +1 -0
  117. package/evals/read-eval-suite.js +603 -0
  118. package/evals/read-eval-suite.js.map +1 -0
  119. package/evals/summary.d.ts +61 -0
  120. package/evals/summary.d.ts.map +1 -0
  121. package/evals/summary.js +69 -0
  122. package/evals/summary.js.map +1 -0
  123. package/evals/types.d.ts +203 -0
  124. package/evals/types.d.ts.map +1 -0
  125. package/evals/types.js +21 -0
  126. package/evals/types.js.map +1 -0
  127. package/files.d.ts +135 -0
  128. package/files.d.ts.map +1 -0
  129. package/files.js +209 -0
  130. package/files.js.map +1 -0
  131. package/frontmatter.d.ts +51 -0
  132. package/frontmatter.d.ts.map +1 -0
  133. package/frontmatter.js +61 -0
  134. package/frontmatter.js.map +1 -0
  135. package/index.d.ts +37 -0
  136. package/index.d.ts.map +1 -0
  137. package/index.js +33 -0
  138. package/index.js.map +1 -0
  139. package/marketplace/messages.d.ts +20 -0
  140. package/marketplace/messages.d.ts.map +1 -0
  141. package/marketplace/messages.js +49 -0
  142. package/marketplace/messages.js.map +1 -0
  143. package/marketplace/outcome.d.ts +70 -0
  144. package/marketplace/outcome.d.ts.map +1 -0
  145. package/marketplace/outcome.js +29 -0
  146. package/marketplace/outcome.js.map +1 -0
  147. package/marketplace/read-marketplace.d.ts +52 -0
  148. package/marketplace/read-marketplace.d.ts.map +1 -0
  149. package/marketplace/read-marketplace.js +351 -0
  150. package/marketplace/read-marketplace.js.map +1 -0
  151. package/messages.d.ts +55 -0
  152. package/messages.d.ts.map +1 -0
  153. package/messages.js +167 -0
  154. package/messages.js.map +1 -0
  155. package/normalise/hooks.d.ts +91 -0
  156. package/normalise/hooks.d.ts.map +1 -0
  157. package/normalise/hooks.js +437 -0
  158. package/normalise/hooks.js.map +1 -0
  159. package/normalise/ignored.d.ts +34 -0
  160. package/normalise/ignored.d.ts.map +1 -0
  161. package/normalise/ignored.js +78 -0
  162. package/normalise/ignored.js.map +1 -0
  163. package/normalise/mcp-servers.d.ts +50 -0
  164. package/normalise/mcp-servers.d.ts.map +1 -0
  165. package/normalise/mcp-servers.js +398 -0
  166. package/normalise/mcp-servers.js.map +1 -0
  167. package/normalise/settings.d.ts +32 -0
  168. package/normalise/settings.d.ts.map +1 -0
  169. package/normalise/settings.js +72 -0
  170. package/normalise/settings.js.map +1 -0
  171. package/normalise/skills.d.ts +39 -0
  172. package/normalise/skills.d.ts.map +1 -0
  173. package/normalise/skills.js +135 -0
  174. package/normalise/skills.js.map +1 -0
  175. package/normalise/stigmer-folder.d.ts +17 -0
  176. package/normalise/stigmer-folder.d.ts.map +1 -0
  177. package/normalise/stigmer-folder.js +19 -0
  178. package/normalise/stigmer-folder.js.map +1 -0
  179. package/normalise/sub-agents.d.ts +54 -0
  180. package/normalise/sub-agents.d.ts.map +1 -0
  181. package/normalise/sub-agents.js +219 -0
  182. package/normalise/sub-agents.js.map +1 -0
  183. package/normalise/variables.d.ts +31 -0
  184. package/normalise/variables.d.ts.map +1 -0
  185. package/normalise/variables.js +170 -0
  186. package/normalise/variables.js.map +1 -0
  187. package/outcome.d.ts +59 -0
  188. package/outcome.d.ts.map +1 -0
  189. package/outcome.js +32 -0
  190. package/outcome.js.map +1 -0
  191. package/package.json +47 -0
  192. package/placeholders.d.ts +65 -0
  193. package/placeholders.d.ts.map +1 -0
  194. package/placeholders.js +99 -0
  195. package/placeholders.js.map +1 -0
  196. package/presentation.d.ts +53 -0
  197. package/presentation.d.ts.map +1 -0
  198. package/presentation.js +156 -0
  199. package/presentation.js.map +1 -0
  200. package/read-plugin-package.d.ts +20 -0
  201. package/read-plugin-package.d.ts.map +1 -0
  202. package/read-plugin-package.js +73 -0
  203. package/read-plugin-package.js.map +1 -0
  204. package/src/client/archive.ts +44 -0
  205. package/src/client/builtin.ts +60 -0
  206. package/src/client/ignore/defaults.ts +155 -0
  207. package/src/client/ignore/match.ts +158 -0
  208. package/src/client/ignore/matcher.ts +156 -0
  209. package/src/client/ignore/pattern.ts +132 -0
  210. package/src/client/prepare-eval.ts +92 -0
  211. package/src/client/prepare.ts +160 -0
  212. package/src/client/presentation.ts +47 -0
  213. package/src/client/refs.ts +170 -0
  214. package/src/client/release.ts +29 -0
  215. package/src/client/reroot.ts +35 -0
  216. package/src/client/select.ts +161 -0
  217. package/src/client/vocabulary.ts +19 -0
  218. package/src/client.ts +102 -0
  219. package/src/detect.ts +245 -0
  220. package/src/dialects/claude.ts +102 -0
  221. package/src/dialects/codex.ts +26 -0
  222. package/src/dialects/cursor.ts +77 -0
  223. package/src/dialects/manifest.ts +316 -0
  224. package/src/dialects/open.ts +50 -0
  225. package/src/documents.ts +158 -0
  226. package/src/evals/eval-dir.ts +101 -0
  227. package/src/evals/fields.ts +184 -0
  228. package/src/evals/glob.ts +141 -0
  229. package/src/evals/graders.ts +293 -0
  230. package/src/evals/markdown.ts +56 -0
  231. package/src/evals/mocks.ts +202 -0
  232. package/src/evals/read-eval-suite.ts +713 -0
  233. package/src/evals/summary.ts +100 -0
  234. package/src/evals/types.ts +217 -0
  235. package/src/files.ts +235 -0
  236. package/src/frontmatter.ts +70 -0
  237. package/src/index.ts +129 -0
  238. package/src/marketplace/messages.ts +61 -0
  239. package/src/marketplace/outcome.ts +91 -0
  240. package/src/marketplace/read-marketplace.ts +388 -0
  241. package/src/messages.ts +248 -0
  242. package/src/normalise/hooks.ts +469 -0
  243. package/src/normalise/ignored.ts +79 -0
  244. package/src/normalise/mcp-servers.ts +423 -0
  245. package/src/normalise/settings.ts +81 -0
  246. package/src/normalise/skills.ts +145 -0
  247. package/src/normalise/stigmer-folder.ts +23 -0
  248. package/src/normalise/sub-agents.ts +231 -0
  249. package/src/normalise/variables.ts +181 -0
  250. package/src/outcome.ts +158 -0
  251. package/src/placeholders.ts +110 -0
  252. package/src/presentation.ts +170 -0
  253. package/src/read-plugin-package.ts +80 -0
  254. package/src/testing.ts +347 -0
  255. package/src/tool-names.ts +30 -0
  256. package/src/trim.ts +10 -0
  257. package/src/types.ts +214 -0
  258. package/testing.d.ts +127 -0
  259. package/testing.d.ts.map +1 -0
  260. package/testing.js +266 -0
  261. package/testing.js.map +1 -0
  262. package/tool-names.d.ts +21 -0
  263. package/tool-names.d.ts.map +1 -0
  264. package/tool-names.js +28 -0
  265. package/tool-names.js.map +1 -0
  266. package/trim.d.ts +7 -0
  267. package/trim.d.ts.map +1 -0
  268. package/trim.js +12 -0
  269. package/trim.js.map +1 -0
  270. package/types.d.ts +179 -0
  271. package/types.d.ts.map +1 -0
  272. package/types.js +19 -0
  273. package/types.js.map +1 -0
@@ -0,0 +1,170 @@
1
+ /**
2
+ * What a storefront shows for a plugin before anyone installs it: a display
3
+ * name, a logo, an author, a version, a description, a category.
4
+ *
5
+ * This is a read apart from `readPluginPackage` on purpose. The full read
6
+ * decides what Stigmer INSTALLS and refuses what it cannot; a card must
7
+ * render for every entry a catalogue lists, refused or not, and must cost
8
+ * one manifest, not the plugin's whole tree. So this reader opens only the
9
+ * manifests present, takes what they say about appearance, and never
10
+ * reports a finding: an unreadable manifest contributes nothing, a wrong
11
+ * type is treated as absent, and the caller draws a fallback face.
12
+ *
13
+ * Which manifest speaks is the full reader's rule (`MANIFEST_PRECEDENCE`:
14
+ * the root manifest, then Claude, Cursor, Codex), so a plugin shipping two
15
+ * dialects is named the same way on a card and in an install. Identity
16
+ * fields (`name`, `version`, `description`, `author`) come from the first
17
+ * manifest that carries each; the presentation fields come from the
18
+ * dialects that define them: Cursor's top-level `displayName`, `logo` and
19
+ * `category`; the legacy Codex manifest's `interface.displayName`,
20
+ * `interface.logo` and `interface.category`; the open format's
21
+ * `extensions["ai.stigmer"]` with the same three names, the one place that
22
+ * format leaves a client for them (Stigmer's authored catalogue plugins are
23
+ * its first users). Claude Code's manifest defines none.
24
+ *
25
+ * A logo is a path inside the plugin, and the card that shows it fetches it
26
+ * by URL from wherever the tree lives. So the path is kept only when it is
27
+ * contained, names an image by extension, is LISTED in the tree, and its
28
+ * declared size is under `PLUGIN_DOCUMENT_LIMITS.logo`; the library's rule
29
+ * that a cap is checked from declared sizes before a byte moves holds here
30
+ * too. Both spellings the vendors use (`assets/logo.png`, `./assets/logo.png`)
31
+ * are accepted, as the marketplace reader accepts both for a source.
32
+ *
33
+ * `logo` stays an ignored component in the full read: nothing in an
34
+ * Organization carries the image after an install, and the preview's "Not
35
+ * installed" line is true until the stored plugin does.
36
+ */
37
+
38
+ import { MANIFEST_PRECEDENCE } from "./detect.js";
39
+ import { stigmerExtensionOf } from "./dialects/manifest.js";
40
+ import { decodeUtf8, isContainedPath, PLUGIN_DOCUMENT_LIMITS, PluginFileIndex, type PluginFiles } from "./files.js";
41
+ import { isJsonObject, type JsonObject } from "./documents.js";
42
+ import { MANIFEST_LOCATIONS } from "./messages.js";
43
+ import type { PluginAuthor, PluginDialect } from "./types.js";
44
+
45
+ /** What a card shows for a plugin; every field optional, because every dialect may omit it. */
46
+ export interface PluginPresentation {
47
+ /** The name for people (`Thermos`); the install name stays `name`. */
48
+ readonly displayName?: string;
49
+ /** A plugin-relative path to an image the tree lists, verified as described in the module header. */
50
+ readonly logo?: string;
51
+ readonly author?: PluginAuthor;
52
+ readonly version?: string;
53
+ readonly description?: string;
54
+ /** The catalogue's own grouping word (`developer-tools`, `Productivity`), as written. */
55
+ readonly category?: string;
56
+ }
57
+
58
+ /** The image extensions a browser renders in an `<img>`; a logo named otherwise is not shown. */
59
+ const LOGO_EXTENSIONS: ReadonlySet<string> = new Set(["png", "svg", "jpg", "jpeg", "webp", "gif"]);
60
+
61
+ /** The appearance fields of every manifest present, merged in precedence order. */
62
+ export function readPluginPresentation(files: PluginFiles): PluginPresentation {
63
+ const index = new PluginFileIndex(files);
64
+ const presentation: { -readonly [K in keyof PluginPresentation]: PluginPresentation[K] } = {};
65
+
66
+ for (const dialect of MANIFEST_PRECEDENCE) {
67
+ const object = readManifest(index, MANIFEST_LOCATIONS[dialect]);
68
+ if (object === undefined) continue;
69
+ const contribution = contributionOf(dialect, object, index);
70
+ for (const key of Object.keys(contribution) as (keyof PluginPresentation)[]) {
71
+ if (presentation[key] === undefined) Object.assign(presentation, { [key]: contribution[key] });
72
+ }
73
+ }
74
+
75
+ return presentation;
76
+ }
77
+
78
+ /** The manifest at `path` as an object, or `undefined` for anything a card should not stumble on. */
79
+ function readManifest(index: PluginFileIndex, path: string): JsonObject | undefined {
80
+ const entry = index.entry(path);
81
+ if (entry === undefined || entry.size > PLUGIN_DOCUMENT_LIMITS.manifest) return undefined;
82
+ let bytes: Uint8Array;
83
+ try {
84
+ bytes = index.files.read(path);
85
+ } catch {
86
+ return undefined;
87
+ }
88
+ if (bytes.length > PLUGIN_DOCUMENT_LIMITS.manifest) return undefined;
89
+ let value: unknown;
90
+ try {
91
+ value = JSON.parse(decodeUtf8(bytes));
92
+ } catch {
93
+ return undefined;
94
+ }
95
+ return isJsonObject(value) ? value : undefined;
96
+ }
97
+
98
+ function contributionOf(dialect: PluginDialect, object: JsonObject, index: PluginFileIndex): PluginPresentation {
99
+ const identity = identityOf(object);
100
+ switch (dialect) {
101
+ case "cursor":
102
+ return { ...identity, ...appearanceOf(object, index) };
103
+ case "codex": {
104
+ const surface = object["interface"];
105
+ return isJsonObject(surface) ? { ...identity, ...appearanceOf(surface, index) } : identity;
106
+ }
107
+ case "agent-plugins": {
108
+ const extension = stigmerExtensionOf(object);
109
+ return extension === undefined ? identity : { ...identity, ...appearanceOf(extension, index) };
110
+ }
111
+ case "claude":
112
+ return identity;
113
+ default: {
114
+ const exhaustive: never = dialect;
115
+ return exhaustive;
116
+ }
117
+ }
118
+ }
119
+
120
+ /** The identity fields every dialect shares, typed loosely: a wrong type is absent, never a finding. */
121
+ function identityOf(object: JsonObject): PluginPresentation {
122
+ const author = authorOf(object["author"]);
123
+ return {
124
+ ...optional("version", object["version"]),
125
+ ...optional("description", object["description"]),
126
+ ...(author !== undefined && { author }),
127
+ };
128
+ }
129
+
130
+ /** `displayName`, `logo` and `category` from an object that carries them at its top level (a Cursor manifest, a Codex `interface`, an open manifest's `extensions["ai.stigmer"]`). */
131
+ function appearanceOf(object: JsonObject, index: PluginFileIndex): PluginPresentation {
132
+ const logo = logoPath(object["logo"], index);
133
+ return {
134
+ ...optional("displayName", object["displayName"]),
135
+ ...optional("category", object["category"]),
136
+ ...(logo !== undefined && { logo }),
137
+ };
138
+ }
139
+
140
+ function authorOf(value: unknown): PluginAuthor | undefined {
141
+ if (!isJsonObject(value)) return undefined;
142
+ const author: { -readonly [K in keyof PluginAuthor]: PluginAuthor[K] } = {
143
+ ...optional("name", value["name"]),
144
+ ...optional("email", value["email"]),
145
+ ...optional("url", value["url"]),
146
+ };
147
+ return Object.keys(author).length === 0 ? undefined : author;
148
+ }
149
+
150
+ /**
151
+ * A declared logo as a plugin-relative path the tree lists, or nothing.
152
+ * `./` is stripped (Codex's spelling); an absolute path, a `..` segment, a
153
+ * URL, a non-image extension, an unlisted file or an over-cap file all
154
+ * mean "no logo", never a refusal.
155
+ */
156
+ function logoPath(value: unknown, index: PluginFileIndex): string | undefined {
157
+ if (typeof value !== "string") return undefined;
158
+ const path = value.startsWith("./") ? value.slice(2) : value;
159
+ if (!isContainedPath(path)) return undefined;
160
+ const dot = path.lastIndexOf(".");
161
+ if (dot === -1 || !LOGO_EXTENSIONS.has(path.slice(dot + 1).toLowerCase())) return undefined;
162
+ const entry = index.entry(path);
163
+ if (entry === undefined || entry.size > PLUGIN_DOCUMENT_LIMITS.logo) return undefined;
164
+ return path;
165
+ }
166
+
167
+ /** `{ [key]: value }` when `value` is a non-empty string; `{}` otherwise. */
168
+ function optional<K extends string>(key: K, value: unknown): Partial<Record<K, string>> {
169
+ return typeof value === "string" && value !== "" ? ({ [key]: value } as Record<K, string>) : {};
170
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * The one entry: a `PluginFiles` in, a `PluginReadOutcome` out.
3
+ *
4
+ * The read is a fixed sequence, each stage a pure function over the index
5
+ * and the stages before it: containment of the listed paths; manifest
6
+ * detection and identity; skills; MCP servers; hooks; variables (which
7
+ * need the servers' and the hooks' references); sub-agents (which need the
8
+ * skills' names); the main agent a Claude plugin's settings name (which
9
+ * needs the sub-agents); the `ai.stigmer/` folder, which is read by
10
+ * nothing and warned; the ignored components. Every stage records findings and keeps going, so the outcome
11
+ * names every problem in the package at once, and the plugin is returned
12
+ * only when no stage refused.
13
+ *
14
+ * Nothing here throws on a plugin's content. A throw means a reader broke
15
+ * its contract (a listed path that cannot be read) or the library has a bug.
16
+ */
17
+
18
+ import { detectManifests } from "./detect.js";
19
+ import { isContainedPath, PluginFileIndex, type PluginFiles } from "./files.js";
20
+ import { Findings } from "./messages.js";
21
+ import { normaliseHooks } from "./normalise/hooks.js";
22
+ import { dedupeIgnored, ignoredOnDisk } from "./normalise/ignored.js";
23
+ import { normaliseMcpServers } from "./normalise/mcp-servers.js";
24
+ import { normaliseMainAgent, readsSettings } from "./normalise/settings.js";
25
+ import { normaliseSkills } from "./normalise/skills.js";
26
+ import { warnStigmerFolder } from "./normalise/stigmer-folder.js";
27
+ import { normaliseSubAgents } from "./normalise/sub-agents.js";
28
+ import { normaliseVariables } from "./normalise/variables.js";
29
+ import type { PluginReadOutcome } from "./outcome.js";
30
+ import type { PluginPackage } from "./types.js";
31
+
32
+ export function readPluginPackage(files: PluginFiles): PluginReadOutcome {
33
+ const findings = new Findings();
34
+
35
+ for (const entry of files.entries) {
36
+ if (!isContainedPath(entry.path)) findings.error("path-uncontained", { path: entry.path });
37
+ }
38
+ const index = new PluginFileIndex(files);
39
+
40
+ const set = detectManifests(index, findings);
41
+ if (set === undefined) {
42
+ return { ok: false, errors: findings.errors, warnings: findings.warnings };
43
+ }
44
+
45
+ const skills = normaliseSkills(index, set, findings);
46
+ const servers = normaliseMcpServers(index, set.mcpSources, findings);
47
+ const mcpServers = servers.servers;
48
+ const hooks = normaliseHooks(index, set, findings);
49
+ const variables = normaliseVariables(set, servers, hooks.references, findings);
50
+ const subAgents = normaliseSubAgents(index, set, skills, findings);
51
+ const mainAgent = normaliseMainAgent(index, set, subAgents, findings);
52
+ warnStigmerFolder(index, findings);
53
+ const ignored = dedupeIgnored([ignoredOnDisk(index, set.readsAgents, readsSettings(set)), ...set.manifests.map((m) => m.ignored)]);
54
+
55
+ if (findings.errors.length > 0 || set.name === undefined) {
56
+ return { ok: false, errors: findings.errors, warnings: findings.warnings };
57
+ }
58
+
59
+ const identity = set.manifests[0]?.identity ?? {};
60
+ const plugin: PluginPackage = {
61
+ name: set.name,
62
+ ...(identity.version !== undefined && { version: identity.version }),
63
+ ...(identity.description !== undefined && { description: identity.description }),
64
+ ...(identity.author !== undefined && { author: identity.author }),
65
+ ...(identity.homepage !== undefined && { homepage: identity.homepage }),
66
+ ...(identity.repository !== undefined && { repository: identity.repository }),
67
+ ...(identity.license !== undefined && { license: identity.license }),
68
+ keywords: identity.keywords ?? [],
69
+ dialect: set.dialect,
70
+ manifestsFound: set.manifests.map((m) => m.path),
71
+ skills,
72
+ mcpServers,
73
+ subAgents,
74
+ variables,
75
+ ...(hooks.hooks !== undefined && { hooks: hooks.hooks }),
76
+ ...(mainAgent !== undefined && { mainAgent }),
77
+ ignored,
78
+ };
79
+ return { ok: true, plugin, warnings: findings.warnings };
80
+ }
package/src/testing.ts ADDED
@@ -0,0 +1,347 @@
1
+ /**
2
+ * In-memory plugin builders for tests (exported as
3
+ * `@stigmer/plugin-package/testing`).
4
+ *
5
+ * One builder per dialect, each writing the exact file layout that
6
+ * dialect's tools produce: `openPlugin` a root `plugin.json` with the
7
+ * canonical `$schema` and an `mcp.json` beside it; `claudePlugin` a
8
+ * `.claude-plugin/plugin.json` with `.mcp.json`, `hooks/hooks.json` and
9
+ * `settings.json`; `cursorPlugin` a `.cursor-plugin/plugin.json` declaring
10
+ * `./skills/`, `./agents/`, `./mcp.json` and `./hooks/hooks.json` the way
11
+ * every published Cursor plugin does; `codexPlugin` the legacy
12
+ * `.codex-plugin/plugin.json` compatibility layout. A `hooks` option is the
13
+ * event map; each builder wraps it in its format's file shape. The
14
+ * adversarial suite starts from a valid plugin and breaks exactly one
15
+ * thing with the mutators (`withFile`, `withoutFile`,
16
+ * `withManifestField`), so each test reads as "this plugin, minus this",
17
+ * and the consumers' tests (the CLI's, the server's) craft their fixtures
18
+ * from the same source so the shapes never drift apart.
19
+ *
20
+ * Builders return a path -> content map; `inMemoryPluginFiles` turns one
21
+ * into the `PluginFiles` the reader takes. Zipping a fixture for an
22
+ * archive-based consumer is `@stigmer/zip-structure/testing`'s job.
23
+ *
24
+ * `GLOB_VALIDITY_TABLE` is the one table of eval globs, well formed or not
25
+ * with the sentence `globError` answers, that this library's test and the
26
+ * server's matcher test both read, so the two readers of the grammar
27
+ * cannot drift apart.
28
+ */
29
+
30
+ import { stringify as stringifyYaml } from "yaml";
31
+
32
+ import { inMemoryPluginFiles } from "./files.js";
33
+ import { AGENT_PLUGINS_MANIFEST_SCHEMA, AGENT_PLUGINS_MCP_SCHEMA, MANIFEST_LOCATIONS } from "./messages.js";
34
+
35
+ export { inMemoryPluginFiles };
36
+
37
+ /** A plugin as files: plugin-relative path -> content. */
38
+ export type PluginFixture = Map<string, string | Uint8Array>;
39
+
40
+ export interface SkillFixture {
41
+ /** The frontmatter `name`; also the directory name unless `dir` says otherwise. */
42
+ readonly name: string;
43
+ readonly description?: string;
44
+ /** Directory under `skills/` (or the declared skills root); defaults to `name`. */
45
+ readonly dir?: string;
46
+ /** Extra frontmatter fields, or `null` for a `SKILL.md` with no frontmatter at all. */
47
+ readonly frontmatter?: Readonly<Record<string, unknown>> | null;
48
+ readonly body?: string;
49
+ /** Extra files inside the skill directory, relative to it. */
50
+ readonly files?: Readonly<Record<string, string>>;
51
+ }
52
+
53
+ export interface AgentFixture {
54
+ /** File name under `agents/`, without `.md`. */
55
+ readonly file: string;
56
+ /** Frontmatter fields, or `null` for a bare prompt with no frontmatter. */
57
+ readonly frontmatter?: Readonly<Record<string, unknown>> | null;
58
+ readonly body?: string;
59
+ }
60
+
61
+ interface CommonFixture {
62
+ readonly name?: string;
63
+ readonly version?: string;
64
+ readonly description?: string;
65
+ /** Extra or overriding manifest fields, merged last. */
66
+ readonly manifest?: Readonly<Record<string, unknown>>;
67
+ readonly skills?: readonly SkillFixture[];
68
+ /** Extra files anywhere in the plugin. */
69
+ readonly files?: Readonly<Record<string, string | Uint8Array>>;
70
+ }
71
+
72
+ export interface OpenPluginFixture extends CommonFixture {
73
+ /** The manifest `$schema`; `null` omits it. Defaults to the canonical 1.0.0 identifier. */
74
+ readonly schema?: string | null;
75
+ /** The `mcpServers` map for `mcp.json`; absent means no `mcp.json`. */
76
+ readonly mcpServers?: Readonly<Record<string, unknown>>;
77
+ /** The `mcp.json` `$schema`; `null` omits it. */
78
+ readonly mcpSchema?: string | null;
79
+ /** Extra top-level `mcp.json` fields. */
80
+ readonly mcpConfig?: Readonly<Record<string, unknown>>;
81
+ }
82
+
83
+ export interface ClaudePluginFixture extends CommonFixture {
84
+ readonly agents?: readonly AgentFixture[];
85
+ /** The `mcpServers` map for `.mcp.json`; absent means no `.mcp.json`. */
86
+ readonly mcpServers?: Readonly<Record<string, unknown>>;
87
+ readonly userConfig?: Readonly<Record<string, unknown>>;
88
+ /** The event map for `hooks/hooks.json`, written as `{"hooks": <map>}`; absent means no file. */
89
+ readonly hooks?: Readonly<Record<string, unknown>>;
90
+ /** The `settings.json` object; absent means no file. */
91
+ readonly settings?: Readonly<Record<string, unknown>>;
92
+ }
93
+
94
+ export interface CursorPluginFixture extends CommonFixture {
95
+ readonly agents?: readonly AgentFixture[];
96
+ /** The `mcpServers` map for `mcp.json`, declared as `"mcpServers": "./mcp.json"`. */
97
+ readonly mcpServers?: Readonly<Record<string, unknown>>;
98
+ /** The `variables` JSON Schema: property name -> `{ type, title, description }`. */
99
+ readonly variables?: Readonly<Record<string, Readonly<Record<string, unknown>>>>;
100
+ readonly required?: readonly string[];
101
+ /** The event map for `hooks/hooks.json`, written as `{"version": 1, "hooks": <map>}` and declared. */
102
+ readonly hooks?: Readonly<Record<string, unknown>>;
103
+ }
104
+
105
+ export interface CodexPluginFixture extends CommonFixture {
106
+ /** The `mcpServers` map for `.mcp.json`, declared as `"mcpServers": "./.mcp.json"`. */
107
+ readonly mcpServers?: Readonly<Record<string, unknown>>;
108
+ /** The `apps` map for `.app.json`. */
109
+ readonly apps?: Readonly<Record<string, unknown>>;
110
+ /** The event map for `hooks/hooks.json`, written as `{"hooks": <map>}`; absent means no file. */
111
+ readonly hooks?: Readonly<Record<string, unknown>>;
112
+ }
113
+
114
+ const DEFAULT_NAME = "example";
115
+
116
+ /** An Agent Plugins 1.0.0 package. */
117
+ export function openPlugin(fixture: OpenPluginFixture = {}): PluginFixture {
118
+ const files: PluginFixture = new Map();
119
+ const manifest: Record<string, unknown> = {
120
+ ...(fixture.schema !== null && { $schema: fixture.schema ?? AGENT_PLUGINS_MANIFEST_SCHEMA }),
121
+ name: fixture.name ?? DEFAULT_NAME,
122
+ ...(fixture.version !== undefined && { version: fixture.version }),
123
+ ...(fixture.description !== undefined && { description: fixture.description }),
124
+ ...fixture.manifest,
125
+ };
126
+ files.set(MANIFEST_LOCATIONS["agent-plugins"], json(manifest));
127
+ if (fixture.mcpServers !== undefined) {
128
+ files.set(
129
+ "mcp.json",
130
+ json({
131
+ ...(fixture.mcpSchema !== null && { $schema: fixture.mcpSchema ?? AGENT_PLUGINS_MCP_SCHEMA }),
132
+ mcpServers: fixture.mcpServers,
133
+ ...fixture.mcpConfig,
134
+ }),
135
+ );
136
+ }
137
+ writeSkills(files, "skills", fixture.skills);
138
+ writeExtra(files, fixture.files);
139
+ return files;
140
+ }
141
+
142
+ /** A Claude Code plugin. */
143
+ export function claudePlugin(fixture: ClaudePluginFixture = {}): PluginFixture {
144
+ const files: PluginFixture = new Map();
145
+ const manifest: Record<string, unknown> = {
146
+ name: fixture.name ?? DEFAULT_NAME,
147
+ ...(fixture.version !== undefined && { version: fixture.version }),
148
+ ...(fixture.description !== undefined && { description: fixture.description }),
149
+ ...(fixture.userConfig !== undefined && { userConfig: fixture.userConfig }),
150
+ ...fixture.manifest,
151
+ };
152
+ files.set(MANIFEST_LOCATIONS.claude, json(manifest));
153
+ if (fixture.mcpServers !== undefined) files.set(".mcp.json", json({ mcpServers: fixture.mcpServers }));
154
+ if (fixture.hooks !== undefined) files.set("hooks/hooks.json", json({ hooks: fixture.hooks }));
155
+ if (fixture.settings !== undefined) files.set("settings.json", json(fixture.settings));
156
+ writeSkills(files, "skills", fixture.skills);
157
+ writeAgents(files, "agents", fixture.agents);
158
+ writeExtra(files, fixture.files);
159
+ return files;
160
+ }
161
+
162
+ /** A Cursor plugin, declaring its components the way the published catalogue does. */
163
+ export function cursorPlugin(fixture: CursorPluginFixture = {}): PluginFixture {
164
+ const files: PluginFixture = new Map();
165
+ const manifest: Record<string, unknown> = {
166
+ name: fixture.name ?? DEFAULT_NAME,
167
+ ...(fixture.version !== undefined && { version: fixture.version }),
168
+ ...(fixture.description !== undefined && { description: fixture.description }),
169
+ ...(fixture.skills !== undefined && { skills: "./skills/" }),
170
+ ...(fixture.agents !== undefined && { agents: "./agents/" }),
171
+ ...(fixture.variables !== undefined && {
172
+ variables: {
173
+ type: "object",
174
+ properties: fixture.variables,
175
+ ...(fixture.required !== undefined && { required: fixture.required }),
176
+ },
177
+ }),
178
+ ...(fixture.mcpServers !== undefined && { mcpServers: "./mcp.json" }),
179
+ ...(fixture.hooks !== undefined && { hooks: "./hooks/hooks.json" }),
180
+ ...fixture.manifest,
181
+ };
182
+ files.set(MANIFEST_LOCATIONS.cursor, json(manifest));
183
+ if (fixture.mcpServers !== undefined) files.set("mcp.json", json({ mcpServers: fixture.mcpServers }));
184
+ if (fixture.hooks !== undefined) files.set("hooks/hooks.json", json({ version: 1, hooks: fixture.hooks }));
185
+ writeSkills(files, "skills", fixture.skills);
186
+ writeAgents(files, "agents", fixture.agents);
187
+ writeExtra(files, fixture.files);
188
+ return files;
189
+ }
190
+
191
+ /** The legacy Codex compatibility layout (`.codex-plugin/plugin.json`, `.mcp.json`, `.app.json`). */
192
+ export function codexPlugin(fixture: CodexPluginFixture = {}): PluginFixture {
193
+ const files: PluginFixture = new Map();
194
+ const manifest: Record<string, unknown> = {
195
+ name: fixture.name ?? DEFAULT_NAME,
196
+ ...(fixture.version !== undefined && { version: fixture.version }),
197
+ ...(fixture.description !== undefined && { description: fixture.description }),
198
+ ...(fixture.skills !== undefined && { skills: "./skills/" }),
199
+ ...(fixture.mcpServers !== undefined && { mcpServers: "./.mcp.json" }),
200
+ ...(fixture.apps !== undefined && { apps: "./.app.json" }),
201
+ ...fixture.manifest,
202
+ };
203
+ files.set(MANIFEST_LOCATIONS.codex, json(manifest));
204
+ if (fixture.mcpServers !== undefined) files.set(".mcp.json", json({ mcpServers: fixture.mcpServers }));
205
+ if (fixture.apps !== undefined) files.set(".app.json", json({ apps: fixture.apps }));
206
+ if (fixture.hooks !== undefined) files.set("hooks/hooks.json", json({ hooks: fixture.hooks }));
207
+ writeSkills(files, "skills", fixture.skills);
208
+ writeExtra(files, fixture.files);
209
+ return files;
210
+ }
211
+
212
+ /** A copy of `files` with `path` set to `content`. */
213
+ export function withFile(files: PluginFixture, path: string, content: string | Uint8Array): PluginFixture {
214
+ const copy = new Map(files);
215
+ copy.set(path, content);
216
+ return copy;
217
+ }
218
+
219
+ /** A copy of `files` without `path`. */
220
+ export function withoutFile(files: PluginFixture, path: string): PluginFixture {
221
+ const copy = new Map(files);
222
+ copy.delete(path);
223
+ return copy;
224
+ }
225
+
226
+ /**
227
+ * A copy of `files` with one top-level field of the JSON document at
228
+ * `manifestPath` set (or removed, when `value` is `undefined`).
229
+ */
230
+ export function withManifestField(files: PluginFixture, manifestPath: string, field: string, value: unknown): PluginFixture {
231
+ const current = files.get(manifestPath);
232
+ if (current === undefined) throw new Error(`fixture has no document at '${manifestPath}'`);
233
+ const text = typeof current === "string" ? current : new TextDecoder().decode(current);
234
+ const document = JSON.parse(text) as Record<string, unknown>;
235
+ if (value === undefined) delete document[field];
236
+ else document[field] = value;
237
+ return withFile(files, manifestPath, json(document));
238
+ }
239
+
240
+ /** A `SKILL.md` document from a fixture description. */
241
+ export function skillMarkdown(skill: SkillFixture): string {
242
+ const body = skill.body ?? `# ${skill.name}\n\nInstructions for the ${skill.name} skill.\n`;
243
+ if (skill.frontmatter === null) return body;
244
+ const frontmatter = {
245
+ name: skill.name,
246
+ ...(skill.description !== undefined && { description: skill.description }),
247
+ ...skill.frontmatter,
248
+ };
249
+ return `---\n${stringifyYaml(frontmatter)}---\n${body}`;
250
+ }
251
+
252
+ /** A sub-agent document from a fixture description. */
253
+ export function agentMarkdown(agent: AgentFixture): string {
254
+ const body = agent.body ?? `You are the ${agent.file} subagent. Do the task thoroughly and report back.\n`;
255
+ if (agent.frontmatter === null) return body;
256
+ const frontmatter = { name: agent.file, ...agent.frontmatter };
257
+ return `---\n${stringifyYaml(frontmatter)}---\n${body}`;
258
+ }
259
+
260
+ function writeSkills(files: PluginFixture, root: string, skills: readonly SkillFixture[] | undefined): void {
261
+ for (const skill of skills ?? []) {
262
+ const dir = `${root}/${skill.dir ?? skill.name}`;
263
+ files.set(`${dir}/SKILL.md`, skillMarkdown(skill));
264
+ for (const [path, content] of Object.entries(skill.files ?? {})) files.set(`${dir}/${path}`, content);
265
+ }
266
+ }
267
+
268
+ function writeAgents(files: PluginFixture, root: string, agents: readonly AgentFixture[] | undefined): void {
269
+ for (const agent of agents ?? []) files.set(`${root}/${agent.file}.md`, agentMarkdown(agent));
270
+ }
271
+
272
+ function writeExtra(files: PluginFixture, extra: Readonly<Record<string, string | Uint8Array>> | undefined): void {
273
+ for (const [path, content] of Object.entries(extra ?? {})) files.set(path, content);
274
+ }
275
+
276
+ function json(value: unknown): string {
277
+ return `${JSON.stringify(value, null, 2)}\n`;
278
+ }
279
+
280
+ /**
281
+ * Eval globs and what `globError` answers for each: `undefined` for a
282
+ * well-formed glob, else its sentence. Shared with the server's matcher
283
+ * test (the module header).
284
+ */
285
+ export const GLOB_VALIDITY_TABLE: ReadonlyArray<readonly [glob: string, error: string | undefined]> = [
286
+ ["", undefined],
287
+ ["review-*", undefined],
288
+ ["src/**/x.ts", undefined],
289
+ ["src/**", undefined],
290
+ ["case-?", undefined],
291
+ ["case-[0-9]", undefined],
292
+ ["case-[!0-9]", undefined],
293
+ ["case-[^a-c]", undefined],
294
+ ["[]]x", undefined],
295
+ ["[!]]x", undefined],
296
+ ["[a-]", undefined],
297
+ ["[-z]", undefined],
298
+ ["{alpha,beta}-*", undefined],
299
+ ["{a,{b,c}}", undefined],
300
+ ["{,a}", undefined],
301
+ ["{}", undefined],
302
+ ["[{]", undefined],
303
+ ["fix}", undefined],
304
+ ["a,b", undefined],
305
+ ["(x).+|^$\\", undefined],
306
+ ["{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}", undefined],
307
+ ["{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}", "it expands to more than 64 alternatives"],
308
+ ["{a,b,c,d,e,f,g,h}{a,b,c,d,e,f,g,h}{a,b}", "it expands to more than 64 alternatives"],
309
+ ["[z-a].txt", "range 'z-a' is reversed"],
310
+ ["x[9-0]", "range '9-0' is reversed"],
311
+ ["case-[", "'[' at 5 is never closed"],
312
+ ["case-[1", "'[' at 5 is never closed"],
313
+ ["[]", "'[' at 0 is never closed"],
314
+ ["{a,b", "'{' at 0 is never closed"],
315
+ ["x{a,{b}", "'{' at 1 is never closed"],
316
+ ["{[}]", "'{' at 0 is never closed"],
317
+ ["{".repeat(17) + "}".repeat(17), "braces are nested more than 16 deep"],
318
+ ["{".repeat(16) + "}".repeat(16), undefined],
319
+ ["{a,b}[z-a]", "range 'z-a' is reversed"],
320
+ ["a".repeat(1024), undefined],
321
+ ["a".repeat(1025), "it is longer than 1024 characters"],
322
+ ["é".repeat(1024), undefined],
323
+ ["😀".repeat(1024), undefined],
324
+ ["😀".repeat(1025), "it is longer than 1024 characters"],
325
+ ["[z-a]" + "a".repeat(1020), "it is longer than 1024 characters"],
326
+ ["{a,b}".repeat(6) + "a".repeat(58), undefined],
327
+ ["{a,b}".repeat(6) + "a".repeat(59), "it expands to more than 4096 tokens"],
328
+ ["{a,b}".repeat(6) + "**/".repeat(29), undefined],
329
+ ["{a,b}".repeat(6) + "**/".repeat(30), "it expands to more than 4096 tokens"],
330
+ ["{a,b}".repeat(6) + "*".repeat(900), undefined],
331
+ ["{a,b}".repeat(6) + "[a-z]".repeat(58), undefined],
332
+ ["{a,b}".repeat(6) + "[a-z]".repeat(59), "it expands to more than 4096 tokens"],
333
+ ["{" + "a".repeat(400) + "," + "b".repeat(400) + "}{x,y,z,w,v}", undefined],
334
+ ["{" + "a".repeat(500) + "," + "b".repeat(500) + "}{x,y,z,w,v}", "it expands to more than 4096 tokens"],
335
+ ["{a,b}".repeat(7) + "a".repeat(100), "it expands to more than 64 alternatives"],
336
+ ["{a,b}".repeat(6) + "a".repeat(59) + "[", "'[' at 89 is never closed"],
337
+ // A class costs its ranges per character, so its size is capped and
338
+ // counted: 64 alternatives around an 829-character class is refused.
339
+ ["{" + Array(64).fill("**").join(",") + "}[" + "a-z".repeat(275) + "ab]", "'[' at 193 holds more than 32 ranges or characters"],
340
+ ["[" + "a-c".repeat(32) + "]", undefined],
341
+ ["[" + "a-c".repeat(33) + "]", "'[' at 0 holds more than 32 ranges or characters"],
342
+ ["x[!" + "a".repeat(32) + "]", undefined],
343
+ ["x[!" + "a".repeat(33) + "]", "'[' at 1 holds more than 32 ranges or characters"],
344
+ ["[" + "a".repeat(33), "'[' at 0 holds more than 32 ranges or characters"],
345
+ ["{a,b}".repeat(6) + "[" + "a-z".repeat(32) + "]" + "a".repeat(26), undefined],
346
+ ["{a,b}".repeat(6) + "[" + "a-z".repeat(32) + "]" + "a".repeat(27), "it expands to more than 4096 tokens"],
347
+ ];
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The name a plugin's MCP server gives its tools, as Claude Code builds it:
3
+ * `mcp__plugin_<plugin>_<server>__<tool>`, with every character outside
4
+ * `A-Za-z0-9_-` in either name written as `_`. The server segment is built
5
+ * from the two names, never parsed back, because either name may hold `_`.
6
+ * The installer refuses a plugin whose segment would hold `__` or end in
7
+ * `_`, and the runner names each plugin server by this segment (its own
8
+ * copy, `shared/plugin-servers.ts`, held equal by a runner test), so an
9
+ * agent's tool list written for Claude Code scopes the same tools here.
10
+ */
11
+
12
+ function claudeNamePart(name: string): string {
13
+ return name.replace(/[^A-Za-z0-9_-]/g, "_");
14
+ }
15
+
16
+ /** `plugin_<plugin>_<server>`: the server part of a plugin tool's name. */
17
+ export function toolServerSegment(pluginName: string, serverName: string): string {
18
+ return `plugin_${claudeNamePart(pluginName)}_${claudeNamePart(serverName)}`;
19
+ }
20
+
21
+ /**
22
+ * Whether `name` can stand as a server segment of a tool name as it is:
23
+ * only `A-Za-z0-9_-`, with no `__` (the separator between a tool name's
24
+ * parts) and no trailing `_` (which would run into it). A pushed eval's
25
+ * mock directory names an agent's MCP server this way, so a name that
26
+ * fails could never match one.
27
+ */
28
+ export function isToolNameSegment(name: string): boolean {
29
+ return /^[A-Za-z0-9_-]+$/.test(name) && !name.includes("__") && !name.endsWith("_");
30
+ }
package/src/trim.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * `value` without its trailing run of `char`. A `/x+$/` regex rescans the run
3
+ * from every starting position, so a long run costs quadratic time; this walks
4
+ * it once.
5
+ */
6
+ export function trimTrailing(value: string, char: string): string {
7
+ let end = value.length;
8
+ while (end > 0 && value[end - 1] === char) end -= 1;
9
+ return end === value.length ? value : value.slice(0, end);
10
+ }