Skip to content

Commit d8325a9

Browse files
fixup! lib: disable REPL completion on proxies and getters
improve solution and use ObjectGetOwnPropertyDescriptor primordial
1 parent 10f3e17 commit d8325a9

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

lib/repl.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ const {
9696
globalThis,
9797
} = primordials;
9898

99+
const {
100+
isProxy,
101+
} = require('internal/util/types');
102+
99103
const { BuiltinModule } = require('internal/bootstrap/realm');
100104
const {
101105
makeRequireFunction,
@@ -1623,29 +1627,28 @@ async function includesProxiesOrGetters(fullExpr, evalFn, context) {
16231627
let currentExpr = '';
16241628
for (let i = 0; i < bits.length - 1; i++) {
16251629
currentExpr += `${i === 0 ? '' : '.'}${bits[i]}`;
1626-
const currentExprIsObject = await evalPromisified(`try { ${currentExpr} !== null && typeof ${currentExpr} === 'object' } catch { false }`);
1627-
if (!currentExprIsObject) {
1628-
return false;
1629-
}
1630+
const currentResult = await new Promise((resolve) =>
1631+
evalFn(`try { ${currentExpr} } catch { }`, context, getREPLResourceName(), (_, currentObj) => {
1632+
if (typeof currentObj !== 'object' || currentObj === null) {
1633+
return resolve(false);
1634+
}
16301635

1631-
const currentExprIsProxy = await evalPromisified(`require("node:util/types").isProxy(${currentExpr})`);
1632-
if (currentExprIsProxy) {
1633-
return true;
1634-
}
1636+
if (isProxy(currentObj)) {
1637+
return resolve(true);
1638+
}
16351639

1636-
const typeOfNextBitGet = await evalPromisified(`typeof Object.getOwnPropertyDescriptor(${currentExpr}, '${bits[i + 1]}')?.get`);
1637-
const nextBitHasGetter = typeOfNextBitGet === 'function';
1638-
if (nextBitHasGetter) {
1639-
return true;
1640-
}
1641-
}
1640+
const nextBitHasGetter = typeof ObjectGetOwnPropertyDescriptor(currentObj, bits[i + 1])?.get === 'function';
1641+
if (nextBitHasGetter) {
1642+
return resolve(true);
1643+
}
16421644

1643-
function evalPromisified(evalExpr) {
1644-
return new Promise((resolve, reject) =>
1645-
evalFn(evalExpr, context, getREPLResourceName(), (_, res) => {
1646-
resolve(res);
1645+
return resolve();
16471646
}));
1647+
if (currentResult !== undefined) {
1648+
return currentResult;
1649+
}
16481650
}
1651+
return false;
16491652
}
16501653

16511654
REPLServer.prototype.completeOnEditorMode = (callback) => (err, results) => {

0 commit comments

Comments
 (0)