Skip to content

Commit

Permalink
feat: use egg v4 (#747)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: only support egg >= 4.0.0

the first app on egg v4

eggjs/egg#3644
  • Loading branch information
fengmk2 authored Feb 9, 2025
1 parent 455fc3a commit 4427a4f
Show file tree
Hide file tree
Showing 162 changed files with 333 additions and 252 deletions.
8 changes: 4 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "eslint-config-egg/typescript",
"rules": {

}
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
76 changes: 66 additions & 10 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [18.20.0, 18, 20, 22]
node-version: [20.18.0, 20, 22]
os: [ubuntu-latest]

steps:
Expand All @@ -67,7 +67,7 @@ jobs:
POSTGRES_PORT: 5432

- name: Code Coverage
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}

Expand All @@ -93,7 +93,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [18.20.0, 18, 20, 22]
node-version: [20.18.0, 20, 22]
os: [ubuntu-latest]

steps:
Expand All @@ -112,16 +112,16 @@ jobs:
run: npm run ci

- name: Code Coverage
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}

test-mysql57-oss-nfs:
runs-on: ${{ matrix.os }}
if: |
contains('
refs/heads/master
refs/heads/dev
refs/heads/master-skip-oss
refs/heads/dev-skip-oss
', github.ref)
services:
Expand All @@ -135,16 +135,14 @@ jobs:
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5

redis:
# https://docs.github.com/en/actions/using-containerized-services/about-service-containers#example-mapping-redis-ports
image: redis
ports:
# Opens tcp port 6379 on the host and service container
- 6379:6379

strategy:
fail-fast: false
matrix:
node-version: [18.20.0, 18, 20, 22]
node-version: [20, 22]
os: [ubuntu-latest]

steps:
Expand All @@ -169,6 +167,64 @@ jobs:
CNPMCORE_NFS_OSS_SECRET: ${{ secrets.CNPMCORE_NFS_OSS_SECRET }}

- name: Code Coverage
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}

test-mysql57-s3-nfs:
runs-on: ${{ matrix.os }}
# if: |
# contains('
# refs/heads/master
# refs/heads/dev
# ', github.ref)

services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_DATABASE: cnpmcore_unittest
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5

redis:
image: redis
ports:
- 6379:6379

strategy:
fail-fast: false
matrix:
node-version: [20, 22]
os: [ubuntu-latest]

steps:
- name: Checkout Git Source
uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install Dependencies
run: npm i

- name: Continuous Integration
run: npm run ci "test/cli/npm/install.test.ts"
env:
CNPMCORE_NFS_TYPE: s3
CNPMCORE_NFS_REMOVE_BEFORE_UPLOAD: true
CNPMCORE_NFS_S3_CLIENT_BUCKET: cnpmcore-unittest-github-nodejs-${{ matrix.node-version }}
CNPMCORE_NFS_S3_CLIENT_ENDPOINT: ${{ secrets.CNPMCORE_NFS_S3_ENDPOINT }}
CNPMCORE_NFS_S3_CLIENT_ID: ${{ secrets.CNPMCORE_NFS_S3_ID }}
CNPMCORE_NFS_S3_CLIENT_SECRET: ${{ secrets.CNPMCORE_NFS_S3_SECRET }}
CNPMCORE_NFS_S3_CLIENT_FORCE_PATH_STYLE: true
# CNPMCORE_NFS_S3_CLIENT_DISABLE_URL: true

- name: Code Coverage
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
3 changes: 0 additions & 3 deletions DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ npm run dev

# 访问
curl -v http://127.0.0.1:7001

# cnpmcore_admin 注册管理员
npm login --registry=http://127.0.0.1:7001
```

### 开发运行 - PostgreSQL
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20
FROM node:22

# Create app directory
WORKDIR /usr/src/app
Expand Down
4 changes: 2 additions & 2 deletions INTEGRATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
{
redis: {
enable: true,
package: 'egg-redis',
package: '@eggjs/redis',
},
teggOrm: {
enable: true,
Expand All @@ -71,7 +71,7 @@
},
tracer: {
enable: true,
package: 'egg-tracer',
package: '@eggjs/tracer',
},
typeboxValidate: {
enable: true,
Expand Down
4 changes: 2 additions & 2 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { readFile } from 'fs/promises';
import path from 'node:path';
import { readFile } from 'node:fs/promises';
import { Application } from 'egg';
import { ChangesStreamService } from './app/core/service/ChangesStreamService';

Expand Down
2 changes: 1 addition & 1 deletion app/common/CryptoUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateKeyPairSync } from 'crypto';
import { generateKeyPairSync } from 'node:crypto';
import NodeRSA from 'node-rsa';

// generate rsa key pair
Expand Down
2 changes: 1 addition & 1 deletion app/common/UserUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import crypto from 'crypto';
import crypto from 'node:crypto';
import base from 'base-x';
import { crc32 } from '@node-rs/crc32';
import * as ssri from 'ssri';
Expand Down
4 changes: 2 additions & 2 deletions app/common/adapter/CacheAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
AccessLevel,
Inject,
} from '@eggjs/tegg';
// FIXME: egg-redis should use ioredis v5
// https://github.com/eggjs/egg-redis/issues/35
// FIXME: @eggjs/redis should use ioredis v5
// https://github.com/eggjs/redis/issues/35
import type { Redis } from 'ioredis';

const ONE_DAY = 3600 * 24;
Expand Down
4 changes: 2 additions & 2 deletions app/common/adapter/NFSAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Readable } from 'stream';
import { Readable } from 'node:stream';
import { IncomingHttpHeaders } from 'node:http';
import {
SingletonProto,
AccessLevel,
Expand All @@ -8,7 +9,6 @@ import { Pointcut } from '@eggjs/tegg/aop';
import { EggLogger } from 'egg';
import { AsyncTimer } from '../aop/AsyncTimer';
import { NFSClient } from '../typing';
import { IncomingHttpHeaders } from 'http';

const INSTANCE_NAME = 'nfsAdapter';

Expand Down
2 changes: 1 addition & 1 deletion app/common/adapter/NPMRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setTimeout } from 'timers/promises';
import { setTimeout } from 'node:timers/promises';
import {
ContextProto,
AccessLevel,
Expand Down
2 changes: 1 addition & 1 deletion app/common/adapter/binary/BucketBinary.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SingletonProto } from '@eggjs/tegg';
import { BinaryType } from '../../enum/Binary';
import binaries, { BinaryName, BinaryTaskConfig } from '../../../../config/binaries';
import path from 'path';
import path from 'node:path';
import { AbstractBinary, FetchResult, BinaryItem, BinaryAdapter } from './AbstractBinary';

@SingletonProto()
Expand Down
2 changes: 1 addition & 1 deletion app/common/adapter/binary/ChromeForTestingBinary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { basename } from 'path';
import { basename } from 'node:path';
import { SingletonProto } from '@eggjs/tegg';
import { BinaryType } from '../../enum/Binary';
import { AbstractBinary, FetchResult, BinaryItem, BinaryAdapter } from './AbstractBinary';
Expand Down
2 changes: 1 addition & 1 deletion app/common/adapter/binary/NodePreGypBinary.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SingletonProto } from '@eggjs/tegg';
import { BinaryType } from '../../enum/Binary';
import binaries, { BinaryName } from '../../../../config/binaries';
import { join } from 'path';
import { join } from 'node:path';
import { AbstractBinary, FetchResult, BinaryItem, BinaryAdapter } from './AbstractBinary';

@SingletonProto()
Expand Down
4 changes: 2 additions & 2 deletions app/common/adapter/binary/PlaywrightBinary.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { AbstractBinary, BinaryAdapter, BinaryItem, FetchResult } from './AbstractBinary';
import util from 'util';
import path from 'path';
import util from 'node:util';
import path from 'node:path';
import { SingletonProto } from '@eggjs/tegg';
import { BinaryType } from '../../enum/Binary';

Expand Down
2 changes: 1 addition & 1 deletion app/common/aop/AsyncTimer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { performance } from 'perf_hooks';
import { performance } from 'node:perf_hooks';
import { Advice, AdviceContext, IAdvice } from '@eggjs/tegg/aop';
import { Inject } from '@eggjs/tegg';
import { EggLogger } from 'egg';
Expand Down
6 changes: 3 additions & 3 deletions app/common/typing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CnpmcoreConfig } from '../port/config';
import { Readable } from 'stream';
import { IncomingHttpHeaders } from 'http';
import { Readable } from 'node:stream';
import { IncomingHttpHeaders } from 'node:http';
import { EggContext } from '@eggjs/tegg';
import { estypes } from '@elastic/elasticsearch';
import { CnpmcoreConfig } from '../port/config';

export interface UploadResult {
key: string;
Expand Down
2 changes: 1 addition & 1 deletion app/core/entity/Hook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Entity, EntityData } from './Entity';
import { EasyData, EntityUtil } from '../util/EntityUtil';
import { HookType } from '../../common/enum/Hook';
import crypto from 'crypto';
import crypto from 'node:crypto';

export type CreateHookData = Omit<EasyData<HookData, 'hookId'>, 'enable' | 'latestTaskId'>;

Expand Down
4 changes: 2 additions & 2 deletions app/core/entity/Task.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os from 'os';
import path from 'path';
import os from 'node:os';
import path from 'node:path';
import { Entity, EntityData } from './Entity';
import { EasyData, EntityUtil } from '../util/EntityUtil';
import { TaskType, TaskState } from '../../common/enum/Task';
Expand Down
5 changes: 2 additions & 3 deletions app/core/service/BinarySyncerService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { rm } from 'fs/promises';
import {
AccessLevel,
SingletonProto,
Expand All @@ -8,7 +7,7 @@ import {
import {
EggHttpClient,
} from 'egg';
import fs from 'fs/promises';
import fs from 'node:fs/promises';
import { sortBy } from 'lodash';
import binaries, { BinaryName, CategoryName } from '../../../config/binaries';
import { BinaryRepository } from '../../repository/BinaryRepository';
Expand Down Expand Up @@ -223,7 +222,7 @@ export class BinarySyncerService extends AbstractService {
logs = [];
} finally {
if (localFile) {
await rm(localFile, { force: true });
await fs.rm(localFile, { force: true });
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/core/service/ChangesStreamService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os from 'os';
import { setTimeout } from 'timers/promises';
import os from 'node:os';
import { setTimeout } from 'node:timers/promises';
import {
AccessLevel,
SingletonProto,
Expand Down
8 changes: 4 additions & 4 deletions app/core/service/ProxyCacheService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EggHttpClient, HttpClientRequestOptions, HttpClientResponse } from 'egg';
import { EggHttpClient, HttpClientRequestOptions, HttpClientResponse, Context } from 'egg';
import { ForbiddenError } from 'egg-errors';
import { SingletonProto, AccessLevel, Inject, EggContext } from '@eggjs/tegg';
import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg';
import { BackgroundTaskHelper } from '@eggjs/tegg-background-task';
import { valid as semverValid } from 'semver';
import { AbstractService } from '../../common/AbstractService';
Expand Down Expand Up @@ -51,7 +51,7 @@ export class ProxyCacheService extends AbstractService {
@Inject()
private readonly backgroundTaskHelper:BackgroundTaskHelper;

async getPackageVersionTarResponse(fullname: string, ctx: EggContext): Promise<HttpClientResponse> {
async getPackageVersionTarResponse(fullname: string, ctx: Context): Promise<HttpClientResponse> {
if (this.config.cnpmcore.syncPackageBlockList.includes(fullname)) {
throw new ForbiddenError(`stop proxy by block list: ${JSON.stringify(this.config.cnpmcore.syncPackageBlockList)}`);
}
Expand Down Expand Up @@ -244,7 +244,7 @@ export class ProxyCacheService extends AbstractService {
await this.nfsAdapter.uploadBytes(storeKey, nfsBytes);
}

async getProxyResponse(ctx: Partial<EggContext>, options?: HttpClientRequestOptions): Promise<HttpClientResponse> {
async getProxyResponse(ctx: Partial<Context>, options?: HttpClientRequestOptions): Promise<HttpClientResponse> {
const registry = this.npmRegistry.registry;
const remoteAuthToken = await this.registryManagerService.getAuthTokenByRegistryHost(registry);
const authorization = this.npmRegistry.genAuthorizationHeader(remoteAuthToken);
Expand Down
2 changes: 1 addition & 1 deletion app/core/service/UserService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import crypto from 'crypto';
import crypto from 'node:crypto';
import {
AccessLevel,
SingletonProto,
Expand Down
2 changes: 1 addition & 1 deletion app/infra/AuthAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SingletonProto,
} from '@eggjs/tegg';
import { Redis } from 'ioredis';
import { randomUUID } from 'crypto';
import { randomUUID } from 'node:crypto';
import { AuthClient, AuthUrlResult, userResult } from '../common/typing';

const ONE_DAY = 3600 * 24;
Expand Down
8 changes: 7 additions & 1 deletion app/infra/NFSClientAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { EggAppConfig, EggLogger } from 'egg';
import FSClient from 'fs-cnpm';
import { AppendResult, NFSClient, UploadOptions, UploadResult, DownloadOptions } from '../common/typing';
import { Readable } from 'stream';
import { Readable } from 'node:stream';

@SingletonProto({
name: 'nfsClient',
Expand Down Expand Up @@ -68,10 +68,16 @@ export class NFSClientAdapter implements NFSClient {
}

async upload(filePath: string, options: UploadOptions): Promise<UploadResult> {
if (this.config.nfs.removeBeforeUpload) {
await this.remove(options.key);
}
return await this._client.upload(filePath, options);
}

async uploadBytes(bytes: Uint8Array, options: UploadOptions): Promise<UploadResult> {
if (this.config.nfs.removeBeforeUpload) {
await this.remove(options.key);
}
if (this._client.uploadBytes) {
return await this._client.uploadBytes(bytes, options);
}
Expand Down
Loading

0 comments on commit 4427a4f

Please sign in to comment.