aboutsummaryrefslogtreecommitdiff
path: root/node_modules/istanbul/lib/store
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-03 15:35:00 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-03 15:35:00 +0200
commitde98e0b232509d5f40c135d540a70e415272ff85 (patch)
treea79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/istanbul/lib/store
parente0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff)
node_modules
Diffstat (limited to 'node_modules/istanbul/lib/store')
-rw-r--r--node_modules/istanbul/lib/store/fslookup.js61
-rw-r--r--node_modules/istanbul/lib/store/index.js123
-rw-r--r--node_modules/istanbul/lib/store/memory.js56
-rw-r--r--node_modules/istanbul/lib/store/tmp.js81
4 files changed, 321 insertions, 0 deletions
diff --git a/node_modules/istanbul/lib/store/fslookup.js b/node_modules/istanbul/lib/store/fslookup.js
new file mode 100644
index 000000000..b00cc179c
--- /dev/null
+++ b/node_modules/istanbul/lib/store/fslookup.js
@@ -0,0 +1,61 @@
+/*
+ Copyright (c) 2012, Yahoo! Inc. All rights reserved.
+ Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
+ */
+
+var util = require('util'),
+ fs = require('fs'),
+ Store = require('./index');
+
+/**
+ * a `Store` implementation that doesn't actually store anything. It assumes that keys
+ * are absolute file paths, and contents are contents of those files.
+ * Thus, `set` for this store is no-op, `get` returns the
+ * contents of the filename that the key represents, `hasKey` returns true if the key
+ * supplied is a valid file path and `keys` always returns an empty array.
+ *
+ * Usage
+ * -----
+ *
+ * var store = require('istanbul').Store.create('fslookup');
+ *
+ *
+ * @class LookupStore
+ * @extends Store
+ * @module store
+ * @constructor
+ */
+function LookupStore(opts) {
+ Store.call(this, opts);
+}
+
+LookupStore.TYPE = 'fslookup';
+util.inherits(LookupStore, Store);
+
+Store.mix(LookupStore, {
+ keys: function () {
+ return [];
+ },
+ get: function (key) {
+ return fs.readFileSync(key, 'utf8');
+ },
+ hasKey: function (key) {
+ var stats;
+ try {
+ stats = fs.statSync(key);
+ return stats.isFile();
+ } catch (ex) {
+ return false;
+ }
+ },
+ set: function (key /*, contents */) {
+ if (!this.hasKey(key)) {
+ throw new Error('Attempt to set contents for non-existent file [' + key + '] on a fslookup store');
+ }
+ return key;
+ }
+});
+
+
+module.exports = LookupStore;
+
diff --git a/node_modules/istanbul/lib/store/index.js b/node_modules/istanbul/lib/store/index.js
new file mode 100644
index 000000000..85ffc4f0a
--- /dev/null
+++ b/node_modules/istanbul/lib/store/index.js
@@ -0,0 +1,123 @@
+/*
+ Copyright (c) 2012, Yahoo! Inc. All rights reserved.
+ Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
+ */
+
+var Factory = require('../util/factory'),
+ factory = new Factory('store', __dirname, false);
+/**
+ * An abstraction for keeping track of content against some keys (e.g.
+ * original source, instrumented source, coverage objects against file names).
+ * This class is both the base class as well as a factory for `Store` implementations.
+ *
+ * Usage
+ * -----
+ *
+ * var Store = require('istanbul').Store,
+ * store = Store.create('memory');
+ *
+ * //basic use
+ * store.set('foo', 'foo-content');
+ * var content = store.get('foo');
+ *
+ * //keys and values
+ * store.keys().forEach(function (key) {
+ * console.log(key + ':\n' + store.get(key);
+ * });
+ * if (store.hasKey('bar') { console.log(store.get('bar'); }
+ *
+ *
+ * //syntactic sugar
+ * store.setObject('foo', { foo: true });
+ * console.log(store.getObject('foo').foo);
+ *
+ * store.dispose();
+ *
+ * @class Store
+ * @constructor
+ * @module store
+ * @param {Object} options Optional. The options supported by a specific store implementation.
+ * @main store
+ */
+function Store(/* options */) {}
+
+//add register, create, mix, loadAll, getStoreList as class methods
+factory.bindClassMethods(Store);
+
+/**
+ * registers a new store implementation.
+ * @method register
+ * @static
+ * @param {Function} constructor the constructor function for the store. This function must have a
+ * `TYPE` property of type String, that will be used in `Store.create()`
+ */
+/**
+ * returns a store implementation of the specified type.
+ * @method create
+ * @static
+ * @param {String} type the type of store to create
+ * @param {Object} opts Optional. Options specific to the store implementation
+ * @return {Store} a new store of the specified type
+ */
+
+Store.prototype = {
+ /**
+ * sets some content associated with a specific key. The manner in which
+ * duplicate keys are handled for multiple `set()` calls with the same
+ * key is implementation-specific.
+ *
+ * @method set
+ * @param {String} key the key for the content
+ * @param {String} contents the contents for the key
+ */
+ set: function (/* key, contents */) { throw new Error("set: must be overridden"); },
+ /**
+ * returns the content associated to a specific key or throws if the key
+ * was not `set`
+ * @method get
+ * @param {String} key the key for which to get the content
+ * @return {String} the content for the specified key
+ */
+ get: function (/* key */) { throw new Error("get: must be overridden"); },
+ /**
+ * returns a list of all known keys
+ * @method keys
+ * @return {Array} an array of seen keys
+ */
+ keys: function () { throw new Error("keys: must be overridden"); },
+ /**
+ * returns true if the key is one for which a `get()` call would work.
+ * @method hasKey
+ * @param {String} key
+ * @return true if the key is valid for this store, false otherwise
+ */
+ hasKey: function (/* key */) { throw new Error("hasKey: must be overridden"); },
+ /**
+ * lifecycle method to dispose temporary resources associated with the store
+ * @method dispose
+ */
+ dispose: function () {},
+ /**
+ * sugar method to return an object associated with a specific key. Throws
+ * if the content set against the key was not a valid JSON string.
+ * @method getObject
+ * @param {String} key the key for which to return the associated object
+ * @return {Object} the object corresponding to the key
+ */
+ getObject: function (key) {
+ return JSON.parse(this.get(key));
+ },
+ /**
+ * sugar method to set an object against a specific key.
+ * @method setObject
+ * @param {String} key the key for the object
+ * @param {Object} object the object to be stored
+ */
+ setObject: function (key, object) {
+ return this.set(key, JSON.stringify(object));
+ }
+};
+
+module.exports = Store;
+
+
diff --git a/node_modules/istanbul/lib/store/memory.js b/node_modules/istanbul/lib/store/memory.js
new file mode 100644
index 000000000..ff96fbd32
--- /dev/null
+++ b/node_modules/istanbul/lib/store/memory.js
@@ -0,0 +1,56 @@
+/*
+ Copyright (c) 2012, Yahoo! Inc. All rights reserved.
+ Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
+ */
+
+var util = require('util'),
+ Store = require('./index');
+
+/**
+ * a `Store` implementation using an in-memory object.
+ *
+ * Usage
+ * -----
+ *
+ * var store = require('istanbul').Store.create('memory');
+ *
+ *
+ * @class MemoryStore
+ * @extends Store
+ * @module store
+ * @constructor
+ */
+function MemoryStore() {
+ Store.call(this);
+ this.map = {};
+}
+
+MemoryStore.TYPE = 'memory';
+util.inherits(MemoryStore, Store);
+
+Store.mix(MemoryStore, {
+ set: function (key, contents) {
+ this.map[key] = contents;
+ },
+
+ get: function (key) {
+ if (!this.hasKey(key)) {
+ throw new Error('Unable to find entry for [' + key + ']');
+ }
+ return this.map[key];
+ },
+
+ hasKey: function (key) {
+ return this.map.hasOwnProperty(key);
+ },
+
+ keys: function () {
+ return Object.keys(this.map);
+ },
+
+ dispose: function () {
+ this.map = {};
+ }
+});
+
+module.exports = MemoryStore;
diff --git a/node_modules/istanbul/lib/store/tmp.js b/node_modules/istanbul/lib/store/tmp.js
new file mode 100644
index 000000000..31789c88b
--- /dev/null
+++ b/node_modules/istanbul/lib/store/tmp.js
@@ -0,0 +1,81 @@
+/*
+ Copyright (c) 2012, Yahoo! Inc. All rights reserved.
+ Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
+ */
+
+var util = require('util'),
+ path = require('path'),
+ os = require('os'),
+ fs = require('fs'),
+ mkdirp = require('mkdirp'),
+ Store = require('./index');
+
+function makeTempDir() {
+ var dir = path.join(os.tmpdir ? os.tmpdir() : /* istanbul ignore next */ (process.env.TMPDIR || '/tmp'), 'ts' + new Date().getTime());
+ mkdirp.sync(dir);
+ return dir;
+}
+/**
+ * a `Store` implementation using temporary files.
+ *
+ * Usage
+ * -----
+ *
+ * var store = require('istanbul').Store.create('tmp');
+ *
+ *
+ * @class TmpStore
+ * @extends Store
+ * @module store
+ * @param {Object} opts Optional.
+ * @param {String} [opts.tmp] a pre-existing directory to use as the `tmp` directory. When not specified, a random directory
+ * is created under `os.tmpdir()`
+ * @constructor
+ */
+function TmpStore(opts) {
+ opts = opts || {};
+ this.tmp = opts.tmp || makeTempDir();
+ this.map = {};
+ this.seq = 0;
+ this.prefix = 't' + new Date().getTime() + '-';
+}
+
+TmpStore.TYPE = 'tmp';
+util.inherits(TmpStore, Store);
+
+Store.mix(TmpStore, {
+ generateTmpFileName: function () {
+ this.seq += 1;
+ return path.join(this.tmp, this.prefix + this.seq + '.tmp');
+ },
+
+ set: function (key, contents) {
+ var tmpFile = this.generateTmpFileName();
+ fs.writeFileSync(tmpFile, contents, 'utf8');
+ this.map[key] = tmpFile;
+ },
+
+ get: function (key) {
+ var tmpFile = this.map[key];
+ if (!tmpFile) { throw new Error('Unable to find tmp entry for [' + tmpFile + ']'); }
+ return fs.readFileSync(tmpFile, 'utf8');
+ },
+
+ hasKey: function (key) {
+ return !!this.map[key];
+ },
+
+ keys: function () {
+ return Object.keys(this.map);
+ },
+
+ dispose: function () {
+ var map = this.map;
+ Object.keys(map).forEach(function (key) {
+ fs.unlinkSync(map[key]);
+ });
+ this.map = {};
+ }
+});
+
+module.exports = TmpStore;