1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00

fix: publish with deprecated field (#2771)

* fix: publish with deprecated field by @Jiasm 

When publish with deprecated field in `package.json`, that will make all old versions miss.

Examples:

I have package@1.0.0 and package@1.0.1.

When `npm deprecate package@1.0.0 "xxx"`, Verdaccio will recived:

```json
{
  "name": "module_name",
  "version": {
    "1.0.0": {
      "deprecated": "xxx"
    },
    "1.0.1": {}
  }
}
```

⬆️ This make sense

But then publish new version with @1.0.2.

Verdaccio will recived:

```json
{
  "name": "module_name",
  "version": {
    "1.0.2": {
      "deprecated": "xxx" // if we set this field in package.json
    },
  }
}
```

and that metadata will override package.json, make old version miss.

migrate from #2766

* remove spaces

* fix: ignore empty package case

* fix: cover normal unpublish case

* refactor: Optimize check logic for lazy execution

* test: upgrade Jest Snapshot

* fix: set storage.getPackage `uplinksLook: false`.

* feat: use `_attachments` to distinguish deprecate

* test: rollback test snapshots

* test: rollback jest snapshots

* test: publish new version with deprecate field

* test: remove space
This commit is contained in:
贾顺名(Jarvis) 2021-12-16 23:17:52 +08:00 committed by GitHub
parent 3b2ec6cbfa
commit b5ac3c0f28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

@ -216,8 +216,8 @@ export function publishPackage(storage: IStorageHandler, config: Config, auth: I
try { try {
const metadata = validateMetadata(req.body, packageName); const metadata = validateMetadata(req.body, packageName);
// treating deprecation as updating a package // check _attachments to distinguish publish and deprecate
if (req.params._rev || isRelatedToDeprecation(req.body)) { if (req.params._rev || (isRelatedToDeprecation(req.body) && _.isEmpty(req.body._attachments))) {
debug('updating a new version for %o', packageName); debug('updating a new version for %o', packageName);
// we check unpublish permissions, an update is basically remove versions // we check unpublish permissions, an update is basically remove versions
const remote = req.remote_user; const remote = req.remote_user;

@ -1073,6 +1073,28 @@ describe('endpoint unit test', () => {
expect(res.body.versions['1.0.1'].deprecated).toEqual('get deprecated'); expect(res.body.versions['1.0.1'].deprecated).toEqual('get deprecated');
done(); done();
}); });
test('should deprecate when publish new version with deprecate field', async (done) => {
await Promise.all([
putPackage(request(app), `/${pkgName}`, generatePackageMetadata(pkgName, '2.0.0'), token),
putPackage(request(app), `/${pkgName}`, generatePackageMetadata(pkgName, '2.0.1'), token),
putPackage(request(app), `/${pkgName}`, generatePackageMetadata(pkgName, '2.0.2'), token)
]);
const pkg = generatePackageMetadata(pkgName, '2.0.3');
pkg.versions['2.0.3'].deprecated = 'get deprecated';
await putPackage(request(app), `/${encodeScopedUri(pkgName)}`, pkg, token);
const [, res] = await getPackage(request(app), '', pkgName);
const versions = Object.keys(res.body.versions);
expect(res.body.versions['2.0.3'].deprecated).toEqual('get deprecated');
expect(versions).toContain('2.0.0');
expect(versions).toContain('2.0.1');
expect(versions).toContain('2.0.2');
expect(versions).toContain('2.0.3');
done();
});
}); });
}); });
}); });