
Security News
Software Engineering Daily Podcast: Feross on AI, Open Source, and Supply Chain Risk
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.
fetch-executable
Advanced tools
Fetch an executable, but only if needed
Intended for use downloading exact versions of executables and storing them on a per-project basis, rather than relying on a globally stored version being good enough for everything.
npm i --save fetch-executable
Then:
import { fetchExecutable } from 'fetch-executable';
await fetchExecutable({
target: './my-exec',
url: 'https://example.com/download/my-exec/{version}',
version: '1.2.3',
versionExecArgs: ['--version'],
});
This will:
./my-exec --version and check if the output is 1.2.3https://example.com/download/my-exec/1.2.3./my-execThere is a selection of executables for which the steps to download and check the version are bundled. These can be installed with eg:
import { kubectl } from 'fetch-executable/executables';
await kubectl('./kubectl', '1.23.0');
The full set is as follows, where the name of the function to download matches the name of the tool. Note also that these functions take an optional third argument that allow overriding of any options in the full set of options (see below).
kubectlsopshelmfilehelmeksctlminikubegomplatemysqlshusqlterraformjqvagrantyqkubentfluxThis is the full set of options that can be passed to
fetchExecutable (or as the optional third argument to any of the
shortcuts, but with no required options).
targetRequired?: yes
Type: string
The target local path on disk, either absolute, or relative to working directory.
urlRequired?: yes
Type: string (is formatted)
URL from which to download if required.
execIsOkRequired?: must set one of: execIsOk; version; hashValueUrl
Type: (filepath: string) => Promise<boolean>
Given a filepath to an executable that might be the right one, return
true if it is, false if it is not. If this is set in addition to
either of version or hashValueUrl, then all tests must pass for
the local executable to be considered "ok".
versionRequired?: must set one of: execIsOk; version; hashValueUrl
Type: string
The desired version. Used with versionExecArgs to determine if the
version of a local executable is as desired. Passed to
formatted.
versionExecArgsRequired?: no, defaults to []. Only relevant if version is set.
Type: Array<string>
Command line arguments to pass to the local executable to make it print its version.
versionExecCaptureStderrRequired?: no, defaults capturing stdout
Type: boolean
If set to true, capture stderr from the executed command. Otherwise,
captures stdout.
versionExecPostProcessRequired?: no, defaults to no post processing. Only relevant if version is set.
Type: (execOutput: string) => string
Given the string output from running the local executable (with
versionExecArgs), process it to return the version number for
comparison with the value set for version.
pathInTarRequired?: no, defaults to saving the downloaded file as-is
Type: string (is formatted)
If set, treat the downloaded file as a tar archive, and extract the
file at the given path. Can be used in conjunction with gzExtract: true to
handle .tar.gz files.
pathInZipRequired?: no, defaults to saving the downloaded file as-is
Type: string (is formatted)
If set, treat the downloaded file as a zip archive, and extract the
file at the given path.
executableSubPathInDirRequired?: no, defaults to assuming the desired executable is a file
at pathInTar. Only relevant if pathInTar is set (not yet supported
with pathInZip).
Type: string (is formatted)
If set, treats pathInTar as a directory, and extracts the contents
to a directory created at target. The executable itself is then at
${target}/${executableSubPathInDir}.
executableSubPathSymlinkRequired?: no, defaults to no symlink created.
Type: string
If set, creates a symlink from the given path to the executable at
${target}/${executableSubPathInDir}.
gzExtractRequired?: no, defaults to saving the downloaded file as-is
Type: boolean
If set to true, gzip-extract the downloaded file. Can be used in
conjunction with pathInTar to handle .tar.gz files.
bz2ExtractRequired?: no, defaults to saving the downloaded file as-is
Type: boolean
If set to true, bz2-extract the downloaded file. Can be used in
conjunction with pathInTar to handle .tar.bz2 files.
Note: if bz2Extract and gzExtract are both set to true, will
gzip-extract then bz2-extract, but this is not expected to be a real
use case.
hashValueUrlRequired?: must set one of: execIsOk; version; hashValueUrl
Type: string (is formatted)
URL that returns checksum data about the executable file. Possibly
with hashMethod and hashChecksumFileMatchFilepath, use this to
check if the local file is the correct version by comparing its hash
with a published, expected hash. Note that need for the hash of the
executable itself to be published at this URL, not of the archive used
to distribute it.
hashMethodRequired?: no, by default uses sha256. Only relevant if hashValueUrl is set.
Type: string
A hash algorithm that node's crypto.createHash understands.
hashChecksumFileMatchFilepathRequired?: no, by default the value fetched from hashValueUrl
as-is. Only relevant if hashValueUrl is set.
Type: string (is formatted)
If the data returned from hashValueUrl is not a single hash, but
rather a list of hashes as returned by a utility like sha256sum,
that lists hashes and filenames, use this to identify the filename
whose hash to use.
messageHandlerRequired?: no
Type: (message: FetchExecutableMessage) => void
A function that is called when fetchExecutable does things. Intended
to allow for communication with the user about what is going on, for
example, when downloading a potentially large file over a potentially
slow network connection.
Note that this takes precedence over messageHandlerBuiltin.
Note that FetchExecutableMessage is exported and has the form:
interface FetchExecutableMessage {
message: string;
kind: string;
target: string;
isVerbose: boolean;
}
The kind string attribute will be one of:
'executable_is_ok', when the pre-existing executable has been checked and is OK'fetching', when starting to fetch'fetch_progress', progress during fetch'saving', when starting to save'done', when saved and made executablemessagerDeprecated alias for messageHandler.
messageHandlerBuiltinRequired?: no
Type: string (one of: 'string', 'json')
Shortcuts for some simple builtin messageHandlers.
'string': just prints the message attribute'json': just prints the whole message as a JSON objectIf a string other than the above options is passed, is ignored.
messagerBuiltinDeprecated alias for messageHandlerBuiltin.
messageHandlerBuiltinVerboseRequired?: no, by default verbose messages not included. Only relevant
if messageHandlerBuiltin is set.
Type: boolean
Whether the builtin messageHandlers should include the messages that have
isVerbose: true.
messagerBuiltinVerboseDeprecated alias for messageHandlerBuiltinVerbose.
messageHandlerBuiltinStreamRequired?: no, by default messages are printed to
process.stderr. Only relevant if messageHandlerBuiltin is set.
Type: stream.Writable
The stream to which messages from the builtin messageHandler are written.
messagerBuiltinStreamDeprecated alias for messageHandlerBuiltinStream.
As a convenience, for the string options noted above, can perform formatting with the following variables:
version, value of the version option, if setplatform, value of process.platformarch, value of process.archAlso, the following transformers are available:
capitalize, upper-case the first characterx64ToAmd64, if the passed value is 'x64', return 'amd64' (intended for use with arch)Eg:
url: 'https://example.com/download/my-exec/{version}/{platform!capitalize}.tar.gz',
version: '1.2.3',
gzExtract: true,
pathInTar: '{arch!x64ToAmd64}/my-exec',
On a Linux, 64-bit OS, would retrieve the file from
https://example.com/download/my-exec/1.2.3/Linux.tar.gz, and gzip
extract, and retrieve the file from amd64/my-exec.
FAQs
Fetch an executable, but only if needed
We found that fetch-executable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.

Security News
Rust’s crates.io team is advancing an RFC to add a Security tab that surfaces RustSec vulnerability and unsoundness advisories directly on crate pages.