forked from smikes/fez
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.js
More file actions
38 lines (33 loc) · 802 Bytes
/
input.js
File metadata and controls
38 lines (33 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var Promise = require("bluebird"),
fs = require("fs");
function Input(input) {
if(typeof input === "string") {
this._filename = input;
} else if(input instanceof Buffer) {
this._buffer = input;
}
}
Input.prototype.asBuffer = function() {
if(this._filename) {
var file = this._filename;
return new Promise(function(resolve, reject) {
fs.readFile(file, function(err, data) {
if(err) reject(err);
else resolve(data);
});
});
} else if (this._buffer) {
return this._buffer;
} else {
throw new Error("What?");
}
};
Input.prototype.asStream = function() {
if(this._filename) {
return fs.createReadStream(this._filename);
}
};
Input.prototype.getFilename = function() {
return this._filename;
};
module.exports = Input;