]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/npm-registry-client/lib/unpublish.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / npm-registry-client / lib / unpublish.js
1 module.exports = unpublish
2
3 // fetch the data
4 // modify to remove the version in question
5 // If no versions remaining, then DELETE
6 // else, PUT the modified data
7 // delete the tarball
8
9 var semver = require('semver')
10 var url = require('url')
11 var chain = require('slide').chain
12 var assert = require('assert')
13
14 function unpublish (uri, params, cb) {
15   assert(typeof uri === 'string', 'must pass registry URI to unpublish')
16   assert(params && typeof params === 'object', 'must pass params to unpublish')
17   assert(typeof cb === 'function', 'must pass callback to unpublish')
18
19   var ver = params.version
20   var auth = params.auth
21   assert(auth && typeof auth === 'object', 'must pass auth to unpublish')
22
23   var options = {
24     timeout: -1,
25     follow: false,
26     auth: auth
27   }
28   this.get(uri + '?write=true', options, function (er, data) {
29     if (er) {
30       this.log.info('unpublish', uri + ' not published')
31       return cb()
32     }
33     // remove all if no version specified
34     if (!ver) {
35       this.log.info('unpublish', 'No version specified, removing all')
36       return this.request(uri + '/-rev/' + data._rev, { method: 'DELETE', auth: auth }, cb)
37     }
38
39     var versions = data.versions || {}
40     var versionPublic = versions.hasOwnProperty(ver)
41
42     var dist
43     if (!versionPublic) {
44       this.log.info('unpublish', uri + '@' + ver + ' not published')
45     } else {
46       dist = versions[ver].dist
47       this.log.verbose('unpublish', 'removing attachments', dist)
48     }
49
50     delete versions[ver]
51     // if it was the only version, then delete the whole package.
52     if (!Object.keys(versions).length) {
53       this.log.info('unpublish', 'No versions remain, removing entire package')
54       return this.request(uri + '/-rev/' + data._rev, { method: 'DELETE', auth: auth }, cb)
55     }
56
57     if (!versionPublic) return cb()
58
59     var latestVer = data['dist-tags'].latest
60     for (var tag in data['dist-tags']) {
61       if (data['dist-tags'][tag] === ver) delete data['dist-tags'][tag]
62     }
63
64     if (latestVer === ver) {
65       data['dist-tags'].latest =
66         Object.getOwnPropertyNames(versions).sort(semver.compareLoose).pop()
67     }
68
69     var rev = data._rev
70     delete data._revisions
71     delete data._attachments
72     var cb_ = detacher.call(this, uri, data, dist, auth, cb)
73
74     this.request(uri + '/-rev/' + rev, { method: 'PUT', body: data, auth: auth }, function (er) {
75       if (er) {
76         this.log.error('unpublish', 'Failed to update data')
77       }
78       cb_(er)
79     }.bind(this))
80   }.bind(this))
81 }
82
83 function detacher (uri, data, dist, credentials, cb) {
84   return function (er) {
85     if (er) return cb(er)
86     this.get(escape(uri, data.name), { auth: credentials }, function (er, data) {
87       if (er) return cb(er)
88
89       var tb = url.parse(dist.tarball)
90
91       detach.call(this, uri, data, tb.pathname, data._rev, credentials, function (er) {
92         if (er || !dist.bin) return cb(er)
93         chain(Object.keys(dist.bin).map(function (bt) {
94           return function (cb) {
95             var d = dist.bin[bt]
96             detach.call(this, uri, data, url.parse(d.tarball).pathname, null, credentials, cb)
97           }.bind(this)
98         }, this), cb)
99       }.bind(this))
100     }.bind(this))
101   }.bind(this)
102 }
103
104 function detach (uri, data, path, rev, credentials, cb) {
105   if (rev) {
106     path += '/-rev/' + rev
107     this.log.info('detach', path)
108     return this.request(url.resolve(uri, path), { method: 'DELETE', auth: credentials }, cb)
109   }
110   this.get(escape(uri, data.name), { auth: credentials }, function (er, data) {
111     rev = data._rev
112     if (!rev) return cb(new Error('No _rev found in ' + data._id))
113     detach.call(this, data, path, rev, cb)
114   }.bind(this))
115 }
116
117 function escape (base, name) {
118   var escaped = name.replace(/\//, '%2f')
119   return url.resolve(base, escaped)
120 }