image-size
Advanced tools
Comparing version 0.8.1 to 0.8.2
/// <reference types="node" /> | ||
import { ISizes, ISize } from './types/interface'; | ||
import './fs.promises'; | ||
declare type CallbackFn = (e: Error | null, b?: Buffer) => void; | ||
declare type Dimensions = ISize | ISizes | null | undefined; | ||
export declare function imageSize(input: Buffer): Dimensions; | ||
export declare function imageSize(input: Buffer | string): Dimensions; | ||
export declare function imageSize(input: string, callback: CallbackFn): void; | ||
@@ -7,0 +8,0 @@ export declare const setConcurrency: (c: number) => void; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -8,2 +17,3 @@ const fs = require("fs"); | ||
const detector_1 = require("./detector"); | ||
require("./fs.promises"); | ||
// Maximum buffer size, with a default of 512 kilobytes. | ||
@@ -37,37 +47,17 @@ // TO-DO: make this adaptive based on the initial signature of the image | ||
* Reads a file into a buffer. | ||
* | ||
* The callback will be called after the process has completed. The | ||
* callback's first argument will be an error (or null). The second argument | ||
* will be the Buffer, if the operation was successful. | ||
* | ||
* @param {String} filepath | ||
* @param {Function} callback | ||
* @returns {Promise<Buffer>} | ||
*/ | ||
function asyncFileToBuffer(filepath, callback) { | ||
// open the file in read only mode | ||
fs.open(filepath, 'r', (err1, descriptor) => { | ||
if (err1) { | ||
return callback(err1); | ||
function asyncFileToBuffer(filepath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const handle = yield fs.promises.open(filepath, 'r'); | ||
const { size } = yield handle.stat(); | ||
if (size <= 0) { | ||
throw new Error('Empty file'); | ||
} | ||
fs.fstat(descriptor, (err2, stats) => { | ||
if (err2) { | ||
return callback(err2); | ||
} | ||
const size = stats.size; | ||
if (size <= 0) { | ||
return callback(new Error('File size is not greater than 0 —— ' + filepath)); | ||
} | ||
const bufferSize = Math.min(size, MaxBufferSize); | ||
const buffer = Buffer.alloc(bufferSize); | ||
// read first buffer block from the file, asynchronously | ||
fs.read(descriptor, buffer, 0, bufferSize, 0, (err3) => { | ||
if (err3) { | ||
return callback(err3); | ||
} | ||
// close the file, we are done | ||
fs.close(descriptor, (err4) => { | ||
callback(err4, buffer); | ||
}); | ||
}); | ||
}); | ||
const bufferSize = Math.min(size, MaxBufferSize); | ||
const buffer = Buffer.alloc(bufferSize); | ||
yield handle.read(buffer, 0, bufferSize, 0); | ||
yield handle.close(); | ||
return buffer; | ||
}); | ||
@@ -108,20 +98,5 @@ } | ||
if (typeof callback === 'function') { | ||
queue.push((done) => { | ||
asyncFileToBuffer(filepath, (err, buffer) => { | ||
if (err || !buffer) { | ||
return callback(err); | ||
} | ||
let dimensions = null; | ||
try { | ||
dimensions = lookup(buffer, filepath); | ||
} | ||
catch (e) { | ||
err = e; | ||
} | ||
callback(err, dimensions); | ||
if (typeof done === 'function') { | ||
done(undefined, {}); | ||
} | ||
}); | ||
}); | ||
queue.push(() => asyncFileToBuffer(filepath) | ||
.then((buffer) => process.nextTick(callback, null, lookup(buffer, filepath))) | ||
.catch(callback)); | ||
} | ||
@@ -128,0 +103,0 @@ else { |
@@ -10,3 +10,5 @@ "use strict"; | ||
const jpg_1 = require("./types/jpg"); | ||
const ktx_1 = require("./types/ktx"); | ||
const png_1 = require("./types/png"); | ||
const pnm_1 = require("./types/pnm"); | ||
const psd_1 = require("./types/psd"); | ||
@@ -24,3 +26,5 @@ const svg_1 = require("./types/svg"); | ||
jpg: jpg_1.JPG, | ||
ktx: ktx_1.KTX, | ||
png: png_1.PNG, | ||
pnm: pnm_1.PNM, | ||
psd: psd_1.PSD, | ||
@@ -27,0 +31,0 @@ svg: svg_1.SVG, |
{ | ||
"name": "image-size", | ||
"version": "0.8.1", | ||
"version": "0.8.2", | ||
"description": "get dimensions of any image file", | ||
@@ -49,5 +49,5 @@ "main": "dist/index.js", | ||
"@types/node": "12.7.5", | ||
"chai": "4.2.0", | ||
"coveralls": "3.0.6", | ||
"eslint": "6.4.0", | ||
"expect.js": "0.3.1", | ||
"glob": "7.1.4", | ||
@@ -54,0 +54,0 @@ "mocha": "6.2.0", |
@@ -15,2 +15,3 @@ # image-size | ||
* CUR | ||
* DDS | ||
* GIF | ||
@@ -20,13 +21,10 @@ * ICNS | ||
* JPEG | ||
* KTX | ||
* PNG | ||
* PNM (PAM, PBM, PFM, PGM, PPM) | ||
* PSD | ||
* SVG | ||
* TIFF | ||
* WebP | ||
* SVG | ||
* DDS | ||
### Upcoming | ||
* SWF | ||
## Programmatic Usage | ||
@@ -33,0 +31,0 @@ |
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
40958
44
1071
138
5