Skip to content

Commit

Permalink
lib,permission: drop repl autocomplete when pm enabled
Browse files Browse the repository at this point in the history
PR-URL: #48920
Fixes: #48884
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
  • Loading branch information
RafaelGSS authored Jul 27, 2023
1 parent 0da3f61 commit b907815
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/internal/util/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
} = primordials;

const { validatePort } = require('internal/validators');
const permission = require('internal/process/permission');

const kMinPort = 1024;
const kMaxPort = 65535;
Expand Down Expand Up @@ -47,6 +48,10 @@ let session;
function sendInspectorCommand(cb, onError) {
const { hasInspector } = internalBinding('config');
if (!hasInspector) return onError();
// Do not preview when the permission model is enabled
// because this feature require access to the inspector,
// which is unavailable in this case.
if (permission.isEnabled()) return onError();
const inspector = require('inspector');
if (session === undefined) session = new inspector.Session();
session.connect();
Expand Down
135 changes: 135 additions & 0 deletions test/parallel/test-repl-permission-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
'use strict';

// Flags: --expose-internals --experimental-permission --allow-fs-read=*

const common = require('../common');
const stream = require('stream');
const REPL = require('internal/repl');
const assert = require('assert');
const { inspect } = require('util');

common.skipIfDumbTerminal();

// Create an input stream specialized for testing an array of actions
class ActionStream extends stream.Stream {
run(data) {
const _iter = data[Symbol.iterator]();
const doAction = () => {
const next = _iter.next();
if (next.done) {
// Close the repl. Note that it must have a clean prompt to do so.
this.emit('keypress', '', { ctrl: true, name: 'd' });
return;
}
const action = next.value;

if (typeof action === 'object') {
this.emit('keypress', '', action);
} else {
this.emit('data', `${action}`);
}
setImmediate(doAction);
};
doAction();
}
resume() {}
pause() {}
}
ActionStream.prototype.readable = true;

// Mock keys
const ENTER = { name: 'enter' };
const TABULATION = { name: 'tab' };

const prompt = '> ';

const tests = [
{
test: (function*() {
yield 'f';
yield TABULATION;
yield ENTER;
})(),
expected: [],
env: {}
},
];

const numtests = tests.length;

const runTestWrap = common.mustCall(runTest, numtests);

function runTest() {
const opts = tests.shift();
if (!opts) return; // All done

const { expected, skip } = opts;

// Test unsupported on platform.
if (skip) {
setImmediate(runTestWrap, true);
return;
}
const lastChunks = [];
let i = 0;

REPL.createInternalRepl(opts.env, {
input: new ActionStream(),
output: new stream.Writable({
write(chunk, _, next) {
const output = chunk.toString();

if (!opts.showEscapeCodes &&
(output[0] === '\x1B' || /^[\r\n]+$/.test(output))) {
return next();
}

lastChunks.push(output);

if (expected.length && !opts.checkTotal) {
try {
assert.strictEqual(output, expected[i]);
} catch (e) {
console.error(`Failed test # ${numtests - tests.length}`);
console.error('Last outputs: ' + inspect(lastChunks, {
breakLength: 5, colors: true
}));
throw e;
}
// TODO(BridgeAR): Auto close on last chunk!
i++;
}

next();
}
}),
allowBlockingCompletions: true,
completer: opts.completer,
prompt,
useColors: false,
preview: opts.preview,
terminal: true
}, function(err, repl) {
if (err) {
console.error(`Failed test # ${numtests - tests.length}`);
throw err;
}

repl.once('close', () => {

if (opts.checkTotal) {
assert.deepStrictEqual(lastChunks, expected);
} else if (expected.length !== i) {
console.error(tests[numtests - tests.length - 1]);
throw new Error(`Failed test # ${numtests - tests.length}`);
}

setImmediate(runTestWrap, true);
});

repl.input.run(opts.test);
});
}

// run the tests
runTest();

0 comments on commit b907815

Please sign in to comment.