From 6c55d5b8514822930c28cb045a0cee777a72acc5 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Mon, 15 Feb 2016 06:57:26 -0800 Subject: [PATCH] Replace custom rolled stable string hashing with library Reviewed By: cpojer Differential Revision: D2937202 fb-gh-sync-id: dc08547c71da2bc35cfad108e63fd5e87f0ba734 shipit-source-id: dc08547c71da2bc35cfad108e63fd5e87f0ba734 --- npm-shrinkwrap.json | 60 +++++++++++-------- package.json | 1 + .../src/DependencyResolver/Module.js | 21 ++----- .../__tests__/Module-test.js | 1 + 4 files changed, 41 insertions(+), 42 deletions(-) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index dc7c0be87..7ad46f5e9 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -127,16 +127,16 @@ "from": "https://registry.npmjs.org/ast-types/-/ast-types-0.8.12.tgz", "resolved": "https://registry.npmjs.org/ast-types/-/ast-types-0.8.12.tgz" }, - "babylon": { - "version": "5.8.29", - "from": "https://registry.npmjs.org/babylon/-/babylon-5.8.29.tgz", - "resolved": "https://registry.npmjs.org/babylon/-/babylon-5.8.29.tgz" - }, "globals": { "version": "6.4.1", "from": "https://registry.npmjs.org/globals/-/globals-6.4.1.tgz", "resolved": "https://registry.npmjs.org/globals/-/globals-6.4.1.tgz" }, + "babylon": { + "version": "5.8.29", + "from": "https://registry.npmjs.org/babylon/-/babylon-5.8.29.tgz", + "resolved": "https://registry.npmjs.org/babylon/-/babylon-5.8.29.tgz" + }, "js-tokens": { "version": "1.0.1", "from": "https://registry.npmjs.org/js-tokens/-/js-tokens-1.0.1.tgz", @@ -1955,16 +1955,16 @@ "from": "brace-expansion@>=1.0.0 <2.0.0", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.1.tgz" }, - "chalk": { - "version": "1.1.1", - "from": "chalk@^1.1.1", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.1.tgz" - }, "caseless": { "version": "0.11.0", "from": "caseless@~0.11.0", "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.11.0.tgz" }, + "chalk": { + "version": "1.1.1", + "from": "chalk@^1.1.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.1.tgz" + }, "combined-stream": { "version": "1.0.5", "from": "combined-stream@~1.0.5", @@ -2065,16 +2065,16 @@ "from": "graceful-readlink@>= 1.0.0", "resolved": "https://registry.npmjs.org/graceful-readlink/-/graceful-readlink-1.0.1.tgz" }, - "has-ansi": { - "version": "2.0.0", - "from": "has-ansi@^2.0.0", - "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz" - }, "har-validator": { "version": "2.0.2", "from": "har-validator@~2.0.2", "resolved": "https://registry.npmjs.org/har-validator/-/har-validator-2.0.2.tgz" }, + "has-ansi": { + "version": "2.0.0", + "from": "has-ansi@^2.0.0", + "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz" + }, "has-unicode": { "version": "1.0.1", "from": "has-unicode@^1.0.0", @@ -2235,16 +2235,16 @@ "from": "request@2.x", "resolved": "https://registry.npmjs.org/request/-/request-2.65.0.tgz" }, - "sntp": { - "version": "1.0.9", - "from": "sntp@1.x.x", - "resolved": "https://registry.npmjs.org/sntp/-/sntp-1.0.9.tgz" - }, "semver": { "version": "5.0.3", "from": "semver@~5.0.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.0.3.tgz" }, + "sntp": { + "version": "1.0.9", + "from": "sntp@1.x.x", + "resolved": "https://registry.npmjs.org/sntp/-/sntp-1.0.9.tgz" + }, "string_decoder": { "version": "0.10.31", "from": "string_decoder@~0.10.x", @@ -2260,16 +2260,16 @@ "from": "strip-ansi@^3.0.0", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.0.tgz" }, - "supports-color": { - "version": "2.0.0", - "from": "supports-color@^2.0.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz" - }, "strip-json-comments": { "version": "0.1.3", "from": "strip-json-comments@0.1.x", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-0.1.3.tgz" }, + "supports-color": { + "version": "2.0.0", + "from": "supports-color@^2.0.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz" + }, "tar": { "version": "2.2.1", "from": "tar@~2.2.0", @@ -3338,11 +3338,21 @@ "from": "https://registry.npmjs.org/jsesc/-/jsesc-0.5.0.tgz", "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-0.5.0.tgz" }, + "json-stable-stringify": { + "version": "1.0.1", + "from": "json-stable-stringify@>=1.0.1 <2.0.0", + "resolved": "https://registry.npmjs.org/json-stable-stringify/-/json-stable-stringify-1.0.1.tgz" + }, "json5": { "version": "0.4.0", "from": "https://registry.npmjs.org/json5/-/json5-0.4.0.tgz", "resolved": "https://registry.npmjs.org/json5/-/json5-0.4.0.tgz" }, + "jsonify": { + "version": "0.0.0", + "from": "jsonify@>=0.0.0 <0.1.0", + "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz" + }, "jstransform": { "version": "11.0.3", "from": "https://registry.npmjs.org/jstransform/-/jstransform-11.0.3.tgz", diff --git a/package.json b/package.json index 742cc4b40..ea74c16ff 100644 --- a/package.json +++ b/package.json @@ -128,6 +128,7 @@ "image-size": "^0.3.5", "immutable": "^3.7.5", "joi": "^6.6.1", + "json-stable-stringify": "^1.0.1", "json5": "^0.4.0", "jstransform": "^11.0.3", "mkdirp": "^0.5.1", diff --git a/packager/react-packager/src/DependencyResolver/Module.js b/packager/react-packager/src/DependencyResolver/Module.js index c75473acc..6b68a620e 100644 --- a/packager/react-packager/src/DependencyResolver/Module.js +++ b/packager/react-packager/src/DependencyResolver/Module.js @@ -11,6 +11,7 @@ const crypto = require('crypto'); const docblock = require('./DependencyGraph/docblock'); const isAbsolutePath = require('absolute-path'); +const jsonStableStringify = require('json-stable-stringify'); const path = require('path'); const extractRequires = require('./lib/extractRequires'); @@ -206,29 +207,15 @@ function whileInDocBlock(chunk, i, result) { const knownHashes = new WeakMap(); function stableObjectHash(object) { let digest = knownHashes.get(object); - if (!digest) { - const hash = crypto.createHash('md5'); - stableObjectHash.addTo(object, hash); - digest = hash.digest('base64'); + digest = crypto.createHash('md5') + .update(jsonStableStringify(object)) + .digest('base64'); knownHashes.set(object, digest); } return digest; } -stableObjectHash.addTo = function addTo(value, hash) { - if (value === null || typeof value !== 'object') { - hash.update(JSON.stringify(value)); - } else { - Object.keys(value).sort().forEach(key => { - const valueForKey = value[key]; - if (valueForKey !== undefined) { - hash.update(key); - addTo(valueForKey, hash); - } - }); - } -}; function cacheKey(field, transformOptions) { return transformOptions !== undefined diff --git a/packager/react-packager/src/DependencyResolver/__tests__/Module-test.js b/packager/react-packager/src/DependencyResolver/__tests__/Module-test.js index 71c3a43dc..bb4fa75ea 100644 --- a/packager/react-packager/src/DependencyResolver/__tests__/Module-test.js +++ b/packager/react-packager/src/DependencyResolver/__tests__/Module-test.js @@ -10,6 +10,7 @@ jest .dontMock('absolute-path') + .dontMock('json-stable-stringify') .dontMock('../fastfs') .dontMock('../lib/extractRequires') .dontMock('../lib/replacePatterns')