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

feat: proxy mode [sql changed] #571

Merged
merged 53 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
151369f
feat: add proxy service.
hezhengxu2018 Jun 20, 2023
8899223
feat: create task when proxy.
hezhengxu2018 Jun 23, 2023
7480e60
fix: revert npmRegistry.
hezhengxu2018 Jul 3, 2023
f3ef419
fix: npm registry adapter.
hezhengxu2018 Jul 3, 2023
03b10cc
feat: init proxy mode sql.
hezhengxu2018 Jul 9, 2023
be7c244
fix: not allow check update.
hezhengxu2018 Jul 7, 2023
c05faa7
feat: init proxy file repo.
hezhengxu2018 Jul 11, 2023
ce74646
fix: add config limit.
hezhengxu2018 Jul 12, 2023
09eedcc
refactor: rename function.
hezhengxu2018 Jul 13, 2023
c264260
feat: init schedule.
hezhengxu2018 Jul 14, 2023
1529ba9
feat: init task.
hezhengxu2018 Jul 22, 2023
d7e6c8c
feat: init task type.
hezhengxu2018 Jul 26, 2023
5fd1b14
feat: rename model.
hezhengxu2018 Jul 31, 2023
ee6beac
wip: proxy contorller.
hezhengxu2018 Aug 3, 2023
c50b0a8
wip: modify entity.
hezhengxu2018 Aug 3, 2023
186e60c
feat: realize basic func.
hezhengxu2018 Aug 4, 2023
02b30f2
fix: fix type.
hezhengxu2018 Aug 4, 2023
c3fb522
fix: fix intarval.
hezhengxu2018 Aug 7, 2023
7025d4d
test: add service test.
hezhengxu2018 Aug 12, 2023
9d2d231
test: fix test
hezhengxu2018 Aug 12, 2023
68cf8c9
test: add schedule & controller test.
hezhengxu2018 Aug 13, 2023
8fc57ec
test: fix test.
hezhengxu2018 Aug 16, 2023
aae817c
style: rename sql file.
hezhengxu2018 Oct 21, 2023
af7007d
fix: use buffer instead of TextDecoder.
hezhengxu2018 Oct 21, 2023
0776afc
feat: support clear cache.
hezhengxu2018 Dec 12, 2023
bb6159a
test: add proxy cache controller test.
hezhengxu2018 Dec 18, 2023
393d260
fix: fix update proxy cache interval
hezhengxu2018 Dec 19, 2023
fca9459
fix: unnecessary slash.
hezhengxu2018 Dec 23, 2023
ea40dba
chore: rename constant, improve type infer.
hezhengxu2018 Dec 23, 2023
dbe07d7
feat: use proxy instead of downloadToTempfile
hezhengxu2018 Dec 23, 2023
4435779
feat: add remote auth token.
hezhengxu2018 Dec 24, 2023
77285f3
feat: try remove cache dir.
hezhengxu2018 Dec 24, 2023
c5bc58d
feat: improve perf.
hezhengxu2018 Dec 29, 2023
0576532
test: improve test.
hezhengxu2018 Dec 29, 2023
e59ab0d
fix: use run in background.
hezhengxu2018 Jan 5, 2024
f2f85fe
fix: rename constant
hezhengxu2018 Mar 30, 2024
1d5a9f7
fix: rollback to temp tgz file.
hezhengxu2018 Apr 20, 2024
9526880
fix: remove unused value.
hezhengxu2018 Apr 20, 2024
42bff6a
feat: use stream instead of temp file.
hezhengxu2018 May 2, 2024
09a9304
refactor: package version controller in proxy mode.
hezhengxu2018 May 5, 2024
6e11691
refactor: package controller & package version controller.
hezhengxu2018 May 6, 2024
42c5f97
test: fix proxy controller test.
hezhengxu2018 May 31, 2024
176d336
fix: fix error proxy header.
hezhengxu2018 Jun 1, 2024
8e64b36
test: fix download package version tar controller.
hezhengxu2018 Jun 11, 2024
ca5b91c
refactor: rename function
hezhengxu2018 Jun 19, 2024
fe1c16c
fix: set version default value to null
hezhengxu2018 Jul 14, 2024
19b348d
test: improve unit test cov.
hezhengxu2018 Jul 24, 2024
f2c84ed
test: improve unit test cov.
hezhengxu2018 Jul 24, 2024
732be72
refactor: rename function
hezhengxu2018 Jul 27, 2024
63d610b
fix: convert stream
hezhengxu2018 Aug 29, 2024
ce71b9f
fix: stream type error.
hezhengxu2018 Sep 5, 2024
1ce994c
fix: enlarge retry times
hezhengxu2018 Sep 18, 2024
b07a17a
Rename 3.47.0.sql to 3.67.0.sql
fengmk2 Oct 13, 2024
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
2 changes: 1 addition & 1 deletion app/common/adapter/NPMRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class NPMRegistry {
};
}

private genAuthorizationHeader(remoteAuthToken?:string) {
public genAuthorizationHeader(remoteAuthToken?:string) {
return remoteAuthToken ? `Bearer ${remoteAuthToken}` : '';
}
}
4 changes: 4 additions & 0 deletions app/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
export const BUG_VERSIONS = 'bug-versions';
export const LATEST_TAG = 'latest';
export const GLOBAL_WORKER = 'GLOBAL_WORKER';
export const PROXY_CACHE_DIR_NAME = 'proxy-cache-packages';
export const ABBREVIATED_META_TYPE = 'application/vnd.npm.install-v1+json';
export const NOT_IMPLEMENTED_PATH = [ '/-/npm/v1/security/audits/quick', '/-/npm/v1/security/advisories/bulk' ];

export enum SyncMode {
none = 'none',
admin = 'admin',
proxy = 'proxy',
exist = 'exist',
all = 'all',
}
Expand Down
1 change: 1 addition & 0 deletions app/common/enum/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum TaskType {
SyncPackage = 'sync_package',
ChangesStream = 'changes_stream',
SyncBinary = 'sync_binary',
UpdateProxyCache = 'update_proxy_cache',
CreateHook = 'create_hook',
TriggerHook = 'trigger_hook',
}
Expand Down
42 changes: 42 additions & 0 deletions app/core/entity/ProxyCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Entity, EntityData } from './Entity';
import { EasyData } from '../util/EntityUtil';
import { DIST_NAMES } from './Package';
import { isPkgManifest } from '../service/ProxyCacheService';
import { PROXY_CACHE_DIR_NAME } from '../../common/constants';
interface ProxyCacheData extends EntityData {
fullname: string;
fileType: DIST_NAMES;
version?: string;
}

export type CreateProxyCacheData = Omit<EasyData<ProxyCacheData, 'id'>, 'id'| 'filePath'>;

export class ProxyCache extends Entity {
readonly fullname: string;
readonly fileType: DIST_NAMES;
readonly filePath: string;
readonly version?: string;

constructor(data: ProxyCacheData) {
super(data);
this.fullname = data.fullname;
this.fileType = data.fileType;
this.version = data.version;
if (isPkgManifest(data.fileType)) {
this.filePath = `/${PROXY_CACHE_DIR_NAME}/${data.fullname}/${data.fileType}`;
} else {
this.filePath = `/${PROXY_CACHE_DIR_NAME}/${data.fullname}/${data.version}/${data.fileType}`;
}
}

public static create(data: CreateProxyCacheData): ProxyCache {
const newData = { ...data, createdAt: new Date(), updatedAt: new Date() };
return new ProxyCache(newData);
}

public static update(data: ProxyCache): ProxyCache {
data.updatedAt = new Date();
return data;
}

}
42 changes: 42 additions & 0 deletions app/core/entity/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import path from 'path';
import { Entity, EntityData } from './Entity';
import { EasyData, EntityUtil } from '../util/EntityUtil';
import { TaskType, TaskState } from '../../common/enum/Task';
import { PROXY_CACHE_DIR_NAME } from '../../common/constants';
import dayjs from '../../common/dayjs';
import { HookEvent } from './HookEvent';
import { DIST_NAMES } from './Package';
import { isPkgManifest } from '../service/ProxyCacheService';
import { InternalServerError } from 'egg-errors';

export const HOST_NAME = os.hostname();
export const PID = process.pid;
Expand Down Expand Up @@ -40,6 +44,12 @@ export type SyncPackageTaskOptions = {
specificVersions?: Array<string>;
};

export type UpdateProxyCacheTaskOptions = {
fullname: string,
version?: string,
fileType: DIST_NAMES,
};

export interface CreateHookTaskData extends TaskBaseData {
hookEvent: HookEvent;
}
Expand All @@ -58,6 +68,13 @@ export interface CreateSyncPackageTaskData extends TaskBaseData {
specificVersions?: Array<string>;
}

export interface CreateUpdateProxyCacheTaskData extends TaskBaseData {
fullname: string,
version?: string,
fileType: DIST_NAMES,
filePath: string
}

export interface ChangesStreamTaskData extends TaskBaseData {
since: string;
last_package?: string,
Expand All @@ -75,6 +92,7 @@ export type CreateHookTask = Task<CreateHookTaskData>;
export type TriggerHookTask = Task<TriggerHookTaskData>;
export type CreateSyncPackageTask = Task<CreateSyncPackageTaskData>;
export type ChangesStreamTask = Task<ChangesStreamTaskData>;
export type CreateUpdateProxyCacheTask = Task<CreateUpdateProxyCacheTaskData>;

export class Task<T extends TaskBaseData = TaskBaseData> extends Entity {
taskId: string;
Expand Down Expand Up @@ -235,6 +253,30 @@ export class Task<T extends TaskBaseData = TaskBaseData> extends Entity {
return [ TaskType.SyncBinary, TaskType.SyncPackage ].includes(type);
}

public static createUpdateProxyCache(targetName: string, options: UpdateProxyCacheTaskOptions):CreateUpdateProxyCacheTask {
if (!isPkgManifest(options.fileType)) {
throw new InternalServerError('should not update package version manifest.');
}
Comment on lines +258 to +259
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

Add test coverage for the new task creation logic.

The logic in createUpdateProxyCache for throwing an error when the file type is not a package manifest is not covered by tests. Ensuring this logic is tested will prevent future regressions.

  • No direct or indirect test cases for createUpdateProxyCache or InternalServerError related to this logic were found in the test files.

Please add test cases to cover this scenario.

Analysis chain

Add test coverage for the new task creation logic.

The new logic in createUpdateProxyCache for throwing an error when the file type is not a package manifest is not covered by tests. Ensuring this logic is tested will prevent future regressions.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify test coverage for the new task creation logic.

# Test: Search for test cases covering lines 258-259. Expect: At least one test case covering these lines.
rg --type python $'app/core/entity/Task.ts#L258-L259'

Length of output: 83



Script:

#!/bin/bash
# Locate the `createUpdateProxyCache` method definition in TypeScript files
ast-grep --lang typescript --pattern 'function createUpdateProxyCache($_) { $$$ }'

Length of output: 84



Script:

#!/bin/bash
# Search for the `createUpdateProxyCache` method definition in TypeScript files
rg 'createUpdateProxyCache' --type ts

Length of output: 358



Script:

#!/bin/bash
# Locate the error-throwing logic within the createUpdateProxyCache method in app/core/entity/Task.ts
rg 'throw new InternalServerError' app/core/entity/Task.ts

Length of output: 144



Script:

#!/bin/bash
# Search for test files related to Task
fd 'Task' --type f --extension ts --search-path test

Length of output: 741



Script:

#!/bin/bash
# Search for references to createUpdateProxyCache in the identified test files
rg 'createUpdateProxyCache' test/

Length of output: 33



Script:

#!/bin/bash
# Search for test cases that handle InternalServerError in the test files
rg 'InternalServerError' test/

Length of output: 30

Tools
GitHub Check: codecov/patch

[warning] 258-259: app/core/entity/Task.ts#L258-L259
Added lines #L258 - L259 were not covered by tests

const filePath = `/${PROXY_CACHE_DIR_NAME}/${options.fullname}/${options.fileType}`;
const data = {
type: TaskType.UpdateProxyCache,
state: TaskState.Waiting,
targetName,
authorId: `pid_${PID}`,
authorIp: HOST_NAME,
data: {
taskWorker: '',
fullname: options.fullname,
version: options?.version,
fileType: options.fileType,
filePath,
},
};
const task = this.create(data);
task.logPath = `/${PROXY_CACHE_DIR_NAME}/${options.fullname}/update-manifest-log/${options.fileType.split('.json')[0]}-${dayjs().format('YYYY/MM/DDHHmm')}-${task.taskId}.log`;
return task;
}

start(): TaskUpdateCondition {
const condition = {
taskId: this.taskId,
Expand Down
Loading
Loading