Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Represent stdin, stdout, and stderr as files #322

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,19 @@ function Binding(system) {
* Counter for file descriptors.
* @type {number}
*/
this._counter = 0;
this._counter = -1;

const stdin = new FileDescriptor(constants.O_RDWR);
stdin.setItem(new File.StandardInput());
this.trackDescriptor(stdin);

const stdout = new FileDescriptor(constants.O_RDWR);
stdout.setItem(new File.StandardOutput());
this.trackDescriptor(stdout);

const stderr = new FileDescriptor(constants.O_RDWR);
stderr.setItem(new File.StandardError());
this.trackDescriptor(stderr);
}

/**
Expand Down
58 changes: 57 additions & 1 deletion lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const EMPTY = bufferAlloc(0);
const constants = require('constants');

/**
* A directory.
* A file.
* @constructor
*/
function File() {
Expand Down Expand Up @@ -67,3 +67,59 @@ File.prototype.getStats = function() {
* @type {function()}
*/
exports = module.exports = File;

/**
* Standard input.
* @constructor
*/
function StandardInput() {
File.call(this);
this.setMode(438); // 0666
}
util.inherits(StandardInput, File);

exports.StandardInput = StandardInput;

/**
* Standard output.
* @constructor
*/
function StandardOutput() {
File.call(this);
this.setMode(438); // 0666
}
util.inherits(StandardOutput, File);

/**
* Write the contents to stdout.
* @param {string|Buffer} content File contents.
*/
StandardOutput.prototype.setContent = function(content) {
if (process.stdout.isTTY) {
process.stdout.write(content);
}
};

exports.StandardOutput = StandardOutput;

/**
* Standard error.
* @constructor
*/
function StandardError() {
File.call(this);
this.setMode(438); // 0666
}
util.inherits(StandardError, File);

/**
* Write the contents to stderr.
* @param {string|Buffer} content File contents.
*/
StandardError.prototype.setContent = function(content) {
if (process.stderr.isTTY) {
process.stderr.write(content);
}
};

exports.StandardError = StandardError;
13 changes: 6 additions & 7 deletions lib/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,13 @@ function Item() {
* Add execute if read allowed
* See notes in index.js -> mapping#addDir
*/
// prettier-ignore
Item.fixWin32Permissions = mode =>
(process.platform !== 'win32')
? mode
: mode |
((mode & permissions.USER_READ) && permissions.USER_EXEC) |
((mode & permissions.GROUP_READ) && permissions.GROUP_EXEC) |
((mode & permissions.OTHER_READ) && permissions.OTHER_EXEC);
process.platform !== 'win32'
? mode
: mode |
(mode & permissions.USER_READ && permissions.USER_EXEC) |
(mode & permissions.GROUP_READ && permissions.GROUP_EXEC) |
(mode & permissions.OTHER_READ && permissions.OTHER_EXEC);

/**
* Determine if the current user has read permission.
Expand Down