Commit a9631fe6 authored by nanahira's avatar nanahira

fix error thing

parent fc9c3eaf
Pipeline #40987 passed with stages
in 1 minute and 9 seconds
......@@ -4,6 +4,7 @@ import { ApiBody, ApiCreatedResponse, ApiOkResponse, ApiOperation, ApiParam, Api
import { DepotDto } from '../dto/Depot.dto';
import { Request, Response } from 'express';
import { createHash } from 'crypto';
import { ReturnMessageDto } from 'src/dto/ReturnMessage.dto';
function md5HexOfString(s: string) {
return createHash('md5').update(s).digest('hex');
......@@ -11,7 +12,6 @@ function md5HexOfString(s: string) {
/**
* 以“稳定字节串”为准计算 ETag,并发送响应。
* - 不捕获异常,让 Nest 接管错误处理
* - 若 If-None-Match 命中,返回 304
* - 若未显式设置 Cache-Control,则使用传入的默认值
*/
......@@ -22,7 +22,14 @@ async function stableSend(
defaultCache = 'public, max-age=31536000, immutable',
contentType = 'application/json; charset=utf-8'
): Promise<void> {
const bodyResolved = await body;
let bodyResolved: any;
try {
bodyResolved = await body;
} catch (e) {
// this is error occurred
res.status(500).json(new ReturnMessageDto(500, 'Internal Server Error'));
return;
}
const bodyString = typeof bodyResolved === 'string' ? bodyResolved : JSON.stringify(bodyResolved);
const hash = md5HexOfString(bodyString);
const quoted = `"${hash}"`;
......@@ -56,19 +63,9 @@ async function stableSend(
export class UpdateController {
constructor(private readonly updateService: UpdateService) {}
private async stableReturn<T>(res: Response, prom: Promise<T> | T, cache = 'public, max-age=31536000, immutable') {
const result = await prom;
const stringified = JSON.stringify(result);
const hash = createHash('md5').update(stringified).digest('hex');
res.setHeader('ETag', `"${hash}"`);
if (res.getHeader('Cache-Control') === undefined) res.setHeader('Cache-Control', cache);
res.end(stringified);
// return result;
}
@Get('apps.json')
@ApiOperation({ summary: '获取 apps.json', description: '懒得解释这是啥了……' })
async getAppsJson(@Req() req: Request, @Res({ passthrough: true }) res: Response) {
async getAppsJson(@Req() req: Request, @Res() res: Response) {
await stableSend(req, res, this.updateService.getAppsJson(), 'public, max-age=600, stale-while-revalidate=600, stale-if-error=604800');
return;
}
......@@ -81,7 +78,7 @@ export class UpdateController {
// @Header('Cache-Control', 'public, max-age=31536000, immutable') // 可留给 stableSend 兜底
async getChecksum(
@Req() req: Request,
@Res({ passthrough: true }) res: Response,
@Res() res: Response,
@Param('id') id: string,
@Query(new ValidationPipe({ transform: true })) depot: DepotDto,
@Param('version') version: string,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment