Skip to content

Commit

Permalink
fix: body parser ignore (#558)
Browse files Browse the repository at this point in the history
> Closes #551 Some versions
encounter issues with audit requests using the GET method & gzip
* 🧶 Modified the NOT_IMPLEMENTED configuration to skip bodyParser
parsing by default.
* 🧶 Added homeService and implemented miscellaneous methods to handle
additional routes.
------
> Closes #551 部分版本 audit 请求为 get
且 参数解析异常
* 🧶 修改 NOT_IMPLEMENTED 配置,默认跳过 bodyParser 解析
* 🧶 添加 homeService,提供 misc 方法处理额外路由

---------

Co-authored-by: fengmk2 <[email protected]>
  • Loading branch information
elrrrrrrr and fengmk2 authored Jul 27, 2023
1 parent 7ba8dbb commit 81d6455
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 36 deletions.
23 changes: 0 additions & 23 deletions .github/workflows/chatgpt-cr.yml

This file was deleted.

1 change: 1 addition & 0 deletions app/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const BUG_VERSIONS = 'bug-versions';
export const LATEST_TAG = 'latest';
export const GLOBAL_WORKER = 'GLOBAL_WORKER';
export const NOT_IMPLEMENTED_PATH = [ '/-/npm/v1/security/audits/quick', '/-/npm/v1/security/advisories/bulk' ];
export enum SyncMode {
none = 'none',
admin = 'admin',
Expand Down
19 changes: 19 additions & 0 deletions app/core/service/HomeService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
AccessLevel,
SingletonProto,
} from '@eggjs/tegg';
import { AbstractService } from '../../common/AbstractService';
import { NOT_IMPLEMENTED_PATH } from '../../common/constants';
import { NotFoundError, NotImplementedError } from 'egg-errors';

@SingletonProto({
accessLevel: AccessLevel.PUBLIC,
})
export class HomeService extends AbstractService {
async misc(path: string) {
if (NOT_IMPLEMENTED_PATH.includes(path)) {
throw new NotImplementedError(`${path} not implemented yet`);
}
throw new NotFoundError(`${path} not found`);
}
}
24 changes: 15 additions & 9 deletions app/port/controller/HomeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import {
} from '@eggjs/tegg';
import { AbstractController } from './AbstractController';
import { CacheService, DownloadInfo, UpstreamRegistryInfo } from '../../core/service/CacheService';
import { NotFoundError, NotImplementedError } from 'egg-errors';
import { HomeService } from '../../core/service/HomeService';

const startTime = new Date();

const NOT_IMPLEMENTED = [ '/-/npm/v1/security/audits/quick', '/-/npm/v1/security/advisories/bulk' ];

// registry 站点信息数据 SiteTotalData
// SiteEnvInfo: 环境、运行时相关信息,实时查询
// UpstreamInfo: 上游信息,实时查询
Expand Down Expand Up @@ -54,6 +52,9 @@ export class HomeController extends AbstractController {
@Inject()
private readonly cacheService: CacheService;

@Inject()
private readonly homeService: HomeService;

@HTTPMethod({
// GET /
// https://github.com/cnpm/cnpmjs.org/blob/master/docs/registry-api.md#schema
Expand Down Expand Up @@ -106,12 +107,17 @@ export class HomeController extends AbstractController {
method: HTTPMethodEnum.POST,
priority: -Infinity,
})
async misc(@Context() ctx: EggContext) {
const { path } = ctx;
if (NOT_IMPLEMENTED.includes(path)) {
throw new NotImplementedError(`${ctx.path} not implemented yet`);
}
async miscPost(@Context() ctx: EggContext) {
await this.homeService.misc(ctx.path);
}

throw new NotFoundError(`${ctx.path} not found`);
@HTTPMethod({
path: '/*',
method: HTTPMethodEnum.GET,
priority: -Infinity,
})
async miscGet(@Context() ctx: EggContext) {
await this.homeService.misc(ctx.path);
}

}
4 changes: 3 additions & 1 deletion config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from 'path';
import { EggAppConfig, PowerPartial } from 'egg';
import OSSClient from 'oss-cnpm';
import { patchAjv } from '../app/port/typebox';
import { ChangesStreamMode, SyncDeleteMode, SyncMode } from '../app/common/constants';
import { ChangesStreamMode, NOT_IMPLEMENTED_PATH, SyncDeleteMode, SyncMode } from '../app/common/constants';
import { CnpmcoreConfig } from '../app/port/config';

export const cnpmcoreConfig: CnpmcoreConfig = {
Expand Down Expand Up @@ -167,6 +167,8 @@ export default (appInfo: EggAppConfig) => {
strict: false,
// set default limit to 10mb, see https://github.com/npm/npm/issues/12750
jsonLimit: '10mb',
// https://github.com/cnpm/cnpmcore/issues/551
ignore: NOT_IMPLEMENTED_PATH,
};

// https://github.com/xiekw2010/egg-typebox-validate#%E5%A6%82%E4%BD%95%E5%86%99%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A0%A1%E9%AA%8C%E8%A7%84%E5%88%99
Expand Down
17 changes: 14 additions & 3 deletions test/port/controller/HomeController/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@ describe('test/port/controller/HomeController/misc.test.ts', () => {
.expect(501);
assert.equal(res.body.error, '[NOT_IMPLEMENTED] /-/npm/v1/security/audits/quick not implemented yet');
});

it('should 404', async () => {
const res = await app.httpRequest()
await app.httpRequest()
.post('/-/greed/is/good')
.expect(404);
assert.equal(res.body.error, '[NOT_FOUND] /-/greed/is/good not found');
});
});

describe('[GET /*] misc()', () => {
it('should 501 even gzip error', async () => {
const res = await app.httpRequest()
.get('/-/npm/v1/security/audits/quick')
.set('Content-Encoding', 'gzip')
.send({
name: 'npm',
})
.expect(501);
assert.equal(res.body.error, '[NOT_IMPLEMENTED] /-/npm/v1/security/audits/quick not implemented yet');
});
});

Expand Down

0 comments on commit 81d6455

Please sign in to comment.