zet-lib 3.1.2 → 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.
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/lib/zdataTable.js CHANGED
@@ -20,10 +20,23 @@ class dataTable {
20
20
  this.types = {}
21
21
  this.roles = myCache.get('ROLES')[datas.zuser.role_id] || {}
22
22
  this.level_approval = this.roles.approvals && this.roles.approvals[datas.routeName] ? this.roles.approvals[datas.routeName] : {}
23
- this.hasLevels = Object.keys(this.level_approval).length == 0 ? false : true
23
+ this.hasLevels = Object.keys(this.level_approval).length > 0
24
24
  this.visiblesKey = this.visibles.map((item) => item.key)
25
25
  this.gridType = datas.gridType || 1
26
26
  this.additionalCss = ''
27
+ this.moneyAttributes = {}
28
+ this.hasMoney = false
29
+ if (this.MYMODEL && this.MYMODEL.widgets) {
30
+ const widgets = this.MYMODEL.widgets
31
+ for (let i = 0; i < this.visiblesKey.length; i++) {
32
+ const item = this.visiblesKey[i]
33
+ const widget = widgets[item]
34
+ if (widget && widget.name == 'money') {
35
+ this.moneyAttributes[item] = widget
36
+ this.hasMoney = true
37
+ }
38
+ }
39
+ }
27
40
  if (this.gridType == 1) {
28
41
  this.dataTableScript = '/modules/datatable/normal.js'
29
42
  } else if (this.gridType == 2) {
@@ -54,7 +67,26 @@ class dataTable {
54
67
  this.relations = obj.relations
55
68
  this.routeName = this.MYMODEL.routeName
56
69
  this.types = obj.TYPES
57
- this.totalFields = this.visiblesKey.filter((item) => this.MYMODEL.widgets[item] && this.MYMODEL.widgets[item].name == 'number')
70
+ this.totalFields = []
71
+ this.moneyAttributes = {}
72
+ this.hasMoney = false
73
+ if (this.MYMODEL && this.MYMODEL.widgets) {
74
+ const widgets = this.MYMODEL.widgets
75
+ for (let i = 0; i < this.visiblesKey.length; i++) {
76
+ const item = this.visiblesKey[i]
77
+ const widget = widgets[item]
78
+ if (widget) {
79
+ const widgetName = widget.name
80
+ if (widgetName == 'number' || widgetName == 'money') {
81
+ this.totalFields.push(item)
82
+ }
83
+ if (widgetName == 'money') {
84
+ this.moneyAttributes[item] = widget
85
+ this.hasMoney = true
86
+ }
87
+ }
88
+ }
89
+ }
58
90
  delete obj.MYMODEL
59
91
  delete obj.relations
60
92
  delete obj.TYPES
@@ -72,9 +104,10 @@ class dataTable {
72
104
  if (this.setColumns) return this.setColumns
73
105
  let html = ''
74
106
  if (Array.isArray(this.visibles)) {
75
- this.visibles.map((item) => {
107
+ for (let i = 0; i < this.visibles.length; i++) {
108
+ const item = this.visibles[i]
76
109
  html += `<th id="data_${item.key}_label">${item.name || ''}</th>`
77
- })
110
+ }
78
111
  } else {
79
112
  for (const key in this.visibles) {
80
113
  html += `<th id="data_${key}_label">${this.visibles[key]}</th>`
@@ -87,9 +120,10 @@ class dataTable {
87
120
  get columnsFilter() {
88
121
  let html = '<tr class="filters">'
89
122
  if (Array.isArray(this.visibles)) {
90
- this.visibles.map((item) => {
123
+ for (let i = 0; i < this.visibles.length; i++) {
124
+ const item = this.visibles[i]
91
125
  html += `<th id="data_${item.key}_label">${this.searchColumns[item.key]}</th>`
92
- })
126
+ }
93
127
  } else {
94
128
  for (const key in this.visibles) {
95
129
  html += `<th id="data_${key}">${this.searchColumns[key]}</th>`
@@ -193,6 +227,9 @@ class dataTable {
193
227
  script += `var dataTableTypes = ${JSON.stringify(this.types, null, 2)};${Util.newLine}`
194
228
  script += `var dataTableRoute = "${this.routeName}";${Util.newLine}`
195
229
  script += `var dataTableHasTotalFields = ${JSON.stringify(this.totalFields)};${Util.newLine}`
230
+ if(this.hasMoney) {
231
+ script += `var dataTableMoneyAttributes = ${JSON.stringify(this.moneyAttributes, null, 2)};${Util.newLine}`
232
+ }
196
233
  script += `</script>${Util.newLine}`
197
234
  script += `<script type="text/javascript" src="${this.dataTableScript}"></script>${Util.newLine}`
198
235
  if (this.searchColumns.FILTERKEY) {
@@ -202,9 +239,10 @@ class dataTable {
202
239
 
203
240
  //additional script for typeahead in grid
204
241
  let typeaheadScript = ''
205
- for (let key in this.MYMODEL.widgets) {
206
- if (this.MYMODEL.widgets[key].name == 'typeahead') {
207
- typeaheadScript += `if($("#${key}").val()){
242
+ if (this.MYMODEL && this.MYMODEL.widgets) {
243
+ for (let key in this.MYMODEL.widgets) {
244
+ if (this.MYMODEL.widgets[key].name == 'typeahead') {
245
+ typeaheadScript += `if($("#${key}").val()){
208
246
  setTimeout(()=>{
209
247
  ajaxPost('/ztypeaheadpost/${this.MYMODEL.table}/${key}', {
210
248
  id: $("#${key}").val()
@@ -217,11 +255,13 @@ class dataTable {
217
255
  },1500)
218
256
  };
219
257
  `
258
+ }
220
259
  }
221
260
  }
222
261
  if (typeaheadScript) {
223
262
  script += `<script type="text/javascript">$(function () {${typeaheadScript}})</script>${Util.newLine}`
224
263
  }
264
+
225
265
  if (this.hasLevels) {
226
266
  //modal-body-approval
227
267
  let APPROVAL_LEVELS = myCache.get('APPROVAL_LEVELS')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "3.1.2",
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",