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