Skip to content

Commit

Permalink
Merge pull request #322 from tschaub/std
Browse files Browse the repository at this point in the history
Represent stdin, stdout, and stderr as files
  • Loading branch information
tschaub committed Apr 22, 2021
2 parents 648d299 + 80d318f commit cb3ad21
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
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

0 comments on commit cb3ad21

Please sign in to comment.