b2b-platform-utils 1.1.58 → 1.1.60

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/fileSystem.js CHANGED
@@ -165,6 +165,89 @@ async function unlinkIfExists(fileName) {
165
165
  // Public API: MIME / extension helpers
166
166
  // -----------------------------------------------------------------------------
167
167
 
168
+ const MIME_TO_EXTENSION = {
169
+ "image/avif": "avif",
170
+ "image/png": "png",
171
+ "image/jpeg": "jpeg",
172
+ "image/jpg": "jpg",
173
+ "image/svg+xml": "svg",
174
+ "image/webp": "webp",
175
+ "image/gif": "gif",
176
+ "image/vnd.microsoft.icon": "ico",
177
+ "image/x-icon": "ico",
178
+ "image/bmp": "bmp",
179
+ "image/tiff": "tiff",
180
+ "font/ttf": "ttf",
181
+ "font/otf": "otf",
182
+ "font/woff": "woff",
183
+ "font/woff2": "woff2",
184
+ "application/font-woff": "woff",
185
+ "application/font-woff2": "woff2",
186
+ "text/plain": "txt",
187
+ "text/csv": "csv",
188
+ "application/json": "json",
189
+ "application/xml": "xml",
190
+ "text/xml": "xml",
191
+ "application/pdf": "pdf",
192
+ "application/msword": "doc",
193
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
194
+ "application/vnd.ms-excel": "xls",
195
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
196
+ "application/vnd.ms-powerpoint": "ppt",
197
+ "application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx",
198
+ "application/zip": "zip",
199
+ "application/x-zip-compressed": "zip",
200
+ "application/x-rar-compressed": "rar",
201
+ "application/vnd.rar": "rar",
202
+ "application/x-7z-compressed": "7z",
203
+ "application/gzip": "gz",
204
+ "video/mp4": "mp4",
205
+ "video/webm": "webm",
206
+ "video/quicktime": "mov",
207
+ "audio/mpeg": "mp3",
208
+ "audio/wav": "wav",
209
+ "audio/webm": "webm",
210
+ "audio/ogg": "ogg",
211
+ };
212
+
213
+ const EXTENSION_TO_MIME = {
214
+ avif: "image/avif",
215
+ png: "image/png",
216
+ gif: "image/gif",
217
+ jpeg: "image/jpeg",
218
+ jpg: "image/jpeg",
219
+ webp: "image/webp",
220
+ ico: "image/vnd.microsoft.icon",
221
+ svg: "image/svg+xml",
222
+ bmp: "image/bmp",
223
+ tif: "image/tiff",
224
+ tiff: "image/tiff",
225
+ otf: "font/otf",
226
+ woff: "font/woff",
227
+ woff2: "font/woff2",
228
+ csv: "text/csv",
229
+ txt: "text/plain",
230
+ json: "application/json",
231
+ xml: "application/xml",
232
+ pdf: "application/pdf",
233
+ xls: "application/vnd.ms-excel",
234
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
235
+ doc: "application/msword",
236
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
237
+ ppt: "application/vnd.ms-powerpoint",
238
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
239
+ zip: "application/zip",
240
+ rar: "application/vnd.rar",
241
+ "7z": "application/x-7z-compressed",
242
+ gz: "application/gzip",
243
+ mp4: "video/mp4",
244
+ webm: "video/webm",
245
+ mov: "video/quicktime",
246
+ mp3: "audio/mpeg",
247
+ wav: "audio/wav",
248
+ ogg: "audio/ogg",
249
+ };
250
+
168
251
  /**
169
252
  * Map known MIME types to file extensions.
170
253
  * Unknown types return extension = null and also return an error message.
@@ -174,58 +257,19 @@ async function unlinkIfExists(fileName) {
174
257
  * @returns {{ extension: string|null, error: string }}
175
258
  */
176
259
  function getImageExtension(mimetype) {
177
- let extension = null;
178
- let error = "";
179
-
180
- switch (mimetype) {
181
- case "image/png":
182
- extension = "png";
183
- break;
184
- case "image/jpeg":
185
- extension = "jpeg";
186
- break;
187
- case "image/jpg":
188
- extension = "jpg";
189
- break;
190
- case "image/svg+xml":
191
- extension = "svg";
192
- break;
193
- case "image/webp":
194
- extension = "webp";
195
- break;
196
- case "image/gif":
197
- extension = "gif";
198
- break;
199
- case "image/vnd.microsoft.icon":
200
- extension = "ico";
201
- break;
202
- case "font/ttf":
203
- extension = "ttf";
204
- break;
205
- case "text/plain":
206
- extension = "txt";
207
- break;
208
- case "text/csv":
209
- extension = "csv";
210
- break;
211
- case "application/pdf":
212
- extension = "pdf";
213
- break;
214
- case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
215
- extension = "xlsx";
216
- break;
217
- default:
218
- error = `Undefined file mimetype: ${mimetype}`;
219
- extension = null;
220
-
221
- // Emit a non-fatal warning. Service can still decide what to do.
222
- if (typeof warningNote === "function") {
223
- warningNote(error);
224
- }
225
- break;
260
+ const extension = MIME_TO_EXTENSION[mimetype] ?? null;
261
+
262
+ if (extension !== null) {
263
+ return { extension, error: "" };
226
264
  }
227
265
 
228
- return { extension, error };
266
+ const error = `Undefined file mimetype: ${mimetype}`;
267
+
268
+ if (typeof warningNote === "function") {
269
+ warningNote(error);
270
+ }
271
+
272
+ return { extension: null, error };
229
273
  }
230
274
 
231
275
  /**
@@ -237,42 +281,7 @@ function getImageExtension(mimetype) {
237
281
  */
238
282
  function getMimeTypeByFileExtension(filename) {
239
283
  const ext = extractCleanExtension(filename);
240
-
241
- switch (ext) {
242
- case "png":
243
- return "image/png";
244
- case "gif":
245
- return "image/gif";
246
- case "jpeg":
247
- case "jpg":
248
- return "image/jpeg";
249
- case "webp":
250
- return "image/webp";
251
- case "ico":
252
- return "image/vnd.microsoft.icon";
253
- case "svg":
254
- return "image/svg+xml";
255
- case "csv":
256
- return "text/csv";
257
- case "txt":
258
- return "text/plain";
259
- case "xml":
260
- return "application/xml";
261
- case "pdf":
262
- return "application/pdf";
263
- case "xls":
264
- return "application/vnd.ms-excel";
265
- case "xlsx":
266
- return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
267
- case "doc":
268
- return "application/msword";
269
- case "docx":
270
- return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
271
- case "mp4":
272
- return "video/mp4";
273
- default:
274
- return "";
275
- }
284
+ return EXTENSION_TO_MIME[ext] || "";
276
285
  }
277
286
 
278
287
  // -----------------------------------------------------------------------------
package/optimizeMedia.js CHANGED
@@ -190,8 +190,8 @@ async function optimizeImageBufferByFormat(fileDataBuffer, originalFileType, opt
190
190
  let normalizedType = normalizeImageFileType(originalFileType);
191
191
 
192
192
  try {
193
- if (normalizedType === 'gif') {
194
- return { buffer: fileDataBuffer, fileType: 'gif', optimized: false };
193
+ if (normalizedType === 'gif' || normalizedType === 'avif') {
194
+ return { buffer: fileDataBuffer, fileType: normalizedType, optimized: false };
195
195
  }
196
196
 
197
197
  if (!normalizedType || !isKnownFormat(normalizedType)) {
@@ -203,29 +203,8 @@ async function optimizeImageBufferByFormat(fileDataBuffer, originalFileType, opt
203
203
  }
204
204
  }
205
205
 
206
- if (normalizedType === 'gif') {
207
- return { buffer: fileDataBuffer, fileType: 'gif', optimized: false };
208
- }
209
-
210
- if (normalizedType === 'avif') {
211
- try {
212
- let pipeline = sharp(fileDataBuffer);
213
-
214
- if (hasResizeOptions(opts)) {
215
- pipeline = pipeline.resize(buildResizeOptions(opts));
216
- }
217
-
218
- const buffer = await pipeline
219
- .avif({
220
- quality: opts.quality,
221
- effort: opts.effort
222
- })
223
- .toBuffer();
224
-
225
- return { buffer, fileType: 'avif', optimized: true };
226
- } catch {
227
- return { buffer: fileDataBuffer, fileType: 'avif', optimized: false };
228
- }
206
+ if (normalizedType === 'gif' || normalizedType === 'avif') {
207
+ return { buffer: fileDataBuffer, fileType: normalizedType, optimized: false };
229
208
  }
230
209
 
231
210
  if (WEBP_CONVERSION_FORMATS.has(normalizedType)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "b2b-platform-utils",
3
- "version": "1.1.58",
3
+ "version": "1.1.60",
4
4
  "description": "Shared utilities for Node.js microservices: errors map, local cache, logger, numbers, dates, filesystem, media optimization, paginator, slugger, crypto wrapper, sanitize HTML, sorting.",
5
5
  "type": "commonjs",
6
6
  "license": "KingSizer",