-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Offer a "oneshot" module that doesn't require initializing a class
- Loading branch information
1 parent
76fb88c
commit 2fec11c
Showing
9 changed files
with
265 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ _reports/ | |
crash-* | ||
index.cjs | ||
index.d.cts | ||
oneshot.cjs | ||
oneshot.d.cts | ||
testing.cjs | ||
testing.d.cts | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* @overview Contains TypeScript type definitions for Shescape's oneshot | ||
* alternative. | ||
* @license MPL-2.0 | ||
*/ | ||
|
||
import type { ShescapeOptions } from "shescape"; | ||
|
||
/** | ||
* Take a single value, the argument, and escape any dangerous characters. | ||
* | ||
* Non-string inputs will be converted to strings using a `toString()` method. | ||
* | ||
* @example | ||
* import { spawn } from "node:child_process"; | ||
* spawn( | ||
* "echo", | ||
* ["Hello", shescape.escape(userInput, { shell: false })], | ||
* null // `options.shell` MUST be falsy | ||
* ); | ||
* @param {string} arg The argument to escape. | ||
* @param {object} [options] The escape options. | ||
* @param {boolean} [options.flagProtection=true] Is flag protection enabled. | ||
* @param {boolean | string} [options.shell=true] The shell to escape for. | ||
* @returns {string} The escaped argument. | ||
* @throws {TypeError} The argument is not stringable. | ||
* @throws {Error} The shell is not supported. | ||
* @since 0.1.0 | ||
*/ | ||
export function escape(arg: string, options?: ShescapeOptions): string; | ||
|
||
/** | ||
* Take an 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], { shell: false }), | ||
* null // `options.shell` MUST be falsy | ||
* ); | ||
* @param {string[]} args The arguments to escape. | ||
* @param {object} [options] The escape options. | ||
* @param {boolean} [options.flagProtection=true] Is flag protection enabled. | ||
* @param {boolean | string} [options.shell=true] The shell to escape for. | ||
* @returns {string[]} The escaped arguments. | ||
* @throws {TypeError} One of the arguments is not stringable. | ||
* @throws {Error} The shell is not supported. | ||
* @since 1.1.0 | ||
*/ | ||
export function escapeAll(args: string[], options?: ShescapeOptions): string[]; | ||
|
||
/** | ||
* Take a single value, the argument, put shell-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 | ||
* spawn( | ||
* "echo", | ||
* ["Hello", shescape.quote(userInput, { shell: spawnOptions.shell })], | ||
* spawnOptions | ||
* ); | ||
* @param {string} arg The argument to quote and escape. | ||
* @param {object} [options] The escape options. | ||
* @param {boolean} [options.flagProtection=true] Is flag protection enabled. | ||
* @param {boolean | string} [options.shell=true] The shell to escape for. | ||
* @returns {string} The quoted and escaped argument. | ||
* @throws {TypeError} The argument is not stringable. | ||
* @throws {Error} The shell is not supported. | ||
* @throws {Error} Quoting is not supported with `shell: false`. | ||
* @since 0.3.0 | ||
*/ | ||
export function quote(arg: string, options?: ShescapeOptions): string; | ||
|
||
/** | ||
* Take an array of values, the arguments, put shell-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 | ||
* spawn( | ||
* "echo", | ||
* shescape.quoteAll(["Hello", userInput], { shell: spawnOptions.shell }), | ||
* spawnOptions | ||
* ); | ||
* @param {string[]} args The arguments to quote and escape. | ||
* @param {object} [options] The escape options. | ||
* @param {boolean} [options.flagProtection=true] Is flag protection enabled. | ||
* @param {boolean | string} [options.shell=true] The shell to escape for. | ||
* @returns {string[]} The quoted and escaped arguments. | ||
* @throws {TypeError} One of the arguments is not stringable. | ||
* @throws {Error} The shell is not supported. | ||
* @throws {Error} Quoting is not supported with `shell: false`. | ||
* @since 0.4.0 | ||
*/ | ||
export function quoteAll(args: string[], options?: ShescapeOptions): string[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* @overview Alternative entrypoint for the library requiring no setup. | ||
* @license MPL-2.0 | ||
*/ | ||
|
||
import { Shescape } from "./index.js"; | ||
|
||
/** | ||
* Take a single value, the argument, and escape any dangerous characters. | ||
* | ||
* Non-string inputs will be converted to strings using a `toString()` method. | ||
* | ||
* @example | ||
* import { spawn } from "node:child_process"; | ||
* spawn( | ||
* "echo", | ||
* ["Hello", shescape.escape(userInput, { shell: false })], | ||
* null // `options.shell` MUST be falsy | ||
* ); | ||
* @param {string} arg The argument to escape. | ||
* @param {object} [options] The escape options. | ||
* @param {boolean} [options.flagProtection=true] Is flag protection enabled. | ||
* @param {boolean | string} [options.shell=true] The shell to escape for. | ||
* @returns {string} The escaped argument. | ||
* @throws {TypeError} The argument is not stringable. | ||
* @throws {Error} The shell is not supported. | ||
* @since 0.1.0 | ||
*/ | ||
export function escape(arg, options) { | ||
const shescape = new Shescape(options); | ||
return shescape.escape(arg); | ||
} | ||
|
||
/** | ||
* Take an 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], { shell: false }), | ||
* null // `options.shell` MUST be falsy | ||
* ); | ||
* @param {string[]} args The arguments to escape. | ||
* @param {object} [options] The escape options. | ||
* @param {boolean} [options.flagProtection=true] Is flag protection enabled. | ||
* @param {boolean | string} [options.shell=true] The shell to escape for. | ||
* @returns {string[]} The escaped arguments. | ||
* @throws {TypeError} One of the arguments is not stringable. | ||
* @throws {Error} The shell is not supported. | ||
* @since 1.1.0 | ||
*/ | ||
export function escapeAll(args, options) { | ||
const shescape = new Shescape(options); | ||
return shescape.escapeAll(args); | ||
} | ||
|
||
/** | ||
* Take a single value, the argument, put shell-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 | ||
* spawn( | ||
* "echo", | ||
* ["Hello", shescape.quote(userInput, { shell: spawnOptions.shell })], | ||
* spawnOptions | ||
* ); | ||
* @param {string} arg The argument to quote and escape. | ||
* @param {object} [options] The escape options. | ||
* @param {boolean} [options.flagProtection=true] Is flag protection enabled. | ||
* @param {boolean | string} [options.shell=true] The shell to escape for. | ||
* @returns {string} The quoted and escaped argument. | ||
* @throws {TypeError} The argument is not stringable. | ||
* @throws {Error} The shell is not supported. | ||
* @throws {Error} Quoting is not supported with `shell: false`. | ||
* @since 0.3.0 | ||
*/ | ||
export function quote(arg, options) { | ||
const shescape = new Shescape(options); | ||
return shescape.quote(arg); | ||
} | ||
|
||
/** | ||
* Take an array of values, the arguments, put shell-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 | ||
* spawn( | ||
* "echo", | ||
* shescape.quoteAll(["Hello", userInput], { shell: spawnOptions.shell }), | ||
* spawnOptions | ||
* ); | ||
* @param {string[]} args The arguments to quote and escape. | ||
* @param {object} [options] The escape options. | ||
* @param {boolean} [options.flagProtection=true] Is flag protection enabled. | ||
* @param {boolean | string} [options.shell=true] The shell to escape for. | ||
* @returns {string[]} The quoted and escaped arguments. | ||
* @throws {TypeError} One of the arguments is not stringable. | ||
* @throws {Error} The shell is not supported. | ||
* @throws {Error} Quoting is not supported with `shell: false`. | ||
* @since 0.4.0 | ||
*/ | ||
export function quoteAll(args, options) { | ||
const shescape = new Shescape(options); | ||
return shescape.quoteAll(args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters