Debian Codegraph @

lib/store/fslookup.js

raw symbols SCIP
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 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 * @module store
26 * @constructor
27 */
28 function LookupStore(opts) {
29 Store.call(this, opts);
30 }
31
32 LookupStore.TYPE = 'fslookup';
33 util.inherits(LookupStore, Store);
34
35 Store.mix(LookupStore, {
36 keys: function () {
37 return [];
38 },
39 get: function (key) {
40 return fs.readFileSync(key, 'utf8');
41 },
42 hasKey: function (key) {
43 var stats;
44 try {
45 stats = fs.statSync(key);
46 return stats.isFile();
47 } catch (ex) {
48 return false;
49 }
50 },
51 set: function (key /*, contents */) {
52 if (!this.hasKey(key)) {
53 throw new Error('Attempt to set contents for non-existent file [' + key + '] on a fslookup store');
54 }
55 return key;
56 }
57 });
58
59
60 module.exports = LookupStore;
61