aptechka 0.11.4 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aptechka",
3
- "version": "0.11.4",
3
+ "version": "0.11.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/denisavitski/aptechka.git"
@@ -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";
@@ -236,10 +236,6 @@ 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 }
243
239
  }
244
240
  });
245
241
  } else if (ALLOWED_VIDEO_EXTENSIONS.includes(
@@ -254,11 +250,6 @@ async function inputFiles({
254
250
  fps: 0,
255
251
  destinationPath,
256
252
  ...(_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 }
262
253
  }
263
254
  });
264
255
  } else {
@@ -273,17 +264,14 @@ async function inputFiles({
273
264
  }
274
265
  return sources;
275
266
  }
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);
267
+ function getNumberSetting(value, min, max) {
268
+ if (typeof value === "number") {
269
+ return clamp(value, min, max);
282
270
  }
283
271
  return value;
284
272
  }
285
273
  async function optimizeImage(source) {
286
- const { settings, restrictions } = source;
274
+ const { settings } = source;
287
275
  const content = await getBuffer(source.content);
288
276
  const ext = extname(settings.destinationPath).toLowerCase();
289
277
  const image = sharp(content);
@@ -291,8 +279,8 @@ async function optimizeImage(source) {
291
279
  const width = meta.width;
292
280
  const height = meta.height;
293
281
  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);
282
+ const scale = getNumberSetting(settings.scale, 0, 1);
283
+ const quality = getNumberSetting(settings.quality, 0, 100);
296
284
  if (width && height) {
297
285
  if (scale) {
298
286
  image.resize(Math.floor(width * scale), Math.floor(height * scale));
@@ -339,14 +327,11 @@ async function optimizeImage(source) {
339
327
  return output;
340
328
  }
341
329
  async function optimizeVideo(source) {
342
- const { settings, restrictions } = source;
330
+ const { settings } = source;
343
331
  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);
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);
350
335
  return FFmpeg({
351
336
  inputPath: settings.destinationPath,
352
337
  fileContent: buffer,
@@ -6,15 +6,11 @@ export type SourceDefaultSettings = {
6
6
  export type SourceSetting = {
7
7
  value: any;
8
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> = {
9
+ export type Source<B = Buffer | File, T extends string = string, S extends SourceDefaultSettings = {}> = {
12
10
  content: B;
13
11
  type: T;
14
12
  settings: S;
15
- } & (R extends undefined ? {} : {
16
- restrictions?: Partial<R>;
17
- });
13
+ };
18
14
  export type SkipSource = Source<Buffer | File, 'skip', {
19
15
  destinationPath: string;
20
16
  }>;
@@ -24,34 +20,12 @@ export type ImageSource = Source<Buffer | File, 'image', {
24
20
  scale?: number;
25
21
  placeholder?: boolean;
26
22
  webp?: boolean;
27
- }, {
28
- quality: {
29
- min: 0;
30
- max: 100;
31
- };
32
- scale: {
33
- min: 0;
34
- max: 1;
35
- };
36
23
  }>;
37
24
  export type VideoSource = Source<Buffer | File, 'video', {
38
25
  destinationPath: string;
39
26
  quality?: number;
40
27
  scale?: number;
41
28
  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
- };
55
29
  }>;
56
30
  export type FaviconSource = Source<Buffer | File, 'favicon', {
57
31
  destinationPath: string;
@@ -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';
@@ -49,7 +49,11 @@ async function getFolderFiles(folderPath) {
49
49
  }
50
50
  return files;
51
51
  }
52
+ function clamp(number, min = 0, max = 0) {
53
+ return Math.max(min, Math.min(number, max));
54
+ }
52
55
  export {
56
+ clamp,
53
57
  clear,
54
58
  getBuffer,
55
59
  getFolderFiles,
@@ -0,0 +1 @@
1
+ export declare function clamp(number: number, min?: number, max?: number): number;
@@ -1,4 +0,0 @@
1
- export declare function getNumberSetting<T extends number | undefined>(value: T, restricstions?: {
2
- min: number;
3
- max: number;
4
- }): number | T;
File without changes
File without changes
File without changes
File without changes