aptechka 0.11.3 → 0.11.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/package.json +1 -1
- package/server-lib/optimizer/generateFavicon.d.ts +1 -1
- package/server-lib/optimizer/generateSequence.d.ts +1 -1
- package/server-lib/optimizer/generateSprite.d.ts +1 -1
- package/server-lib/optimizer/index.d.ts +1 -1
- package/server-lib/optimizer/index.js +22 -13
- package/server-lib/optimizer/optimizeImage.d.ts +1 -1
- package/server-lib/optimizer/optimizeVideo.d.ts +1 -1
- package/server-lib/optimizer/types.d.ts +6 -3
- package/server-lib/optimizer/utils.d.ts +1 -0
- package/server-lib/utils/index.d.ts +1 -0
- package/server-lib/utils/index.js +4 -0
- package/server-lib/utils/math.d.ts +1 -0
package/package.json
CHANGED
|
@@ -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,7 +1,7 @@
|
|
|
1
1
|
import { mkdir, readFile, readdir } from "fs/promises";
|
|
2
2
|
import { dirname, join, sep, extname } from "path";
|
|
3
3
|
import Ffmpeg from "fluent-ffmpeg";
|
|
4
|
-
import { getTmpPath, outputFile, getFolderFiles, removeExtension, clear, getBuffer, replaceExtension, outputFiles } from "../utils/index.js";
|
|
4
|
+
import { getTmpPath, outputFile, getFolderFiles, removeExtension, clear, getBuffer, clamp, replaceExtension, outputFiles } from "../utils/index.js";
|
|
5
5
|
import favicons from "favicons";
|
|
6
6
|
import { parse } from "node-html-parser";
|
|
7
7
|
import { statSync } from "fs";
|
|
@@ -247,6 +247,7 @@ async function inputFiles({
|
|
|
247
247
|
settings: {
|
|
248
248
|
quality: 80,
|
|
249
249
|
scale: 1,
|
|
250
|
+
fps: 0,
|
|
250
251
|
destinationPath,
|
|
251
252
|
...(_e = settings == null ? void 0 : settings.video) == null ? void 0 : _e.call(settings, { destinationPath })
|
|
252
253
|
}
|
|
@@ -263,6 +264,12 @@ async function inputFiles({
|
|
|
263
264
|
}
|
|
264
265
|
return sources;
|
|
265
266
|
}
|
|
267
|
+
function getNumberSetting(value, min, max) {
|
|
268
|
+
if (typeof value === "number") {
|
|
269
|
+
return clamp(value, min, max);
|
|
270
|
+
}
|
|
271
|
+
return value;
|
|
272
|
+
}
|
|
266
273
|
async function optimizeImage(source) {
|
|
267
274
|
const { settings } = source;
|
|
268
275
|
const content = await getBuffer(source.content);
|
|
@@ -272,24 +279,23 @@ async function optimizeImage(source) {
|
|
|
272
279
|
const width = meta.width;
|
|
273
280
|
const height = meta.height;
|
|
274
281
|
const output = [];
|
|
282
|
+
const scale = getNumberSetting(settings.scale, 0, 1);
|
|
283
|
+
const quality = getNumberSetting(settings.quality, 0, 100);
|
|
275
284
|
if (width && height) {
|
|
276
|
-
if (
|
|
277
|
-
image.resize(
|
|
278
|
-
Math.floor(width * settings.scale),
|
|
279
|
-
Math.floor(height * settings.scale)
|
|
280
|
-
);
|
|
285
|
+
if (scale) {
|
|
286
|
+
image.resize(Math.floor(width * scale), Math.floor(height * scale));
|
|
281
287
|
}
|
|
282
288
|
}
|
|
283
289
|
if (ext === ".jpg" || ext === ".jpeg") {
|
|
284
290
|
image.jpeg({
|
|
285
291
|
mozjpeg: true,
|
|
286
|
-
quality
|
|
292
|
+
quality
|
|
287
293
|
});
|
|
288
294
|
} else if (ext === ".png") {
|
|
289
295
|
image.png({
|
|
290
296
|
compressionLevel: 9,
|
|
291
297
|
adaptiveFiltering: true,
|
|
292
|
-
quality
|
|
298
|
+
quality,
|
|
293
299
|
effort: 8
|
|
294
300
|
});
|
|
295
301
|
}
|
|
@@ -300,7 +306,7 @@ async function optimizeImage(source) {
|
|
|
300
306
|
});
|
|
301
307
|
if (settings == null ? void 0 : settings.webp) {
|
|
302
308
|
const buffer2 = await image.webp({
|
|
303
|
-
quality
|
|
309
|
+
quality,
|
|
304
310
|
effort: 5
|
|
305
311
|
}).toBuffer();
|
|
306
312
|
output.push({
|
|
@@ -323,17 +329,20 @@ async function optimizeImage(source) {
|
|
|
323
329
|
async function optimizeVideo(source) {
|
|
324
330
|
const { settings } = source;
|
|
325
331
|
const buffer = await getBuffer(source.content);
|
|
332
|
+
const quality = getNumberSetting((settings == null ? void 0 : settings.quality) || 80, 0, 100);
|
|
333
|
+
const fps = getNumberSetting((settings == null ? void 0 : settings.fps) || 0, 0, 300);
|
|
334
|
+
const scale = getNumberSetting((settings == null ? void 0 : settings.scale) || 1, 0, 1);
|
|
326
335
|
return FFmpeg({
|
|
327
336
|
inputPath: settings.destinationPath,
|
|
328
337
|
fileContent: buffer,
|
|
329
338
|
instructions: (command) => {
|
|
330
339
|
command.addOutputOption(
|
|
331
|
-
`-crf ${Math.round((100 -
|
|
340
|
+
`-crf ${Math.round((100 - quality) * 51 / 100)}`
|
|
332
341
|
);
|
|
333
|
-
if (
|
|
334
|
-
command.fps(
|
|
342
|
+
if (fps) {
|
|
343
|
+
command.fps(fps);
|
|
335
344
|
}
|
|
336
|
-
command.size(`${
|
|
345
|
+
command.size(`${scale * 100}%`);
|
|
337
346
|
}
|
|
338
347
|
});
|
|
339
348
|
}
|
|
@@ -1,13 +1,16 @@
|
|
|
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 = {}> = {
|
|
7
10
|
content: B;
|
|
8
11
|
type: T;
|
|
9
12
|
settings: S;
|
|
10
|
-
}
|
|
13
|
+
};
|
|
11
14
|
export type SkipSource = Source<Buffer | File, 'skip', {
|
|
12
15
|
destinationPath: string;
|
|
13
16
|
}>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getNumberSetting<T extends number | undefined>(value: T, min: number, max: number): number | T;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { getBuffer } from './buffer';
|
|
2
2
|
export { removeExtension, replaceExtension, getTmpPath } from './path';
|
|
3
3
|
export { outputFile, outputFiles, clear, getFolderFiles } from './fs';
|
|
4
|
+
export { clamp } from './math';
|
|
4
5
|
export type { WriteFileData, OutputItem, Output } from './types';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function clamp(number: number, min?: number, max?: number): number;
|