Install: conditionally use Brotli or gzip prebuilt libvips (#2412)

This commit is contained in:
Matt Kane 2020-11-16 15:22:13 +00:00 committed by GitHub
parent fabe720b9b
commit 65acd96c8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 10 deletions

View File

@ -16,7 +16,7 @@ Lanczos resampling ensures quality is not sacrificed for speed.
As well as image resizing, operations such as As well as image resizing, operations such as
rotation, extraction, compositing and gamma correction are available. rotation, extraction, compositing and gamma correction are available.
Most modern macOS, Windows and Linux systems running Node.js v10.16.0+ Most modern macOS, Windows and Linux systems running Node.js v10+
do not require any additional install or runtime dependencies. do not require any additional install or runtime dependencies.
## Examples ## Examples

View File

@ -16,7 +16,7 @@ Lanczos resampling ensures quality is not sacrificed for speed.
As well as image resizing, operations such as As well as image resizing, operations such as
rotation, extraction, compositing and gamma correction are available. rotation, extraction, compositing and gamma correction are available.
Most modern macOS, Windows and Linux systems running Node.js v10.16.0+ Most modern macOS, Windows and Linux systems running Node.js v10+
do not require any additional install or runtime dependencies. do not require any additional install or runtime dependencies.
### Formats ### Formats

View File

@ -10,19 +10,19 @@ yarn add sharp
## Prerequisites ## Prerequisites
* Node.js v10.16.0+ * Node.js v10+
## Prebuilt binaries ## Prebuilt binaries
Ready-compiled sharp and libvips binaries are provided for use with Ready-compiled sharp and libvips binaries are provided for use with
Node.js v10.16.0+ on the most common platforms: Node.js v10+ on the most common platforms:
* macOS x64 (>= 10.13) * macOS x64 (>= 10.13)
* Linux x64 (glibc >= 2.17, musl >= 1.1.24) * Linux x64 (glibc >= 2.17, musl >= 1.1.24)
* Linux ARM64 (glibc >= 2.29) * Linux ARM64 (glibc >= 2.29)
* Windows * Windows
A ~7MB tarball containing libvips and its most commonly used dependencies A 7-8MB tarball containing libvips and its most commonly used dependencies
is downloaded via HTTPS and stored within `node_modules/sharp/vendor` during `npm install`. is downloaded via HTTPS and stored within `node_modules/sharp/vendor` during `npm install`.
This provides support for the This provides support for the
@ -89,11 +89,14 @@ To install the prebuilt libvips binaries from a custom URL,
set the `sharp_libvips_binary_host` npm config option set the `sharp_libvips_binary_host` npm config option
or the `npm_config_sharp_libvips_binary_host` environment variable. or the `npm_config_sharp_libvips_binary_host` environment variable.
The version subpath and file name are appended to these. The version subpath and file name are appended to these. There should be tarballs available
that are compressed with both gzip and Brotli, as the format downloaded will vary depending
on whether the user's version of Node supports Brotli decompression (Node.js v10.16.0+)
For example, if `sharp_libvips_binary_host` is set to `https://hostname/path` For example, if `sharp_libvips_binary_host` is set to `https://hostname/path`
and the libvips version is `1.2.3` then the resultant URL will be and the libvips version is `1.2.3` then the resultant URL will be
`https://hostname/path/v1.2.3/libvips-1.2.3-platform-arch.tar.br`. `https://hostname/path/v1.2.3/libvips-1.2.3-platform-arch.tar.br` or
`https://hostname/path/v1.2.3/libvips-1.2.3-platform-arch.tar.gz`.
See the Chinese mirror below for a further example. See the Chinese mirror below for a further example.

View File

@ -25,6 +25,7 @@ const minimumGlibcVersionByArch = {
const { minimumLibvipsVersion, minimumLibvipsVersionLabelled } = libvips; const { minimumLibvipsVersion, minimumLibvipsVersionLabelled } = libvips;
const distHost = process.env.npm_config_sharp_libvips_binary_host || 'https://github.com/lovell/sharp-libvips/releases/download'; const distHost = process.env.npm_config_sharp_libvips_binary_host || 'https://github.com/lovell/sharp-libvips/releases/download';
const distBaseUrl = process.env.npm_config_sharp_dist_base_url || process.env.SHARP_DIST_BASE_URL || `${distHost}/v${minimumLibvipsVersionLabelled}/`; const distBaseUrl = process.env.npm_config_sharp_dist_base_url || process.env.SHARP_DIST_BASE_URL || `${distHost}/v${minimumLibvipsVersionLabelled}/`;
const supportsBrotli = ('BrotliDecompress' in zlib);
const fail = function (err) { const fail = function (err) {
npmLog.error('sharp', err.message); npmLog.error('sharp', err.message);
@ -43,7 +44,7 @@ const extractTarball = function (tarPath) {
libvips.mkdirSync(versionedVendorPath); libvips.mkdirSync(versionedVendorPath);
stream.pipeline( stream.pipeline(
fs.createReadStream(tarPath), fs.createReadStream(tarPath),
new zlib.BrotliDecompress(), supportsBrotli ? new zlib.BrotliDecompress() : new zlib.Gunzip(),
tarFs.extract(versionedVendorPath), tarFs.extract(versionedVendorPath),
function (err) { function (err) {
if (err) { if (err) {
@ -58,6 +59,7 @@ const extractTarball = function (tarPath) {
try { try {
const useGlobalLibvips = libvips.useGlobalLibvips(); const useGlobalLibvips = libvips.useGlobalLibvips();
if (useGlobalLibvips) { if (useGlobalLibvips) {
const globalLibvipsVersion = libvips.globalLibvipsVersion(); const globalLibvipsVersion = libvips.globalLibvipsVersion();
npmLog.info('sharp', `Detected globally-installed libvips v${globalLibvipsVersion}`); npmLog.info('sharp', `Detected globally-installed libvips v${globalLibvipsVersion}`);
@ -86,8 +88,10 @@ try {
throw new Error(`Expected Node.js version ${supportedNodeVersion} but found ${process.versions.node}`); throw new Error(`Expected Node.js version ${supportedNodeVersion} but found ${process.versions.node}`);
} }
const extension = supportsBrotli ? 'br' : 'gz';
// Download to per-process temporary file // Download to per-process temporary file
const tarFilename = ['libvips', minimumLibvipsVersion, platformAndArch].join('-') + '.tar.br'; const tarFilename = ['libvips', minimumLibvipsVersion, platformAndArch].join('-') + '.tar.' + extension;
const tarPathCache = path.join(libvips.cachePath(), tarFilename); const tarPathCache = path.join(libvips.cachePath(), tarFilename);
if (fs.existsSync(tarPathCache)) { if (fs.existsSync(tarPathCache)) {
npmLog.info('sharp', `Using cached ${tarPathCache}`); npmLog.info('sharp', `Using cached ${tarPathCache}`);

View File

@ -146,7 +146,7 @@
"target": 3 "target": 3
}, },
"engines": { "engines": {
"node": ">=10.16.0" "node": ">=10"
}, },
"funding": { "funding": {
"url": "https://opencollective.com/libvips" "url": "https://opencollective.com/libvips"