Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: body parser ignore #558

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
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`);
}
}
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
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);
}

}
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
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
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
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');
});
});

fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down