diff --git a/package-lock.json b/package-lock.json index ba29a35..72da390 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,6 @@ "@types/express": "^4.17.21", "@types/node": "^22.5.0", "avsc": "^5.7.7", - "axios": "^1.7.5", "better-sqlite3": "^11.2.1", "cookie-parser": "^1.4.6", "cors": "^2.8.5", @@ -373,12 +372,6 @@ "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==", "license": "MIT" }, - "node_modules/asynckit": { - "version": "0.4.0", - "resolved": "https://registry.npmmirror.com/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "license": "MIT" - }, "node_modules/avsc": { "version": "5.7.7", "resolved": "https://registry.npmmirror.com/avsc/-/avsc-5.7.7.tgz", @@ -388,16 +381,6 @@ "node": ">=0.11" } }, - "node_modules/axios": { - "version": "1.7.5", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.5.tgz", - "integrity": "sha512-fZu86yCo+svH3uqJ/yTdQ0QHpQu5oL+/QE+QPSv6BZSkDAoky9vytxp7u5qk83OJFS3kEBcesWni9WTZAv3tSw==", - "dependencies": { - "follow-redirects": "^1.15.6", - "form-data": "^4.0.0", - "proxy-from-env": "^1.1.0" - } - }, "node_modules/base64-js": { "version": "1.5.1", "resolved": "https://registry.npmmirror.com/base64-js/-/base64-js-1.5.1.tgz", @@ -584,18 +567,6 @@ "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==", "license": "ISC" }, - "node_modules/combined-stream": { - "version": "1.0.8", - "resolved": "https://registry.npmmirror.com/combined-stream/-/combined-stream-1.0.8.tgz", - "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "license": "MIT", - "dependencies": { - "delayed-stream": "~1.0.0" - }, - "engines": { - "node": ">= 0.8" - } - }, "node_modules/content-disposition": { "version": "0.5.4", "resolved": "https://registry.npmmirror.com/content-disposition/-/content-disposition-0.5.4.tgz", @@ -726,15 +697,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/delayed-stream": { - "version": "1.0.0", - "resolved": "https://registry.npmmirror.com/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "license": "MIT", - "engines": { - "node": ">=0.4.0" - } - }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmmirror.com/depd/-/depd-2.0.0.tgz", @@ -990,40 +952,6 @@ "node": ">= 0.8" } }, - "node_modules/follow-redirects": { - "version": "1.15.6", - "resolved": "https://registry.npmmirror.com/follow-redirects/-/follow-redirects-1.15.6.tgz", - "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", - "funding": [ - { - "type": "individual", - "url": "https://github.com/sponsors/RubenVerborgh" - } - ], - "license": "MIT", - "engines": { - "node": ">=4.0" - }, - "peerDependenciesMeta": { - "debug": { - "optional": true - } - } - }, - "node_modules/form-data": { - "version": "4.0.0", - "resolved": "https://registry.npmmirror.com/form-data/-/form-data-4.0.0.tgz", - "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "license": "MIT", - "dependencies": { - "asynckit": "^0.4.0", - "combined-stream": "^1.0.8", - "mime-types": "^2.1.12" - }, - "engines": { - "node": ">= 6" - } - }, "node_modules/form-data-encoder": { "version": "4.0.2", "resolved": "https://registry.npmmirror.com/form-data-encoder/-/form-data-encoder-4.0.2.tgz", @@ -1740,12 +1668,6 @@ "node": ">= 0.10" } }, - "node_modules/proxy-from-env": { - "version": "1.1.0", - "resolved": "https://registry.npmmirror.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", - "license": "MIT" - }, "node_modules/pump": { "version": "3.0.0", "resolved": "https://registry.npmmirror.com/pump/-/pump-3.0.0.tgz", diff --git a/package.json b/package.json index e15efc1..dffabb1 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ "@types/express": "^4.17.21", "@types/node": "^22.5.0", "avsc": "^5.7.7", - "axios": "^1.7.5", "better-sqlite3": "^11.2.1", "cookie-parser": "^1.4.6", "cors": "^2.8.5", diff --git a/src/config.ts b/src/config.ts index 4463a34..bad3527 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,6 +1,6 @@ import dotenv from 'dotenv' import env from 'env-var' -import { Utilities } from './utilities' +import { Utilities } from './utilities.js' export class Config { public static instance: Config @@ -14,6 +14,7 @@ export class Config { public readonly certificateDir: string = env.get('CERTIFICATE_DIRECTORY').default("./certificates").asString() public readonly port: number = env.get('PORT').default(9388).asPortNumber() public readonly adminToken: string = env.get('ADMIN_TOKEN').default(Utilities.generateRandomString(24)).asString() + public static readonly version: string = "3.0.2"; private constructor() { } diff --git a/src/database/github-user.ts b/src/database/github-user.ts index 8020e64..ca30be1 100644 --- a/src/database/github-user.ts +++ b/src/database/github-user.ts @@ -1,5 +1,5 @@ -import { Table } from "../sqlite"; -import { UserEntity } from "./user"; +import { Table } from "../sqlite.js"; +import { UserEntity } from "./user.js"; @Table('github_users', ` id INTEGER PRIMARY KEY, diff --git a/src/index.ts b/src/index.ts index 464c7e7..6a84721 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { exec } from "child_process"; -import { Config } from "./config"; -import { Server } from "./server"; -import { Utilities } from "./utilities"; +import { Config } from "./config.js"; +import { Server } from "./server.js"; +import { Utilities } from "./utilities.js"; function onStop(signal: string) { server.db.close(); diff --git a/src/server.ts b/src/server.ts index f472700..188d9d7 100644 --- a/src/server.ts +++ b/src/server.ts @@ -5,7 +5,6 @@ import path from 'path'; import { Server as SocketIOServer } from 'socket.io'; // import cors from 'cors'; import JwtHelper from './jwt-helper.js'; -import axios from 'axios'; import { SQLiteHelper } from './sqlite.js'; import { UserEntity } from './database/user.js'; import { ClusterEntity } from './database/cluster.js'; @@ -19,7 +18,7 @@ import { HourlyStatsStorage } from './statistics/hourly-stats.js'; import cookieParser from 'cookie-parser'; import { Plugin } from './plugin/Plugin.js'; import { PluginLoader } from './plugin/PluginLoader.js'; -import { Got } from 'got'; +import got, {type Got} from 'got' // 创建一个中间件函数 const logMiddleware = (req: Request, res: Response, next: NextFunction) => { @@ -52,6 +51,7 @@ export class Server { protected centerStats: HourlyStatsStorage; protected plugins: Plugin[]; protected pluginLoader: PluginLoader; + protected got: Got; public constructor() { this.plugins = []; @@ -63,6 +63,12 @@ export class Server { }) .catch(error => console.error(error)); + this.got = got.extend({ + headers: { + 'User-Agent': `93AtHome-V3/${Config.version}` + } + }); + this.files = []; this.avroBytes = new Uint8Array(); @@ -192,28 +198,27 @@ export class Server { const code = req.query.code as string || ''; // 请求GitHub获取access_token - const tokenResponse = await axios.post(`https://${Config.getInstance().githubUrl}/login/oauth/access_token`, { - code, - client_id: Config.getInstance().githubOAuthClientId, - client_secret: Config.getInstance().githubOAuthClientSecret - }, { - headers: { - 'Accept': 'application/json' - } + const tokenResponse = await this.got.post(`https://${Config.getInstance().githubUrl}/login/oauth/access_token`, { + json: { + code, + client_id: Config.getInstance().githubOAuthClientId, + client_secret: Config.getInstance().githubOAuthClientSecret + }, + responseType: 'json' }); - const tokenData = tokenResponse.data as { access_token: string }; + const tokenData = tokenResponse.body as { access_token: string }; const accessToken = tokenData.access_token; let userResponse; try { - userResponse = await axios.get(`https://${Config.getInstance().githubApiUrl}/user`, { + userResponse = await this.got.get(`https://${Config.getInstance().githubApiUrl}/user`, { headers: { 'Authorization': `token ${accessToken}`, 'Accept': 'application/json', 'User-Agent': 'Open93AtHome-V3/3.0.0' // GitHub API要求设置User-Agent } - }).then(response => response.data) as { id: number, login: string, avatar_url: string, name: string }; + }).then(response => response.body) as { id: number, login: string, avatar_url: string, name: string }; } catch (error) { console.error('Error fetching GitHub user info: ', error as Error); throw error; // 或者返回一个默认的错误响应 diff --git a/src/sqlite.ts b/src/sqlite.ts index cf8a7ce..8c31814 100644 --- a/src/sqlite.ts +++ b/src/sqlite.ts @@ -1,5 +1,5 @@ import Database from 'better-sqlite3'; -import { ClusterEntity } from './database/cluster'; +import { ClusterEntity } from './database/cluster.js'; // 表的定义映射 const tableSchemaMap = new Map(); diff --git a/src/utilities.ts b/src/utilities.ts index a1ee230..6c6db68 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -6,11 +6,11 @@ import JwtHelper from './jwt-helper.js'; import { File, IFileInfo } from './database/file.js'; import { compress } from '@mongodb-js/zstd'; import avsc from 'avsc'; -import axios from 'axios'; import { exec } from 'child_process'; import { ClusterEntity } from './database/cluster.js'; import { SQLiteHelper } from './sqlite.js'; import { UserEntity } from './database/user.js'; +import got, { Got } from 'got'; export const FileListSchema = avsc.Type.forSchema({ type: 'array', @@ -27,6 +27,8 @@ export const FileListSchema = avsc.Type.forSchema({ }) export class Utilities { + public static got: Got = got.extend(); + public static isRunningInDocker(): boolean { return process.env.IS_IN_DOCKER === 'true'; } @@ -156,8 +158,10 @@ export class Utilities { public static async checkUrl(url: string): Promise<{ url: string; hash: string }> { try { - const response = await axios.get(url, { responseType: 'arraybuffer' }); - const responseData = response.data as Buffer; + const response = await got.get(url, { + responseType: 'buffer' + }); + const responseData = response.body as Buffer; // 计算响应数据的哈希值 const hash = crypto.createHash('sha1').update(responseData).digest('hex');