@websolutespa/bom-mixer-image 1.0.5 → 1.0.7

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 CHANGED
@@ -1,5 +1,19 @@
1
1
  # @websolutespa/bom-mixer-image
2
2
 
3
+ ## 1.0.7
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixing: imageHandler Vercel support
8
+ - Updated dependencies
9
+ - @websolutespa/bom-core@1.7.7
10
+
11
+ ## 1.0.6
12
+
13
+ ### Patch Changes
14
+
15
+ - Modified: imageHandler, re-route not found to origin
16
+
3
17
  ## 1.0.5
4
18
 
5
19
  ### Patch Changes
package/dist/index.js CHANGED
@@ -94,7 +94,10 @@ function imageHandler({ domains = [] }) {
94
94
  response.setHeader("Cache-Control", `public, max-age=${IMAGE_MAX_AGE}`);
95
95
  return response.send(cachedImage);
96
96
  }
97
- const originalImage = await getOriginalImage(url, domains);
97
+ if (url.indexOf("http") === -1 && isVercel()) {
98
+ response.redirect(307, url);
99
+ }
100
+ const originalImage = await getOriginalImage(url, domains, request.headers);
98
101
  if (originalImage) {
99
102
  const resizedImage = (0, import_sharp.default)(originalImage).resize({
100
103
  width,
@@ -117,10 +120,10 @@ function imageHandler({ domains = [] }) {
117
120
  }
118
121
  });
119
122
  }
120
- function getOriginalImage(url, domains) {
121
- return new Promise((resolve2, reject) => {
123
+ function getOriginalImage(url, domains, headers, skipUrlValidity = false) {
124
+ return new Promise((resolve, reject) => {
122
125
  if (url.indexOf("http") === 0) {
123
- if (!checkUrlValidity(url, domains)) {
126
+ if (!skipUrlValidity && !checkUrlValidity(url, domains)) {
124
127
  console.error("imageHandler.getOriginalImage.error invalid url");
125
128
  return reject({ status: 401, message: "invalid url" });
126
129
  }
@@ -135,7 +138,7 @@ function getOriginalImage(url, domains) {
135
138
  return reject({ status: response.status, message: response.statusText });
136
139
  }
137
140
  response.arrayBuffer().then((buffer) => {
138
- return resolve2(Buffer.from(buffer));
141
+ return resolve(Buffer.from(buffer));
139
142
  }).catch((error) => {
140
143
  return reject(error);
141
144
  });
@@ -143,23 +146,23 @@ function getOriginalImage(url, domains) {
143
146
  return reject(error);
144
147
  });
145
148
  } else {
146
- const filePath = path.resolve(".", "public", url);
149
+ const filePath = path.join(process.cwd(), "public", url);
147
150
  fs.readFile(filePath, (error, data) => {
148
151
  if (error) {
149
152
  return reject(error);
150
153
  }
151
- return resolve2(data);
154
+ return resolve(data);
152
155
  });
153
156
  }
154
157
  });
155
158
  }
156
159
  function getCachedImage(pathname) {
157
- return new Promise((resolve2, reject) => {
160
+ return new Promise((resolve, reject) => {
158
161
  fs.readFile(pathname, (error, data) => {
159
162
  if (error) {
160
163
  return reject(error);
161
164
  }
162
- return resolve2(data);
165
+ return resolve(data);
163
166
  });
164
167
  });
165
168
  }
package/dist/index.mjs CHANGED
@@ -58,7 +58,10 @@ function imageHandler({ domains = [] }) {
58
58
  response.setHeader("Cache-Control", `public, max-age=${IMAGE_MAX_AGE}`);
59
59
  return response.send(cachedImage);
60
60
  }
61
- const originalImage = await getOriginalImage(url, domains);
61
+ if (url.indexOf("http") === -1 && isVercel()) {
62
+ response.redirect(307, url);
63
+ }
64
+ const originalImage = await getOriginalImage(url, domains, request.headers);
62
65
  if (originalImage) {
63
66
  const resizedImage = sharp(originalImage).resize({
64
67
  width,
@@ -81,10 +84,10 @@ function imageHandler({ domains = [] }) {
81
84
  }
82
85
  });
83
86
  }
84
- function getOriginalImage(url, domains) {
85
- return new Promise((resolve2, reject) => {
87
+ function getOriginalImage(url, domains, headers, skipUrlValidity = false) {
88
+ return new Promise((resolve, reject) => {
86
89
  if (url.indexOf("http") === 0) {
87
- if (!checkUrlValidity(url, domains)) {
90
+ if (!skipUrlValidity && !checkUrlValidity(url, domains)) {
88
91
  console.error("imageHandler.getOriginalImage.error invalid url");
89
92
  return reject({ status: 401, message: "invalid url" });
90
93
  }
@@ -99,7 +102,7 @@ function getOriginalImage(url, domains) {
99
102
  return reject({ status: response.status, message: response.statusText });
100
103
  }
101
104
  response.arrayBuffer().then((buffer) => {
102
- return resolve2(Buffer.from(buffer));
105
+ return resolve(Buffer.from(buffer));
103
106
  }).catch((error) => {
104
107
  return reject(error);
105
108
  });
@@ -107,23 +110,23 @@ function getOriginalImage(url, domains) {
107
110
  return reject(error);
108
111
  });
109
112
  } else {
110
- const filePath = path.resolve(".", "public", url);
113
+ const filePath = path.join(process.cwd(), "public", url);
111
114
  fs.readFile(filePath, (error, data) => {
112
115
  if (error) {
113
116
  return reject(error);
114
117
  }
115
- return resolve2(data);
118
+ return resolve(data);
116
119
  });
117
120
  }
118
121
  });
119
122
  }
120
123
  function getCachedImage(pathname) {
121
- return new Promise((resolve2, reject) => {
124
+ return new Promise((resolve, reject) => {
122
125
  fs.readFile(pathname, (error, data) => {
123
126
  if (error) {
124
127
  return reject(error);
125
128
  }
126
- return resolve2(data);
129
+ return resolve(data);
127
130
  });
128
131
  });
129
132
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@websolutespa/bom-mixer-image",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Mixer Image module of the BOM Repository",
5
5
  "keywords": [
6
6
  "bom",
@@ -1,3 +1,4 @@
1
+ import { IncomingHttpHeaders } from 'http';
1
2
  // Importing the 'fs' module to work with the file system
2
3
  import * as fs from 'fs';
3
4
  // Importing the 'NextApiRequest' and 'NextApiResponse' from 'next' for handling API requests and responses
@@ -57,8 +58,12 @@ export function imageHandler({ domains = [] }: { domains: string[] }) {
57
58
  // Ending the response and sending the cached image
58
59
  return response.send(cachedImage);
59
60
  }
61
+ // Temporarily fix Vercel local image issue
62
+ if (url.indexOf('http') === -1 && isVercel()) {
63
+ response.redirect(307, url);
64
+ }
60
65
  // Reading the original image
61
- const originalImage = await getOriginalImage(url, domains);
66
+ const originalImage = await getOriginalImage(url, domains, request.headers);
62
67
  // Checking if the original image exists
63
68
  if (originalImage) {
64
69
  // Resizing the original image and converting it to WebP format
@@ -92,12 +97,15 @@ export function imageHandler({ domains = [] }: { domains: string[] }) {
92
97
  }
93
98
 
94
99
  // Defining the 'getOriginalImage' function
95
- function getOriginalImage(url: string, domains: string[]): Promise<Buffer> {
100
+ function getOriginalImage(url: string, domains: string[], headers: IncomingHttpHeaders, skipUrlValidity: boolean = false): Promise<Buffer> {
96
101
  // Returning a new promise
97
102
  return new Promise((resolve, reject) => {
98
103
  // Checking if 'url' starts with 'http'
99
104
  if (url.indexOf('http') === 0) {
100
- if (!checkUrlValidity(url, domains)) {
105
+ if (
106
+ !skipUrlValidity &&
107
+ !checkUrlValidity(url, domains)
108
+ ) {
101
109
  console.error('imageHandler.getOriginalImage.error invalid url');
102
110
  return reject({ status: 401, message: 'invalid url' });
103
111
  }
@@ -125,17 +133,7 @@ function getOriginalImage(url: string, domains: string[]): Promise<Buffer> {
125
133
  });
126
134
  } else {
127
135
  // Reading the image from the file system
128
- // console.log('imageHandler.getOriginalImage.isDevelopment', isDevelopment);
129
- const filePath = path.resolve('.', 'public', url);
130
- /*
131
- const { serverRuntimeConfig } = getConfig();
132
- const filePath = path.join(serverRuntimeConfig.PROJECT_ROOT, 'public', url);
133
- */
134
- /*
135
- const filePath = isDevelopment ?
136
- path.join(process.cwd(), 'public', url) :
137
- path.join(process.cwd(), url);
138
- */
136
+ const filePath = path.join(process.cwd(), 'public', url);
139
137
  // console.log('imageHandler.getOriginalImage.filePath', filePath);
140
138
  fs.readFile(filePath, (error, data) => {
141
139
  if (error) {
@@ -144,6 +142,21 @@ function getOriginalImage(url: string, domains: string[]): Promise<Buffer> {
144
142
  // Resolving the promise with the image data
145
143
  return resolve(data);
146
144
  });
145
+ /*
146
+ // Check if the file exists
147
+ if (fs.existsSync(filePath)) {
148
+ fs.readFile(filePath, (error, data) => {
149
+ if (error) {
150
+ return reject(error);
151
+ }
152
+ // Resolving the promise with the image data
153
+ return resolve(data);
154
+ });
155
+ } else {
156
+ const origin = getOrigin(headers);
157
+ return getOriginalImage(`${origin}${url}`, domains, headers, true);
158
+ }
159
+ */
147
160
  }
148
161
  });
149
162
  }