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.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 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function lookup(buffer, filepath) {
|
31 |
|
32 | const type = detector_1.detector(buffer);
|
33 |
|
34 | if (type && type in types_1.typeHandlers) {
|
35 | const size = types_1.typeHandlers[type].calculate(buffer, filepath);
|
36 | if (size !== undefined) {
|
37 | size.type = type;
|
38 | return size;
|
39 | }
|
40 | }
|
41 |
|
42 | throw new TypeError('unsupported file type: ' + type + ' (file: ' + filepath + ')');
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function asyncFileToBuffer(filepath) {
|
50 | return __awaiter(this, void 0, void 0, function* () {
|
51 | const handle = yield fs.promises.open(filepath, 'r');
|
52 | const { size } = yield handle.stat();
|
53 | if (size <= 0) {
|
54 | yield handle.close();
|
55 | throw new Error('Empty file');
|
56 | }
|
57 | const bufferSize = Math.min(size, MaxBufferSize);
|
58 | const buffer = Buffer.alloc(bufferSize);
|
59 | yield handle.read(buffer, 0, bufferSize, 0);
|
60 | yield handle.close();
|
61 | return buffer;
|
62 | });
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function syncFileToBuffer(filepath) {
|
71 |
|
72 | const descriptor = fs.openSync(filepath, 'r');
|
73 | const { size } = fs.fstatSync(descriptor);
|
74 | if (size <= 0) {
|
75 | fs.closeSync(descriptor);
|
76 | throw new Error('Empty file');
|
77 | }
|
78 | const bufferSize = Math.min(size, MaxBufferSize);
|
79 | const buffer = Buffer.alloc(bufferSize);
|
80 | fs.readSync(descriptor, buffer, 0, bufferSize, 0);
|
81 | fs.closeSync(descriptor);
|
82 | return buffer;
|
83 | }
|
84 |
|
85 | module.exports = exports = imageSize;
|
86 | exports.default = imageSize;
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function imageSize(input, callback) {
|
92 |
|
93 | if (Buffer.isBuffer(input)) {
|
94 | return lookup(input);
|
95 | }
|
96 |
|
97 | if (typeof input !== 'string') {
|
98 | throw new TypeError('invalid invocation');
|
99 | }
|
100 |
|
101 | const filepath = path.resolve(input);
|
102 | if (typeof callback === 'function') {
|
103 | queue.push(() => asyncFileToBuffer(filepath)
|
104 | .then((buffer) => process.nextTick(callback, null, lookup(buffer, filepath)))
|
105 | .catch(callback));
|
106 | }
|
107 | else {
|
108 | const buffer = syncFileToBuffer(filepath);
|
109 | return lookup(buffer, filepath);
|
110 | }
|
111 | }
|
112 | exports.imageSize = imageSize;
|
113 | exports.setConcurrency = (c) => { queue.concurrency = c; };
|
114 | exports.types = Object.keys(types_1.typeHandlers);
|