/
zip.ts
154 lines (136 loc) · 3.87 KB
/
zip.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { Stats } from 'fs';
import { join, normalize, relative } from 'path';
import { ZipFile } from 'yazl';
import {
readdirAsync,
safeCreateReadStream,
safeCreateWriteStream,
statAsync,
} from './fs';
import { StringStream } from './string-stream';
import { streamToUInt8Array } from './string-to-uint';
export type TransformAsyncCode = (code: Uint8Array) => Promise<string>;
export interface ZipArtifact {
path: string;
/**
* @deprecated It will be removed in the next versions because it is not used
*/
name: string;
metadataPath?: string;
type: 'file' | 'directory';
shouldIgnore?: (fileName: string) => boolean;
transformer?: (
filePath: string,
metadataPath: string,
) => TransformAsyncCode | undefined;
}
export class FasterZip {
public async run(
rootPath: string,
outputPath: string,
zipArtifacts: ZipArtifact[],
): Promise<void> {
await new Promise<void>((resolve, reject) => {
(async () => {
const zipfile = new ZipFile();
const stream = safeCreateWriteStream(outputPath).once('error', reject);
zipfile.outputStream.pipe(stream);
for (const artifact of zipArtifacts) {
await this.handleArtifact(artifact, zipfile, rootPath, reject).catch(
reject,
);
}
zipfile.end();
stream.once('error', reject).once('close', () => resolve());
})();
});
}
protected async readdirAndAddToZip(
zipFile: ZipFile,
rootPath: string,
source: ZipArtifact,
path: string,
onErrorOnStream: (reason?: any) => void,
) {
const filePaths = await readdirAsync(path).then(files =>
files.map(file => join(path, file)),
);
const filePathsAndStats: [string, Stats][] = await Promise.all(
filePaths.map(async filePath => [filePath, await statAsync(filePath)]),
);
await Promise.all(
filePathsAndStats.map(async ([filePath, stat]) => {
if (stat.isFile()) {
if (source.shouldIgnore && source.shouldIgnore(filePath)) return;
await this.addFileToZip(
zipFile,
source,
rootPath,
filePath,
onErrorOnStream,
);
} else {
await this.readdirAndAddToZip(
zipFile,
rootPath,
source,
filePath,
onErrorOnStream,
);
}
}),
);
}
protected async addFileToZip(
zipFile: ZipFile,
source: ZipArtifact,
rootPath: string,
filePath: string,
onErrorOnStream: (reason?: any) => void,
): Promise<void> {
const metadataPath = normalize(
source.metadataPath
? filePath.replace(source.path, source.metadataPath)
: relative(rootPath, filePath),
);
const transformer =
source.transformer && source.transformer(filePath, metadataPath);
const readStream = safeCreateReadStream(filePath).once('error', err =>
onErrorOnStream(err),
);
if (transformer) {
try {
const code = await streamToUInt8Array(readStream);
const finalContent = await transformer(code);
const fileContentReadable = new StringStream(finalContent);
zipFile.addReadStream(fileContentReadable, metadataPath);
} catch (e) {
zipFile.addReadStream(readStream, metadataPath);
}
} else zipFile.addReadStream(readStream, metadataPath);
}
protected async handleArtifact(
artifact: ZipArtifact,
zipfile: ZipFile,
rootPath: string,
onErrorOnStream: (reason?: any) => void,
): Promise<void> {
if (artifact.type === 'directory') {
await this.readdirAndAddToZip(
zipfile,
rootPath,
artifact,
artifact.path,
onErrorOnStream,
);
} else {
await this.addFileToZip(
zipfile,
artifact,
rootPath,
artifact.path,
onErrorOnStream,
);
}
}
}