更新依赖项,优化 WebSocket 处理,添加文件流管道功能,改进用户认证逻辑

This commit is contained in:
2025-12-21 02:06:38 +08:00
parent d22be3a840
commit 15fcfdad18
16 changed files with 150 additions and 262 deletions

View File

@@ -7,4 +7,6 @@ export * from './get-router.ts'
export * from './get-content-type.ts'
export * from './utils.ts'
export * from './utils.ts'
export { pipeFileStream, pipeStream } from './pipe.ts'

View File

@@ -0,0 +1,19 @@
import * as http from 'http';
import * as fs from 'fs';
import { isBun } from '../../utils/get-engine.ts';
export const pipeFileStream = (filePath: string, res: http.ServerResponse) => {
const readStream = fs.createReadStream(filePath);
if (isBun) {
res.pipe(readStream as any);
} else {
readStream.pipe(res, { end: true });
}
}
export const pipeStream = (readStream: fs.ReadStream, res: http.ServerResponse) => {
if (isBun) {
res.pipe(readStream as any);
} else {
readStream.pipe(res, { end: true });
}
}

View File

@@ -10,7 +10,7 @@ import { addStat } from '@/modules/html/stat/index.ts';
import path from 'path';
import { getTextContentType } from '@/modules/fm-manager/index.ts';
import { logger } from '@/modules/logger.ts';
import { pipeStream } from '../pipe.ts';
const pipelineAsync = promisify(pipeline);
export async function downloadFileFromMinio(fileUrl: string, destFile: string) {
@@ -74,7 +74,7 @@ export async function minioProxy(
res.writeHead(200, {
...headers,
});
objectStream.pipe(res, { end: true });
pipeStream(objectStream as any, res);
}
return true;
} catch (error) {
@@ -154,7 +154,7 @@ export const httpProxy = async (
res.writeHead(proxyRes.statusCode, {
...headers,
});
proxyRes.pipe(res, { end: true });
pipeStream(proxyRes as any, res);
}
});
proxyReq.on('error', (err) => {

View File

@@ -1,4 +1,4 @@
import http from 'http';
import http from 'node:http';
import { minioClient } from '@/modules/minio.ts';
type ProxyInfo = {
path?: string;

View File

@@ -1,5 +1,6 @@
import { IncomingMessage } from 'node:http';
import http from 'node:http';
import { logger } from '../logger.ts';
export const getUserFromRequest = (req: IncomingMessage) => {
const url = new URL(req.url, `http://${req.headers.host}`);
@@ -14,8 +15,8 @@ export const getUserFromRequest = (req: IncomingMessage) => {
export const getDNS = (req: http.IncomingMessage) => {
const hostName = req.headers.host;
const ip = req.socket.remoteAddress;
const hostName = req.headers?.host;
const ip = req?.socket?.remoteAddress || '';
return { hostName, ip };
};