gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
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
"use strict";
/* eslint-disable complexity */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = __importDefault(require("assert"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const common_1 = require("./common");
const log_1 = require("./log");
const { version } = JSON.parse(fs_extra_1.default.readFileSync(path_1.default.join(__dirname, '../package.json'), 'utf-8'));
const bootstrapText = fs_extra_1.default
    .readFileSync(require.resolve('../prelude/bootstrap.js'), 'utf8')
    .replace('%VERSION%', version);
const commonText = fs_extra_1.default.readFileSync(require.resolve('./common'), 'utf8');
const diagnosticText = fs_extra_1.default.readFileSync(require.resolve('../prelude/diagnostic.js'), 'utf8');
function itemsToText(items) {
    const len = items.length;
    return len.toString() + (len % 10 === 1 ? ' item' : ' items');
}
function hasAnyStore(record) {
    // discarded records like native addons
    for (const store of [common_1.STORE_BLOB, common_1.STORE_CONTENT, common_1.STORE_LINKS, common_1.STORE_STAT]) {
        if (record[store])
            return true;
    }
    return false;
}
function packer({ records, entrypoint, bytecode, }) {
    const stripes = [];
    for (const snap in records) {
        if (records[snap]) {
            const record = records[snap];
            const { file } = record;
            if (!hasAnyStore(record)) {
                continue;
            }
            (0, assert_1.default)(record[common_1.STORE_STAT], 'packer: no STORE_STAT');
            (0, assert_1.default)(record[common_1.STORE_BLOB] ||
                record[common_1.STORE_CONTENT] ||
                record[common_1.STORE_LINKS] ||
                record[common_1.STORE_STAT]);
            if (record[common_1.STORE_BLOB] && !bytecode) {
                delete record[common_1.STORE_BLOB];
                if (!record[common_1.STORE_CONTENT]) {
                    // TODO make a test for it?
                    throw (0, log_1.wasReported)('--no-bytecode and no source breaks final executable', [
                        file,
                        'Please run with "-d" and without "--no-bytecode" first, and make',
                        'sure that debug log does not contain "was included as bytecode".',
                    ]);
                }
            }
            for (const store of [
                common_1.STORE_BLOB,
                common_1.STORE_CONTENT,
                common_1.STORE_LINKS,
                common_1.STORE_STAT,
            ]) {
                const value = record[store];
                if (!value) {
                    continue;
                }
                if (store === common_1.STORE_BLOB || store === common_1.STORE_CONTENT) {
                    if (record.body === undefined) {
                        stripes.push({ snap, store, file });
                    }
                    else if (Buffer.isBuffer(record.body)) {
                        stripes.push({ snap, store, buffer: record.body });
                    }
                    else if (typeof record.body === 'string') {
                        stripes.push({ snap, store, buffer: Buffer.from(record.body) });
                    }
                    else {
                        (0, assert_1.default)(false, 'packer: bad STORE_BLOB/STORE_CONTENT');
                    }
                }
                else if (store === common_1.STORE_LINKS) {
                    if (Array.isArray(value)) {
                        const dedupedValue = [...new Set(value)];
                        log_1.log.debug('files & folders deduped = ', dedupedValue);
                        const buffer = Buffer.from(JSON.stringify(dedupedValue));
                        stripes.push({ snap, store, buffer });
                    }
                    else {
                        (0, assert_1.default)(false, 'packer: bad STORE_LINKS');
                    }
                }
                else if (store === common_1.STORE_STAT) {
                    if (typeof value === 'object') {
                        const newStat = Object.assign({}, value);
                        const buffer = Buffer.from(JSON.stringify(newStat));
                        stripes.push({ snap, store, buffer });
                    }
                    else {
                        (0, assert_1.default)(false, 'packer: unknown store');
                    }
                }
                if (record[common_1.STORE_CONTENT]) {
                    const disclosed = (0, common_1.isDotJS)(file) || (0, common_1.isDotJSON)(file);
                    log_1.log.debug(disclosed
                        ? 'The file was included as DISCLOSED code (with sources)'
                        : 'The file was included as asset content', file);
                }
                else if (record[common_1.STORE_BLOB]) {
                    log_1.log.debug('The file was included as bytecode (no sources)', file);
                }
                else if (record[common_1.STORE_LINKS]) {
                    const link = record[common_1.STORE_LINKS];
                    log_1.log.debug(`The directory files list was included (${itemsToText(link)})`, file);
                }
            }
        }
    }
    const prelude = `return (function (REQUIRE_COMMON, VIRTUAL_FILESYSTEM, DEFAULT_ENTRYPOINT, SYMLINKS, DICT, DOCOMPRESS) {
        ${bootstrapText}${log_1.log.debugMode ? diagnosticText : ''}\n})(function (exports) {\n${commonText}\n},\n` +
        `%VIRTUAL_FILESYSTEM%` +
        `\n,\n` +
        `%DEFAULT_ENTRYPOINT%` +
        `\n,\n` +
        `%SYMLINKS%` +
        '\n,\n' +
        '%DICT%' +
        '\n,\n' +
        '%DOCOMPRESS%' +
        `\n);`;
    return { prelude, entrypoint, stripes };
}
exports.default = packer;
//# sourceMappingURL=packer.js.map