@revizly/sharp 0.33.2-revizly3

Sign up to get free protection for your applications and to get access to all the features.
package/lib/is.js ADDED
@@ -0,0 +1,169 @@
1
+ // Copyright 2013 Lovell Fuller and others.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ 'use strict';
5
+
6
+ /**
7
+ * Is this value defined and not null?
8
+ * @private
9
+ */
10
+ const defined = function (val) {
11
+ return typeof val !== 'undefined' && val !== null;
12
+ };
13
+
14
+ /**
15
+ * Is this value an object?
16
+ * @private
17
+ */
18
+ const object = function (val) {
19
+ return typeof val === 'object';
20
+ };
21
+
22
+ /**
23
+ * Is this value a plain object?
24
+ * @private
25
+ */
26
+ const plainObject = function (val) {
27
+ return Object.prototype.toString.call(val) === '[object Object]';
28
+ };
29
+
30
+ /**
31
+ * Is this value a function?
32
+ * @private
33
+ */
34
+ const fn = function (val) {
35
+ return typeof val === 'function';
36
+ };
37
+
38
+ /**
39
+ * Is this value a boolean?
40
+ * @private
41
+ */
42
+ const bool = function (val) {
43
+ return typeof val === 'boolean';
44
+ };
45
+
46
+ /**
47
+ * Is this value a Buffer object?
48
+ * @private
49
+ */
50
+ const buffer = function (val) {
51
+ return val instanceof Buffer;
52
+ };
53
+
54
+ /**
55
+ * Is this value a typed array object?. E.g. Uint8Array or Uint8ClampedArray?
56
+ * @private
57
+ */
58
+ const typedArray = function (val) {
59
+ if (defined(val)) {
60
+ switch (val.constructor) {
61
+ case Uint8Array:
62
+ case Uint8ClampedArray:
63
+ case Int8Array:
64
+ case Uint16Array:
65
+ case Int16Array:
66
+ case Uint32Array:
67
+ case Int32Array:
68
+ case Float32Array:
69
+ case Float64Array:
70
+ return true;
71
+ }
72
+ }
73
+
74
+ return false;
75
+ };
76
+
77
+ /**
78
+ * Is this value an ArrayBuffer object?
79
+ * @private
80
+ */
81
+ const arrayBuffer = function (val) {
82
+ return val instanceof ArrayBuffer;
83
+ };
84
+
85
+ /**
86
+ * Is this value a non-empty string?
87
+ * @private
88
+ */
89
+ const string = function (val) {
90
+ return typeof val === 'string' && val.length > 0;
91
+ };
92
+
93
+ /**
94
+ * Is this value a real number?
95
+ * @private
96
+ */
97
+ const number = function (val) {
98
+ return typeof val === 'number' && !Number.isNaN(val);
99
+ };
100
+
101
+ /**
102
+ * Is this value an integer?
103
+ * @private
104
+ */
105
+ const integer = function (val) {
106
+ return Number.isInteger(val);
107
+ };
108
+
109
+ /**
110
+ * Is this value within an inclusive given range?
111
+ * @private
112
+ */
113
+ const inRange = function (val, min, max) {
114
+ return val >= min && val <= max;
115
+ };
116
+
117
+ /**
118
+ * Is this value within the elements of an array?
119
+ * @private
120
+ */
121
+ const inArray = function (val, list) {
122
+ return list.includes(val);
123
+ };
124
+
125
+ /**
126
+ * Create an Error with a message relating to an invalid parameter.
127
+ *
128
+ * @param {string} name - parameter name.
129
+ * @param {string} expected - description of the type/value/range expected.
130
+ * @param {*} actual - the value received.
131
+ * @returns {Error} Containing the formatted message.
132
+ * @private
133
+ */
134
+ const invalidParameterError = function (name, expected, actual) {
135
+ return new Error(
136
+ `Expected ${expected} for ${name} but received ${actual} of type ${typeof actual}`
137
+ );
138
+ };
139
+
140
+ /**
141
+ * Ensures an Error from C++ contains a JS stack.
142
+ *
143
+ * @param {Error} native - Error with message from C++.
144
+ * @param {Error} context - Error with stack from JS.
145
+ * @returns {Error} Error with message and stack.
146
+ * @private
147
+ */
148
+ const nativeError = function (native, context) {
149
+ context.message = native.message;
150
+ return context;
151
+ };
152
+
153
+ module.exports = {
154
+ defined,
155
+ object,
156
+ plainObject,
157
+ fn,
158
+ bool,
159
+ buffer,
160
+ typedArray,
161
+ arrayBuffer,
162
+ string,
163
+ number,
164
+ integer,
165
+ inRange,
166
+ inArray,
167
+ invalidParameterError,
168
+ nativeError
169
+ };
package/lib/libvips.js ADDED
@@ -0,0 +1,195 @@
1
+ // Copyright 2013 Lovell Fuller and others.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ 'use strict';
5
+
6
+ const { spawnSync } = require('node:child_process');
7
+ const { createHash } = require('node:crypto');
8
+ const semverCoerce = require('semver/functions/coerce');
9
+ const semverGreaterThanOrEqualTo = require('semver/functions/gte');
10
+ const semverSatisfies = require('semver/functions/satisfies');
11
+ const detectLibc = require('detect-libc');
12
+
13
+ const { engines, optionalDependencies } = require('../package.json');
14
+
15
+ const minimumLibvipsVersionLabelled = process.env.npm_package_config_libvips || /* istanbul ignore next */
16
+ engines.libvips;
17
+ const minimumLibvipsVersion = semverCoerce(minimumLibvipsVersionLabelled).version;
18
+
19
+ const prebuiltPlatforms = [
20
+ 'darwin-arm64', 'darwin-x64',
21
+ 'linux-arm', 'linux-arm64', 'linux-s390x', 'linux-x64',
22
+ 'linuxmusl-arm64', 'linuxmusl-x64',
23
+ 'win32-ia32', 'win32-x64'
24
+ ];
25
+
26
+ const spawnSyncOptions = {
27
+ encoding: 'utf8',
28
+ shell: true
29
+ };
30
+
31
+ const log = (item) => {
32
+ if (item instanceof Error) {
33
+ console.error(`sharp: Installation error: ${item.message}`);
34
+ } else {
35
+ console.log(`sharp: ${item}`);
36
+ }
37
+ };
38
+
39
+ /* istanbul ignore next */
40
+ const runtimeLibc = () => detectLibc.isNonGlibcLinuxSync() ? detectLibc.familySync() : '';
41
+
42
+ const runtimePlatformArch = () => `${process.platform}${runtimeLibc()}-${process.arch}`;
43
+
44
+ /* istanbul ignore next */
45
+ const buildPlatformArch = () => {
46
+ if (isEmscripten()) {
47
+ return 'wasm32';
48
+ }
49
+ /* eslint camelcase: ["error", { allow: ["^npm_config_"] }] */
50
+ const { npm_config_arch, npm_config_platform, npm_config_libc } = process.env;
51
+ const libc = typeof npm_config_libc === 'string' ? npm_config_libc : runtimeLibc();
52
+ return `${npm_config_platform || process.platform}${libc}-${npm_config_arch || process.arch}`;
53
+ };
54
+
55
+ const buildSharpLibvipsIncludeDir = () => {
56
+ try {
57
+ return require(`@revizly/sharp-libvips-dev-${buildPlatformArch()}/include`);
58
+ } catch {
59
+ try {
60
+ return require('@revizly/sharp-libvips-dev/include');
61
+ } catch {}
62
+ }
63
+ /* istanbul ignore next */
64
+ return '';
65
+ };
66
+
67
+ const buildSharpLibvipsCPlusPlusDir = () => {
68
+ try {
69
+ return require('@revizly/sharp-libvips-dev/cplusplus');
70
+ } catch {}
71
+ /* istanbul ignore next */
72
+ return '';
73
+ };
74
+
75
+ const buildSharpLibvipsLibDir = () => {
76
+ try {
77
+ return require(`@revizly/sharp-libvips-dev-${buildPlatformArch()}/lib`);
78
+ } catch {
79
+ try {
80
+ return require(`@revizly/sharp-libvips-${buildPlatformArch()}/lib`);
81
+ } catch {}
82
+ }
83
+ /* istanbul ignore next */
84
+ return '';
85
+ };
86
+
87
+ const isUnsupportedNodeRuntime = () => {
88
+ /* istanbul ignore next */
89
+ if (process.release?.name === 'node' && process.versions) {
90
+ if (!semverSatisfies(process.versions.node, engines.node)) {
91
+ return { found: process.versions.node, expected: engines.node };
92
+ }
93
+ }
94
+ };
95
+
96
+ /* istanbul ignore next */
97
+ const isEmscripten = () => {
98
+ const { CC } = process.env;
99
+ return Boolean(CC && CC.endsWith('/emcc'));
100
+ };
101
+
102
+ const isRosetta = () => {
103
+ /* istanbul ignore next */
104
+ if (process.platform === 'darwin' && process.arch === 'x64') {
105
+ const translated = spawnSync('sysctl sysctl.proc_translated', spawnSyncOptions).stdout;
106
+ return (translated || '').trim() === 'sysctl.proc_translated: 1';
107
+ }
108
+ return false;
109
+ };
110
+
111
+ const sha512 = (s) => createHash('sha512').update(s).digest('hex');
112
+
113
+ const yarnLocator = () => {
114
+ try {
115
+ const identHash = sha512(`imgsharp-libvips-${buildPlatformArch()}`);
116
+ const npmVersion = semverCoerce(optionalDependencies[`@revizly/sharp-libvips-${buildPlatformArch()}`]).version;
117
+ return sha512(`${identHash}npm:${npmVersion}`).slice(0, 10);
118
+ } catch {}
119
+ return '';
120
+ };
121
+
122
+ /* istanbul ignore next */
123
+ const spawnRebuild = () =>
124
+ spawnSync(`node-gyp rebuild --directory=src ${isEmscripten() ? '--nodedir=emscripten' : ''}`, {
125
+ ...spawnSyncOptions,
126
+ stdio: 'inherit'
127
+ }).status;
128
+
129
+ const globalLibvipsVersion = () => {
130
+ if (process.platform !== 'win32') {
131
+ const globalLibvipsVersion = spawnSync('pkg-config --modversion vips-cpp', {
132
+ ...spawnSyncOptions,
133
+ env: {
134
+ ...process.env,
135
+ PKG_CONFIG_PATH: pkgConfigPath()
136
+ }
137
+ }).stdout;
138
+ /* istanbul ignore next */
139
+ return (globalLibvipsVersion || '').trim();
140
+ } else {
141
+ return '';
142
+ }
143
+ };
144
+
145
+ /* istanbul ignore next */
146
+ const pkgConfigPath = () => {
147
+ if (process.platform !== 'win32') {
148
+ const brewPkgConfigPath = spawnSync(
149
+ 'which brew >/dev/null 2>&1 && brew environment --plain | grep PKG_CONFIG_LIBDIR | cut -d" " -f2',
150
+ spawnSyncOptions
151
+ ).stdout || '';
152
+ return [
153
+ brewPkgConfigPath.trim(),
154
+ process.env.PKG_CONFIG_PATH,
155
+ '/usr/local/lib/pkgconfig',
156
+ '/usr/lib/pkgconfig',
157
+ '/usr/local/libdata/pkgconfig',
158
+ '/usr/libdata/pkgconfig'
159
+ ].filter(Boolean).join(':');
160
+ } else {
161
+ return '';
162
+ }
163
+ };
164
+
165
+ const useGlobalLibvips = () => {
166
+ if (Boolean(process.env.SHARP_IGNORE_GLOBAL_LIBVIPS) === true) {
167
+ log('Detected SHARP_IGNORE_GLOBAL_LIBVIPS, skipping search for globally-installed libvips');
168
+ return false;
169
+ }
170
+ /* istanbul ignore next */
171
+ if (isRosetta()) {
172
+ log('Detected Rosetta, skipping search for globally-installed libvips');
173
+ return false;
174
+ }
175
+ const globalVipsVersion = globalLibvipsVersion();
176
+ return !!globalVipsVersion && /* istanbul ignore next */
177
+ semverGreaterThanOrEqualTo(globalVipsVersion, minimumLibvipsVersion);
178
+ };
179
+
180
+ module.exports = {
181
+ minimumLibvipsVersion,
182
+ prebuiltPlatforms,
183
+ buildPlatformArch,
184
+ buildSharpLibvipsIncludeDir,
185
+ buildSharpLibvipsCPlusPlusDir,
186
+ buildSharpLibvipsLibDir,
187
+ isUnsupportedNodeRuntime,
188
+ runtimePlatformArch,
189
+ log,
190
+ yarnLocator,
191
+ spawnRebuild,
192
+ globalLibvipsVersion,
193
+ pkgConfigPath,
194
+ useGlobalLibvips
195
+ };