UNPKG

@khoohaoyit/image-size

Version:

A stream based image size extractor in Node

77 lines (76 loc) 2.75 kB
"use strict"; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractIcnsSize = void 0; exports.extractIcnsSize = function (stream) { return __asyncGenerator(this, arguments, function* () { if ((yield __await(stream.readString(0, 4))) !== 'icns') return yield __await(void 0); const fileLength = yield __await(stream.readUint32BE(FILE_LENGTH_OFFSET)); let imageOffset = SIZE_HEADER; do { const iconType = yield __await(stream.readString(imageOffset, ENTRY_LENGTH_OFFSET)); const size = ICON_TYPE_SIZE[iconType]; if (!size) throw new Error(`Unknown iconType: ${iconType}`); yield yield __await({ width: size, height: size, type: iconType }); imageOffset += yield __await(stream.readUint32BE(imageOffset + ENTRY_LENGTH_OFFSET)); } while (imageOffset !== fileLength); }); }; const SIZE_HEADER = 4 + 4; const FILE_LENGTH_OFFSET = 4; const ENTRY_LENGTH_OFFSET = 4; const ICON_TYPE_SIZE = { ICON: 32, 'ICN#': 32, // m => 16 x 16 'icm#': 16, icm4: 16, icm8: 16, // s => 16 x 16 'ics#': 16, ics4: 16, ics8: 16, is32: 16, s8mk: 16, icp4: 16, // l => 32 x 32 icl4: 32, icl8: 32, il32: 32, l8mk: 32, icp5: 32, ic11: 32, // h => 48 x 48 ich4: 48, ich8: 48, ih32: 48, h8mk: 48, // . => 64 x 64 icp6: 64, ic12: 32, // t => 128 x 128 it32: 128, t8mk: 128, ic07: 128, // . => 256 x 256 ic08: 256, ic13: 256, // . => 512 x 512 ic09: 512, ic14: 512, // . => 1024 x 1024 ic10: 1024, };