zet-lib 3.1.3 → 3.1.4

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.
Files changed (2) hide show
  1. package/lib/zAppRouter.js +42 -5
  2. package/package.json +4 -2
package/lib/zAppRouter.js CHANGED
@@ -22,8 +22,25 @@ const pm2 = require("pm2");
22
22
  const zFunction = require("./zFunction");
23
23
  const qs = require("qs");
24
24
  const zip = require("express-zip");
25
- const sharp = require("sharp");
26
25
  const path = require("path");
26
+
27
+ // Lazy load sharp with error handling for cross-platform compatibility
28
+ let sharp = null;
29
+ function getSharp() {
30
+ if (sharp === null) {
31
+ try {
32
+ sharp = require("sharp");
33
+ } catch (error) {
34
+ console.warn("Warning: sharp module is not available. Image processing features will be disabled.");
35
+ console.warn("To enable image processing, install sharp: npm install --include=optional sharp");
36
+ sharp = false; // Mark as unavailable
37
+ }
38
+ }
39
+ if (sharp === false) {
40
+ throw new Error("sharp module is not available. Please install it: npm install --include=optional sharp");
41
+ }
42
+ return sharp;
43
+ }
27
44
  const { Dropbox } = require("dropbox");
28
45
  const fetch = require("isomorphic-fetch");
29
46
  const JSZip = require("jszip");
@@ -1747,6 +1764,15 @@ router.post("/zcompress-dropzone", async (req, res) => {
1747
1764
  io.to(room).emit("errormessage", message);
1748
1765
  res.json(Util.flashError(message));
1749
1766
  } else {
1767
+ // Check if sharp is available
1768
+ try {
1769
+ getSharp();
1770
+ } catch (error) {
1771
+ const message = "Image compression is not available. Please install sharp: npm install --include=optional sharp";
1772
+ io.to(room).emit("errormessage", message);
1773
+ return res.json(Util.flashError(message));
1774
+ }
1775
+
1750
1776
  const config = {
1751
1777
  jpeg: { quality: 60, compressionLevel: 9 },
1752
1778
  webp: { quality: 60, compressionLevel: 9 },
@@ -1782,7 +1808,8 @@ router.post("/zcompress-dropzone", async (req, res) => {
1782
1808
  await wait(timeExcecute); // Wait for file system
1783
1809
  let image;
1784
1810
  try {
1785
- image = sharp(tempPath).resize({
1811
+ const sharpLib = getSharp();
1812
+ image = sharpLib(tempPath).resize({
1786
1813
  width: 1024,
1787
1814
  height: null, // Maintain aspect ratio
1788
1815
  fit: "inside",
@@ -2146,6 +2173,15 @@ router.post("/zcompress-dropbox", async (req, res) => {
2146
2173
  io.to(room).emit("errormessage", message);
2147
2174
  res.json(Util.flashError(message));
2148
2175
  } else {
2176
+ // Check if sharp is available
2177
+ try {
2178
+ getSharp();
2179
+ } catch (error) {
2180
+ const message = "Image compression is not available. Please install sharp: npm install --include=optional sharp";
2181
+ io.to(room).emit("errormessage", message);
2182
+ return res.json(Util.flashError(message));
2183
+ }
2184
+
2149
2185
  let arr = [];
2150
2186
  let files = result[field];
2151
2187
  const count = files.length || 0;
@@ -2162,7 +2198,8 @@ router.post("/zcompress-dropbox", async (req, res) => {
2162
2198
  // Get the file name from the path
2163
2199
  const fileName = file;
2164
2200
  // Process the image with sharp
2165
- const processedImage = await sharp(response.result.fileBinary)
2201
+ const sharpLib = getSharp();
2202
+ const processedImage = await sharpLib(response.result.fileBinary)
2166
2203
  .resize({
2167
2204
  width: 1024,
2168
2205
  height: null, // Maintain aspect ratio
@@ -2173,7 +2210,7 @@ router.post("/zcompress-dropbox", async (req, res) => {
2173
2210
  .then((buffer) => {
2174
2211
  // Check if the file is PNG
2175
2212
  if (fileName.toLowerCase().endsWith(".png")) {
2176
- return sharp(buffer)
2213
+ return sharpLib(buffer)
2177
2214
  .png({
2178
2215
  quality: 60,
2179
2216
  compressionLevel: 9, // Maximum compression for PNG
@@ -2181,7 +2218,7 @@ router.post("/zcompress-dropbox", async (req, res) => {
2181
2218
  .toBuffer();
2182
2219
  } else {
2183
2220
  // For JPEG files
2184
- return sharp(buffer)
2221
+ return sharpLib(buffer)
2185
2222
  .jpeg({
2186
2223
  quality: 60,
2187
2224
  mozjpeg: true, // Better compression
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "3.1.3",
3
+ "version": "3.1.4",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"
@@ -49,12 +49,14 @@
49
49
  "qs": "^6.14.0",
50
50
  "randomstring": "^1.3.1",
51
51
  "read-excel-file": "^5.8.0",
52
- "sharp": "^0.34.1",
53
52
  "socket.io": "^4.8.1",
54
53
  "uglify-js": "^3.19.3",
55
54
  "uuid": "^9.0.1",
56
55
  "xlsx": "^0.18.5"
57
56
  },
57
+ "optionalDependencies": {
58
+ "sharp": "^0.34.1"
59
+ },
58
60
  "overrides": {
59
61
  "unzipper": "^0.12.3",
60
62
  "rimraf": "^6.1.2",