@uxf/resizer 1.2.2 → 2.0.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.
@@ -0,0 +1,15 @@
1
+ [
2
+ {
3
+ "route": "/generated/:namespace/:p1/:p2/:filename([a-f0-9\\-]+)_:width(\\d+)_:height(\\d+)_:fit([a-z]+)_:position([a-z]+)_:background([a-z]+)_:trim([a-z]+)_:extension.:toFormat",
4
+ "pathToSourceFile": "/Users/vejvis/UXF/projects/resizer/example-upload/:namespace/:p1/:p2/:filename.:extension"
5
+ },
6
+ {
7
+ "route": "/generated/:namespace/:p1/:p2/:filename([a-f0-9\\-]+)_:width(\\d+)_:height(\\d+)_:fit([a-z]+).:extension",
8
+ "pathToSourceFile": "/Users/vejvis/UXF/projects/resizer/example-upload/:namespace/:p1/:p2/:filename.:extension"
9
+ },
10
+ {
11
+ "route": "/generated/opentopo/:width(\\d+)_:height(\\d+)_:fit([a-z]+)/:filename",
12
+ "pathToSourceFile": "/:filename",
13
+ "sourceDomain": "https://opentopo.app"
14
+ }
15
+ ]
package/README.md CHANGED
@@ -7,27 +7,54 @@
7
7
  // global installation
8
8
  yarn global add @uxf/resizer
9
9
  export PATH="$(yarn global bin):$PATH"
10
- UXF_RESIZER_SOURCE_PATH=${PWD}/example/source UXF_RESIZER_GENERATE_PATH=${PWD}/example/generated uxf-resizer
10
+ uxf-resizer
11
11
 
12
12
  // local installation
13
13
  yarn add @uxf/resizer
14
- UXF_RESIZER_SOURCE_PATH=${PWD}/example/source UXF_RESIZER_GENERATE_PATH=${PWD}/example/generated ./node-modules/.bin/uxf-resizer
14
+ uxf-resizer
15
15
  ```
16
16
 
17
- ## Default filename regexp
17
+ ## Configuration
18
18
 
19
- ```
20
- :originalFilename([a-f0-9\-]+)_:width(\d+)_:height(\d+)_:fit([a-z]+)_:position([a-z]+)_:background([a-z]+)_:trim([a-z]+)_:originalExtension.:toFormat
19
+ You can use configuration file `.resizer-config.json` or environment variable `UXF_RESIZER_CONFIG`
20
+
21
+ ```json
22
+ [
23
+ {
24
+ "route": "/generated/:namespace/:p1/:p2/:filename([a-f0-9\\-]+)_:width(\\d+)_:height(\\d+)_:fit([a-z]+)_:position([a-z]+)_:background([a-z]+)_:trim([a-z]+)_:extension.:toFormat",
25
+ "pathToSourceFile": "/var/www/UXF/projects/resizer/example-upload/:namespace/:p1/:p2/:filename.:extension"
26
+ },
27
+ {
28
+ "route": "/generated/:namespace/:p1/:p2/:filename([a-f0-9\\-]+)_:width(\\d+)_:height(\\d+)_:fit([a-z]+).:extension",
29
+ "pathToSourceFile": "/var/www/UXF/projects/resizer/example-upload/:namespace/:p1/:p2/:filename.:extension"
30
+ },
31
+ {
32
+ "route": "/generated/opentopo/:width(\\d+)_:height(\\d+)_:fit([a-z]+)/:filename",
33
+ "pathToSourceFile": "/:filename",
34
+ "sourceDomain": "https://opentopo.app"
35
+ }
36
+ ]
21
37
  ```
22
38
 
23
- ### fit
39
+ ### Supported route parameters
40
+ | Parameter name | Type | Default sharp value |
41
+ |----------------|-------------------------------|---------------------|
42
+ | width | `number` or `x` (auto) | `undefined` |
43
+ | height | `number` or `x` (auto) | `undefined` |
44
+ | fit | `Fit` | cover |
45
+ | position | `Position` | |
46
+ | background | `FFFFFF` or `t` (transparent) | |
47
+ | toFormat | `webp`, `png`, `avif`, `jpg` | `jpg` |
48
+ | trim | `number` or `nt` (not trim) | |
49
+
50
+ #### Fit
24
51
  - `cv`: cover,
25
52
  - `f`: fill,
26
53
  - `cn`: contain,
27
54
  - `in`: inside,
28
55
  - `out`: outside,
29
56
 
30
- ### position
57
+ #### Position
31
58
  - `a`: attention
32
59
  - `b`: bottom
33
60
  - `c`: centre
@@ -40,17 +67,6 @@ UXF_RESIZER_SOURCE_PATH=${PWD}/example/source UXF_RESIZER_GENERATE_PATH=${PWD}/e
40
67
  - `rt`: right top
41
68
  - `t`: top
42
69
 
43
- ### background
44
- - `t`: transparent
45
- - `FFFFF`: white (or other hex color)
46
-
47
- ### trim
48
- - `nt`: not trim
49
- - `number`: number value
50
-
51
- ### toFormat
52
- - `webp | png | avif | or other`
53
-
54
70
  ## Help
55
71
 
56
72
  ```
@@ -1,5 +1,28 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
3
26
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
27
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
28
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -36,17 +59,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
59
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
37
60
  }
38
61
  };
39
- var __rest = (this && this.__rest) || function (s, e) {
40
- var t = {};
41
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
42
- t[p] = s[p];
43
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
44
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
45
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
46
- t[p[i]] = s[p[i]];
47
- }
48
- return t;
49
- };
50
62
  var __importDefault = (this && this.__importDefault) || function (mod) {
51
63
  return (mod && mod.__esModule) ? mod : { "default": mod };
52
64
  };
@@ -54,80 +66,35 @@ Object.defineProperty(exports, "__esModule", { value: true });
54
66
  var process_1 = require("process");
55
67
  var yargs_1 = __importDefault(require("yargs"));
56
68
  var index_1 = require("../index");
69
+ var express_1 = __importDefault(require("express"));
70
+ var fs = __importStar(require("fs"));
57
71
  var uxfResizer = function () { return __awaiter(void 0, void 0, void 0, function () {
58
- var _a, help, options, sourceFolderDepth_1, generatedFolderDepth_1, filenameRegexp_1, disableNamespace_1, sourcePath_1, generatePath_1;
72
+ var help, configFilename, configString, config_1;
73
+ var _a;
59
74
  return __generator(this, function (_b) {
60
75
  yargs_1.default.command("$0", "UXF Resizer", function (yargs) {
61
- yargs.demandCommand(0, 0).usage("\nUsage:\n uxf-resizer\n \nEnvironment variables:\n UXF_RESIZER_SOURCE_PATH - required\n UXF_RESIZER_GENERATE_PATH - required");
62
- })
63
- .option("disable-namespace", {
64
- group: "Options",
65
- default: false,
66
- type: "boolean",
67
- })
68
- .option("source-folder-depth", {
69
- group: "Options",
70
- description: "Depth of the source folder",
71
- default: 2,
72
- type: "number",
73
- })
74
- .option("generated-folder-depth", {
75
- group: "Options",
76
- description: "Depth of the generated folder",
77
- default: 2,
78
- type: "number",
79
- })
80
- .option("filename-regexp", {
81
- group: "Options",
82
- description: "Filename regexp (see: https://github.com/pillarjs/path-to-regexp)",
83
- type: "string",
76
+ yargs.demandCommand(0, 0).usage("\nUsage:\n uxf-resizer\n \nEnvironment variables:\n UXF_RESIZER_GENERATE_PATH - required");
84
77
  })
85
78
  .option("h", { alias: "help", group: "Options" })
86
79
  .strict(false)
87
80
  .exitProcess(false);
88
81
  try {
89
- _a = yargs_1.default.parse(process_1.argv.slice(2)), help = _a.help, options = __rest(_a, ["help"]);
82
+ help = yargs_1.default.parse(process_1.argv.slice(2)).help;
90
83
  if (help) {
91
84
  return [2 /*return*/, 0];
92
85
  }
93
- sourceFolderDepth_1 = options["source-folder-depth"];
94
- generatedFolderDepth_1 = options["generated-folder-depth"];
95
- filenameRegexp_1 = options["filename-regexp"];
96
- disableNamespace_1 = options["disable-namespace"];
97
- sourcePath_1 = process_1.env.UXF_RESIZER_SOURCE_PATH;
98
- if (!sourcePath_1) {
99
- // eslint-disable-next-line no-console
100
- console.error("Source directory must be set. Use environment variable UXF_RESIZER_SOURCE_PATH.");
101
- return [2 /*return*/, 1];
102
- }
103
- generatePath_1 = process_1.env.UXF_RESIZER_GENERATE_PATH;
104
- if (!generatePath_1) {
105
- // eslint-disable-next-line no-console
106
- console.error("Generated directory must be set. Use environment variable UXF_RESIZER_GENERATED_PATH.");
107
- return [2 /*return*/, 1];
86
+ configFilename = process.cwd() + "/.resizer-config.json";
87
+ configString = (_a = process_1.env.UXF_RESIZER_CONFIG) !== null && _a !== void 0 ? _a : (fs.existsSync(configFilename) ? fs.readFileSync(configFilename).toString() : "[]");
88
+ config_1 = JSON.parse(configString);
89
+ if (!Array.isArray(config_1) || config_1.length === 0) {
90
+ throw new Error("Invalid or empty configuration");
108
91
  }
109
92
  return [2 /*return*/, new Promise(function (resolve, reject) {
110
93
  try {
111
- var app_1 = (0, index_1.createApp)(sourcePath_1, generatePath_1, sourceFolderDepth_1, generatedFolderDepth_1, filenameRegexp_1, disableNamespace_1);
94
+ var app_1 = (0, express_1.default)().use((0, index_1.resizerMiddleware)(config_1));
112
95
  var listenedApp_1 = app_1.listen(3000, function () {
113
- // eslint-disable-next-line no-console
114
- console.log("\nSOURCE_PATH: ".concat(sourcePath_1));
115
- // eslint-disable-next-line no-console
116
- console.log("GENERATED_PATH: ".concat(generatePath_1));
117
- // eslint-disable-next-line no-console
118
- console.log("Source folder depth: ".concat(sourceFolderDepth_1));
119
- // eslint-disable-next-line no-console
120
- console.log("Generated folder depth: ".concat(generatedFolderDepth_1));
121
- // eslint-disable-next-line no-console
122
- console.log("Filename regexp: ".concat(filenameRegexp_1));
123
- // eslint-disable-next-line no-console
124
- console.log("Namespace: ".concat(disableNamespace_1 ? "disabled" : "enabled"));
125
96
  // eslint-disable-next-line no-console
126
97
  console.log("\nImage resizer app listening on port 3000");
127
- if (process.env.NODE_ENV !== "production") {
128
- // eslint-disable-next-line no-console
129
- console.log("Example image: http://localhost:3000/generated/namespace/a/b/ab7dea3a-ba56-4fa0-a407-d9c4a236d153_300_500_cv_c_t_nt_png.png");
130
- }
131
98
  app_1.on("close", function () {
132
99
  listenedApp_1.close();
133
100
  resolve(true);
Binary file
package/index.js CHANGED
@@ -1,27 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -62,11 +39,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
62
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
63
40
  };
64
41
  Object.defineProperty(exports, "__esModule", { value: true });
65
- exports.createApp = void 0;
66
- var express_1 = __importDefault(require("express"));
42
+ exports.resizerMiddleware = void 0;
43
+ var express_1 = require("express");
67
44
  var node_fetch_1 = __importDefault(require("node-fetch"));
68
45
  var fs_1 = require("fs");
69
- var fs = __importStar(require("fs"));
46
+ var path_1 = require("path");
70
47
  var sharp_1 = __importDefault(require("sharp"));
71
48
  var path_to_regexp_1 = require("path-to-regexp");
72
49
  var FIT_OPTIONS = {
@@ -89,107 +66,130 @@ var POSITION_OPTIONS = {
89
66
  rt: "right top",
90
67
  t: "top",
91
68
  };
92
- var isTrim = function (value) { return value !== "nt"; };
93
- var getTrimValue = function (value) { return (isTrim(value) ? Number(value) : 0); };
94
- var createApp = function (sourcePathOrUrl, generatePath, sourceFolderDepth, generatedFolderDepth, filenameRegexp, disableNamespace) {
95
- if (filenameRegexp === void 0) { filenameRegexp = ":originalFilename([a-f0-9\\-]+)_:width(\\d+)_:height(\\d+)_:fit([a-z]+)_:position([a-z]+)_:background([a-z]+)_:trim([a-z]+)_:originalExtension.:toFormat"; }
96
- var parseFilename = (0, path_to_regexp_1.match)(filenameRegexp, { decode: decodeURIComponent });
97
- var generatedPxArray = Array.from({ length: generatedFolderDepth }, function (_, i) { return ":p".concat(i + 1); });
98
- var sourcePxArray = Array.from({ length: sourceFolderDepth }, function (_, i) { return ":p".concat(i + 1); });
99
- var routeRegexpBase = disableNamespace ? "/generated" : "/generated/:namespace";
100
- var routeRegexp = generatedFolderDepth > 0
101
- ? "".concat(routeRegexpBase, "/").concat(generatedPxArray.join("/"), "/:filename")
102
- : "".concat(routeRegexpBase, "/:filename");
103
- // eslint-disable-next-line no-console
104
- console.log("\nExpress route regexp: ".concat(routeRegexp));
69
+ var getWidth = function (_a) {
70
+ var width = _a.width;
71
+ return (width ? Number(width) : undefined);
72
+ };
73
+ var getHeight = function (_a) {
74
+ var height = _a.height;
75
+ return (height ? Number(height) : undefined);
76
+ };
77
+ var getBackground = function (_a) {
78
+ var background = _a.background;
79
+ return background === undefined ? undefined : background === "t" ? "transparent" : "#".concat(background);
80
+ };
81
+ var getFit = function (_a) {
82
+ var _b;
83
+ var fit = _a.fit;
84
+ return (fit ? (_b = FIT_OPTIONS[fit]) !== null && _b !== void 0 ? _b : "cover" : "cover");
85
+ };
86
+ var getPosition = function (_a) {
87
+ var _b;
88
+ var position = _a.position;
89
+ return (position ? (_b = POSITION_OPTIONS[position]) !== null && _b !== void 0 ? _b : "centre" : undefined);
90
+ };
91
+ var getWithoutEnlargement = function (_a) {
92
+ var extension = _a.extension;
93
+ return extension !== "svg";
94
+ };
95
+ var getTrim = function (_a) {
96
+ var trim = _a.trim;
97
+ return (trim === undefined || trim === "nt" ? undefined : Number(trim));
98
+ };
99
+ function log(message) {
105
100
  // eslint-disable-next-line no-console
106
- console.log("Filename regexp: ".concat(filenameRegexp));
107
- return (0, express_1.default)().get(routeRegexp, function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
108
- var _a, filename, namespace, parsedFilename, _b, originalFilename, originalExtension, fit, height, position, toFormat, trim, width, background, generatedPxDirectories, generatedDirectoryPart, generatedDirectory, generatedFilePath, sourcePxDirectories, sourceDirectoryPart, sourceDirectory, sourceFilename, sourceFilePath, pipeline, contentType, cacheMaxAge;
109
- return __generator(this, function (_c) {
110
- switch (_c.label) {
111
- case 0:
112
- _a = req.params, filename = _a.filename, namespace = _a.namespace;
113
- parsedFilename = parseFilename(filename);
114
- if (!parsedFilename) {
115
- process.stdout.write("Filename '".concat(filename, "' not found."));
116
- return [2 /*return*/, res.sendStatus(404)];
117
- }
118
- _b = parsedFilename.params, originalFilename = _b.originalFilename, originalExtension = _b.originalExtension, fit = _b.fit, height = _b.height, position = _b.position, toFormat = _b.toFormat, trim = _b.trim, width = _b.width, background = _b.background;
119
- generatedPxDirectories = generatedPxArray.map(function (_, index) { return originalFilename.charAt(index); });
120
- generatedDirectoryPart = disableNamespace ? generatePath : "".concat(generatePath, "/").concat(namespace);
121
- generatedDirectory = generatedPxDirectories.length > 0
122
- ? "".concat(generatedDirectoryPart, "/").concat(generatedPxDirectories.join("/"))
123
- : generatedDirectoryPart;
124
- generatedFilePath = "".concat(generatedDirectory, "/").concat(filename);
125
- // try load from disk
126
- if (fs.existsSync(generatedFilePath)) {
127
- return [2 /*return*/, res.sendFile(generatedFilePath)];
128
- }
129
- if (!(0, fs_1.existsSync)(generatedDirectory)) {
130
- (0, fs_1.mkdirSync)(generatedDirectory, { recursive: true });
131
- }
132
- sourcePxDirectories = sourcePxArray.map(function (_, index) { return originalFilename.charAt(index); });
133
- sourceDirectoryPart = disableNamespace ? sourcePathOrUrl : "".concat(sourcePathOrUrl, "/").concat(namespace);
134
- sourceDirectory = sourcePxDirectories.length > 0
135
- ? "".concat(sourceDirectoryPart, "/").concat(sourcePxDirectories.join("/"))
136
- : sourceDirectoryPart;
137
- sourceFilename = "".concat(originalFilename, ".").concat(originalExtension);
138
- sourceFilePath = "".concat(sourceDirectory, "/").concat(sourceFilename);
139
- if (!sourcePathOrUrl.startsWith("https://")) return [3 /*break*/, 3];
140
- return [4 /*yield*/, (0, node_fetch_1.default)(sourceFilePath)];
141
- case 1: return [4 /*yield*/, (_c.sent()).buffer()];
142
- case 2:
143
- sourceFilePath = _c.sent();
144
- _c.label = 3;
145
- case 3: return [4 /*yield*/, (0, sharp_1.default)(sourceFilePath)
146
- .rotate()
147
- .resize(width ? Number(width) : undefined, height ? Number(height) : undefined, {
148
- background: background === "t" ? "transparent" : "#".concat(background),
149
- fastShrinkOnLoad: false,
150
- fit: FIT_OPTIONS[fit] || "cover",
151
- position: POSITION_OPTIONS[position] || "centre",
152
- withoutEnlargement: originalExtension !== "svg",
153
- })];
154
- case 4:
155
- pipeline = _c.sent();
156
- if (isTrim(trim)) {
157
- pipeline.trim(getTrimValue(trim));
158
- }
159
- switch (toFormat) {
160
- case "webp":
161
- pipeline.webp({ quality: 60 });
162
- contentType = "image/webp";
163
- break;
164
- case "png":
165
- pipeline.png({ progressive: true, quality: 60 });
166
- contentType = "image/png";
167
- break;
168
- case "avif":
169
- pipeline.avif();
170
- contentType = "image/avif";
171
- break;
172
- default:
173
- pipeline.jpeg({ progressive: true, quality: 60 });
174
- contentType = "image/jpeg";
175
- break;
176
- }
177
- cacheMaxAge = 7 * 24 * 60 * 60;
178
- res.setHeader("cache-control", "public, max-age=".concat(cacheMaxAge));
179
- res.setHeader("Content-Type", contentType);
180
- pipeline
181
- .withMetadata({ density: 72 })
182
- .toColorspace("srgb")
183
- .toFile(generatedFilePath)
184
- .then(function () { return res.sendFile(generatedFilePath); })
185
- .catch(function (e) {
186
- // eslint-disable-next-line no-console
187
- console.log(e);
188
- return res.sendStatus(404);
189
- });
190
- return [2 /*return*/];
191
- }
192
- });
193
- }); });
101
+ console.log(message);
102
+ }
103
+ var resizerMiddleware = function (config) {
104
+ var middleware = (0, express_1.Router)();
105
+ config.forEach(function (c, i) {
106
+ log("\nConfig #".concat(i + 1, ":"));
107
+ log(" Route: ".concat(c.route));
108
+ log(" Source file: ".concat(c.pathToSourceFile));
109
+ if (c.sourceDomain) {
110
+ log(" Domain: ".concat(c.sourceDomain));
111
+ }
112
+ middleware.get(c.route, function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
113
+ var domain, sourceFilename, generatedFilename, generatedDirectory, params, sourceFile, _a, pipeline, trim;
114
+ var _b;
115
+ return __generator(this, function (_c) {
116
+ switch (_c.label) {
117
+ case 0:
118
+ domain = c.sourceDomain;
119
+ sourceFilename = (0, path_to_regexp_1.compile)(c.pathToSourceFile, {})(req.params);
120
+ generatedFilename = ((_b = process.env.UXF_RESIZER_GENERATE_PATH) !== null && _b !== void 0 ? _b : process.cwd()) + req.path;
121
+ if ((0, fs_1.existsSync)(generatedFilename)) {
122
+ return [2 /*return*/, res.sendFile(generatedFilename)];
123
+ }
124
+ log("----------------------------------------");
125
+ log(req.params);
126
+ log("Source filename: ".concat(sourceFilename));
127
+ log("Generated filename: ".concat(generatedFilename));
128
+ generatedDirectory = (0, path_1.dirname)(generatedFilename);
129
+ if (!(0, fs_1.existsSync)(generatedDirectory)) {
130
+ (0, fs_1.mkdirSync)(generatedDirectory, { recursive: true });
131
+ }
132
+ params = req.params;
133
+ if (!domain) return [3 /*break*/, 3];
134
+ return [4 /*yield*/, (0, node_fetch_1.default)(domain + sourceFilename)];
135
+ case 1: return [4 /*yield*/, (_c.sent()).buffer()];
136
+ case 2:
137
+ _a = _c.sent();
138
+ return [3 /*break*/, 4];
139
+ case 3:
140
+ _a = sourceFilename;
141
+ _c.label = 4;
142
+ case 4:
143
+ sourceFile = _a;
144
+ return [4 /*yield*/, (0, sharp_1.default)(sourceFile)
145
+ .rotate()
146
+ .resize({
147
+ width: getWidth(params),
148
+ height: getHeight(params),
149
+ background: getBackground(params),
150
+ fastShrinkOnLoad: false,
151
+ fit: getFit(params),
152
+ position: getPosition(params),
153
+ withoutEnlargement: getWithoutEnlargement(params),
154
+ })];
155
+ case 5:
156
+ pipeline = _c.sent();
157
+ trim = getTrim(params);
158
+ if (trim !== undefined) {
159
+ pipeline.trim(trim);
160
+ }
161
+ switch (params.toFormat) {
162
+ case "webp":
163
+ pipeline.webp({ quality: 60 });
164
+ res.setHeader("Content-Type", "image/webp");
165
+ break;
166
+ case "png":
167
+ pipeline.png({ progressive: true, quality: 60 });
168
+ res.setHeader("Content-Type", "image/png");
169
+ break;
170
+ case "avif":
171
+ pipeline.avif();
172
+ res.setHeader("Content-Type", "image/avif");
173
+ break;
174
+ default:
175
+ pipeline.jpeg({ progressive: true, quality: 60 });
176
+ res.setHeader("Content-Type", "image/jpeg");
177
+ break;
178
+ }
179
+ pipeline
180
+ .withMetadata({ density: 72 })
181
+ .toColorspace("srgb")
182
+ .toFile(generatedFilename)
183
+ .then(function () { return res.sendFile(generatedFilename); })
184
+ .catch(function (e) {
185
+ log(e);
186
+ return res.sendStatus(404);
187
+ });
188
+ return [2 /*return*/];
189
+ }
190
+ });
191
+ }); });
192
+ });
193
+ return middleware;
194
194
  };
195
- exports.createApp = createApp;
195
+ exports.resizerMiddleware = resizerMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/resizer",
3
- "version": "1.2.2",
3
+ "version": "2.0.0",
4
4
  "main": "index.js",
5
5
  "description": "UXF Resizer",
6
6
  "author": "UXFans <dev@uxf.cz>",
@@ -25,7 +25,6 @@
25
25
  "yargs": "^17.2.1"
26
26
  },
27
27
  "devDependencies": {
28
- "@types/crypto-js": "^4.0.2",
29
28
  "@types/express": "^4.17.13",
30
29
  "@types/jest": "^27.0.1",
31
30
  "@types/node-fetch": "^2.5.12",