Skip to content

Commit 4b34a0d

Browse files
authored
Merge pull request #33 from nromito/feature/add-third-party-main-support
Adding support for _third_party_main.js
2 parents 54aae3e + 5a155e2 commit 4b34a0d

4 files changed

Lines changed: 76 additions & 5 deletions

File tree

src/NodeBuilder.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

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

4544
class NodeJsBuilder {
46-
constructor(cwd, version, mainAppFile, appName) {
47-
45+
constructor(cwd, version, mainAppFile, appName, patchDir) {
4846
this.version = version;
4947
this.appFile = resolve(mainAppFile);
5048
this.appName = appName;
@@ -61,6 +59,7 @@ class NodeJsBuilder {
6159
this.make = isWindows ? 'vcbuild.bat' : isBsd ? 'gmake' : 'make';
6260
this.configure = isWindows ? 'configure' : './configure';
6361
this.srcDir = join(__dirname);
62+
this.patchDir = patchDir || join(this.srcDir, 'patch', version);
6463
this.buildDir = join(cwd || process.cwd(), 'build');
6564
this.nodeSrcFile = join(this.buildDir, `node-v${version}.tar.gz`);
6665
this.nodeSrcDir = join(this.buildDir, `node-v${version}`);
@@ -190,6 +189,15 @@ class NodeJsBuilder {
190189
});
191190
}
192191

192+
async patchThirdPartyMain() {
193+
await patchFile(
194+
this.nodePath('lib', 'internal', 'main', 'run_third_party_main.js'),
195+
join(this.patchDir, 'run_third_party_main.js.patch'));
196+
await patchFile(
197+
this.nodePath('src', 'node.cc'),
198+
join(this.patchDir, 'node.cc.patch'));
199+
}
200+
193201
printDiskUsage() {
194202
if (isWindows) { return runCommand('fsutil', ['volume', 'diskfree', 'd:']); }
195203
return runCommand('df', ['-h']);
@@ -234,6 +242,7 @@ class NodeJsBuilder {
234242
return this.printDiskUsage()
235243
.then(() => this.downloadExpandNodeSource())
236244
.then(() => this.prepareNodeJsBuild())
245+
.then(() => this.version.split('.')[0] >= 15 ? this.patchThirdPartyMain() : Promise.resolve())
237246
.then(() => {
238247
if (isWindows) { return runCommand(this.make, makeArgs, this.nodeSrcDir); }
239248
if (isDarwin) {

src/patch/16.16.0/node.cc.patch

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--- src/node.cc 2022-08-26 13:05:24.458396341 -0500
2+
+++ src/node.cc 2022-08-26 13:06:12.179824998 -0500
3+
@@ -479,6 +479,13 @@
4+
return scope.EscapeMaybe(cb(info));
5+
}
6+
7+
+ // To allow people to extend Node in different ways, this hook allows
8+
+ // one to drop a file lib/_third_party_main.js into the build
9+
+ // directory which will be executed instead of Node's normal loading.
10+
+ if (NativeModuleEnv::Exists("_third_party_main")) {
11+
+ return StartExecution(env, "internal/main/run_third_party_main");
12+
+ }
13+
+
14+
if (env->worker_context() != nullptr) {
15+
return StartExecution(env, "internal/main/worker_thread");
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--- /dev/null 2022-08-18 11:11:24.665352687 -0500
2+
+++ lib/internal/main/run_third_party_main.js 2022-08-26 13:55:53.482283827 -0500
3+
@@ -0,0 +1,13 @@
4+
+'use strict';
5+
+
6+
+const {
7+
+ prepareMainThreadExecution
8+
+} = require('internal/bootstrap/pre_execution');
9+
+
10+
+prepareMainThreadExecution();
11+
+markBootstrapComplete();
12+
+
13+
+// Legacy _third_party_main.js support
14+
+process.nextTick(() => {
15+
+ require('_third_party_main');
16+
+});

src/util.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { join, dirname } = require('path');
55
const { promisify } = require('util');
66
const fs = require('fs');
77
const { URL } = require('url');
8+
const { pipeline } = require('stream');
89

910
const mkdirAsync = promisify(fs.mkdir);
1011
const copyFileAsync = promisify(fs.copyFile);
@@ -78,6 +79,34 @@ function runCommand(command, args = [], cwd = undefined, env = undefined, verbos
7879
});
7980
}
8081

82+
async function patchFile(file, patchFile) {
83+
await new Promise((resolve, reject) => {
84+
const proc = spawn(
85+
'patch',
86+
[
87+
'-uN',
88+
file
89+
],
90+
{
91+
stdio: [
92+
null,
93+
'inherit',
94+
'inherit'
95+
]
96+
})
97+
.once('exit', code => {
98+
if (code !== 0) return reject(new Error(`falied to patch file=${file} with patch=${patchFile} code=${code}`));
99+
return resolve();
100+
})
101+
.once('error', reject);
102+
pipeline(
103+
fs.createReadStream(patchFile),
104+
proc.stdin,
105+
err => err ? reject(err) : undefined
106+
);
107+
});
108+
}
109+
81110
function fetch(url, headers) {
82111
return new Promise((resolve, reject) => {
83112
if (!url || url.length === 0) {
@@ -225,5 +254,6 @@ module.exports = {
225254
mkdirp,
226255
rmrf,
227256
copyFileAsync,
228-
renameAsync
257+
renameAsync,
258+
patchFile
229259
};

0 commit comments

Comments
 (0)