@uxf/resizer 10.0.0-beta.80 → 10.0.0-beta.85

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 CHANGED
@@ -31,11 +31,22 @@ You can use configuration file `.resizer-config.json` or environment variable `U
31
31
  {
32
32
  "route": "/generated/static/:width(\\d+)_:height(\\d+)/:filename(*).:extension.:toExtension",
33
33
  "source": "https://www.uxf.cz/:filename+"
34
+ },
35
+ {
36
+ "route": "/:appName/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",
37
+ "source": "https://s3.uxf.dev/:appName/:namespace/:p1/:p2/:filename.:extension"
38
+ },
39
+ {
40
+ "route": "/:url/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",
41
+ "source": "https://:url/:filename+.:extension"
34
42
  }
35
43
  ]
36
44
  ```
37
45
 
38
46
  ### UXF Basic configuration
47
+
48
+ This is deprecated. Please use our global resizer for all projects.
49
+
39
50
  ```json
40
51
  [
41
52
  {
package/index.js CHANGED
@@ -32,8 +32,9 @@ const fs = __importStar(require("fs"));
32
32
  const fs_1 = require("fs");
33
33
  const node_fetch_1 = __importDefault(require("node-fetch"));
34
34
  const path_1 = require("path");
35
- const path_to_regexp_1 = require("path-to-regexp");
36
35
  const sharp_1 = __importDefault(require("sharp"));
36
+ const get_source_filename_1 = require("./utils/get-source-filename");
37
+ const log_1 = require("./utils/log");
37
38
  const FIT_OPTIONS = {
38
39
  cv: "cover",
39
40
  f: "fill",
@@ -62,51 +63,30 @@ const getFit = ({ fit }) => { var _a; return (fit ? (_a = FIT_OPTIONS[fit]) !==
62
63
  const getPosition = ({ position }) => { var _a; return (position ? (_a = POSITION_OPTIONS[position]) !== null && _a !== void 0 ? _a : "centre" : undefined); };
63
64
  const getWithoutEnlargement = ({ extension }) => extension !== "svg";
64
65
  const getTrim = ({ trim }) => (trim === undefined || trim === "nt" ? undefined : Number(trim));
65
- function log(message) {
66
- // eslint-disable-next-line no-console
67
- console.log(message);
68
- }
69
- /**
70
- * TODO @vejvis HACK
71
- *
72
- * pro odchycení celé cesty i s lomítkama je potřeba udělat hack: /generated/:restPath(*) => restPath bude string
73
- * pro zpětné složení pomocí pathToRegexp::compile() se používá: /generated/:restPath+ => restPath musí být string[]
74
- * Tato funkce projde celý objekt a pokud v hodnotě atributu najde "/" tak tento atribut převede na pole
75
- */
76
- const repairParams = (params) => {
77
- const obj = {};
78
- Object.keys(params).forEach((key) => {
79
- const value = params[key];
80
- obj[key] = value.includes("/") ? value.split("/") : value;
81
- });
82
- return obj;
83
- };
84
66
  const resizerMiddleware = (config) => {
85
67
  const middleware = (0, express_1.Router)();
86
68
  config.forEach((c, i) => {
87
- log(`Config #${i + 1}:`);
88
- log(` Route: ${c.route}`);
89
- log(` Source: ${c.source}`);
69
+ (0, log_1.log)(`Config #${i + 1}:`);
70
+ (0, log_1.log)(` Route: ${c.route}`);
71
+ (0, log_1.log)(` Source: ${c.source}`);
90
72
  middleware.get(c.route, async (req, res) => {
91
- var _a, _b;
92
- const url = c.source.startsWith("http") ? new URL(c.source) : null;
93
- const domain = url === null || url === void 0 ? void 0 : url.origin;
94
- const repairedParams = repairParams(req.params);
95
- const sourceFilename = (0, path_to_regexp_1.compile)((_a = url === null || url === void 0 ? void 0 : url.pathname) !== null && _a !== void 0 ? _a : c.source, {})(repairedParams);
96
- const generatedFilename = ((_b = process.env.UXF_RESIZER_GENERATE_PATH) !== null && _b !== void 0 ? _b : process.cwd()) + req.path;
73
+ var _a;
74
+ (0, log_1.log)("----------------------------------------");
75
+ const params = req.params;
76
+ const generatedFilename = ((_a = process.env.UXF_RESIZER_GENERATE_PATH) !== null && _a !== void 0 ? _a : process.cwd()) + req.path;
77
+ (0, log_1.log)(`Generated filename: ${generatedFilename}`);
97
78
  if ((0, fs_1.existsSync)(generatedFilename)) {
98
79
  return res.sendFile(generatedFilename);
99
80
  }
100
- log("----------------------------------------");
101
- log(repairedParams);
102
- log(`Source filename: ${sourceFilename}`);
103
- log(`Generated filename: ${generatedFilename}`);
104
81
  const generatedDirectory = (0, path_1.dirname)(generatedFilename);
105
82
  if (!(0, fs_1.existsSync)(generatedDirectory)) {
106
83
  (0, fs_1.mkdirSync)(generatedDirectory, { recursive: true });
107
84
  }
108
- const params = req.params;
109
- const sourceFile = domain ? await (await (0, node_fetch_1.default)(domain + sourceFilename)).buffer() : sourceFilename;
85
+ const sourceFilename = (0, get_source_filename_1.getSourceFilename)(c.source, params);
86
+ (0, log_1.log)(`Source filename: ${sourceFilename}`);
87
+ const sourceFile = /^(http|https)/.test(sourceFilename)
88
+ ? await (0, node_fetch_1.default)(sourceFilename).then((response) => response.buffer())
89
+ : sourceFilename;
110
90
  if (params.toFormat === "svg") {
111
91
  fs.writeFileSync(generatedFilename, sourceFile);
112
92
  return res.sendFile(generatedFilename);
@@ -151,7 +131,7 @@ const resizerMiddleware = (config) => {
151
131
  .toFile(generatedFilename)
152
132
  .then(() => res.sendFile(generatedFilename))
153
133
  .catch((e) => {
154
- log(e);
134
+ (0, log_1.log)(e);
155
135
  return res.sendStatus(404);
156
136
  });
157
137
  });
package/index.ts CHANGED
@@ -3,8 +3,9 @@ import * as fs from "fs";
3
3
  import { existsSync, mkdirSync } from "fs";
4
4
  import fetch from "node-fetch";
5
5
  import { dirname } from "path";
6
- import { compile } from "path-to-regexp";
7
6
  import sharp, { FitEnum, ResizeOptions } from "sharp";
7
+ import { getSourceFilename } from "./utils/get-source-filename";
8
+ import { log } from "./utils/log";
8
9
 
9
10
  const FIT_OPTIONS: Record<string, ResizeOptions["fit"]> = {
10
11
  cv: "cover",
@@ -43,27 +44,6 @@ const getPosition = ({ position }: any) => (position ? POSITION_OPTIONS[position
43
44
  const getWithoutEnlargement = ({ extension }: any) => extension !== "svg";
44
45
  const getTrim = ({ trim }: any) => (trim === undefined || trim === "nt" ? undefined : Number(trim));
45
46
 
46
- function log(message: any) {
47
- // eslint-disable-next-line no-console
48
- console.log(message);
49
- }
50
-
51
- /**
52
- * TODO @vejvis HACK
53
- *
54
- * pro odchycení celé cesty i s lomítkama je potřeba udělat hack: /generated/:restPath(*) => restPath bude string
55
- * pro zpětné složení pomocí pathToRegexp::compile() se používá: /generated/:restPath+ => restPath musí být string[]
56
- * Tato funkce projde celý objekt a pokud v hodnotě atributu najde "/" tak tento atribut převede na pole
57
- */
58
- const repairParams = (params: any) => {
59
- const obj: any = {};
60
- Object.keys(params).forEach((key) => {
61
- const value: string = params[key];
62
- obj[key] = value.includes("/") ? value.split("/") : value;
63
- });
64
- return obj;
65
- };
66
-
67
47
  export const resizerMiddleware = (config: Config) => {
68
48
  const middleware = Router();
69
49
 
@@ -73,29 +53,27 @@ export const resizerMiddleware = (config: Config) => {
73
53
  log(` Source: ${c.source}`);
74
54
 
75
55
  middleware.get(c.route, async (req, res) => {
76
- const url = c.source.startsWith("http") ? new URL(c.source) : null;
77
- const domain = url?.origin;
78
- const repairedParams = repairParams(req.params);
79
- const sourceFilename = compile(url?.pathname ?? c.source, {})(repairedParams);
56
+ log("----------------------------------------");
57
+ const params = req.params;
58
+
80
59
  const generatedFilename = (process.env.UXF_RESIZER_GENERATE_PATH ?? process.cwd()) + req.path;
60
+ log(`Generated filename: ${generatedFilename}`);
81
61
 
82
62
  if (existsSync(generatedFilename)) {
83
63
  return res.sendFile(generatedFilename);
84
64
  }
85
65
 
86
- log("----------------------------------------");
87
- log(repairedParams);
88
- log(`Source filename: ${sourceFilename}`);
89
- log(`Generated filename: ${generatedFilename}`);
90
-
91
66
  const generatedDirectory = dirname(generatedFilename);
92
67
  if (!existsSync(generatedDirectory)) {
93
68
  mkdirSync(generatedDirectory, { recursive: true });
94
69
  }
95
70
 
96
- const params = req.params;
71
+ const sourceFilename = getSourceFilename(c.source, params);
72
+ log(`Source filename: ${sourceFilename}`);
97
73
 
98
- const sourceFile = domain ? await (await fetch(domain + sourceFilename)).buffer() : sourceFilename;
74
+ const sourceFile = /^(http|https)/.test(sourceFilename)
75
+ ? await fetch(sourceFilename).then((response) => response.buffer())
76
+ : sourceFilename;
99
77
 
100
78
  if (params.toFormat === "svg") {
101
79
  fs.writeFileSync(generatedFilename, sourceFile);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/resizer",
3
- "version": "10.0.0-beta.80",
3
+ "version": "10.0.0-beta.85",
4
4
  "main": "index.js",
5
5
  "description": "UXF Resizer",
6
6
  "author": "UXFans <dev@uxf.cz>",
@@ -17,7 +17,7 @@
17
17
  "license": "MIT",
18
18
  "dependencies": {
19
19
  "express": "4.18.2",
20
- "node-fetch": "3.3.2",
20
+ "node-fetch": "^2.6.7",
21
21
  "path-to-regexp": "6.2.1",
22
22
  "process": "0.11.10",
23
23
  "sharp": "0.32.6",
@@ -25,6 +25,7 @@
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/express": "4.17.18",
28
+ "@types/node-fetch": "^2.5.12",
28
29
  "concurrently": "8.2.1",
29
30
  "crypto-js": "4.1.1"
30
31
  },
@@ -0,0 +1 @@
1
+ export declare function getSourceFilename(source: string, params: Record<string, any>): string;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSourceFilename = void 0;
4
+ const path_to_regexp_1 = require("path-to-regexp");
5
+ const log_1 = require("./log");
6
+ const parse_http_source_1 = require("./parse-http-source");
7
+ const repair_params_1 = require("./repair-params");
8
+ function getSourceFilename(source, params) {
9
+ const parsedHttpSource = (0, parse_http_source_1.parseHttpSource)(source);
10
+ const repairedParams = (0, repair_params_1.repairParams)(params);
11
+ (0, log_1.log)(repairedParams);
12
+ if (parsedHttpSource) {
13
+ const compiled = (0, path_to_regexp_1.compile)(`/${parsedHttpSource.domain}/${parsedHttpSource.path}`, {})(repairedParams);
14
+ return `${parsedHttpSource.protocol}:/${compiled}`;
15
+ }
16
+ return (0, path_to_regexp_1.compile)(source, {})(repairedParams);
17
+ }
18
+ exports.getSourceFilename = getSourceFilename;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const get_source_filename_1 = require("./get-source-filename");
4
+ test("get source filename", () => {
5
+ expect((0, get_source_filename_1.getSourceFilename)("https://domain.uxf.dev/:filename+.:extension", {
6
+ filename: ["path", "to", "file"],
7
+ extension: "jpg",
8
+ })).toStrictEqual("https://domain.uxf.dev/path/to/file.jpg");
9
+ expect((0, get_source_filename_1.getSourceFilename)("https://:url/:filename+.:extension", {
10
+ url: "domain-as-parameter.uxf.dev",
11
+ filename: ["path", "to", "file"],
12
+ extension: "jpg",
13
+ })).toStrictEqual("https://domain-as-parameter.uxf.dev/path/to/file.jpg");
14
+ expect((0, get_source_filename_1.getSourceFilename)("/var/www/:filename+.:extension", {
15
+ filename: ["path", "to", "file"],
16
+ extension: "jpg",
17
+ })).toStrictEqual("/var/www/path/to/file.jpg");
18
+ });
@@ -0,0 +1,25 @@
1
+ import { getSourceFilename } from "./get-source-filename";
2
+
3
+ test("get source filename", () => {
4
+ expect(
5
+ getSourceFilename("https://domain.uxf.dev/:filename+.:extension", {
6
+ filename: ["path", "to", "file"],
7
+ extension: "jpg",
8
+ }),
9
+ ).toStrictEqual("https://domain.uxf.dev/path/to/file.jpg");
10
+
11
+ expect(
12
+ getSourceFilename("https://:url/:filename+.:extension", {
13
+ url: "domain-as-parameter.uxf.dev",
14
+ filename: ["path", "to", "file"],
15
+ extension: "jpg",
16
+ }),
17
+ ).toStrictEqual("https://domain-as-parameter.uxf.dev/path/to/file.jpg");
18
+
19
+ expect(
20
+ getSourceFilename("/var/www/:filename+.:extension", {
21
+ filename: ["path", "to", "file"],
22
+ extension: "jpg",
23
+ }),
24
+ ).toStrictEqual("/var/www/path/to/file.jpg");
25
+ });
@@ -0,0 +1,17 @@
1
+ import { compile } from "path-to-regexp";
2
+ import { log } from "./log";
3
+ import { parseHttpSource } from "./parse-http-source";
4
+ import { repairParams } from "./repair-params";
5
+
6
+ export function getSourceFilename(source: string, params: Record<string, any>): string {
7
+ const parsedHttpSource = parseHttpSource(source);
8
+ const repairedParams = repairParams(params);
9
+ log(repairedParams);
10
+
11
+ if (parsedHttpSource) {
12
+ const compiled = compile(`/${parsedHttpSource.domain}/${parsedHttpSource.path}`, {})(repairedParams);
13
+ return `${parsedHttpSource.protocol}:/${compiled}`;
14
+ }
15
+
16
+ return compile(source, {})(repairedParams);
17
+ }
package/utils/log.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function log(message: any): void;
package/utils/log.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.log = void 0;
4
+ function log(message) {
5
+ // eslint-disable-next-line no-console
6
+ console.log(message);
7
+ }
8
+ exports.log = log;
package/utils/log.ts ADDED
@@ -0,0 +1,4 @@
1
+ export function log(message: any) {
2
+ // eslint-disable-next-line no-console
3
+ console.log(message);
4
+ }
@@ -0,0 +1,7 @@
1
+ interface Result {
2
+ protocol: string;
3
+ path: string;
4
+ domain: string;
5
+ }
6
+ export declare function parseHttpSource(source: string): Result | undefined;
7
+ export {};
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseHttpSource = void 0;
4
+ function parseHttpSource(source) {
5
+ var _a;
6
+ return (_a = /^(?<protocol>http|https):\/\/(?<domain>.*)\/(?<path>.*)$/.exec(source)) === null || _a === void 0 ? void 0 : _a.groups;
7
+ }
8
+ exports.parseHttpSource = parseHttpSource;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const parse_http_source_1 = require("./parse-http-source");
4
+ test("parse source", () => {
5
+ expect((0, parse_http_source_1.parseHttpSource)("https://domain.uxf.dev/:filename+.:extension")).toEqual({
6
+ protocol: "https",
7
+ domain: "domain.uxf.dev",
8
+ path: ":filename+.:extension",
9
+ });
10
+ expect((0, parse_http_source_1.parseHttpSource)("https://:url/:filename+.:extension")).toEqual({
11
+ protocol: "https",
12
+ domain: ":url",
13
+ path: ":filename+.:extension",
14
+ });
15
+ expect((0, parse_http_source_1.parseHttpSource)("/var/www/:filename+.:extension")).toBeUndefined();
16
+ });
@@ -0,0 +1,15 @@
1
+ import { parseHttpSource } from "./parse-http-source";
2
+
3
+ test("parse source", () => {
4
+ expect(parseHttpSource("https://domain.uxf.dev/:filename+.:extension")).toEqual({
5
+ protocol: "https",
6
+ domain: "domain.uxf.dev",
7
+ path: ":filename+.:extension",
8
+ });
9
+ expect(parseHttpSource("https://:url/:filename+.:extension")).toEqual({
10
+ protocol: "https",
11
+ domain: ":url",
12
+ path: ":filename+.:extension",
13
+ });
14
+ expect(parseHttpSource("/var/www/:filename+.:extension")).toBeUndefined();
15
+ });
@@ -0,0 +1,9 @@
1
+ interface Result {
2
+ protocol: string;
3
+ path: string;
4
+ domain: string;
5
+ }
6
+
7
+ export function parseHttpSource(source: string): Result | undefined {
8
+ return /^(?<protocol>http|https):\/\/(?<domain>.*)\/(?<path>.*)$/.exec(source)?.groups as Result | undefined;
9
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * TODO @vejvis HACK
3
+ *
4
+ * pro odchycení celé cesty i s lomítkama je potřeba udělat hack: /generated/:restPath(*) => restPath bude string
5
+ * pro zpětné složení pomocí pathToRegexp::compile() se používá: /generated/:restPath+ => restPath musí být string[]
6
+ * Tato funkce projde celý objekt a pokud v hodnotě atributu najde "/" tak tento atribut převede na pole
7
+ */
8
+ export declare const repairParams: (params: any) => any;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.repairParams = void 0;
4
+ /**
5
+ * TODO @vejvis HACK
6
+ *
7
+ * pro odchycení celé cesty i s lomítkama je potřeba udělat hack: /generated/:restPath(*) => restPath bude string
8
+ * pro zpětné složení pomocí pathToRegexp::compile() se používá: /generated/:restPath+ => restPath musí být string[]
9
+ * Tato funkce projde celý objekt a pokud v hodnotě atributu najde "/" tak tento atribut převede na pole
10
+ */
11
+ const repairParams = (params) => {
12
+ const obj = {};
13
+ Object.keys(params).forEach((key) => {
14
+ const value = params[key];
15
+ obj[key] = value.includes("/") ? value.split("/") : value;
16
+ });
17
+ return obj;
18
+ };
19
+ exports.repairParams = repairParams;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * TODO @vejvis HACK
3
+ *
4
+ * pro odchycení celé cesty i s lomítkama je potřeba udělat hack: /generated/:restPath(*) => restPath bude string
5
+ * pro zpětné složení pomocí pathToRegexp::compile() se používá: /generated/:restPath+ => restPath musí být string[]
6
+ * Tato funkce projde celý objekt a pokud v hodnotě atributu najde "/" tak tento atribut převede na pole
7
+ */
8
+ export const repairParams = (params: any) => {
9
+ const obj: any = {};
10
+ Object.keys(params).forEach((key) => {
11
+ const value: string = params[key];
12
+ obj[key] = value.includes("/") ? value.split("/") : value;
13
+ });
14
+ return obj;
15
+ };