1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const os = require('os');
|
5 | const path = require('path');
|
6 |
|
7 | const detectLibc = require('detect-libc');
|
8 | const npmLog = require('npmlog');
|
9 | const semver = require('semver');
|
10 | const simpleGet = require('simple-get');
|
11 | const tar = require('tar');
|
12 |
|
13 | const agent = require('../lib/agent');
|
14 | const libvips = require('../lib/libvips');
|
15 | const platform = require('../lib/platform');
|
16 |
|
17 | const minimumGlibcVersionByArch = {
|
18 | arm: '2.28',
|
19 | arm64: '2.29',
|
20 | x64: '2.17'
|
21 | };
|
22 |
|
23 | const { minimumLibvipsVersion, minimumLibvipsVersionLabelled } = libvips;
|
24 | const distHost = process.env.npm_config_sharp_libvips_binary_host || 'https://github.com/lovell/sharp-libvips/releases/download';
|
25 | const distBaseUrl = process.env.npm_config_sharp_dist_base_url || process.env.SHARP_DIST_BASE_URL || `${distHost}/v${minimumLibvipsVersionLabelled}/`;
|
26 |
|
27 | const fail = function (err) {
|
28 | npmLog.error('sharp', err.message);
|
29 | if (err.code === 'EACCES') {
|
30 | npmLog.info('sharp', 'Are you trying to install as a root or sudo user? Try again with the --unsafe-perm flag');
|
31 | }
|
32 | npmLog.info('sharp', 'Attempting to build from source via node-gyp but this may fail due to the above error');
|
33 | npmLog.info('sharp', 'Please see https://sharp.pixelplumbing.com/install for required dependencies');
|
34 | process.exit(1);
|
35 | };
|
36 |
|
37 | const extractTarball = function (tarPath) {
|
38 | const vendorPath = path.join(__dirname, '..', 'vendor');
|
39 | libvips.mkdirSync(vendorPath);
|
40 | tar
|
41 | .extract({
|
42 | file: tarPath,
|
43 | cwd: vendorPath,
|
44 | strict: true
|
45 | })
|
46 | .catch(function (err) {
|
47 | if (/unexpected end of file/.test(err.message)) {
|
48 | npmLog.error('sharp', `Please delete ${tarPath} as it is not a valid tarball`);
|
49 | }
|
50 | fail(err);
|
51 | });
|
52 | };
|
53 |
|
54 | try {
|
55 | const useGlobalLibvips = libvips.useGlobalLibvips();
|
56 | if (useGlobalLibvips) {
|
57 | const globalLibvipsVersion = libvips.globalLibvipsVersion();
|
58 | npmLog.info('sharp', `Detected globally-installed libvips v${globalLibvipsVersion}`);
|
59 | npmLog.info('sharp', 'Building from source via node-gyp');
|
60 | process.exit(1);
|
61 | } else if (libvips.hasVendoredLibvips()) {
|
62 | npmLog.info('sharp', `Using existing vendored libvips v${minimumLibvipsVersion}`);
|
63 | } else {
|
64 |
|
65 | const arch = process.env.npm_config_arch || process.arch;
|
66 | const platformAndArch = platform();
|
67 | if (arch === 'ia32' && !platformAndArch.startsWith('win32')) {
|
68 | throw new Error(`Intel Architecture 32-bit systems require manual installation of libvips >= ${minimumLibvipsVersion}`);
|
69 | }
|
70 | if (platformAndArch === 'freebsd-x64' || platformAndArch === 'openbsd-x64' || platformAndArch === 'sunos-x64') {
|
71 | throw new Error(`BSD/SunOS systems require manual installation of libvips >= ${minimumLibvipsVersion}`);
|
72 | }
|
73 | if (detectLibc.family === detectLibc.GLIBC && detectLibc.version) {
|
74 | if (semver.lt(`${detectLibc.version}.0`, `${minimumGlibcVersionByArch[arch]}.0`)) {
|
75 | throw new Error(`Use with glibc ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`);
|
76 | }
|
77 | }
|
78 |
|
79 | const tarFilename = ['libvips', minimumLibvipsVersion, platformAndArch].join('-') + '.tar.gz';
|
80 | const tarPathCache = path.join(libvips.cachePath(), tarFilename);
|
81 | if (fs.existsSync(tarPathCache)) {
|
82 | npmLog.info('sharp', `Using cached ${tarPathCache}`);
|
83 | extractTarball(tarPathCache);
|
84 | } else {
|
85 | const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`);
|
86 | const tmpFile = fs.createWriteStream(tarPathTemp);
|
87 | const url = distBaseUrl + tarFilename;
|
88 | npmLog.info('sharp', `Downloading ${url}`);
|
89 | simpleGet({ url: url, agent: agent() }, function (err, response) {
|
90 | if (err) {
|
91 | fail(err);
|
92 | } else if (response.statusCode === 404) {
|
93 | fail(new Error(`Prebuilt libvips ${minimumLibvipsVersion} binaries are not yet available for ${platformAndArch}`));
|
94 | } else if (response.statusCode !== 200) {
|
95 | fail(new Error(`Status ${response.statusCode} ${response.statusMessage}`));
|
96 | } else {
|
97 | response
|
98 | .on('error', fail)
|
99 | .pipe(tmpFile);
|
100 | }
|
101 | });
|
102 | tmpFile
|
103 | .on('error', fail)
|
104 | .on('close', function () {
|
105 | try {
|
106 |
|
107 | fs.renameSync(tarPathTemp, tarPathCache);
|
108 | } catch (err) {
|
109 |
|
110 | fs.copyFileSync(tarPathTemp, tarPathCache);
|
111 | fs.unlinkSync(tarPathTemp);
|
112 | }
|
113 | extractTarball(tarPathCache);
|
114 | });
|
115 | }
|
116 | }
|
117 | } catch (err) {
|
118 | fail(err);
|
119 | }
|