1 |
|
/* |
2 |
|
Copyright (c) 2012, Yahoo! Inc. All rights reserved. |
3 |
|
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. |
4 |
|
*/ |
5 |
|
|
6 |
1 |
var util = require('util'), |
7 |
|
fs = require('fs'), |
8 |
|
Store = require('./index'); |
9 |
|
|
10 |
|
/** |
11 |
|
* a `Store` implementation that doesn't actually store anything. It assumes that keys |
12 |
|
* are absolute file paths, and contents are contents of those files. |
13 |
|
* Thus, `set` for this store is no-op, `get` returns the |
14 |
|
* contents of the filename that the key represents, `hasKey` returns true if the key |
15 |
|
* supplied is a valid file path and `keys` always returns an empty array. |
16 |
|
* |
17 |
|
* Usage |
18 |
|
* ----- |
19 |
|
* |
20 |
|
* var store = require('istanbul').Store.create('fslookup'); |
21 |
|
* |
22 |
|
* |
23 |
|
* @class LookupStore |
24 |
|
* @extends Store |
25 |
|
* @constructor |
26 |
|
*/ |
27 |
1 |
function LookupStore(opts) { |
28 |
9 |
Store.call(this, opts); |
29 |
|
} |
30 |
|
|
31 |
1 |
LookupStore.TYPE = 'fslookup'; |
32 |
1 |
util.inherits(LookupStore, Store); |
33 |
|
|
34 |
1 |
Store.mix(LookupStore, { |
35 |
|
keys: function () { |
36 |
1 |
return []; |
37 |
|
}, |
38 |
|
get: function (key) { |
39 |
19 |
return fs.readFileSync(key, 'utf8'); |
40 |
|
}, |
41 |
|
hasKey: function (key) { |
42 |
5 |
var stats; |
43 |
5 |
try { |
44 |
5 |
stats = fs.statSync(key); |
45 |
3 |
return stats.isFile(); |
46 |
|
} catch (ex) { |
47 |
2 |
return false; |
48 |
|
} |
49 |
|
}, |
50 |
|
set: function (key, contents) { |
51 |
2 |
if (!this.hasKey(key)) { |
52 |
1 |
throw new Error('Attempt to set contents for non-existent file [' + key + '] on a fslookup store'); |
53 |
|
} |
54 |
1 |
return key; |
55 |
|
} |
56 |
|
}); |
57 |
|
|
58 |
|
|
59 |
1 |
module.exports = LookupStore; |
60 |
|
|
61 |
|
|