# npm packages

> Publish scoped npm packages to branchd and install them with npm or pnpm. Claiming a scope, who may install and publish under it, .npmrc, pipelines of other teams, dist-tags, deprecating and unpublishing.
branchd serves an npm registry for **scoped** packages (`@scope/name`):

```
https://registry.branchd.run/npm/
```

npm and pnpm install from it and publish to it themselves. There is nothing
to install on top of them.

- **A scope belongs to one group.** A group's administrator
  [claims it](https://docs.branchd.run/branchd/packages/npm#claim-a-scope).
- **A package belongs to its scope**, and so to that group. No repository
  owns a package: a repository is only where a version was published from.
- **Who may install and publish is granted**, on the scope or on one
  package: to people, and to the pipelines of repositories. See
  [Who may install and publish](https://docs.branchd.run/branchd/packages/npm#who-may-install-and-publish).
- **A version is published once.** Its number is never used again, even
  after it is unpublished.

The examples read a token from `BRANCHD_TOKEN`. On your own machine, set it
to a [personal API key](https://docs.branchd.run/branchd/access/personal-keys) or to
`$(branchd auth token)`. In a pipeline job it is already set.

## Claim a scope

Before the first publish, a group claims the scope. It needs **admin**
access to the group. A team's root group is named by the team's slug.

```sh
branchd npm scope claim acme @acme         # the team acme's root group
branchd npm scope claim acme/tools @tools  # a group
branchd npm scope list acme
```

Or in the console: the group's **Settings › General**, **npm scopes**,
**Claim a scope**.

- **A scope has one owner on the whole of branchd.** Claiming one another
  group holds is refused.
- **A scope name** is lower-case letters, digits and `.` `_` `-`, starting
  with a letter or digit, at most 100 characters. It does not have to match
  the group's name.
- **Claiming makes one grant**: the pipelines of the group's own
  repositories may publish and install under the scope. It is listed with
  the scope's other grants, and can be revoked like them.
- **Releasing a scope** (`branchd npm scope release @acme`) is refused while
  a package under it still has a published version. Its grants go with it.
- **Deleting the group** is refused the same way while one of its scopes has
  a published version.

## Install a package

Two lines of `.npmrc`: where the scope lives, and the credential for it.

```ini
@acme:registry=https://registry.branchd.run/npm/
//registry.branchd.run/npm/:_authToken=${BRANCHD_TOKEN}
```

```sh
npm install @acme/widgets
pnpm add @acme/widgets
```

- **Put the token line in your own `~/.npmrc`.** npm also reads it from a
  project's `.npmrc`, but **pnpm does not**: it ignores a credential with a
  variable in a project's `.npmrc`, and the install fails with `401`. The
  `@acme:registry=` line can live in either file, so a project can commit it.
- **Never commit a token.** `${BRANCHD_TOKEN}` is read from the environment
  when npm or pnpm runs.
- **Where the scope's group shows packages publicly**, the registry line
  alone is enough. See [Public packages](https://docs.branchd.run/branchd/packages/access#public-npm-packages).
- **Other scopes and unscoped packages** still come from the registry you
  already use. Only `@acme` is sent to branchd.

`branchd npm view @acme/widgets` prints a package's owner, your own level on
it, its dist-tags, versions and these lines, ready to copy. So does the
package's page in the console.

## Publish a package

Everyone publishes at the registry itself, with the same two `.npmrc` lines
that install. Nothing in `package.json` says where:

```sh
npm publish
pnpm publish
```

- **You need write access to the scope**, or to the package. See
  [Who may install and publish](https://docs.branchd.run/branchd/packages/npm#who-may-install-and-publish).
- **A new name under the scope** needs access to the scope itself. Access
  to one package publishes new versions of that package only.
- **The first publish gets the `latest` dist-tag.** Later publishes move
  `latest` unless you pass `--tag`: `npm publish --tag next`.
- **A tarball is at most 32 MiB.**
- **Publishing a version that exists is refused** (`403`, "You cannot
  publish over the previously published versions").
- **A scope nobody has claimed is refused**, with the command that claims it.

`https://registry.branchd.run/npm/-/repository/<repository>/` still answers:
it is the same registry. Packages belonged to repositories at first and a
publish went through that URL. You no longer need it, or a `publishConfig`.

## Who may install and publish

Access is on a **scope** (every package under it, new names included) or on
**one package**.

| Level    | For people                                            | For pipelines                                |
| -------- | ----------------------------------------------------- | -------------------------------------------- |
| read     | Install; see versions and dist-tags                   | Install                                      |
| write    | Publish; move dist-tags                               | Publish; move dist-tags; install             |
| maintain | Deprecate and unpublish; manage the scope's CI grants | Never: a job does not deprecate or unpublish |
| admin    | Also manage people's access                           |                                              |

### People

- **Members of the group's team** have their team role on every scope the
  group holds, as on anything else in that group. Access grants and limits
  set on the group apply too.
- **Anyone else** is given a level on the scope or on one package: a person,
  every person in a Xeonr Auth team, a bot, or someone invited by email.

```sh
branchd npm access grant @acme --to @ana --role write
branchd npm access grant @acme/widgets --to team:partners --role read
branchd npm access list @acme/widgets --inherited
branchd npm access explain @acme/widgets --for @ana
branchd npm access revoke @acme --to @ana
```

- These are [`branchd access`](https://docs.branchd.run/branchd/access) with an npm name. A
  limit (`branchd npm access limit`) caps what someone has from above.
- **Changing access needs admin** on the scope, as yourself or an agent
  acting for you: a bot or an API key cannot.
- **A grant on one package needs the package to exist.**

### Pipelines

A job's token reaches a scope or a package **only through a CI grant** on
it: who (whose jobs), may (read or write), when (on which runs). A job has
no access because the person who started it does.

```sh
# every repository of two other teams installs the whole scope, on any run
branchd npm grant create @acme --to team:tmpsn --to team:upl-im --may read --when any

# one repository installs one package
branchd npm grant create @acme/sekrit-tools --to repo:upl-im/api --may read --when any

# one repository publishes one package, from its protected branches
branchd npm grant create @acme/private-tools --to repo:upl-im/api --may write

# a team, or one group, publishes anything under the scope
branchd npm grant create @acme --to team:acme --may write
branchd npm grant create @acme --to group:acme/test --may write
```

- **`--to`** is `repo:<path>`, `group:<path>` (every repository under it),
  `team:<slug>` or `company:<urn>`. Repeat it for several.
- **`--may read`** installs. **`--may write`** publishes new versions (on a
  scope, new packages too), moves dist-tags and installs.
- **`--when`** is `protected` (runs of the grantee's protected branches, the
  default), `any` (pull requests too), or `owner`: whoever the run answers
  to must have that access to the package themselves.
- **A job publishes only from a protected run**, whatever the grant says: a
  push or a manual run of a protected branch, with `permissions: packages:
  write`. `--when any` on a write grant adds installing from every run, not
  publishing.
- **A grant on a package can be made before its first version**, so the
  repository that will publish it, and the ones that will install it, are
  let in first.
- **A person with maintain access to the scope makes or revokes a grant**,
  or an agent acting for them, recorded as the grant's actor. A bot or a job
  cannot.

```sh
branchd npm grant list @acme
branchd npm grant list @acme/widgets --inherited   # with its scope's
branchd npm grant revoke <id>
branchd npm grant explain @acme/widgets --from upl-im/api --protected
branchd repo grant reaches upl-im/api              # everything that repository's jobs reach
```

`explain` says whether a job of that repository would install or publish,
and which grant or rule decided. A job that is refused is told the grant it
needs, in the error npm prints and in its run's record.

In the console, a scope's page (the group's **Settings › General**, **npm
scopes**) and each package's page list **Pipelines** and **People**, with
the same controls.

## In a pipeline

A job publishes with its own token and plain `npm publish`. No secret is
stored:

```yaml title=".branchd/pipelines/publish.yaml"
# .branchd/pipelines/publish.yaml
# Publishes the npm package in this repository to branchd's npm registry with
# the job's own token: no stored secret. @acme is a scope whose CI grants let
# this repository publish: one a group above it claimed, or one another team
# granted it.
name: Publish
on:
  push:
    branches: [main]
permissions:
  packages: write
jobs:
  publish:
    image: node:22-alpine
    steps:
      - run: |
          printf '@acme:registry=https://registry.branchd.run/npm/\n//registry.branchd.run/npm/:_authToken=${BRANCHD_TOKEN}\n' > .npmrc
          npm publish
```

- **`permissions: packages: write`** lets the job's token publish, on a run
  of a protected branch. `packages: read` is enough to install. See
  [The job token](https://docs.branchd.run/branchd/pipelines/job-token).
- **The repository needs a CI grant on the scope or the package.** A
  repository in the group that claimed the scope has one from the claim.
- **Give each run a new version**, or publish from a tag: a version that
  exists is refused.
- **A job never deprecates or unpublishes.**
- **Each version records where it was published from**: the repository,
  branch, commit and run. `branchd npm view` and the console show it.

### From another team

A repository in another team publishes under your scope, and installs from
it, once you grant it. Say team `acme` holds `@acme`, `partner/sdk`
publishes `@acme/sdk`, and `partner/app` installs it:

```sh
branchd npm grant create @acme/sdk --to repo:partner/sdk --may write
branchd npm grant create @acme/sdk --to repo:partner/app --may read --when any
```

- **`partner/sdk`** then runs `npm publish` on its protected branch, with
  `permissions: packages: write` and the two `.npmrc` lines above.
- **`partner/app`** runs `npm install` with `permissions: packages: read`.
- **The package is `acme`'s.** Its people manage it; the people of
  `partner` have no access to it unless you give them some.

The typed step `branchd/steps/node/npm-package-publish@v1` builds and publishes a
package in one step. With no `registry:` input it publishes to branchd with
the job's token, and its `package_scopes:` input lists scopes to install from
branchd the same way.

## Dist-tags

A dist-tag is a movable name for a version: `npm install @acme/widgets@next`.
`latest` is what an install with no tag or version gets.

```sh
npm dist-tag add @acme/widgets@2.0.0-rc.1 next
npm dist-tag ls @acme/widgets

branchd npm dist-tag set @acme/widgets next 2.0.0-rc.1
branchd npm dist-tag remove @acme/widgets next
```

- **A dist-tag is a name starting with a letter**, never something that
  reads as a version or a range (`1.4.2`, `v1`).
- **`latest` moves but is never removed.**
- Moving a dist-tag needs write access to the package or its scope. In the
  console, use **Add a dist-tag** or the edit button on a tag's row.

## Deprecate and unpublish

Both need **maintain** access to the package or its scope.

```sh
npm deprecate @acme/widgets@1.3.0 "Use 1.4 or later"
branchd npm deprecate @acme/widgets 1.3.0 "Use 1.4 or later"
branchd npm deprecate @acme/widgets 1.3.0 --clear

branchd npm unpublish @acme/widgets 1.4.1
```

- **Deprecating** sets the message every install of that version prints.
  The version still installs. An empty message clears it.
- **Unpublishing** deletes the version's tarball. Installs that pin it fail,
  lockfiles included. Its number is never used again. A dist-tag that
  pointed at it is removed, and `latest` moves to the highest version left.
  This cannot be undone: to warn people off a version, deprecate it instead.
- **A package's name stays its scope's** after every version is
  unpublished. Deleting a repository that published it changes nothing.

## What branchd checks

- **Names.** `@scope/name`, lower case, at most 214 characters. An unscoped
  name is refused.
- **Versions.** Full semantic versions (`1.4.2`, `2.0.0-rc.1`), with no
  leading `v`.
- **Checksums.** branchd computes each version's `shasum` and `integrity`
  from the bytes it stored. It does not take them from the client.

## The API

`xeonr.forge.v1.NpmPackageService` is what the console and the CLI use for
everything npm itself has no command for:

| Method                                                             | Does                                                                                                                                                                    |
| ------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ListNpmPackages`, `GetNpmPackage`                                 | The packages of a scope, of a group's scopes, or the ones a repository published; or one by name: its owning group, your level, `registry`, dist-tags and version count |
| `ListNpmPackageVersions`                                           | Versions, newest first: size, `shasum`, `integrity`, dist-tags, deprecation, where it was published from, and whether it was unpublished                                |
| `SetNpmDistTag`, `DeleteNpmDistTag`                                | Move or remove a dist-tag                                                                                                                                               |
| `DeprecateNpmPackageVersion`, `UnpublishNpmPackageVersion`         | Deprecate (an empty `message` clears it), or unpublish with `confirm_version`                                                                                           |
| `ListNpmScopes`, `GetNpmScope`, `ClaimNpmScope`, `ReleaseNpmScope` | A group's scopes                                                                                                                                                        |

Grants are `GrantService` (`CreateGrant`, `ListGrants`, `RevokeGrant`,
`ExplainCIAccess`) and `AccessService` (`GrantAccess`, `ListAccess`,
`ExplainAccess`), with `npm` set to `@scope` or `@scope/name`.

Source: https://docs.branchd.run/branchd/packages/npm
