@uxf/resizer 11.40.1 → 11.41.2
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/README.md +8 -0
- package/package.json +1 -1
- package/src/middleware.js +10 -45
- package/src/middleware.ts +12 -58
- package/src/next-route.js +9 -44
- package/src/next-route.ts +13 -58
- package/src/utils/image-converter.d.ts +3 -0
- package/src/utils/image-converter.js +43 -0
- package/src/utils/image-converter.ts +56 -0
- package/src/utils/log.d.ts +1 -1
- package/src/utils/log.js +2 -2
- package/src/utils/log.ts +2 -2
- package/src/utils/tools.d.ts +20 -8
- package/src/utils/tools.js +2 -2
- package/src/utils/tools.ts +21 -9
package/README.md
CHANGED
package/package.json
CHANGED
package/src/middleware.js
CHANGED
|
@@ -22,17 +22,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
26
|
exports.resizerMiddleware = void 0;
|
|
30
27
|
const express_1 = require("express");
|
|
31
28
|
const fs = __importStar(require("fs"));
|
|
32
29
|
const fs_1 = require("fs");
|
|
33
30
|
const path_1 = require("path");
|
|
34
|
-
const sharp_1 = __importDefault(require("sharp"));
|
|
35
31
|
const get_source_filename_1 = require("./utils/get-source-filename");
|
|
32
|
+
const image_converter_1 = require("./utils/image-converter");
|
|
36
33
|
const log_1 = require("./utils/log");
|
|
37
34
|
const tools_1 = require("./utils/tools");
|
|
38
35
|
const resizerMiddleware = (config) => {
|
|
@@ -63,49 +60,17 @@ const resizerMiddleware = (config) => {
|
|
|
63
60
|
fs.writeFileSync(generatedFilename, sourceFile instanceof ArrayBuffer ? Buffer.from(sourceFile) : sourceFile);
|
|
64
61
|
return res.sendFile(generatedFilename);
|
|
65
62
|
}
|
|
66
|
-
|
|
67
|
-
.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
fit: (0, tools_1.getFit)(params),
|
|
74
|
-
position: (0, tools_1.getPosition)(params),
|
|
75
|
-
withoutEnlargement: (0, tools_1.getWithoutEnlargement)(params),
|
|
76
|
-
});
|
|
77
|
-
const trim = (0, tools_1.getTrim)(params);
|
|
78
|
-
if (trim !== undefined) {
|
|
79
|
-
pipeline.trim({
|
|
80
|
-
threshold: trim,
|
|
63
|
+
(0, image_converter_1.convertImage)(sourceFile, generatedFilename, params)
|
|
64
|
+
.then(() => {
|
|
65
|
+
var _a;
|
|
66
|
+
return res.sendFile(generatedFilename, {
|
|
67
|
+
headers: {
|
|
68
|
+
"Content-Type": (_a = tools_1.CONTENT_TYPES[params.toFormat]) !== null && _a !== void 0 ? _a : "image/jpeg",
|
|
69
|
+
},
|
|
81
70
|
});
|
|
82
|
-
}
|
|
83
|
-
const quality = (0, tools_1.getQuality)(params);
|
|
84
|
-
switch (params.toFormat) {
|
|
85
|
-
case "webp":
|
|
86
|
-
pipeline.webp({ quality });
|
|
87
|
-
res.setHeader("Content-Type", "image/webp");
|
|
88
|
-
break;
|
|
89
|
-
case "png":
|
|
90
|
-
pipeline.png({ progressive: true, quality });
|
|
91
|
-
res.setHeader("Content-Type", "image/png");
|
|
92
|
-
break;
|
|
93
|
-
case "avif":
|
|
94
|
-
pipeline.avif({ quality });
|
|
95
|
-
res.setHeader("Content-Type", "image/avif");
|
|
96
|
-
break;
|
|
97
|
-
default:
|
|
98
|
-
pipeline.jpeg({ progressive: true, quality });
|
|
99
|
-
res.setHeader("Content-Type", "image/jpeg");
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
pipeline
|
|
103
|
-
.withMetadata({ density: 72 })
|
|
104
|
-
.toColorspace("srgb")
|
|
105
|
-
.toFile(generatedFilename)
|
|
106
|
-
.then(() => res.sendFile(generatedFilename))
|
|
71
|
+
})
|
|
107
72
|
.catch((e) => {
|
|
108
|
-
(0, log_1.log)(e);
|
|
73
|
+
(0, log_1.log)(e, "error");
|
|
109
74
|
return res.sendStatus(404);
|
|
110
75
|
});
|
|
111
76
|
});
|
package/src/middleware.ts
CHANGED
|
@@ -2,19 +2,10 @@ import { Router } from "express";
|
|
|
2
2
|
import * as fs from "fs";
|
|
3
3
|
import { existsSync, mkdirSync } from "fs";
|
|
4
4
|
import { dirname } from "path";
|
|
5
|
-
import sharp from "sharp";
|
|
6
5
|
import { getSourceFilename } from "./utils/get-source-filename";
|
|
6
|
+
import { convertImage } from "./utils/image-converter";
|
|
7
7
|
import { log } from "./utils/log";
|
|
8
|
-
import {
|
|
9
|
-
getBackground,
|
|
10
|
-
getFit,
|
|
11
|
-
getHeight,
|
|
12
|
-
getPosition,
|
|
13
|
-
getQuality,
|
|
14
|
-
getTrim,
|
|
15
|
-
getWidth,
|
|
16
|
-
getWithoutEnlargement,
|
|
17
|
-
} from "./utils/tools";
|
|
8
|
+
import { CONTENT_TYPES, Params } from "./utils/tools";
|
|
18
9
|
|
|
19
10
|
export type Config = Array<{
|
|
20
11
|
route: string;
|
|
@@ -31,7 +22,7 @@ export const resizerMiddleware = (config: Config) => {
|
|
|
31
22
|
|
|
32
23
|
middleware.get(c.route, async (req, res) => {
|
|
33
24
|
log("----------------------------------------");
|
|
34
|
-
const params = req.params;
|
|
25
|
+
const params = req.params as unknown as Params;
|
|
35
26
|
|
|
36
27
|
const generatedFilename = (process.env.UXF_RESIZER_GENERATE_PATH ?? process.cwd()) + req.path;
|
|
37
28
|
log(`Generated filename: ${generatedFilename}`);
|
|
@@ -60,53 +51,16 @@ export const resizerMiddleware = (config: Config) => {
|
|
|
60
51
|
return res.sendFile(generatedFilename);
|
|
61
52
|
}
|
|
62
53
|
|
|
63
|
-
|
|
64
|
-
.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
position: getPosition(params),
|
|
72
|
-
withoutEnlargement: getWithoutEnlargement(params),
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
const trim = getTrim(params);
|
|
76
|
-
if (trim !== undefined) {
|
|
77
|
-
pipeline.trim({
|
|
78
|
-
threshold: trim,
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const quality = getQuality(params);
|
|
83
|
-
|
|
84
|
-
switch (params.toFormat) {
|
|
85
|
-
case "webp":
|
|
86
|
-
pipeline.webp({ quality });
|
|
87
|
-
res.setHeader("Content-Type", "image/webp");
|
|
88
|
-
break;
|
|
89
|
-
case "png":
|
|
90
|
-
pipeline.png({ progressive: true, quality });
|
|
91
|
-
res.setHeader("Content-Type", "image/png");
|
|
92
|
-
break;
|
|
93
|
-
case "avif":
|
|
94
|
-
pipeline.avif({ quality });
|
|
95
|
-
res.setHeader("Content-Type", "image/avif");
|
|
96
|
-
break;
|
|
97
|
-
default:
|
|
98
|
-
pipeline.jpeg({ progressive: true, quality });
|
|
99
|
-
res.setHeader("Content-Type", "image/jpeg");
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
pipeline
|
|
104
|
-
.withMetadata({ density: 72 })
|
|
105
|
-
.toColorspace("srgb")
|
|
106
|
-
.toFile(generatedFilename)
|
|
107
|
-
.then(() => res.sendFile(generatedFilename))
|
|
54
|
+
convertImage(sourceFile, generatedFilename, params)
|
|
55
|
+
.then(() =>
|
|
56
|
+
res.sendFile(generatedFilename, {
|
|
57
|
+
headers: {
|
|
58
|
+
"Content-Type": CONTENT_TYPES[params.toFormat] ?? "image/jpeg",
|
|
59
|
+
},
|
|
60
|
+
}),
|
|
61
|
+
)
|
|
108
62
|
.catch((e) => {
|
|
109
|
-
log(e);
|
|
63
|
+
log(e, "error");
|
|
110
64
|
return res.sendStatus(404);
|
|
111
65
|
});
|
|
112
66
|
});
|
package/src/next-route.js
CHANGED
|
@@ -22,15 +22,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
26
|
exports.StaticGET = StaticGET;
|
|
30
27
|
exports.ProxyGET = ProxyGET;
|
|
31
28
|
const fs_1 = __importStar(require("fs"));
|
|
32
29
|
const path_1 = require("path");
|
|
33
|
-
const
|
|
30
|
+
const image_converter_1 = require("./utils/image-converter");
|
|
34
31
|
const log_1 = require("./utils/log");
|
|
35
32
|
const tools_1 = require("./utils/tools");
|
|
36
33
|
const sendFile = (generatedFilename, contentType) => {
|
|
@@ -51,10 +48,6 @@ async function StaticGET(request) {
|
|
|
51
48
|
if (params === undefined) {
|
|
52
49
|
return Response.error();
|
|
53
50
|
}
|
|
54
|
-
// import usage: _next/static/media/example-logo-resizer.941ec59a => example-logo-resizer
|
|
55
|
-
if (params.filename.startsWith("_next/static/media/")) {
|
|
56
|
-
params.filename = params.filename.replace("_next/static/media/", "").replace(/\.[^.]*$/, "");
|
|
57
|
-
}
|
|
58
51
|
const generatedFilename = process.cwd() + path;
|
|
59
52
|
(0, log_1.log)(`Generated filename: ${generatedFilename}`);
|
|
60
53
|
const contentType = (_b = tools_1.CONTENT_TYPES[params.toFormat]) !== null && _b !== void 0 ? _b : "image/jpeg";
|
|
@@ -65,45 +58,17 @@ async function StaticGET(request) {
|
|
|
65
58
|
if (!(0, fs_1.existsSync)(generatedDirectory)) {
|
|
66
59
|
(0, fs_1.mkdirSync)(generatedDirectory, { recursive: true });
|
|
67
60
|
}
|
|
68
|
-
const
|
|
69
|
-
|
|
61
|
+
const staticUrl = `${process.env.NEXT_PUBLIC_FRONTEND_URL}/${params.filename}.${params.extension}`;
|
|
62
|
+
// import usage: _next/static/media/example-logo-resizer.941ec59a => example-logo-resizer
|
|
63
|
+
const sourceFile = params.filename.startsWith("_next/static/media/")
|
|
64
|
+
? await fetch(staticUrl).then((response) => response.arrayBuffer())
|
|
65
|
+
: process.cwd() + `/public/${params.filename}.${params.extension}`;
|
|
66
|
+
(0, log_1.log)(`Source filename: ${sourceFile instanceof ArrayBuffer ? staticUrl : sourceFile}`);
|
|
70
67
|
if (params.toFormat === "svg") {
|
|
71
|
-
fs_1.default.writeFileSync(generatedFilename, sourceFile);
|
|
68
|
+
fs_1.default.writeFileSync(generatedFilename, sourceFile instanceof ArrayBuffer ? Buffer.from(sourceFile) : sourceFile);
|
|
72
69
|
return sendFile(generatedFilename, "image/svg+xml");
|
|
73
70
|
}
|
|
74
|
-
|
|
75
|
-
.rotate()
|
|
76
|
-
.resize({
|
|
77
|
-
width: (0, tools_1.getWidth)(params),
|
|
78
|
-
height: (0, tools_1.getHeight)(params),
|
|
79
|
-
background: (0, tools_1.getBackground)(params),
|
|
80
|
-
fastShrinkOnLoad: false,
|
|
81
|
-
fit: (0, tools_1.getFit)(params),
|
|
82
|
-
position: (0, tools_1.getPosition)(params),
|
|
83
|
-
withoutEnlargement: (0, tools_1.getWithoutEnlargement)(params),
|
|
84
|
-
});
|
|
85
|
-
const trim = (0, tools_1.getTrim)(params);
|
|
86
|
-
if (trim !== undefined) {
|
|
87
|
-
pipeline.trim({
|
|
88
|
-
threshold: trim,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
const quality = (0, tools_1.getQuality)(params);
|
|
92
|
-
switch (params.toFormat) {
|
|
93
|
-
case "webp":
|
|
94
|
-
pipeline.webp({ quality });
|
|
95
|
-
break;
|
|
96
|
-
case "png":
|
|
97
|
-
pipeline.png({ progressive: true, quality });
|
|
98
|
-
break;
|
|
99
|
-
case "avif":
|
|
100
|
-
pipeline.avif({ quality });
|
|
101
|
-
break;
|
|
102
|
-
default:
|
|
103
|
-
pipeline.jpeg({ progressive: true, quality });
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
await pipeline.withMetadata({ density: 72 }).toColorspace("srgb").toFile(generatedFilename);
|
|
71
|
+
await (0, image_converter_1.convertImage)(sourceFile, generatedFilename, params);
|
|
107
72
|
return sendFile(generatedFilename, contentType);
|
|
108
73
|
}
|
|
109
74
|
// proxy na dev
|
package/src/next-route.ts
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
1
|
import fs, { existsSync, mkdirSync } from "fs";
|
|
2
2
|
import { dirname } from "path";
|
|
3
|
-
import
|
|
3
|
+
import { convertImage } from "./utils/image-converter";
|
|
4
4
|
import { log } from "./utils/log";
|
|
5
|
-
import {
|
|
6
|
-
CONTENT_TYPES,
|
|
7
|
-
getBackground,
|
|
8
|
-
getFit,
|
|
9
|
-
getHeight,
|
|
10
|
-
getPosition,
|
|
11
|
-
getQuality,
|
|
12
|
-
getTrim,
|
|
13
|
-
getWidth,
|
|
14
|
-
getWithoutEnlargement,
|
|
15
|
-
} from "./utils/tools";
|
|
5
|
+
import { CONTENT_TYPES, Params } from "./utils/tools";
|
|
16
6
|
|
|
17
7
|
const sendFile = (generatedFilename: string, contentType: string) => {
|
|
18
8
|
const data = fs.readFileSync(generatedFilename);
|
|
@@ -31,16 +21,11 @@ export async function StaticGET(request: Request) {
|
|
|
31
21
|
const regex =
|
|
32
22
|
/^\/generated\/static\/(?<width>\d+|x)_(?<height>\d+|x)_(?<fit>[a-z]+)_(?<position>[a-z]+)_(?<background>[0-9a-f]+)_(?<trim>[a-z]+)_(?<quality>\d+|x)\/(?<version>[a-z0-9]+)\/(?<filename>.+)\.(?<extension>[a-z]+)\.(?<toFormat>[a-z]+)/i;
|
|
33
23
|
|
|
34
|
-
const params = path.match(regex)?.groups;
|
|
24
|
+
const params = path.match(regex)?.groups as Params | undefined;
|
|
35
25
|
if (params === undefined) {
|
|
36
26
|
return Response.error();
|
|
37
27
|
}
|
|
38
28
|
|
|
39
|
-
// import usage: _next/static/media/example-logo-resizer.941ec59a => example-logo-resizer
|
|
40
|
-
if (params.filename.startsWith("_next/static/media/")) {
|
|
41
|
-
params.filename = params.filename.replace("_next/static/media/", "").replace(/\.[^.]*$/, "");
|
|
42
|
-
}
|
|
43
|
-
|
|
44
29
|
const generatedFilename = process.cwd() + path;
|
|
45
30
|
log(`Generated filename: ${generatedFilename}`);
|
|
46
31
|
|
|
@@ -55,51 +40,21 @@ export async function StaticGET(request: Request) {
|
|
|
55
40
|
mkdirSync(generatedDirectory, { recursive: true });
|
|
56
41
|
}
|
|
57
42
|
|
|
58
|
-
const
|
|
59
|
-
log(`Source filename: ${sourceFile}`);
|
|
60
|
-
|
|
61
|
-
if (params.toFormat === "svg") {
|
|
62
|
-
fs.writeFileSync(generatedFilename, sourceFile);
|
|
63
|
-
return sendFile(generatedFilename, "image/svg+xml");
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const pipeline = sharp(sourceFile)
|
|
67
|
-
.rotate()
|
|
68
|
-
.resize({
|
|
69
|
-
width: getWidth(params),
|
|
70
|
-
height: getHeight(params),
|
|
71
|
-
background: getBackground(params),
|
|
72
|
-
fastShrinkOnLoad: false,
|
|
73
|
-
fit: getFit(params),
|
|
74
|
-
position: getPosition(params),
|
|
75
|
-
withoutEnlargement: getWithoutEnlargement(params),
|
|
76
|
-
});
|
|
43
|
+
const staticUrl = `${process.env.NEXT_PUBLIC_FRONTEND_URL}/${params.filename}.${params.extension}`;
|
|
77
44
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
});
|
|
83
|
-
}
|
|
45
|
+
// import usage: _next/static/media/example-logo-resizer.941ec59a => example-logo-resizer
|
|
46
|
+
const sourceFile = params.filename.startsWith("_next/static/media/")
|
|
47
|
+
? await fetch(staticUrl).then((response) => response.arrayBuffer())
|
|
48
|
+
: process.cwd() + `/public/${params.filename}.${params.extension}`;
|
|
84
49
|
|
|
85
|
-
|
|
50
|
+
log(`Source filename: ${sourceFile instanceof ArrayBuffer ? staticUrl : sourceFile}`);
|
|
86
51
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
break;
|
|
91
|
-
case "png":
|
|
92
|
-
pipeline.png({ progressive: true, quality });
|
|
93
|
-
break;
|
|
94
|
-
case "avif":
|
|
95
|
-
pipeline.avif({ quality });
|
|
96
|
-
break;
|
|
97
|
-
default:
|
|
98
|
-
pipeline.jpeg({ progressive: true, quality });
|
|
99
|
-
break;
|
|
52
|
+
if (params.toFormat === "svg") {
|
|
53
|
+
fs.writeFileSync(generatedFilename, sourceFile instanceof ArrayBuffer ? Buffer.from(sourceFile) : sourceFile);
|
|
54
|
+
return sendFile(generatedFilename, "image/svg+xml");
|
|
100
55
|
}
|
|
101
56
|
|
|
102
|
-
await
|
|
57
|
+
await convertImage(sourceFile, generatedFilename, params);
|
|
103
58
|
|
|
104
59
|
return sendFile(generatedFilename, contentType);
|
|
105
60
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.convertImage = convertImage;
|
|
7
|
+
const sharp_1 = __importDefault(require("sharp"));
|
|
8
|
+
const tools_1 = require("./tools");
|
|
9
|
+
function convertImage(sourceFile, generatedFilename, params) {
|
|
10
|
+
const pipeline = (0, sharp_1.default)(sourceFile)
|
|
11
|
+
.rotate()
|
|
12
|
+
.resize({
|
|
13
|
+
width: (0, tools_1.getWidth)(params),
|
|
14
|
+
height: (0, tools_1.getHeight)(params),
|
|
15
|
+
background: (0, tools_1.getBackground)(params),
|
|
16
|
+
fastShrinkOnLoad: false,
|
|
17
|
+
fit: (0, tools_1.getFit)(params),
|
|
18
|
+
position: (0, tools_1.getPosition)(params),
|
|
19
|
+
withoutEnlargement: (0, tools_1.getWithoutEnlargement)(params),
|
|
20
|
+
});
|
|
21
|
+
const trim = (0, tools_1.getTrim)(params);
|
|
22
|
+
if (trim !== undefined) {
|
|
23
|
+
pipeline.trim({
|
|
24
|
+
threshold: trim,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
const quality = (0, tools_1.getQuality)(params);
|
|
28
|
+
switch (params.toFormat) {
|
|
29
|
+
case "webp":
|
|
30
|
+
pipeline.webp({ quality });
|
|
31
|
+
break;
|
|
32
|
+
case "png":
|
|
33
|
+
pipeline.png({ progressive: true, quality });
|
|
34
|
+
break;
|
|
35
|
+
case "avif":
|
|
36
|
+
pipeline.avif({ quality });
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
pipeline.jpeg({ progressive: true, quality });
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
return pipeline.withMetadata({ density: 72 }).toColorspace("srgb").toFile(generatedFilename);
|
|
43
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import sharp, { OutputInfo } from "sharp";
|
|
2
|
+
import {
|
|
3
|
+
getBackground,
|
|
4
|
+
getFit,
|
|
5
|
+
getHeight,
|
|
6
|
+
getPosition,
|
|
7
|
+
getQuality,
|
|
8
|
+
getTrim,
|
|
9
|
+
getWidth,
|
|
10
|
+
getWithoutEnlargement,
|
|
11
|
+
Params,
|
|
12
|
+
} from "./tools";
|
|
13
|
+
|
|
14
|
+
export function convertImage(
|
|
15
|
+
sourceFile: string | ArrayBuffer,
|
|
16
|
+
generatedFilename: string,
|
|
17
|
+
params: Params,
|
|
18
|
+
): Promise<OutputInfo> {
|
|
19
|
+
const pipeline = sharp(sourceFile)
|
|
20
|
+
.rotate()
|
|
21
|
+
.resize({
|
|
22
|
+
width: getWidth(params),
|
|
23
|
+
height: getHeight(params),
|
|
24
|
+
background: getBackground(params),
|
|
25
|
+
fastShrinkOnLoad: false,
|
|
26
|
+
fit: getFit(params),
|
|
27
|
+
position: getPosition(params),
|
|
28
|
+
withoutEnlargement: getWithoutEnlargement(params),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const trim = getTrim(params);
|
|
32
|
+
if (trim !== undefined) {
|
|
33
|
+
pipeline.trim({
|
|
34
|
+
threshold: trim,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const quality = getQuality(params);
|
|
39
|
+
|
|
40
|
+
switch (params.toFormat) {
|
|
41
|
+
case "webp":
|
|
42
|
+
pipeline.webp({ quality });
|
|
43
|
+
break;
|
|
44
|
+
case "png":
|
|
45
|
+
pipeline.png({ progressive: true, quality });
|
|
46
|
+
break;
|
|
47
|
+
case "avif":
|
|
48
|
+
pipeline.avif({ quality });
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
pipeline.jpeg({ progressive: true, quality });
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return pipeline.withMetadata({ density: 72 }).toColorspace("srgb").toFile(generatedFilename);
|
|
56
|
+
}
|
package/src/utils/log.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function log(message: any): void;
|
|
1
|
+
export declare function log(message: any, level?: "log" | "warn" | "error"): void;
|
package/src/utils/log.js
CHANGED
package/src/utils/log.ts
CHANGED
package/src/utils/tools.d.ts
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
import type { FitEnum } from "sharp";
|
|
2
2
|
export declare const CONTENT_TYPES: Record<string, string | undefined>;
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
export interface Params {
|
|
4
|
+
width: string;
|
|
5
|
+
height: string;
|
|
6
|
+
fit: string;
|
|
7
|
+
position: string;
|
|
8
|
+
background: string;
|
|
9
|
+
trim: string;
|
|
10
|
+
quality: string;
|
|
11
|
+
filename: string;
|
|
12
|
+
extension: string;
|
|
13
|
+
toFormat: string;
|
|
14
|
+
}
|
|
15
|
+
export declare const getQuality: ({ quality }: Params) => number | undefined;
|
|
16
|
+
export declare const getWidth: ({ width }: Params) => number | undefined;
|
|
17
|
+
export declare const getHeight: ({ height }: Params) => number | undefined;
|
|
18
|
+
export declare const getBackground: ({ background }: Params) => string;
|
|
19
|
+
export declare const getFit: ({ fit }: Params) => keyof FitEnum;
|
|
20
|
+
export declare const getPosition: ({ position }: Params) => string | number | undefined;
|
|
21
|
+
export declare const getWithoutEnlargement: ({ extension }: Params) => boolean;
|
|
22
|
+
export declare const getTrim: ({ trim }: Params) => number | undefined;
|
package/src/utils/tools.js
CHANGED
|
@@ -33,7 +33,7 @@ const getWidth = ({ width }) => (width && width !== "x" ? Number(width) : undefi
|
|
|
33
33
|
exports.getWidth = getWidth;
|
|
34
34
|
const getHeight = ({ height }) => (height && height !== "x" ? Number(height) : undefined);
|
|
35
35
|
exports.getHeight = getHeight;
|
|
36
|
-
const getBackground = ({ background }) => background ===
|
|
36
|
+
const getBackground = ({ background }) => (background === "t" ? "transparent" : `#${background}`);
|
|
37
37
|
exports.getBackground = getBackground;
|
|
38
38
|
const getFit = ({ fit }) => { var _a; return (fit ? ((_a = FIT_OPTIONS[fit]) !== null && _a !== void 0 ? _a : "cover") : "cover"); };
|
|
39
39
|
exports.getFit = getFit;
|
|
@@ -41,5 +41,5 @@ const getPosition = ({ position }) => { var _a; return (position ? ((_a = POSITI
|
|
|
41
41
|
exports.getPosition = getPosition;
|
|
42
42
|
const getWithoutEnlargement = ({ extension }) => extension !== "svg";
|
|
43
43
|
exports.getWithoutEnlargement = getWithoutEnlargement;
|
|
44
|
-
const getTrim = ({ trim }) => (trim ===
|
|
44
|
+
const getTrim = ({ trim }) => (trim === "nt" ? undefined : Number(trim));
|
|
45
45
|
exports.getTrim = getTrim;
|
package/src/utils/tools.ts
CHANGED
|
@@ -29,12 +29,24 @@ export const CONTENT_TYPES: Record<string, string | undefined> = {
|
|
|
29
29
|
jpeg: "image/jpeg",
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
export
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
32
|
+
export interface Params {
|
|
33
|
+
width: string;
|
|
34
|
+
height: string;
|
|
35
|
+
fit: string;
|
|
36
|
+
position: string;
|
|
37
|
+
background: string;
|
|
38
|
+
trim: string;
|
|
39
|
+
quality: string;
|
|
40
|
+
filename: string;
|
|
41
|
+
extension: string;
|
|
42
|
+
toFormat: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const getQuality = ({ quality }: Params) => (quality && quality !== "x" ? Number(quality) : undefined);
|
|
46
|
+
export const getWidth = ({ width }: Params) => (width && width !== "x" ? Number(width) : undefined);
|
|
47
|
+
export const getHeight = ({ height }: Params) => (height && height !== "x" ? Number(height) : undefined);
|
|
48
|
+
export const getBackground = ({ background }: Params) => (background === "t" ? "transparent" : `#${background}`);
|
|
49
|
+
export const getFit = ({ fit }: Params): keyof FitEnum => (fit ? (FIT_OPTIONS[fit] ?? "cover") : "cover");
|
|
50
|
+
export const getPosition = ({ position }: Params) => (position ? (POSITION_OPTIONS[position] ?? "centre") : undefined);
|
|
51
|
+
export const getWithoutEnlargement = ({ extension }: Params) => extension !== "svg";
|
|
52
|
+
export const getTrim = ({ trim }: Params) => (trim === "nt" ? undefined : Number(trim));
|