This commit is contained in:
熊潇 2025-04-30 19:27:31 +08:00
parent d4aa63eea2
commit 5e03ef2dd9
7 changed files with 82 additions and 131 deletions

View File

@ -1,5 +1,11 @@
name: Sync to CNB name: Sync to CNB
on: [push] on:
push:
branches:
# - main
- 'releases/**'
workflow_dispatch:
# This workflow is triggered on push events to the repository. # This workflow is triggered on push events to the repository.
jobs: jobs:
sync: sync:

20
bun.config.mjs Normal file
View File

@ -0,0 +1,20 @@
// @ts-check
// https://bun.sh/docs/bundler
// @ts-ignore
import pkg from './package.json';
import { resolvePath as rp } from '@kevisual/use-config/env';
// bun run src/index.ts --
await Bun.build({
target: 'node',
format: 'esm',
entrypoints: [rp('./src/main.ts')],
outdir: rp('./dist'),
naming: {
entry: 'app.mjs',
},
define: {
VERSION: JSON.stringify(pkg.version),
},
env: 'KEVISUAL_*',
});

View File

@ -13,11 +13,8 @@
] ]
}, },
"scripts": { "scripts": {
"watch": "rollup -c rollup.config.mjs -w", "dev": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 bun --watch src/dev.ts ",
"build": "rollup -c rollup.config.mjs", "build": "rimraf dist && bun run bun.config.mjs",
"dev": "cross-env NODE_ENV=development nodemon --delay 2.5 -e js,cjs,mjs --exec node dist/app.mjs",
"dev:watch": "cross-env NODE_ENV=development concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"sleep 1 && npm run dev\" ",
"bun:dev": "bun run dev",
"test": "tsx test/**/*.ts", "test": "tsx test/**/*.ts",
"clean": "rm -rf dist", "clean": "rm -rf dist",
"pub": "npm run build && envision pack -p -u", "pub": "npm run build && envision pack -p -u",
@ -37,9 +34,8 @@
}, },
"dependencies": { "dependencies": {
"@kevisual/code-center-module": "0.0.18", "@kevisual/code-center-module": "0.0.18",
"@kevisual/use-config": "^1.0.12",
"@kevisual/mark": "0.0.7",
"@kevisual/router": "0.0.13", "@kevisual/router": "0.0.13",
"@kevisual/use-config": "^1.0.12",
"cookie": "^1.0.2", "cookie": "^1.0.2",
"dayjs": "^1.11.13", "dayjs": "^1.11.13",
"formidable": "^3.5.4", "formidable": "^3.5.4",
@ -48,12 +44,7 @@
"devDependencies": { "devDependencies": {
"@kevisual/types": "^0.0.9", "@kevisual/types": "^0.0.9",
"@kevisual/use-config": "^1.0.12", "@kevisual/use-config": "^1.0.12",
"@rollup/plugin-alias": "^5.1.1", "@types/bun": "^1.2.11",
"@rollup/plugin-commonjs": "^28.0.3",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-replace": "^6.0.2",
"@rollup/plugin-typescript": "^12.1.2",
"@types/crypto-js": "^4.2.2", "@types/crypto-js": "^4.2.2",
"@types/formidable": "^3.4.5", "@types/formidable": "^3.4.5",
"@types/lodash-es": "^4.17.12", "@types/lodash-es": "^4.17.12",
@ -63,20 +54,11 @@
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"inquire": "^0.4.8", "inquire": "^0.4.8",
"ioredis": "^5.6.1", "ioredis": "^5.6.1",
"jsrepo": "^1.47.1",
"nodemon": "^3.1.10", "nodemon": "^3.1.10",
"pg": "^8.15.6", "pg": "^8.15.6",
"pino": "^9.6.0",
"pino-pretty": "^13.0.0",
"pm2": "^6.0.5",
"rimraf": "^6.0.1", "rimraf": "^6.0.1",
"rollup": "^4.40.1",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-dts": "^6.2.1",
"rollup-plugin-esbuild": "^6.2.1",
"sequelize": "^6.37.7", "sequelize": "^6.37.7",
"tape": "^5.9.0", "tape": "^5.9.0",
"tsx": "^4.19.4",
"typescript": "^5.8.3" "typescript": "^5.8.3"
}, },
"packageManager": "pnpm@10.10.0" "packageManager": "pnpm@10.10.0"

View File

@ -1,75 +0,0 @@
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import path from 'path';
import esbuild from 'rollup-plugin-esbuild';
import alias from '@rollup/plugin-alias';
import replace from '@rollup/plugin-replace';
import pkgs from './package.json' with {type: 'json'};
const isDev = process.env.NODE_ENV === 'development';
const input = isDev ? './src/dev.ts' : './src/main.ts';
/**
* @type {import('rollup').RollupOptions}
*/
const config = {
input,
output: {
dir: './dist',
entryFileNames: 'app.mjs',
chunkFileNames: '[name]-[hash].mjs',
format: 'esm',
},
plugins: [
replace({
preventAssignment: true, // 防止意外赋值
DEV_SERVER: JSON.stringify(isDev), // 替换 process.env.NODE_ENV
APP_VERSION: JSON.stringify(pkgs.version),
}),
alias({
// only esbuild needs to be configured
entries: [
{ find: '@', replacement: path.resolve('src') }, // 配置 @ 为 src 目录
{ find: 'http', replacement: 'node:http' },
{ find: 'https', replacement: 'node:https' },
{ find: 'fs', replacement: 'node:fs' },
{ find: 'path', replacement: 'node:path' },
{ find: 'crypto', replacement: 'node:crypto' },
{ find: 'zlib', replacement: 'node:zlib' },
{ find: 'stream', replacement: 'node:stream' },
{ find: 'net', replacement: 'node:net' },
{ find: 'tty', replacement: 'node:tty' },
{ find: 'tls', replacement: 'node:tls' },
{ find: 'buffer', replacement: 'node:buffer' },
{ find: 'timers', replacement: 'node:timers' },
// { find: 'string_decoder', replacement: 'node:string_decoder' },
{ find: 'dns', replacement: 'node:dns' },
{ find: 'domain', replacement: 'node:domain' },
{ find: 'os', replacement: 'node:os' },
{ find: 'events', replacement: 'node:events' },
{ find: 'url', replacement: 'node:url' },
{ find: 'assert', replacement: 'node:assert' },
{ find: 'util', replacement: 'node:util' },
],
}),
resolve({
preferBuiltins: true, // 强制优先使用内置模块
}),
commonjs(),
esbuild({
target: 'node22', //
minify: false, // 启用代码压缩
tsconfig: 'tsconfig.json',
}),
json(),
],
external: [
/@kevisual\/router(\/.*)?/, //, // 路由
/@kevisual\/use-config(\/.*)?/, //
'sequelize', // 数据库 orm
'ioredis', // redis
'pg', // pg
],
};
export default config;

View File

@ -1,6 +1,5 @@
import { app } from './index.ts'; import { app } from './index.ts';
import { useConfig } from '@kevisual/use-config/env'; import { config } from './modules/config.ts';
app app
.route({ .route({
path: 'auth', path: 'auth',
@ -38,7 +37,6 @@ app
}) })
.addTo(app); .addTo(app);
const config = useConfig();
const port = config.PORT || 4000; const port = config.PORT || 4000;
console.log('run demo: http://localhost:' + port + '/api/router?path=demo&key=demo'); console.log('run demo: http://localhost:' + port + '/api/router?path=demo&key=demo');

View File

@ -1,27 +1,42 @@
import { Redis } from 'ioredis'; import { Redis } from 'ioredis';
import { config } from './config.ts';
type initRedisOpts = {
onConnect?: () => void; // 连接成功的回调函数
onError?: (msg?: any) => void; // 连接断开的回调函数
};
const initRedis = (config?: any, options?: initRedisOpts) => {
const redis = new Redis({
host: config?.REDIS_HOST || 'localhost', // Redis 服务器的主机名或 IP 地址
port: config?.REDIS_PORT || 6379, // Redis 服务器的端口号
// password: 'your_password', // Redis 的密码 (如果有)
db: 0, // 要使用的 Redis 数据库索引 (0-15)
keyPrefix: '', // key 前缀
retryStrategy(times) {
// 连接重试策略
return Math.min(times * 50, 2000); // 每次重试时延迟增加
},
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
...config, // 其他配置项
});
// 监听连接事件
redis.on('connect', () => {
if (options?.onConnect) {
options.onConnect();
console.log('Redis 连接成功');
}
});
redis.on('error', (err) => {
console.error('Redis 连接错误', err);
if (options?.onError) {
options.onError(err);
}
});
return redis;
};
// 配置 Redis 连接 // 配置 Redis 连接
export const redis = new Redis({ export const redis = useConfigKey('redis', () => initRedis(config));
host: 'localhost', // Redis 服务器的主机名或 IP 地址
port: 6379, // Redis 服务器的端口号
// password: 'your_password', // Redis 的密码 (如果有)
db: 0, // 要使用的 Redis 数据库索引 (0-15)
keyPrefix: '', // key 前缀
retryStrategy(times) {
// 连接重试策略
return Math.min(times * 50, 2000); // 每次重试时延迟增加
},
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
});
// 监听连接事件
redis.on('connect', () => {
console.log('Redis 连接成功');
});
redis.on('error', (err) => {
console.error('Redis 连接错误', err);
});
// 初始化 Redis 客户端 // 初始化 Redis 客户端
export const redisPublisher = new Redis(); // 用于发布消息 export const redisPublisher = new Redis(); // 用于发布消息

View File

@ -1,5 +1,6 @@
import { Sequelize } from 'sequelize'; import { Sequelize } from 'sequelize';
import { config } from './config'; import { config } from './config.ts';
import { useContextKey } from '@kevisual/use-config/context';
export type PostgresConfig = { export type PostgresConfig = {
postgres: { postgres: {
@ -21,9 +22,13 @@ const postgresConfig = {
port: parseInt(config.POSTGRES_PORT || '5432'), port: parseInt(config.POSTGRES_PORT || '5432'),
database: config.POSTGRES_DB || 'postgres', database: config.POSTGRES_DB || 'postgres',
}; };
// connect to db
export const sequelize = new Sequelize({ export const init = async () => {
dialect: 'postgres', return new Sequelize({
...postgresConfig, dialect: 'postgres',
// logging: false, ...postgresConfig,
}); // logging: false,
});
};
export const sequelize = useContextKey('sequelize', () => init());