-
Notifications
You must be signed in to change notification settings - Fork 251
/
filesystem.ts
214 lines (183 loc) · 5.73 KB
/
filesystem.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import * as os from 'os';
import * as path from 'path';
import { promisify } from 'util';
import * as stream from 'stream';
import { FileIntegrity, getFileIntegrity } from './integrity';
import fs from './wrapped-fs';
import { CrawledFileType } from './crawlfs';
const UINT32_MAX = 2 ** 32 - 1;
const pipeline = promisify(stream.pipeline);
export type FilesystemDirectoryEntry = {
files: Record<string, FilesystemEntry>;
unpacked?: boolean;
};
export type FilesystemFileEntry = {
unpacked: boolean;
executable: boolean;
offset: string;
size: number;
integrity: FileIntegrity;
};
export type FilesystemLinkEntry = {
link: string;
};
export type FilesystemEntry = FilesystemDirectoryEntry | FilesystemFileEntry | FilesystemLinkEntry;
export class Filesystem {
private src: string;
private header: FilesystemEntry;
private headerSize: number;
private offset: bigint;
constructor(src: string) {
this.src = path.resolve(src);
this.header = { files: Object.create(null) };
this.headerSize = 0;
this.offset = BigInt(0);
}
getRootPath() {
return this.src;
}
getHeader() {
return this.header;
}
getHeaderSize() {
return this.headerSize;
}
setHeader(header: FilesystemEntry, headerSize: number) {
this.header = header;
this.headerSize = headerSize;
}
searchNodeFromDirectory(p: string) {
let json = this.header;
const dirs = p.split(path.sep);
for (const dir of dirs) {
if (dir !== '.') {
if ('files' in json) {
if (!json.files[dir]) {
json.files[dir] = { files: Object.create(null) };
}
json = json.files[dir];
} else {
throw new Error('Unexpected directory state while traversing: ' + p);
}
}
}
return json;
}
searchNodeFromPath(p: string) {
p = path.relative(this.src, p);
if (!p) {
return this.header;
}
const name = path.basename(p);
const node = this.searchNodeFromDirectory(path.dirname(p)) as FilesystemDirectoryEntry;
if (!node.files) {
node.files = Object.create(null);
}
if (!node.files[name]) {
node.files[name] = Object.create(null);
}
return node.files[name];
}
insertDirectory(p: string, shouldUnpack: boolean) {
const node = this.searchNodeFromPath(p) as FilesystemDirectoryEntry;
if (shouldUnpack) {
node.unpacked = shouldUnpack;
}
node.files = node.files || Object.create(null);
return node.files;
}
async insertFile(
p: string,
shouldUnpack: boolean,
file: CrawledFileType,
options: {
transform?: (filePath: string) => NodeJS.ReadWriteStream | void;
} = {},
) {
const dirNode = this.searchNodeFromPath(path.dirname(p)) as FilesystemDirectoryEntry;
const node = this.searchNodeFromPath(p) as FilesystemFileEntry;
if (shouldUnpack || dirNode.unpacked) {
node.size = file.stat.size;
node.unpacked = true;
node.integrity = await getFileIntegrity(p);
return Promise.resolve();
}
let size: number;
const transformed = options.transform && options.transform(p);
if (transformed) {
const tmpdir = await fs.mkdtemp(path.join(os.tmpdir(), 'asar-'));
const tmpfile = path.join(tmpdir, path.basename(p));
const out = fs.createWriteStream(tmpfile);
const readStream = fs.createReadStream(p);
await pipeline(readStream, transformed, out);
file.transformed = {
path: tmpfile,
stat: await fs.lstat(tmpfile),
};
size = file.transformed.stat.size;
} else {
size = file.stat.size;
}
// JavaScript cannot precisely present integers >= UINT32_MAX.
if (size > UINT32_MAX) {
throw new Error(`${p}: file size can not be larger than 4.2GB`);
}
node.size = size;
node.offset = this.offset.toString();
node.integrity = await getFileIntegrity(p);
if (process.platform !== 'win32' && file.stat.mode & 0o100) {
node.executable = true;
}
this.offset += BigInt(size);
}
insertLink(p: string) {
const symlink = fs.readlinkSync(p);
// /var => /private/var
const parentPath = fs.realpathSync(path.dirname(p));
const link = path.relative(fs.realpathSync(this.src), path.join(parentPath, symlink));
if (link.startsWith('..')) {
throw new Error(`${p}: file "${link}" links out of the package`);
}
const node = this.searchNodeFromPath(p) as FilesystemLinkEntry;
node.link = link;
return link;
}
listFiles(options?: { isPack: boolean }) {
const files: string[] = [];
const fillFilesFromMetadata = function (basePath: string, metadata: FilesystemEntry) {
if (!('files' in metadata)) {
return;
}
for (const [childPath, childMetadata] of Object.entries(metadata.files)) {
const fullPath = path.join(basePath, childPath);
const packState =
'unpacked' in childMetadata && childMetadata.unpacked ? 'unpack' : 'pack ';
files.push(options && options.isPack ? `${packState} : ${fullPath}` : fullPath);
fillFilesFromMetadata(fullPath, childMetadata);
}
};
fillFilesFromMetadata('/', this.header);
return files;
}
getNode(p: string) {
const node = this.searchNodeFromDirectory(path.dirname(p));
const name = path.basename(p);
if (name) {
return (node as FilesystemDirectoryEntry).files[name];
} else {
return node;
}
}
getFile(p: string, followLinks: boolean = true): FilesystemEntry {
const info = this.getNode(p);
if (!info) {
throw new Error(`"${p}" was not found in this archive`);
}
// if followLinks is false we don't resolve symlinks
if ('link' in info && followLinks) {
return this.getFile(info.link, followLinks);
} else {
return info;
}
}
}