@@ -9,35 +9,39 @@ const testStrings = [
9
9
'Simple string' ,
10
10
'String with spaces' ,
11
11
'String with "double quotes"' ,
12
- ' String with \ 'single quotes\'' ,
12
+ " String with 'single quotes'" ,
13
13
'String with $variable' ,
14
14
'String with `backticks`' ,
15
15
'String with newline\ncharacter' ,
16
16
'String with & and | operators' ,
17
17
'String with > redirect' ,
18
18
'String with * wildcard' ,
19
- 'Complex string with "quotes", \'single\', $var, `backticks`, \n, and special chars &|><*'
19
+ 'Complex string with "quotes", \'single\', $var, `backticks`, \n, and special chars &|><*' ,
20
20
] ;
21
21
22
22
console . log ( '=== Testing stdinContent approaches ===' ) ;
23
23
24
24
// Helper function to wait for all tests to complete
25
- const wait = ( ms ) => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
25
+ const wait = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
26
26
27
27
// Helper function to execute a command with encoded content
28
- const execWithEncodedContent = async ( command , content , isWindows = process . platform === 'win32' ) => {
28
+ const execWithEncodedContent = async (
29
+ command ,
30
+ content ,
31
+ isWindows = process . platform === 'win32' ,
32
+ ) => {
29
33
return new Promise ( ( resolve , reject ) => {
30
34
const encodedContent = Buffer . from ( content ) . toString ( 'base64' ) ;
31
35
let cmd ;
32
-
36
+
33
37
if ( isWindows ) {
34
38
// Windows approach using PowerShell
35
39
cmd = `powershell -Command "[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${ encodedContent } ')) | ${ command } "` ;
36
40
} else {
37
41
// POSIX approach (Linux/macOS)
38
42
cmd = `echo "${ encodedContent } " | base64 -d | ${ command } ` ;
39
43
}
40
-
44
+
41
45
exec ( cmd , ( error , stdout , stderr ) => {
42
46
if ( error ) {
43
47
reject ( error ) ;
@@ -52,7 +56,7 @@ const execWithEncodedContent = async (command, content, isWindows = process.plat
52
56
console . log ( '\n=== Testing Base64 encoding approach ===' ) ;
53
57
for ( const str of testStrings ) {
54
58
console . log ( `\nOriginal: "${ str } "` ) ;
55
-
59
+
56
60
try {
57
61
// Test the encoded content approach
58
62
const { stdout } = await execWithEncodedContent ( 'cat' , str ) ;
@@ -61,7 +65,7 @@ for (const str of testStrings) {
61
65
} catch ( error ) {
62
66
console . error ( `Error: ${ error . message } ` ) ;
63
67
}
64
-
68
+
65
69
// Add a small delay to ensure orderly output
66
70
await wait ( 100 ) ;
67
71
}
@@ -70,30 +74,33 @@ for (const str of testStrings) {
70
74
console . log ( '\n=== Comparing with temporary file approach ===' ) ;
71
75
for ( const str of testStrings ) {
72
76
console . log ( `\nOriginal: "${ str } "` ) ;
73
-
77
+
74
78
// Create a temporary file with the content
75
79
const tempFile = path . join ( os . tmpdir ( ) , `test-content-${ Date . now ( ) } .txt` ) ;
76
80
fs . writeFileSync ( tempFile , str ) ;
77
-
81
+
78
82
// Execute command using the temporary file
79
83
exec ( `cat "${ tempFile } "` , async ( error , stdout , stderr ) => {
80
84
console . log ( `Output (temp file): "${ stdout . trim ( ) } "` ) ;
81
85
console . log ( `Success (temp file): ${ stdout . trim ( ) === str } ` ) ;
82
-
86
+
83
87
try {
84
88
// Test the encoded content approach
85
- const { stdout : encodedStdout } = await execWithEncodedContent ( 'cat' , str ) ;
89
+ const { stdout : encodedStdout } = await execWithEncodedContent (
90
+ 'cat' ,
91
+ str ,
92
+ ) ;
86
93
console . log ( `Output (encoded): "${ encodedStdout . trim ( ) } "` ) ;
87
94
console . log ( `Success (encoded): ${ encodedStdout . trim ( ) === str } ` ) ;
88
95
console . log ( `Match: ${ stdout . trim ( ) === encodedStdout . trim ( ) } ` ) ;
89
96
} catch ( error ) {
90
97
console . error ( `Error: ${ error . message } ` ) ;
91
98
}
92
-
99
+
93
100
// Clean up the temporary file
94
101
fs . unlinkSync ( tempFile ) ;
95
102
} ) ;
96
-
103
+
97
104
// Add a small delay to ensure orderly output
98
105
await wait ( 300 ) ;
99
- }
106
+ }
0 commit comments