Skip to content

Commit

Permalink
refactor: 💡 clean code, reorganized config, and purge feature
Browse files Browse the repository at this point in the history
  • Loading branch information
charlzyx committed Mar 5, 2024
1 parent 6fb732d commit eab44c6
Show file tree
Hide file tree
Showing 41 changed files with 672 additions and 1,807 deletions.
14 changes: 9 additions & 5 deletions .env
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=
14 changes: 9 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ RUN bun install --production
# case by DOCKER ENV
RUN rm -rf .env

ENV NODE_ENV production
ENV NPM_REGISTRY_URL=https://registry.npmmirror.com/
ENV NPM_AUTH_TOKEN=
ENV CORS_ORIGIN=*
ENV CACHE_DIR=/cache
ENV PORT 4567
ENV HOST 0.0.0.0
ENV CORS_ORIGIN *
ENV CACHE_DIR /cache
ENV CACHE_MAX_SIZE 4
ENV NPM_REGISTRY_URL https://registry.npmmirror.com/
ENV NPM_MAX_TGZ_SIZE 100
ENV NPM_AUTH_TOKEN ''
ENV JWT_SECRET ''

VOLUME [ "/cache" ]

Expand Down
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ PORT=4567
HOST_NAME=127.0.0.1
CORS_ORIGIN=*
CACHE_DIR=/cache
CACHE_GIB=4
NPM_AUTH_TOKEN=
CACHE_MAX_SIZE=4
NPM_AUTHTOKEN=
```

## docker
Expand All @@ -97,7 +97,7 @@ NPM_AUTH_TOKEN=
# CACHE_GIB=4
# NPM_AUTH_TOKEN=
docker run -i -t -p 4567:4567 chaogpt/bunpkg
docker run --env-file .env -i -t -p 4567:4567 -v /cache:./cache chaogpt/bunpkg
docker run --env-file .env -i -t -p 4567:4567 -v /cache:./cache -v bunpkg.config.ts:/app/bunpkg.config.ts chaogpt/bunpkg
```

## Roadmap
Expand Down
30 changes: 0 additions & 30 deletions hash.ts

This file was deleted.

13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,27 @@
"@elysiajs/html": "^0.8.0",
"@elysiajs/jwt": "^0.8.0",
"bun-compression": "^0.0.4",
"changelogen": "^0.5.5",
"consola": "^3.2.3",
"defu": "^6.1.4",
"elysia": "^0.8.17",
"get-port-please": "^3.1.2",
"gunzip-maybe": "^1.4.2",
"lru-cache": "^10.2.0",
"mime": "^3.0.0",
"nanotar": "^0.1.1",
"semver": "^7.6.0",
"sri-toolbox": "^0.2.0",
"tar-stream": "^3.1.7",
"validate-npm-package-name": "^5.0.0",
"whatwg-url": "^14.0.0"
"validate-npm-package-name": "^5.0.0"
},
"devDependencies": {
"@types/gunzip-maybe": "^1.4.2",
"@types/mime": "^3.0.4",
"@types/react": "^18.2.61",
"@types/react": "^18.2.62",
"@types/react-dom": "^18.2.19",
"@types/semver": "^7.5.8",
"@types/tar-stream": "^3.1.3",
"@types/validate-npm-package-name": "^4.0.2",
"@types/whatwg-url": "^11.0.4",
"bun-types": "latest",
"vocs": "^1.0.0-alpha.41"
"changelogen": "^0.5.5",
"vocs": "^1.0.0-alpha.42"
}
}
24 changes: 12 additions & 12 deletions src/__tests__/esm.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BunPkgConfig } from "../config";
import { BunPkgConfig } from "../config.final";
import { toESM } from "../experimental/esm";

import { describe, expect, it } from "bun:test";
Expand All @@ -21,19 +21,19 @@ export { why } from './not';
`;

const output = `
import * as whatever from 'https://bunpkg.esm/pkg@v1.2.3/some/esm/feature/whatever.js?module'
import a, { b } from 'https://bunpkg.esm/aha?module'
import c, { d } from 'https://bunpkg.esm/pkg@v1.2.3/some/esm/feature/aha?module'
import e, { f } from 'https://bunpkg.esm/pkg@v1.2.3/some/esm/aha?module'
import g, { h } from 'https://bunpkg.esm/pkg@v1.2.3/some/aha?module'
import i, { j } from 'https://bunpkg.esm/pkg@v1.2.3/aha?module'
export * from 'https://bunpkg.esm/pkg@v1.2.3/some/esm/feature/aha?module';
export * from 'https://bunpkg.esm/ha?module';
export { what } from 'https://bunpkg.esm/else?module';
export { why } from 'https://bunpkg.esm/pkg@v1.2.3/some/esm/feature/not?module';
import * as whatever from 'https://bunpkg.esm/esm/pkg@v1.2.3/some/esm/feature/whatever.js'
import a, { b } from 'https://bunpkg.esm/esm/aha'
import c, { d } from 'https://bunpkg.esm/esm/pkg@v1.2.3/some/esm/feature/aha'
import e, { f } from 'https://bunpkg.esm/esm/pkg@v1.2.3/some/esm/aha'
import g, { h } from 'https://bunpkg.esm/esm/pkg@v1.2.3/some/aha'
import i, { j } from 'https://bunpkg.esm/esm/pkg@v1.2.3/aha'
export * from 'https://bunpkg.esm/esm/pkg@v1.2.3/some/esm/feature/aha';
export * from 'https://bunpkg.esm/esm/ha';
export { what } from 'https://bunpkg.esm/esm/else';
export { why } from 'https://bunpkg.esm/esm/pkg@v1.2.3/some/esm/feature/not';
`;
const out = toESM(ORIGIN, "pkg@v1.2.3/some/esm/feature/index.js", input);
console.log("🚀 ~ it ~ out:", out);
console.log(`🚀 ~ it ~ o:`, out);
expect(out).toEqual(output);
});
});
3 changes: 2 additions & 1 deletion src/__tests__/sqlcache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ const getSql = (expire?: boolean) => {
current: [] as string[],
};
const sql = new SqliteLRUCache({
database: ":memory:",
maxLen: 3,
maxByteSize: 6,
onRemove(items, reason) {
// console.log(`🚀 ~ onRemove ~ items:`, reason, items);
// console.log(`🚀 ~ onRemove ~ items:`, items);
ref.current = ref.current.concat(items.map((x) => x.key));
},
});
Expand Down
76 changes: 76 additions & 0 deletions src/bunpkg.config.ts
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: "",
},
};
31 changes: 20 additions & 11 deletions src/common/cache.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { unlink } from "node:fs";
import { SqliteLRUCache } from "./sqlite-lru-cache";
import { BunPkgConfig } from "../config";
import path from "path";
import { BunPkgConfig } from "../config.final";
import path from "node:path";
import { LRUCache } from "lru-cache";
import { BunFile } from "bun";
import { serialize, deserialize } from "v8";
import { markError } from "./err";

const database = path.join(BunPkgConfig.cacheDir, `cache.sqlite`);
const database = path.join(BunPkgConfig.cache.dir, `cache.sqlite`);

const sqlite = new SqliteLRUCache<{
file_path?: string;
Expand All @@ -15,9 +16,9 @@ const sqlite = new SqliteLRUCache<{
}>({
database,
/** 4Gib */
maxByteSize: 4 * Math.pow(2, 30),
/** 10W */
maxLen: 10 * Math.pow(10, 4),
maxByteSize: BunPkgConfig.cache.maxSize * Math.pow(2, 30),
/** 8W */
maxLen: 8 * 10000,
onRemove(items, reason) {
items.forEach((item) => {
if (item?.meta?.file_path) {
Expand All @@ -29,6 +30,9 @@ const sqlite = new SqliteLRUCache<{
const tasks: Record<string, Promise<any>> = {};

export const sqliteCache = {
purge(key: string, wild?: boolean) {
sqlite.purge(key, wild);
},
async read(key: string) {
const meta = sqlite.get(key)?.meta;
const maybe = meta?.file_path;
Expand Down Expand Up @@ -65,7 +69,7 @@ export const sqliteCache = {
expire,
});
} else {
const filepath = path.join(BunPkgConfig.cacheDir, "file", key);
const filepath = path.join(BunPkgConfig.cache.dir, "files", key);
if (filepath in tasks) return tasks[filepath];
tasks[filepath] = Bun.write(filepath, content).then((size) => {
sqlite.set({
Expand All @@ -80,10 +84,15 @@ export const sqliteCache = {
// size checking
if (meta.size && meta.size !== size) {
unlink(filepath, () => {});
throw new Error(
`File Write Error, size not match ${filepath}, except ${meta.size}, recived ${size} `,
throw markError(
"InternalServerError",
"Sqlite Cache",
"File Write Failed, Case by",
"File Size Not Match",
`${filepath} except ${meta.size}, recived ${size} `,
);
}
return [filepath, Bun.file(filepath)] as const;
});
return tasks[filepath];
}
Expand All @@ -97,8 +106,8 @@ export const memoLRU = new LRUCache<string, any>({
sizeCalculation: (value) => {
return Buffer.byteLength(value as any) || 1;
},
// 1 Gib
maxSize: 1 * Math.pow(2, 30),
// 500 Mib
maxSize: 500 * Math.pow(2, 20),
});

export const memoCache = {
Expand Down
58 changes: 53 additions & 5 deletions src/common/err.ts
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 ?? ""}`);
};
Loading

0 comments on commit eab44c6

Please sign in to comment.