Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
serialize shrinkwrap payload in order
Browse files Browse the repository at this point in the history
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
Raynos authored and isaacs committed Feb 17, 2014
1 parent c9b1e5d commit 059b2bf
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/shrinkwrap.js
Expand Up @@ -56,6 +56,9 @@ function shrinkwrap_ (pkginfo, silent, dev, cb) {




function save (pkginfo, silent, 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 { try {
var swdata = JSON.stringify(pkginfo, null, 2) + "\n" var swdata = JSON.stringify(pkginfo, null, 2) + "\n"
} catch (er) { } catch (er) {
Expand All @@ -72,3 +75,12 @@ function save (pkginfo, silent, cb) {
cb(null, pkginfo) 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.