image-size
Advanced tools
Comparing version 2.0.0-alpha.2 to 2.0.0-beta
@@ -5,27 +5,23 @@ "use strict"; | ||
const index_1 = require("./types/index"); | ||
const keys = Object.keys(index_1.typeHandlers); | ||
// This map helps avoid validating for every single image type | ||
const firstBytes = { | ||
0x38: 'psd', | ||
0x42: 'bmp', | ||
0x44: 'dds', | ||
0x47: 'gif', | ||
0x49: 'tiff', | ||
0x4d: 'tiff', | ||
0x52: 'webp', | ||
0x69: 'icns', | ||
0x89: 'png', | ||
0xff: 'jpg', | ||
}; | ||
const firstBytes = new Map([ | ||
[0x38, 'psd'], | ||
[0x42, 'bmp'], | ||
[0x44, 'dds'], | ||
[0x47, 'gif'], | ||
[0x49, 'tiff'], | ||
[0x4d, 'tiff'], | ||
[0x52, 'webp'], | ||
[0x69, 'icns'], | ||
[0x89, 'png'], | ||
[0xff, 'jpg'], | ||
]); | ||
function detector(input) { | ||
const byte = input[0]; | ||
if (byte in firstBytes) { | ||
const type = firstBytes[byte]; | ||
if (type && index_1.typeHandlers[type].validate(input)) { | ||
return type; | ||
} | ||
const type = firstBytes.get(byte); | ||
if (type && index_1.typeHandlers.get(type).validate(input)) { | ||
return type; | ||
} | ||
const finder = (key) => index_1.typeHandlers[key].validate(input); | ||
return keys.find(finder); | ||
return index_1.types.find((type) => index_1.typeHandlers.get(type).validate(input)); | ||
} | ||
exports.detector = detector; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.types = exports.disableTypes = exports.imageSize = void 0; | ||
exports.disableTypes = exports.imageSize = exports.types = void 0; | ||
var types_1 = require("./types"); | ||
Object.defineProperty(exports, "types", { enumerable: true, get: function () { return types_1.types; } }); | ||
var lookup_1 = require("./lookup"); | ||
Object.defineProperty(exports, "imageSize", { enumerable: true, get: function () { return lookup_1.lookup; } }); | ||
Object.defineProperty(exports, "disableTypes", { enumerable: true, get: function () { return lookup_1.disableTypes; } }); | ||
Object.defineProperty(exports, "types", { enumerable: true, get: function () { return lookup_1.types; } }); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.types = exports.disableTypes = exports.lookup = void 0; | ||
exports.disableTypes = exports.lookup = void 0; | ||
const index_1 = require("./types/index"); | ||
@@ -13,3 +13,3 @@ const detector_1 = require("./detector"); | ||
* @param {Uint8Array} input | ||
* @returns {Object} | ||
* @returns {ISizeCalculationResult} | ||
*/ | ||
@@ -24,8 +24,6 @@ function lookup(input) { | ||
// find an appropriate handler for this file type | ||
if (type in index_1.typeHandlers) { | ||
const size = index_1.typeHandlers[type].calculate(input); | ||
if (size !== undefined) { | ||
size.type = type; | ||
return size; | ||
} | ||
const size = index_1.typeHandlers.get(type).calculate(input); | ||
if (size !== undefined) { | ||
size.type = size.type ?? type; | ||
return size; | ||
} | ||
@@ -41,2 +39,1 @@ } | ||
exports.disableTypes = disableTypes; | ||
exports.types = Object.keys(index_1.typeHandlers); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.typeHandlers = void 0; | ||
exports.types = exports.typeHandlers = void 0; | ||
// load all available handlers explicitely for browserify support | ||
@@ -9,2 +9,3 @@ const bmp_1 = require("./bmp"); | ||
const gif_1 = require("./gif"); | ||
const heif_1 = require("./heif"); | ||
const icns_1 = require("./icns"); | ||
@@ -23,20 +24,22 @@ const ico_1 = require("./ico"); | ||
const webp_1 = require("./webp"); | ||
exports.typeHandlers = { | ||
bmp: bmp_1.BMP, | ||
cur: cur_1.CUR, | ||
dds: dds_1.DDS, | ||
gif: gif_1.GIF, | ||
icns: icns_1.ICNS, | ||
ico: ico_1.ICO, | ||
j2c: j2c_1.J2C, | ||
jp2: jp2_1.JP2, | ||
jpg: jpg_1.JPG, | ||
ktx: ktx_1.KTX, | ||
png: png_1.PNG, | ||
pnm: pnm_1.PNM, | ||
psd: psd_1.PSD, | ||
svg: svg_1.SVG, | ||
tga: tga_1.TGA, | ||
tiff: tiff_1.TIFF, | ||
webp: webp_1.WEBP, | ||
}; | ||
exports.typeHandlers = new Map([ | ||
['bmp', bmp_1.BMP], | ||
['cur', cur_1.CUR], | ||
['dds', dds_1.DDS], | ||
['gif', gif_1.GIF], | ||
['heif', heif_1.HEIF], | ||
['icns', icns_1.ICNS], | ||
['ico', ico_1.ICO], | ||
['j2c', j2c_1.J2C], | ||
['jp2', jp2_1.JP2], | ||
['jpg', jpg_1.JPG], | ||
['ktx', ktx_1.KTX], | ||
['png', png_1.PNG], | ||
['pnm', pnm_1.PNM], | ||
['psd', psd_1.PSD], | ||
['svg', svg_1.SVG], | ||
['tga', tga_1.TGA], | ||
['tiff', tiff_1.TIFF], | ||
['webp', webp_1.WEBP], | ||
]); | ||
exports.types = Array.from(exports.typeHandlers.keys()); |
@@ -5,58 +5,22 @@ "use strict"; | ||
const utils_1 = require("./utils"); | ||
const BoxTypes = { | ||
ftyp: '66747970', | ||
ihdr: '69686472', | ||
jp2h: '6a703268', | ||
jp__: '6a502020', | ||
rreq: '72726571', | ||
xml_: '786d6c20', | ||
}; | ||
const calculateRREQLength = (box) => { | ||
const unit = box[0]; | ||
let offset = 1 + 2 * unit; | ||
const numStdFlags = (0, utils_1.readUInt16BE)(box, offset); | ||
const flagsLength = numStdFlags * (2 + unit); | ||
offset = offset + 2 + flagsLength; | ||
const numVendorFeatures = (0, utils_1.readUInt16BE)(box, offset); | ||
const featuresLength = numVendorFeatures * (16 + unit); | ||
return offset + 2 + featuresLength; | ||
}; | ||
const parseIHDR = (box) => { | ||
return { | ||
height: (0, utils_1.readUInt32BE)(box, 4), | ||
width: (0, utils_1.readUInt32BE)(box, 8), | ||
}; | ||
}; | ||
exports.JP2 = { | ||
validate(input) { | ||
const signature = (0, utils_1.toHexString)(input, 4, 8); | ||
const signatureLength = (0, utils_1.readUInt32BE)(input, 0); | ||
if (signature !== BoxTypes.jp__ || signatureLength < 1) { | ||
if ((0, utils_1.readUInt32BE)(input, 4) !== 0x6a502020 || (0, utils_1.readUInt32BE)(input, 0) < 1) | ||
return false; | ||
} | ||
const ftypeBoxStart = signatureLength + 4; | ||
const ftypBoxLength = (0, utils_1.readUInt32BE)(input, signatureLength); | ||
const ftypBox = input.slice(ftypeBoxStart, ftypeBoxStart + ftypBoxLength); | ||
return (0, utils_1.toHexString)(ftypBox, 0, 4) === BoxTypes.ftyp; | ||
const ftypBox = (0, utils_1.findBox)(input, 'ftyp', 0); | ||
if (!ftypBox) | ||
return false; | ||
return (0, utils_1.readUInt32BE)(input, ftypBox.offset + 4) === 0x66747970; | ||
}, | ||
calculate(input) { | ||
const signatureLength = (0, utils_1.readUInt32BE)(input, 0); | ||
const ftypBoxLength = (0, utils_1.readUInt16BE)(input, signatureLength + 2); | ||
let offset = signatureLength + 4 + ftypBoxLength; | ||
const nextBoxType = (0, utils_1.toHexString)(input, offset, offset + 4); | ||
switch (nextBoxType) { | ||
case BoxTypes.rreq: | ||
// WHAT ARE THESE 4 BYTES????? | ||
// eslint-disable-next-line no-case-declarations | ||
const MAGIC = 4; | ||
offset = | ||
offset + 4 + MAGIC + calculateRREQLength(input.slice(offset + 4)); | ||
return parseIHDR(input.slice(offset + 8, offset + 24)); | ||
case BoxTypes.jp2h: | ||
return parseIHDR(input.slice(offset + 8, offset + 24)); | ||
default: | ||
throw new TypeError('Unsupported header found: ' + | ||
(0, utils_1.toUTF8String)(input, offset, offset + 4)); | ||
const jp2hBox = (0, utils_1.findBox)(input, 'jp2h', 0); | ||
const ihdrBox = jp2hBox && (0, utils_1.findBox)(input, 'ihdr', jp2hBox.offset + 8); | ||
if (ihdrBox) { | ||
return { | ||
height: (0, utils_1.readUInt32BE)(input, ihdrBox.offset + 8), | ||
width: (0, utils_1.readUInt32BE)(input, ihdrBox.offset + 12), | ||
}; | ||
} | ||
throw new TypeError('Unsupported JPEG 2000 format'); | ||
}, | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.readUInt = exports.readUInt32LE = exports.readUInt32BE = exports.readInt32LE = exports.readUInt24LE = exports.readUInt16LE = exports.readUInt16BE = exports.readInt16LE = exports.toHexString = exports.toUTF8String = void 0; | ||
exports.findBox = exports.readUInt = exports.readUInt32LE = exports.readUInt32BE = exports.readInt32LE = exports.readUInt24LE = exports.readUInt16LE = exports.readUInt16BE = exports.readInt16LE = exports.toHexString = exports.toUTF8String = void 0; | ||
const decoder = new TextDecoder(); | ||
@@ -51,1 +51,24 @@ const toUTF8String = (input, start = 0, end = input.length) => decoder.decode(input.slice(start, end)); | ||
exports.readUInt = readUInt; | ||
function readBox(buffer, offset) { | ||
if (buffer.length - offset < 4) | ||
return; | ||
const boxSize = (0, exports.readUInt32BE)(buffer, offset); | ||
if (buffer.length - offset < boxSize) | ||
return; | ||
return { | ||
name: (0, exports.toUTF8String)(buffer, 4 + offset, 8 + offset), | ||
offset, | ||
size: boxSize, | ||
}; | ||
} | ||
function findBox(buffer, boxName, offset) { | ||
while (offset < buffer.length) { | ||
const box = readBox(buffer, offset); | ||
if (!box) | ||
break; | ||
if (box.name === boxName) | ||
return box; | ||
offset += box.size; | ||
} | ||
} | ||
exports.findBox = findBox; |
@@ -1,1 +0,2 @@ | ||
export { lookup as imageSize, disableTypes, types } from './lookup'; | ||
export { types } from './types'; | ||
export { lookup as imageSize, disableTypes } from './lookup'; |
@@ -7,6 +7,5 @@ import type { imageType } from './types/index'; | ||
* @param {Uint8Array} input | ||
* @returns {Object} | ||
* @returns {ISizeCalculationResult} | ||
*/ | ||
export declare function lookup(input: Uint8Array): ISizeCalculationResult; | ||
export declare const disableTypes: (types: imageType[]) => void; | ||
export declare const types: string[]; |
@@ -1,20 +0,3 @@ | ||
export declare const typeHandlers: { | ||
bmp: import("./interface").IImage; | ||
cur: import("./interface").IImage; | ||
dds: import("./interface").IImage; | ||
gif: import("./interface").IImage; | ||
icns: import("./interface").IImage; | ||
ico: import("./interface").IImage; | ||
j2c: import("./interface").IImage; | ||
jp2: import("./interface").IImage; | ||
jpg: import("./interface").IImage; | ||
ktx: import("./interface").IImage; | ||
png: import("./interface").IImage; | ||
pnm: import("./interface").IImage; | ||
psd: import("./interface").IImage; | ||
svg: import("./interface").IImage; | ||
tga: import("./interface").IImage; | ||
tiff: import("./interface").IImage; | ||
webp: import("./interface").IImage; | ||
}; | ||
export type imageType = keyof typeof typeHandlers; | ||
export declare const typeHandlers: Map<"svg" | "heif" | "icns" | "bmp" | "cur" | "dds" | "gif" | "ico" | "j2c" | "jp2" | "jpg" | "ktx" | "png" | "pnm" | "psd" | "tga" | "tiff" | "webp", import("./interface").IImage>; | ||
export declare const types: ("svg" | "heif" | "icns" | "bmp" | "cur" | "dds" | "gif" | "ico" | "j2c" | "jp2" | "jpg" | "ktx" | "png" | "pnm" | "psd" | "tga" | "tiff" | "webp")[]; | ||
export type imageType = typeof types[number]; |
@@ -11,1 +11,6 @@ export declare const toUTF8String: (input: Uint8Array, start?: number, end?: number) => string; | ||
export declare function readUInt(input: Uint8Array, bits: 16 | 32, offset: number, isBigEndian: boolean): number; | ||
export declare function findBox(buffer: Uint8Array, boxName: string, offset: number): { | ||
name: string; | ||
offset: number; | ||
size: number; | ||
} | undefined; |
{ | ||
"name": "image-size", | ||
"version": "2.0.0-alpha.2", | ||
"version": "2.0.0-beta", | ||
"description": "get dimensions of any image file", | ||
@@ -50,14 +50,17 @@ "main": "./dist/cjs/index.js", | ||
"height", | ||
"png", | ||
"jpeg", | ||
"avif", | ||
"bmp", | ||
"cur", | ||
"gif", | ||
"heic", | ||
"heif", | ||
"icns", | ||
"ico", | ||
"jpeg", | ||
"png", | ||
"psd", | ||
"svg", | ||
"tga", | ||
"tiff", | ||
"webp", | ||
"svg", | ||
"icns", | ||
"ico", | ||
"cur" | ||
"webp" | ||
], | ||
@@ -89,3 +92,7 @@ "repository": "git://github.com/image-size/image-size.git", | ||
"typescript": "5.2.2" | ||
}, | ||
"nyc": { | ||
"include": "lib", | ||
"exclude": "specs/*.spec.ts" | ||
} | ||
} |
@@ -15,2 +15,3 @@ # image-size | ||
- GIF | ||
- HEIC (HEIF, AVCI, AVIF) | ||
- ICNS | ||
@@ -17,0 +18,0 @@ - ICO |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
84588
81
2212
150
0