Permalink
Browse files

serialize shrinkwrap payload in order

JavaScript objects will serialize in an arbitrary key order because key
enumeration is not well defined.

To avoid this we sort the keys and create a fresh object in a specific
order. v8 will serialize this object in a consistent fashion.

The problem is *probably* caused because `commands.ls` will write keys
to the `pkginfo` in arbitrary order based on disk IO latency and such
  • Loading branch information...
1 parent c9b1e5d commit 059b2bfd06ae775205a37257dca80142596a0113 @Raynos Raynos committed with isaacs Jan 27, 2014
Showing with 12 additions and 0 deletions.
  1. +12 −0 lib/shrinkwrap.js
View
@@ -56,6 +56,9 @@ function shrinkwrap_ (pkginfo, silent, dev, cb) {
function save (pkginfo, silent, cb) {
+ // copy the keys over in a well defined order
+ // because javascript objects serialize arbitrarily
+ pkginfo.dependencies = copyOrder(pkginfo.dependencies)
try {
var swdata = JSON.stringify(pkginfo, null, 2) + "\n"
} catch (er) {
@@ -72,3 +75,12 @@ function save (pkginfo, silent, cb) {
cb(null, pkginfo)
})
}
+
+function copyOrder(obj) {
+ var result = {}
+ var keys = Object.keys(obj).sort()
+ keys.forEach(function (key) {
+ result[key] = obj[key]
+ })
+ return result
+}

0 comments on commit 059b2bf

Please sign in to comment.