15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.readAsarJson = exports.readAsar = exports.readAsarHeader = exports.AsarFilesystem = exports.Node = void 0;
const chromium_pickle_js_1 = require("chromium-pickle-js");
const fs_extra_1 = require("fs-extra");
const path = require("path");
/** @internal */
class Node {
}
exports.Node = Node;
class AsarFilesystem {
    constructor(src, header = new Node(), headerSize = -1) {
        this.src = src;
        this.header = header;
        this.headerSize = headerSize;
        this.offset = 0;
        if (this.header.files == null) {
            this.header.files = {};
        }
    }
    searchNodeFromDirectory(p, isCreate) {
        let node = this.header;
        for (const dir of p.split(path.sep)) {
            if (dir !== ".") {
                let child = node.files[dir];
                if (child == null) {
                    if (!isCreate) {
                        return null;
                    }
                    child = new Node();
                    child.files = {};
                    node.files[dir] = child;
                }
                node = child;
            }
        }
        return node;
    }
    getOrCreateNode(p) {
        if (p == null || p.length === 0) {
            return this.header;
        }
        const name = path.basename(p);
        const dirNode = this.searchNodeFromDirectory(path.dirname(p), true);
        if (dirNode.files == null) {
            dirNode.files = {};
        }
        let result = dirNode.files[name];
        if (result == null) {
            result = new Node();
            dirNode.files[name] = result;
        }
        return result;
    }
    addFileNode(file, dirNode, size, unpacked, stat, integrity) {
        if (size > 4294967295) {
            throw new Error(`${file}: file size cannot be larger than 4.2GB`);
        }
        const node = new Node();
        node.size = size;
        if (integrity) {
            node.integrity = integrity;
        }
        if (unpacked) {
            node.unpacked = true;
        }
        else {
            // electron expects string
            node.offset = this.offset.toString();
            if (process.platform !== "win32" && stat.mode & 0o100) {
                node.executable = true;
            }
            this.offset += node.size;
        }
        let children = dirNode.files;
        if (children == null) {
            children = {};
            dirNode.files = children;
        }
        children[path.basename(file)] = node;
        return node;
    }
    getNode(p) {
        const node = this.searchNodeFromDirectory(path.dirname(p), false);
        return node.files[path.basename(p)];
    }
    getFile(p, followLinks = true) {
        const info = this.getNode(p);
        // if followLinks is false we don't resolve symlinks
        return followLinks && info.link != null ? this.getFile(info.link) : info;
    }
    async readJson(file) {
        return JSON.parse((await this.readFile(file)).toString());
    }
    readFile(file) {
        return readFileFromAsar(this, file, this.getFile(file));
    }
}
exports.AsarFilesystem = AsarFilesystem;
async function readAsarHeader(archive) {
    const fd = await fs_extra_1.open(archive, "r");
    let size;
    let headerBuf;
    try {
        const sizeBuf = Buffer.allocUnsafe(8);
        if ((await fs_extra_1.read(fd, sizeBuf, 0, 8, null)).bytesRead !== 8) {
            throw new Error("Unable to read header size");
        }
        const sizePickle = chromium_pickle_js_1.createFromBuffer(sizeBuf);
        size = sizePickle.createIterator().readUInt32();
        headerBuf = Buffer.allocUnsafe(size);
        if ((await fs_extra_1.read(fd, headerBuf, 0, size, null)).bytesRead !== size) {
            throw new Error("Unable to read header");
        }
    }
    finally {
        await fs_extra_1.close(fd);
    }
    const headerPickle = chromium_pickle_js_1.createFromBuffer(headerBuf);
    return { header: headerPickle.createIterator().readString(), size };
}
exports.readAsarHeader = readAsarHeader;
async function readAsar(archive) {
    const { header, size } = await readAsarHeader(archive);
    return new AsarFilesystem(archive, JSON.parse(header), size);
}
exports.readAsar = readAsar;
async function readAsarJson(archive, file) {
    const fs = await readAsar(archive);
    return await fs.readJson(file);
}
exports.readAsarJson = readAsarJson;
async function readFileFromAsar(filesystem, filename, info) {
    const size = info.size;
    const buffer = Buffer.allocUnsafe(size);
    if (size <= 0) {
        return buffer;
    }
    if (info.unpacked) {
        return await fs_extra_1.readFile(path.join(`${filesystem.src}.unpacked`, filename));
    }
    const fd = await fs_extra_1.open(filesystem.src, "r");
    try {
        const offset = 8 + filesystem.headerSize + parseInt(info.offset, 10);
        await fs_extra_1.read(fd, buffer, 0, size, offset);
    }
    finally {
        await fs_extra_1.close(fd);
    }
    return buffer;
}
//# sourceMappingURL=asar.js.map