1 | 'use strict';
|
2 |
|
3 | const util = require('util');
|
4 |
|
5 | const Item = require('./item');
|
6 |
|
7 | const EMPTY = Buffer.alloc(0);
|
8 | const constants = require('constants');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function File() {
|
15 | Item.call(this);
|
16 |
|
17 | |
18 |
|
19 |
|
20 |
|
21 | this._content = EMPTY;
|
22 | }
|
23 | util.inherits(File, Item);
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | File.prototype.getContent = function() {
|
30 | this.setATime(new Date());
|
31 | return this._content;
|
32 | };
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | File.prototype.setContent = function(content) {
|
39 | if (typeof content === 'string') {
|
40 | content = Buffer.from(content);
|
41 | } else if (!Buffer.isBuffer(content)) {
|
42 | throw new Error('File content must be a string or buffer');
|
43 | }
|
44 | this._content = content;
|
45 | const now = Date.now();
|
46 | this.setCTime(new Date(now));
|
47 | this.setMTime(new Date(now));
|
48 | };
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | File.prototype.getStats = function(bigint) {
|
55 | const size = this._content.length;
|
56 | const stats = Item.prototype.getStats.call(this, bigint);
|
57 | const convert = bigint ? v => BigInt(v) : v => v;
|
58 |
|
59 | stats[1] = convert(this.getMode() | constants.S_IFREG);
|
60 | stats[8] = convert(size);
|
61 | stats[9] = convert(Math.ceil(size / 512));
|
62 |
|
63 | return stats;
|
64 | };
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | exports = module.exports = File;
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | function StandardInput() {
|
77 | File.call(this);
|
78 | this.setMode(438);
|
79 | }
|
80 | util.inherits(StandardInput, File);
|
81 |
|
82 | exports.StandardInput = StandardInput;
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function StandardOutput() {
|
89 | File.call(this);
|
90 | this.setMode(438);
|
91 | }
|
92 | util.inherits(StandardOutput, File);
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | StandardOutput.prototype.setContent = function(content) {
|
99 | if (process.stdout.isTTY) {
|
100 | process.stdout.write(content);
|
101 | }
|
102 | };
|
103 |
|
104 | exports.StandardOutput = StandardOutput;
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function StandardError() {
|
111 | File.call(this);
|
112 | this.setMode(438);
|
113 | }
|
114 | util.inherits(StandardError, File);
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | StandardError.prototype.setContent = function(content) {
|
121 | if (process.stderr.isTTY) {
|
122 | process.stderr.write(content);
|
123 | }
|
124 | };
|
125 |
|
126 | exports.StandardError = StandardError;
|