Skip to content

Commit 854521b

Browse files
wraithgarfritzy
authored andcommitted
feat(rewrite): Rewrite libnpmaccess
BREAKING CHANGE: the api for libnpmaccess is different now It is aligned more with how npm uses it, consolidating the mfa functions into a single command, and renames the functions to be easier to eventually consolidate into a registry client library. See the README for the new api.
1 parent 601bb11 commit 854521b

File tree

6 files changed

+276
-684
lines changed

6 files changed

+276
-684
lines changed

DEPENDENCIES.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,6 @@ graph LR;
323323
init-package-json-->validate-npm-package-name;
324324
is-cidr-->cidr-regex;
325325
is-core-module-->has;
326-
libnpmaccess-->aproba;
327-
libnpmaccess-->minipass;
328326
libnpmaccess-->nock;
329327
libnpmaccess-->npm-package-arg;
330328
libnpmaccess-->npm-registry-fetch;

package-lock.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13917,8 +13917,6 @@
1391713917
"version": "7.0.0-pre.0",
1391813918
"license": "ISC",
1391913919
"dependencies": {
13920-
"aproba": "^2.0.0",
13921-
"minipass": "^3.1.1",
1392213920
"npm-package-arg": "^9.0.1",
1392313921
"npm-registry-fetch": "^13.0.0"
1392413922
},

workspaces/libnpmaccess/README.md

Lines changed: 41 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -6,241 +6,88 @@
66

77
[`libnpmaccess`](https://github.com/npm/libnpmaccess) is a Node.js
88
library that provides programmatic access to the guts of the npm CLI's `npm
9-
access` command and its various subcommands. This includes managing account 2FA,
10-
listing packages and permissions, looking at package collaborators, and defining
9+
access` command. This includes managing account mfa settings, listing
10+
packages and permissions, looking at package collaborators, and defining
1111
package permissions for users, orgs, and teams.
1212

1313
## Example
1414

1515
```javascript
1616
const access = require('libnpmaccess')
17+
const opts = { '//registry.npmjs.org/:_authToken: 'npm_token }
1718

1819
// List all packages @zkat has access to on the npm registry.
19-
console.log(Object.keys(await access.lsPackages('zkat')))
20+
console.log(Object.keys(await access.getPackages('zkat', opts)))
2021
```
2122

22-
## Table of Contents
23-
24-
* [Installing](#install)
25-
* [Example](#example)
26-
* [Contributing](#contributing)
27-
* [API](#api)
28-
* [access opts](#opts)
29-
* [`public()`](#public)
30-
* [`restricted()`](#restricted)
31-
* [`grant()`](#grant)
32-
* [`revoke()`](#revoke)
33-
* [`tfaRequired()`](#tfa-required)
34-
* [`tfaNotRequired()`](#tfa-not-required)
35-
* [`lsPackages()`](#ls-packages)
36-
* [`lsPackages.stream()`](#ls-packages-stream)
37-
* [`lsCollaborators()`](#ls-collaborators)
38-
* [`lsCollaborators.stream()`](#ls-collaborators-stream)
39-
40-
### Install
41-
42-
`$ npm install libnpmaccess`
43-
4423
### API
4524

46-
#### <a name="opts"></a> `opts` for `libnpmaccess` commands
25+
#### `opts` for all `libnpmaccess` commands
4726

4827
`libnpmaccess` uses [`npm-registry-fetch`](https://npm.im/npm-registry-fetch).
49-
All options are passed through directly to that library, so please refer to [its
50-
own `opts`
28+
29+
All options are passed through directly to that library, so please refer
30+
to [its own `opts`
5131
documentation](https://www.npmjs.com/package/npm-registry-fetch#fetch-options)
5232
for options that can be passed in.
5333

54-
A couple of options of note for those in a hurry:
55-
56-
* `opts.token` - can be passed in and will be used as the authentication token for the registry. For other ways to pass in auth details, see the n-r-f docs.
57-
* `opts.otp` - certain operations will require an OTP token to be passed in. If a `libnpmaccess` command fails with `err.code === EOTP`, please retry the request with `{otp: <2fa token>}`
58-
59-
#### <a name="public"></a> `> access.public(spec, [opts]) -> Promise<Boolean>`
34+
#### `spec` parameter for all `libnpmaccess` commands
6035

6136
`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible
6237
registry spec.
6338

64-
Makes package described by `spec` public.
65-
66-
##### Example
67-
68-
```javascript
69-
await access.public('@foo/bar', {token: 'myregistrytoken'})
70-
// `@foo/bar` is now public
71-
```
72-
73-
#### <a name="restricted"></a> `> access.restricted(spec, [opts]) -> Promise<Boolean>`
74-
75-
`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible
76-
registry spec.
77-
78-
Makes package described by `spec` private/restricted.
79-
80-
##### Example
39+
#### `access.getCollaborators(spec, opts) -> Promise<Object>`
8140

82-
```javascript
83-
await access.restricted('@foo/bar', {token: 'myregistrytoken'})
84-
// `@foo/bar` is now private
85-
```
41+
Gets collaborators for a given package
8642

87-
#### <a name="grant"></a> `> access.grant(spec, team, permissions, [opts]) -> Promise<Boolean>`
43+
#### `access.getPackages(user|scope|team, opts) -> Promise<Object>`
8844

89-
`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible
90-
registry spec. `team` must be a fully-qualified team name, in the `scope:team`
91-
format, with or without the `@` prefix, and the team must be a valid team within
92-
that scope. `permissions` must be one of `'read-only'` or `'read-write'`.
45+
Gets all packages for a given user, scope, or team.
9346

94-
Grants `read-only` or `read-write` permissions for a certain package to a team.
47+
Teams should be in the format `scope:team` or `@scope:team`
9548

96-
##### Example
49+
Users and scopes can be in the format `@scope` or `scope`
9750

98-
```javascript
99-
await access.grant('@foo/bar', '@foo:myteam', 'read-write', {
100-
token: 'myregistrytoken'
101-
})
102-
// `@foo/bar` is now read/write enabled for the @foo:myteam team.
103-
```
51+
#### `access.getVisibility(spec, opts) -> Promise<Object>`
10452

105-
#### <a name="revoke"></a> `> access.revoke(spec, team, [opts]) -> Promise<Boolean>`
53+
Gets the visibility of a given package
10654

107-
`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible
108-
registry spec. `team` must be a fully-qualified team name, in the `scope:team`
109-
format, with or without the `@` prefix, and the team must be a valid team within
110-
that scope. `permissions` must be one of `'read-only'` or `'read-write'`.
55+
#### `access.removePermissions(team, spec, opts) -> Promise<Boolean>`
11156

112-
Removes access to a package from a certain team.
57+
Removes the access for a given team to a package.
11358

114-
##### Example
59+
Teams should be in the format `scope:team` or `@scope:team`
11560

116-
```javascript
117-
await access.revoke('@foo/bar', '@foo:myteam', {
118-
token: 'myregistrytoken'
119-
})
120-
// @foo:myteam can no longer access `@foo/bar`
121-
```
61+
#### `access.setAccess(package, access, opts) -> Promise<Boolean>`
12262

123-
#### <a name="tfa-required"></a> `> access.tfaRequired(spec, [opts]) -> Promise<Boolean>`
63+
Sets access level for package described by `spec`.
12464

125-
`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible
126-
registry spec.
127-
128-
Makes it so publishing or managing a package requires using 2FA tokens to
129-
complete operations.
65+
The npm registry accepts the following `access` levels:
13066

131-
##### Example
67+
`public`: package is public
68+
`private`: package is private
13269

133-
```javascript
134-
await access.tfaRequires('lodash', {token: 'myregistrytoken'})
135-
// Publishing or changing dist-tags on `lodash` now require OTP to be enabled.
136-
```
70+
The npm registry also only allows scoped packages to have their access
71+
level set.
13772

138-
#### <a name="tfa-not-required"></a> `> access.tfaNotRequired(spec, [opts]) -> Promise<Boolean>`
73+
#### access.setMfa(spec, level, opts) -> Promise<Boolean>`
13974

140-
`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible
141-
registry spec.
75+
Sets the publishing mfa requirements for a given package. Level must be one of the
76+
following
14277

143-
Disabled the package-level 2FA requirement for `spec`. Note that you will need
144-
to pass in an `otp` token in `opts` in order to complete this operation.
78+
`none`: mfa is not required to publish this package.
79+
`publish`: mfa is required to publish this package, automation tokens
80+
cannot be used to publish.
81+
`automation`: mfa is required to publish this package, automation tokens
82+
may also be used for publishing from continuous integration workflows.
14583

146-
##### Example
84+
#### access.setPermissions(team, spec, permssions, opts) -> Promise<Boolean>`
14785

148-
```javascript
149-
await access.tfaNotRequired('lodash', {otp: '123654', token: 'myregistrytoken'})
150-
// Publishing or editing dist-tags on `lodash` no longer requires OTP to be
151-
// enabled.
152-
```
86+
Sets permissions levels for a given team to a package.
15387

154-
#### <a name="ls-packages"></a> `> access.lsPackages(entity, [opts]) -> Promise<Object | null>`
88+
Teams should be in the format `scope:team` or `@scope:team`
15589

156-
`entity` must be either a valid org or user name, or a fully-qualified team name
157-
in the `scope:team` format, with or without the `@` prefix.
90+
The npm registry accepts the following `permissions`:
15891

159-
Lists out packages a user, org, or team has access to, with corresponding
160-
permissions. Packages that the access token does not have access to won't be
161-
listed.
162-
163-
In order to disambiguate between users and orgs, two requests may end up being
164-
made when listing orgs or users.
165-
166-
For a streamed version of these results, see
167-
[`access.lsPackages.stream()`](#ls-package-stream).
168-
169-
##### Example
170-
171-
```javascript
172-
await access.lsPackages('zkat', {
173-
token: 'myregistrytoken'
174-
})
175-
// Lists all packages `@zkat` has access to on the registry, and the
176-
// corresponding permissions.
177-
```
178-
179-
#### <a name="ls-packages-stream"></a> `> access.lsPackages.stream(scope, [team], [opts]) -> Stream`
180-
181-
`entity` must be either a valid org or user name, or a fully-qualified team name
182-
in the `scope:team` format, with or without the `@` prefix.
183-
184-
Streams out packages a user, org, or team has access to, with corresponding
185-
permissions, with each stream entry being formatted like `[packageName,
186-
permissions]`. Packages that the access token does not have access to won't be
187-
listed.
188-
189-
In order to disambiguate between users and orgs, two requests may end up being
190-
made when listing orgs or users.
191-
192-
The returned stream is a valid `asyncIterator`.
193-
194-
##### Example
195-
196-
```javascript
197-
for await (let [pkg, perm] of access.lsPackages.stream('zkat')) {
198-
console.log('zkat has', perm, 'access to', pkg)
199-
}
200-
// zkat has read-write access to eggplant
201-
// zkat has read-only access to @npmcorp/secret
202-
```
203-
204-
#### <a name="ls-collaborators"></a> `> access.lsCollaborators(spec, [user], [opts]) -> Promise<Object | null>`
205-
206-
`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible
207-
registry spec. `user` must be a valid user name, with or without the `@`
208-
prefix.
209-
210-
Lists out access privileges for a certain package. Will only show permissions
211-
for packages to which you have at least read access. If `user` is passed in, the
212-
list is filtered only to teams _that_ user happens to belong to.
213-
214-
For a streamed version of these results, see [`access.lsCollaborators.stream()`](#ls-collaborators-stream).
215-
216-
##### Example
217-
218-
```javascript
219-
await access.lsCollaborators('@npm/foo', 'zkat', {
220-
token: 'myregistrytoken'
221-
})
222-
// Lists all teams with access to @npm/foo that @zkat belongs to.
223-
```
224-
225-
#### <a name="ls-collaborators-stream"></a> `> access.lsCollaborators.stream(spec, [user], [opts]) -> Stream`
226-
227-
`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible
228-
registry spec. `user` must be a valid user name, with or without the `@`
229-
prefix.
230-
231-
Stream out access privileges for a certain package, with each entry in `[user,
232-
permissions]` format. Will only show permissions for packages to which you have
233-
at least read access. If `user` is passed in, the list is filtered only to teams
234-
_that_ user happens to belong to.
235-
236-
The returned stream is a valid `asyncIterator`.
237-
238-
##### Example
239-
240-
```javascript
241-
for await (let [usr, perm] of access.lsCollaborators.stream('npm')) {
242-
console.log(usr, 'has', perm, 'access to npm')
243-
}
244-
// zkat has read-write access to npm
245-
// iarna has read-write access to npm
246-
```
92+
`read-only`: Read only permissions
93+
`read-write`: Read and write (aka publish) permissions

0 commit comments

Comments
 (0)