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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aptechka",
3
- "version": "0.11.3",
3
+ "version": "0.11.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/denisavitski/aptechka.git"
@@ -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 (settings == null ? void 0 : settings.scale) {
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: settings == null ? void 0 : settings.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: settings == null ? void 0 : settings.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: settings.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 - ((settings == null ? void 0 : settings.quality) || 80)) * 51 / 100)}`
355
+ `-crf ${Math.round((100 - quality) * 51 / 100)}`
332
356
  );
333
- if (settings == null ? void 0 : settings.fps) {
334
- command.fps(settings.fps);
357
+ if (fps) {
358
+ command.fps(fps);
335
359
  }
336
- command.size(`${((settings == null ? void 0 : settings.scale) || 1) * 100}%`);
360
+ command.size(`${scale * 100}%`);
337
361
  }
338
362
  });
339
363
  }
@@ -0,0 +1,4 @@
1
+ import { FaviconSource } from './types';
2
+ import { Output } from '../utils';
3
+
4
+ export declare function generateFavicon(source: Omit<FaviconSource, 'type'>): Promise<Output>;
@@ -0,0 +1,3 @@
1
+ import { SequenceSource } from './types';
2
+
3
+ export declare function generateSequence(source: Omit<SequenceSource, 'type'>): Promise<import('../utils').Output>;
@@ -0,0 +1,4 @@
1
+ import { SpriteSource } from './types';
2
+ import { Output } from '../utils';
3
+
4
+ export declare function generateSprite(source: Omit<SpriteSource, 'type'>): Promise<Output>;
@@ -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 SourceSettings, type Source, type SkipSource, type ImageSource, type VideoSource, type FaviconSource, type SpriteSource, type SequenceSource, type KnownSource, ALLOWED_IMAGE_EXTENSIONS, ALLOWED_VIDEO_EXTENSIONS, } from './types';
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';
@@ -0,0 +1,4 @@
1
+ import { ImageSource } from './types';
2
+ import { Output } from '../utils';
3
+
4
+ export declare function optimizeImage(source: Omit<ImageSource, 'type'>): Promise<Output>;
@@ -0,0 +1,3 @@
1
+ import { VideoSource } from './types';
2
+
3
+ export declare function optimizeVideo(source: Omit<VideoSource, 'type'>): Promise<import('../utils').Output>;
@@ -1,13 +1,20 @@
1
1
  import { FaviconOptions } from 'favicons';
2
2
 
3
- export type SourceSettings = {
3
+ export type SourceDefaultSettings = {
4
4
  [key: string]: any;
5
5
  };
6
- export interface Source<B = Buffer | File, T extends string = string, S extends SourceSettings = {}> {
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;
@@ -0,0 +1,4 @@
1
+ export declare function getNumberSetting<T extends number | undefined>(value: T, restricstions?: {
2
+ min: number;
3
+ max: number;
4
+ }): number | T;
@@ -1,4 +0,0 @@
1
- import { FaviconSource } from './types';
2
- import { Output } from '../utils';
3
-
4
- export declare function generateFavicon(source: FaviconSource): Promise<Output>;
@@ -1,3 +0,0 @@
1
- import { SequenceSource } from './types';
2
-
3
- export declare function generateSequence(source: SequenceSource): Promise<import('../utils').Output>;
@@ -1,4 +0,0 @@
1
- import { SpriteSource } from './types';
2
- import { Output } from '../utils';
3
-
4
- export declare function generateSprite(source: SpriteSource): Promise<Output>;
@@ -1,4 +0,0 @@
1
- import { ImageSource } from './types';
2
- import { Output } from '../utils';
3
-
4
- export declare function optimizeImage(source: ImageSource): Promise<Output>;
@@ -1,3 +0,0 @@
1
- import { VideoSource } from './types';
2
-
3
- export declare function optimizeVideo(source: VideoSource): Promise<import('../utils').Output>;
File without changes
File without changes
File without changes
File without changes
File without changes