@uxf/resizer 15.0.0 → 16.16.0
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/LICENSE +21 -0
- package/bin/uxf-resizer.js +2 -2
- package/index.d.ts +1 -1
- package/index.js +3 -3
- package/package.json +3 -6
- package/src/handler.js +21 -21
- package/src/handler.test.js +2 -2
- package/src/next-route.js +18 -16
- package/src/next-route.test.js +2 -2
- package/src/utils/fetch-source.test.js +4 -4
- package/src/utils/get-source-filename.js +2 -2
- package/src/utils/get-source-filename.test.js +9 -9
- package/src/utils/image-converter.d.ts +1 -1
- package/src/utils/image-converter.js +9 -9
- package/src/utils/match-route.d.ts +1 -1
- package/src/utils/match-route.js +2 -2
- package/src/utils/match-route.test.js +11 -11
- package/src/utils/tools.test.js +16 -16
- package/bin/uxf-resizer.ts +0 -68
- package/index.ts +0 -3
- package/src/handler.test.ts +0 -84
- package/src/handler.ts +0 -131
- package/src/next-route.test.ts +0 -31
- package/src/next-route.ts +0 -119
- package/src/utils/fetch-source.test.ts +0 -28
- package/src/utils/fetch-source.ts +0 -16
- package/src/utils/get-source-filename.test.ts +0 -65
- package/src/utils/get-source-filename.ts +0 -21
- package/src/utils/image-converter.ts +0 -56
- package/src/utils/log.ts +0 -4
- package/src/utils/match-route.test.ts +0 -94
- package/src/utils/match-route.ts +0 -41
- package/src/utils/tools.test.ts +0 -48
- package/src/utils/tools.ts +0 -57
- package/tsconfig.json +0 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022–2026 UX Fans s.r.o.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/bin/uxf-resizer.js
CHANGED
|
@@ -42,7 +42,7 @@ const fs = __importStar(require("fs"));
|
|
|
42
42
|
const hono_1 = require("hono");
|
|
43
43
|
const node_process_1 = require("node:process");
|
|
44
44
|
const yargs_1 = __importDefault(require("yargs"));
|
|
45
|
-
const
|
|
45
|
+
const handler_ts_1 = require("../src/handler.js");
|
|
46
46
|
const PORT = 3000;
|
|
47
47
|
const uxfResizer = async () => {
|
|
48
48
|
var _a;
|
|
@@ -69,7 +69,7 @@ Environment variables:
|
|
|
69
69
|
if (!Array.isArray(config) || config.length === 0) {
|
|
70
70
|
throw new Error("Invalid or empty configuration");
|
|
71
71
|
}
|
|
72
|
-
const handler = (0,
|
|
72
|
+
const handler = (0, handler_ts_1.createResizerHandler)(config);
|
|
73
73
|
const app = new hono_1.Hono();
|
|
74
74
|
app.get("*", (c) => handler(c.req.raw));
|
|
75
75
|
return new Promise((resolve) => {
|
package/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ProxyGET, StaticGET } from "./src/next-route";
|
|
1
|
+
import { ProxyGET, StaticGET } from "./src/next-route.ts";
|
|
2
2
|
export { ProxyGET, StaticGET };
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StaticGET = exports.ProxyGET = void 0;
|
|
4
|
-
const
|
|
5
|
-
Object.defineProperty(exports, "ProxyGET", { enumerable: true, get: function () { return
|
|
6
|
-
Object.defineProperty(exports, "StaticGET", { enumerable: true, get: function () { return
|
|
4
|
+
const next_route_ts_1 = require("./src/next-route.js");
|
|
5
|
+
Object.defineProperty(exports, "ProxyGET", { enumerable: true, get: function () { return next_route_ts_1.ProxyGET; } });
|
|
6
|
+
Object.defineProperty(exports, "StaticGET", { enumerable: true, get: function () { return next_route_ts_1.StaticGET; } });
|
package/package.json
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uxf/resizer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "16.16.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"description": "UXF Resizer",
|
|
6
|
-
"author": "
|
|
7
|
-
"
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "git+https://gitlab.com/uxf-npm/resizer.git"
|
|
10
|
-
},
|
|
6
|
+
"author": "UX Fans s.r.o. <dev@uxf.cz>",
|
|
7
|
+
"homepage": "https://fe.uxf.dev/docs/resizer",
|
|
11
8
|
"bin": {
|
|
12
9
|
"uxf-resizer": "bin/uxf-resizer.js"
|
|
13
10
|
},
|
package/src/handler.js
CHANGED
|
@@ -5,12 +5,12 @@ const crypto_1 = require("crypto");
|
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
6
|
const promises_1 = require("fs/promises");
|
|
7
7
|
const path_1 = require("path");
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
8
|
+
const fetch_source_ts_1 = require("./utils/fetch-source.js");
|
|
9
|
+
const get_source_filename_ts_1 = require("./utils/get-source-filename.js");
|
|
10
|
+
const image_converter_ts_1 = require("./utils/image-converter.js");
|
|
11
|
+
const log_ts_1 = require("./utils/log.js");
|
|
12
|
+
const match_route_ts_1 = require("./utils/match-route.js");
|
|
13
|
+
const tools_ts_1 = require("./utils/tools.js");
|
|
14
14
|
function matchRoute(routes, pathname) {
|
|
15
15
|
for (const route of routes) {
|
|
16
16
|
const params = route.match(pathname);
|
|
@@ -52,36 +52,36 @@ async function writeAtomically(filename, write) {
|
|
|
52
52
|
*/
|
|
53
53
|
function createResizerHandler(config) {
|
|
54
54
|
const routes = config.map((c, i) => {
|
|
55
|
-
(0,
|
|
56
|
-
(0,
|
|
57
|
-
(0,
|
|
58
|
-
return { match: (0,
|
|
55
|
+
(0, log_ts_1.log)(`Config #${i + 1}:`);
|
|
56
|
+
(0, log_ts_1.log)(` Route: ${c.route}`);
|
|
57
|
+
(0, log_ts_1.log)(` Source: ${c.source}`);
|
|
58
|
+
return { match: (0, match_route_ts_1.createRouteMatcher)(c.route), source: c.source };
|
|
59
59
|
});
|
|
60
60
|
return async (request) => {
|
|
61
61
|
var _a, _b;
|
|
62
62
|
try {
|
|
63
63
|
const { pathname } = new URL(request.url);
|
|
64
|
-
(0,
|
|
65
|
-
(0,
|
|
64
|
+
(0, log_ts_1.log)("----------------------------------------");
|
|
65
|
+
(0, log_ts_1.log)(`${request.method} ${pathname}`);
|
|
66
66
|
const matched = matchRoute(routes, pathname);
|
|
67
67
|
if (matched === undefined) {
|
|
68
|
-
(0,
|
|
68
|
+
(0, log_ts_1.log)("No route matched", "warn");
|
|
69
69
|
return new Response("Not Found", { status: 404 });
|
|
70
70
|
}
|
|
71
71
|
const { params, source } = matched;
|
|
72
72
|
const generatedFilename = ((_a = process.env.UXF_RESIZER_GENERATE_PATH) !== null && _a !== void 0 ? _a : process.cwd()) + pathname;
|
|
73
|
-
(0,
|
|
74
|
-
const contentType = params.toFormat === "svg" ? "image/svg+xml" : ((_b =
|
|
73
|
+
(0, log_ts_1.log)(`Generated filename: ${generatedFilename}`);
|
|
74
|
+
const contentType = params.toFormat === "svg" ? "image/svg+xml" : ((_b = tools_ts_1.CONTENT_TYPES[params.toFormat]) !== null && _b !== void 0 ? _b : "image/jpeg");
|
|
75
75
|
if ((0, fs_1.existsSync)(generatedFilename)) {
|
|
76
76
|
return await fileResponse(generatedFilename, contentType);
|
|
77
77
|
}
|
|
78
78
|
await (0, promises_1.mkdir)((0, path_1.dirname)(generatedFilename), { recursive: true });
|
|
79
|
-
const sourceFilename = (0,
|
|
80
|
-
(0,
|
|
79
|
+
const sourceFilename = (0, get_source_filename_ts_1.getSourceFilename)(source, params);
|
|
80
|
+
(0, log_ts_1.log)(`Source filename: ${sourceFilename}`);
|
|
81
81
|
const isRemote = /^https?:\/\//.test(sourceFilename);
|
|
82
82
|
if (params.toFormat === "svg") {
|
|
83
83
|
if (isRemote) {
|
|
84
|
-
const svg = Buffer.from(await (0,
|
|
84
|
+
const svg = Buffer.from(await (0, fetch_source_ts_1.fetchSource)(sourceFilename));
|
|
85
85
|
await writeAtomically(generatedFilename, (tmpFilename) => (0, promises_1.writeFile)(tmpFilename, svg));
|
|
86
86
|
}
|
|
87
87
|
else {
|
|
@@ -89,12 +89,12 @@ function createResizerHandler(config) {
|
|
|
89
89
|
}
|
|
90
90
|
return await fileResponse(generatedFilename, contentType);
|
|
91
91
|
}
|
|
92
|
-
const sourceFile = isRemote ? await (0,
|
|
93
|
-
await writeAtomically(generatedFilename, (tmpFilename) => (0,
|
|
92
|
+
const sourceFile = isRemote ? await (0, fetch_source_ts_1.fetchSource)(sourceFilename) : sourceFilename;
|
|
93
|
+
await writeAtomically(generatedFilename, (tmpFilename) => (0, image_converter_ts_1.convertImage)(sourceFile, tmpFilename, params));
|
|
94
94
|
return await fileResponse(generatedFilename, contentType);
|
|
95
95
|
}
|
|
96
96
|
catch (e) {
|
|
97
|
-
(0,
|
|
97
|
+
(0, log_ts_1.log)(e, "error");
|
|
98
98
|
return new Response("Not Found", { status: 404 });
|
|
99
99
|
}
|
|
100
100
|
};
|
package/src/handler.test.js
CHANGED
|
@@ -10,7 +10,7 @@ const fs_1 = require("fs");
|
|
|
10
10
|
const os_1 = require("os");
|
|
11
11
|
const path_1 = require("path");
|
|
12
12
|
const sharp_1 = __importDefault(require("sharp"));
|
|
13
|
-
const
|
|
13
|
+
const handler_ts_1 = require("./handler.js");
|
|
14
14
|
const STATIC_ROUTE = "/generated/static/:width(\\d+|x)_:height(\\d+|x)_:fit([a-z]+)_:position([a-z]+)_:background([a-z0-9]+)_:trim([a-z0-9]+)_:quality(\\d+|x)/:version/:filename(.*).:extension.:toFormat";
|
|
15
15
|
let workDir;
|
|
16
16
|
let sourceDir;
|
|
@@ -29,7 +29,7 @@ beforeAll(async () => {
|
|
|
29
29
|
.toFile((0, path_1.join)(sourceDir, "img.png"));
|
|
30
30
|
const config = [{ route: STATIC_ROUTE, source: `${sourceDir}/:filename+.:extension` }];
|
|
31
31
|
logSpy = jest.spyOn(console, "log").mockImplementation(() => undefined);
|
|
32
|
-
handler = (0,
|
|
32
|
+
handler = (0, handler_ts_1.createResizerHandler)(config);
|
|
33
33
|
});
|
|
34
34
|
afterAll(() => {
|
|
35
35
|
logSpy.mockRestore();
|
package/src/next-route.js
CHANGED
|
@@ -37,14 +37,14 @@ exports.StaticGET = StaticGET;
|
|
|
37
37
|
exports.ProxyGET = ProxyGET;
|
|
38
38
|
const fs_1 = __importStar(require("fs"));
|
|
39
39
|
const path_1 = require("path");
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
const
|
|
40
|
+
const fetch_source_ts_1 = require("./utils/fetch-source.js");
|
|
41
|
+
const image_converter_ts_1 = require("./utils/image-converter.js");
|
|
42
|
+
const log_ts_1 = require("./utils/log.js");
|
|
43
|
+
const tools_ts_1 = require("./utils/tools.js");
|
|
44
44
|
// A source that cannot be fetched (missing upload, error status, timeout) answers 404 like the Hono handler, not
|
|
45
45
|
// an unhandled 500.
|
|
46
46
|
function sourceNotFound(error) {
|
|
47
|
-
(0,
|
|
47
|
+
(0, log_ts_1.log)(error, "error");
|
|
48
48
|
return new Response("Not Found", { status: 404 });
|
|
49
49
|
}
|
|
50
50
|
const sendFile = (generatedFilename, contentType) => {
|
|
@@ -57,7 +57,8 @@ const sendFile = (generatedFilename, contentType) => {
|
|
|
57
57
|
};
|
|
58
58
|
// lokalni zpracovani obrazku
|
|
59
59
|
async function StaticGET(request) {
|
|
60
|
-
var _a
|
|
60
|
+
var _a;
|
|
61
|
+
var _b;
|
|
61
62
|
const url = new URL(request.url);
|
|
62
63
|
const path = url.pathname;
|
|
63
64
|
const regex = /^\/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;
|
|
@@ -66,8 +67,8 @@ async function StaticGET(request) {
|
|
|
66
67
|
return Response.error();
|
|
67
68
|
}
|
|
68
69
|
const generatedFilename = process.cwd() + path;
|
|
69
|
-
(0,
|
|
70
|
-
const contentType = (_b =
|
|
70
|
+
(0, log_ts_1.log)(`Generated filename: ${generatedFilename}`);
|
|
71
|
+
const contentType = (_b = tools_ts_1.CONTENT_TYPES[params.toFormat]) !== null && _b !== void 0 ? _b : "image/jpeg";
|
|
71
72
|
if ((0, fs_1.existsSync)(generatedFilename)) {
|
|
72
73
|
return sendFile(generatedFilename, contentType);
|
|
73
74
|
}
|
|
@@ -81,23 +82,24 @@ async function StaticGET(request) {
|
|
|
81
82
|
let sourceFile;
|
|
82
83
|
try {
|
|
83
84
|
sourceFile = params.filename.includes("_next/static/media/")
|
|
84
|
-
? await (0,
|
|
85
|
+
? await (0, fetch_source_ts_1.fetchSource)(staticUrl)
|
|
85
86
|
: process.cwd() + `/public/${params.filename}.${params.extension}`;
|
|
86
87
|
}
|
|
87
88
|
catch (e) {
|
|
88
89
|
return sourceNotFound(e);
|
|
89
90
|
}
|
|
90
|
-
(0,
|
|
91
|
+
(0, log_ts_1.log)(`Source filename: ${sourceFile instanceof ArrayBuffer ? staticUrl : sourceFile}`);
|
|
91
92
|
if (params.toFormat === "svg") {
|
|
92
93
|
fs_1.default.writeFileSync(generatedFilename, sourceFile instanceof ArrayBuffer ? Buffer.from(sourceFile) : sourceFile);
|
|
93
94
|
return sendFile(generatedFilename, "image/svg+xml");
|
|
94
95
|
}
|
|
95
|
-
await (0,
|
|
96
|
+
await (0, image_converter_ts_1.convertImage)(sourceFile, generatedFilename, params);
|
|
96
97
|
return sendFile(generatedFilename, contentType);
|
|
97
98
|
}
|
|
98
99
|
// proxy na dev
|
|
99
100
|
async function ProxyGET(request) {
|
|
100
|
-
var _a
|
|
101
|
+
var _a;
|
|
102
|
+
var _b;
|
|
101
103
|
const url = new URL(request.url);
|
|
102
104
|
const path = url.pathname;
|
|
103
105
|
const regex = /^\/generated\/(?<namespace>[0-9a-z-_]+)\/(?<p1>[0-9a-f])\/(?<p2>[0-9a-f-])\/(?<filename>[0-9a-f-]+)_(?<width>\d+|x)_(?<height>\d+|x)_(?<fit>[a-z]+)_(?<position>[a-z]+)_(?<background>[0-9a-f]+)_(?<trim>[a-z]+)_(?<quality>\d+|x)_(?<extension>[a-z]+)\.(?<toFormat>[a-z]+)/i;
|
|
@@ -105,7 +107,7 @@ async function ProxyGET(request) {
|
|
|
105
107
|
if (params === undefined) {
|
|
106
108
|
return Response.error();
|
|
107
109
|
}
|
|
108
|
-
const contentType = (_b =
|
|
110
|
+
const contentType = (_b = tools_ts_1.CONTENT_TYPES[params.toFormat]) !== null && _b !== void 0 ? _b : "image/jpeg";
|
|
109
111
|
const generatedFilename = process.cwd() + path;
|
|
110
112
|
if ((0, fs_1.existsSync)(generatedFilename)) {
|
|
111
113
|
return sendFile(generatedFilename, contentType);
|
|
@@ -115,10 +117,10 @@ async function ProxyGET(request) {
|
|
|
115
117
|
(0, fs_1.mkdirSync)(generatedDirectory, { recursive: true });
|
|
116
118
|
}
|
|
117
119
|
const sourceUrl = `${process.env.NEXT_PUBLIC_API_URL}/upload/${params.namespace}/${params.p1}/${params.p2}/${params.filename}.${params.extension}`;
|
|
118
|
-
(0,
|
|
120
|
+
(0, log_ts_1.log)(`Generated: ${sourceUrl} => ${generatedFilename}`);
|
|
119
121
|
let sourceFile;
|
|
120
122
|
try {
|
|
121
|
-
sourceFile = await (0,
|
|
123
|
+
sourceFile = await (0, fetch_source_ts_1.fetchSource)(sourceUrl);
|
|
122
124
|
}
|
|
123
125
|
catch (e) {
|
|
124
126
|
return sourceNotFound(e);
|
|
@@ -127,6 +129,6 @@ async function ProxyGET(request) {
|
|
|
127
129
|
fs_1.default.writeFileSync(generatedFilename, Buffer.from(sourceFile));
|
|
128
130
|
return sendFile(generatedFilename, "image/svg+xml");
|
|
129
131
|
}
|
|
130
|
-
await (0,
|
|
132
|
+
await (0, image_converter_ts_1.convertImage)(sourceFile, generatedFilename, params);
|
|
131
133
|
return sendFile(generatedFilename, contentType);
|
|
132
134
|
}
|
package/src/next-route.test.js
CHANGED
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
const fs_1 = require("fs");
|
|
5
5
|
const os_1 = require("os");
|
|
6
6
|
const path_1 = require("path");
|
|
7
|
-
const
|
|
7
|
+
const next_route_ts_1 = require("./next-route.js");
|
|
8
8
|
const originalCwd = process.cwd();
|
|
9
9
|
let workDir;
|
|
10
10
|
beforeEach(() => {
|
|
@@ -20,6 +20,6 @@ afterEach(() => {
|
|
|
20
20
|
});
|
|
21
21
|
test("a source the API does not have answers 404, not an unhandled error", async () => {
|
|
22
22
|
jest.spyOn(global, "fetch").mockResolvedValue(new Response("<html>Not found</html>", { status: 404 }));
|
|
23
|
-
const response = await (0,
|
|
23
|
+
const response = await (0, next_route_ts_1.ProxyGET)(new Request("http://localhost/generated/upload/a/b/abc123_100_100_cover_center_ffffff_false_80_svg.svg"));
|
|
24
24
|
expect(response.status).toBe(404);
|
|
25
25
|
});
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
/** @jest-environment node */
|
|
4
|
-
const
|
|
4
|
+
const fetch_source_ts_1 = require("./fetch-source.js");
|
|
5
5
|
afterEach(() => {
|
|
6
6
|
jest.restoreAllMocks();
|
|
7
7
|
});
|
|
8
8
|
test("rejects an error response instead of returning its body as the image", async () => {
|
|
9
9
|
jest.spyOn(global, "fetch").mockResolvedValue(new Response("<html>Not found</html>", { status: 404 }));
|
|
10
|
-
await expect((0,
|
|
10
|
+
await expect((0, fetch_source_ts_1.fetchSource)("https://api.example/upload/a.svg")).rejects.toThrow("responded with 404");
|
|
11
11
|
});
|
|
12
12
|
test("returns the body of a successful response", async () => {
|
|
13
13
|
jest.spyOn(global, "fetch").mockResolvedValue(new Response("<svg/>", { status: 200 }));
|
|
14
|
-
expect(Buffer.from(await (0,
|
|
14
|
+
expect(Buffer.from(await (0, fetch_source_ts_1.fetchSource)("https://api.example/upload/a.svg")).toString()).toBe("<svg/>");
|
|
15
15
|
});
|
|
16
16
|
test("gives up on a source that does not answer within 30 s", async () => {
|
|
17
17
|
var _a, _b, _c;
|
|
18
18
|
const timeout = jest.spyOn(AbortSignal, "timeout");
|
|
19
19
|
const fetchMock = jest.spyOn(global, "fetch").mockResolvedValue(new Response("", { status: 200 }));
|
|
20
|
-
await (0,
|
|
20
|
+
await (0, fetch_source_ts_1.fetchSource)("https://api.example/upload/a.svg");
|
|
21
21
|
expect(timeout).toHaveBeenCalledWith(30000);
|
|
22
22
|
expect((_b = (_a = fetchMock.mock.calls.at(0)) === null || _a === void 0 ? void 0 : _a[1]) === null || _b === void 0 ? void 0 : _b.signal).toBe((_c = timeout.mock.results.at(0)) === null || _c === void 0 ? void 0 : _c.value);
|
|
23
23
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getSourceFilename = getSourceFilename;
|
|
4
|
-
const
|
|
4
|
+
const log_ts_1 = require("./log.js");
|
|
5
5
|
/**
|
|
6
6
|
* `:name`, optionally followed by a path-to-regexp v6 modifier (`+`, `*`, `?`) that is ignored here –
|
|
7
7
|
* the matched value already contains any slashes. A name must start with a letter, so the port in
|
|
@@ -9,7 +9,7 @@ const log_1 = require("./log");
|
|
|
9
9
|
*/
|
|
10
10
|
const PARAM_PATTERN = /:([A-Za-z_]\w*)[+*?]?/g;
|
|
11
11
|
function getSourceFilename(source, params) {
|
|
12
|
-
(0,
|
|
12
|
+
(0, log_ts_1.log)(params);
|
|
13
13
|
return source.replace(PARAM_PATTERN, (_match, key) => {
|
|
14
14
|
const value = params[key];
|
|
15
15
|
if (value === undefined) {
|
|
@@ -1,36 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
3
|
+
const get_source_filename_ts_1 = require("./get-source-filename.js");
|
|
4
4
|
test("get source filename", () => {
|
|
5
|
-
expect((0,
|
|
5
|
+
expect((0, get_source_filename_ts_1.getSourceFilename)("https://domain.uxf.dev/:filename+.:extension", {
|
|
6
6
|
filename: ["path", "to", "file"],
|
|
7
7
|
extension: "jpg",
|
|
8
8
|
})).toStrictEqual("https://domain.uxf.dev/path/to/file.jpg");
|
|
9
|
-
expect((0,
|
|
9
|
+
expect((0, get_source_filename_ts_1.getSourceFilename)("http://localhost:3000/:filename+.:extension", {
|
|
10
10
|
filename: ["path", "to", "file"],
|
|
11
11
|
extension: "jpg",
|
|
12
12
|
})).toStrictEqual("http://localhost:3000/path/to/file.jpg");
|
|
13
|
-
expect((0,
|
|
13
|
+
expect((0, get_source_filename_ts_1.getSourceFilename)("https://:url/:filename+.:extension", {
|
|
14
14
|
url: "domain-as-parameter.uxf.dev",
|
|
15
15
|
filename: ["path", "to", "file"],
|
|
16
16
|
extension: "jpg",
|
|
17
17
|
})).toStrictEqual("https://domain-as-parameter.uxf.dev/path/to/file.jpg");
|
|
18
|
-
expect((0,
|
|
18
|
+
expect((0, get_source_filename_ts_1.getSourceFilename)("/var/www/:filename+.:extension", {
|
|
19
19
|
filename: ["path", "to", "file"],
|
|
20
20
|
extension: "jpg",
|
|
21
21
|
})).toStrictEqual("/var/www/path/to/file.jpg");
|
|
22
22
|
});
|
|
23
23
|
test("accepts a string value containing slashes (what URLPattern yields for :filename(.*))", () => {
|
|
24
|
-
expect((0,
|
|
24
|
+
expect((0, get_source_filename_ts_1.getSourceFilename)("https://static.example.dev/:filename+.:extension", {
|
|
25
25
|
filename: "_next/static/media/logo.941ec59a",
|
|
26
26
|
extension: "png",
|
|
27
27
|
})).toStrictEqual("https://static.example.dev/_next/static/media/logo.941ec59a.png");
|
|
28
28
|
});
|
|
29
29
|
test("ignores the * and ? modifiers", () => {
|
|
30
|
-
expect((0,
|
|
30
|
+
expect((0, get_source_filename_ts_1.getSourceFilename)("/var/www/:dir*/:file?.:ext", { dir: "a/b", file: "c", ext: "png" })).toStrictEqual("/var/www/a/b/c.png");
|
|
31
31
|
});
|
|
32
32
|
test("keeps the uploaded-image source intact", () => {
|
|
33
|
-
expect((0,
|
|
33
|
+
expect((0, get_source_filename_ts_1.getSourceFilename)("https://s3.example.dev/:namespace/:p1/:p2/:filename.:extension", {
|
|
34
34
|
namespace: "product",
|
|
35
35
|
p1: "0",
|
|
36
36
|
p2: "f",
|
|
@@ -39,5 +39,5 @@ test("keeps the uploaded-image source intact", () => {
|
|
|
39
39
|
})).toStrictEqual("https://s3.example.dev/product/0/f/0f1e2d3c-1111-2222-3333-444455556666.jpg");
|
|
40
40
|
});
|
|
41
41
|
test("throws when a parameter is missing", () => {
|
|
42
|
-
expect(() => (0,
|
|
42
|
+
expect(() => (0, get_source_filename_ts_1.getSourceFilename)("/var/www/:filename.:extension", { filename: "x" })).toThrow('Missing parameter "extension"');
|
|
43
43
|
});
|
|
@@ -5,26 +5,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.convertImage = convertImage;
|
|
7
7
|
const sharp_1 = __importDefault(require("sharp"));
|
|
8
|
-
const
|
|
8
|
+
const tools_ts_1 = require("./tools.js");
|
|
9
9
|
function convertImage(sourceFile, generatedFilename, params) {
|
|
10
10
|
const pipeline = (0, sharp_1.default)(sourceFile)
|
|
11
11
|
.rotate()
|
|
12
12
|
.resize({
|
|
13
|
-
width: (0,
|
|
14
|
-
height: (0,
|
|
15
|
-
background: (0,
|
|
13
|
+
width: (0, tools_ts_1.getWidth)(params),
|
|
14
|
+
height: (0, tools_ts_1.getHeight)(params),
|
|
15
|
+
background: (0, tools_ts_1.getBackground)(params),
|
|
16
16
|
fastShrinkOnLoad: false,
|
|
17
|
-
fit: (0,
|
|
18
|
-
position: (0,
|
|
19
|
-
withoutEnlargement: (0,
|
|
17
|
+
fit: (0, tools_ts_1.getFit)(params),
|
|
18
|
+
position: (0, tools_ts_1.getPosition)(params),
|
|
19
|
+
withoutEnlargement: (0, tools_ts_1.getWithoutEnlargement)(params),
|
|
20
20
|
});
|
|
21
|
-
const trim = (0,
|
|
21
|
+
const trim = (0, tools_ts_1.getTrim)(params);
|
|
22
22
|
if (trim !== undefined) {
|
|
23
23
|
pipeline.trim({
|
|
24
24
|
threshold: trim,
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
|
-
const quality = (0,
|
|
27
|
+
const quality = (0, tools_ts_1.getQuality)(params);
|
|
28
28
|
switch (params.toFormat) {
|
|
29
29
|
case "webp":
|
|
30
30
|
pipeline.webp({ quality });
|
package/src/utils/match-route.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.normalizeRoute = normalizeRoute;
|
|
4
4
|
exports.createRouteMatcher = createRouteMatcher;
|
|
5
|
-
const
|
|
5
|
+
const log_ts_1 = require("./log.js");
|
|
6
6
|
const LEGACY_WILDCARD = "(*)";
|
|
7
7
|
/**
|
|
8
8
|
* Express 4 accepted `:name(*)` as "match anything, slashes included". URLPattern rejects a bare `*`
|
|
@@ -12,7 +12,7 @@ function normalizeRoute(route) {
|
|
|
12
12
|
if (!route.includes(LEGACY_WILDCARD)) {
|
|
13
13
|
return route;
|
|
14
14
|
}
|
|
15
|
-
(0,
|
|
15
|
+
(0, log_ts_1.log)(`Route "${route}" uses the deprecated "(*)" group – use "(.*)" instead.`, "warn");
|
|
16
16
|
return route.replaceAll(LEGACY_WILDCARD, "(.*)");
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
@@ -3,12 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
/**
|
|
4
4
|
* @jest-environment node
|
|
5
5
|
*/
|
|
6
|
-
const
|
|
6
|
+
const match_route_ts_1 = require("./match-route.js");
|
|
7
7
|
// the two shapes `resizerGetDefaultConfig` (@uxf/core) emits
|
|
8
8
|
const STATIC_ROUTE = "/generated/static/:width(\\d+|x)_:height(\\d+|x)_:fit([a-z]+)_:position([a-z]+)_:background([a-z]+)_:trim([a-z]+)_:quality(\\d+|x)/:version/:filename(.*).:extension.:toFormat";
|
|
9
9
|
const UPLOAD_ROUTE = "/generated/:namespace/:p1/:p2/:filename([a-f0-9\\-]+)_:width(\\d+|x)_:height(\\d+|x)_:fit([a-z]+)_:position([a-z]+)_:background([a-z]+)_:trim([a-z]+)_:quality(\\d+|x)_:extension.:toFormat";
|
|
10
10
|
test("matches the uploaded-image route produced by resizerImageUrl", () => {
|
|
11
|
-
const match = (0,
|
|
11
|
+
const match = (0, match_route_ts_1.createRouteMatcher)(UPLOAD_ROUTE);
|
|
12
12
|
expect(match("/generated/product/0/f/0f1e2d3c-1111-2222-3333-444455556666_300_x_cv_c_FFF_nt_80_jpg.webp")).toEqual({
|
|
13
13
|
namespace: "product",
|
|
14
14
|
p1: "0",
|
|
@@ -26,7 +26,7 @@ test("matches the uploaded-image route produced by resizerImageUrl", () => {
|
|
|
26
26
|
});
|
|
27
27
|
});
|
|
28
28
|
test("matches the static route with a nested, dotted filename", () => {
|
|
29
|
-
const match = (0,
|
|
29
|
+
const match = (0, match_route_ts_1.createRouteMatcher)(STATIC_ROUTE);
|
|
30
30
|
expect(match("/generated/static/300_200_cv_c_t_nt_x/1/_next/static/media/logo.941ec59a.png.avif")).toEqual({
|
|
31
31
|
width: "300",
|
|
32
32
|
height: "200",
|
|
@@ -43,8 +43,8 @@ test("matches the static route with a nested, dotted filename", () => {
|
|
|
43
43
|
});
|
|
44
44
|
test("legacy (*) group matches exactly like (.*)", () => {
|
|
45
45
|
const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => undefined);
|
|
46
|
-
const legacy = (0,
|
|
47
|
-
const current = (0,
|
|
46
|
+
const legacy = (0, match_route_ts_1.createRouteMatcher)(STATIC_ROUTE.replace("(.*)", "(*)"));
|
|
47
|
+
const current = (0, match_route_ts_1.createRouteMatcher)(STATIC_ROUTE);
|
|
48
48
|
const pathname = "/generated/static/x_x_cn_lt_FFF_nt_75/3/images/deep/dir/file.name.jpg.webp";
|
|
49
49
|
expect(warnSpy).toHaveBeenCalledTimes(1);
|
|
50
50
|
warnSpy.mockRestore();
|
|
@@ -56,24 +56,24 @@ test("legacy (*) group matches exactly like (.*)", () => {
|
|
|
56
56
|
});
|
|
57
57
|
});
|
|
58
58
|
test("normalizeRoute rewrites every (*) and leaves other routes alone", () => {
|
|
59
|
-
expect((0,
|
|
60
|
-
expect((0,
|
|
59
|
+
expect((0, match_route_ts_1.normalizeRoute)("/a/:x(*)/:y(*)")).toBe("/a/:x(.*)/:y(.*)");
|
|
60
|
+
expect((0, match_route_ts_1.normalizeRoute)(STATIC_ROUTE)).toBe(STATIC_ROUTE);
|
|
61
61
|
});
|
|
62
62
|
test("matching is case-insensitive (background FFF against [a-z]+)", () => {
|
|
63
|
-
const match = (0,
|
|
63
|
+
const match = (0, match_route_ts_1.createRouteMatcher)("/x/:background([a-z]+)");
|
|
64
64
|
expect(match("/x/FFF")).toEqual({ background: "FFF" });
|
|
65
65
|
expect(match("/X/fff")).toEqual({ background: "fff" });
|
|
66
66
|
});
|
|
67
67
|
test("percent-decodes captured values", () => {
|
|
68
|
-
const match = (0,
|
|
68
|
+
const match = (0, match_route_ts_1.createRouteMatcher)("/files/:filename(.*).:extension");
|
|
69
69
|
expect(match("/files/My%20Photo.jpg")).toEqual({ filename: "My Photo", extension: "jpg" });
|
|
70
70
|
});
|
|
71
71
|
test("returns undefined when the path does not match", () => {
|
|
72
|
-
const match = (0,
|
|
72
|
+
const match = (0, match_route_ts_1.createRouteMatcher)(UPLOAD_ROUTE);
|
|
73
73
|
expect(match("/generated/static/300_200/x.png.webp")).toBeUndefined();
|
|
74
74
|
expect(match("/generated/product/0/f/not-a-uuid_300_x_cv_c_FFF_nt_80_jpg.webp")).toBeUndefined();
|
|
75
75
|
expect(match("/generated/product/0/f/0f1e2d3c_abc_x_cv_c_FFF_nt_80_jpg.webp")).toBeUndefined();
|
|
76
76
|
});
|
|
77
77
|
test("throws on an invalid route at construction time", () => {
|
|
78
|
-
expect(() => (0,
|
|
78
|
+
expect(() => (0, match_route_ts_1.createRouteMatcher)("/x/:a([")).toThrow();
|
|
79
79
|
});
|
package/src/utils/tools.test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
3
|
+
const tools_ts_1 = require("./tools.js");
|
|
4
4
|
const minimal = {
|
|
5
5
|
width: "146",
|
|
6
6
|
height: "100",
|
|
@@ -17,13 +17,13 @@ const minimal = {
|
|
|
17
17
|
namespace: undefined,
|
|
18
18
|
};
|
|
19
19
|
test("a route without the optional parameters falls back to defaults", () => {
|
|
20
|
-
expect((0,
|
|
21
|
-
expect((0,
|
|
22
|
-
expect((0,
|
|
23
|
-
expect((0,
|
|
24
|
-
expect((0,
|
|
25
|
-
expect((0,
|
|
26
|
-
expect((0,
|
|
20
|
+
expect((0, tools_ts_1.getWidth)(minimal)).toBe(146);
|
|
21
|
+
expect((0, tools_ts_1.getHeight)(minimal)).toBe(100);
|
|
22
|
+
expect((0, tools_ts_1.getFit)(minimal)).toBe("cover");
|
|
23
|
+
expect((0, tools_ts_1.getPosition)(minimal)).toBeUndefined();
|
|
24
|
+
expect((0, tools_ts_1.getBackground)(minimal)).toBe("transparent");
|
|
25
|
+
expect((0, tools_ts_1.getTrim)(minimal)).toBeUndefined();
|
|
26
|
+
expect((0, tools_ts_1.getQuality)(minimal)).toBeUndefined();
|
|
27
27
|
});
|
|
28
28
|
test("explicit parameters are mapped", () => {
|
|
29
29
|
const params = {
|
|
@@ -35,12 +35,12 @@ test("explicit parameters are mapped", () => {
|
|
|
35
35
|
trim: "10",
|
|
36
36
|
quality: "75",
|
|
37
37
|
};
|
|
38
|
-
expect((0,
|
|
39
|
-
expect((0,
|
|
40
|
-
expect((0,
|
|
41
|
-
expect((0,
|
|
42
|
-
expect((0,
|
|
43
|
-
expect((0,
|
|
44
|
-
expect((0,
|
|
45
|
-
expect((0,
|
|
38
|
+
expect((0, tools_ts_1.getWidth)(params)).toBeUndefined();
|
|
39
|
+
expect((0, tools_ts_1.getFit)(params)).toBe("contain");
|
|
40
|
+
expect((0, tools_ts_1.getPosition)(params)).toBe("left top");
|
|
41
|
+
expect((0, tools_ts_1.getBackground)(params)).toBe("transparent");
|
|
42
|
+
expect((0, tools_ts_1.getTrim)(params)).toBe(10);
|
|
43
|
+
expect((0, tools_ts_1.getQuality)(params)).toBe(75);
|
|
44
|
+
expect((0, tools_ts_1.getBackground)({ ...minimal, background: "ff0000" })).toBe("#ff0000");
|
|
45
|
+
expect((0, tools_ts_1.getTrim)({ ...minimal, trim: "nt" })).toBeUndefined();
|
|
46
46
|
});
|