Skip to content

Commit

Permalink
chore: daily development
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Dec 23, 2024
1 parent aa63818 commit bc29120
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/canyon-collect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@
"dependencies": {
"@nestjs/common": "^10.4.15",
"@nestjs/core": "^10.4.15",
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.4.15",
"@nestjs/typeorm": "^10.0.2",
"@prisma/client": "5.16.1",
"canyon-data": "^2.0.0-beta.7",
"canyon-map": "^2.0.0-beta.6",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"diff": "^7.0.0",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"istanbul-lib-coverage": "^3.2.2",
"istanbul-lib-source-maps": "^5.0.6",
"passport-jwt": "^4.0.1",
"prisma": "5.16.1",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"zod": "^3.24.1",
"@nestjs/typeorm": "^10.0.2",
"typeorm": "^0.3.20",
"sqlite3": "^5.1.7",
"test-exclude": "^7.0.1",
"canyon-data": "^2.0.0-beta.7",
"canyon-map": "^2.0.0-beta.6",
"diff": "^7.0.0"
"typeorm": "^0.3.20",
"zod": "^3.24.1"
},
"devDependencies": {
"@nestjs/cli": "^10.4.9",
Expand All @@ -42,6 +45,7 @@
"@types/jest": "^29.5.14",
"@types/node": "^22.10.2",
"@types/supertest": "^6.0.2",
"@types/test-exclude": "^6.0.2",
"@typescript-eslint/eslint-plugin": "^8.18.1",
"@typescript-eslint/parser": "^8.18.1",
"eslint": "^8",
Expand All @@ -55,7 +59,6 @@
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.7.2",
"@types/test-exclude": "^6.0.2"
"typescript": "^5.7.2"
}
}
2 changes: 2 additions & 0 deletions packages/canyon-collect/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CollectModule } from "./apps/collect/collect.module";
import { PrismaService } from "./prisma/prisma.service";
import { TypeOrmModule } from "@nestjs/typeorm";
import { CoveragediskEntity } from "./apps/collect/entity/coveragedisk.entity";
import { AuthModule } from "./apps/auth/auth.module";
@Module({
imports: [
TypeOrmModule.forRoot({
Expand All @@ -13,6 +14,7 @@ import { CoveragediskEntity } from "./apps/collect/entity/coveragedisk.entity";
synchronize: true,
entities: [CoveragediskEntity],
}),
AuthModule,
CollectModule,
],
controllers: [AppController],
Expand Down
24 changes: 24 additions & 0 deletions packages/canyon-collect/src/apps/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Module } from "@nestjs/common";
import { JwtModule } from "@nestjs/jwt";
import { PassportModule } from "@nestjs/passport";
// import { AuthService } from "./auth.service";
import { jwtConstants } from "./constants";
import { JwtStrategy } from "./strategies/jwt.strategy";
// import { LocalStrategy } from "./strategies/local.strategy";
// import { AuthController } from "./auth.controller";
// import { PrismaModule } from "../prisma/prisma.module";
// import { OauthgitproviderService } from "./services/oauthgitprovider.service";

@Module({
imports: [
PassportModule,
JwtModule.register({
secret: jwtConstants.secret,
signOptions: { expiresIn: "10y" },
}),
],
controllers: [],
providers: [JwtStrategy],
exports: [],
})
export class AuthModule {}
3 changes: 3 additions & 0 deletions packages/canyon-collect/src/apps/auth/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const jwtConstants = {
secret: "secretKey",
};
13 changes: 13 additions & 0 deletions packages/canyon-collect/src/apps/auth/guards/jwt-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ExecutionContext, Injectable } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";
import { Reflector } from "@nestjs/core";
// import { IS_PUBLIC_KEY } from "../../decorators/public.decorator";
@Injectable()
export class JwtAuthGuard extends AuthGuard("jwt") {
constructor(private reflector: Reflector) {
super();
}
canActivate(context: ExecutionContext): any {
return super.canActivate(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Injectable } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";

@Injectable()
export class LocalAuthGuard extends AuthGuard("local") {}
19 changes: 19 additions & 0 deletions packages/canyon-collect/src/apps/auth/strategies/jwt.strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { ExtractJwt, Strategy } from "passport-jwt";
import { jwtConstants } from "../constants";

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: jwtConstants.secret,
});
}

async validate(payload: any) {
return payload;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
Controller,
Post,
UploadedFile,
UseGuards,
UseInterceptors,
Request,
} from "@nestjs/common";
import { PrismaService } from "../../prisma/prisma.service";
import { CoverageClientService } from "./services/coverage-client.service";
Expand All @@ -12,6 +14,7 @@ import { FileInterceptor } from "@nestjs/platform-express";
import { CoverageMapClientService } from "./services/coverage-map-client.service";
import { CoverageMapClientDto } from "./dto/coverage-map-client.dto";
import zlib from "zlib";
import { JwtAuthGuard } from "../auth/guards/jwt-auth.guard";
// 解压 GZIP 的 Buffer 数据
async function decompressData(buffer) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -39,15 +42,18 @@ export class CollectController {
4. 经过测试在macbookpro上compressDataWithStream压缩1600kb的数据,压缩后64kb左右,耗时8ms左右
TODO 得调研8ms对navigator.sendBeacon有没有影响
*/
@UseGuards(JwtAuthGuard)
@UseInterceptors(FileInterceptor("coverage"))
@Post("coverage/client")
async coverageClient(
@UploadedFile() cov: any,
@Body() coverageClientDto: CoverageClientDto,
@Request() req: any,
) {
if (coverageClientDto.coverage) {
return this.coverageClientService.invoke({
...coverageClientDto,
reporter: String(req.user.id),
});
}
let coverage = {};
Expand All @@ -65,6 +71,7 @@ export class CollectController {
return this.coverageClientService.invoke({
...coverageClientDto,
coverage,
reporter: String(req.user.id),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class CoveragediskService {
reportID: string;
coverage: any;
compareTarget: string;
reporter: string;
}) {
return this.coveragediskRepository.insert({
pid: String(process.pid),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class CoverageClientService {
reportID: _reportID,
branch,
compareTarget,
reporter,
}) {
const reportID = _reportID || sha;
// #region == Step x: 解析出上报上来的覆盖率数据
Expand Down Expand Up @@ -116,6 +117,7 @@ export class CoverageClientService {
reportID,
compareTarget: compareTarget || sha,
coverage: hit,
reporter,
});
return {
success: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class CoverageMapClientService {
...summaryToDbSummary(overallSummary),
reportID: sha,
compareTarget: compareTarget || sha, // 默认是自己
reporter: "canyon",
},
})
.catch(() => {
Expand Down

0 comments on commit bc29120

Please sign in to comment.