]> 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/access.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 / access.js
1 module.exports = access
2
3 var assert = require('assert')
4 var url = require('url')
5 var npa = require('npm-package-arg')
6 var subcommands = {}
7
8 function access (sub, uri, params, cb) {
9   accessAssertions(sub, uri, params, cb)
10   return subcommands[sub].call(this, uri, params, cb)
11 }
12
13 subcommands.public = function (uri, params, cb) {
14   return setAccess.call(this, 'public', uri, params, cb)
15 }
16 subcommands.restricted = function (uri, params, cb) {
17   return setAccess.call(this, 'restricted', uri, params, cb)
18 }
19
20 function setAccess (access, uri, params, cb) {
21   return this.request(apiUri(uri, 'package', params.package, 'access'), {
22     method: 'POST',
23     auth: params.auth,
24     body: JSON.stringify({ access: access })
25   }, cb)
26 }
27
28 subcommands.grant = function (uri, params, cb) {
29   var reqUri = apiUri(uri, 'team', params.scope, params.team, 'package')
30   return this.request(reqUri, {
31     method: 'PUT',
32     auth: params.auth,
33     body: JSON.stringify({
34       permissions: params.permissions,
35       package: params.package
36     })
37   }, cb)
38 }
39
40 subcommands.revoke = function (uri, params, cb) {
41   var reqUri = apiUri(uri, 'team', params.scope, params.team, 'package')
42   return this.request(reqUri, {
43     method: 'DELETE',
44     auth: params.auth,
45     body: JSON.stringify({
46       package: params.package
47     })
48   }, cb)
49 }
50
51 subcommands['ls-packages'] = function (uri, params, cb, type) {
52   type = type || (params.team ? 'team' : 'org')
53   var client = this
54   var uriParams = '?format=cli'
55   var reqUri = apiUri(uri, type, params.scope, params.team, 'package')
56   return client.request(reqUri + uriParams, {
57     method: 'GET',
58     auth: params.auth
59   }, function (err, perms) {
60     if (err && err.statusCode === 404 && type === 'org') {
61       subcommands['ls-packages'].call(client, uri, params, cb, 'user')
62     } else {
63       cb(err, perms && translatePermissions(perms))
64     }
65   })
66 }
67
68 subcommands['ls-collaborators'] = function (uri, params, cb) {
69   var uriParams = '?format=cli'
70   if (params.user) {
71     uriParams += ('&user=' + encodeURIComponent(params.user))
72   }
73   var reqUri = apiUri(uri, 'package', params.package, 'collaborators')
74   return this.request(reqUri + uriParams, {
75     method: 'GET',
76     auth: params.auth
77   }, function (err, perms) {
78     cb(err, perms && translatePermissions(perms))
79   })
80 }
81
82 subcommands.edit = function () {
83   throw new Error('edit subcommand is not implemented yet')
84 }
85
86 function apiUri (registryUri) {
87   var path = Array.prototype.slice.call(arguments, 1)
88     .filter(function (x) { return x })
89     .map(encodeURIComponent)
90     .join('/')
91   return url.resolve(registryUri, '-/' + path)
92 }
93
94 function accessAssertions (subcommand, uri, params, cb) {
95   assert(subcommands.hasOwnProperty(subcommand),
96          'access subcommand must be one of ' +
97          Object.keys(subcommands).join(', '))
98   typeChecks({
99     'uri': [uri, 'string'],
100     'params': [params, 'object'],
101     'auth': [params.auth, 'object'],
102     'callback': [cb, 'function']
103   })
104   if (contains([
105     'public', 'restricted'
106   ], subcommand)) {
107     typeChecks({ 'package': [params.package, 'string'] })
108     assert(!!npa(params.package).scope,
109            'access commands are only accessible for scoped packages')
110   }
111   if (contains(['grant', 'revoke', 'ls-packages'], subcommand)) {
112     typeChecks({ 'scope': [params.scope, 'string'] })
113   }
114   if (contains(['grant', 'revoke'], subcommand)) {
115     typeChecks({ 'team': [params.team, 'string'] })
116   }
117   if (subcommand === 'grant') {
118     typeChecks({ 'permissions': [params.permissions, 'string'] })
119     assert(params.permissions === 'read-only' ||
120            params.permissions === 'read-write',
121            'permissions must be either read-only or read-write')
122   }
123 }
124
125 function typeChecks (specs) {
126   Object.keys(specs).forEach(function (key) {
127     var checks = specs[key]
128     assert(typeof checks[0] === checks[1],
129            key + ' is required and must be of type ' + checks[1])
130   })
131 }
132
133 function contains (arr, item) {
134   return arr.indexOf(item) !== -1
135 }
136
137 function translatePermissions (perms) {
138   var newPerms = {}
139   for (var key in perms) {
140     if (perms.hasOwnProperty(key)) {
141       if (perms[key] === 'read') {
142         newPerms[key] = 'read-only'
143       } else if (perms[key] === 'write') {
144         newPerms[key] = 'read-write'
145       } else {
146         // This shouldn't happen, but let's not break things
147         // if the API starts returning different things.
148         newPerms[key] = perms[key]
149       }
150     }
151   }
152   return newPerms
153 }