@websolutespa/bom-mixer-image 1.0.6 → 1.0.8
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/CHANGELOG.md +14 -0
- package/dist/index.js +14 -14
- package/dist/index.mjs +14 -14
- package/package.json +1 -1
- package/src/image/image.handler.ts +22 -22
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @websolutespa/bom-mixer-image
|
|
2
2
|
|
|
3
|
+
## 1.0.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Added: Cache-Control
|
|
8
|
+
|
|
9
|
+
## 1.0.7
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Fixing: imageHandler Vercel support
|
|
14
|
+
- Updated dependencies
|
|
15
|
+
- @websolutespa/bom-core@1.7.7
|
|
16
|
+
|
|
3
17
|
## 1.0.6
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/dist/index.js
CHANGED
|
@@ -38,7 +38,6 @@ module.exports = __toCommonJS(src_exports);
|
|
|
38
38
|
var fs = __toESM(require("fs"));
|
|
39
39
|
var path = __toESM(require("path"));
|
|
40
40
|
var import_sharp = __toESM(require("sharp"));
|
|
41
|
-
var import_bom_core = require("@websolutespa/bom-core");
|
|
42
41
|
|
|
43
42
|
// src/api/api.handler.ts
|
|
44
43
|
function apiHandler(handler) {
|
|
@@ -95,6 +94,9 @@ function imageHandler({ domains = [] }) {
|
|
|
95
94
|
response.setHeader("Cache-Control", `public, max-age=${IMAGE_MAX_AGE}`);
|
|
96
95
|
return response.send(cachedImage);
|
|
97
96
|
}
|
|
97
|
+
if (url.indexOf("http") === -1 && isVercel()) {
|
|
98
|
+
response.redirect(307, url);
|
|
99
|
+
}
|
|
98
100
|
const originalImage = await getOriginalImage(url, domains, request.headers);
|
|
99
101
|
if (originalImage) {
|
|
100
102
|
const resizedImage = (0, import_sharp.default)(originalImage).resize({
|
|
@@ -104,11 +106,14 @@ function imageHandler({ domains = [] }) {
|
|
|
104
106
|
position: import_sharp.default.strategy.entropy,
|
|
105
107
|
withoutEnlargement: true
|
|
106
108
|
}).webp({
|
|
107
|
-
quality
|
|
108
|
-
nearLossless: true
|
|
109
|
+
quality
|
|
110
|
+
// nearLossless: true,
|
|
109
111
|
});
|
|
110
112
|
resizedImage.toFile(cachedName).then(() => console.log("imageHandler.sharp.cached", cachedName)).catch((error) => console.error("error caching image", cachedName, error));
|
|
111
113
|
const data = await resizedImage.toBuffer();
|
|
114
|
+
response.setHeader("Content-Type", "image/webp");
|
|
115
|
+
response.setHeader("Expires", new Date(Date.now() + IMAGE_MAX_AGE * 1e3).toUTCString());
|
|
116
|
+
response.setHeader("Cache-Control", `public, max-age=${IMAGE_MAX_AGE}`);
|
|
112
117
|
return response.end(data);
|
|
113
118
|
}
|
|
114
119
|
} catch (error) {
|
|
@@ -145,17 +150,12 @@ function getOriginalImage(url, domains, headers, skipUrlValidity = false) {
|
|
|
145
150
|
});
|
|
146
151
|
} else {
|
|
147
152
|
const filePath = path.join(process.cwd(), "public", url);
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
});
|
|
155
|
-
} else {
|
|
156
|
-
const origin = (0, import_bom_core.getOrigin)(headers);
|
|
157
|
-
return getOriginalImage(`${origin}${url}`, domains, headers, true);
|
|
158
|
-
}
|
|
153
|
+
fs.readFile(filePath, (error, data) => {
|
|
154
|
+
if (error) {
|
|
155
|
+
return reject(error);
|
|
156
|
+
}
|
|
157
|
+
return resolve(data);
|
|
158
|
+
});
|
|
159
159
|
}
|
|
160
160
|
});
|
|
161
161
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
import * as fs from "fs";
|
|
3
3
|
import * as path from "path";
|
|
4
4
|
import sharp from "sharp";
|
|
5
|
-
import { getOrigin } from "@websolutespa/bom-core";
|
|
6
5
|
|
|
7
6
|
// src/api/api.handler.ts
|
|
8
7
|
function apiHandler(handler) {
|
|
@@ -59,6 +58,9 @@ function imageHandler({ domains = [] }) {
|
|
|
59
58
|
response.setHeader("Cache-Control", `public, max-age=${IMAGE_MAX_AGE}`);
|
|
60
59
|
return response.send(cachedImage);
|
|
61
60
|
}
|
|
61
|
+
if (url.indexOf("http") === -1 && isVercel()) {
|
|
62
|
+
response.redirect(307, url);
|
|
63
|
+
}
|
|
62
64
|
const originalImage = await getOriginalImage(url, domains, request.headers);
|
|
63
65
|
if (originalImage) {
|
|
64
66
|
const resizedImage = sharp(originalImage).resize({
|
|
@@ -68,11 +70,14 @@ function imageHandler({ domains = [] }) {
|
|
|
68
70
|
position: sharp.strategy.entropy,
|
|
69
71
|
withoutEnlargement: true
|
|
70
72
|
}).webp({
|
|
71
|
-
quality
|
|
72
|
-
nearLossless: true
|
|
73
|
+
quality
|
|
74
|
+
// nearLossless: true,
|
|
73
75
|
});
|
|
74
76
|
resizedImage.toFile(cachedName).then(() => console.log("imageHandler.sharp.cached", cachedName)).catch((error) => console.error("error caching image", cachedName, error));
|
|
75
77
|
const data = await resizedImage.toBuffer();
|
|
78
|
+
response.setHeader("Content-Type", "image/webp");
|
|
79
|
+
response.setHeader("Expires", new Date(Date.now() + IMAGE_MAX_AGE * 1e3).toUTCString());
|
|
80
|
+
response.setHeader("Cache-Control", `public, max-age=${IMAGE_MAX_AGE}`);
|
|
76
81
|
return response.end(data);
|
|
77
82
|
}
|
|
78
83
|
} catch (error) {
|
|
@@ -109,17 +114,12 @@ function getOriginalImage(url, domains, headers, skipUrlValidity = false) {
|
|
|
109
114
|
});
|
|
110
115
|
} else {
|
|
111
116
|
const filePath = path.join(process.cwd(), "public", url);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
});
|
|
119
|
-
} else {
|
|
120
|
-
const origin = getOrigin(headers);
|
|
121
|
-
return getOriginalImage(`${origin}${url}`, domains, headers, true);
|
|
122
|
-
}
|
|
117
|
+
fs.readFile(filePath, (error, data) => {
|
|
118
|
+
if (error) {
|
|
119
|
+
return reject(error);
|
|
120
|
+
}
|
|
121
|
+
return resolve(data);
|
|
122
|
+
});
|
|
123
123
|
}
|
|
124
124
|
});
|
|
125
125
|
}
|
package/package.json
CHANGED
|
@@ -8,7 +8,6 @@ import * as path from 'path';
|
|
|
8
8
|
// Importing 'sharp' for image processing
|
|
9
9
|
import sharp from 'sharp';
|
|
10
10
|
// Importing 'apiHandler' from '../api/api.handler'
|
|
11
|
-
import { getOrigin } from '@websolutespa/bom-core';
|
|
12
11
|
import { apiHandler } from '../api/api.handler';
|
|
13
12
|
|
|
14
13
|
const IMAGE_MAX_AGE = 604800;
|
|
@@ -59,6 +58,10 @@ export function imageHandler({ domains = [] }: { domains: string[] }) {
|
|
|
59
58
|
// Ending the response and sending the cached image
|
|
60
59
|
return response.send(cachedImage);
|
|
61
60
|
}
|
|
61
|
+
// Temporarily fix Vercel local image issue
|
|
62
|
+
if (url.indexOf('http') === -1 && isVercel()) {
|
|
63
|
+
response.redirect(307, url);
|
|
64
|
+
}
|
|
62
65
|
// Reading the original image
|
|
63
66
|
const originalImage = await getOriginalImage(url, domains, request.headers);
|
|
64
67
|
// Checking if the original image exists
|
|
@@ -74,14 +77,21 @@ export function imageHandler({ domains = [] }: { domains: string[] }) {
|
|
|
74
77
|
})
|
|
75
78
|
.webp({
|
|
76
79
|
quality,
|
|
77
|
-
nearLossless: true,
|
|
80
|
+
// nearLossless: true,
|
|
78
81
|
});
|
|
79
82
|
// Saving the resized image to the cache
|
|
80
83
|
resizedImage.toFile(cachedName)
|
|
81
84
|
.then(() => console.log('imageHandler.sharp.cached', cachedName))
|
|
82
|
-
.catch(error => console.error('error caching image', cachedName, error));
|
|
83
|
-
// Converting the resized image to a buffer
|
|
85
|
+
.catch((error: any) => console.error('error caching image', cachedName, error));
|
|
86
|
+
// Converting the resized image to a buffer
|
|
84
87
|
const data = await resizedImage.toBuffer();
|
|
88
|
+
// Setting the 'Content-Type' header to 'image/webp'
|
|
89
|
+
response.setHeader('Content-Type', 'image/webp');
|
|
90
|
+
// Setting the 'Expires' header to the current time plus the maximum age of the image (in seconds)
|
|
91
|
+
response.setHeader('Expires', new Date(Date.now() + IMAGE_MAX_AGE * 1000).toUTCString());
|
|
92
|
+
// Setting the 'Cache-Control' header to 'public' and setting 'max-age' to the maximum age of the image
|
|
93
|
+
response.setHeader('Cache-Control', `public, max-age=${IMAGE_MAX_AGE}`);
|
|
94
|
+
// Sending data in the response
|
|
85
95
|
return response.end(data);
|
|
86
96
|
}
|
|
87
97
|
} catch (error) {
|
|
@@ -130,18 +140,16 @@ function getOriginalImage(url: string, domains: string[], headers: IncomingHttpH
|
|
|
130
140
|
});
|
|
131
141
|
} else {
|
|
132
142
|
// Reading the image from the file system
|
|
133
|
-
// console.log('imageHandler.getOriginalImage.isDevelopment', isDevelopment);
|
|
134
143
|
const filePath = path.join(process.cwd(), 'public', url);
|
|
135
|
-
/*
|
|
136
|
-
const { serverRuntimeConfig } = getConfig();
|
|
137
|
-
const filePath = path.join(serverRuntimeConfig.PROJECT_ROOT, 'public', url);
|
|
138
|
-
*/
|
|
139
|
-
/*
|
|
140
|
-
const filePath = isDevelopment ?
|
|
141
|
-
path.join(process.cwd(), 'public', url) :
|
|
142
|
-
path.join(process.cwd(), url);
|
|
143
|
-
*/
|
|
144
144
|
// console.log('imageHandler.getOriginalImage.filePath', filePath);
|
|
145
|
+
fs.readFile(filePath, (error, data) => {
|
|
146
|
+
if (error) {
|
|
147
|
+
return reject(error);
|
|
148
|
+
}
|
|
149
|
+
// Resolving the promise with the image data
|
|
150
|
+
return resolve(data);
|
|
151
|
+
});
|
|
152
|
+
/*
|
|
145
153
|
// Check if the file exists
|
|
146
154
|
if (fs.existsSync(filePath)) {
|
|
147
155
|
fs.readFile(filePath, (error, data) => {
|
|
@@ -155,14 +163,6 @@ function getOriginalImage(url: string, domains: string[], headers: IncomingHttpH
|
|
|
155
163
|
const origin = getOrigin(headers);
|
|
156
164
|
return getOriginalImage(`${origin}${url}`, domains, headers, true);
|
|
157
165
|
}
|
|
158
|
-
/*
|
|
159
|
-
fs.readFile(filePath, (error, data) => {
|
|
160
|
-
if (error) {
|
|
161
|
-
return reject(error);
|
|
162
|
-
}
|
|
163
|
-
// Resolving the promise with the image data
|
|
164
|
-
return resolve(data);
|
|
165
|
-
});
|
|
166
166
|
*/
|
|
167
167
|
}
|
|
168
168
|
});
|