From 45e8071599772f320d0d8314fedf0b13b0592d28 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Wed, 10 Jan 2024 13:50:20 +0000 Subject: [PATCH] Add runtime check for outdated Node.js version --- lib/libvips.js | 11 +++++++++++ lib/sharp.js | 11 +++++++++-- test/unit/libvips.js | 3 +++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/libvips.js b/lib/libvips.js index a76c9b46f..a24c70eb4 100644 --- a/lib/libvips.js +++ b/lib/libvips.js @@ -7,6 +7,7 @@ const { spawnSync } = require('node:child_process'); const { createHash } = require('node:crypto'); const semverCoerce = require('semver/functions/coerce'); const semverGreaterThanOrEqualTo = require('semver/functions/gte'); +const semverSatisfies = require('semver/functions/satisfies'); const detectLibc = require('detect-libc'); const { engines, optionalDependencies } = require('../package.json'); @@ -83,6 +84,15 @@ const buildSharpLibvipsLibDir = () => { return ''; }; +const isUnsupportedNodeRuntime = () => { + /* istanbul ignore next */ + if (process.release?.name === 'node' && process.versions) { + if (!semverSatisfies(process.versions.node, engines.node)) { + return { found: process.versions.node, expected: engines.node }; + } + } +}; + /* istanbul ignore next */ const isEmscripten = () => { const { CC } = process.env; @@ -174,6 +184,7 @@ module.exports = { buildSharpLibvipsIncludeDir, buildSharpLibvipsCPlusPlusDir, buildSharpLibvipsLibDir, + isUnsupportedNodeRuntime, runtimePlatformArch, log, yarnLocator, diff --git a/lib/sharp.js b/lib/sharp.js index 6101fd645..44c7463a1 100644 --- a/lib/sharp.js +++ b/lib/sharp.js @@ -7,7 +7,7 @@ const { familySync, versionSync } = require('detect-libc'); -const { runtimePlatformArch, prebuiltPlatforms, minimumLibvipsVersion } = require('./libvips'); +const { runtimePlatformArch, isUnsupportedNodeRuntime, prebuiltPlatforms, minimumLibvipsVersion } = require('./libvips'); const runtimePlatform = runtimePlatformArch(); const paths = [ @@ -44,7 +44,14 @@ if (sharp) { const messages = errors.map(err => err.message).join(' '); help.push('Possible solutions:'); // Common error messages - if (prebuiltPlatforms.includes(runtimePlatform)) { + if (isUnsupportedNodeRuntime()) { + const { found, expected } = isUnsupportedNodeRuntime(); + help.push( + '- Please upgrade Node.js:', + ` Found ${found}`, + ` Requires ${expected}` + ); + } else if (prebuiltPlatforms.includes(runtimePlatform)) { const [os, cpu] = runtimePlatform.split('-'); const libc = os.endsWith('musl') ? ' --libc=musl' : ''; help.push( diff --git a/test/unit/libvips.js b/test/unit/libvips.js index 7c26bf820..6e11b84d4 100644 --- a/test/unit/libvips.js +++ b/test/unit/libvips.js @@ -123,6 +123,9 @@ describe('libvips binaries', function () { const [, arch] = libvips.runtimePlatformArch().split('-'); assert.strict(['arm', 'arm64', 'ia32', 'x64'].includes(arch)); }); + it('isUnsupportedNodeRuntime', () => { + assert.strictEqual(libvips.isUnsupportedNodeRuntime(), undefined); + }); }); describe('logger', function () {