@wonderwhy-er/desktop-commander 0.3.2-alpha.1 → 0.3.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 (194) hide show
  1. package/dist/bootstrap.js +7 -7
  2. package/dist/command-manager.d.ts +98 -1
  3. package/dist/command-manager.js +237 -16
  4. package/dist/remote-device/desktop-commander-integration.js +3 -1
  5. package/dist/remote-device/diagnostics/redact.js +30 -8
  6. package/dist/remote-device/diagnostics/report.js +30 -13
  7. package/dist/remote-device/mqtt/mqtt-enrollment.js +1 -1
  8. package/dist/remote-device/mqtt/mqtt-transport.js +2 -2
  9. package/dist/remote-device/remote-channel.d.ts +1 -1
  10. package/dist/remote-device/remote-channel.js +1 -1
  11. package/dist/remote-device/transport-telemetry.js +5 -4
  12. package/dist/server.js +2 -0
  13. package/dist/terminal-manager.js +4 -6
  14. package/dist/tools/improved-process-tools.d.ts +2 -0
  15. package/dist/tools/improved-process-tools.js +68 -40
  16. package/dist/tools/pdf/markdown.js +7 -3
  17. package/dist/tools/process.js +10 -2
  18. package/dist/track-installation.js +2 -2
  19. package/dist/ui/config-editor/config-editor-runtime.js +80 -61
  20. package/dist/ui/file-preview/preview-runtime.js +225 -207
  21. package/dist/utils/brace-expansion.d.ts +30 -0
  22. package/dist/utils/brace-expansion.js +192 -0
  23. package/dist/utils/capture.d.ts +8 -2
  24. package/dist/utils/capture.js +1 -1
  25. package/dist/utils/cmd-commands.d.ts +3 -0
  26. package/dist/utils/cmd-commands.js +370 -0
  27. package/dist/utils/command-reading.d.ts +92 -0
  28. package/dist/utils/command-reading.js +14 -0
  29. package/dist/utils/command-wrappers.d.ts +45 -0
  30. package/dist/utils/command-wrappers.js +964 -0
  31. package/dist/utils/inline-shells.d.ts +67 -0
  32. package/dist/utils/inline-shells.js +310 -0
  33. package/dist/utils/posix-commands.d.ts +6 -0
  34. package/dist/utils/posix-commands.js +237 -0
  35. package/dist/utils/posix-shell-capabilities.d.ts +8 -0
  36. package/dist/utils/posix-shell-capabilities.js +120 -0
  37. package/dist/utils/powershell-commands.d.ts +61 -0
  38. package/dist/utils/powershell-commands.js +468 -0
  39. package/dist/utils/process-tree.js +4 -4
  40. package/dist/utils/shell-script-files.d.ts +8 -0
  41. package/dist/utils/shell-script-files.js +140 -0
  42. package/dist/utils/shell.d.ts +37 -0
  43. package/dist/utils/shell.js +73 -0
  44. package/dist/utils/system-info.d.ts +2 -0
  45. package/dist/utils/system-info.js +5 -1
  46. package/dist/utils/threadpool-size.d.ts +36 -0
  47. package/dist/utils/threadpool-size.js +143 -0
  48. package/dist/version.d.ts +1 -1
  49. package/dist/version.js +1 -1
  50. package/package.json +11 -9
  51. package/dist/bootstrap.js.map +0 -1
  52. package/dist/command-manager.js.map +0 -1
  53. package/dist/config-field-definitions.js.map +0 -1
  54. package/dist/config-manager.js.map +0 -1
  55. package/dist/config-recovery.js.map +0 -1
  56. package/dist/config.js.map +0 -1
  57. package/dist/custom-stdio.js.map +0 -1
  58. package/dist/error-handlers.js.map +0 -1
  59. package/dist/handlers/edit-search-handlers.js.map +0 -1
  60. package/dist/handlers/filesystem-handlers.js.map +0 -1
  61. package/dist/handlers/history-handlers.js.map +0 -1
  62. package/dist/handlers/index.js.map +0 -1
  63. package/dist/handlers/process-handlers.js.map +0 -1
  64. package/dist/handlers/search-answers.js.map +0 -1
  65. package/dist/handlers/search-handlers.js.map +0 -1
  66. package/dist/handlers/terminal-handlers.js.map +0 -1
  67. package/dist/index.js.map +0 -1
  68. package/dist/npm-scripts/remote.js.map +0 -1
  69. package/dist/npm-scripts/setup.js.map +0 -1
  70. package/dist/npm-scripts/uninstall.js.map +0 -1
  71. package/dist/npm-scripts/verify-ripgrep.js.map +0 -1
  72. package/dist/remote-device/desktop-commander-integration.js.map +0 -1
  73. package/dist/remote-device/device-authenticator.js.map +0 -1
  74. package/dist/remote-device/device.js.map +0 -1
  75. package/dist/remote-device/diagnostics/device-log.js.map +0 -1
  76. package/dist/remote-device/diagnostics/redact.js.map +0 -1
  77. package/dist/remote-device/diagnostics/report.js.map +0 -1
  78. package/dist/remote-device/diagnostics/upload.js.map +0 -1
  79. package/dist/remote-device/mqtt/config.js.map +0 -1
  80. package/dist/remote-device/mqtt/mqtt-enrollment.js.map +0 -1
  81. package/dist/remote-device/mqtt/mqtt-transport.js.map +0 -1
  82. package/dist/remote-device/realtime-health-telemetry.js.map +0 -1
  83. package/dist/remote-device/remote-channel.js.map +0 -1
  84. package/dist/remote-device/remote-utils.js.map +0 -1
  85. package/dist/remote-device/rollouts.js.map +0 -1
  86. package/dist/remote-device/transport-telemetry.js.map +0 -1
  87. package/dist/search-manager.js.map +0 -1
  88. package/dist/server.js.map +0 -1
  89. package/dist/terminal-manager.js.map +0 -1
  90. package/dist/tools/config.js.map +0 -1
  91. package/dist/tools/edit.js.map +0 -1
  92. package/dist/tools/feedback.js.map +0 -1
  93. package/dist/tools/filesystem.js.map +0 -1
  94. package/dist/tools/fuzzySearch.js.map +0 -1
  95. package/dist/tools/fuzzySearchCore.js.map +0 -1
  96. package/dist/tools/improved-process-tools.js.map +0 -1
  97. package/dist/tools/mime-types.js.map +0 -1
  98. package/dist/tools/pdf/extract-images.js.map +0 -1
  99. package/dist/tools/pdf/index.js.map +0 -1
  100. package/dist/tools/pdf/lib/pdf2md.js.map +0 -1
  101. package/dist/tools/pdf/manipulations.js.map +0 -1
  102. package/dist/tools/pdf/markdown.js.map +0 -1
  103. package/dist/tools/pdf/utils.js.map +0 -1
  104. package/dist/tools/process.js.map +0 -1
  105. package/dist/tools/prompts.js.map +0 -1
  106. package/dist/tools/schemas.js.map +0 -1
  107. package/dist/tools/usage.js.map +0 -1
  108. package/dist/types.js.map +0 -1
  109. package/dist/ui/config-editor/src/app.js.map +0 -1
  110. package/dist/ui/config-editor/src/array-modal.js.map +0 -1
  111. package/dist/ui/config-editor/src/main.js.map +0 -1
  112. package/dist/ui/contracts.js.map +0 -1
  113. package/dist/ui/file-preview/shared/preview-file-types.js.map +0 -1
  114. package/dist/ui/file-preview/src/app.js.map +0 -1
  115. package/dist/ui/file-preview/src/components/code-viewer.js.map +0 -1
  116. package/dist/ui/file-preview/src/components/highlighting.js.map +0 -1
  117. package/dist/ui/file-preview/src/components/html-renderer.js.map +0 -1
  118. package/dist/ui/file-preview/src/components/markdown-renderer.js.map +0 -1
  119. package/dist/ui/file-preview/src/directory-controller.js.map +0 -1
  120. package/dist/ui/file-preview/src/document-layout.js.map +0 -1
  121. package/dist/ui/file-preview/src/document-outline.js.map +0 -1
  122. package/dist/ui/file-preview/src/document-workspace.js.map +0 -1
  123. package/dist/ui/file-preview/src/file-type-handlers.js.map +0 -1
  124. package/dist/ui/file-preview/src/host/external-actions.js.map +0 -1
  125. package/dist/ui/file-preview/src/host/selection-context.js.map +0 -1
  126. package/dist/ui/file-preview/src/image-preview.js.map +0 -1
  127. package/dist/ui/file-preview/src/main.js.map +0 -1
  128. package/dist/ui/file-preview/src/markdown/conflict-dialog.js.map +0 -1
  129. package/dist/ui/file-preview/src/markdown/controller.js.map +0 -1
  130. package/dist/ui/file-preview/src/markdown/editor.js.map +0 -1
  131. package/dist/ui/file-preview/src/markdown/linking.js.map +0 -1
  132. package/dist/ui/file-preview/src/markdown/outline.js.map +0 -1
  133. package/dist/ui/file-preview/src/markdown/parser.js.map +0 -1
  134. package/dist/ui/file-preview/src/markdown/preview.js.map +0 -1
  135. package/dist/ui/file-preview/src/markdown/slugify.js.map +0 -1
  136. package/dist/ui/file-preview/src/markdown/utils.js.map +0 -1
  137. package/dist/ui/file-preview/src/model.js.map +0 -1
  138. package/dist/ui/file-preview/src/panel-actions.js.map +0 -1
  139. package/dist/ui/file-preview/src/path-utils.js.map +0 -1
  140. package/dist/ui/file-preview/src/payload-utils.js.map +0 -1
  141. package/dist/ui/file-preview/src/types.js.map +0 -1
  142. package/dist/ui/resources.js.map +0 -1
  143. package/dist/ui/shared/compact-row.js.map +0 -1
  144. package/dist/ui/shared/escape-html.js.map +0 -1
  145. package/dist/ui/shared/host-context.js.map +0 -1
  146. package/dist/ui/shared/tool-bridge.js.map +0 -1
  147. package/dist/ui/shared/tool-shell.js.map +0 -1
  148. package/dist/ui/shared/ui-event-tracker.js.map +0 -1
  149. package/dist/ui/shared/widget-state.js.map +0 -1
  150. package/dist/utils/ab-test.js.map +0 -1
  151. package/dist/utils/atomic-write.js.map +0 -1
  152. package/dist/utils/capture.js.map +0 -1
  153. package/dist/utils/dockerPrompt.js.map +0 -1
  154. package/dist/utils/exit-process.js.map +0 -1
  155. package/dist/utils/feature-flags.js.map +0 -1
  156. package/dist/utils/files/base.js.map +0 -1
  157. package/dist/utils/files/binary.js.map +0 -1
  158. package/dist/utils/files/docx.js.map +0 -1
  159. package/dist/utils/files/excel.js.map +0 -1
  160. package/dist/utils/files/factory.js.map +0 -1
  161. package/dist/utils/files/image.js.map +0 -1
  162. package/dist/utils/files/index.js.map +0 -1
  163. package/dist/utils/files/pdf.js.map +0 -1
  164. package/dist/utils/files/text.js.map +0 -1
  165. package/dist/utils/fuzzySearchLogger.js.map +0 -1
  166. package/dist/utils/internal-facts.js.map +0 -1
  167. package/dist/utils/lazy-package.js.map +0 -1
  168. package/dist/utils/lineEndingHandler.js.map +0 -1
  169. package/dist/utils/logger.js.map +0 -1
  170. package/dist/utils/mcp-ui-ab-test.js.map +0 -1
  171. package/dist/utils/open-browser.js.map +0 -1
  172. package/dist/utils/process-detection.js.map +0 -1
  173. package/dist/utils/process-tree.js.map +0 -1
  174. package/dist/utils/rename.js.map +0 -1
  175. package/dist/utils/ripgrep-resolver.js.map +0 -1
  176. package/dist/utils/shell.js.map +0 -1
  177. package/dist/utils/system-info.js.map +0 -1
  178. package/dist/utils/terminal-timestamps.js.map +0 -1
  179. package/dist/utils/toolHistory.js.map +0 -1
  180. package/dist/utils/trackTools.js.map +0 -1
  181. package/dist/utils/unsupportedParams.js.map +0 -1
  182. package/dist/utils/usageTracker.js.map +0 -1
  183. package/dist/utils/welcome-onboarding.js.map +0 -1
  184. package/dist/utils/withTimeout.js.map +0 -1
  185. package/dist/version.js.map +0 -1
  186. package/node_modules/@desktop-commander/telemetry-contract/README.md +0 -56
  187. package/node_modules/@desktop-commander/telemetry-contract/package.json +0 -32
  188. package/node_modules/@desktop-commander/telemetry-contract/transport.d.ts +0 -39
  189. package/node_modules/@desktop-commander/telemetry-contract/transport.js +0 -167
  190. package/vendor/telemetry-contract/README.md +0 -56
  191. package/vendor/telemetry-contract/package.json +0 -32
  192. package/vendor/telemetry-contract/transport.d.ts +0 -38
  193. package/vendor/telemetry-contract/transport.js +0 -164
  194. package/vendor/telemetry-contract/transport.test.js +0 -39
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Brace expansion, the way bash does it before it runs a command: x{1,2}y
3
+ * becomes the words x1y and x2y, and {1..3} becomes 1 2 3. So {sudo,ls} runs
4
+ * sudo, and su{d..d}o runs sudo too. zsh expands the same forms. The command
5
+ * check's POSIX reader uses it to find the program such a command name runs.
6
+ */
7
+ /** One character of a word, and whether it is quoted or escaped there: a quoted "{", "," or "}" is only text */
8
+ export interface WordChar {
9
+ char: string;
10
+ quoted: boolean;
11
+ }
12
+ /** The most words of one expansion the check reads; past that it gives up rather than check part of it */
13
+ export declare const MAX_BRACE_WORDS = 1000;
14
+ /** Default calls retain the historical reading; native capabilities select one range grammar. */
15
+ export type BraceSemantics = 'legacy' | 'bash3' | 'bash5.2' | 'zsh';
16
+ export interface BraceOptions {
17
+ semantics?: BraceSemantics;
18
+ }
19
+ /** Characters that are all text: a quoted part of a word, or a part with no brace syntax in it */
20
+ export declare function quotedChars(text: string): WordChar[];
21
+ /** Characters as written outside quotes, where a backslash makes the character after it text */
22
+ export declare function unquotedChars(text: string): WordChar[];
23
+ /**
24
+ * The words bash makes from a word by brace expansion, in bash's order, without
25
+ * the unquoted empty ones: bash drops {,sudo}'s empty word, but keeps {"",sudo}'s.
26
+ * A word without an expansion gives itself. Past MAX_BRACE_WORDS words or
27
+ * MAX_BRACE_STEPS steps it gives null, and the reader takes the command as
28
+ * unreadable.
29
+ */
30
+ export declare function braceWords(chars: WordChar[], { semantics }?: BraceOptions): string[] | null;
@@ -0,0 +1,192 @@
1
+ /**
2
+ * Brace expansion, the way bash does it before it runs a command: x{1,2}y
3
+ * becomes the words x1y and x2y, and {1..3} becomes 1 2 3. So {sudo,ls} runs
4
+ * sudo, and su{d..d}o runs sudo too. zsh expands the same forms. The command
5
+ * check's POSIX reader uses it to find the program such a command name runs.
6
+ */
7
+ /** The most words of one expansion the check reads; past that it gives up rather than check part of it */
8
+ export const MAX_BRACE_WORDS = 1000;
9
+ /**
10
+ * The most steps one expansion may take, a step being a character looked at
11
+ * to find a brace or one put in a word. The words are bounded by
12
+ * MAX_BRACE_WORDS, but the work to make them isn't: the rest of the word is
13
+ * expanded again for each word of a list, and a nested group's braces take
14
+ * a time that grows with the square of its depth. This keeps the check to a
15
+ * fraction of a second; past it the check gives up, as past MAX_BRACE_WORDS.
16
+ */
17
+ const MAX_BRACE_STEPS = 10000000;
18
+ /** Characters that are all text: a quoted part of a word, or a part with no brace syntax in it */
19
+ export function quotedChars(text) {
20
+ // An empty quoted part still keeps its word after quote removal.
21
+ return Array.from(text || [''], (char) => ({ char, quoted: true }));
22
+ }
23
+ /** Characters as written outside quotes, where a backslash makes the character after it text */
24
+ export function unquotedChars(text) {
25
+ const chars = [];
26
+ const all = Array.from(text);
27
+ for (let i = 0; i < all.length; i++) {
28
+ if (all[i] === '\\' && i + 1 < all.length) {
29
+ chars.push({ char: all[++i], quoted: true });
30
+ }
31
+ else {
32
+ chars.push({ char: all[i], quoted: false });
33
+ }
34
+ }
35
+ return chars;
36
+ }
37
+ /**
38
+ * The words bash makes from a word by brace expansion, in bash's order, without
39
+ * the unquoted empty ones: bash drops {,sudo}'s empty word, but keeps {"",sudo}'s.
40
+ * A word without an expansion gives itself. Past MAX_BRACE_WORDS words or
41
+ * MAX_BRACE_STEPS steps it gives null, and the reader takes the command as
42
+ * unreadable.
43
+ */
44
+ export function braceWords(chars, { semantics = 'legacy' } = {}) {
45
+ const words = [];
46
+ const steps = { left: MAX_BRACE_STEPS, semantics };
47
+ let read = 0;
48
+ try {
49
+ for (const word of expand(chars, steps)) {
50
+ if (++read > MAX_BRACE_WORDS)
51
+ return null;
52
+ take(steps, word.text.length);
53
+ if (word.text !== '' || word.quoted)
54
+ words.push(word.text);
55
+ }
56
+ }
57
+ catch (error) {
58
+ if (error instanceof TooManySteps)
59
+ return null;
60
+ throw error;
61
+ }
62
+ return words;
63
+ }
64
+ class TooManySteps extends Error {
65
+ }
66
+ /** Takes n steps, or stops the expansion when there aren't that many left */
67
+ function take(steps, n) {
68
+ steps.left -= n;
69
+ if (steps.left < 0)
70
+ throw new TooManySteps();
71
+ }
72
+ const isSyntax = (c, char) => !c.quoted && c.char === char;
73
+ const textOf = (chars) => chars.map((c) => c.char).join('');
74
+ function* expand(chars, steps) {
75
+ const brace = findBrace(chars, steps);
76
+ if (!brace) {
77
+ yield { text: textOf(chars), quoted: chars.some((c) => c.quoted) };
78
+ return;
79
+ }
80
+ const preamble = chars.slice(0, brace.open);
81
+ const postscript = chars.slice(brace.close + 1);
82
+ for (const middle of middles(chars, brace, steps)) {
83
+ for (const end of expand(postscript, steps)) {
84
+ yield {
85
+ text: textOf(preamble) + middle.text + end.text,
86
+ quoted: preamble.some((c) => c.quoted) || middle.quoted || end.quoted,
87
+ };
88
+ }
89
+ }
90
+ }
91
+ /** The words that replace the braces, in order: each piece between the commas, expanded, or the sequence */
92
+ function* middles(chars, brace, steps) {
93
+ if ('text' in brace) {
94
+ // A generated backslash removed by Bash3/5.2 still keeps its empty argument after quote removal.
95
+ for (const text of sequence(brace.text, steps))
96
+ yield { text, quoted: text === '' };
97
+ return;
98
+ }
99
+ const bounds = [brace.open, ...brace.commas, brace.close];
100
+ for (let k = 0; k + 1 < bounds.length; k++) {
101
+ yield* expand(chars.slice(bounds[k] + 1, bounds[k + 1]), steps);
102
+ }
103
+ }
104
+ // bash reads a number here with its sign, - or +: su{d..d..+1}o runs sudo
105
+ const NUMBERS = /^([-+]?\d+)\.\.([-+]?\d+)(?:\.\.([-+]?\d+))?$/;
106
+ const LETTERS = /^([A-Za-z])\.\.([A-Za-z])(?:\.\.([-+]?\d+))?$/;
107
+ /**
108
+ * The first {a,b} or {x..y} in the word. As in bash, a "{" without a matching
109
+ * "}", or whose braces hold neither a comma nor a sequence ({sudo}), is text,
110
+ * and the search goes on after it.
111
+ */
112
+ function findBrace(chars, steps) {
113
+ take(steps, chars.length);
114
+ const { semantics } = steps;
115
+ for (let open = 0; open < chars.length; open++) {
116
+ if (!isSyntax(chars[open], '{'))
117
+ continue;
118
+ let depth = 0;
119
+ let close = -1;
120
+ const commas = [];
121
+ let i = open + 1;
122
+ for (; i < chars.length && close < 0; i++) {
123
+ if (isSyntax(chars[i], '{')) {
124
+ depth++;
125
+ }
126
+ else if (isSyntax(chars[i], '}')) {
127
+ if (depth === 0)
128
+ close = i;
129
+ else
130
+ depth--;
131
+ }
132
+ else if (depth === 0 && isSyntax(chars[i], ',')) {
133
+ commas.push(i);
134
+ }
135
+ }
136
+ take(steps, i - open);
137
+ if (close < 0)
138
+ continue;
139
+ if (commas.length > 0)
140
+ return { open, close, commas };
141
+ const inside = chars.slice(open + 1, close);
142
+ const text = textOf(inside);
143
+ const range = NUMBERS.exec(text) ?? LETTERS.exec(text);
144
+ if (semantics === 'zsh' && range && (range[1].startsWith('+') || range[2].startsWith('+')))
145
+ continue;
146
+ if (semantics === 'zsh' && range?.[3] !== undefined
147
+ && (range[3].startsWith('+') || LETTERS.test(text)))
148
+ continue;
149
+ if (inside.every((c) => !c.quoted) && range && !(semantics === 'bash3' && range[3] !== undefined)) {
150
+ return { open, close, text };
151
+ }
152
+ }
153
+ return null;
154
+ }
155
+ /** The words of a {1..10}, {01..10..2} or {a..e} sequence (findBrace has matched its text) */
156
+ function* sequence(text, { semantics }) {
157
+ const numbers = NUMBERS.exec(text);
158
+ if (numbers) {
159
+ const [, first, last, increment] = numbers;
160
+ if (semantics === 'zsh' && increment !== undefined && Number(increment) === 0) {
161
+ yield text;
162
+ return;
163
+ }
164
+ // Match bash: a leading zero (after -, not +) pads to the longer endpoint's width, including its sign.
165
+ let width = semantics !== 'bash3' && (/^-?0\d/.test(first) || /^-?0\d/.test(last))
166
+ ? Math.max(first.length, last.length) : 0;
167
+ // Zsh takes the width from the first padded endpoint, even across a sign change.
168
+ if (semantics === 'zsh' && width)
169
+ width = /^-?0\d/.test(first) ? first.length : last.length;
170
+ const pad = (n) => (n < 0 ? '-' + String(-n).padStart(width - 1, '0') : String(n).padStart(width, '0'));
171
+ for (const n of range(Number(first), Number(last), { increment, semantics }))
172
+ yield pad(n);
173
+ return;
174
+ }
175
+ const [, first, last, increment] = LETTERS.exec(text);
176
+ for (const n of range(first.charCodeAt(0), last.charCodeAt(0), { increment, semantics })) {
177
+ // Bash3/5.2 remove the generated backslash after expansion; Bash5.3 omits its whole word.
178
+ if (n === 0x5c && (semantics === 'bash3' || semantics === 'bash5.2'))
179
+ yield '';
180
+ if (n !== 0x5c || semantics === 'zsh')
181
+ yield String.fromCharCode(n);
182
+ }
183
+ }
184
+ /** from, from ± step, … as far as to, in either direction; bash takes a missing or zero step as 1 */
185
+ function* range(from, to, { increment, semantics }) {
186
+ if (semantics === 'zsh' && Number(increment) < 0)
187
+ [from, to] = [to, from];
188
+ const size = Math.abs(Number(increment ?? 1)) || 1;
189
+ const step = from <= to ? size : -size;
190
+ for (let n = from; Math.abs(n - from) <= Math.abs(to - from); n += step)
191
+ yield n;
192
+ }
@@ -1,4 +1,10 @@
1
- import type { TransportParams } from '@desktop-commander/telemetry-contract/transport';
1
+ /** Core fields required by the collector; producers own their optional diagnostic fields. */
2
+ export interface TransportParams {
3
+ call_id: string;
4
+ stage: string;
5
+ timestamp_utc: string;
6
+ [key: string]: unknown;
7
+ }
2
8
  export declare function runInUiOriginCallContext<T>(fn: () => T): T;
3
9
  export declare function isInsideUiOriginCall(): boolean;
4
10
  /** Runs a tool call with its own set of known paths. */
@@ -36,7 +42,7 @@ export declare const captureBase: (captureURL: string, event: string, properties
36
42
  export declare const buildEventProperties: (properties?: any) => Promise<any>;
37
43
  export declare const capture: (event: string, properties?: any) => Promise<void>;
38
44
  /**
39
- * Send only contract-validated transport fields through the existing proxy sender,
45
+ * Send transport observations through the existing proxy sender,
40
46
  * redacted like every other event (known paths, path properties, error text).
41
47
  */
42
48
  export declare const captureTransport: (event: string, properties: TransportParams, userId: string) => Promise<void>;
@@ -550,7 +550,7 @@ export const capture = async (event, properties) => {
550
550
  })();
551
551
  };
552
552
  /**
553
- * Send only contract-validated transport fields through the existing proxy sender,
553
+ * Send transport observations through the existing proxy sender,
554
554
  * redacted like every other event (known paths, path properties, error text).
555
555
  */
556
556
  export const captureTransport = async (event, properties, userId) => {
@@ -0,0 +1,3 @@
1
+ import type { CommandReadOptions, CommandReading } from './command-reading.js';
2
+ /** Reads a command for cmd */
3
+ export declare function readCmdCommands(command: string, { cmdDelayedExpansion }?: CommandReadOptions): Promise<CommandReading>;
@@ -0,0 +1,370 @@
1
+ /** Percent expansion can span a substitution delimiter, but must start in the executable to change its name. */
2
+ function hasPercentVariable(text, { startsBefore = text.length } = {}) {
3
+ const variable = /%[^%\s]+%/.exec(text);
4
+ return variable !== null && variable.index < startsBefore;
5
+ }
6
+ /**
7
+ * In explicitly enabled delayed expansion, !VAR! is runtime information
8
+ * rather than a literal program we can claim to have checked.
9
+ * Unquoted carets were removed while tokenizing. A remaining caret protects
10
+ * the next character and interrupts a variable name, as in "!literal^!".
11
+ */
12
+ function hasDelayedVariable(value, { startsBefore = value.length } = {}) {
13
+ let opening = -1;
14
+ for (let i = 0; i < value.length; i++) {
15
+ if (value[i] === '^') {
16
+ opening = -1;
17
+ i++;
18
+ }
19
+ else if (value[i] === '!') {
20
+ if (opening >= 0 && opening < startsBefore && i > opening + 1)
21
+ return true;
22
+ opening = i;
23
+ }
24
+ }
25
+ return false;
26
+ }
27
+ /** CMD consumes a caret layer in the executable word even within double quotes. */
28
+ function commandWordValue(text) {
29
+ let value = '';
30
+ for (let i = 0; i < text.length; i++) {
31
+ if (text[i] === '^' && i + 1 < text.length)
32
+ value += text[++i];
33
+ else if (text[i] !== '"')
34
+ value += text[i];
35
+ }
36
+ return value;
37
+ }
38
+ function tokenize(text, { cmdDelayedExpansion = 'unknown' }) {
39
+ const tokens = [];
40
+ let i = 0;
41
+ while (i < text.length) {
42
+ const char = text[i];
43
+ if (char === ' ' || char === '\t') {
44
+ i++;
45
+ }
46
+ else if (char === '\r' || char === '\n') {
47
+ tokens.push({ kind: 'op', value: '\n', text: char, start: i, atRunTime: false });
48
+ i++;
49
+ }
50
+ else if (char === '&' || char === '|') {
51
+ const value = text[i + 1] === char ? char + char : char;
52
+ tokens.push({ kind: 'op', value, text: value, start: i, atRunTime: false });
53
+ i += value.length;
54
+ }
55
+ else if (char === '(' || char === ')') {
56
+ tokens.push({ kind: char === '(' ? 'open' : 'close', value: char, text: char, start: i, atRunTime: false });
57
+ i++;
58
+ }
59
+ else if (char === '>' || char === '<' || (/[0-9]/.test(char) && /[<>]/.test(text[i + 1] ?? ''))) {
60
+ // A redirection: an optional handle number, > >> or <, and &N when it points at another handle
61
+ const start = i;
62
+ if (/[0-9]/.test(char))
63
+ i++;
64
+ i += text[i] === '>' && text[i + 1] === '>' ? 2 : 1;
65
+ if (text[i] === '&' && /[0-9]/.test(text[i + 1] ?? ''))
66
+ i += 2;
67
+ tokens.push({ kind: 'redirect', value: text.slice(start, i), text: text.slice(start, i), start, atRunTime: false });
68
+ }
69
+ else {
70
+ const start = i;
71
+ let value = '';
72
+ let inQuotes = false;
73
+ // Where in value a ; , = or / is outside "…": cmd ends a command's name at the first one
74
+ const ends = [];
75
+ while (i < text.length) {
76
+ const c = text[i];
77
+ if (inQuotes) {
78
+ if (c === '"')
79
+ inQuotes = false;
80
+ else if (c === '\r' || c === '\n')
81
+ break;
82
+ else
83
+ value += c;
84
+ i++;
85
+ }
86
+ else if (c === '"') {
87
+ inQuotes = true;
88
+ i++;
89
+ }
90
+ else if (c === '^' && i + 1 < text.length) {
91
+ if (';,=/'.includes(text[i + 1]))
92
+ ends.push({ value: value.length, text: i - start });
93
+ value += text[i + 1];
94
+ i += 2;
95
+ }
96
+ else if (' \t\r\n&|()<>'.includes(c)) {
97
+ break;
98
+ }
99
+ else {
100
+ if (';,=/'.includes(c))
101
+ ends.push({ value: value.length, text: i - start });
102
+ value += c;
103
+ i++;
104
+ }
105
+ }
106
+ const written = text.slice(start, i);
107
+ const lead = /^[;,=@]*/.exec(written)[0].length;
108
+ const drive = /^[A-Za-z]:/.test(value.slice(lead));
109
+ const end = ends.find((at) => at.value >= lead && !(drive && value[at.value] === '/'));
110
+ const atRunTime = hasPercentVariable(written) || (cmdDelayedExpansion === 'on' && hasDelayedVariable(value));
111
+ const ending = { nameEnd: end?.value, textEnd: end?.text };
112
+ tokens.push({ kind: 'word', value, text: written, start, atRunTime, lead, ...ending });
113
+ }
114
+ }
115
+ return tokens;
116
+ }
117
+ const IF_TESTS = ['exist', 'defined', 'errorlevel', 'cmdextversion'];
118
+ const IF_COMPARISONS = ['equ', 'neq', 'lss', 'leq', 'gtr', 'geq'];
119
+ /**
120
+ * How many words an if condition starting at words[i] takes: "exist x" and
121
+ * the other tests take 2, "a==b" 1, "a== b" and "a ==b" 2, "a == b" and
122
+ * "a equ b" 3. Undefined while the words so far aren't a condition yet.
123
+ */
124
+ function ifConditionLength(words, i) {
125
+ const word = words[i]?.value;
126
+ if (word === undefined)
127
+ return undefined;
128
+ const next = words[i + 1]?.value;
129
+ if (IF_TESTS.includes(word.toLowerCase()))
130
+ return 2;
131
+ if (/^.+==.+$/.test(word))
132
+ return 1;
133
+ if (word.endsWith('=='))
134
+ return 2;
135
+ if (next === '==')
136
+ return 3;
137
+ if (next?.startsWith('=='))
138
+ return 2;
139
+ if (next !== undefined && IF_COMPARISONS.includes(next.toLowerCase()))
140
+ return 3;
141
+ return undefined;
142
+ }
143
+ /** Whether a word is only ; , = and @, which cmd skips where a command starts */
144
+ const isLead = (word) => word.lead !== undefined && word.lead > 0 && word.lead === word.value.length;
145
+ /** The name a command's first word gives, without the ; , = and @ that cmd skips before it (;sc runs sc) */
146
+ const commandName = (word) => word.value.slice(word.lead ?? 0, word.nameEnd);
147
+ /** The rest of a command's first word after its name, cmd's first argument: user for net,user, /query for sc/query */
148
+ const afterName = (word) => word.nameEnd === undefined ? '' : word.value.slice(word.nameEnd).replace(/^[;,=]+/, '');
149
+ /** The same rest as written, without the ; , = (and its ^) the name ends at */
150
+ const writtenAfterName = (word) => word.textEnd === undefined ? '' : word.text.slice(word.textEnd).replace(/^(\^?[;,=])+/, '');
151
+ class CmdReader {
152
+ constructor(tokens, text, options) {
153
+ this.tokens = tokens;
154
+ this.text = text;
155
+ this.options = options;
156
+ this.position = 0;
157
+ this.commands = [];
158
+ }
159
+ /** Reads commands and the operators between them, up to the end, or up to the ) that closes a group */
160
+ readList(inGroup) {
161
+ while (this.position < this.tokens.length) {
162
+ const token = this.tokens[this.position];
163
+ if (token.kind === 'op') {
164
+ this.position++;
165
+ }
166
+ else if (token.kind === 'close' && inGroup) {
167
+ return;
168
+ }
169
+ else {
170
+ this.readCommand(inGroup);
171
+ }
172
+ }
173
+ if (inGroup)
174
+ throw new Error('a ( group is not closed with )');
175
+ }
176
+ /** Reads one command: a group, or words up to the next operator */
177
+ readCommand(inGroup) {
178
+ // Words of only ; , = and @ before the command are skipped, as spaces are (@ sc runs sc)
179
+ while (this.tokens[this.position]?.kind === 'word' && isLead(this.tokens[this.position]))
180
+ this.position++;
181
+ const first = this.tokens[this.position];
182
+ if (first === undefined || first.kind === 'op' || (first.kind === 'close' && inGroup))
183
+ return;
184
+ if (first.kind === 'open') {
185
+ this.position++;
186
+ this.readList(true);
187
+ this.position++; // the closing )
188
+ this.skipRedirects();
189
+ return;
190
+ }
191
+ const words = [];
192
+ let parens = 0;
193
+ while (this.position < this.tokens.length) {
194
+ const token = this.tokens[this.position];
195
+ if (token.kind === 'open' && parens === 0 && this.readForSet(words))
196
+ continue;
197
+ if (token.kind === 'op')
198
+ break;
199
+ if (token.kind === 'close' && parens === 0 && inGroup)
200
+ break;
201
+ if (token.kind === 'redirect') {
202
+ this.skipRedirects();
203
+ continue;
204
+ }
205
+ // An if or for condition: the command after it starts here, and a ( there opens a group
206
+ if (words.length > 0 && token.kind !== 'close' && this.startsCommandAfter(words, parens)) {
207
+ this.readCommand(inGroup);
208
+ return;
209
+ }
210
+ if (token.kind === 'open')
211
+ parens++;
212
+ if (token.kind === 'close')
213
+ parens--;
214
+ words.push(token);
215
+ this.position++;
216
+ }
217
+ if (words.length > 0 && !this.startsCommandAfter(words, parens))
218
+ this.addCommand(words);
219
+ }
220
+ /** A FOR /F quoted set is a command or literal, not part of the outer list. */
221
+ readForSet(words) {
222
+ if (words.length === 0 || commandName(words[0]).toLowerCase() !== 'for'
223
+ || words[words.length - 1].value.toLowerCase() !== 'in')
224
+ return false;
225
+ const rest = afterName(words[0]);
226
+ // Comma/equal/semicolon may separate FOR from /F; an attached slash cannot.
227
+ if (rest && !';,='.includes(words[0].value[words[0].nameEnd]))
228
+ return false;
229
+ const args = [...(rest ? [rest] : []), ...words.slice(1).map(word => word.value)];
230
+ if (args[0]?.toLowerCase() !== '/f')
231
+ return false;
232
+ const usebackq = /(?:^|\s)usebackq(?:\s|$)/i.test(args[1] ?? '');
233
+ const open = this.tokens[this.position];
234
+ let start = open.start + 1;
235
+ while (/[ \t]/.test(this.text[start] ?? '') && start < this.text.length)
236
+ start++;
237
+ const quote = this.text[start];
238
+ const isCommand = quote === (usebackq ? '`' : "'");
239
+ const isLiteral = quote === (usebackq ? "'" : '"') || (usebackq && quote === '"');
240
+ if (!isCommand && !isLiteral)
241
+ return false;
242
+ // Keep the raw span: tokenization removes quotes and splits the inner operators.
243
+ let end = start + 1;
244
+ let depth = 0;
245
+ let inQuotes = false;
246
+ let close = -1;
247
+ for (; end < this.text.length; end++) {
248
+ const char = this.text[end];
249
+ if (isCommand && char === '^' && !inQuotes) {
250
+ end++;
251
+ continue;
252
+ }
253
+ if (isCommand && char === '"')
254
+ inQuotes = !inQuotes;
255
+ if (!inQuotes && depth === 0 && char === quote) {
256
+ let next = end + 1;
257
+ while (/[ \t]/.test(this.text[next] ?? '') && next < this.text.length)
258
+ next++;
259
+ if (this.text[next] === ')') {
260
+ close = next;
261
+ break;
262
+ }
263
+ }
264
+ if (isCommand && !inQuotes) {
265
+ if (char === '(')
266
+ depth++;
267
+ if (char === ')')
268
+ depth--;
269
+ }
270
+ }
271
+ if (close < 0)
272
+ throw new Error('a for /f quoted set is not closed');
273
+ if (isCommand) {
274
+ // cmd consumes one caret-escape layer before passing the command to its child.
275
+ let command = '';
276
+ const offsets = [];
277
+ inQuotes = false;
278
+ for (let i = start + 1; i < end; i++) {
279
+ const char = this.text[i];
280
+ if (char === '"')
281
+ inQuotes = !inQuotes;
282
+ if (char === '^' && !inQuotes && i + 1 < end)
283
+ i++;
284
+ offsets.push(i);
285
+ command += this.text[i];
286
+ }
287
+ const reader = new CmdReader(tokenize(command, this.options), command, this.options);
288
+ reader.readList(false);
289
+ this.commands.push(...reader.commands.map(found => ({ ...found, start: offsets[found.start] })));
290
+ }
291
+ // Retain a balanced placeholder so the existing DO/body/group logic still applies.
292
+ words.push(open, { kind: 'close', value: ')', text: ')', start: close, atRunTime: false });
293
+ while (this.position < this.tokens.length && this.tokens[this.position].start <= close)
294
+ this.position++;
295
+ return true;
296
+ }
297
+ /**
298
+ * Whether the words so far are a whole if condition, a for clause up to its
299
+ * "do", an "else" or a "call", so the next token starts the command that runs.
300
+ * parens: how many of the words' ( are not closed yet.
301
+ */
302
+ startsCommandAfter(words, parens) {
303
+ const first = commandName(words[0]).toLowerCase();
304
+ if (first === 'else')
305
+ return words.length === 1;
306
+ // CALL can carry its program in the same token: call,net user must keep net.
307
+ if (first === 'call')
308
+ return words.length === 1 && afterName(words[0]) === '';
309
+ if (first === 'for') {
310
+ return words.length > 1 && parens === 0 && words[words.length - 1].value.toLowerCase() === 'do';
311
+ }
312
+ if (first !== 'if')
313
+ return false;
314
+ let i = 1;
315
+ while (i < words.length && ['/i', 'not'].includes(words[i].value.toLowerCase()))
316
+ i++;
317
+ const length = ifConditionLength(words, i);
318
+ return length !== undefined && words.length === i + length;
319
+ }
320
+ skipRedirects() {
321
+ while (this.tokens[this.position]?.kind === 'redirect') {
322
+ const redirect = this.tokens[this.position++];
323
+ if (!/&[0-9]$/.test(redirect.value) && this.tokens[this.position]?.kind === 'word')
324
+ this.position++;
325
+ }
326
+ }
327
+ addCommand(words) {
328
+ const first = words[0];
329
+ const name = commandWordValue(first.text.slice(first.lead ?? 0, first.textEnd));
330
+ if (!name && words.length === 1)
331
+ return; // "" alone runs nothing
332
+ const rest = afterName(first);
333
+ const firstWords = rest ? [name, rest] : [name];
334
+ // Executable words and arguments have different caret phases. Keep the
335
+ // argument reading intact and classify the executable's spelling separately.
336
+ // !tool:old=new! starts in the executable even though its substitution spans a name delimiter.
337
+ const nameLength = commandWordValue(first.text.slice(0, first.textEnd)).length;
338
+ const nameAtRunTime = hasPercentVariable(first.text, { startsBefore: first.textEnd })
339
+ || (this.options.cmdDelayedExpansion === 'on'
340
+ && hasDelayedVariable(commandWordValue(first.text), { startsBefore: nameLength }));
341
+ const firstWordValues = firstWords.map((word) => (first.atRunTime ? null : word));
342
+ firstWordValues[0] = nameAtRunTime ? null : name;
343
+ this.commands.push({
344
+ name,
345
+ words: [
346
+ ...firstWordValues,
347
+ ...words.slice(1).map((word) => (word.atRunTime ? null : word.value)),
348
+ ],
349
+ written: [
350
+ ...(rest ? [first.text.slice(0, first.textEnd), writtenAfterName(first)] : [first.text]),
351
+ ...words.slice(1).map((word) => word.text),
352
+ ],
353
+ wordTexts: [...firstWords, ...words.slice(1).map((word) => word.value)],
354
+ start: first.start,
355
+ nameAtRunTime,
356
+ });
357
+ }
358
+ }
359
+ /** Reads a command for cmd */
360
+ export async function readCmdCommands(command, { cmdDelayedExpansion = 'unknown' } = {}) {
361
+ const options = { cmdDelayedExpansion };
362
+ const reader = new CmdReader(tokenize(command, options), command, options);
363
+ try {
364
+ reader.readList(false);
365
+ }
366
+ catch (error) {
367
+ return { commands: [], functions: [], parseError: error instanceof Error ? error.message : String(error) };
368
+ }
369
+ return { commands: reader.commands.sort((a, b) => a.start - b.start), functions: [] };
370
+ }