diff options
Diffstat (limited to 'node_modules/package-hash')
-rw-r--r-- | node_modules/package-hash/LICENSE | 14 | ||||
-rw-r--r-- | node_modules/package-hash/README.md | 79 | ||||
-rw-r--r-- | node_modules/package-hash/index.js | 170 | ||||
-rw-r--r-- | node_modules/package-hash/node_modules/md5-hex/browser.js | 10 | ||||
-rw-r--r-- | node_modules/package-hash/node_modules/md5-hex/index.js | 23 | ||||
-rw-r--r-- | node_modules/package-hash/node_modules/md5-hex/license | 21 | ||||
-rw-r--r-- | node_modules/package-hash/node_modules/md5-hex/package.json | 39 | ||||
-rw-r--r-- | node_modules/package-hash/node_modules/md5-hex/readme.md | 46 | ||||
-rw-r--r-- | node_modules/package-hash/package.json | 60 |
9 files changed, 462 insertions, 0 deletions
diff --git a/node_modules/package-hash/LICENSE b/node_modules/package-hash/LICENSE new file mode 100644 index 000000000..ddc33e02a --- /dev/null +++ b/node_modules/package-hash/LICENSE @@ -0,0 +1,14 @@ +ISC License (ISC) +Copyright (c) 2016-2017, Mark Wubben <mark@novemberborn.net> (novemberborn.net) + +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/package-hash/README.md b/node_modules/package-hash/README.md new file mode 100644 index 000000000..d2f29c8fa --- /dev/null +++ b/node_modules/package-hash/README.md @@ -0,0 +1,79 @@ +# package-hash + +Generates a hash for an installed npm package, useful for salting caches. +[AVA](https://github.com/sindresorhus/ava) for example caches precompiled test +files. It generates a salt for its cache based on the various packages that are +used when compiling the test files. + +`package-hash` can generate an appropriate hash based on the package location +(on disk) and the `package.json` file. This hash is salted with a hash +for the `package-hash` itself. + +`package-hash` can detect when the package-to-be-hashed is a Git repository. In +the AVA example this is useful when you're debugging one of the packages used to +compile the test files. You can clone it locally and use `npm link` so AVA can +find the clone. The hash will include the HEAD (`.git/HEAD`) and its +corresponding ref (e.g. `.git/refs/heads/master`), any packed refs +(`.git/packed-refs`), as well as the diff (`git diff`) for any non-committed +changes. This makes it really easy to test your changes without having to +explicitly clear the cache in the parent project. + +## Installation + +```console +$ npm install --save package-hash +``` + +## Usage + +```js +const packageHash = require('package-hash') + +// Asynchronously: +const hash = await packageHash(require.resolve('babel-core/package.json')) + +// Synchronously: +const hash = packageHash.sync(require.resolve('babel-core/package.json')) +``` + +`packageHash()` / `packageHash.sync()` must be called with a file path for an +existing `package.json` file. To get the path to an npm package it's easiest to +use `require.resolve('the-name/package.json')`. + +You can provide multiple paths: + +```js +const hash = await packageHash([ + require.resolve('babel-core/package.json'), + require.resolve('babel-preset-es2015/package.json') +]) +``` + +An optional salt value can also be provided: + +```js +const hash = await packageHash(require.resolve('babel-core/package.json'), 'salt value') +``` + +## API + +### `packageHash(paths, salt?)` + +`paths: string | string[]` ➜ can be a single file path, or an array of paths. + +`salt: Array | Buffer | Object | string` ➜ optional. If an `Array` or `Object` (not `null`) it is first converted to a JSON string. + +Returns a promise for the hex-encoded hash string. + +### `packageHash.sync(paths, salt?)` + +`paths: string | string[]` ➜ can be a single file path, or an array of paths. + +`salt: Array | Buffer | Object | string` ➜ optional. If an `Array` or `Object` (not `null`) it is first converted to a JSON string. + +Returns a hex-encoded hash string. + +## Compatibility + +`package-hash` has been tested with Node.js 4 and above, including Windows +support. diff --git a/node_modules/package-hash/index.js b/node_modules/package-hash/index.js new file mode 100644 index 000000000..8947a02d9 --- /dev/null +++ b/node_modules/package-hash/index.js @@ -0,0 +1,170 @@ +'use strict' + +const cp = require('child_process') +const fs = require('fs') +const path = require('path') + +const gfs = require('graceful-fs') +const flattenDeep = require('lodash.flattendeep') +const md5hex = require('md5-hex') +const releaseZalgo = require('release-zalgo') + +const PACKAGE_FILE = require.resolve('./package.json') +const TEN_MEBIBYTE = 1024 * 1024 * 10 + +const readFile = { + async (file) { + return new Promise((resolve, reject) => { + gfs.readFile(file, (err, contents) => { + err ? reject(err) : resolve(contents) + }) + }) + }, + sync (file) { + return fs.readFileSync(file) + } +} + +const tryReadFile = { + async (file) { + return new Promise(resolve => { + gfs.readFile(file, (err, contents) => { + resolve(err ? null : contents) + }) + }) + }, + + sync (file) { + try { + return fs.readFileSync(file) + } catch (err) { + return null + } + } +} + +const tryExecFile = { + async (file, args, options) { + return new Promise(resolve => { + cp.execFile(file, args, options, (err, stdout) => { + resolve(err ? null : stdout) + }) + }) + }, + + sync (file, args, options) { + try { + return cp.execFileSync(file, args, options) + } catch (err) { + return null + } + } +} + +const git = { + tryGetRef (zalgo, dir, head) { + const m = /^ref: (.+)$/.exec(head.toString('utf8').trim()) + if (!m) return null + + return zalgo.run(tryReadFile, path.join(dir, '.git', m[1])) + }, + + tryGetDiff (zalgo, dir) { + return zalgo.run(tryExecFile, + 'git', + // Attempt to get consistent output no matter the platform. Diff both + // staged and unstaged changes. + ['--no-pager', 'diff', 'HEAD', '--no-color', '--no-ext-diff'], + { + cwd: dir, + maxBuffer: TEN_MEBIBYTE, + env: Object.assign({}, process.env, { + // Force the GIT_DIR to prevent git from diffing a parent repository + // in case the directory isn't actually a repository. + GIT_DIR: path.join(dir, '.git') + }), + // Ignore stderr. + stdio: ['ignore', 'pipe', 'ignore'] + }) + } +} + +function addPackageData (zalgo, pkgPath) { + const dir = path.dirname(pkgPath) + + return zalgo.all([ + dir, + zalgo.run(readFile, pkgPath), + zalgo.run(tryReadFile, path.join(dir, '.git', 'HEAD')) + .then(head => { + if (!head) return [] + + return zalgo.all([ + zalgo.run(tryReadFile, path.join(dir, '.git', 'packed-refs')), + git.tryGetRef(zalgo, dir, head), + git.tryGetDiff(zalgo, dir) + ]) + .then(results => { + return [head].concat(results.filter(Boolean)) + }) + }) + ]) +} + +function computeHash (zalgo, paths, pepper, salt) { + const inputs = [] + if (pepper) inputs.push(pepper) + + if (typeof salt !== 'undefined') { + if (Buffer.isBuffer(salt) || typeof salt === 'string') { + inputs.push(salt) + } else if (typeof salt === 'object' && salt !== null) { + inputs.push(JSON.stringify(salt)) + } else { + throw new TypeError('Salt must be an Array, Buffer, Object or string') + } + } + + return zalgo.all(paths.map(pkgPath => addPackageData(zalgo, pkgPath))) + .then(furtherInputs => md5hex(flattenDeep([inputs, furtherInputs]))) +} + +let ownHash = null +let ownHashPromise = null +function run (zalgo, paths, salt) { + if (!ownHash) { + return zalgo.run({ + async () { + if (!ownHashPromise) { + ownHashPromise = computeHash(zalgo, [PACKAGE_FILE]) + } + return ownHashPromise + }, + sync () { + return computeHash(zalgo, [PACKAGE_FILE]) + } + }) + .then(hash => { + ownHash = new Buffer(hash, 'hex') + ownHashPromise = null + return run(zalgo, paths, salt) + }) + } + + if (paths === PACKAGE_FILE && typeof salt === 'undefined') { + // Special case that allow the pepper value to be obtained. Mainly here for + // testing purposes. + return zalgo.returns(ownHash.toString('hex')) + } + + paths = Array.isArray(paths) ? paths : [paths] + return computeHash(zalgo, paths, ownHash, salt) +} + +module.exports = (paths, salt) => { + return run(releaseZalgo.async(), paths, salt) +} +module.exports.sync = (paths, salt) => { + const result = run(releaseZalgo.sync(), paths, salt) + return releaseZalgo.unwrapSync(result) +} diff --git a/node_modules/package-hash/node_modules/md5-hex/browser.js b/node_modules/package-hash/node_modules/md5-hex/browser.js new file mode 100644 index 000000000..d6c2da0bf --- /dev/null +++ b/node_modules/package-hash/node_modules/md5-hex/browser.js @@ -0,0 +1,10 @@ +'use strict'; +const md5OMatic = require('md5-o-matic'); + +module.exports = input => { + if (Array.isArray(input)) { + input = input.join(''); + } + + return md5OMatic(input); +}; diff --git a/node_modules/package-hash/node_modules/md5-hex/index.js b/node_modules/package-hash/node_modules/md5-hex/index.js new file mode 100644 index 000000000..82cfae306 --- /dev/null +++ b/node_modules/package-hash/node_modules/md5-hex/index.js @@ -0,0 +1,23 @@ +'use strict'; +const crypto = require('crypto'); + +module.exports = function (input) { + const hash = crypto.createHash('md5'); + + const update = buf => { + const inputEncoding = typeof buf === 'string' ? 'utf8' : undefined; + hash.update(buf, inputEncoding); + }; + + if (arguments.length > 1) { + throw new Error('Too many arguments. Try specifying an array.'); + } + + if (Array.isArray(input)) { + input.forEach(update); + } else { + update(input); + } + + return hash.digest('hex'); +}; diff --git a/node_modules/package-hash/node_modules/md5-hex/license b/node_modules/package-hash/node_modules/md5-hex/license new file mode 100644 index 000000000..654d0bfe9 --- /dev/null +++ b/node_modules/package-hash/node_modules/md5-hex/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) + +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/package-hash/node_modules/md5-hex/package.json b/node_modules/package-hash/node_modules/md5-hex/package.json new file mode 100644 index 000000000..a87ce154f --- /dev/null +++ b/node_modules/package-hash/node_modules/md5-hex/package.json @@ -0,0 +1,39 @@ +{ + "name": "md5-hex", + "version": "2.0.0", + "description": "Create a MD5 hash with hex encoding", + "license": "MIT", + "repository": "sindresorhus/md5-hex", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=4" + }, + "scripts": { + "test": "xo && ava" + }, + "files": [ + "index.js", + "browser.js" + ], + "keywords": [ + "hash", + "crypto", + "md5", + "hex", + "buffer", + "browser", + "browserify" + ], + "dependencies": { + "md5-o-matic": "^0.1.1" + }, + "devDependencies": { + "ava": "*", + "xo": "*" + }, + "browser": "browser.js" +} diff --git a/node_modules/package-hash/node_modules/md5-hex/readme.md b/node_modules/package-hash/node_modules/md5-hex/readme.md new file mode 100644 index 000000000..630b31d0b --- /dev/null +++ b/node_modules/package-hash/node_modules/md5-hex/readme.md @@ -0,0 +1,46 @@ +# md5-hex [](https://travis-ci.org/sindresorhus/md5-hex) + +> Create a MD5 hash with hex encoding + +*Please don't use MD5 hashes for anything sensitive!* + +Works in the browser too, when used with browserify/webpack. + +Checkout [`hasha`](https://github.com/sindresorhus/hasha) if you need something more flexible. + + +## Install + +``` +$ npm install --save md5-hex +``` + + +## Usage + +```js +const fs = require('fs'); +const md5Hex = require('md5-hex'); +const buffer = fs.readFileSync('unicorn.png'); + +md5Hex(buffer); +//=> '1abcb33beeb811dca15f0ac3e47b88d9' +``` + + +## API + +### md5Hex(input) + +#### input + +Type: `Buffer` `string` `Buffer[]` `string[]` + +Prefer buffers as they're faster to hash, but strings can be useful for small things. + +Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation. + + +## License + +MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/node_modules/package-hash/package.json b/node_modules/package-hash/package.json new file mode 100644 index 000000000..ec862b50e --- /dev/null +++ b/node_modules/package-hash/package.json @@ -0,0 +1,60 @@ +{ + "name": "package-hash", + "version": "2.0.0", + "description": "Generates a hash for an installed npm package, useful for salting caches", + "main": "index.js", + "files": [ + "index.js" + ], + "engines": { + "node": ">=4" + }, + "scripts": { + "lint": "as-i-preach", + "unpack-fixtures": "node scripts/unpack-fixtures.js", + "pregenerate-fixture-index": "npm run unpack-fixtures", + "generate-fixture-index": "node scripts/generate-fixture-index.js", + "pretest": "npm run unpack-fixtures", + "test": "ava", + "posttest": "npm run lint", + "coverage": "nyc npm test", + "watch:test": "npm run test -- --watch" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/novemberborn/package-hash.git" + }, + "author": "Mark Wubben (https://novemberborn.net/)", + "license": "ISC", + "bugs": { + "url": "https://github.com/novemberborn/package-hash/issues" + }, + "homepage": "https://github.com/novemberborn/package-hash#readme", + "dependencies": { + "graceful-fs": "^4.1.11", + "lodash.flattendeep": "^4.4.0", + "md5-hex": "^2.0.0", + "release-zalgo": "^1.0.0" + }, + "devDependencies": { + "@novemberborn/as-i-preach": "^7.0.0", + "ava": "^0.18.0", + "coveralls": "^2.11.15", + "nyc": "^10.0.0", + "rimraf": "^2.5.2", + "tar": "^2.2.1" + }, + "nyc": { + "cache": true, + "exclude": [ + "scripts", + "test" + ], + "reporter": [ + "html", + "lcov", + "text" + ] + }, + "standard-engine": "@novemberborn/as-i-preach" +} |