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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var zlib_1 = require("zlib");
var crypto_1 = __importDefault(require("crypto"));
var fs_extra_1 = __importDefault(require("fs-extra"));
var os_1 = __importDefault(require("os"));
var path_1 = __importDefault(require("path"));
var stream_1 = require("stream");
var util_1 = require("util");
var tar_fs_1 = __importDefault(require("tar-fs"));
var places_1 = require("./places");
var utils_1 = require("./utils");
var system_1 = require("./system");
var log_1 = require("./log");
var patches_json_1 = __importDefault(require("../patches/patches.json"));
var buildPath = path_1.default.resolve(process.env.PKG_BUILD_PATH ||
    path_1.default.join(os_1.default.tmpdir(), "pkg." + crypto_1.default.randomBytes(12).toString('hex')));
var nodePath = path_1.default.join(buildPath, 'node');
var patchesPath = path_1.default.resolve(__dirname, '../patches');
var nodeRepo = 'https://nodejs.org/dist';
var nodeArchivePath = path_1.default.join(places_1.cachePath, 'node');
function getMajor(nodeVersion) {
    var _a = nodeVersion.match(/^v?(\d+)/) || ['', 0], version = _a[1];
    return Number(version) | 0;
}
function getConfigureArgs(major, targetPlatform) {
    var args = [];
    // first of all v8_inspector introduces the use
    // of `prime_rehash_policy` symbol that requires
    // GLIBCXX_3.4.18 on some systems
    // also we don't support any kind of debugging
    // against packaged apps, hence v8_inspector is useless
    args.push('--without-inspector');
    if (system_1.hostPlatform === 'alpine') {
        // Statically Link against libgcc and libstdc++ libraries. See vercel/pkg#555.
        // libgcc and libstdc++ grant GCC Runtime Library Exception of GPL
        args.push('--partly-static');
    }
    if (targetPlatform === 'linuxstatic') {
        args.push('--fully-static');
    }
    // Link Time Optimization
    if (major >= 12) {
        if (system_1.hostPlatform !== 'win') {
            args.push('--enable-lto');
        }
    }
    // production binaries do NOT take NODE_OPTIONS from end-users
    args.push('--without-node-options');
    // DTrace
    args.push('--without-dtrace');
    // bundled npm package manager
    args.push('--without-npm');
    // Small ICU
    args.push('--with-intl=small-icu');
    // Workaround for nodejs/node#39313
    // All supported macOS versions have zlib as a system library
    if (targetPlatform === 'macos') {
        args.push('--shared-zlib');
    }
    return args;
}
function tarFetch(nodeVersion) {
    return __awaiter(this, void 0, void 0, function () {
        var distUrl, tarName, archivePath, hashPath, _a, _b, _c;
        return __generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    log_1.log.info('Fetching Node.js source archive from nodejs.org...');
                    distUrl = nodeRepo + "/" + nodeVersion;
                    tarName = "node-" + nodeVersion + ".tar.gz";
                    archivePath = path_1.default.join(nodeArchivePath, tarName);
                    hashPath = path_1.default.join(nodeArchivePath, tarName + ".sha256sum");
                    if (fs_extra_1.default.existsSync(hashPath) && fs_extra_1.default.existsSync(archivePath)) {
                        return [2 /*return*/];
                    }
                    return [4 /*yield*/, fs_extra_1.default.remove(hashPath).catch(function () { return undefined; })];
                case 1:
                    _d.sent();
                    return [4 /*yield*/, fs_extra_1.default.remove(archivePath).catch(function () { return undefined; })];
                case 2:
                    _d.sent();
                    return [4 /*yield*/, (0, utils_1.downloadUrl)(distUrl + "/SHASUMS256.txt", hashPath)];
                case 3:
                    _d.sent();
                    _b = (_a = fs_extra_1.default).writeFile;
                    _c = [hashPath];
                    return [4 /*yield*/, fs_extra_1.default.readFile(hashPath, 'utf8')];
                case 4: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.sent())
                            .split('\n')
                            .filter(function (l) { return l.includes(tarName); })[0]]))];
                case 5:
                    _d.sent();
                    return [4 /*yield*/, (0, utils_1.downloadUrl)(distUrl + "/" + tarName, archivePath)];
                case 6:
                    _d.sent();
                    return [2 /*return*/];
            }
        });
    });
}
function tarExtract(nodeVersion) {
    return __awaiter(this, void 0, void 0, function () {
        var tarName, expectedHash, actualHash, pipe, source, gunzip, extract;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    log_1.log.info('Extracting Node.js source archive...');
                    tarName = "node-" + nodeVersion + ".tar.gz";
                    return [4 /*yield*/, fs_extra_1.default.readFile(path_1.default.join(nodeArchivePath, tarName + ".sha256sum"), 'utf8')];
                case 1:
                    expectedHash = (_a.sent()).split(' ')[0];
                    return [4 /*yield*/, (0, utils_1.hash)(path_1.default.join(nodeArchivePath, tarName))];
                case 2:
                    actualHash = _a.sent();
                    if (!(expectedHash !== actualHash)) return [3 /*break*/, 5];
                    return [4 /*yield*/, fs_extra_1.default.remove(path_1.default.join(nodeArchivePath, tarName))];
                case 3:
                    _a.sent();
                    return [4 /*yield*/, fs_extra_1.default.remove(path_1.default.join(nodeArchivePath, tarName + ".sha256sum"))];
                case 4:
                    _a.sent();
                    throw (0, log_1.wasReported)("Hash mismatch for " + tarName);
                case 5:
                    pipe = (0, util_1.promisify)(stream_1.pipeline);
                    source = fs_extra_1.default.createReadStream(path_1.default.join(nodeArchivePath, tarName));
                    gunzip = (0, zlib_1.createGunzip)();
                    extract = tar_fs_1.default.extract(nodePath, {
                        strip: 1,
                        map: function (header) {
                            log_1.log.info(header.name);
                            return header;
                        },
                    });
                    return [4 /*yield*/, pipe(source, gunzip, extract)];
                case 6:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    });
}
function applyPatches(nodeVersion) {
    return __awaiter(this, void 0, void 0, function () {
        var storedPatches, storedPatch, patches, _i, patches_1, patch, patchPath, args;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    log_1.log.info('Applying patches');
                    storedPatches = patches_json_1.default[nodeVersion];
                    storedPatch = 'patches' in storedPatches ? storedPatches.patches : storedPatches;
                    patches = 'sameAs' in storedPatch
                        ? patches_json_1.default[storedPatch.sameAs]
                        : storedPatch;
                    _i = 0, patches_1 = patches;
                    _a.label = 1;
                case 1:
                    if (!(_i < patches_1.length)) return [3 /*break*/, 4];
                    patch = patches_1[_i];
                    patchPath = path_1.default.join(patchesPath, patch);
                    args = ['-p1', '-i', patchPath];
                    return [4 /*yield*/, (0, utils_1.spawn)('patch', args, { cwd: nodePath, stdio: 'inherit' })];
                case 2:
                    _a.sent();
                    _a.label = 3;
                case 3:
                    _i++;
                    return [3 /*break*/, 1];
                case 4: return [2 /*return*/];
            }
        });
    });
}
function compileOnWindows(nodeVersion, targetArch, targetPlatform) {
    return __awaiter(this, void 0, void 0, function () {
        var args, major, config_flags;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    args = ['/c', 'vcbuild.bat', targetArch];
                    major = getMajor(nodeVersion);
                    config_flags = getConfigureArgs(major, targetPlatform);
                    // Event Tracing for Windows
                    args.push('noetw');
                    // Performance counters on Windows
                    if (major <= 10) {
                        args.push('noperfctr');
                    }
                    // Link Time Code Generation
                    if (major >= 12) {
                        args.push('ltcg');
                    }
                    // Can't cross compile for arm64 with small-icu
                    if (system_1.hostArch !== targetArch &&
                        !config_flags.includes('--with-intl=full-icu')) {
                        config_flags.push('--without-intl');
                    }
                    return [4 /*yield*/, (0, utils_1.spawn)('cmd', args, {
                            cwd: nodePath,
                            env: __assign(__assign({}, process.env), { config_flags: config_flags.join(' ') }),
                            stdio: 'inherit',
                        })];
                case 1:
                    _a.sent();
                    if (major <= 10) {
                        return [2 /*return*/, path_1.default.join(nodePath, 'Release/node.exe')];
                    }
                    return [2 /*return*/, path_1.default.join(nodePath, 'out/Release/node.exe')];
            }
        });
    });
}
var _a = process.env.MAKE_JOB_COUNT, MAKE_JOB_COUNT = _a === void 0 ? os_1.default.cpus().length : _a;
function compileOnUnix(nodeVersion, targetArch, targetPlatform) {
    return __awaiter(this, void 0, void 0, function () {
        var args, cpu, output;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    args = [];
                    cpu = {
                        x86: 'ia32',
                        x64: 'x64',
                        armv6: 'arm',
                        armv7: 'arm',
                        arm64: 'arm64',
                        ppc64: 'ppc64',
                        s390x: 's390x',
                    }[targetArch];
                    if (cpu) {
                        args.push('--dest-cpu', cpu);
                    }
                    if (system_1.hostArch !== targetArch) {
                        log_1.log.warn('Cross compiling!');
                        log_1.log.warn('You are responsible for appropriate env like CC, CC_host, etc.');
                        args.push('--cross-compiling');
                    }
                    args.push.apply(args, getConfigureArgs(getMajor(nodeVersion), targetPlatform));
                    // TODO same for windows?
                    return [4 /*yield*/, (0, utils_1.spawn)('/bin/sh', __spreadArray(['./configure'], args, true), {
                            cwd: nodePath,
                            stdio: 'inherit',
                        })];
                case 1:
                    // TODO same for windows?
                    _a.sent();
                    return [4 /*yield*/, (0, utils_1.spawn)(system_1.hostPlatform === 'freebsd' ? 'gmake' : 'make', ['-j', String(MAKE_JOB_COUNT)], {
                            cwd: nodePath,
                            stdio: 'inherit',
                        })];
                case 2:
                    _a.sent();
                    output = path_1.default.join(nodePath, 'out/Release/node');
                    return [4 /*yield*/, (0, utils_1.spawn)(process.env.STRIP || 'strip', __spreadArray(__spreadArray([], (targetPlatform === 'macos' ? ['-x'] : []), true), [output], false), {
                            stdio: 'inherit',
                        })];
                case 3:
                    _a.sent();
                    if (!(targetPlatform === 'macos')) return [3 /*break*/, 5];
                    // Newer versions of Apple Clang automatically ad-hoc sign the compiled executable.
                    // However, for final executable to be signable, base binary MUST NOT have an existing signature.
                    return [4 /*yield*/, (0, utils_1.spawn)('codesign', ['--remove-signature', output], {
                            stdio: 'inherit',
                        })];
                case 4:
                    // Newer versions of Apple Clang automatically ad-hoc sign the compiled executable.
                    // However, for final executable to be signable, base binary MUST NOT have an existing signature.
                    _a.sent();
                    _a.label = 5;
                case 5: return [2 /*return*/, output];
            }
        });
    });
}
function compile(nodeVersion, targetArch, targetPlatform) {
    return __awaiter(this, void 0, void 0, function () {
        var win;
        return __generator(this, function (_a) {
            log_1.log.info('Compiling Node.js from sources...');
            win = system_1.hostPlatform === 'win';
            if (win) {
                return [2 /*return*/, compileOnWindows(nodeVersion, targetArch, targetPlatform)];
            }
            return [2 /*return*/, compileOnUnix(nodeVersion, targetArch, targetPlatform)];
        });
    });
}
function build(nodeVersion, targetArch, targetPlatform, local) {
    return __awaiter(this, void 0, void 0, function () {
        var output, outputHash;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, fs_extra_1.default.remove(buildPath)];
                case 1:
                    _a.sent();
                    return [4 /*yield*/, fs_extra_1.default.mkdirp(nodePath)];
                case 2:
                    _a.sent();
                    return [4 /*yield*/, fs_extra_1.default.mkdirp(nodeArchivePath)];
                case 3:
                    _a.sent();
                    return [4 /*yield*/, tarFetch(nodeVersion)];
                case 4:
                    _a.sent();
                    return [4 /*yield*/, tarExtract(nodeVersion)];
                case 5:
                    _a.sent();
                    return [4 /*yield*/, applyPatches(nodeVersion)];
                case 6:
                    _a.sent();
                    return [4 /*yield*/, compile(nodeVersion, targetArch, targetPlatform)];
                case 7:
                    output = _a.sent();
                    return [4 /*yield*/, (0, utils_1.hash)(output)];
                case 8:
                    outputHash = _a.sent();
                    return [4 /*yield*/, fs_extra_1.default.mkdirp(path_1.default.dirname(local))];
                case 9:
                    _a.sent();
                    return [4 /*yield*/, fs_extra_1.default.copy(output, local)];
                case 10:
                    _a.sent();
                    return [4 /*yield*/, fs_extra_1.default.promises.writeFile(local + ".sha256sum", outputHash + "  " + path_1.default.basename(local) + "\n")];
                case 11:
                    _a.sent();
                    return [4 /*yield*/, fs_extra_1.default.remove(buildPath)];
                case 12:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    });
}
exports.default = build;
//# sourceMappingURL=build.js.map