aptechka 0.11.3 → 0.11.4
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/package.json +1 -1
- package/server-lib/optimizer/index.js +38 -14
- package/server-lib/server/optimizer/generateFavicon.d.ts +4 -0
- package/server-lib/server/optimizer/generateSequence.d.ts +3 -0
- package/server-lib/server/optimizer/generateSprite.d.ts +4 -0
- package/server-lib/{optimizer → server/optimizer}/index.d.ts +1 -1
- package/server-lib/server/optimizer/optimizeImage.d.ts +4 -0
- package/server-lib/server/optimizer/optimizeVideo.d.ts +3 -0
- package/server-lib/{optimizer → server/optimizer}/types.d.ts +32 -3
- package/server-lib/server/optimizer/utils.d.ts +4 -0
- package/server-lib/optimizer/generateFavicon.d.ts +0 -4
- package/server-lib/optimizer/generateSequence.d.ts +0 -3
- package/server-lib/optimizer/generateSprite.d.ts +0 -4
- package/server-lib/optimizer/optimizeImage.d.ts +0 -4
- package/server-lib/optimizer/optimizeVideo.d.ts +0 -3
- /package/server-lib/{optimizer → server/optimizer}/FFmpeg.d.ts +0 -0
- /package/server-lib/{optimizer → server/optimizer}/inputFiles.d.ts +0 -0
- /package/server-lib/{optimizer → server/optimizer}/optimize.d.ts +0 -0
- /package/server-lib/{optimizer → server/optimizer}/optimizer.d.ts +0 -0
- /package/server-lib/{utils → server/utils}/buffer.d.ts +0 -0
- /package/server-lib/{utils → server/utils}/fs.d.ts +0 -0
- /package/server-lib/{utils → server/utils}/index.d.ts +0 -0
- /package/server-lib/{utils → server/utils}/path.d.ts +0 -0
- /package/server-lib/{utils → server/utils}/types.d.ts +0 -0
package/package.json
CHANGED
|
@@ -236,6 +236,10 @@ async function inputFiles({
|
|
|
236
236
|
webp: false,
|
|
237
237
|
destinationPath,
|
|
238
238
|
...(_d = settings == null ? void 0 : settings.image) == null ? void 0 : _d.call(settings, { destinationPath })
|
|
239
|
+
},
|
|
240
|
+
restrictions: {
|
|
241
|
+
quality: { min: 0, max: 100 },
|
|
242
|
+
scale: { min: 0, max: 1 }
|
|
239
243
|
}
|
|
240
244
|
});
|
|
241
245
|
} else if (ALLOWED_VIDEO_EXTENSIONS.includes(
|
|
@@ -247,8 +251,14 @@ async function inputFiles({
|
|
|
247
251
|
settings: {
|
|
248
252
|
quality: 80,
|
|
249
253
|
scale: 1,
|
|
254
|
+
fps: 0,
|
|
250
255
|
destinationPath,
|
|
251
256
|
...(_e = settings == null ? void 0 : settings.video) == null ? void 0 : _e.call(settings, { destinationPath })
|
|
257
|
+
},
|
|
258
|
+
restrictions: {
|
|
259
|
+
quality: { min: 0, max: 100 },
|
|
260
|
+
scale: { min: 0, max: 1 },
|
|
261
|
+
fps: { min: 0, max: 300 }
|
|
252
262
|
}
|
|
253
263
|
});
|
|
254
264
|
} else {
|
|
@@ -263,8 +273,17 @@ async function inputFiles({
|
|
|
263
273
|
}
|
|
264
274
|
return sources;
|
|
265
275
|
}
|
|
276
|
+
function clamp(number, min = 0, max = 0) {
|
|
277
|
+
return Math.max(min, Math.min(number, max));
|
|
278
|
+
}
|
|
279
|
+
function getNumberSetting(value, restricstions) {
|
|
280
|
+
if (typeof value === "number" && restricstions) {
|
|
281
|
+
return clamp(value, restricstions.min, restricstions.max);
|
|
282
|
+
}
|
|
283
|
+
return value;
|
|
284
|
+
}
|
|
266
285
|
async function optimizeImage(source) {
|
|
267
|
-
const { settings } = source;
|
|
286
|
+
const { settings, restrictions } = source;
|
|
268
287
|
const content = await getBuffer(source.content);
|
|
269
288
|
const ext = extname(settings.destinationPath).toLowerCase();
|
|
270
289
|
const image = sharp(content);
|
|
@@ -272,24 +291,23 @@ async function optimizeImage(source) {
|
|
|
272
291
|
const width = meta.width;
|
|
273
292
|
const height = meta.height;
|
|
274
293
|
const output = [];
|
|
294
|
+
const scale = getNumberSetting(settings.scale, restrictions == null ? void 0 : restrictions.scale);
|
|
295
|
+
const quality = getNumberSetting(settings.quality, restrictions == null ? void 0 : restrictions.quality);
|
|
275
296
|
if (width && height) {
|
|
276
|
-
if (
|
|
277
|
-
image.resize(
|
|
278
|
-
Math.floor(width * settings.scale),
|
|
279
|
-
Math.floor(height * settings.scale)
|
|
280
|
-
);
|
|
297
|
+
if (scale) {
|
|
298
|
+
image.resize(Math.floor(width * scale), Math.floor(height * scale));
|
|
281
299
|
}
|
|
282
300
|
}
|
|
283
301
|
if (ext === ".jpg" || ext === ".jpeg") {
|
|
284
302
|
image.jpeg({
|
|
285
303
|
mozjpeg: true,
|
|
286
|
-
quality
|
|
304
|
+
quality
|
|
287
305
|
});
|
|
288
306
|
} else if (ext === ".png") {
|
|
289
307
|
image.png({
|
|
290
308
|
compressionLevel: 9,
|
|
291
309
|
adaptiveFiltering: true,
|
|
292
|
-
quality
|
|
310
|
+
quality,
|
|
293
311
|
effort: 8
|
|
294
312
|
});
|
|
295
313
|
}
|
|
@@ -300,7 +318,7 @@ async function optimizeImage(source) {
|
|
|
300
318
|
});
|
|
301
319
|
if (settings == null ? void 0 : settings.webp) {
|
|
302
320
|
const buffer2 = await image.webp({
|
|
303
|
-
quality
|
|
321
|
+
quality,
|
|
304
322
|
effort: 5
|
|
305
323
|
}).toBuffer();
|
|
306
324
|
output.push({
|
|
@@ -321,19 +339,25 @@ async function optimizeImage(source) {
|
|
|
321
339
|
return output;
|
|
322
340
|
}
|
|
323
341
|
async function optimizeVideo(source) {
|
|
324
|
-
const { settings } = source;
|
|
342
|
+
const { settings, restrictions } = source;
|
|
325
343
|
const buffer = await getBuffer(source.content);
|
|
344
|
+
const quality = getNumberSetting(
|
|
345
|
+
(settings == null ? void 0 : settings.quality) || 80,
|
|
346
|
+
restrictions == null ? void 0 : restrictions.quality
|
|
347
|
+
);
|
|
348
|
+
const fps = getNumberSetting((settings == null ? void 0 : settings.fps) || 0, restrictions == null ? void 0 : restrictions.fps);
|
|
349
|
+
const scale = getNumberSetting((settings == null ? void 0 : settings.scale) || 1, restrictions == null ? void 0 : restrictions.scale);
|
|
326
350
|
return FFmpeg({
|
|
327
351
|
inputPath: settings.destinationPath,
|
|
328
352
|
fileContent: buffer,
|
|
329
353
|
instructions: (command) => {
|
|
330
354
|
command.addOutputOption(
|
|
331
|
-
`-crf ${Math.round((100 -
|
|
355
|
+
`-crf ${Math.round((100 - quality) * 51 / 100)}`
|
|
332
356
|
);
|
|
333
|
-
if (
|
|
334
|
-
command.fps(
|
|
357
|
+
if (fps) {
|
|
358
|
+
command.fps(fps);
|
|
335
359
|
}
|
|
336
|
-
command.size(`${
|
|
360
|
+
command.size(`${scale * 100}%`);
|
|
337
361
|
}
|
|
338
362
|
});
|
|
339
363
|
}
|
|
@@ -7,4 +7,4 @@ export { optimize, type OptimizeOptions, type OptimizeProgressCallback, type Opt
|
|
|
7
7
|
export { optimizeImage } from './optimizeImage';
|
|
8
8
|
export { optimizer } from './optimizer';
|
|
9
9
|
export { optimizeVideo } from './optimizeVideo';
|
|
10
|
-
export { type
|
|
10
|
+
export { type SourceDefaultSettings, type Source, type SkipSource, type ImageSource, type VideoSource, type FaviconSource, type SpriteSource, type SequenceSource, type KnownSource, ALLOWED_IMAGE_EXTENSIONS, ALLOWED_VIDEO_EXTENSIONS, } from './types';
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { FaviconOptions } from 'favicons';
|
|
2
2
|
|
|
3
|
-
export type
|
|
3
|
+
export type SourceDefaultSettings = {
|
|
4
4
|
[key: string]: any;
|
|
5
5
|
};
|
|
6
|
-
export
|
|
6
|
+
export type SourceSetting = {
|
|
7
|
+
value: any;
|
|
8
|
+
};
|
|
9
|
+
export type Source<B = Buffer | File, T extends string = string, S extends SourceDefaultSettings = {}, R extends Partial<{
|
|
10
|
+
[K in keyof S]: any;
|
|
11
|
+
}> | undefined = undefined> = {
|
|
7
12
|
content: B;
|
|
8
13
|
type: T;
|
|
9
14
|
settings: S;
|
|
10
|
-
}
|
|
15
|
+
} & (R extends undefined ? {} : {
|
|
16
|
+
restrictions?: Partial<R>;
|
|
17
|
+
});
|
|
11
18
|
export type SkipSource = Source<Buffer | File, 'skip', {
|
|
12
19
|
destinationPath: string;
|
|
13
20
|
}>;
|
|
@@ -17,12 +24,34 @@ export type ImageSource = Source<Buffer | File, 'image', {
|
|
|
17
24
|
scale?: number;
|
|
18
25
|
placeholder?: boolean;
|
|
19
26
|
webp?: boolean;
|
|
27
|
+
}, {
|
|
28
|
+
quality: {
|
|
29
|
+
min: 0;
|
|
30
|
+
max: 100;
|
|
31
|
+
};
|
|
32
|
+
scale: {
|
|
33
|
+
min: 0;
|
|
34
|
+
max: 1;
|
|
35
|
+
};
|
|
20
36
|
}>;
|
|
21
37
|
export type VideoSource = Source<Buffer | File, 'video', {
|
|
22
38
|
destinationPath: string;
|
|
23
39
|
quality?: number;
|
|
24
40
|
scale?: number;
|
|
25
41
|
fps?: number;
|
|
42
|
+
}, {
|
|
43
|
+
quality: {
|
|
44
|
+
min: 0;
|
|
45
|
+
max: 100;
|
|
46
|
+
};
|
|
47
|
+
scale: {
|
|
48
|
+
min: 0;
|
|
49
|
+
max: 1;
|
|
50
|
+
};
|
|
51
|
+
fps: {
|
|
52
|
+
min: 0;
|
|
53
|
+
max: 300;
|
|
54
|
+
};
|
|
26
55
|
}>;
|
|
27
56
|
export type FaviconSource = Source<Buffer | File, 'favicon', {
|
|
28
57
|
destinationPath: string;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|