libvips.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*!
  2. Copyright 2013 Lovell Fuller and others.
  3. SPDX-License-Identifier: Apache-2.0
  4. */
  5. const { spawnSync } = require('node:child_process');
  6. const { createHash } = require('node:crypto');
  7. const semverCoerce = require('semver/functions/coerce');
  8. const semverGreaterThanOrEqualTo = require('semver/functions/gte');
  9. const semverSatisfies = require('semver/functions/satisfies');
  10. const detectLibc = require('detect-libc');
  11. const { config, engines, optionalDependencies } = require('../package.json');
  12. /* node:coverage ignore next */
  13. const minimumLibvipsVersionLabelled = process.env.npm_package_config_libvips || config.libvips;
  14. const minimumLibvipsVersion = semverCoerce(minimumLibvipsVersionLabelled).version;
  15. const prebuiltPlatforms = [
  16. 'darwin-arm64', 'darwin-x64',
  17. 'linux-arm', 'linux-arm64', 'linux-ppc64', 'linux-riscv64', 'linux-s390x', 'linux-x64',
  18. 'linuxmusl-arm64', 'linuxmusl-x64',
  19. 'win32-arm64', 'win32-ia32', 'win32-x64'
  20. ];
  21. const spawnSyncOptions = {
  22. encoding: 'utf8',
  23. shell: true
  24. };
  25. const log = (item) => {
  26. if (item instanceof Error) {
  27. console.error(`sharp: Installation error: ${item.message}`);
  28. } else {
  29. console.log(`sharp: ${item}`);
  30. }
  31. };
  32. /* node:coverage ignore next */
  33. const runtimeLibc = () => detectLibc.isNonGlibcLinuxSync() ? detectLibc.familySync() : '';
  34. const runtimePlatformArch = () => `${process.platform}${runtimeLibc()}-${process.arch}`;
  35. const buildPlatformArch = () => {
  36. /* node:coverage ignore next 3 */
  37. if (isEmscripten()) {
  38. return 'wasm32';
  39. }
  40. const { npm_config_arch, npm_config_platform, npm_config_libc } = process.env;
  41. const libc = typeof npm_config_libc === 'string' ? npm_config_libc : runtimeLibc();
  42. return `${npm_config_platform || process.platform}${libc}-${npm_config_arch || process.arch}`;
  43. };
  44. const buildSharpLibvipsIncludeDir = () => {
  45. try {
  46. return require(`@img/sharp-libvips-dev-${buildPlatformArch()}/include`);
  47. } catch {
  48. /* node:coverage ignore next 5 */
  49. try {
  50. return require('@img/sharp-libvips-dev/include');
  51. } catch {}
  52. }
  53. return '';
  54. };
  55. const buildSharpLibvipsCPlusPlusDir = () => {
  56. /* node:coverage ignore next 4 */
  57. try {
  58. return require('@img/sharp-libvips-dev/cplusplus');
  59. } catch {}
  60. return '';
  61. };
  62. const buildSharpLibvipsLibDir = () => {
  63. try {
  64. return require(`@img/sharp-libvips-dev-${buildPlatformArch()}/lib`);
  65. } catch {
  66. /* node:coverage ignore next 5 */
  67. try {
  68. return require(`@img/sharp-libvips-${buildPlatformArch()}/lib`);
  69. } catch {}
  70. }
  71. return '';
  72. };
  73. /* node:coverage disable */
  74. const isUnsupportedNodeRuntime = () => {
  75. if (process.release?.name === 'node' && process.versions) {
  76. if (!semverSatisfies(process.versions.node, engines.node)) {
  77. return { found: process.versions.node, expected: engines.node };
  78. }
  79. }
  80. };
  81. const isEmscripten = () => {
  82. const { CC } = process.env;
  83. return Boolean(CC?.endsWith('/emcc'));
  84. };
  85. const isRosetta = () => {
  86. if (process.platform === 'darwin' && process.arch === 'x64') {
  87. const translated = spawnSync('sysctl sysctl.proc_translated', spawnSyncOptions).stdout;
  88. return (translated || '').trim() === 'sysctl.proc_translated: 1';
  89. }
  90. return false;
  91. };
  92. /* node:coverage enable */
  93. const sha512 = (s) => createHash('sha512').update(s).digest('hex');
  94. const yarnLocator = () => {
  95. try {
  96. const identHash = sha512(`imgsharp-libvips-${buildPlatformArch()}`);
  97. const npmVersion = semverCoerce(optionalDependencies[`@img/sharp-libvips-${buildPlatformArch()}`], {
  98. includePrerelease: true
  99. }).version;
  100. return sha512(`${identHash}npm:${npmVersion}`).slice(0, 10);
  101. } catch {}
  102. return '';
  103. };
  104. /* node:coverage disable */
  105. const spawnRebuild = () =>
  106. spawnSync(`node-gyp rebuild --directory=src ${isEmscripten() ? '--nodedir=emscripten' : ''}`, {
  107. ...spawnSyncOptions,
  108. stdio: 'inherit'
  109. }).status;
  110. const globalLibvipsVersion = () => {
  111. if (process.platform !== 'win32') {
  112. const globalLibvipsVersion = spawnSync('pkg-config --modversion vips-cpp', {
  113. ...spawnSyncOptions,
  114. env: {
  115. ...process.env,
  116. PKG_CONFIG_PATH: pkgConfigPath()
  117. }
  118. }).stdout;
  119. return (globalLibvipsVersion || '').trim();
  120. } else {
  121. return '';
  122. }
  123. };
  124. /* node:coverage enable */
  125. const pkgConfigPath = () => {
  126. if (process.platform !== 'win32') {
  127. /* node:coverage ignore next 4 */
  128. const brewPkgConfigPath = spawnSync(
  129. 'which brew >/dev/null 2>&1 && brew environment --plain | grep PKG_CONFIG_LIBDIR | cut -d" " -f2',
  130. spawnSyncOptions
  131. ).stdout || '';
  132. return [
  133. brewPkgConfigPath.trim(),
  134. process.env.PKG_CONFIG_PATH,
  135. '/usr/local/lib/pkgconfig',
  136. '/usr/lib/pkgconfig',
  137. '/usr/local/libdata/pkgconfig',
  138. '/usr/libdata/pkgconfig'
  139. ].filter(Boolean).join(':');
  140. } else {
  141. return '';
  142. }
  143. };
  144. const skipSearch = (status, reason, logger) => {
  145. if (logger) {
  146. logger(`Detected ${reason}, skipping search for globally-installed libvips`);
  147. }
  148. return status;
  149. };
  150. const useGlobalLibvips = (logger) => {
  151. if (Boolean(process.env.SHARP_IGNORE_GLOBAL_LIBVIPS) === true) {
  152. return skipSearch(false, 'SHARP_IGNORE_GLOBAL_LIBVIPS', logger);
  153. }
  154. if (Boolean(process.env.SHARP_FORCE_GLOBAL_LIBVIPS) === true) {
  155. return skipSearch(true, 'SHARP_FORCE_GLOBAL_LIBVIPS', logger);
  156. }
  157. /* node:coverage ignore next 3 */
  158. if (isRosetta()) {
  159. return skipSearch(false, 'Rosetta', logger);
  160. }
  161. const globalVipsVersion = globalLibvipsVersion();
  162. /* node:coverage ignore next */
  163. return !!globalVipsVersion && semverGreaterThanOrEqualTo(globalVipsVersion, minimumLibvipsVersion);
  164. };
  165. module.exports = {
  166. minimumLibvipsVersion,
  167. prebuiltPlatforms,
  168. buildPlatformArch,
  169. buildSharpLibvipsIncludeDir,
  170. buildSharpLibvipsCPlusPlusDir,
  171. buildSharpLibvipsLibDir,
  172. isUnsupportedNodeRuntime,
  173. runtimePlatformArch,
  174. log,
  175. yarnLocator,
  176. spawnRebuild,
  177. globalLibvipsVersion,
  178. pkgConfigPath,
  179. useGlobalLibvips
  180. };