1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | exports.types = exports.setConcurrency = exports.disableTypes = exports.disableFS = exports.imageSize = void 0;
|
13 | const fs = require("fs");
|
14 | const path = require("path");
|
15 | const queue_1 = require("queue");
|
16 | const types_1 = require("./types");
|
17 | const detector_1 = require("./detector");
|
18 |
|
19 |
|
20 | const MaxBufferSize = 512 * 1024;
|
21 |
|
22 | const queue = new queue_1.default({ concurrency: 100, autostart: true });
|
23 | const globalOptions = {
|
24 | disabledFS: false,
|
25 | disabledTypes: []
|
26 | };
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function lookup(buffer, filepath) {
|
35 |
|
36 | const type = detector_1.detector(buffer);
|
37 | if (typeof type !== 'undefined') {
|
38 | if (globalOptions.disabledTypes.indexOf(type) > -1) {
|
39 | throw new TypeError('disabled file type: ' + type);
|
40 | }
|
41 |
|
42 | if (type in types_1.typeHandlers) {
|
43 | const size = types_1.typeHandlers[type].calculate(buffer, filepath);
|
44 | if (size !== undefined) {
|
45 | size.type = type;
|
46 | return size;
|
47 | }
|
48 | }
|
49 | }
|
50 |
|
51 | throw new TypeError('unsupported file type: ' + type + ' (file: ' + filepath + ')');
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function asyncFileToBuffer(filepath) {
|
59 | return __awaiter(this, void 0, void 0, function* () {
|
60 | const handle = yield fs.promises.open(filepath, 'r');
|
61 | const { size } = yield handle.stat();
|
62 | if (size <= 0) {
|
63 | yield handle.close();
|
64 | throw new Error('Empty file');
|
65 | }
|
66 | const bufferSize = Math.min(size, MaxBufferSize);
|
67 | const buffer = Buffer.alloc(bufferSize);
|
68 | yield handle.read(buffer, 0, bufferSize, 0);
|
69 | yield handle.close();
|
70 | return buffer;
|
71 | });
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function syncFileToBuffer(filepath) {
|
80 |
|
81 | const descriptor = fs.openSync(filepath, 'r');
|
82 | const { size } = fs.fstatSync(descriptor);
|
83 | if (size <= 0) {
|
84 | fs.closeSync(descriptor);
|
85 | throw new Error('Empty file');
|
86 | }
|
87 | const bufferSize = Math.min(size, MaxBufferSize);
|
88 | const buffer = Buffer.alloc(bufferSize);
|
89 | fs.readSync(descriptor, buffer, 0, bufferSize, 0);
|
90 | fs.closeSync(descriptor);
|
91 | return buffer;
|
92 | }
|
93 |
|
94 | module.exports = exports = imageSize;
|
95 | exports.default = imageSize;
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | function imageSize(input, callback) {
|
101 |
|
102 | if (Buffer.isBuffer(input)) {
|
103 | return lookup(input);
|
104 | }
|
105 |
|
106 | if (typeof input !== 'string' || globalOptions.disabledFS) {
|
107 | throw new TypeError('invalid invocation. input should be a Buffer');
|
108 | }
|
109 |
|
110 | const filepath = path.resolve(input);
|
111 | if (typeof callback === 'function') {
|
112 | queue.push(() => asyncFileToBuffer(filepath)
|
113 | .then((buffer) => process.nextTick(callback, null, lookup(buffer, filepath)))
|
114 | .catch(callback));
|
115 | }
|
116 | else {
|
117 | const buffer = syncFileToBuffer(filepath);
|
118 | return lookup(buffer, filepath);
|
119 | }
|
120 | }
|
121 | exports.imageSize = imageSize;
|
122 | const disableFS = (v) => { globalOptions.disabledFS = v; };
|
123 | exports.disableFS = disableFS;
|
124 | const disableTypes = (types) => { globalOptions.disabledTypes = types; };
|
125 | exports.disableTypes = disableTypes;
|
126 | const setConcurrency = (c) => { queue.concurrency = c; };
|
127 | exports.setConcurrency = setConcurrency;
|
128 | exports.types = Object.keys(types_1.typeHandlers);
|