Skip to content

Commit d4d8f4c

Browse files
authored
fix: extend node-bindings workaround hack (#24)
In addition to replacing fs methods, also hook Error.prepareStackTrace which node-bindings uses to locate its caller module, specifically to work around the case in which `stack[i].getFileName()` returns `null`.
1 parent 229a09d commit d4d8f4c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

resources/entry-point-trampoline.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const hydatedRequireMappings =
1212
requireMappings.map(([re, reFlags, linked]) => [new RegExp(re, reFlags), linked]);
1313

1414
if (enableBindingsPatch) {
15+
// Hack around various deficiencies in https://github.com/TooTallNate/node-bindings
1516
const fs = require('fs');
1617
const origFsAccessSync = fs.accessSync;
1718
fs.accessSync = (filename, ...args) => {
@@ -21,6 +22,33 @@ if (enableBindingsPatch) {
2122
}
2223
return origFsAccessSync.call(fs, filename, ...args);
2324
};
25+
26+
let epst = Error.prepareStackTrace;
27+
Object.defineProperty(Error, 'prepareStackTrace', {
28+
configurable: true,
29+
get() {
30+
return epst;
31+
},
32+
set(v) {
33+
if (typeof v !== 'function') {
34+
epst = v;
35+
return;
36+
}
37+
epst = function(error, stack) {
38+
stack = stack.map(entry => {
39+
if (!entry) return entry;
40+
const origGetFileName = entry.getFileName;
41+
Object.defineProperty(entry, 'getFileName', {
42+
value: function(...args) {
43+
return origGetFileName.call(this, ...args) || '';
44+
}
45+
});
46+
return entry;
47+
})
48+
return v.call(this, error, stack);
49+
};
50+
}
51+
});
2452
}
2553

2654
module.exports = (() => {

0 commit comments

Comments
 (0)