Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/NodeBuilder.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

const { log, download, upload, fetch, mkdirp, rmrf, copyFileAsync, runCommand, renameAsync } = require('./util');
const { log, download, upload, fetch, mkdirp, rmrf, copyFileAsync, runCommand, renameAsync, patchFile } = require('./util');
const { gzipSync, createGunzip } = require('zlib');
const { join, dirname, basename, resolve } = require('path');
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');
Expand Down Expand Up @@ -43,8 +42,7 @@ function buildName(platform, arch, placeHolderSizeMB, version) {
}

class NodeJsBuilder {
constructor(cwd, version, mainAppFile, appName) {

constructor(cwd, version, mainAppFile, appName, patchDir) {
this.version = version;
this.appFile = resolve(mainAppFile);
this.appName = appName;
Expand All @@ -61,6 +59,7 @@ class NodeJsBuilder {
this.make = isWindows ? 'vcbuild.bat' : isBsd ? 'gmake' : 'make';
this.configure = isWindows ? 'configure' : './configure';
this.srcDir = join(__dirname);
this.patchDir = patchDir || join(this.srcDir, 'patch', version);
this.buildDir = join(cwd || process.cwd(), 'build');
this.nodeSrcFile = join(this.buildDir, `node-v${version}.tar.gz`);
this.nodeSrcDir = join(this.buildDir, `node-v${version}`);
Expand Down Expand Up @@ -190,6 +189,15 @@ class NodeJsBuilder {
});
}

async patchThirdPartyMain() {
await patchFile(
this.nodePath('lib', 'internal', 'main', 'run_third_party_main.js'),
join(this.patchDir, 'run_third_party_main.js.patch'));
await patchFile(
this.nodePath('src', 'node.cc'),
join(this.patchDir, 'node.cc.patch'));
}

printDiskUsage() {
if (isWindows) { return runCommand('fsutil', ['volume', 'diskfree', 'd:']); }
return runCommand('df', ['-h']);
Expand Down Expand Up @@ -234,6 +242,7 @@ class NodeJsBuilder {
return this.printDiskUsage()
.then(() => this.downloadExpandNodeSource())
.then(() => this.prepareNodeJsBuild())
.then(() => this.version.split('.')[0] >= 15 ? this.patchThirdPartyMain() : Promise.resolve())
.then(() => {
if (isWindows) { return runCommand(this.make, makeArgs, this.nodeSrcDir); }
if (isDarwin) {
Expand Down
16 changes: 16 additions & 0 deletions src/patch/16.16.0/node.cc.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--- src/node.cc 2022-08-26 13:05:24.458396341 -0500
+++ src/node.cc 2022-08-26 13:06:12.179824998 -0500
@@ -479,6 +479,13 @@
return scope.EscapeMaybe(cb(info));
}

+ // To allow people to extend Node in different ways, this hook allows
+ // one to drop a file lib/_third_party_main.js into the build
+ // directory which will be executed instead of Node's normal loading.
+ if (NativeModuleEnv::Exists("_third_party_main")) {
+ return StartExecution(env, "internal/main/run_third_party_main");
+ }
+
if (env->worker_context() != nullptr) {
return StartExecution(env, "internal/main/worker_thread");
}
16 changes: 16 additions & 0 deletions src/patch/16.16.0/run_third_party_main.js.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--- /dev/null 2022-08-18 11:11:24.665352687 -0500
+++ lib/internal/main/run_third_party_main.js 2022-08-26 13:55:53.482283827 -0500
@@ -0,0 +1,13 @@
+'use strict';
+
+const {
+ prepareMainThreadExecution
+} = require('internal/bootstrap/pre_execution');
+
+prepareMainThreadExecution();
+markBootstrapComplete();
+
+// Legacy _third_party_main.js support
+process.nextTick(() => {
+ require('_third_party_main');
+});
32 changes: 31 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { join, dirname } = require('path');
const { promisify } = require('util');
const fs = require('fs');
const { URL } = require('url');
const { pipeline } = require('stream');

const mkdirAsync = promisify(fs.mkdir);
const copyFileAsync = promisify(fs.copyFile);
Expand Down Expand Up @@ -78,6 +79,34 @@ function runCommand(command, args = [], cwd = undefined, env = undefined, verbos
});
}

async function patchFile(file, patchFile) {
await new Promise((resolve, reject) => {
const proc = spawn(
'patch',
[
'-uN',
file
],
{
stdio: [
null,
'inherit',
'inherit'
]
})
.once('exit', code => {
if (code !== 0) return reject(new Error(`falied to patch file=${file} with patch=${patchFile} code=${code}`));
return resolve();
})
.once('error', reject);
pipeline(
fs.createReadStream(patchFile),
proc.stdin,
err => err ? reject(err) : undefined
);
});
}

function fetch(url, headers) {
return new Promise((resolve, reject) => {
if (!url || url.length === 0) {
Expand Down Expand Up @@ -225,5 +254,6 @@ module.exports = {
mkdirp,
rmrf,
copyFileAsync,
renameAsync
renameAsync,
patchFile
};