Skip to content

Commit

Permalink
feat: support npm search command like npmio
Browse files Browse the repository at this point in the history
  • Loading branch information
Beace committed Jun 28, 2023
1 parent f588e27 commit dadd39d
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 0 deletions.
93 changes: 93 additions & 0 deletions app/core/event/SyncESPackage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// TODO sync event
/* eslint-disable @typescript-eslint/no-unused-vars */
import { EggAppConfig } from 'egg';
import { Event, Inject } from '@eggjs/tegg';
import {
PACKAGE_UNPUBLISHED,
PACKAGE_VERSION_ADDED,
PACKAGE_VERSION_REMOVED,
PACKAGE_TAG_ADDED,
PACKAGE_TAG_CHANGED,
PACKAGE_TAG_REMOVED,
PACKAGE_MAINTAINER_CHANGED,
PACKAGE_MAINTAINER_REMOVED,
PACKAGE_META_CHANGED, PackageMetaChange,
} from './index';

import { PackageSearchService } from '../service/PackageSearchService';
import { User } from '../entity/User';

class SyncESPackage {
@Inject()
protected readonly packageSearchService: PackageSearchService;

@Inject()
protected readonly config: EggAppConfig;

protected async doSomething(): Promise<unknown> {
throw Error('Not Implemented');
}
}

@Event(PACKAGE_UNPUBLISHED)
export class PackageUnpublished extends SyncESPackage {
async handle() {
throw Error('Not Implemented');
}
}

@Event(PACKAGE_VERSION_ADDED)
export class PackageVersionAdded extends SyncESPackage {
async handle(_fullname: string, _version: string, _tag?: string) {
throw Error('Not Implemented');
}
}

@Event(PACKAGE_VERSION_REMOVED)
export class PackageVersionRemoved extends SyncESPackage {
async handle(_fullname: string, _version: string, _tag?: string) {
throw Error('Not Implemented');
}
}

@Event(PACKAGE_TAG_ADDED)
export class PackageTagAdded extends SyncESPackage {
async handle(_fullname: string, _tag: string) {
throw Error('Not Implemented');
}
}

@Event(PACKAGE_TAG_CHANGED)
export class PackageTagChanged extends SyncESPackage {
async handle(_fullname: string, _tag: string) {
throw Error('Not Implemented');
}
}

@Event(PACKAGE_TAG_REMOVED)
export class PackageTagRemoved extends SyncESPackage {
async handle(_fullname: string, _tag: string) {
throw Error('Not Implemented');
}
}

@Event(PACKAGE_MAINTAINER_CHANGED)
export class PackageMaintainerChanged extends SyncESPackage {
async handle(_fullname: string, _maintainers: User[]) {
throw Error('Not Implemented');
}
}

@Event(PACKAGE_MAINTAINER_REMOVED)
export class PackageMaintainerRemoved extends SyncESPackage {
async handle(_fullname: string, _maintainer: string) {
throw Error('Not Implemented');
}
}

@Event(PACKAGE_META_CHANGED)
export class PackageMetaChanged extends SyncESPackage {
async handle(_fullname: string, _meta: PackageMetaChange) {
throw Error('Not Implemented');
}
}
19 changes: 19 additions & 0 deletions app/core/service/PackageSearchService.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';

@SingletonProto({
accessLevel: AccessLevel.PUBLIC,
})
export class PackageSearchService extends AbstractService {
createOrUpdatePackage() {
throw Error('Not Implemented');
}

searchPackage() {
throw Error('Not Implemented');
}

removePackage() {
throw Error('Not Implemented');
}
}
2 changes: 2 additions & 0 deletions app/core/service/PackageSyncerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export class PackageSyncerService extends AbstractService {
if (!this.allowSyncDownloadData) {
return;
}
// TODO Sync Download data to ES

const fullname = pkg.fullname;
const start = '2011-01-01';
const end = this.config.cnpmcore.syncDownloadDataMaxDate;
Expand Down
4 changes: 4 additions & 0 deletions app/port/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,8 @@ export type CnpmcoreConfig = {
* in most cases, you should set to false to keep the same behavior as source registry.
*/
strictSyncSpecivicVersion: boolean,
/**
* enable elastic search
*/
enableESSearch: boolean,
};
27 changes: 27 additions & 0 deletions app/port/controller/package/SearchPackageController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
HTTPController,
HTTPMethod,
HTTPMethodEnum,
Context,
EggContext,
HTTPQuery,
Inject,
} from '@eggjs/tegg';
import { AbstractController } from '../AbstractController';
import { Client as ElasticsearchClient } from '@elastic/elasticsearch';

@HTTPController()
export class SearchPackageController extends AbstractController {
@Inject()
private readonly elasticsearch: ElasticsearchClient;
@HTTPMethod({
// GET /-/v1/search?text=react&size=20&from=0&quality=0.65&popularity=0.98&maintenance=0.5
path: '/-/v1/search',
method: HTTPMethodEnum.GET,
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async search(@Context() _ctx: EggContext, @HTTPQuery() _text: string) {
console.log(this.elasticsearch);
return null;
}
}
8 changes: 8 additions & 0 deletions config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const cnpmcoreConfig: CnpmcoreConfig = {
redirectNotFound: true,
enableUnpkg: true,
strictSyncSpecivicVersion: false,
enableESSearch: true,
};

export default (appInfo: EggAppConfig) => {
Expand Down Expand Up @@ -186,5 +187,12 @@ export default (appInfo: EggAppConfig) => {
},
};

// more options: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html
config.elasticsearch = {
client: {
node: 'http://localhost:9200',
},
};

return config;
};
4 changes: 4 additions & 0 deletions config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ const plugin: EggPlugin = {
enable: true,
package: 'egg-status',
},
elasticsearch: {
enable: true,
package: 'eggjs-elasticsearch',
},
};

export default plugin;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@eggjs/tegg-plugin": "^3.2.1",
"@eggjs/tegg-schedule-plugin": "^3.2.1",
"@eggjs/tsconfig": "^1.0.0",
"@elastic/elasticsearch": "^8.8.1",
"@node-rs/crc32": "^1.2.2",
"@simplewebauthn/server": "^7.0.1",
"@sinclair/typebox": "^0.23.0",
Expand All @@ -93,6 +94,7 @@
"egg-tracer": "^1.1.0",
"egg-typebox-validate": "^2.0.0",
"egg-view-nunjucks": "^2.3.0",
"eggjs-elasticsearch": "^0.0.6",
"fs-cnpm": "^2.4.0",
"ioredis": "^5.3.1",
"leoric": "^2.6.2",
Expand Down

0 comments on commit dadd39d

Please sign in to comment.