Skip to content

Commit

Permalink
Merge branch 'master' into fix--incorrect-latest-tag
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhengxu2018 authored Nov 7, 2023
2 parents 27bc21a + 072e146 commit 36d38ea
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [3.48.3](https://github.com/cnpm/cnpmcore/compare/v3.48.2...v3.48.3) (2023-11-06)


### Bug Fixes

* es query script score syntax fix and add error handler for 404 error ([#607](https://github.com/cnpm/cnpmcore/issues/607)) ([8e1f4ca](https://github.com/cnpm/cnpmcore/commit/8e1f4ca880c6ad09f766807e6a751b5ae960b550))

## [3.48.2](https://github.com/cnpm/cnpmcore/compare/v3.48.1...v3.48.2) (2023-11-03)


Expand Down
15 changes: 12 additions & 3 deletions app/core/service/PackageSearchService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AccessLevel, Inject, SingletonProto } from '@eggjs/tegg';
import type { estypes } from '@elastic/elasticsearch';
import { estypes, errors } from '@elastic/elasticsearch';
import dayjs from 'dayjs';

import { AbstractService } from '../../common/AbstractService';
Expand Down Expand Up @@ -138,7 +138,16 @@ export class PackageSearchService extends AbstractService {
}

async removePackage(fullname: string) {
return await this.searchRepository.removePackage(fullname);
try {
return await this.searchRepository.removePackage(fullname);
} catch (error) {
// if the package does not exist, returns success
if (error instanceof errors.ResponseError && error?.statusCode === 404) {
this.logger.warn('[PackageSearchService.removePackage] remove package:%s not found', fullname);
return fullname;
}
throw error;
}
}

// https://github.com/npms-io/queries/blob/master/lib/search.js#L8C1-L78C2
Expand Down Expand Up @@ -213,7 +222,7 @@ export class PackageSearchService extends AbstractService {
private _buildScriptScore(params: { text: string | undefined, scoreEffect: number }) {
// keep search simple, only download(popularity)
const downloads = 'doc["downloads.all"].value';
const source = `doc["package.name.raw"].value.equals("${params.text}") ? 100000 + ${downloads} : _score * Math.pow(${downloads}, ${params.scoreEffect})`;
const source = `doc["package.name.raw"].value.equals(params.text) ? 100000 + ${downloads} : _score * Math.pow(${downloads}, params.scoreEffect)`;
return {
script: {
source,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cnpmcore",
"version": "3.48.2",
"version": "3.48.3",
"description": "npm core",
"files": [
"dist/**/*"
Expand Down
38 changes: 38 additions & 0 deletions test/port/controller/package/SearchPackageController.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'assert';
import { app, mock } from 'egg-mock/bootstrap';
import { errors } from '@elastic/elasticsearch';

import { mockES } from '../../../../config/config.unittest';
import { TestUtil } from '../../../TestUtil';
Expand Down Expand Up @@ -123,5 +124,42 @@ describe('test/port/controller/package/SearchPackageController.test.ts', () => {
.set('authorization', admin.authorization);
assert.equal(res.body.package, name);
});

it('should delete a non existent package', async () => {
const name = 'non-existent-search-package';
mockES.add({
method: 'DELETE',
path: `/${app.config.cnpmcore.elasticsearchIndex}/_doc/:id`,
}, () => {
return new errors.ResponseError({
body: { errors: {}, status: 404 },
statusCode: 404,
warnings: null,
meta: {
name: '',
context: '',
request: {
params: {
method: 'delete',
path: `/${app.config.cnpmcore.elasticsearchIndex}/_doc/:id`,
},
options: {},
id: '',
},
connection: null,
attempts: 1,
aborted: true,
},
});
});
mock(app.config.cnpmcore, 'allowPublishNonScopePackage', true);
mock(app.config.cnpmcore, 'enableElasticsearch', true);
mock(app.config.cnpmcore, 'registry', 'https://registry.example.com');

const res = await app.httpRequest()
.delete(`/-/v1/search/sync/${name}`)
.set('authorization', admin.authorization);
assert.equal(res.body.package, name);
});
});
});

0 comments on commit 36d38ea

Please sign in to comment.