From 65acd96c8dd09fe5ee76a66fc1a1d84de110ba52 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Mon, 16 Nov 2020 15:22:13 +0000 Subject: [PATCH] Install: conditionally use Brotli or gzip prebuilt libvips (#2412) --- README.md | 2 +- docs/README.md | 2 +- docs/install.md | 13 ++++++++----- install/libvips.js | 8 ++++++-- package.json | 2 +- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 28541f24..70f0992e 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Lanczos resampling ensures quality is not sacrificed for speed. As well as image resizing, operations such as 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. ## Examples diff --git a/docs/README.md b/docs/README.md index 31683731..c4c1b15e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,7 +16,7 @@ Lanczos resampling ensures quality is not sacrificed for speed. As well as image resizing, operations such as 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. ### Formats diff --git a/docs/install.md b/docs/install.md index f02e2beb..525ee470 100644 --- a/docs/install.md +++ b/docs/install.md @@ -10,19 +10,19 @@ yarn add sharp ## Prerequisites -* Node.js v10.16.0+ +* Node.js v10+ ## Prebuilt binaries 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) * Linux x64 (glibc >= 2.17, musl >= 1.1.24) * Linux ARM64 (glibc >= 2.29) * 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`. 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 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` 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. diff --git a/install/libvips.js b/install/libvips.js index c641dec8..6b3385f2 100644 --- a/install/libvips.js +++ b/install/libvips.js @@ -25,6 +25,7 @@ const minimumGlibcVersionByArch = { const { minimumLibvipsVersion, minimumLibvipsVersionLabelled } = libvips; 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 supportsBrotli = ('BrotliDecompress' in zlib); const fail = function (err) { npmLog.error('sharp', err.message); @@ -43,7 +44,7 @@ const extractTarball = function (tarPath) { libvips.mkdirSync(versionedVendorPath); stream.pipeline( fs.createReadStream(tarPath), - new zlib.BrotliDecompress(), + supportsBrotli ? new zlib.BrotliDecompress() : new zlib.Gunzip(), tarFs.extract(versionedVendorPath), function (err) { if (err) { @@ -58,6 +59,7 @@ const extractTarball = function (tarPath) { try { const useGlobalLibvips = libvips.useGlobalLibvips(); + if (useGlobalLibvips) { const globalLibvipsVersion = libvips.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}`); } + const extension = supportsBrotli ? 'br' : 'gz'; + // 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); if (fs.existsSync(tarPathCache)) { npmLog.info('sharp', `Using cached ${tarPathCache}`); diff --git a/package.json b/package.json index 6804a400..73b7c4ed 100644 --- a/package.json +++ b/package.json @@ -146,7 +146,7 @@ "target": 3 }, "engines": { - "node": ">=10.16.0" + "node": ">=10" }, "funding": { "url": "https://opencollective.com/libvips"