@shibam/sticker-maker 1.1.4 → 1.1.6
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 +5 -3
- package/dist/index.js +75 -183
- package/dist/lib/ToWebp.d.ts +0 -1
- package/dist/lib/ToWebp.js +45 -94
- package/dist/lib/changeMetaInfo.d.ts +0 -2
- package/dist/lib/changeMetaInfo.js +23 -84
- package/dist/lib/extractMetaData.d.ts +0 -1
- package/dist/lib/extractMetaData.js +7 -52
- package/dist/lib/toGif.d.ts +0 -1
- package/dist/lib/toGif.js +43 -84
- package/dist/lib/utils.d.ts +0 -2
- package/dist/lib/utils.js +54 -140
- package/package.json +3 -3
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';
|
|
@@ -45,4 +43,8 @@ declare class Sticker {
|
|
|
45
43
|
*/
|
|
46
44
|
extractMetaData(data: Buffer): Promise<Partial<MetaDataType>>;
|
|
47
45
|
}
|
|
48
|
-
|
|
46
|
+
declare const _default: {
|
|
47
|
+
Sticker: typeof Sticker;
|
|
48
|
+
StickerTypes: typeof StickerTypes;
|
|
49
|
+
};
|
|
50
|
+
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -1,55 +1,16 @@
|
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
var Sticker = /** @class */ (function () {
|
|
51
|
-
function Sticker(data, metaInfo) {
|
|
52
|
-
if (metaInfo === void 0) { metaInfo = {}; }
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const utils_js_1 = __importDefault(require("./lib/utils.js"));
|
|
8
|
+
const ToWebp_js_1 = __importDefault(require("./lib/ToWebp.js"));
|
|
9
|
+
const StickerTypes_js_1 = require("./types/StickerTypes.js");
|
|
10
|
+
const changeMetaInfo_js_1 = __importDefault(require("./lib/changeMetaInfo.js"));
|
|
11
|
+
const extractMetaData_js_1 = __importDefault(require("./lib/extractMetaData.js"));
|
|
12
|
+
class Sticker {
|
|
13
|
+
constructor(data, metaInfo = {}) {
|
|
53
14
|
this.data = data;
|
|
54
15
|
this.metaInfo = metaInfo;
|
|
55
16
|
this.utils = new utils_js_1.default();
|
|
@@ -66,158 +27,89 @@ var Sticker = /** @class */ (function () {
|
|
|
66
27
|
* - Reads and analyzes input data to determine MIME type and extension type.
|
|
67
28
|
* - Generates ID and quality metadata if not provided.
|
|
68
29
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
};
|
|
30
|
+
async initialize() {
|
|
31
|
+
try {
|
|
32
|
+
this.buffer = await this.utils.buffer(this.data);
|
|
33
|
+
const fileType = await this.utils.getMimeType(this.buffer);
|
|
34
|
+
this.mimeType = fileType?.mime ?? '';
|
|
35
|
+
this.extType = fileType?.ext ?? '';
|
|
36
|
+
this.metaInfo.pack = this.metaInfo.pack ?? '';
|
|
37
|
+
this.metaInfo.author = this.metaInfo.author ?? '';
|
|
38
|
+
this.metaInfo.id = this.metaInfo.id ?? this.utils.getId();
|
|
39
|
+
this.metaInfo.category = this.metaInfo.category ?? [];
|
|
40
|
+
this.metaInfo.type = this.metaInfo.type ?? StickerTypes_js_1.StickerTypes.DEFAULT;
|
|
41
|
+
this.metaInfo.quality = this.metaInfo?.quality ?? this.utils.getQuality(this.buffer);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
throw new Error(`Initialization error: ${error}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
101
47
|
/**
|
|
102
48
|
* Converts input data to a Buffer containing the converted content.
|
|
103
49
|
* @returns Promise<Buffer> A Promise resolving to the converted content as Buffer.
|
|
104
50
|
*/
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
};
|
|
51
|
+
async toBuffer() {
|
|
52
|
+
try {
|
|
53
|
+
await this.initialize();
|
|
54
|
+
const buffer = await (0, ToWebp_js_1.default)(this.buffer, this.metaInfo, this.extType, this.mimeType);
|
|
55
|
+
this.outBuffer = await new changeMetaInfo_js_1.default(this.metaInfo).add(buffer);
|
|
56
|
+
this.activeBuff = true;
|
|
57
|
+
return this.outBuffer;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
this.activeBuff = false;
|
|
61
|
+
throw new Error(`Conversion to buffer failed: ${error}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
133
64
|
/**
|
|
134
65
|
* Converts input data and writes it to a file at the specified outputPath.
|
|
135
66
|
* @param outputPath The path where the converted file will be saved.
|
|
136
67
|
* @returns Promise<void> A Promise resolving when the file is successfully written.
|
|
137
68
|
*/
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
};
|
|
69
|
+
async toFile(outputPath) {
|
|
70
|
+
try {
|
|
71
|
+
if (!this.activeBuff && !this.activeMeta && this.extType?.includes('webp')) {
|
|
72
|
+
await this.changeMetaInfo();
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
await this.toBuffer();
|
|
76
|
+
}
|
|
77
|
+
await fs_1.default.promises.writeFile(outputPath, this.outBuffer);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
throw new Error(`Conversion to file failed: ${error}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
167
83
|
/**
|
|
168
84
|
* Changes the metadata of the sticker.
|
|
169
85
|
* @param newMetaInfo Partial metadata to update.
|
|
170
86
|
* @returns Promise<Buffer> A Promise resolving to the Buffer with updated metadata.
|
|
171
87
|
*/
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
-
};
|
|
88
|
+
async changeMetaInfo() {
|
|
89
|
+
try {
|
|
90
|
+
await this.initialize();
|
|
91
|
+
this.outBuffer = await new changeMetaInfo_js_1.default(this.metaInfo).add(this.buffer);
|
|
92
|
+
this.activeMeta = true;
|
|
93
|
+
return this.outBuffer;
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
this.activeMeta = false;
|
|
97
|
+
throw new Error(`Error changing meta info: ${error}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
197
100
|
/**
|
|
198
101
|
* Extracts metadata from the provided data.
|
|
199
102
|
* @param data Buffer containing the data to extract metadata from.
|
|
200
103
|
* @returns Promise<Partial<MetaDataType>> A Promise resolving to the extracted metadata.
|
|
201
104
|
*/
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
return
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
}());
|
|
223
|
-
exports.Sticker = Sticker;
|
|
105
|
+
async extractMetaData(data) {
|
|
106
|
+
try {
|
|
107
|
+
await this.initialize();
|
|
108
|
+
return (0, extractMetaData_js_1.default)(data);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
throw new Error(`Error extracting meta data: ${error}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.default = { Sticker, StickerTypes: StickerTypes_js_1.StickerTypes };
|
package/dist/lib/ToWebp.d.ts
CHANGED
package/dist/lib/ToWebp.js
CHANGED
|
@@ -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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
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,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
|
-
|
|
43
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 =
|
|
62
|
-
|
|
63
|
-
|
|
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 =
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
122
|
-
}());
|
|
61
|
+
}
|
|
123
62
|
exports.default = Exif;
|
|
@@ -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
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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;
|
package/dist/lib/toGif.d.ts
CHANGED
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
48
|
-
return new Promise(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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;
|
package/dist/lib/utils.d.ts
CHANGED
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
193
|
-
return
|
|
194
|
-
}
|
|
195
|
-
|
|
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.
|
|
3
|
+
"version": "1.1.6",
|
|
4
4
|
"description": "A package for creating stickers",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
-
"type": "
|
|
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": "
|
|
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"
|