-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
333 lines (309 loc) · 10.1 KB
/
index.js
File metadata and controls
333 lines (309 loc) · 10.1 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
const Addon = require('./build/Release/BundlesAddon.node');
const fs = require('fs');
const check = require('check-types');
const Q = require('q');
/**
* Types of bundle attributes
* @enum {string}
*/
const BundleAttributeType = {
SYSTEM: 'System',
PUBLIC: 'Public',
PRIVATE: 'Private'
};
class AggregionBundle {
/**
* Constructs a new instance
* @param {object} options
* @param {string} options.path Path to file
* @param {boolean} [options.readonly] Open for read-only
*/
constructor(options) {
check.assert.assigned(options, '"options" is required argument');
check.assert.assigned(options.path, '"options.path" is required argument');
check.assert.nonEmptyString(options.path, '"options.path" should be non-empty string');
this._closed = false;
let {path} = options;
let mode = options.readonly ? ['Read'] : ['Read', 'Write', 'OpenAlways'];
this._bundle = new Addon.Bundle(path, mode);
}
/**
* Returns list of files in the bundle
* @return {Promise.<string[]>}
*/
getFiles() {
this._checkNotClosed();
let {_bundle: bundle} = this;
return Promise.resolve(bundle.FileNames());
}
/**
* Returns bundle info data
* @return {Promise.<Buffer>}
*/
getBundleInfoData() {
this._checkNotClosed();
return this._readBundleAttribute(BundleAttributeType.PUBLIC);
}
/**
* Sets bundle info data
* @param {Buffer|string} data
* @return {Promise}
*/
setBundleInfoData(data) {
this._checkNotClosed();
check.assert.assigned(data, '"data" is required argument');
return this._writeBundleAttribute(BundleAttributeType.PUBLIC, data);
}
/**
* Returns bundle properties data
* @return {Promise.<Buffer>}
*/
getBundlePropertiesData() {
this._checkNotClosed();
return this._readBundleAttribute(BundleAttributeType.PRIVATE);
}
/**
* Sets bundle properties data
* @param {Buffer|string} data
* @return {Promise}
*/
setBundlePropertiesData(data) {
this._checkNotClosed();
check.assert.assigned(data, '"data" is required argument');
return this._writeBundleAttribute(BundleAttributeType.PRIVATE, data);
}
/**
* Creates a new file
* @param {string} path Path to the file in the bundle
* @return {Promise.<number>} File descriptor
*/
createFile(path) {
this._checkNotClosed();
check.assert.assigned(path, '"path" is required argument');
check.assert.nonEmptyString(path, '"path" should be non-empty string');
return Promise.resolve(this._openFile(path, true));
}
/**
* Opens an existent file
* @param {string} path Path to the file in the bundle
* @return {number} File descriptor
*/
openFile(path) {
this._checkNotClosed();
check.assert.assigned(path, '"path" is required argument');
check.assert.nonEmptyString(path, '"path" should be non-empty string');
return this._openFile(path, false);
}
/**
* Deletes the file
* @param {string} path Path to the file in the bundle
*/
deleteFile(path) {
this._checkNotClosed();
check.assert.assigned(path, '"path" is required argument');
check.assert.nonEmptyString(path, '"path" should be non-empty string');
let {_bundle: bundle} = this;
let d = this.openFile(path);
bundle.FileDelete(d);
}
/**
* Returns size of the file
* @param {string} path Path to the file in the bundle
* @return {number}
*/
getFileSize(path) {
this._checkNotClosed();
check.assert.assigned(path, '"path" is required argument');
check.assert.nonEmptyString(path, '"path" should be non-empty string');
let {_bundle: bundle} = this;
let d = this.openFile(path);
return bundle.FileLength(d);
}
/**
* Move to position in the file
* @param {number} fd File descriptor
* @param {number} position Position in the file to seek (in bytes)
*/
seekFile(fd, position) {
this._checkNotClosed();
check.assert.assigned(fd, '"fd" is required argument');
check.assert.assigned(position, '"position" is required argument');
check.assert.integer(position, '"position" should be integer');
check.assert.greaterOrEqual(position, 0, '"position" should be greater or equal to 0');
let {_bundle: bundle} = this;
bundle.FileSeek(fd, position, 'Set');
}
/**
* Reads a block of data from the file
* @param {number} fd File descriptor
* @param {number} size Size of block to read
* @return {Promise.<Buffer>} Block data
*/
readFileBlock(fd, size) {
this._checkNotClosed();
check.assert.assigned(fd, '"fd" is required argument');
check.assert.assigned(size, '"size" is required argument');
check.assert.integer(size, '"size" should be integer');
check.assert.greaterOrEqual(size, 0, '"size" should be greater or equal to 0');
let {_bundle: bundle} = this;
let def = Q.defer();
bundle.FileRead(fd, size, (err, data) => {
if (err) {
def.reject(new Error(err));
} else {
def.resolve(data);
}
});
return def.promise;
}
/**
* Reads attributes data from file
* @param {number} fd File descriptor
* @return {Promise.<Buffer>} Attributes data
*/
readFilePropertiesData(fd) {
this._checkNotClosed();
check.assert.assigned(fd, '"fd" is required argument');
let {_bundle: bundle} = this;
let def = Q.defer();
bundle.FileAttributeGet(fd, (err, data) => {
if (err) {
def.reject(new Error(err));
} else {
def.resolve(data);
}
});
return def.promise;
}
/**
* Writes block of data to the file
* @param {number} fd File descriptor
* @param {Buffer|string} data Data to write
* @return {Promise}
*/
writeFileBlock(fd, data) {
this._checkNotClosed();
check.assert.assigned(fd, '"fd" is required argument');
check.assert.assigned(data, '"data" is required argument');
if (typeof data === 'string') {
data = new Buffer(data, 'UTF-8');
}
if (!(data instanceof Buffer)) {
throw new Error('"data" should be Buffer or string');
}
let {_bundle: bundle} = this;
let def = Q.defer();
bundle.FileWrite(fd, data, (err) => {
if (err) {
def.reject(new Error(err));
} else {
def.resolve();
}
});
return def.promise;
}
/**
* Writes file attributes
* @param {number} fd File descriptor
* @param {Buffer|string} data Data to write
* @return {Promise}
*/
writeFilePropertiesData(fd, data) {
this._checkNotClosed();
check.assert.assigned(fd, '"fd" is required argument');
check.assert.assigned(data, '"data" is required argument');
if (typeof data === 'string') {
data = new Buffer(data, 'UTF-8');
}
if (!(data instanceof Buffer)) {
throw new Error('"data" should be Buffer or string');
}
let {_bundle: bundle} = this;
let def = Q.defer();
bundle.FileAttributeSet(fd, data, (err) => {
if (err) {
def.reject(new Error(err));
} else {
def.resolve();
}
});
return def.promise;
}
/**
* Closes the bundle
*/
close() {
this._checkNotClosed();
let {_bundle: bundle} = this;
bundle.Close();
this._closed = true;
}
/**
* Opens file for read or write
* @param {string} path Path to file in the bundle
* @param {boolean} [create=false] If "true", then file will be created
* @return {number} File descriptor
* @private
*/
_openFile(path, create = false) {
check.assert.nonEmptyString(path, '"path" is required and should be non-empty string');
check.assert.boolean(create, '"create" should be boolean');
let {_bundle: bundle} = this;
return bundle.FileOpen(path, create);
}
/**
* Writes attribute to bundle
* @param {BundleAttributeType} type Type of attribute
* @param {Buffer|string} data Data to write
* @return {Promise}
* @private
* @throws {Error} Will throw if invalid arguments passed
*/
_writeBundleAttribute(type, data) {
check.assert.nonEmptyString(type, '"type" should be non-empty string');
check.assert.assigned(data, '"data" is required argument');
let {_bundle: bundle} = this;
if (typeof data === 'string') {
data = new Buffer(data, 'UTF-8');
}
if (!(data instanceof Buffer)) {
throw new Error('"data" should be Buffer or string');
}
let def = Q.defer();
bundle.AttributeSet(type, data, (err) => {
if (err) {
def.reject(new Error(err));
} else {
def.resolve();
}
});
return def.promise;
}
/**
* Reads attribute from bundle
* @param {BundleAttributeType} type
* @return {Promise.<Buffer>}
* @private
*/
_readBundleAttribute(type) {
check.assert.nonEmptyString(type, '"type" should be non-empty string');
let {_bundle: bundle} = this;
let def = Q.defer();
bundle.AttributeGet(type, (err, data) => {
if (err) {
def.reject(new Error(err));
} else {
def.resolve(data);
}
});
return def.promise;
}
/**
* Checks that bundle is not closed
* @private
*/
_checkNotClosed() {
check.assert.equal(this._closed, false, 'You can\'t do anything with bundle after close');
}
}
module.exports = AggregionBundle;