command-stream 0.9.0 → 0.9.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 (39) hide show
  1. package/js/src/$.ansi.mjs +147 -0
  2. package/js/src/$.mjs +49 -6382
  3. package/js/src/$.process-runner-base.mjs +563 -0
  4. package/js/src/$.process-runner-execution.mjs +1497 -0
  5. package/js/src/$.process-runner-orchestration.mjs +250 -0
  6. package/js/src/$.process-runner-pipeline.mjs +1162 -0
  7. package/js/src/$.process-runner-stream-kill.mjs +312 -0
  8. package/js/src/$.process-runner-virtual.mjs +297 -0
  9. package/js/src/$.quote.mjs +161 -0
  10. package/js/src/$.result.mjs +23 -0
  11. package/js/src/$.shell-settings.mjs +84 -0
  12. package/js/src/$.shell.mjs +157 -0
  13. package/js/src/$.state.mjs +401 -0
  14. package/js/src/$.stream-emitter.mjs +111 -0
  15. package/js/src/$.stream-utils.mjs +390 -0
  16. package/js/src/$.trace.mjs +36 -0
  17. package/js/src/$.utils.mjs +2 -23
  18. package/js/src/$.virtual-commands.mjs +113 -0
  19. package/js/src/commands/$.which.mjs +3 -1
  20. package/js/src/commands/index.mjs +24 -0
  21. package/js/src/shell-parser.mjs +125 -83
  22. package/js/tests/resource-cleanup-internals.test.mjs +22 -24
  23. package/js/tests/sigint-cleanup.test.mjs +3 -0
  24. package/package.json +1 -1
  25. package/rust/src/ansi.rs +194 -0
  26. package/rust/src/events.rs +305 -0
  27. package/rust/src/lib.rs +71 -60
  28. package/rust/src/macros.rs +165 -0
  29. package/rust/src/pipeline.rs +411 -0
  30. package/rust/src/quote.rs +161 -0
  31. package/rust/src/state.rs +333 -0
  32. package/rust/src/stream.rs +369 -0
  33. package/rust/src/trace.rs +152 -0
  34. package/rust/src/utils.rs +53 -158
  35. package/rust/tests/events.rs +207 -0
  36. package/rust/tests/macros.rs +77 -0
  37. package/rust/tests/pipeline.rs +93 -0
  38. package/rust/tests/state.rs +207 -0
  39. package/rust/tests/stream.rs +102 -0
@@ -0,0 +1,147 @@
1
+ // ANSI control character utilities for command-stream
2
+ // Handles stripping and processing of ANSI escape codes
3
+
4
+ import { trace } from './$.trace.mjs';
5
+
6
+ /**
7
+ * ANSI control character utilities
8
+ */
9
+ export const AnsiUtils = {
10
+ /**
11
+ * Strip ANSI escape codes from text
12
+ * @param {string} text - Text to process
13
+ * @returns {string} Text without ANSI codes
14
+ */
15
+ stripAnsi(text) {
16
+ if (typeof text !== 'string') {
17
+ return text;
18
+ }
19
+ return text.replace(/\x1b\[[0-9;]*[mGKHFJ]/g, '');
20
+ },
21
+
22
+ /**
23
+ * Strip control characters from text (preserving newlines, carriage returns, tabs)
24
+ * @param {string} text - Text to process
25
+ * @returns {string} Text without control characters
26
+ */
27
+ stripControlChars(text) {
28
+ if (typeof text !== 'string') {
29
+ return text;
30
+ }
31
+ // Preserve newlines (\n = \x0A), carriage returns (\r = \x0D), and tabs (\t = \x09)
32
+ return text.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, '');
33
+ },
34
+
35
+ /**
36
+ * Strip both ANSI codes and control characters
37
+ * @param {string} text - Text to process
38
+ * @returns {string} Cleaned text
39
+ */
40
+ stripAll(text) {
41
+ if (typeof text !== 'string') {
42
+ return text;
43
+ }
44
+ // Preserve newlines (\n = \x0A), carriage returns (\r = \x0D), and tabs (\t = \x09)
45
+ return text.replace(
46
+ /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]|\x1b\[[0-9;]*[mGKHFJ]/g,
47
+ ''
48
+ );
49
+ },
50
+
51
+ /**
52
+ * Clean data for processing (handles both Buffer and string)
53
+ * @param {Buffer|string} data - Data to clean
54
+ * @returns {Buffer|string} Cleaned data
55
+ */
56
+ cleanForProcessing(data) {
57
+ if (Buffer.isBuffer(data)) {
58
+ return Buffer.from(this.stripAll(data.toString('utf8')));
59
+ }
60
+ return this.stripAll(data);
61
+ },
62
+ };
63
+
64
+ // Global ANSI configuration
65
+ let globalAnsiConfig = {
66
+ preserveAnsi: true,
67
+ preserveControlChars: true,
68
+ };
69
+
70
+ /**
71
+ * Configure global ANSI handling
72
+ * @param {object} options - Configuration options
73
+ * @param {boolean} options.preserveAnsi - Whether to preserve ANSI codes
74
+ * @param {boolean} options.preserveControlChars - Whether to preserve control chars
75
+ * @returns {object} Current configuration
76
+ */
77
+ export function configureAnsi(options = {}) {
78
+ trace(
79
+ 'AnsiUtils',
80
+ () => `configureAnsi() called | ${JSON.stringify({ options }, null, 2)}`
81
+ );
82
+ globalAnsiConfig = { ...globalAnsiConfig, ...options };
83
+ trace(
84
+ 'AnsiUtils',
85
+ () => `New ANSI config | ${JSON.stringify({ globalAnsiConfig }, null, 2)}`
86
+ );
87
+ return globalAnsiConfig;
88
+ }
89
+
90
+ /**
91
+ * Get current ANSI configuration
92
+ * @returns {object} Current configuration
93
+ */
94
+ export function getAnsiConfig() {
95
+ trace(
96
+ 'AnsiUtils',
97
+ () =>
98
+ `getAnsiConfig() returning | ${JSON.stringify({ globalAnsiConfig }, null, 2)}`
99
+ );
100
+ return { ...globalAnsiConfig };
101
+ }
102
+
103
+ /**
104
+ * Reset ANSI configuration to defaults
105
+ */
106
+ export function resetAnsiConfig() {
107
+ globalAnsiConfig = {
108
+ preserveAnsi: true,
109
+ preserveControlChars: true,
110
+ };
111
+ trace('AnsiUtils', () => 'ANSI config reset to defaults');
112
+ }
113
+
114
+ /**
115
+ * Process output data according to current ANSI configuration
116
+ * @param {Buffer|string} data - Data to process
117
+ * @param {object} options - Override options
118
+ * @returns {Buffer|string} Processed data
119
+ */
120
+ export function processOutput(data, options = {}) {
121
+ trace(
122
+ 'AnsiUtils',
123
+ () =>
124
+ `processOutput() called | ${JSON.stringify(
125
+ {
126
+ dataType: typeof data,
127
+ dataLength: Buffer.isBuffer(data) ? data.length : data?.length,
128
+ options,
129
+ },
130
+ null,
131
+ 2
132
+ )}`
133
+ );
134
+ const config = { ...globalAnsiConfig, ...options };
135
+ if (!config.preserveAnsi && !config.preserveControlChars) {
136
+ return AnsiUtils.cleanForProcessing(data);
137
+ } else if (!config.preserveAnsi) {
138
+ return Buffer.isBuffer(data)
139
+ ? Buffer.from(AnsiUtils.stripAnsi(data.toString('utf8')))
140
+ : AnsiUtils.stripAnsi(data);
141
+ } else if (!config.preserveControlChars) {
142
+ return Buffer.isBuffer(data)
143
+ ? Buffer.from(AnsiUtils.stripControlChars(data.toString('utf8')))
144
+ : AnsiUtils.stripControlChars(data);
145
+ }
146
+ return data;
147
+ }