-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 💡 clean code, reorganized config, and purge feature
- Loading branch information
Showing
41 changed files
with
672 additions
and
1,807 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
NPM_REGISTRY_URL=https://registry.npmmirror.com/ | ||
PORT=4567 | ||
HOST_NAME=0.0.0.0 | ||
CORS_ORIGIN=* | ||
CACHE_DIR=/cache | ||
NPM_AUTH_TOKEN= | ||
# HOST=0.0.0.0 | ||
# CORS_ORIGIN=* | ||
CACHE_DIR=./.cache | ||
# CACHE_MAX_SIZE=4 | ||
NPM_REGISTRY=https://registry.npmmirror.com/ | ||
ESM_ORIGIN=http://bunpkg.com | ||
# NPM_MAX_TGZ_SIZE=100 | ||
# NPM_AUTH_TOKEN= | ||
# JWT_SECRET= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import path from "node:path"; | ||
import { cors } from "@elysiajs/cors"; | ||
|
||
export const BunPkgConfig = { | ||
/** 服务器相关 */ | ||
server: { | ||
/** | ||
* 端口号 | ||
* @default process.env.PORT || 4567 | ||
*/ | ||
port: 4567, | ||
/** | ||
* 主机名 | ||
* @default process.env.HOST || '0.0.0.0' | ||
*/ | ||
// host: "0.0.0.0", | ||
/** | ||
* 跨域配置 | ||
* @default {origin: process.env.CORS_ORIGIN } || Paramaters<typeof cors[0]> | ||
* @docuemnt see more https://elysiajs.com/plugins/cors.html#config | ||
*/ | ||
cors: { | ||
origin: "*", | ||
}, | ||
}, | ||
/** 缓存配置 */ | ||
cache: { | ||
/** | ||
* 缓存硬盘占用空间最大值 (Gib) | ||
* @default process.env.CACHE_MAX_SIZE || 4 | ||
*/ | ||
maxSize: 4, | ||
/** | ||
* 磁盘缓存目录位置 | ||
* @default process.env.CACHE_DIR || '/cache' | ||
*/ | ||
dir: "/cache", | ||
}, | ||
/** NPM 配置 */ | ||
npm: { | ||
/** | ||
* 上游 NPM 源地址 | ||
* @default process.env.NPM_REGISTRY || 'https://registry.npmjs.org/' | ||
*/ | ||
registry: "https://registry.npmjs.org/", | ||
/** | ||
* 私有 npm 认证头 | ||
* Authorization: Bearer ${authToken} | ||
*/ | ||
// authToken: "", | ||
/** | ||
* 支持最大 npm tgz 压缩包尺寸 (mib) | ||
* @default 100 (Mib) | ||
* @default process.env.NPM_MAX_TGZ_SIZE || 100 | ||
**/ | ||
maxTgzSize: 100, | ||
}, | ||
esm: { | ||
/** | ||
* ESM 前缀配置 | ||
* @default process.env.ESM_ORIGIN | ||
*/ | ||
origin: "", | ||
}, | ||
/** | ||
* 是否开启 JWT 认证, 只有有当前配置项并添加了 secret 的情况下才会开启 | ||
* seemore https://elysiajs.com/plugins/jwt.html | ||
*/ | ||
jwt: { | ||
// /** | ||
// * 认证密钥 | ||
// * @default process.env.JWT_SECRET | ||
// **/ | ||
// secret: "", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,58 @@ | ||
export const erros = { | ||
// 解析 url path 失败 | ||
ParsePathError: 403, | ||
import { InternalServerError, NotFoundError, ParseError } from "elysia"; | ||
|
||
class PathValidationError extends Error { | ||
code = "PathValidationError"; | ||
status = 400; | ||
} | ||
|
||
class UnAuthorizedError extends Error { | ||
code = "UnAuthorizedError"; | ||
status = 401; | ||
} | ||
|
||
class ForbiddenError extends Error { | ||
code = "ForbiddenError"; | ||
status = 403; | ||
} | ||
|
||
class TarballSizeLimitedError extends Error { | ||
code = "TgzSizeLimitError"; | ||
status = 500; | ||
} | ||
|
||
class BanPackageError extends Error { | ||
code = "BanPackageError"; | ||
status = 404; | ||
} | ||
|
||
const TypedError = { | ||
NotFoundError, | ||
InternalServerError, | ||
ParseError, | ||
PathValidationError, | ||
UnAuthorizedError, | ||
ForbiddenError, | ||
TarballSizeLimitedError, | ||
BanPackageError, | ||
}; | ||
|
||
export const err = (...labels: (string | undefined)[]) => { | ||
export enum ErrorCodes { | ||
NotFoundError = "NotFoundError", | ||
InternalServerError = "InternalServerError", | ||
ParseError = "ParseError", | ||
PathValidationError = "PathValidationError", | ||
UnAuthorizedError = "UnAuthorizedError", | ||
ForbiddenError = "ForbiddenError", | ||
TarballSizeLimitedError = "TarballSizeLimitedError", | ||
BanPackageError = "BanPackageError", | ||
} | ||
|
||
export const markError = ( | ||
code: keyof typeof TypedError, | ||
...labels: (string | undefined)[] | ||
) => { | ||
const msg = labels.pop(); | ||
const Factory = TypedError[code] ?? Error; | ||
|
||
return new Error(`${labels.join(" - ")}:: ${msg ?? ""}`); | ||
return new Factory(`[${code}]::${labels.join(" - ")} | ${msg ?? ""}`); | ||
}; |
Oops, something went wrong.