forked from ericcornelissen/shescape
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cjs
937 lines (851 loc) · 26.6 KB
/
index.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
'use strict';
var os = require('os');
var process = require('process');
var fs = require('fs');
var path = require('path');
var util = require('util');
var which = require('which');
function _interopNamespaceDefault(e) {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
/**
* @overview Provides functionality related to working with executables.
* @license MPL-2.0
*/
/**
* Resolves the location of an executable given an arbitrary valid string
* representation of that executable.
*
* To obtain the location of the executable this function (if necessary):
* - Expands the provided string to a absolute path.
* - Follows symbolic links.
*
* @param {object} args The arguments for this function.
* @param {string} args.executable A string representation of the executable.
* @param {object} deps The dependencies for this function.
* @param {Function} deps.exists A function to check if a file exists.
* @param {Function} deps.readlink A function to resolve (sym)links.
* @param {Function} deps.which A function to perform a `which(1)`-like lookup.
* @returns {string} The full path to the binary of the executable.
* @throws {Error} If the `deps` aren't provided.
*/
function resolveExecutable({ executable }, { exists, readlink, which }) {
if (readlink === undefined || which === undefined) {
throw new Error();
}
try {
executable = which(executable);
} catch (_) {
// For backwards compatibility return the executable even if its location
// cannot be obtained
return executable;
}
if (!exists(executable)) {
// For backwards compatibility return the executable even if there exists no
// file at the specified path
return executable;
}
try {
executable = readlink(executable);
} catch (_) {
// An error will be thrown if the executable is not a (sym)link, this is not
// a problem so the error is ignored
}
return executable;
}
/**
* @overview Provides an API to consistently escape or quote shell arguments
* across platforms.
* @license MPL-2.0
*/
/**
* The error message for incorrect parameter types.
*
* @constant
* @type {string}
*/
const typeError =
"Shescape requires strings or values that can be converted into a string using .toString()";
/**
* The `typeof` value of functions.
*
* @constant
* @type {string}
*/
const typeofFunction = "function";
/**
* The `typeof` value of strings.
*
* @constant
* @type {string}
*/
const typeofString = "string";
/**
* Checks if a value is a string.
*
* @param {any} value The value of interest.
* @returns {boolean} `true` if `value` is a string, `false` otherwise.
*/
function isString(value) {
return typeof value === typeofString;
}
/**
* Checks if a value can be converted into a string.
*
* @param {any} value The value of interest.
* @returns {boolean} `true` if `value` is stringable, `false` otherwise.
*/
function isStringable(value) {
if (value === undefined || value === null) {
return false;
}
if (typeof value.toString !== typeofFunction) {
return false;
}
const str = value.toString();
return isString(str);
}
/**
* Parses options provided to {@link escapeShellArg} or {@link quoteShellArg}.
*
* @param {object} args The arguments for this function.
* @param {object} args.options The options for escaping.
* @param {string} [args.options.shell] The shell to escape for.
* @param {boolean} [args.options.interpolation] Is interpolation enabled.
* @param {object} args.process The `process` values.
* @param {object} args.process.env The environment variables.
* @param {object} deps The dependencies for this function.
* @param {Function} deps.getDefaultShell Get the default shell for the system.
* @param {Function} deps.getShellName Get the name of a shell.
* @returns {object} The parsed arguments.
*/
function parseOptions(
{ options: { interpolation, shell }, process: { env } },
{ getDefaultShell, getShellName }
) {
interpolation = interpolation ? true : false;
shell = isString(shell) ? shell : getDefaultShell({ env });
const shellName = getShellName({ shell }, { resolveExecutable });
return { interpolation, shellName };
}
/**
* Escapes an argument for the given shell.
*
* @param {object} args The arguments for this function.
* @param {string} args.arg The argument to escape.
* @param {boolean} args.interpolation Is interpolation enabled.
* @param {boolean} args.quoted Is `arg` being quoted.
* @param {string} args.shellName The name of the shell to escape `arg` for.
* @param {object} deps The dependencies for this function.
* @param {Function} deps.getEscapeFunction Get the escape function for a shell.
* @returns {string} The escaped argument.
* @throws {TypeError} The argument to escape is not stringable.
*/
function escape$1(
{ arg, interpolation, quoted, shellName },
{ getEscapeFunction }
) {
if (!isStringable(arg)) {
throw new TypeError(typeError);
}
const argAsString = arg.toString();
const escape = getEscapeFunction(shellName);
const escapedArg = escape(argAsString, { interpolation, quoted });
return escapedArg;
}
/**
* Quotes and escape an argument for the given shell.
*
* @param {object} args The arguments for this function.
* @param {string} args.arg The argument to escape.
* @param {string} args.shellName The name of the shell to escape `arg` for.
* @param {object} deps The dependencies for this function.
* @param {Function} deps.getEscapeFunction Get the escape function for a shell.
* @param {Function} deps.getQuoteFunction Get the quote function for a shell.
* @returns {string} The quoted and escaped argument.
* @throws {TypeError} The argument to escape is not stringable.
*/
function quote$1({ arg, shellName }, { getEscapeFunction, getQuoteFunction }) {
const escapedArg = escape$1(
{ arg, interpolation: false, quoted: true, shellName },
{ getEscapeFunction }
);
const quote = getQuoteFunction(shellName);
const escapedAndQuotedArg = quote(escapedArg);
return escapedAndQuotedArg;
}
/**
* Escapes an argument for the given shell.
*
* @param {object} args The arguments for this function.
* @param {string} args.arg The argument to escape.
* @param {object} args.options The options for escaping `arg`.
* @param {boolean} [args.options.interpolation] Is interpolation enabled.
* @param {string} [args.options.shell] The shell to escape `arg` for.
* @param {object} args.process The `process` values.
* @param {object} args.process.env The environment variables.
* @param {object} deps The dependencies for this function.
* @param {Function} deps.getDefaultShell Get the default shell for the system.
* @param {Function} deps.getEscapeFunction Get an escape function for a shell.
* @param {Function} deps.getShellName Get the name of a shell.
* @returns {string} The escaped argument.
*/
function escapeShellArg(
{ arg, options: { interpolation, shell }, process: { env } },
{ getDefaultShell, getEscapeFunction, getShellName }
) {
const options = parseOptions(
{ options: { interpolation, shell }, process: { env } },
{ getDefaultShell, getShellName }
);
return escape$1(
{
arg,
interpolation: options.interpolation,
quoted: false,
shellName: options.shellName,
},
{ getEscapeFunction }
);
}
/**
* Quotes and escape an argument for the given shell.
*
* @param {object} args The arguments for this function.
* @param {string} args.arg The argument to escape.
* @param {object} args.options The options for escaping `arg`.
* @param {string} [args.options.shell] The shell to escape `arg` for.
* @param {object} args.process The `process` values.
* @param {object} args.process.env The environment variables.
* @param {object} deps The dependencies for this function.
* @param {Function} deps.getDefaultShell Get the default shell for the system.
* @param {Function} deps.getEscapeFunction Get an escape function for a shell.
* @param {Function} deps.getQuoteFunction Get a quote function for a shell.
* @param {Function} deps.getShellName Get the name of a shell.
* @returns {string} The quoted and escaped argument.
*/
function quoteShellArg(
{ arg, options: { shell }, process: { env } },
{ getDefaultShell, getEscapeFunction, getQuoteFunction, getShellName }
) {
const options = parseOptions(
{ options: { shell }, process: { env } },
{ getDefaultShell, getShellName }
);
return quote$1(
{ arg, shellName: options.shellName },
{ getEscapeFunction, getQuoteFunction }
);
}
/**
* @overview Provides functionality specifically for Unix systems.
* @license MPL-2.0
*/
/**
* The name of the Bourne-again shell (Bash) binary.
*
* @constant
* @type {string}
*/
const binBash = "bash";
/**
* The name of the C shell (csh) binary.
*
* @constant
* @type {string}
*/
const binCsh = "csh";
/**
* The name of the Debian Almquist shell (Dash) binary.
*
* @constant
* @type {string}
*/
const binDash = "dash";
/**
* The name of the Z shell (Zsh) binary.
*
* @constant
* @type {string}
*/
const binZsh = "zsh";
/**
* Escapes a shell argument for use in Bash(-like shells).
*
* @param {string} arg The argument to escape.
* @param {object} options The escape options.
* @param {boolean} options.interpolation Is interpolation enabled.
* @param {boolean} options.quoted Is `arg` being quoted.
* @returns {string} The escaped argument.
*/
function escapeArgBash(arg, { interpolation, quoted }) {
let result = arg
.replace(/[\0\u0008\u001B\u009B]/gu, "")
.replace(/\r(?!\n)/gu, "");
if (interpolation) {
result = result
.replace(/\\/gu, "\\\\")
.replace(/\r?\n/gu, " ")
.replace(/(^|\s)([#~])/gu, "$1\\$2")
.replace(/(["$&'()*;<>?`{|])/gu, "\\$1")
.replace(/(?<=[:=])(~)(?=[\s+\-/0:=]|$)/gu, "\\$1")
.replace(/([\t ])/gu, "\\$1");
} else if (quoted) {
result = result.replace(/'/gu, `'\\''`);
}
return result;
}
/**
* Escapes a shell argument for use in csh.
*
* @param {string} arg The argument to escape.
* @param {object} options The escape options.
* @param {boolean} options.interpolation Is interpolation enabled.
* @param {boolean} options.quoted Is `arg` being quoted.
* @returns {string} The escaped argument.
*/
function escapeArgCsh(arg, { interpolation, quoted }) {
let result = arg
.replace(/[\0\u0008\u001B\u009B]/gu, "")
.replace(/\r?\n|\r/gu, " ");
if (interpolation) {
result = result
.replace(/\\/gu, "\\\\")
.replace(/(^|\s)(~)/gu, "$1\\$2")
.replace(/(["#$&'()*;<>?[`{|])/gu, "\\$1")
.replace(/([\t ])/gu, "\\$1");
const textEncoder = new util.TextEncoder();
result = result
.split("")
.map(
// Due to a bug in C shell version 20110502-7, when a character whose
// utf-8 encoding includes the bytes 0xA0 (160 in decimal) appears in
// an argument after an escaped character, it will hang and endlessly
// consume memory unless the character is escaped with quotes.
// ref: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=995013
(char) => (textEncoder.encode(char).includes(160) ? `'${char}'` : char)
)
.join("");
} else {
result = result.replace(/\\!$/gu, "\\\\!");
if (quoted) {
result = result.replace(/'/gu, `'\\''`);
}
}
result = result.replace(/!(?!$)/gu, "\\!");
return result;
}
/**
* Escapes a shell argument for use in Dash.
*
* @param {string} arg The argument to escape.
* @param {object} options The escape options.
* @param {boolean} options.interpolation Is interpolation enabled.
* @param {boolean} options.quoted Is `arg` being quoted.
* @returns {string} The escaped argument.
*/
function escapeArgDash(arg, { interpolation, quoted }) {
let result = arg
.replace(/[\0\u0008\u001B\u009B]/gu, "")
.replace(/\r(?!\n)/gu, "");
if (interpolation) {
result = result
.replace(/\\/gu, "\\\\")
.replace(/\r?\n/gu, " ")
.replace(/(^|\s)([#~])/gu, "$1\\$2")
.replace(/(["$&'()*;<>?`|])/gu, "\\$1")
.replace(/([\t\n ])/gu, "\\$1");
} else if (quoted) {
result = result.replace(/'/gu, `'\\''`);
}
return result;
}
/**
* Escapes a shell argument for use in Zsh.
*
* @param {string} arg The argument to escape.
* @param {object} options The escape options.
* @param {boolean} options.interpolation Is interpolation enabled.
* @param {boolean} options.quoted Is `arg` being quoted.
* @returns {string} The escaped argument.
*/
function escapeArgZsh(arg, { interpolation, quoted }) {
let result = arg
.replace(/[\0\u0008\u001B\u009B]/gu, "")
.replace(/\r(?!\n)/gu, "");
if (interpolation) {
result = result
.replace(/\\/gu, "\\\\")
.replace(/\r?\n/gu, " ")
.replace(/(^|\s)([#=~])/gu, "$1\\$2")
.replace(/(["$&'()*;<>?[\]`{|}])/gu, "\\$1")
.replace(/([\t ])/gu, "\\$1");
} else if (quoted) {
result = result.replace(/'/gu, `'\\''`);
}
return result;
}
/**
* Quotes an argument for use in a Unix shell.
*
* @param {string} arg The argument to quote.
* @returns {string} The quoted argument.
*/
function quoteArg$1(arg) {
return `'${arg}'`;
}
/**
* Returns the basename of a directory or file path on a Unix system.
*
* @param {string} fullPath A Unix-style directory or file path.
* @returns {string} The basename of `fullPath`.
*/
function getBasename$1(fullPath) {
return path__namespace.basename(fullPath);
}
/**
* Returns the default shell for Unix systems.
*
* For more information, see `options.shell` in:
* https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback.
*
* @returns {string} The default shell.
*/
function getDefaultShell$1() {
return "/bin/sh";
}
/**
* Returns a function to escape arguments for use in a particular shell.
*
* @param {string} shellName The name of a Unix shell.
* @returns {Function?} A function to escape arguments for use in the shell.
*/
function getEscapeFunction$1(shellName) {
switch (shellName) {
case binBash:
return escapeArgBash;
case binCsh:
return escapeArgCsh;
case binDash:
return escapeArgDash;
case binZsh:
return escapeArgZsh;
default:
return null;
}
}
/**
* Returns a function to quote arguments for use in a particular shell.
*
* @param {string} shellName The name of a Unix shell.
* @returns {Function?} A function to quote arguments for use in the shell.
*/
function getQuoteFunction$1(shellName) {
switch (shellName) {
case binBash:
case binCsh:
case binDash:
case binZsh:
return quoteArg$1;
default:
return null;
}
}
/**
* Determines the name of the shell identified by a file path or file name.
*
* @param {object} args The arguments for this function.
* @param {string} args.shell The name or path of the shell.
* @param {object} deps The dependencies for this function.
* @param {Function} deps.resolveExecutable Resolve the path to an executable.
* @returns {string} The shell name.
*/
function getShellName$1({ shell }, { resolveExecutable }) {
shell = resolveExecutable(
{ executable: shell },
{ exists: fs__namespace.existsSync, readlink: fs__namespace.readlinkSync, which: which.sync }
);
const shellName = getBasename$1(shell);
if (getEscapeFunction$1(shellName) === null) {
return binBash;
}
return shellName;
}
var unix = /*#__PURE__*/Object.freeze({
__proto__: null,
getDefaultShell: getDefaultShell$1,
getEscapeFunction: getEscapeFunction$1,
getQuoteFunction: getQuoteFunction$1,
getShellName: getShellName$1
});
/**
* @overview Provides functionality specifically for Windows systems.
* @license MPL-2.0
*/
/**
* The name of the Windows Command Prompt binary.
*
* @constant
* @type {string}
*/
const binCmd = "cmd.exe";
/**
* The name of the Windows PowerShell binary.
*
* @constant
* @type {string}
*/
const binPowerShell = "powershell.exe";
/**
* Escapes a shell argument for use in Windows Command Prompt.
*
* @param {string} arg The argument to escape.
* @param {object} options The escape options.
* @param {boolean} options.interpolation Is interpolation enabled.
* @param {boolean} options.quoted Is `arg` being quoted.
* @returns {string} The escaped argument.
*/
function escapeArgCmd(arg, { interpolation, quoted }) {
let result = arg
.replace(/[\0\u0008\u001B\u009B]/gu, "")
.replace(/\r?\n|\r/gu, " ");
if (interpolation) {
result = result.replace(/\^/gu, "^^").replace(/(["&<>|])/gu, "^$1");
} else if (quoted) {
result = result.replace(/"/gu, `""`);
}
return result;
}
/**
* Escapes a shell argument for use in Windows PowerShell.
*
* @param {string} arg The argument to escape.
* @param {object} options The escape options.
* @param {boolean} options.interpolation Is interpolation enabled.
* @param {boolean} options.quoted Is `arg` being quoted.
* @returns {string} The escaped argument.
*/
function escapeArgPowerShell(arg, { interpolation, quoted }) {
let result = arg
.replace(/[\0\u0008\u001B\u009B]/gu, "")
.replace(/`/gu, "``")
.replace(/\$/gu, "`$$")
.replace(/\r(?!\n)/gu, "");
if (interpolation) {
result = result
.replace(/\r?\n/gu, " ")
.replace(/(^|[\s\u0085])([*1-6]?)(>)/gu, "$1$2`$3")
.replace(/(^|[\s\u0085])([#\-:<@\]])/gu, "$1`$2")
.replace(/(["&'(),;{|}‘’‚‛“”„])/gu, "`$1")
.replace(/([\s\u0085])/gu, "`$1");
} else if (quoted) {
result = result.replace(/(["“”„])/gu, "$1$1");
}
return result;
}
/**
* Quotes an argument for use in a Windows shell.
*
* @param {string} arg The argument to quote.
* @returns {string} The quoted argument.
*/
function quoteArg(arg) {
return `"${arg}"`;
}
/**
* Returns the basename of a directory or file path on a Windows system.
*
* @param {string} fullPath A Windows-style directory or file path.
* @returns {string} The basename of `fullPath`.
*/
function getBasename(fullPath) {
return path__namespace.win32.basename(fullPath);
}
/**
* Returns the default shell for Windows systems.
*
* For more information, see:
* https://nodejs.org/api/child_process.html#default-windows-shell.
*
* @param {object} args The arguments for this function.
* @param {object} args.env The environment variables.
* @param {string} [args.env.ComSpec] The %COMSPEC% value.
* @returns {string} The default shell.
*/
function getDefaultShell({ env: { ComSpec } }) {
if (ComSpec !== undefined) {
return ComSpec;
}
return binCmd;
}
/**
* Returns a function to escape arguments for use in a particular shell.
*
* @param {string} shellName The name of a Windows shell.
* @returns {Function?} A function to escape arguments for use in the shell.
*/
function getEscapeFunction(shellName) {
switch (shellName) {
case binCmd:
return escapeArgCmd;
case binPowerShell:
return escapeArgPowerShell;
default:
return null;
}
}
/**
* Returns a function to quote arguments for use in a particular shell.
*
* @param {string} shellName The name of a Windows shell.
* @returns {Function?} A function to quote arguments for use in the shell.
*/
function getQuoteFunction(shellName) {
switch (shellName) {
case binCmd:
case binPowerShell:
return quoteArg;
default:
return null;
}
}
/**
* Determines the name of the shell identified by a file path or file name.
*
* @param {object} args The arguments for this function.
* @param {string} args.shell The name or path of the shell.
* @param {object} deps The dependencies for this function.
* @param {Function} deps.resolveExecutable Resolve the path to an executable.
* @returns {string} The shell name.
*/
function getShellName({ shell }, { resolveExecutable }) {
shell = resolveExecutable(
{ executable: shell },
{ exists: fs__namespace.existsSync, readlink: fs__namespace.readlinkSync, which: which.sync }
);
const shellName = getBasename(shell);
if (getEscapeFunction(shellName) === null) {
return binCmd;
}
return shellName;
}
var win = /*#__PURE__*/Object.freeze({
__proto__: null,
getDefaultShell: getDefaultShell,
getEscapeFunction: getEscapeFunction,
getQuoteFunction: getQuoteFunction,
getShellName: getShellName
});
/**
* @overview Provides functionality related to getting the platform module for
* the current system.
* @license MPL-2.0
*/
/**
* The string identifying the OS type Cygwin.
*
* @constant
* @type {string}
*/
const cygwin = "cygwin";
/**
* The string identifying the OS type MSYS.
*
* @constant
* @type {string}
*/
const msys = "msys";
/**
* The string identifying Windows platforms.
*
* @constant
* @type {string}
*/
const win32 = "win32";
/**
* Checks if the current system is a Windows system.
*
* @param {object} args The arguments for this function.
* @param {Object<string, string>} args.env The environment variables.
* @param {string} args.platform The `os.platform()` value.
* @returns {boolean} `true` if the system is Windows, `false` otherwise.
*/
function isWindow({ env, platform }) {
return env.OSTYPE === cygwin || env.OSTYPE === msys || platform === win32;
}
/**
* Returns all helper functions for a specific system.
*
* @param {object} args The arguments for this function.
* @param {Object<string, string>} args.env The environment variables.
* @param {string} args.platform The `os.platform()` value.
* @returns {object} The helper functions for the current system.
*/
function getHelpersByPlatform({ env, platform }) {
if (isWindow({ env, platform })) {
return win;
}
return unix;
}
/**
* A simple shell escape library. Use it to escape user-controlled inputs to
* shell commands to prevent shell injection.
*
* @overview Entrypoint for the library.
* @module shescape
* @version 1.6.4
* @license MPL-2.0
*/
/**
* Get the helper functions for the current platform.
*
* @returns {object} The helper functions for the current platform.
*/
function getPlatformHelpers() {
const platform = os.platform();
const helpers = getHelpersByPlatform({ env: process.env, platform });
return helpers;
}
/**
* Converts the provided value into an array if it is not already an array and
* returns the array.
*
* @param {Array | any} x The value to convert to an array if necessary.
* @returns {Array} An array containing `x` or `x` itself.
*/
function toArrayIfNecessary(x) {
return Array.isArray(x) ? x : [x];
}
/**
* Take a single value, the argument, and escape any dangerous characters.
*
* Non-string inputs will be converted to strings using a `toString()` method.
*
* NOTE: when the `interpolation` option is set to `true`, whitespace is escaped
* to prevent argument splitting except for cmd.exe (which does not support it).
*
* @example
* import { spawn } from "node:child_process";
* spawn(
* "echo",
* ["Hello", shescape.escape(userInput)],
* null // `options.shell` MUST be falsy
* );
* @param {string} arg The argument to escape.
* @param {object} [options] The escape options.
* @param {boolean} [options.interpolation=false] Is interpolation enabled.
* @param {boolean | string} [options.shell] The shell to escape for.
* @returns {string} The escaped argument.
* @throws {TypeError} The argument is not stringable.
* @since 0.1.0
*/
function escape(arg, options = {}) {
const helpers = getPlatformHelpers();
return escapeShellArg({ arg, options, process }, helpers);
}
/**
* Take a array of values, the arguments, and escape any dangerous characters in
* every argument.
*
* Non-array inputs will be converted to one-value arrays and non-string values
* will be converted to strings using a `toString()` method.
*
* @example
* import { spawn } from "node:child_process";
* spawn(
* "echo",
* shescape.escapeAll(["Hello", userInput]),
* null // `options.shell` MUST be falsy
* );
* @param {string[]} args The arguments to escape.
* @param {object} [options] The escape options.
* @param {boolean} [options.interpolation=false] Is interpolation enabled.
* @param {boolean | string} [options.shell] The shell to escape for.
* @returns {string[]} The escaped arguments.
* @throws {TypeError} One of the arguments is not stringable.
* @since 1.1.0
*/
function escapeAll(args, options = {}) {
args = toArrayIfNecessary(args);
return args.map((arg) => escape(arg, options));
}
/**
* Take a single value, the argument, put OS-specific quotes around it and
* escape any dangerous characters.
*
* Non-string inputs will be converted to strings using a `toString()` method.
*
* @example
* import { spawn } from "node:child_process";
* const spawnOptions = { shell: true }; // `options.shell` SHOULD be truthy
* const shescapeOptions = { ...spawnOptions };
* spawn(
* "echo",
* ["Hello", shescape.quote(userInput, shescapeOptions)],
* spawnOptions
* );
* @example
* import { exec } from "node:child_process";
* const execOptions = null || { };
* const shescapeOptions = { ...execOptions };
* exec(
* `echo Hello ${shescape.quote(userInput, shescapeOptions)}`,
* execOptions
* );
* @param {string} arg The argument to quote and escape.
* @param {object} [options] The escape and quote options.
* @param {boolean | string} [options.shell] The shell to escape for.
* @returns {string} The quoted and escaped argument.
* @throws {TypeError} The argument is not stringable.
* @since 0.3.0
*/
function quote(arg, options = {}) {
const helpers = getPlatformHelpers();
return quoteShellArg({ arg, options, process }, helpers);
}
/**
* Take an array of values, the arguments, put OS-specific quotes around every
* argument and escape any dangerous characters in every argument.
*
* Non-array inputs will be converted to one-value arrays and non-string values
* will be converted to strings using a `toString()` method.
*
* @example
* import { spawn } from "node:child_process";
* const spawnOptions = { shell: true }; // `options.shell` SHOULD be truthy
* const shescapeOptions = { ...spawnOptions };
* spawn(
* "echo",
* shescape.quoteAll(["Hello", userInput], shescapeOptions),
* spawnOptions
* );
* @param {string[]} args The arguments to quote and escape.
* @param {object} [options] The escape and quote options.
* @param {boolean | string} [options.shell] The shell to escape for.
* @returns {string[]} The quoted and escaped arguments.
* @throws {TypeError} One of the arguments is not stringable.
* @since 0.4.0
*/
function quoteAll(args, options = {}) {
args = toArrayIfNecessary(args);
return args.map((arg) => quote(arg, options));
}
exports.escape = escape;
exports.escapeAll = escapeAll;
exports.quote = quote;
exports.quoteAll = quoteAll;