@shibam/sticker-maker 1.1.4 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
1
  import { Readable } from 'stream';
4
2
  import { MetaDataType } from './types/metaInfoType.js';
5
3
  import { StickerTypes } from './types/StickerTypes.js';
package/dist/index.js CHANGED
@@ -1,55 +1,18 @@
1
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
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
4
  };
41
5
  Object.defineProperty(exports, "__esModule", { value: true });
42
6
  exports.StickerTypes = exports.Sticker = void 0;
43
- var fs_1 = __importDefault(require("fs"));
44
- var utils_js_1 = __importDefault(require("./lib/utils.js"));
45
- var ToWebp_js_1 = __importDefault(require("./lib/ToWebp.js"));
46
- var StickerTypes_js_1 = require("./types/StickerTypes.js");
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const utils_js_1 = __importDefault(require("./lib/utils.js"));
9
+ const ToWebp_js_1 = __importDefault(require("./lib/ToWebp.js"));
10
+ const StickerTypes_js_1 = require("./types/StickerTypes.js");
47
11
  Object.defineProperty(exports, "StickerTypes", { enumerable: true, get: function () { return StickerTypes_js_1.StickerTypes; } });
48
- var changeMetaInfo_js_1 = __importDefault(require("./lib/changeMetaInfo.js"));
49
- var extractMetaData_js_1 = __importDefault(require("./lib/extractMetaData.js"));
50
- var Sticker = /** @class */ (function () {
51
- function Sticker(data, metaInfo) {
52
- if (metaInfo === void 0) { metaInfo = {}; }
12
+ const changeMetaInfo_js_1 = __importDefault(require("./lib/changeMetaInfo.js"));
13
+ const extractMetaData_js_1 = __importDefault(require("./lib/extractMetaData.js"));
14
+ class Sticker {
15
+ constructor(data, metaInfo = {}) {
53
16
  this.data = data;
54
17
  this.metaInfo = metaInfo;
55
18
  this.utils = new utils_js_1.default();
@@ -66,158 +29,89 @@ var Sticker = /** @class */ (function () {
66
29
  * - Reads and analyzes input data to determine MIME type and extension type.
67
30
  * - Generates ID and quality metadata if not provided.
68
31
  */
69
- Sticker.prototype.initialize = function () {
70
- return __awaiter(this, void 0, void 0, function () {
71
- var _a, fileType, error_1;
72
- var _b, _c, _d, _e, _f, _g, _h, _j, _k;
73
- return __generator(this, function (_l) {
74
- switch (_l.label) {
75
- case 0:
76
- _l.trys.push([0, 3, , 4]);
77
- _a = this;
78
- return [4 /*yield*/, this.utils.buffer(this.data)];
79
- case 1:
80
- _a.buffer = _l.sent();
81
- return [4 /*yield*/, this.utils.getMimeType(this.buffer)];
82
- case 2:
83
- fileType = _l.sent();
84
- this.mimeType = (_b = fileType === null || fileType === void 0 ? void 0 : fileType.mime) !== null && _b !== void 0 ? _b : '';
85
- this.extType = (_c = fileType === null || fileType === void 0 ? void 0 : fileType.ext) !== null && _c !== void 0 ? _c : '';
86
- this.metaInfo.pack = (_d = this.metaInfo.pack) !== null && _d !== void 0 ? _d : '';
87
- this.metaInfo.author = (_e = this.metaInfo.author) !== null && _e !== void 0 ? _e : '';
88
- this.metaInfo.id = (_f = this.metaInfo.id) !== null && _f !== void 0 ? _f : this.utils.getId();
89
- this.metaInfo.category = (_g = this.metaInfo.category) !== null && _g !== void 0 ? _g : [];
90
- this.metaInfo.type = (_h = this.metaInfo.type) !== null && _h !== void 0 ? _h : StickerTypes_js_1.StickerTypes.DEFAULT;
91
- this.metaInfo.quality = (_k = (_j = this.metaInfo) === null || _j === void 0 ? void 0 : _j.quality) !== null && _k !== void 0 ? _k : this.utils.getQuality(this.buffer);
92
- return [3 /*break*/, 4];
93
- case 3:
94
- error_1 = _l.sent();
95
- throw new Error("Initialization error: ".concat(error_1));
96
- case 4: return [2 /*return*/];
97
- }
98
- });
99
- });
100
- };
32
+ async initialize() {
33
+ try {
34
+ this.buffer = await this.utils.buffer(this.data);
35
+ const fileType = await this.utils.getMimeType(this.buffer);
36
+ this.mimeType = fileType?.mime ?? '';
37
+ this.extType = fileType?.ext ?? '';
38
+ this.metaInfo.pack = this.metaInfo.pack ?? '';
39
+ this.metaInfo.author = this.metaInfo.author ?? '';
40
+ this.metaInfo.id = this.metaInfo.id ?? this.utils.getId();
41
+ this.metaInfo.category = this.metaInfo.category ?? [];
42
+ this.metaInfo.type = this.metaInfo.type ?? StickerTypes_js_1.StickerTypes.DEFAULT;
43
+ this.metaInfo.quality = this.metaInfo?.quality ?? this.utils.getQuality(this.buffer);
44
+ }
45
+ catch (error) {
46
+ throw new Error(`Initialization error: ${error}`);
47
+ }
48
+ }
101
49
  /**
102
50
  * Converts input data to a Buffer containing the converted content.
103
51
  * @returns Promise<Buffer> A Promise resolving to the converted content as Buffer.
104
52
  */
105
- Sticker.prototype.toBuffer = function () {
106
- return __awaiter(this, void 0, void 0, function () {
107
- var buffer, _a, error_2;
108
- return __generator(this, function (_b) {
109
- switch (_b.label) {
110
- case 0:
111
- _b.trys.push([0, 4, , 5]);
112
- return [4 /*yield*/, this.initialize()];
113
- case 1:
114
- _b.sent();
115
- return [4 /*yield*/, (0, ToWebp_js_1.default)(this.buffer, this.metaInfo, this.extType, this.mimeType)];
116
- case 2:
117
- buffer = _b.sent();
118
- _a = this;
119
- return [4 /*yield*/, new changeMetaInfo_js_1.default(this.metaInfo).add(buffer)];
120
- case 3:
121
- _a.outBuffer = _b.sent();
122
- this.activeBuff = true;
123
- return [2 /*return*/, this.outBuffer];
124
- case 4:
125
- error_2 = _b.sent();
126
- this.activeBuff = false;
127
- throw new Error("Conversion to buffer failed: ".concat(error_2));
128
- case 5: return [2 /*return*/];
129
- }
130
- });
131
- });
132
- };
53
+ async toBuffer() {
54
+ try {
55
+ await this.initialize();
56
+ const buffer = await (0, ToWebp_js_1.default)(this.buffer, this.metaInfo, this.extType, this.mimeType);
57
+ this.outBuffer = await new changeMetaInfo_js_1.default(this.metaInfo).add(buffer);
58
+ this.activeBuff = true;
59
+ return this.outBuffer;
60
+ }
61
+ catch (error) {
62
+ this.activeBuff = false;
63
+ throw new Error(`Conversion to buffer failed: ${error}`);
64
+ }
65
+ }
133
66
  /**
134
67
  * Converts input data and writes it to a file at the specified outputPath.
135
68
  * @param outputPath The path where the converted file will be saved.
136
69
  * @returns Promise<void> A Promise resolving when the file is successfully written.
137
70
  */
138
- Sticker.prototype.toFile = function (outputPath) {
139
- return __awaiter(this, void 0, void 0, function () {
140
- var error_3;
141
- var _a;
142
- return __generator(this, function (_b) {
143
- switch (_b.label) {
144
- case 0:
145
- _b.trys.push([0, 6, , 7]);
146
- if (!(!this.activeBuff && !this.activeMeta && ((_a = this.extType) === null || _a === void 0 ? void 0 : _a.includes('webp')))) return [3 /*break*/, 2];
147
- return [4 /*yield*/, this.changeMetaInfo()];
148
- case 1:
149
- _b.sent();
150
- return [3 /*break*/, 4];
151
- case 2: return [4 /*yield*/, this.toBuffer()];
152
- case 3:
153
- _b.sent();
154
- _b.label = 4;
155
- case 4: return [4 /*yield*/, fs_1.default.promises.writeFile(outputPath, this.outBuffer)];
156
- case 5:
157
- _b.sent();
158
- return [3 /*break*/, 7];
159
- case 6:
160
- error_3 = _b.sent();
161
- throw new Error("Conversion to file failed: ".concat(error_3));
162
- case 7: return [2 /*return*/];
163
- }
164
- });
165
- });
166
- };
71
+ async toFile(outputPath) {
72
+ try {
73
+ if (!this.activeBuff && !this.activeMeta && this.extType?.includes('webp')) {
74
+ await this.changeMetaInfo();
75
+ }
76
+ else {
77
+ await this.toBuffer();
78
+ }
79
+ await fs_1.default.promises.writeFile(outputPath, this.outBuffer);
80
+ }
81
+ catch (error) {
82
+ throw new Error(`Conversion to file failed: ${error}`);
83
+ }
84
+ }
167
85
  /**
168
86
  * Changes the metadata of the sticker.
169
87
  * @param newMetaInfo Partial metadata to update.
170
88
  * @returns Promise<Buffer> A Promise resolving to the Buffer with updated metadata.
171
89
  */
172
- Sticker.prototype.changeMetaInfo = function () {
173
- return __awaiter(this, void 0, void 0, function () {
174
- var _a, error_4;
175
- return __generator(this, function (_b) {
176
- switch (_b.label) {
177
- case 0:
178
- _b.trys.push([0, 3, , 4]);
179
- return [4 /*yield*/, this.initialize()];
180
- case 1:
181
- _b.sent();
182
- _a = this;
183
- return [4 /*yield*/, new changeMetaInfo_js_1.default(this.metaInfo).add(this.buffer)];
184
- case 2:
185
- _a.outBuffer = _b.sent();
186
- this.activeMeta = true;
187
- return [2 /*return*/, this.outBuffer];
188
- case 3:
189
- error_4 = _b.sent();
190
- this.activeMeta = false;
191
- throw new Error("Error changing meta info: ".concat(error_4));
192
- case 4: return [2 /*return*/];
193
- }
194
- });
195
- });
196
- };
90
+ async changeMetaInfo() {
91
+ try {
92
+ await this.initialize();
93
+ this.outBuffer = await new changeMetaInfo_js_1.default(this.metaInfo).add(this.buffer);
94
+ this.activeMeta = true;
95
+ return this.outBuffer;
96
+ }
97
+ catch (error) {
98
+ this.activeMeta = false;
99
+ throw new Error(`Error changing meta info: ${error}`);
100
+ }
101
+ }
197
102
  /**
198
103
  * Extracts metadata from the provided data.
199
104
  * @param data Buffer containing the data to extract metadata from.
200
105
  * @returns Promise<Partial<MetaDataType>> A Promise resolving to the extracted metadata.
201
106
  */
202
- Sticker.prototype.extractMetaData = function (data) {
203
- return __awaiter(this, void 0, void 0, function () {
204
- var error_5;
205
- return __generator(this, function (_a) {
206
- switch (_a.label) {
207
- case 0:
208
- _a.trys.push([0, 2, , 3]);
209
- return [4 /*yield*/, this.initialize()];
210
- case 1:
211
- _a.sent();
212
- return [2 /*return*/, (0, extractMetaData_js_1.default)(data)];
213
- case 2:
214
- error_5 = _a.sent();
215
- throw new Error("Error extracting meta data: ".concat(error_5));
216
- case 3: return [2 /*return*/];
217
- }
218
- });
219
- });
220
- };
221
- return Sticker;
222
- }());
107
+ async extractMetaData(data) {
108
+ try {
109
+ await this.initialize();
110
+ return (0, extractMetaData_js_1.default)(data);
111
+ }
112
+ catch (error) {
113
+ throw new Error(`Error extracting meta data: ${error}`);
114
+ }
115
+ }
116
+ }
223
117
  exports.Sticker = Sticker;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { MetaDataType } from '../types/metaInfoType.js';
3
2
  /**
4
3
  * Converts a given buffer to WebP format with optional transformations.
@@ -22,49 +22,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __generator = (this && this.__generator) || function (thisArg, body) {
35
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
- function verb(n) { return function (v) { return step([n, v]); }; }
38
- function step(op) {
39
- if (f) throw new TypeError("Generator is already executing.");
40
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
41
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
42
- if (y = 0, t) op = [op[0] & 2, t.value];
43
- switch (op[0]) {
44
- case 0: case 1: t = op; break;
45
- case 4: _.label++; return { value: op[1], done: false };
46
- case 5: _.label++; y = op[1]; op = [0]; continue;
47
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
- default:
49
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
- if (t[2]) _.ops.pop();
54
- _.trys.pop(); continue;
55
- }
56
- op = body.call(thisArg, _);
57
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
- }
60
- };
61
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
62
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
63
27
  };
64
28
  Object.defineProperty(exports, "__esModule", { value: true });
65
- var sharp_1 = __importStar(require("sharp"));
66
- var StickerTypes_js_1 = require("../types/StickerTypes.js");
67
- var toGif_js_1 = __importDefault(require("./toGif.js"));
29
+ const sharp_1 = __importStar(require("sharp"));
30
+ const StickerTypes_js_1 = require("../types/StickerTypes.js");
31
+ const toGif_js_1 = __importDefault(require("./toGif.js"));
68
32
  /**
69
33
  * Converts a given buffer to WebP format with optional transformations.
70
34
  *
@@ -74,61 +38,48 @@ var toGif_js_1 = __importDefault(require("./toGif.js"));
74
38
  * @param {string} mimeType - The MIME type of the input buffer.
75
39
  * @returns {Promise<Buffer>} - A promise that resolves to the converted WebP buffer.
76
40
  */
77
- var ToWebp = function (buffer, metaInfo, mimeExt, mimeType) { return __awaiter(void 0, void 0, void 0, function () {
78
- var data, _a, isAnimated, res, error_1;
79
- return __generator(this, function (_b) {
80
- switch (_b.label) {
81
- case 0:
82
- _b.trys.push([0, 4, , 5]);
83
- if (mimeExt === 'webp' || mimeExt === 'webm')
84
- return [2 /*return*/, buffer];
85
- if (!(mimeType === null || mimeType === void 0 ? void 0 : mimeType.includes('video'))) return [3 /*break*/, 2];
86
- return [4 /*yield*/, (0, toGif_js_1.default)(buffer, mimeExt, metaInfo.type || StickerTypes_js_1.StickerTypes.DEFAULT)];
87
- case 1:
88
- _a = _b.sent();
89
- return [3 /*break*/, 3];
90
- case 2:
91
- _a = buffer;
92
- _b.label = 3;
93
- case 3:
94
- data = _a;
95
- isAnimated = (mimeType === null || mimeType === void 0 ? void 0 : mimeType.includes('video')) || (mimeExt === null || mimeExt === void 0 ? void 0 : mimeExt.includes('gif'));
96
- res = (0, sharp_1.default)(data, { animated: isAnimated });
97
- if (metaInfo.type === StickerTypes_js_1.StickerTypes.CIRCLE) {
98
- res.resize(512, 512, {
99
- fit: sharp_1.fit.cover
100
- }).composite([
101
- {
102
- input: Buffer.from("<svg width=\"512\" height=\"512\"><circle cx=\"256\" cy=\"256\" r=\"256\" fill=\"\"/></svg>"),
103
- blend: 'dest-in',
104
- gravity: 'northeast',
105
- tile: true
106
- }
107
- ]);
108
- }
109
- else if (metaInfo.type === StickerTypes_js_1.StickerTypes.SQUARE && !(mimeType === null || mimeType === void 0 ? void 0 : mimeType.includes('video'))) {
110
- res.resize(512, 512, {
111
- fit: sharp_1.fit.fill
112
- });
113
- }
114
- else {
115
- res.resize(512, 512, {
116
- fit: sharp_1.fit.contain,
117
- background: { r: 0, g: 0, b: 0, alpha: 0 }
118
- });
41
+ const ToWebp = async (buffer, metaInfo, mimeExt, mimeType) => {
42
+ try {
43
+ if (mimeExt === 'webp')
44
+ return buffer;
45
+ let data = mimeType?.includes('video')
46
+ ? await (0, toGif_js_1.default)(buffer, mimeExt, metaInfo.type || StickerTypes_js_1.StickerTypes.DEFAULT)
47
+ : buffer;
48
+ let isAnimated = mimeType?.includes('video') || mimeExt?.includes('gif');
49
+ const res = (0, sharp_1.default)(data, { animated: isAnimated });
50
+ if (metaInfo.type === StickerTypes_js_1.StickerTypes.CIRCLE) {
51
+ res.resize(512, 512, {
52
+ fit: sharp_1.fit.cover
53
+ }).composite([
54
+ {
55
+ input: Buffer.from(`<svg width="512" height="512"><circle cx="256" cy="256" r="256" fill=""/></svg>`),
56
+ blend: 'dest-in',
57
+ gravity: 'northeast',
58
+ tile: true
119
59
  }
120
- return [2 /*return*/, res
121
- .toFormat('webp')
122
- .webp({
123
- quality: metaInfo.quality,
124
- lossless: mimeExt.includes('gif') ? true : false
125
- })
126
- .toBuffer()];
127
- case 4:
128
- error_1 = _b.sent();
129
- throw new Error("Conversion failed: ".concat(error_1));
130
- case 5: return [2 /*return*/];
60
+ ]);
131
61
  }
132
- });
133
- }); };
62
+ else if (metaInfo.type === StickerTypes_js_1.StickerTypes.SQUARE && !mimeType?.includes('video')) {
63
+ res.resize(512, 512, {
64
+ fit: sharp_1.fit.fill
65
+ });
66
+ }
67
+ else {
68
+ res.resize(512, 512, {
69
+ fit: sharp_1.fit.contain,
70
+ background: { r: 0, g: 0, b: 0, alpha: 0 }
71
+ });
72
+ }
73
+ return res
74
+ .toFormat('webp')
75
+ .webp({
76
+ quality: metaInfo.quality,
77
+ lossless: mimeExt.includes('gif') ? true : false
78
+ })
79
+ .toBuffer();
80
+ }
81
+ catch (error) {
82
+ throw new Error(`Conversion failed: ${error}`);
83
+ }
84
+ };
134
85
  exports.default = ToWebp;
@@ -1,5 +1,3 @@
1
- /// <reference path="../../src/lib/node-webpmux.d.ts" />
2
- /// <reference types="node" />
3
1
  import Image from 'node-webpmux';
4
2
  /**
5
3
  * The Exif class is responsible for handling the metadata (Exif data)
@@ -1,66 +1,29 @@
1
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
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
4
  };
41
5
  Object.defineProperty(exports, "__esModule", { value: true });
42
- var node_webpmux_1 = __importDefault(require("node-webpmux"));
43
- var util_1 = require("util");
6
+ const node_webpmux_1 = __importDefault(require("node-webpmux"));
7
+ const util_1 = require("util");
44
8
  /**
45
9
  * The Exif class is responsible for handling the metadata (Exif data)
46
10
  * for sticker images, particularly those used in messaging applications.
47
11
  */
48
- var Exif = /** @class */ (function () {
12
+ class Exif {
49
13
  /**
50
14
  * Constructs an Exif instance with the given options.
51
15
  * @param options - An object containing metadata for the sticker.
52
16
  */
53
- function Exif(options) {
54
- var _this = this;
17
+ constructor(options) {
55
18
  this.data = {};
56
19
  this.exif = null;
57
20
  /**
58
21
  * Builds the Exif metadata as a Buffer.
59
22
  * @returns A Buffer containing the constructed Exif data.
60
23
  */
61
- this.build = function () {
62
- var data = JSON.stringify(_this.data);
63
- var exif = Buffer.concat([
24
+ this.build = () => {
25
+ const data = JSON.stringify(this.data);
26
+ const exif = Buffer.concat([
64
27
  Buffer.from([
65
28
  0x49, 0x49, 0x2a, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00,
66
29
  0x00, 0x16, 0x00, 0x00, 0x00
@@ -75,49 +38,25 @@ var Exif = /** @class */ (function () {
75
38
  * @param image - A Buffer or Image instance representing the image to which Exif data should be added.
76
39
  * @returns A Promise that resolves to a Buffer containing the image with the added Exif data.
77
40
  */
78
- this.add = function (image) { return __awaiter(_this, void 0, void 0, function () {
79
- var exif, _a;
80
- var _this = this;
81
- return __generator(this, function (_b) {
82
- switch (_b.label) {
83
- case 0:
84
- exif = this.exif || this.build();
85
- if (!(image instanceof node_webpmux_1.default.Image)) return [3 /*break*/, 1];
86
- _a = image;
87
- return [3 /*break*/, 3];
88
- case 1: return [4 /*yield*/, (function () { return __awaiter(_this, void 0, void 0, function () {
89
- var img;
90
- return __generator(this, function (_a) {
91
- switch (_a.label) {
92
- case 0:
93
- img = new node_webpmux_1.default.Image();
94
- return [4 /*yield*/, img.load(image)];
95
- case 1:
96
- _a.sent();
97
- return [2 /*return*/, img];
98
- }
99
- });
100
- }); })()
101
- // Set the Exif data on the image and save it.
102
- ];
103
- case 2:
104
- _a = _b.sent();
105
- _b.label = 3;
106
- case 3:
107
- // Load the image if it is not already an instance of Image.Image.
108
- image = _a;
109
- // Set the Exif data on the image and save it.
110
- image.exif = exif;
111
- return [4 /*yield*/, image.save(null)];
112
- case 4: return [2 /*return*/, _b.sent()];
113
- }
114
- });
115
- }); };
41
+ this.add = async (image) => {
42
+ const exif = this.exif || this.build();
43
+ // Load the image if it is not already an instance of Image.Image.
44
+ image =
45
+ image instanceof node_webpmux_1.default.Image
46
+ ? image
47
+ : await (async () => {
48
+ const img = new node_webpmux_1.default.Image();
49
+ await img.load(image);
50
+ return img;
51
+ })();
52
+ // Set the Exif data on the image and save it.
53
+ image.exif = exif;
54
+ return await image.save(null);
55
+ };
116
56
  this.data['sticker-pack-id'] = options.id || '';
117
57
  this.data['sticker-pack-name'] = options.pack || '';
118
58
  this.data['sticker-pack-publisher'] = options.author || '';
119
59
  this.data['emojis'] = options.category || ['😹'];
120
60
  }
121
- return Exif;
122
- }());
61
+ }
123
62
  exports.default = Exif;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  /**
3
2
  * Extracts metadata from a WebP image.
4
3
  * @param {Buffer}image - The image buffer to extract metadata from
@@ -1,62 +1,17 @@
1
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
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
4
  };
41
5
  Object.defineProperty(exports, "__esModule", { value: true });
42
- var node_webpmux_1 = __importDefault(require("node-webpmux"));
6
+ const node_webpmux_1 = __importDefault(require("node-webpmux"));
43
7
  /**
44
8
  * Extracts metadata from a WebP image.
45
9
  * @param {Buffer}image - The image buffer to extract metadata from
46
10
  */
47
- var extractMetadata = function (image) { return __awaiter(void 0, void 0, void 0, function () {
48
- var img, exif;
49
- var _a, _b, _c;
50
- return __generator(this, function (_d) {
51
- switch (_d.label) {
52
- case 0:
53
- img = new node_webpmux_1.default.Image();
54
- return [4 /*yield*/, img.load(image)];
55
- case 1:
56
- _d.sent();
57
- exif = (_b = (_a = img.exif) === null || _a === void 0 ? void 0 : _a.toString('utf-8')) !== null && _b !== void 0 ? _b : '{}';
58
- return [2 /*return*/, JSON.parse((_c = exif.substring(exif.indexOf('{'), exif.lastIndexOf('}') + 1)) !== null && _c !== void 0 ? _c : '{}')];
59
- }
60
- });
61
- }); };
11
+ const extractMetadata = async (image) => {
12
+ const img = new node_webpmux_1.default.Image();
13
+ await img.load(image);
14
+ const exif = img.exif?.toString('utf-8') ?? '{}';
15
+ return JSON.parse(exif.substring(exif.indexOf('{'), exif.lastIndexOf('}') + 1) ?? '{}');
16
+ };
62
17
  exports.default = extractMetadata;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { StickerTypes } from '../types/StickerTypes.js';
3
2
  declare const videoToGif: (buffer: Buffer, extType: string, type: StickerTypes) => Promise<Buffer>;
4
3
  export default videoToGif;
package/dist/lib/toGif.js CHANGED
@@ -1,92 +1,51 @@
1
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
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
4
  };
41
5
  Object.defineProperty(exports, "__esModule", { value: true });
42
- var fluent_ffmpeg_1 = __importDefault(require("fluent-ffmpeg"));
43
- var ffmpeg_1 = __importDefault(require("@ffmpeg-installer/ffmpeg"));
44
- var stream_1 = require("stream");
45
- var StickerTypes_js_1 = require("../types/StickerTypes.js");
6
+ const fluent_ffmpeg_1 = __importDefault(require("fluent-ffmpeg"));
7
+ const ffmpeg_1 = __importDefault(require("@ffmpeg-installer/ffmpeg"));
8
+ const stream_1 = require("stream");
9
+ const StickerTypes_js_1 = require("../types/StickerTypes.js");
46
10
  fluent_ffmpeg_1.default.setFfmpegPath(ffmpeg_1.default.path);
47
- var videoToGif = function (buffer, extType, type) {
48
- return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
49
- var outputStream, inputStream, chunks_1, shape;
50
- return __generator(this, function (_a) {
51
- try {
52
- outputStream = new stream_1.PassThrough();
53
- inputStream = new stream_1.PassThrough();
54
- inputStream.end(buffer);
55
- chunks_1 = [];
56
- outputStream.on('data', function (chunk) {
57
- chunks_1.push(chunk);
58
- });
59
- outputStream.on('end', function () { return __awaiter(void 0, void 0, void 0, function () {
60
- return __generator(this, function (_a) {
61
- resolve(Buffer.concat(chunks_1));
62
- return [2 /*return*/];
63
- });
64
- }); });
65
- outputStream.on('error', function (err) { return __awaiter(void 0, void 0, void 0, function () {
66
- return __generator(this, function (_a) {
67
- switch (_a.label) {
68
- case 0: return [4 /*yield*/, videoToGif(buffer, extType, type)];
69
- case 1:
70
- _a.sent();
71
- reject(err);
72
- return [2 /*return*/];
73
- }
74
- });
75
- }); });
76
- shape = type === StickerTypes_js_1.StickerTypes.SQUARE
77
- ? 'scale=320:-1:flags=lanczos,fps=10,crop=min(iw\\,ih):min(iw\\,ih)'
78
- : 'scale=320:-1:flags=lanczos,fps=10';
79
- (0, fluent_ffmpeg_1.default)(inputStream)
80
- .inputFormat(extType)
81
- .outputOptions(['-vf', shape, '-loop', '0', '-lossless', '0', '-t', '6', '-preset', 'ultrafast'])
82
- .toFormat('gif')
83
- .pipe(outputStream, { end: true });
84
- }
85
- catch (error) {
86
- reject(error);
87
- }
88
- return [2 /*return*/];
89
- });
90
- }); });
11
+ const videoToGif = (buffer, extType, type) => {
12
+ return new Promise(async (resolve, reject) => {
13
+ try {
14
+ const outputStream = new stream_1.PassThrough({ allowHalfOpen: false });
15
+ const inputStream = new stream_1.PassThrough({ allowHalfOpen: false });
16
+ inputStream.write(buffer);
17
+ inputStream.end();
18
+ const chunks = [];
19
+ outputStream.on('data', (chunk) => {
20
+ chunks.push(chunk);
21
+ });
22
+ outputStream.on('end', () => {
23
+ resolve(Buffer.concat(chunks));
24
+ inputStream.destroy();
25
+ outputStream.destroy();
26
+ });
27
+ outputStream.on('error', (err) => {
28
+ reject(err);
29
+ inputStream.destroy();
30
+ outputStream.destroy();
31
+ });
32
+ const shape = type === StickerTypes_js_1.StickerTypes.SQUARE
33
+ ? 'scale=320:-1:flags=lanczos,fps=10,crop=min(iw\\,ih):min(iw\\,ih)'
34
+ : 'scale=320:-1:flags=lanczos,fps=10';
35
+ (0, fluent_ffmpeg_1.default)(inputStream)
36
+ .inputFormat(extType)
37
+ .outputOptions(['-vf', shape, '-loop', '0', '-lossless', '0', '-t', '7', '-preset', 'ultrafast'])
38
+ .toFormat('gif')
39
+ .pipe(outputStream)
40
+ .on('error', (err) => {
41
+ inputStream.destroy();
42
+ outputStream.destroy();
43
+ reject(err);
44
+ });
45
+ }
46
+ catch (error) {
47
+ reject(error);
48
+ }
49
+ });
91
50
  };
92
51
  exports.default = videoToGif;
@@ -1,5 +1,3 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
1
  import { Readable } from 'stream';
4
2
  export default class Utils {
5
3
  constructor();
package/dist/lib/utils.js CHANGED
@@ -22,176 +22,90 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __generator = (this && this.__generator) || function (thisArg, body) {
35
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
- function verb(n) { return function (v) { return step([n, v]); }; }
38
- function step(op) {
39
- if (f) throw new TypeError("Generator is already executing.");
40
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
41
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
42
- if (y = 0, t) op = [op[0] & 2, t.value];
43
- switch (op[0]) {
44
- case 0: case 1: t = op; break;
45
- case 4: _.label++; return { value: op[1], done: false };
46
- case 5: _.label++; y = op[1]; op = [0]; continue;
47
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
- default:
49
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
- if (t[2]) _.ops.pop();
54
- _.trys.pop(); continue;
55
- }
56
- op = body.call(thisArg, _);
57
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
- }
60
- };
61
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
62
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
63
- if (ar || !(i in from)) {
64
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
65
- ar[i] = from[i];
66
- }
67
- }
68
- return to.concat(ar || Array.prototype.slice.call(from));
69
- };
70
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
71
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
72
27
  };
73
28
  Object.defineProperty(exports, "__esModule", { value: true });
74
- var stream_1 = require("stream");
75
- var fs_1 = __importDefault(require("fs"));
76
- var Utils = /** @class */ (function () {
77
- function Utils() {
78
- }
29
+ const stream_1 = require("stream");
30
+ const fs_1 = __importDefault(require("fs"));
31
+ class Utils {
32
+ constructor() { }
79
33
  /**
80
34
  * Converts various types of input data to a Buffer.
81
35
  * @param data Input data as Buffer, string (file path), or Readable stream.
82
36
  * @returns Promise<Buffer> A Promise resolving to the converted Buffer.
83
37
  * @throws Error if conversion fails.
84
38
  */
85
- Utils.prototype.buffer = function (data) {
86
- return __awaiter(this, void 0, void 0, function () {
87
- var buffer, _a, _b, error_1;
88
- return __generator(this, function (_c) {
89
- switch (_c.label) {
90
- case 0:
91
- _c.trys.push([0, 7, , 8]);
92
- if (!(typeof data === 'string')) return [3 /*break*/, 2];
93
- return [4 /*yield*/, fs_1.default.promises.readFile(data)];
94
- case 1:
95
- _a = _c.sent();
96
- return [3 /*break*/, 6];
97
- case 2:
98
- if (!(data instanceof stream_1.Readable)) return [3 /*break*/, 4];
99
- return [4 /*yield*/, this.streamToBuffer(data)];
100
- case 3:
101
- _b = _c.sent();
102
- return [3 /*break*/, 5];
103
- case 4:
104
- _b = Buffer.from(data);
105
- _c.label = 5;
106
- case 5:
107
- _a = _b;
108
- _c.label = 6;
109
- case 6:
110
- buffer = _a;
111
- return [2 /*return*/, buffer];
112
- case 7:
113
- error_1 = _c.sent();
114
- throw new Error("Error converting to buffer: ".concat(error_1));
115
- case 8: return [2 /*return*/];
116
- }
117
- });
118
- });
119
- };
39
+ async buffer(data) {
40
+ try {
41
+ const buffer = typeof data === 'string'
42
+ ? await fs_1.default.promises.readFile(data)
43
+ : data instanceof stream_1.Readable
44
+ ? await this.streamToBuffer(data)
45
+ : Buffer.from(data);
46
+ return buffer;
47
+ }
48
+ catch (error) {
49
+ throw new Error(`Error converting to buffer: ${error}`);
50
+ }
51
+ }
120
52
  /**
121
53
  * Converts a Readable stream to a Buffer.
122
54
  * @param stream Readable stream to convert.
123
55
  * @returns Promise<Buffer> A Promise resolving to the converted Buffer.
124
56
  */
125
- Utils.prototype.streamToBuffer = function (stream) {
126
- return __awaiter(this, void 0, void 0, function () {
127
- var chunks;
128
- return __generator(this, function (_a) {
129
- chunks = [];
130
- return [2 /*return*/, new Promise(function (resolve, reject) {
131
- stream.on('data', function (chunk) {
132
- if (Buffer.isBuffer(chunk)) {
133
- chunks.push(chunk);
134
- }
135
- else {
136
- chunks.push(Buffer.from(chunk));
137
- }
138
- });
139
- stream.on('end', function () {
140
- resolve(Buffer.concat(chunks));
141
- });
142
- stream.on('error', function (err) {
143
- reject(err);
144
- });
145
- })];
57
+ async streamToBuffer(stream) {
58
+ const chunks = [];
59
+ return new Promise((resolve, reject) => {
60
+ stream.on('data', (chunk) => {
61
+ if (Buffer.isBuffer(chunk)) {
62
+ chunks.push(chunk);
63
+ }
64
+ else {
65
+ chunks.push(Buffer.from(chunk));
66
+ }
67
+ });
68
+ stream.on('end', () => {
69
+ resolve(Buffer.concat(chunks));
70
+ });
71
+ stream.on('error', (err) => {
72
+ reject(err);
146
73
  });
147
74
  });
148
- };
75
+ }
149
76
  /**
150
77
  * Determines the quality of the data based on its size.
151
78
  * @param data Buffer containing the data.
152
79
  * @returns number Quality value based on data size.
153
80
  */
154
- Utils.prototype.getQuality = function (data) {
155
- var buffer = Buffer.from(data);
156
- var bytes = buffer.length / 1024;
157
- var quality = bytes > 4 * 1024 ? 8 : bytes > 3 * 1024 ? 10 : bytes > 2 * 1024 ? 12 : 15;
81
+ getQuality(data) {
82
+ let buffer = Buffer.from(data);
83
+ let bytes = buffer.length / 1024;
84
+ let quality = bytes > 4 * 1024 ? 8 : bytes > 3 * 1024 ? 10 : bytes > 2 * 1024 ? 12 : 15;
158
85
  return quality;
159
- };
86
+ }
160
87
  /**
161
88
  * Determines the MIME type of the data buffer.
162
89
  * @param data Buffer containing the data.
163
90
  * @returns Promise<{ mime: string; ext: string }> A Promise resolving to an object with MIME type and extension.
164
91
  */
165
- Utils.prototype.getMimeType = function (data) {
166
- return __awaiter(this, void 0, void 0, function () {
167
- var fromBuffer, fileType, error_2;
168
- return __generator(this, function (_a) {
169
- switch (_a.label) {
170
- case 0:
171
- _a.trys.push([0, 3, , 4]);
172
- return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require('file-type')); })];
173
- case 1:
174
- fromBuffer = (_a.sent()).fromBuffer;
175
- return [4 /*yield*/, fromBuffer(data)];
176
- case 2:
177
- fileType = _a.sent();
178
- return [2 /*return*/, fileType];
179
- case 3:
180
- error_2 = _a.sent();
181
- console.error("Error getting MIME type: ".concat(error_2));
182
- return [2 /*return*/, undefined];
183
- case 4: return [2 /*return*/];
184
- }
185
- });
186
- });
187
- };
92
+ async getMimeType(data) {
93
+ try {
94
+ const { fileTypeFromBuffer } = await Promise.resolve().then(() => __importStar(require('file-type')));
95
+ const fileType = await fileTypeFromBuffer(data);
96
+ return fileType;
97
+ }
98
+ catch (error) {
99
+ console.error(`Error getting MIME type: ${error}`);
100
+ return undefined;
101
+ }
102
+ }
188
103
  /**
189
104
  * Generates a random ID.
190
105
  * @returns string A random alphanumeric ID.
191
106
  */
192
- Utils.prototype.getId = function () {
193
- return __spreadArray([], Array(5), true).map(function () { return Math.random().toString(36).substring(2, 15); }).join('');
194
- };
195
- return Utils;
196
- }());
107
+ getId() {
108
+ return [...Array(5)].map(() => Math.random().toString(36).substring(2, 15)).join('');
109
+ }
110
+ }
197
111
  exports.default = Utils;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@shibam/sticker-maker",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "A package for creating stickers",
5
5
  "main": "dist/index.js",
6
- "type": "commonjs",
6
+ "type": "module",
7
7
  "files": [
8
8
  "dist"
9
9
  ],
@@ -35,7 +35,7 @@
35
35
  "dependencies": {
36
36
  "@ffmpeg-installer/ffmpeg": "^1.1.0",
37
37
  "@types/fluent-ffmpeg": "^2.1.24",
38
- "file-type": "16.5.0",
38
+ "file-type": "^19.4.0",
39
39
  "fluent-ffmpeg": "^2.1.3",
40
40
  "node-webpmux": "^3.2.0",
41
41
  "sharp": "^0.33.4"