UNPKG

@sketch-hq/sketch-assistant-utils

Version:

Utility functions and types for Sketch Assistants.

43 lines (42 loc) 1.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getImageMetadata = void 0; const node_stream_zip_1 = __importDefault(require("node-stream-zip")); const probe_image_size_1 = __importDefault(require("probe-image-size")); /** * Efficiently access image metadata from a zipped Sketch document. Streams * the image from the zip, and returns as soon as the image dimensions are * parsed from the header chunks. */ const getImageMetadata = (ref, filepath) => new Promise((resolve, reject) => { const zip = new node_stream_zip_1.default({ file: filepath, storeEntries: true }); zip.on('error', (error) => reject(error)); zip.on('ready', () => { zip.stream(ref, (error, stream) => { if (!stream) return reject(); stream.on('end', () => zip.close()); if (error) { reject(error); } else { probe_image_size_1.default(stream) .then((result) => { // @ts-ignore if ('destroy' in stream) stream.destroy(); resolve({ width: result.width, height: result.height, ref, }); }) .catch((error) => reject(error)); } }); }); }); exports.getImageMetadata = getImageMetadata;