Skip to content

Commit

Permalink
adding support for new npm behaviour, closes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Dec 29, 2013
1 parent 8c05cfe commit 5453589
Showing 1 changed file with 63 additions and 6 deletions.
69 changes: 63 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,78 @@ module.exports = function(config_hash) {

if (req.params._rev) {
storage.change_package(name, metadata, req.params.revision, function(err) {
if (err) return next(err)
res.status(201)
return res.send({
ok: 'package changed'
})
after_change(err, 'package changed')
})
} else {
storage.add_package(name, metadata, function(err) {
after_change(err, 'created new package')
})
}

function after_change(err, ok_message) {
if (metadata._attachments == null && metadata.versions == null) {
if (err) return next(err)
res.status(201)
return res.send({
ok: 'created new package'
ok: ok_message
})
}

// npm-registry-client 0.3+ embeds tarball into the json upload
// https://github.com/isaacs/npm-registry-client/commit/e9fbeb8b67f249394f735c74ef11fe4720d46ca0
// issue #31, dealing with it here:

if (typeof(metadata._attachments) != 'object'
|| Object.keys(metadata._attachments).length != 1
|| typeof(metadata.versions) != 'object'
|| Object.keys(metadata.versions).length != 1) {

// npm is doing something strange again
// if this happens in normal circumstances, report it as a bug
return next(new UError({
status: 400,
msg: 'unsupported registry call',
}))
}

if (err && err.status != 409) return next(err)

// at this point document is either created or existed before
var t1 = Object.keys(metadata._attachments)[0]
create_tarball(t1, metadata._attachments[t1], function(err) {
if (err) return err

var t2 = Object.keys(metadata.versions)[0]
create_version(t2, metadata.versions[t2], function(err) {
if (err) return err

res.status(201)
return res.send({
ok: ok_message
})
})
})
}

function create_tarball(filename, data, cb) {
var stream = storage.add_tarball(name, filename)
stream.on('error', function(err) {
cb(err)
})
stream.on('success', function() {
cb()
})

// this is dumb and memory-consuming, but what choices do we have?
stream.end(new Buffer(data.data, 'base64'))
stream.done()
}

function create_version(version, data, cb) {
// assume latest tag, it's ignored anyway
// if you want tags, tag packages explicitly
storage.add_version(name, version, data, 'latest', cb)
}
})

// unpublishing an entire package
Expand Down

0 comments on commit 5453589

Please sign in to comment.