Skip to content
This repository was archived by the owner on May 23, 2021. It is now read-only.

Commit 725fb91

Browse files
committed
Don't overwrite extracted native module
1 parent 7b303f6 commit 725fb91

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

node.patch

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js
2-
index 885546c5d6..f1287bb191 100644
2+
index 885546c5d6..f21e7993f1 100644
33
--- a/lib/internal/bootstrap/node.js
44
+++ b/lib/internal/bootstrap/node.js
55
@@ -215,11 +215,16 @@
66
// are running from a script and running the REPL - but there are a few
77
// others like the debugger or running --eval arguments. Here we decide
8-
// which mode we run in.
8+
// which mode we run in.
99
+
1010
+ if (NativeModule.exists('_third_party_main')) {
1111
+ NativeModule.require('_third_party_main');
@@ -21,7 +21,7 @@ index 885546c5d6..f1287bb191 100644
2121
// one to drop a file lib/_third_party_main.js into the build
2222
// directory which will be executed instead of Node's normal loading.
2323
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
24-
index fb3770b729..eb65385afe 100644
24+
index fb3770b729..9e210090e5 100644
2525
--- a/lib/internal/modules/cjs/loader.js
2626
+++ b/lib/internal/modules/cjs/loader.js
2727
@@ -44,6 +44,9 @@ const { getOptionValue } = require('internal/options');
@@ -101,17 +101,19 @@ index fb3770b729..eb65385afe 100644
101101
try {
102102
module.exports = JSON.parse(stripBOM(content));
103103
} catch (err) {
104-
@@ -715,7 +742,14 @@ Module._extensions['.json'] = function(module, filename) {
104+
@@ -715,7 +742,16 @@ Module._extensions['.json'] = function(module, filename) {
105105

106106
// Native extension for .node
107107
Module._extensions['.node'] = function(module, filename) {
108108
- return process.dlopen(module, path.toNamespacedPath(filename));
109109
+ let isInternal = false;
110110
+ if (nbin.existsSync(filename)) {
111-
+ const tmpFile = path.join(os.tmpdir(), `.nbin-${path.basename(filename)}`);
112-
+ fs.writeFileSync(tmpFile, nbin.readFileSync(filename));
113-
+ filename = tmpFile;
114-
+ isInternal = true;
111+
+ const tmpFile = path.join(os.tmpdir(), `.nbin${nbin.version}-${path.basename(filename)}`);
112+
+ if (!fs.existsSync(tmpFile)) {
113+
+ fs.writeFileSync(tmpFile, nbin.readFileSync(filename));
114+
+ }
115+
+ filename = tmpFile;
116+
+ isInternal = true;
115117
+ }
116118
+ return process.dlopen(module, isInternal ? filename : path.toNamespacedPath(filename));
117119
};

src/patches/nbin.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ReadableFilesystem } from "../common/filesystem";
66
import { readFooter } from "../common/footer";
77
import { fillFs } from "./fs";
88

9+
const version = require("../../package.json").version;
910
const execPath = process.execPath;
1011
const execPathStat = fs.statSync(execPath);
1112
const nbinFd = fs.openSync(execPath, "r");
@@ -82,6 +83,7 @@ const createNotFound = (): Error => {
8283

8384
const exported: typeof import("nbin") = {
8485
mainFile: mainFile.value,
86+
version,
8587

8688
existsSync: (pathName: string): boolean => {
8789
const stat = exported.statSync(pathName);

typings/nbin.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ declare module 'nbin' {
3939
function readFileSync(path: string, encoding?: "buffer", offset?: number, length?: number): Buffer;
4040
function readFileSync(path: string, encoding?: "utf8", offset?: number, length?: number): Buffer;
4141

42+
/**
43+
* nbin version.
44+
*/
45+
export const version: string;
46+
4247
/**
4348
* Returns the entrypoint of the application.
4449
*/
@@ -97,7 +102,7 @@ declare module '@coder/nbin' {
97102
* Will bundle a module based on path and name.
98103
* Allows you to do `writeModule("/test/bananas/node_modules/frog")` and
99104
* embed the `frog` module within the binary.
100-
*
105+
*
101106
* All modules by default will be placed in `/node_modules`
102107
*/
103108
public writeModule(modulePath: string): void;

0 commit comments

Comments
 (0)