aboutsummaryrefslogtreecommitdiff
path: root/node_modules/caching-transform
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-28 00:38:50 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-28 00:40:43 +0200
commit7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027 (patch)
tree6de9a1aebd150a23b7f8c273ec657a5d0a18fe3e /node_modules/caching-transform
parent963b7a41feb29cc4be090a2446bdfe0c1f1bcd81 (diff)
downloadwallet-core-7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027.tar.xz
add linting (and some initial fixes)
Diffstat (limited to 'node_modules/caching-transform')
-rw-r--r--node_modules/caching-transform/index.js67
-rw-r--r--node_modules/caching-transform/license21
l---------node_modules/caching-transform/node_modules/.bin/mkdirp1
-rw-r--r--node_modules/caching-transform/node_modules/write-file-atomic/LICENSE6
-rw-r--r--node_modules/caching-transform/node_modules/write-file-atomic/README.md44
-rw-r--r--node_modules/caching-transform/node_modules/write-file-atomic/index.js129
-rw-r--r--node_modules/caching-transform/node_modules/write-file-atomic/package.json38
-rw-r--r--node_modules/caching-transform/package.json49
-rw-r--r--node_modules/caching-transform/readme.md136
9 files changed, 491 insertions, 0 deletions
diff --git a/node_modules/caching-transform/index.js b/node_modules/caching-transform/index.js
new file mode 100644
index 000000000..a66a6741d
--- /dev/null
+++ b/node_modules/caching-transform/index.js
@@ -0,0 +1,67 @@
+'use strict';
+
+var mkdirp = require('mkdirp');
+var md5Hex = require('md5-hex');
+var fs = require('fs');
+var path = require('path');
+var writeFileAtomic = require('write-file-atomic');
+
+function defaultHash(input, additionalData, salt) {
+ return md5Hex([input, salt || '']);
+}
+
+function wrap(opts) {
+ if (!(opts.factory || opts.transform) || (opts.factory && opts.transform)) {
+ throw new Error('specify factory or transform but not both');
+ }
+ if (typeof opts.cacheDir !== 'string' && !opts.disableCache) {
+ throw new Error('cacheDir must be a string');
+ }
+
+ var transformFn = opts.transform;
+ var factory = opts.factory;
+ var cacheDir = opts.cacheDir;
+ var cacheDirCreated = opts.createCacheDir === false;
+ var created = transformFn && cacheDirCreated;
+ var ext = opts.ext || '';
+ var salt = opts.salt || '';
+ var shouldTransform = opts.shouldTransform;
+ var disableCache = opts.disableCache;
+ var hashFn = opts.hash || defaultHash;
+ var encoding = opts.encoding === 'buffer' ? undefined : opts.encoding || 'utf8';
+
+ function transform(input, metadata, hash) {
+ if (!created) {
+ if (!cacheDirCreated && !disableCache) {
+ mkdirp.sync(cacheDir);
+ }
+ if (!transformFn) {
+ transformFn = factory(cacheDir);
+ }
+ created = true;
+ }
+ return transformFn(input, metadata, hash);
+ }
+
+ return function (input, metadata) {
+ if (shouldTransform && !shouldTransform(input, metadata)) {
+ return input;
+ }
+ if (disableCache) {
+ return transform(input, metadata);
+ }
+
+ var hash = hashFn(input, metadata, salt);
+ var cachedPath = path.join(cacheDir, hash + ext);
+
+ try {
+ return fs.readFileSync(cachedPath, encoding);
+ } catch (e) {
+ var result = transform(input, metadata, hash);
+ writeFileAtomic.sync(cachedPath, result, encoding);
+ return result;
+ }
+ };
+}
+
+module.exports = wrap;
diff --git a/node_modules/caching-transform/license b/node_modules/caching-transform/license
new file mode 100644
index 000000000..ad5d021ed
--- /dev/null
+++ b/node_modules/caching-transform/license
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) James Talmage <james@talmage.io> (github.com/jamestalmage)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/node_modules/caching-transform/node_modules/.bin/mkdirp b/node_modules/caching-transform/node_modules/.bin/mkdirp
new file mode 120000
index 000000000..91a5f623f
--- /dev/null
+++ b/node_modules/caching-transform/node_modules/.bin/mkdirp
@@ -0,0 +1 @@
+../../../mkdirp/bin/cmd.js \ No newline at end of file
diff --git a/node_modules/caching-transform/node_modules/write-file-atomic/LICENSE b/node_modules/caching-transform/node_modules/write-file-atomic/LICENSE
new file mode 100644
index 000000000..95e65a770
--- /dev/null
+++ b/node_modules/caching-transform/node_modules/write-file-atomic/LICENSE
@@ -0,0 +1,6 @@
+Copyright (c) 2015, Rebecca Turner
+
+Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
diff --git a/node_modules/caching-transform/node_modules/write-file-atomic/README.md b/node_modules/caching-transform/node_modules/write-file-atomic/README.md
new file mode 100644
index 000000000..a9d3461db
--- /dev/null
+++ b/node_modules/caching-transform/node_modules/write-file-atomic/README.md
@@ -0,0 +1,44 @@
+write-file-atomic
+-----------------
+
+This is an extension for node's `fs.writeFile` that makes its operation
+atomic and allows you set ownership (uid/gid of the file).
+
+### var writeFileAtomic = require('write-file-atomic')<br>writeFileAtomic(filename, data, [options], callback)
+
+* filename **String**
+* data **String** | **Buffer**
+* options **Object**
+ * chown **Object**
+ * uid **Number**
+ * gid **Number**
+ * encoding **String** | **Null** default = 'utf8'
+ * mode **Number** default = 438 (aka 0666 in Octal)
+callback **Function**
+
+Atomically and asynchronously writes data to a file, replacing the file if it already
+exists. data can be a string or a buffer.
+
+The file is initially named `filename + "." + murmurhex(__filename, process.pid, ++invocations)`.
+If writeFile completes successfully then, if passed the **chown** option it will change
+the ownership of the file. Finally it renames the file back to the filename you specified. If
+it encounters errors at any of these steps it will attempt to unlink the temporary file and then
+pass the error back to the caller.
+
+If provided, the **chown** option requires both **uid** and **gid** properties or else
+you'll get an error.
+
+The **encoding** option is ignored if **data** is a buffer. It defaults to 'utf8'.
+
+Example:
+
+```javascript
+writeFileAtomic('message.txt', 'Hello Node', {chown:{uid:100,gid:50}}, function (err) {
+ if (err) throw err;
+ console.log('It\'s saved!');
+});
+```
+
+### var writeFileAtomicSync = require('write-file-atomic').sync<br>writeFileAtomicSync(filename, data, [options])
+
+The synchronous version of **writeFileAtomic**.
diff --git a/node_modules/caching-transform/node_modules/write-file-atomic/index.js b/node_modules/caching-transform/node_modules/write-file-atomic/index.js
new file mode 100644
index 000000000..7bacf32ad
--- /dev/null
+++ b/node_modules/caching-transform/node_modules/write-file-atomic/index.js
@@ -0,0 +1,129 @@
+'use strict'
+module.exports = writeFile
+module.exports.sync = writeFileSync
+module.exports._getTmpname = getTmpname // for testing
+
+var fs = require('graceful-fs')
+var chain = require('slide').chain
+var MurmurHash3 = require('imurmurhash')
+var extend = Object.assign || require('util')._extend
+
+var invocations = 0
+function getTmpname (filename) {
+ return filename + '.' +
+ MurmurHash3(__filename)
+ .hash(String(process.pid))
+ .hash(String(++invocations))
+ .result()
+}
+
+function writeFile (filename, data, options, callback) {
+ if (options instanceof Function) {
+ callback = options
+ options = null
+ }
+ if (!options) options = {}
+ fs.realpath(filename, function (_, realname) {
+ _writeFile(realname || filename, data, options, callback)
+ })
+}
+function _writeFile (filename, data, options, callback) {
+ var tmpfile = getTmpname(filename)
+
+ if (options.mode && options.chown) {
+ return thenWriteFile()
+ } else {
+ // Either mode or chown is not explicitly set
+ // Default behavior is to copy it from original file
+ return fs.stat(filename, function (err, stats) {
+ if (err || !stats) return thenWriteFile()
+
+ options = extend({}, options)
+ if (!options.mode) {
+ options.mode = stats.mode
+ }
+ if (!options.chown && process.getuid) {
+ options.chown = { uid: stats.uid, gid: stats.gid }
+ }
+ return thenWriteFile()
+ })
+ }
+
+ function thenWriteFile () {
+ chain([
+ [writeFileAsync, tmpfile, data, options.mode, options.encoding || 'utf8'],
+ options.chown && [fs, fs.chown, tmpfile, options.chown.uid, options.chown.gid],
+ options.mode && [fs, fs.chmod, tmpfile, options.mode],
+ [fs, fs.rename, tmpfile, filename]
+ ], function (err) {
+ err ? fs.unlink(tmpfile, function () { callback(err) })
+ : callback()
+ })
+ }
+
+ // doing this instead of `fs.writeFile` in order to get the ability to
+ // call `fsync`.
+ function writeFileAsync (file, data, mode, encoding, cb) {
+ fs.open(file, 'w', options.mode, function (err, fd) {
+ if (err) return cb(err)
+ if (Buffer.isBuffer(data)) {
+ return fs.write(fd, data, 0, data.length, 0, syncAndClose)
+ } else if (data != null) {
+ return fs.write(fd, String(data), 0, String(encoding), syncAndClose)
+ } else {
+ return syncAndClose()
+ }
+ function syncAndClose (err) {
+ if (err) return cb(err)
+ fs.fsync(fd, function (err) {
+ if (err) return cb(err)
+ fs.close(fd, cb)
+ })
+ }
+ })
+ }
+}
+
+function writeFileSync (filename, data, options) {
+ if (!options) options = {}
+ try {
+ filename = fs.realpathSync(filename)
+ } catch (ex) {
+ // it's ok, it'll happen on a not yet existing file
+ }
+ var tmpfile = getTmpname(filename)
+
+ try {
+ if (!options.mode || !options.chown) {
+ // Either mode or chown is not explicitly set
+ // Default behavior is to copy it from original file
+ try {
+ var stats = fs.statSync(filename)
+ options = extend({}, options)
+ if (!options.mode) {
+ options.mode = stats.mode
+ }
+ if (!options.chown && process.getuid) {
+ options.chown = { uid: stats.uid, gid: stats.gid }
+ }
+ } catch (ex) {
+ // ignore stat errors
+ }
+ }
+
+ var fd = fs.openSync(tmpfile, 'w', options.mode)
+ if (Buffer.isBuffer(data)) {
+ fs.writeSync(fd, data, 0, data.length, 0)
+ } else if (data != null) {
+ fs.writeSync(fd, String(data), 0, String(options.encoding || 'utf8'))
+ }
+ fs.fsyncSync(fd)
+ fs.closeSync(fd)
+ if (options.chown) fs.chownSync(tmpfile, options.chown.uid, options.chown.gid)
+ if (options.mode) fs.chmodSync(tmpfile, options.mode)
+ fs.renameSync(tmpfile, filename)
+ } catch (err) {
+ try { fs.unlinkSync(tmpfile) } catch (e) {}
+ throw err
+ }
+}
diff --git a/node_modules/caching-transform/node_modules/write-file-atomic/package.json b/node_modules/caching-transform/node_modules/write-file-atomic/package.json
new file mode 100644
index 000000000..5453d2b8b
--- /dev/null
+++ b/node_modules/caching-transform/node_modules/write-file-atomic/package.json
@@ -0,0 +1,38 @@
+{
+ "name": "write-file-atomic",
+ "version": "1.3.4",
+ "description": "Write files in an atomic fashion w/configurable ownership",
+ "main": "index.js",
+ "scripts": {
+ "test": "standard && tap --coverage test/*.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git@github.com:iarna/write-file-atomic.git"
+ },
+ "keywords": [
+ "writeFile",
+ "atomic"
+ ],
+ "author": "Rebecca Turner <me@re-becca.org> (http://re-becca.org)",
+ "license": "ISC",
+ "bugs": {
+ "url": "https://github.com/iarna/write-file-atomic/issues"
+ },
+ "homepage": "https://github.com/iarna/write-file-atomic",
+ "dependencies": {
+ "graceful-fs": "^4.1.11",
+ "imurmurhash": "^0.1.4",
+ "slide": "^1.1.5"
+ },
+ "devDependencies": {
+ "mkdirp": "^0.5.1",
+ "require-inject": "^1.4.0",
+ "rimraf": "^2.5.4",
+ "standard": "^9.0.2",
+ "tap": "^10.3.2"
+ },
+ "files": [
+ "index.js"
+ ]
+}
diff --git a/node_modules/caching-transform/package.json b/node_modules/caching-transform/package.json
new file mode 100644
index 000000000..b57d34f0d
--- /dev/null
+++ b/node_modules/caching-transform/package.json
@@ -0,0 +1,49 @@
+{
+ "name": "caching-transform",
+ "version": "1.0.1",
+ "description": "Wraps a transform and provides caching",
+ "license": "MIT",
+ "repository": "jamestalmage/caching-transform",
+ "author": {
+ "name": "James Talmage",
+ "email": "james@talmage.io",
+ "url": "github.com/jamestalmage"
+ },
+ "engines": {
+ "node": ">=0.10.0"
+ },
+ "scripts": {
+ "test": "xo && nyc --reporter=lcov --reporter=text ava"
+ },
+ "files": [
+ "index.js"
+ ],
+ "keywords": [
+ "transform",
+ "cache",
+ "require",
+ "transpile",
+ "fast",
+ "speed",
+ "hash"
+ ],
+ "dependencies": {
+ "md5-hex": "^1.2.0",
+ "mkdirp": "^0.5.1",
+ "write-file-atomic": "^1.1.4"
+ },
+ "devDependencies": {
+ "ava": "^0.8.0",
+ "coveralls": "^2.11.6",
+ "mock-fs": "^3.5.0",
+ "nyc": "5.0.1",
+ "proxyquire": "^1.7.3",
+ "sinon": "^1.17.2",
+ "xo": "^0.12.1"
+ },
+ "xo": {
+ "ignores": [
+ "test.js"
+ ]
+ }
+}
diff --git a/node_modules/caching-transform/readme.md b/node_modules/caching-transform/readme.md
new file mode 100644
index 000000000..cdf8b664f
--- /dev/null
+++ b/node_modules/caching-transform/readme.md
@@ -0,0 +1,136 @@
+# caching-transform [![Build Status](https://travis-ci.org/jamestalmage/caching-transform.svg?branch=master)](https://travis-ci.org/jamestalmage/caching-transform) [![Coverage Status](https://coveralls.io/repos/jamestalmage/caching-transform/badge.svg?branch=master&service=github)](https://coveralls.io/github/jamestalmage/caching-transform?branch=master)
+
+> Wraps a transform and provides caching.
+
+Caching transform results can greatly improve performance. `nyc` saw [dramatic performance increases](https://github.com/bcoe/nyc/pull/101#issuecomment-165716069) when we implemented caching.
+
+
+## Install
+
+```
+$ npm install --save caching-transform
+```
+
+
+## Usage
+
+```js
+const cachingTransform = require('caching-transform');
+
+const transform = cachingTransform({
+ cacheDir: '/path/to/cache/directory',
+ salt: 'hash-salt',
+ transform: (input, metadata, hash) => {
+ // ... expensive operations ...
+ return transformedResult;
+ }
+});
+
+transform('some input for transpilation')
+// => fetch from the cache,
+// or run the transform and save to the cache if not found there
+```
+
+
+## API
+
+### cachingTransform(options)
+
+Returns a transform callback that takes two arguments:
+
+ - `input` a string to be transformed
+ - `metadata` an arbitrary data object.
+
+Both arguments are passed to the wrapped transform. Results are cached in the cache directory using an `md5` hash of `input` and an optional `salt` value. If a cache entry already exist for `input`, the wrapped transform function will never be called.
+
+#### options
+
+##### salt
+
+Type: `string`, or `buffer`
+Default: `empty string`
+
+A value that uniquely identifies your transform:
+
+```js
+ const pkg = require('my-transform/package.json');
+ const salt = pkg.name + ':' + pkg.version;
+```
+
+Including the version in the salt ensures existing cache entries will be automatically invalidated when you bump the version of your transform. If your transform relies on additional dependencies, and the transform output might change as those dependencies update, then your salt should incorporate the versions of those dependencies as well.
+
+##### transform
+
+Type: `Function(input: string|buffer, metadata: *, hash: string): string|buffer`
+
+ - `input`: The value to be transformed. It is passed through from the wrapper.
+ - `metadata`: An arbitrary data object passed through from the wrapper. A typical value might be a string filename.
+ - `hash`: The salted hash of `input`. Useful if you intend to create additional cache entries beyond the transform result (i.e. `nyc` also creates cache entries for source-map data). This value is not available if the cache is disabled, if you still need it, the default can be computed via [`md5Hex([input, salt])`](https://www.npmjs.com/package/md5-hex).
+
+The transform function will return a `string` (or Buffer if `encoding === 'buffer'`) containing the result of transforming `input`.
+
+##### factory
+
+Type: `Function(cacheDir: string): transformFunction`
+
+If the `transform` function is expensive to create, and it is reasonable to expect that it may never be called during the life of the process, you may supply a `factory` function that will be used to create the `transform` function the first time it is needed.
+
+A typical usage would be to prevent eagerly `require`ing expensive dependencies like Babel:
+
+```js
+function factory() {
+ // Using the factory function, you can avoid loading Babel until you are sure it is needed.
+ var babel = require('babel-core');
+
+ return function (code, metadata) {
+ return babel.transform(code, {filename: metadata.filename, plugins: [/* ... */]});
+ };
+}
+```
+
+##### cacheDir
+
+Type: `string`
+*Required unless caching is disabled*
+
+The directory where cached transform results will be stored. The directory is automatically created with [`mkdirp`](https://www.npmjs.com/package/mkdirp). You can set `options.createCacheDir = false` if you are certain the directory already exists.
+
+##### ext
+
+Type: `string`
+Default: `empty string`
+
+An extension that will be appended to the salted hash to create the filename inside your cache directory. It is not required, but recommended if you know the file type. Appending the extension allows you to easily inspect the contents of the cache directory with your file browser.
+
+##### shouldTransform
+
+Type: `Function(input: string|buffer, additonalData: *)`
+Default: `always transform`
+
+A function that examines `input` and `metadata` to determine whether the transform should be applied. Returning `false` means the transform will not be applied and `input` will be returned unmodified.
+
+##### disableCache
+
+Type: `boolean`
+Default: `false`
+
+If `true`, the cache is ignored and the transform is used every time regardless of cache contents.
+
+##### hash
+
+Type: `Function(input: string|buffer, metadata: *, salt: string): string`
+
+Provide a custom hashing function for the given input. The default hashing function does not take the `metadata` into account:
+
+> [`md5Hex([input, salt])`](https://www.npmjs.com/package/md5-hex)
+
+##### encoding
+
+Type: `string`
+Default: `utf8`
+
+The encoding to use when writing to / reading from the filesystem. If set to `"buffer"`, then buffers will be returned from the cache instead of strings.
+
+## License
+
+MIT © [James Talmage](http://github.com/jamestalmage)