Skip to content

Commit b6000c9

Browse files
committed
fix: zipper performance and bug fixes
1 parent a7ff4b3 commit b6000c9

3 files changed

Lines changed: 84 additions & 116 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@jotforminc/zenith",
33
"packageManager": "pnpm@7.18.0",
4-
"version": "1.1.3",
4+
"version": "1.2.0",
55
"description": "",
66
"main": "./build/index.js",
77
"files": [

src/classes/Cache/Cacher.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,26 @@ export default abstract class Cacher {
7676
});
7777
}
7878

79-
cacheZip(cachePath: string, output: string, directoryPath: string) {
80-
return new Promise<void>((resolve, reject) => {
81-
Zipper.zip(directoryPath, (error: Error | null, zipped: ZipExporter) => {
82-
if (error) {
83-
Logger.log(2, 'ERROR => ', error);
84-
reject(error);
85-
return;
79+
async cacheZip(cachePath: string, output: string, directoryPath: string) {
80+
await Zipper.zip(directoryPath, (error: Error | null, zipped: ZipExporter) => {
81+
if (error) {
82+
Logger.log(2, 'ERROR CZ-1 => ', error);
83+
return;
84+
}
85+
zipped.compress();
86+
zipped.memory().then((buff) => {
87+
if (buff instanceof Buffer) {
88+
this.putObject({
89+
Key: `${cachePath}/${output}.zip`,
90+
Body: buff
91+
}).then(() => {
92+
Logger.log(3, 'Zip Cache successfully stored');
93+
}).catch((err) => {
94+
Logger.log(2, err);
95+
});
8696
}
87-
zipped.compress();
88-
zipped.memory().then((buff) => {
89-
if (buff instanceof Buffer) {
90-
this.putObject(
91-
{
92-
Key: `${cachePath}/${output}.zip`,
93-
Body: buff
94-
}).then(() => {
95-
Logger.log(3, 'Zip Cache successfully stored');
96-
resolve();
97-
}).catch((err) => {
98-
Logger.log(2, err);
99-
reject(err);
100-
});
101-
}
102-
}).catch((err) => {
103-
Logger.log(2, err);
104-
reject(err);
105-
});
97+
}).catch((err) => {
98+
Logger.log(2, err);
10699
});
107100
});
108101
}

src/classes/Zipper.js

Lines changed: 64 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,112 +3,87 @@ import fs from 'fs';
33
import path from 'path';
44
import JSZip from 'jszip';
55
import ZipExporter from '../libs/zipExporter';
6-
import UnzippedReader from '../libs/UnzippedReader';
7-
import { Readable } from 'stream';
86

97
JSZip.make = () => {
10-
const augment = (_opts) => {
11-
const opts = _opts || {};
12-
opts.createFolders = opts.createFolders || true;
13-
return opts;
14-
}
8+
const augment = (_opts) => {
9+
const opts = _opts || {};
10+
opts.createFolders = opts.createFolders || true;
11+
return opts;
12+
};
1513

16-
const instance = new JSZip();
17-
const originals = {
18-
loadAsync: instance.loadAsync,
19-
file: instance.file,
20-
};
14+
const instance = new JSZip();
15+
const originals = {
16+
loadAsync: instance.loadAsync,
17+
file: instance.file,
18+
};
2119

22-
instance.loadAsync = (data, options) => {
23-
return originals.loadAsync.call(instance, data, augment(options));
24-
};
20+
instance.loadAsync = (data, options) => {
21+
return originals.loadAsync.call(instance, data, augment(options));
22+
};
2523

26-
instance.file = (name, content, options) => {
27-
if (!content) {
28-
return originals.file.call(instance, name);
29-
}
30-
return originals.file.call(instance, name, content, augment(options));
31-
};
32-
return instance;
24+
instance.file = (name, content, options) => {
25+
if (!content) {
26+
return originals.file.call(instance, name);
27+
}
28+
return originals.file.call(instance, name, content, augment(options));
29+
};
30+
return instance;
3331
};
3432

3533
const zipDir = async (dir, zippedDir) => {
36-
return new Promise((resolve, reject) => {
37-
fs.readdir(dir, (err, files) => {
38-
if (err) {
39-
reject(err);
40-
}
41-
const filePaths = files.map(file => path.join(dir, file));
42-
const typedFiles = filePaths.reduce((acc, filePath) => {
43-
const stats = fs.statSync(filePath);
44-
acc.push({
45-
path: filePath,
46-
isDirectory: stats.isDirectory() ? 'dir' : 'file',
47-
});
48-
return acc;
49-
}, []);
50-
for (const entry of typedFiles) {
51-
const parsedEntryPath = path.parse(entry.path);
52-
if (entry.type === 'dir') {
53-
const newZippedDir = zippedDir.folder(parsedEntryPath.base);
54-
zipDir(entry.path, newZippedDir).then(console.log);
55-
}
56-
else {
57-
fs.readFile(entry.path, (err, data) => {
58-
if (err) {
59-
reject(err);
60-
}
61-
zippedDir.file(parsedEntryPath.base, data);
62-
});
63-
}
64-
}
65-
resolve();
66-
});
34+
const files = await fs.promises.readdir(dir);
35+
const filePaths = files.map(file => path.join(dir, file));
36+
const typedFiles = [];
37+
for (const filePath of filePaths) {
38+
const stats = await fs.promises.stat(filePath);
39+
typedFiles.push({
40+
path: filePath,
41+
type: stats.isDirectory() ? 'dir' : 'file',
6742
});
68-
}
69-
70-
const zipDirSync = (dir, zippedDir) => {
71-
const entries = fs.readdirSync(dir);
72-
73-
for (const entry of entries) {
74-
const entryNormalizedPath = path.normalize(path.join(dir, entry));
75-
const stats = fs.statSync(entryNormalizedPath);
76-
if (stats.isDirectory()) {
77-
const newZippedDir = zippedDir.folder(entry);
78-
zipDirSync(entryNormalizedPath, newZippedDir);
79-
}
80-
else {
81-
const data = fs.readFileSync(entryNormalizedPath);
82-
zippedDir.file(entry, data);
83-
}
43+
}
44+
for (const entry of typedFiles) {
45+
const parsedEntryPath = path.parse(entry.path);
46+
if (entry.type === 'dir') {
47+
zippedDir.folder(parsedEntryPath.base);
48+
await zipDir(entry.path, zippedDir);
8449
}
85-
return zippedDir;
86-
}
50+
else {
51+
const data = await fs.promises.readFile(entry.path);
52+
zippedDir.file(parsedEntryPath.base, data.toString());
53+
}
54+
}
55+
};
8756

8857
const Zipper = {};
8958

90-
Zipper.zip = (entity, _callback, _shiftedCallback) => {
91-
const zippedObj = JSZip.make();
92-
if (entity instanceof Buffer) {
93-
// entity is a buffer containing a file, _callback is the name of the buffer
94-
const callback = _shiftedCallback || (() => {});
95-
zippedObj.file(_callback, entity);
96-
callback(null, new ZipExporter(zippedObj, false, true));
97-
}
98-
else if (entity instanceof UnzippedReader) {
99-
// entity is an unzipped file
100-
const callback = _callback || (() => {});
101-
callback(null, new ZipExporter(entity.unzipped_file, false, true));
59+
Zipper.zip = async (entity, _callback, _shiftedCallback) => {
60+
const zippedObj = JSZip.make();
61+
if (typeof entity === 'string') {
62+
// entity is a path to a file/directory
63+
const callback = _callback || (() => {});
64+
const normalizedPath = path.normalize(entity);
65+
const stats = await fs.promises.stat(normalizedPath);
66+
if (stats.isDirectory()) {
67+
await zipDir(normalizedPath, zippedObj);
68+
callback(null, new ZipExporter(zippedObj, false, true));
69+
return;
10270
}
10371
else {
104-
_callback(new Error('Invalid entity type'));
72+
const parsedPath = path.parse(normalizedPath);
73+
const data = await fs.promises.readFile(normalizedPath);
74+
zippedObj.file(parsedPath.base, data);
75+
callback(null, new ZipExporter(zippedObj, false, true));
10576
}
77+
}
78+
else {
79+
_callback(new Error('Invalid entity type'));
80+
}
10681
};
10782

10883
Zipper.unzip = async (entity) => {
109-
const zippedObj = JSZip.make();
110-
await zippedObj.loadAsync(entity);
111-
return new ZipExporter(zippedObj, true, true);
112-
}
84+
const zippedObj = JSZip.make();
85+
await zippedObj.loadAsync(entity);
86+
return new ZipExporter(zippedObj, true, true);
87+
};
11388

114-
export default Zipper;
89+
export default Zipper;

0 commit comments

Comments
 (0)