@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.
@@ -1,94 +0,0 @@
1
- /**
2
- * @jest-environment node
3
- */
4
- import { createRouteMatcher, normalizeRoute } from "./match-route";
5
-
6
- // the two shapes `resizerGetDefaultConfig` (@uxf/core) emits
7
- const STATIC_ROUTE =
8
- "/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
- const UPLOAD_ROUTE =
10
- "/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";
11
-
12
- test("matches the uploaded-image route produced by resizerImageUrl", () => {
13
- const match = createRouteMatcher(UPLOAD_ROUTE);
14
-
15
- expect(match("/generated/product/0/f/0f1e2d3c-1111-2222-3333-444455556666_300_x_cv_c_FFF_nt_80_jpg.webp")).toEqual({
16
- namespace: "product",
17
- p1: "0",
18
- p2: "f",
19
- filename: "0f1e2d3c-1111-2222-3333-444455556666",
20
- width: "300",
21
- height: "x",
22
- fit: "cv",
23
- position: "c",
24
- background: "FFF",
25
- trim: "nt",
26
- quality: "80",
27
- extension: "jpg",
28
- toFormat: "webp",
29
- });
30
- });
31
-
32
- test("matches the static route with a nested, dotted filename", () => {
33
- const match = createRouteMatcher(STATIC_ROUTE);
34
-
35
- expect(match("/generated/static/300_200_cv_c_t_nt_x/1/_next/static/media/logo.941ec59a.png.avif")).toEqual({
36
- width: "300",
37
- height: "200",
38
- fit: "cv",
39
- position: "c",
40
- background: "t",
41
- trim: "nt",
42
- quality: "x",
43
- version: "1",
44
- filename: "_next/static/media/logo.941ec59a",
45
- extension: "png",
46
- toFormat: "avif",
47
- });
48
- });
49
-
50
- test("legacy (*) group matches exactly like (.*)", () => {
51
- const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => undefined);
52
- const legacy = createRouteMatcher(STATIC_ROUTE.replace("(.*)", "(*)"));
53
- const current = createRouteMatcher(STATIC_ROUTE);
54
- const pathname = "/generated/static/x_x_cn_lt_FFF_nt_75/3/images/deep/dir/file.name.jpg.webp";
55
-
56
- expect(warnSpy).toHaveBeenCalledTimes(1);
57
- warnSpy.mockRestore();
58
- expect(legacy(pathname)).toEqual(current(pathname));
59
- expect(current(pathname)).toMatchObject({
60
- filename: "images/deep/dir/file.name",
61
- extension: "jpg",
62
- toFormat: "webp",
63
- });
64
- });
65
-
66
- test("normalizeRoute rewrites every (*) and leaves other routes alone", () => {
67
- expect(normalizeRoute("/a/:x(*)/:y(*)")).toBe("/a/:x(.*)/:y(.*)");
68
- expect(normalizeRoute(STATIC_ROUTE)).toBe(STATIC_ROUTE);
69
- });
70
-
71
- test("matching is case-insensitive (background FFF against [a-z]+)", () => {
72
- const match = createRouteMatcher("/x/:background([a-z]+)");
73
-
74
- expect(match("/x/FFF")).toEqual({ background: "FFF" });
75
- expect(match("/X/fff")).toEqual({ background: "fff" });
76
- });
77
-
78
- test("percent-decodes captured values", () => {
79
- const match = createRouteMatcher("/files/:filename(.*).:extension");
80
-
81
- expect(match("/files/My%20Photo.jpg")).toEqual({ filename: "My Photo", extension: "jpg" });
82
- });
83
-
84
- test("returns undefined when the path does not match", () => {
85
- const match = createRouteMatcher(UPLOAD_ROUTE);
86
-
87
- expect(match("/generated/static/300_200/x.png.webp")).toBeUndefined();
88
- expect(match("/generated/product/0/f/not-a-uuid_300_x_cv_c_FFF_nt_80_jpg.webp")).toBeUndefined();
89
- expect(match("/generated/product/0/f/0f1e2d3c_abc_x_cv_c_FFF_nt_80_jpg.webp")).toBeUndefined();
90
- });
91
-
92
- test("throws on an invalid route at construction time", () => {
93
- expect(() => createRouteMatcher("/x/:a([")).toThrow();
94
- });
@@ -1,41 +0,0 @@
1
- import { log } from "./log";
2
- import { Params } from "./tools";
3
-
4
- const LEGACY_WILDCARD = "(*)";
5
-
6
- /**
7
- * Express 4 accepted `:name(*)` as "match anything, slashes included". URLPattern rejects a bare `*`
8
- * inside a regexp group, so the legacy form is rewritten to the equivalent `(.*)`.
9
- */
10
- export function normalizeRoute(route: string): string {
11
- if (!route.includes(LEGACY_WILDCARD)) {
12
- return route;
13
- }
14
-
15
- log(`Route "${route}" uses the deprecated "(*)" group – use "(.*)" instead.`, "warn");
16
- return route.replaceAll(LEGACY_WILDCARD, "(.*)");
17
- }
18
-
19
- /**
20
- * Compiles a `route` from the resizer config into a matcher. The syntax is the one built into Node's
21
- * `URLPattern` (path-to-regexp v6 compatible): `:name`, `:name(regexp)`, `:name+`, several parameters
22
- * per segment. Matching is case-insensitive and the captured values are percent-decoded, mirroring the
23
- * Express 4 router this replaced.
24
- */
25
- export function createRouteMatcher(route: string): (pathname: string) => Params | undefined {
26
- const pattern = new URLPattern({ pathname: normalizeRoute(route) }, { ignoreCase: true });
27
-
28
- return (pathname) => {
29
- const groups = pattern.exec({ pathname })?.pathname.groups;
30
- if (groups === undefined) {
31
- return undefined;
32
- }
33
-
34
- const params: Partial<Record<string, string>> = {};
35
- Object.entries(groups).forEach(([key, value]) => {
36
- params[key] = value === undefined ? undefined : decodeURIComponent(value);
37
- });
38
-
39
- return params as unknown as Params;
40
- };
41
- }
@@ -1,48 +0,0 @@
1
- import { getBackground, getFit, getHeight, getPosition, getQuality, getTrim, getWidth, Params } from "./tools";
2
-
3
- const minimal: Params = {
4
- width: "146",
5
- height: "100",
6
- fit: undefined,
7
- position: undefined,
8
- background: undefined,
9
- trim: undefined,
10
- quality: undefined,
11
- filename: "logo",
12
- extension: "png",
13
- toFormat: "avif",
14
- p1: undefined,
15
- p2: undefined,
16
- namespace: undefined,
17
- };
18
-
19
- test("a route without the optional parameters falls back to defaults", () => {
20
- expect(getWidth(minimal)).toBe(146);
21
- expect(getHeight(minimal)).toBe(100);
22
- expect(getFit(minimal)).toBe("cover");
23
- expect(getPosition(minimal)).toBeUndefined();
24
- expect(getBackground(minimal)).toBe("transparent");
25
- expect(getTrim(minimal)).toBeUndefined();
26
- expect(getQuality(minimal)).toBeUndefined();
27
- });
28
-
29
- test("explicit parameters are mapped", () => {
30
- const params: Params = {
31
- ...minimal,
32
- width: "x",
33
- fit: "cn",
34
- position: "lt",
35
- background: "t",
36
- trim: "10",
37
- quality: "75",
38
- };
39
-
40
- expect(getWidth(params)).toBeUndefined();
41
- expect(getFit(params)).toBe("contain");
42
- expect(getPosition(params)).toBe("left top");
43
- expect(getBackground(params)).toBe("transparent");
44
- expect(getTrim(params)).toBe(10);
45
- expect(getQuality(params)).toBe(75);
46
- expect(getBackground({ ...minimal, background: "ff0000" })).toBe("#ff0000");
47
- expect(getTrim({ ...minimal, trim: "nt" })).toBeUndefined();
48
- });
@@ -1,57 +0,0 @@
1
- import type { FitEnum, ResizeOptions } from "sharp";
2
-
3
- const FIT_OPTIONS: Record<string, ResizeOptions["fit"]> = {
4
- cv: "cover",
5
- f: "fill",
6
- cn: "contain",
7
- in: "inside",
8
- out: "outside",
9
- } as const;
10
-
11
- const POSITION_OPTIONS: Record<string, ResizeOptions["position"]> = {
12
- a: "attention",
13
- b: "bottom",
14
- c: "centre",
15
- e: "entropy",
16
- l: "left",
17
- lb: "left bottom",
18
- lt: "left top",
19
- r: "right",
20
- rb: "right bottom",
21
- rt: "right top",
22
- t: "top",
23
- };
24
-
25
- export const CONTENT_TYPES: Record<string, string | undefined> = {
26
- webp: "image/webp",
27
- png: "image/png",
28
- avif: "image/avif",
29
- jpeg: "image/jpeg",
30
- };
31
-
32
- export type Params = {
33
- width: string | undefined;
34
- height: string | undefined;
35
- fit: string | undefined;
36
- position: string | undefined;
37
- background: string | undefined;
38
- trim: string | undefined;
39
- quality: string | undefined;
40
- filename: string;
41
- extension: string;
42
- toFormat: string;
43
- //
44
- p1: string | undefined;
45
- p2: string | undefined;
46
- namespace: string | undefined;
47
- };
48
-
49
- export const getQuality = ({ quality }: Params) => (quality && quality !== "x" ? Number(quality) : undefined);
50
- export const getWidth = ({ width }: Params) => (width && width !== "x" ? Number(width) : undefined);
51
- export const getHeight = ({ height }: Params) => (height && height !== "x" ? Number(height) : undefined);
52
- export const getBackground = ({ background: bg }: Params) =>
53
- bg === undefined || bg === "t" ? "transparent" : `#${bg}`;
54
- export const getFit = ({ fit }: Params): keyof FitEnum => (fit ? (FIT_OPTIONS[fit] ?? "cover") : "cover");
55
- export const getPosition = ({ position }: Params) => (position ? (POSITION_OPTIONS[position] ?? "centre") : undefined);
56
- export const getWithoutEnlargement = ({ extension }: Params) => extension !== "svg";
57
- export const getTrim = ({ trim }: Params) => (trim === undefined || trim === "nt" ? undefined : Number(trim));
package/tsconfig.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "composite": true,
5
- "outDir": "./"
6
- }
7
- }