-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
100 lines (92 loc) · 2.44 KB
/
index.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var Stream = require('stream');
var isBuffer = function (o) { return Buffer.isBuffer(o); };
var isStream = function (o) { return !!o && o instanceof Stream; };
var isNull = function (o) { return o === null; };
var isValid = function (o) { return isBuffer(o)||isStream(o)||isNull(o); }
function Record (file) {
if (!file) {
file = {};
}
this.path = file.path||null;
this.encoding = file.encoding||null;
if(typeof file.contents === 'string') {
this.contents = new Buffer(file.contents, file.encoding);
} else {
this.contents = file.contents||null;
}
}
Record.prototype.isBuffer = function () { return isBuffer(this.contents); };
Record.prototype.isStream = function () { return isStream(this.contents); };
Record.prototype.isNull = function () { return isNull(this.contents); };
Record.prototype.type = function () {
var type = 'Null';
if(this.isBuffer()) {
type = 'Buffer';
}
if(this.isStream()) {
type = this.contents.constructor.name;
if (type.toLowerCase().search('stream') === -1) {
type += 'Stream';
}
}
return type;
}
Record.prototype.clone = function() {
var contents = this.contents;
if (this.isBuffer()) {
contents = new Buffer(contents.length);
this.contents.copy(contents);
}
return new this.constructor({
path: this.path,
encoding: this.encoding,
contents: contents
});
};
Record.prototype.toString = function (encoding) {
encoding = encoding||this.encoding;
if (!this.isBuffer()) {
throw new Error('toString is only valid for Buffer backed Records.');
}
return this.contents.toString(encoding);
}
Record.prototype.pipe = function(stream, opt) {
if (!opt) {
opt = {};
}
if (typeof opt.end === 'undefined') {
opt.end = true;
}
if (this.isStream()) {
return this.contents.pipe(stream, opt);
}
if (this.isBuffer()) {
if (opt.end) {
stream.end(this.contents);
} else {
stream.write(this.contents);
}
return stream;
}
if (this.isNull()) {
if (opt.end) {
stream.end();
}
return stream;
}
};
Record.prototype.inspect = function() {
return '<Record::'+this.type()+(this.path?' path="'+this.path+'"':'')+'>';
};
Object.defineProperty(Record.prototype, 'contents', {
get: function() {
return this._contents;
},
set: function(val) {
if (!isValid(val)) {
throw new Error('Contents can only be a Buffer, a Stream, or null.');
}
this._contents = val;
}
});
module.exports = Record;