Skip to content

Commit e1fa7ca

Browse files
cyfung1031CodFrm
andauthored
♻️ 使用 JSZipp 替换 jszip 处理 ZIP 文件 (#1479)
* refactor: 使用 fflate 替换 jszip 处理 ZIP 文件 * change to JSZipp * JSZipp@1.0.5 * JSZipp@1.0.6 * update * JSZipp@1.0.8 --------- Co-authored-by: 王一之 <yz@ggnb.top>
1 parent 04fe882 commit e1fa7ca

6 files changed

Lines changed: 208 additions & 123 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"react-router-dom": "^7.13.0",
5353
"string-similarity-js": "^2.1.4",
5454
"uuid": "^11.1.0",
55+
"web-jszipp": "^1.0.8",
5556
"webdav": "^5.9.0",
5657
"yaml": "^2.8.3"
5758
},
@@ -86,7 +87,6 @@
8687
"husky": "^9.1.7",
8788
"iconv-lite": "^0.7.2",
8889
"jsdom": "^26.1.0",
89-
"jszip": "^3.10.1",
9090
"mock-xmlhttprequest": "^8.4.1",
9191
"postcss": "^8.5.6",
9292
"postcss-loader": "^8.2.0",

packages/filesystem/zip/rw.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { JSZipObject } from "jszip";
2-
import type { JSZipFileOptions, JSZipFile } from "@App/pkg/utils/jszip-x";
1+
import type { JSZipFileOptions, JSZipFile, JSZipObject } from "@App/pkg/utils/jszip-x";
32
import type { FileCreateOptions, FileReader, FileWriter } from "../filesystem";
43

54
export class ZipFileReader implements FileReader {
@@ -29,13 +28,13 @@ export class ZipFileWriter implements FileWriter {
2928
}
3029
}
3130

32-
async write(content: string): Promise<void> {
31+
async write(content: string | Blob): Promise<void> {
3332
const opts = {} as JSZipFileOptions;
3433
if (this.modifiedDate) {
35-
const date = new Date(this.modifiedDate);
36-
const dateWithOffset = new Date(date.getTime() - date.getTimezoneOffset() * 60000);
37-
opts.date = dateWithOffset;
34+
opts.date = new Date(this.modifiedDate);
35+
// jszipp does not require timezone adjustment to UTC Date
3836
}
39-
this.zip.file(this.path, content, opts);
37+
const fileData = typeof content === "string" ? content : new Uint8Array(await content.arrayBuffer());
38+
this.zip.file(this.path, fileData, opts);
4039
}
4140
}

packages/filesystem/zip/zip.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ export default class ZipFileSystem implements FileSystem {
4747
const files: FileInfo[] = [];
4848
for (const [filename, jsZipObject] of Object.entries(this.zip.files)) {
4949
const date = jsZipObject.date; // the last modification date
50-
const dateWithOffset = new Date(date.getTime() + date.getTimezoneOffset() * 60000);
51-
const lastModificationDate = dateWithOffset.getTime();
50+
const lastModificationDate = date.getTime();
5251
files.push({
5352
name: filename,
5453
path: filename,

pnpm-lock.yaml

Lines changed: 9 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/pack.js

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* global process */
22
import { promises as fs } from "fs";
3-
import { createWriteStream } from "fs";
4-
import JSZip from "jszip";
3+
import { ZipWriter } from "web-jszipp";
54
import ChromeExtension from "crx";
65
import { execSync } from "child_process";
76
import manifest from "../src/manifest.json" with { type: "json" };
@@ -16,12 +15,14 @@ const PACK_FIREFOX = false;
1615

1716
// ============================================================================
1817

19-
const createJSZip = () => {
20-
const currDate = new Date();
21-
const dateWithOffset = new Date(currDate.getTime() - currDate.getTimezoneOffset() * 60000);
22-
// replace the default date with dateWithOffset
23-
JSZip.defaults.date = dateWithOffset;
24-
return new JSZip();
18+
const zipMtime = new Date();
19+
20+
const addZipFile = async (zip, path, content) => {
21+
await zip.add({
22+
path,
23+
data: content,
24+
meta: { modifiedAt: zipMtime },
25+
});
2526
};
2627

2728
// 判断是否为beta版本
@@ -113,8 +114,8 @@ firefoxManifest.optional_permissions = firefoxManifest.optional_permissions?.fil
113114
(permission) => permission !== "background"
114115
);
115116

116-
const chrome = createJSZip();
117-
const firefox = createJSZip();
117+
const chrome = new ZipWriter({ outputAs: "uint8array" });
118+
const firefox = new ZipWriter({ outputAs: "uint8array" });
118119

119120
async function addDir(zip, localDir, toDir, filters) {
120121
const sub = async (localDir, toDir) => {
@@ -129,38 +130,26 @@ async function addDir(zip, localDir, toDir, filters) {
129130
if (stats.isDirectory()) {
130131
await sub(localPath, `${toPath}/`);
131132
} else {
132-
zip.file(toPath, await fs.readFile(localPath));
133+
await addZipFile(zip, toPath, await fs.readFile(localPath));
133134
}
134135
}
135136
};
136137
await sub(localDir, toDir);
137138
}
138139

139-
chrome.file("manifest.json", JSON.stringify(chromeManifest));
140-
firefox.file("manifest.json", JSON.stringify(firefoxManifest));
140+
await addZipFile(chrome, "manifest.json", JSON.stringify(chromeManifest));
141+
await addZipFile(firefox, "manifest.json", JSON.stringify(firefoxManifest));
141142

142143
await Promise.all([
143144
addDir(chrome, "./dist/ext", "", ["manifest.json"]),
144145
addDir(firefox, "./dist/ext", "", ["manifest.json"]),
145146
]);
146147

147148
// 导出zip包
148-
chrome
149-
.generateNodeStream({
150-
type: "nodebuffer",
151-
streamFiles: true,
152-
compression: "DEFLATE",
153-
})
154-
.pipe(createWriteStream(`./dist/${packageInfo.name}-v${packageInfo.version}-chrome.zip`));
149+
await fs.writeFile(`./dist/${packageInfo.name}-v${packageInfo.version}-chrome.zip`, await chrome.close());
155150

156151
PACK_FIREFOX &&
157-
firefox
158-
.generateNodeStream({
159-
type: "nodebuffer",
160-
streamFiles: true,
161-
compression: "DEFLATE",
162-
})
163-
.pipe(createWriteStream(`./dist/${packageInfo.name}-v${packageInfo.version}-firefox.zip`));
152+
(await fs.writeFile(`./dist/${packageInfo.name}-v${packageInfo.version}-firefox.zip`, await firefox.close()));
164153

165154
// 处理crx
166155
const crx = new ChromeExtension({

0 commit comments

Comments
 (0)