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;IAAC,OAAO,CAAC,EAAE;QACV,MAAM,IAAI,KAAK,CAAC,4EAA4E,CAAC,CAAC;KAC/F;IAED,2CAA2C;IAC3C,MAAM,SAAS,GAAG,sBAAW,CAAC,MAAM,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IAE5D,IAAI,SAAS,EAAE;QACb,IAAI;YACF,8CAA8C;YAC9C,cAAc,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;SACrC;QAAC,MAAM,GAAE;KACX;IAED,OAAO,cAAc,CAAC;AACxB,CAAC;AAtCD,wDAsCC;AAED,SAAS,aAAa,CAAC,kBAA0B;IAC/C,OAAO,IAAI,KAAK,CACd,iCAAiC,kBAAkB,uBAAuB;QACxE,uDAAuD,kBAAkB,QAAQ;QACjF,IAAI;QACJ,kFAAkF,CACrF,CAAC;AACJ,CAAC;AAED,IAAI,2BAA2B,GAAG,KAAK,CAAC;AAExC,SAAS,0BAA0B,CAAC,kBAA0B,EAAE,mBAA2B;IACzF,IAAI,2BAA2B,EAAE;QAC/B,OAAO;KACR;IACD,OAAO,CAAC,IAAI,CACV,0CAA0C,kBAAkB,uBAAuB;QACjF,iCAAiC,mBAAmB,MAAM;QAC1D,sBAAsB,kBAAkB,MAAM;QAC9C,uDAAuD,kBAAkB,QAAQ;QACjF,IAAI;QACJ,kFAAkF,CACrF,CAAC;IACF,2BAA2B,GAAG,IAAI,CAAC;AACrC,CAAC","sourcesContent":["import spawnAsync from '@expo/spawn-async';\nimport { boolish } from 'getenv';\nimport resolveFrom from 'resolve-from';\nimport semver from 'semver';\n\nimport { Options, SharpCommandOptions, SharpGlobalOptions } from './sharp.types';\n\nconst SHARP_HELP_PATTERN = /\\n\\nSpecify --help for available options/g;\nconst SHARP_REQUIRED_VERSION = '^1.10.0';\n\nexport async function resizeBufferAsync(buffer: Buffer, sizes: number[]): Promise {\n const sharp = await findSharpInstanceAsync();\n\n const metadata = await sharp(buffer).metadata();\n // Create buffer for each size\n const resizedBuffers = await Promise.all(\n sizes.map(dimension => {\n const density = (dimension / Math.max(metadata.width, metadata.height)) * metadata.density;\n return sharp(buffer, {\n density: isNaN(density) ? undefined : Math.ceil(density),\n })\n .resize(dimension, dimension, { fit: 'contain', background: 'transparent' })\n .toBuffer();\n })\n );\n\n return resizedBuffers;\n}\n\nconst isSharpDisabled = boolish('EXPO_IMAGE_UTILS_NO_SHARP', false);\n\n/**\n * Returns `true` if a global sharp instance can be found.\n * This functionality can be overridden with `process.env.EXPO_IMAGE_UTILS_NO_SHARP=1`.\n */\nexport async function isAvailableAsync(): Promise {\n if (isSharpDisabled) {\n return false;\n }\n try {\n return !!(await findSharpBinAsync());\n } catch {\n return false;\n }\n}\n\nexport async function sharpAsync(\n options: SharpGlobalOptions,\n commands: SharpCommandOptions[] = []\n): Promise {\n const bin = await findSharpBinAsync();\n try {\n const { stdout } = await spawnAsync(bin, [\n ...getOptions(options),\n ...getCommandOptions(commands),\n ]);\n const outputFilePaths = stdout.trim().split('\\n');\n return outputFilePaths;\n } catch (error) {\n if (error.stderr) {\n throw new Error(\n '\\nProcessing images using sharp-cli failed: ' +\n error.message +\n '\\nOutput: ' +\n error.stderr.replace(SHARP_HELP_PATTERN, '')\n );\n } else {\n throw error;\n }\n }\n}\n\nfunction getOptions(options: Options): string[] {\n const args = [];\n for (const [key, value] of Object.entries(options)) {\n if (value != null && value !== false) {\n if (typeof value === 'boolean') {\n args.push(`--${key}`);\n } else if (typeof value === 'number') {\n args.push(`--${key}`, value.toFixed());\n } else {\n args.push(`--${key}`, value);\n }\n }\n }\n return args;\n}\n\nfunction getCommandOptions(commands: SharpCommandOptions[]): string[] {\n const args: string[] = [];\n for (const command of commands) {\n if (command.operation === 'resize') {\n const { operation, width, ...namedOptions } = command;\n args.push(operation, width.toFixed(), ...getOptions(namedOptions));\n } else {\n const { operation, ...namedOptions } = command;\n args.push(operation, ...getOptions(namedOptions));\n }\n args.push('--');\n }\n return args;\n}\n\nlet _sharpBin: string | null = null;\nlet _sharpInstance: any | null = null;\n\nasync function findSharpBinAsync(): Promise {\n if (_sharpBin) {\n return _sharpBin;\n }\n try {\n const sharpCliPackage = require('sharp-cli/package.json');\n const libVipsVersion = require('sharp').versions.vips;\n if (\n sharpCliPackage &&\n semver.satisfies(sharpCliPackage.version, SHARP_REQUIRED_VERSION) &&\n typeof sharpCliPackage.bin.sharp === 'string' &&\n typeof libVipsVersion === 'string'\n ) {\n _sharpBin = require.resolve(`sharp-cli/${sharpCliPackage.bin.sharp}`);\n return _sharpBin;\n }\n } catch (e) {\n // fall back to global sharp-cli\n }\n\n let installedCliVersion;\n try {\n installedCliVersion = (await spawnAsync('sharp', ['--version'])).stdout.toString().trim();\n } catch (e) {\n throw notFoundError(SHARP_REQUIRED_VERSION);\n }\n\n if (!semver.satisfies(installedCliVersion, SHARP_REQUIRED_VERSION)) {\n showVersionMismatchWarning(SHARP_REQUIRED_VERSION, installedCliVersion);\n }\n _sharpBin = 'sharp';\n return _sharpBin;\n}\n\n/**\n * Returns the instance of `sharp` installed by the global `sharp-cli` package.\n * This method will throw errors if the `sharp` instance cannot be found, these errors can be circumvented by ensuring `isAvailableAsync()` resolves to `true`.\n */\nexport async function findSharpInstanceAsync(): Promise {\n if (isSharpDisabled) {\n throw new Error(\n 'Global instance of sharp-cli cannot be retrieved because sharp-cli has been disabled with the environment variable `EXPO_IMAGE_UTILS_NO_SHARP`'\n );\n }\n if (_sharpInstance) {\n return _sharpInstance;\n }\n // Ensure sharp-cli version is correct\n await findSharpBinAsync();\n\n // Attempt to use local sharp package\n try {\n const sharp = require('sharp');\n _sharpInstance = sharp;\n return sharp;\n } catch {}\n\n // Attempt to resolve the sharp instance used by the global CLI\n let sharpCliPath;\n try {\n sharpCliPath = (await spawnAsync('which', ['sharp'])).stdout.toString().trim();\n } catch (e) {\n throw new Error(`Failed to find the instance of sharp used by the global sharp-cli package.`);\n }\n\n // resolve sharp from the sharp-cli package\n const sharpPath = resolveFrom.silent(sharpCliPath, 'sharp');\n\n if (sharpPath) {\n try {\n // attempt to require the global sharp package\n _sharpInstance = require(sharpPath);\n } catch {}\n }\n\n return _sharpInstance;\n}\n\nfunction notFoundError(requiredCliVersion: string): Error {\n return new Error(\n `This command requires version ${requiredCliVersion} of \\`sharp-cli\\`. \\n` +\n `You can install it using \\`npm install -g sharp-cli@${requiredCliVersion}\\`. \\n` +\n '\\n' +\n 'For prerequisites, see: https://sharp.dimens.io/en/stable/install/#prerequisites'\n );\n}\n\nlet versionMismatchWarningShown = false;\n\nfunction showVersionMismatchWarning(requiredCliVersion: string, installedCliVersion: string) {\n if (versionMismatchWarningShown) {\n return;\n }\n console.warn(\n `Warning: This command requires version ${requiredCliVersion} of \\`sharp-cli\\`. \\n` +\n `Currently installed version: \"${installedCliVersion}\" \\n` +\n `Required version: \"${requiredCliVersion}\" \\n` +\n `You can install it using \\`npm install -g sharp-cli@${requiredCliVersion}\\`. \\n` +\n '\\n' +\n 'For prerequisites, see: https://sharp.dimens.io/en/stable/install/#prerequisites'\n );\n versionMismatchWarningShown = true;\n}\n"]}