-
Notifications
You must be signed in to change notification settings - Fork 20
/
FileStream.js
72 lines (59 loc) · 1.62 KB
/
FileStream.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
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require /*, exports, module*/) {
"use strict";
var Promise = require("node_modules/spromise/dist/spromise.min");
/**
* @constructor
* File provider to read and wrtie from and to a file.
*/
function FileStream(file) {
this._file = file.handle;
}
/**
* Reads the file content
* @returns {Promise};
*/
FileStream.prototype.read = function() {
var _self = this;
return new Promise(function(resolve, reject) {
_self._file.read(function(err, content /*, stat*/) {
if (err) {
reject(err);
}
else {
resolve({
file: _self._file,
content: content
});
}
});
});
};
/**
* Writes content to file
* @param {string} content String to be written to file
* @param {Object} options
* @returns {Promise};
*/
FileStream.prototype.write = function(content, options) {
var _self = this;
return new Promise(function(resolve, reject) {
_self._file.write(content, options, function(err /*, stat*/) {
if (err) {
reject(err);
}
else {
resolve({
file: _self._file,
content: content
});
}
});
});
};
return FileStream;
});