更新依赖项,优化 WebSocket 处理,添加文件流管道功能,改进用户认证逻辑
This commit is contained in:
@@ -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'
|
||||
19
src/modules/fm-manager/pipe.ts
Normal file
19
src/modules/fm-manager/pipe.ts
Normal 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 });
|
||||
}
|
||||
}
|
||||
@@ -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) => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import http from 'http';
|
||||
import http from 'node:http';
|
||||
import { minioClient } from '@/modules/minio.ts';
|
||||
type ProxyInfo = {
|
||||
path?: string;
|
||||
|
||||
@@ -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 };
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user