Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

machinepack-util

Package Overview
Dependencies
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

machinepack-util - npm Package Compare versions

Comparing version 6.1.2 to 7.0.0-0

machines/construct.js

60

machines/hash.js

@@ -7,6 +7,11 @@ module.exports = {

description: 'Generate unique string from the provided value.',
description: 'Generate a unique, deterministic string from the provided value.',
extendedDescription: 'Useful for checksums (error-checking) and hash keys (caching, etc.) Uses the crypto module from Node core via `object-hash` on NPM (see http://npmjs.org/package/object-hash)',
extendedDescription:
'Useful for checksums (error-checking) and hash keys (caching, etc.) '+
'Uses the `crypto` module from Node core via the same strategy as in the machine runner itself, '+
'(see http://npmjs.org/package/machine)\n'+
'Regardless of how deeply nested a dictionary is in the provided value, _key order does not matter_ '+
'when computing this hash string.',

@@ -17,3 +22,3 @@

cacheable: true,
sideEffects: 'cacheable',

@@ -24,3 +29,2 @@

value: {
friendlyName: 'Value',
example: '*',

@@ -37,4 +41,5 @@ description: 'The value for which to calculate a unique hash string.',

success: {
description: 'Done.',
example: 'e003c89cdf35cdf46d8239b4692436364b7259f9'
outputFriendlyName: 'Hashed value',
outputDescription: 'The unique hash derived from a value.',
outputExample: 'e003c89cdf35cdf46d8239b4692436364b7259f9'
}

@@ -47,8 +52,45 @@

var hashFn = require('object-hash');
var hash = hashFn(inputs.value);
// Import `crypto` and `lodash`.
var crypto = require('crypto');
var _ = require('lodash');
// --•
// At this point, since we can safely assume `inputs.value` has already been validated,
// we can safely trust that it is JSON-serializable.
// Build a modified ("deep-ish") clone of this value with all of its keys sorted-- recursively deep.
var sortifiedValue = (function _sortKeysRecursive(val){
// --• misc
if (!_.isObject(val)) { return val; }
// --• array
if (_.isArray(val)) {
return _.map(val, function (item){
return _sortKeysRecursive(item);
});//</_.map()>
}
// --• dictionary
var sortedSubKeys = _.keys(val).sort();
return _.reduce(sortedSubKeys, function (memo, subKey) {
memo[subKey] = _sortKeysRecursive(val[subKey]);
return memo;
}, {});//</_.reduce()>
})(inputs.value);//</invoked self-calling recursive function :: _sortKeysRecursive()>
// Now encode that as a JSON string.
var sortedAndStringifiedValue = JSON.stringify(sortifiedValue);
// Finally, compute & return an MD5 hash.
var hash = crypto.createHash('md5').update(sortedAndStringifiedValue).digest('hex');
// Return the result through the `success` exit.
return exits.success(hash);
},
}
};

@@ -14,3 +14,3 @@ module.exports = {

'\n'+
'Also note that this method uses the default behavior of `requiree()` in Node.js, so beware of circular/cyclical '+
'Also note that this method uses the default behavior of `require()` in Node.js, so beware of circular/cyclical '+
'dependencies! See [Modules/Cycles in the Node.js docs](https://nodejs.org/api/modules.html#modules_cycles) '+

@@ -23,2 +23,5 @@ 'for more information.',

sideEffects: 'cacheable',
inputs: {

@@ -31,2 +34,9 @@

required: true
},
clearCache: {
friendlyName: 'Clear cache?',
description: 'Whether to clear the requested module from the require cache before attempting to load.',
example: true,
defaultsTo: false
}

@@ -46,7 +56,7 @@

moduleNotFound: {
description: 'No module exists at the specified path.'
description: 'No module could be found at the specified path.'
},
couldNotLoad: {
description: 'A file exists at the specified path, but it could not be loaded as a Node.js module.',
description: 'A file was found at the specified path, but it could not be loaded as a Node.js module.',
extendedDescription:

@@ -65,13 +75,57 @@ 'This usually means that either the module has bugs (e.g. trying to require a module with a typo), '+

fn: function (inputs, exits) {
// Import `path`.
var path = require('path');
var absPath = path.resolve(process.cwd(), inputs.path);
// Import `lodash` and `resolve`
var _ = require('lodash');
var resolve = require('resolve');
// Look for the requested module using `resolve`, so that we can get the correct location
// relative to the current working directory (as opposed to being relative to the location
// of machinepack-utils).
var absPath;
try {
absPath = resolve.sync(inputs.path, {basedir: process.cwd()});
}
// If the path couldn't be resolved, leave through `moduleNotFound`.
catch(eCouldNotResolvePath) {
return exits.moduleNotFound(eCouldNotResolvePath);
}
// If `inputs.clearCache` is set, clear this path AND ALL CHILDREN from the require
// cache before attempting to load.
if (inputs.clearCache) {
// First, see if the item is actually cached.
if (require.cache[absPath]) {
// If so, add it to a stack of modules to remove.
var modulesToRemove = [require.cache[absPath]];
// While there are items in the stack...
while (modulesToRemove.length) {
// Pop a module off the stack.
var moduleToRemove = modulesToRemove.pop();
// Add its children to the stack.
var children = (require.cache[moduleToRemove.id] && require.cache[moduleToRemove.id].children) || [];
modulesToRemove = modulesToRemove.concat(children);
// Delete the module from the cache.
delete require.cache[moduleToRemove.id];
}
}
}
// Attempt to require the module.
try {
var result = require(absPath);
// If successful, output the module through the `success` exit.
return exits.success(result);
}
catch (e) {
// If the module could not be found, leave through `moduleNotFound`.
if (e.code === 'MODULE_NOT_FOUND') { return exits.moduleNotFound(e); }
// Otherwise leave through `couldNotLoad`.
else { return exits.couldNotLoad(e); }
}
}

@@ -78,0 +132,0 @@

24

package.json
{
"name": "machinepack-util",
"version": "6.1.2",
"version": "7.0.0-0",
"description": "Miscellaneous utilities for everyday tasks with arrays, dictionaries, strings, etc.",
"scripts": {
"test": "node ./node_modules/test-machinepack-mocha/bin/testmachinepack-mocha.js"
"test": "node ./node_modules/test-machinepack-mocha/bin/testmachinepack-mocha.js && mocha tests/**"
},

@@ -21,8 +21,7 @@ "keywords": [

"dependencies": {
"async": "2.0.1",
"browserify-transform-machinepack": "^1.0.3",
"lodash": "3.10.1",
"lodash.iserror": "3.1.1",
"lodash.isobject": "3.0.2",
"machine": "^12.3.0",
"object-hash": "0.5.0"
"machine": "^13.0.0-7",
"resolve": "1.1.7"
},

@@ -34,4 +33,4 @@ "repository": {

"devDependencies": {
"mocha": "2.1.0",
"test-machinepack-mocha": "^2.1.1"
"mocha": "3.0.2",
"test-machinepack-mocha": "^2.1.6"
},

@@ -42,7 +41,8 @@ "machinepack": {

"machines": [
"log",
"hash",
"inspect",
"coalesce",
"require"
"guarantee",
"require",
"construct",
"whilst",
"pretty-print"
],

@@ -49,0 +49,0 @@ "testsUrl": "https://travis-ci.org/treelinehq/machinepack-util"

@@ -9,3 +9,3 @@ {

"outcome": "success",
"returns": "cef2d36c0fde830d67beb47dcbdc6a7eb62d9d57"
"returns": "d49d79f6e52adc33f4300005b0f14b13"
},

@@ -17,3 +17,3 @@ {

"outcome": "success",
"returns": "24ca1d88dc90dad521e916c9276ff71feaaed299"
"returns": "31e9ea65b4998c479b32607e0ef7f0cd"
},

@@ -25,3 +25,3 @@ {

"outcome": "success",
"returns": "cdf22d2a18b96ef07f6105cd8093ae12a8772cb3"
"returns": "b326b5062b2f0e69046810717534cb09"
},

@@ -33,3 +33,3 @@ {

"outcome": "success",
"returns": "b29c63990dea846689120516761de20c056e3539"
"returns": "68934a3e9455fa72420237eb05902327"
},

@@ -41,3 +41,3 @@ {

"outcome": "success",
"returns": "a4e96f4ba6cc1ba036800452568fcbc7fd4572b2"
"returns": "c60dfcabb7215a04bfe46920ecb15d3d"
},

@@ -53,3 +53,3 @@ {

"outcome": "success",
"returns": "f18749fbdf15d7a41654d95a32445e5b16bb62b7"
"returns": "c29a5747d698b2f95cdfd5ed6502f19d"
},

@@ -65,10 +65,24 @@ {

"outcome": "success",
"returns": "a2fc48da101b2469cbb9b3e4c38e01e0c45ba127"
"returns": "f1e46f328e6decd56c64dd5e761dc2b7"
},
{
"using": {
"value": {"foo":"bar", "abc": 123}
},
"outcome": "success",
"returns": "900b407f53639b07439a489454047a90"
},
{
"using": {
"value": {"abc": 123, "foo":"bar"}
},
"outcome": "success",
"returns": "900b407f53639b07439a489454047a90"
},
{
"using": {
"value": []
},
"outcome": "success",
"returns": "989db2448f309bfdd99b513f37c84b8f5794d2b5"
"returns": "d751713988987e9331980363e24189ce"
},

@@ -80,5 +94,5 @@ {

"outcome": "success",
"returns": "0426c65f9613a523407c71497c3795ae931bf5cc"
"returns": "99914b932bd37a50b983c5e7c90ae93b"
}
]
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc