@fastcar/cli 0.0.7 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +259 -21
- package/bin/cli.js +202 -32
- package/package.json +3 -3
- package/skills/fastcar-database/SKILL.md +374 -0
- package/skills/fastcar-framework/SKILL.md +661 -0
- package/skills/fastcar-framework/assets/project-template/package.json +24 -0
- package/skills/fastcar-framework/assets/project-template/resource/application.yml +14 -0
- package/skills/fastcar-framework/assets/project-template/src/app.ts +18 -0
- package/skills/fastcar-framework/assets/project-template/src/component/StartupRunner.ts +15 -0
- package/skills/fastcar-framework/assets/project-template/src/controller/HelloController.ts +21 -0
- package/skills/fastcar-framework/assets/project-template/src/service/HelloService.ts +12 -0
- package/skills/fastcar-framework/assets/project-template/tsconfig.json +21 -0
- package/skills/fastcar-framework/references/api-reference.md +353 -0
- package/skills/fastcar-rpc-microservices/SKILL.md +311 -0
- package/skills/fastcar-serverless/SKILL.md +211 -0
- package/skills/fastcar-toolkit/SKILL.md +282 -0
- package/src/init.js +615 -201
- package/src/pack.js +204 -0
- package/src/reverse.js +204 -0
- package/src/skill-targets.js +74 -0
- package/src/skill.js +362 -0
- package/src/templates.json +32 -0
- package/src/utils.js +51 -66
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: fastcar-toolkit
|
|
3
|
+
description: FastCar 工具集开发指南。Use when working with FastCar framework for: (1) In-memory caching with @fastcar/cache, (2) Scheduled tasks and cron jobs with @fastcar/timer, (3) Delayed tasks with HashedWheelTimer from @fastcar/timewheel, (4) CPU-intensive operations with @fastcar/workerpool, (5) File watching with @fastcar/watchfile, (6) Object storage operations with @fastcar/cossdk.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# FastCar Toolkit
|
|
7
|
+
|
|
8
|
+
FastCar Toolkit 汇总了框架提供的各类工具模块,包括缓存、定时任务、时间轮、工作线程池、文件监听和对象存储 SDK。
|
|
9
|
+
|
|
10
|
+
## 缓存 (@fastcar/cache)
|
|
11
|
+
|
|
12
|
+
### 开启缓存
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { Application } from "@fastcar/core/annotation";
|
|
16
|
+
import { EnableCache } from "@fastcar/cache";
|
|
17
|
+
|
|
18
|
+
@Application
|
|
19
|
+
@EnableCache
|
|
20
|
+
class APP {}
|
|
21
|
+
export default new APP();
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### 使用 CacheApplication
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { Service, Autowired } from "@fastcar/core/annotation";
|
|
28
|
+
import { CacheApplication } from "@fastcar/cache";
|
|
29
|
+
|
|
30
|
+
@Service
|
|
31
|
+
class UserCacheService {
|
|
32
|
+
@Autowired
|
|
33
|
+
private cache!: CacheApplication;
|
|
34
|
+
|
|
35
|
+
setUser(id: string, user: any) {
|
|
36
|
+
// ttl 单位秒,0 为不过期
|
|
37
|
+
this.cache.set("userStore", id, user, { ttl: 60 });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getUser(id: string) {
|
|
41
|
+
return this.cache.get("userStore", id);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
hasUser(id: string) {
|
|
45
|
+
return this.cache.has("userStore", id);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
deleteUser(id: string) {
|
|
49
|
+
return this.cache.delete("userStore", id);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
getTTL(id: string) {
|
|
53
|
+
return this.cache.getTTL("userStore", id);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
getAllUsers() {
|
|
57
|
+
return this.cache.getDictionary("userStore");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 持久化缓存配置
|
|
63
|
+
|
|
64
|
+
通过 `@CacheMapping` 配置缓存节点,支持文件持久化或数据库持久化:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { CacheMapping } from "@fastcar/cache";
|
|
68
|
+
import { FSClient } from "@fastcar/cache";
|
|
69
|
+
|
|
70
|
+
@CacheMapping([
|
|
71
|
+
{
|
|
72
|
+
store: "fileStore",
|
|
73
|
+
dbClient: new FSClient("./cache-data"),
|
|
74
|
+
syncTimer: 5, // 每 5 秒同步一次
|
|
75
|
+
ttl: 0,
|
|
76
|
+
},
|
|
77
|
+
])
|
|
78
|
+
class CacheConfig {}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## 定时任务 (@fastcar/timer)
|
|
82
|
+
|
|
83
|
+
### 开启定时任务
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { Application } from "@fastcar/core/annotation";
|
|
87
|
+
import { EnableScheduling } from "@fastcar/timer";
|
|
88
|
+
|
|
89
|
+
@Application
|
|
90
|
+
@EnableScheduling
|
|
91
|
+
class APP {}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 间隔任务
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { ScheduledInterval } from "@fastcar/timer";
|
|
98
|
+
import { Component } from "@fastcar/core/annotation";
|
|
99
|
+
|
|
100
|
+
@Component
|
|
101
|
+
class HeartbeatTask {
|
|
102
|
+
@ScheduledInterval({ fixedRate: 5000 })
|
|
103
|
+
async beat() {
|
|
104
|
+
console.log("心跳检测", new Date().toISOString());
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Cron 任务
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { ScheduledCron } from "@fastcar/timer";
|
|
113
|
+
import { Component } from "@fastcar/core/annotation";
|
|
114
|
+
|
|
115
|
+
@Component
|
|
116
|
+
class ReportTask {
|
|
117
|
+
@ScheduledCron("0 0 * * * *")
|
|
118
|
+
async hourly() {
|
|
119
|
+
console.log("每小时执行一次报表任务");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## 时间轮 (@fastcar/timewheel)
|
|
125
|
+
|
|
126
|
+
适用于需要大量延时任务的场景(如订单超时取消、消息延时投递)。
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { HashedWheelTimer } from "@fastcar/timewheel";
|
|
130
|
+
|
|
131
|
+
const timer = new HashedWheelTimer<string>({
|
|
132
|
+
tickDuration: 100, // 每个槽位 100ms
|
|
133
|
+
wheelSize: 512, // 时间轮大小 512
|
|
134
|
+
slotMaxSize: 100, // 每次 tick 最大处理数量
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// 添加一个 5 秒后触发的任务
|
|
138
|
+
timer.addId("order-123", 5000);
|
|
139
|
+
|
|
140
|
+
// 配合心跳循环处理
|
|
141
|
+
setInterval(() => {
|
|
142
|
+
const ids = timer.tick();
|
|
143
|
+
if (ids) {
|
|
144
|
+
ids.forEach(id => {
|
|
145
|
+
console.log("任务到期", id);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}, 100);
|
|
149
|
+
|
|
150
|
+
// 取消任务
|
|
151
|
+
timer.removeId("order-123", slotId);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## 工作线程池 (@fastcar/workerpool)
|
|
155
|
+
|
|
156
|
+
将 CPU 密集型操作卸载到 worker 线程执行,避免阻塞主线程。
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { WorkerPool, TaskType, TaskSyncType } from "@fastcar/workerpool";
|
|
160
|
+
|
|
161
|
+
const pool = new WorkerPool({
|
|
162
|
+
minWorkers: 2,
|
|
163
|
+
maxWorkers: 4,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// 执行同步任务
|
|
167
|
+
const result = await pool.exec("heavyComputation", [1, 2, 3, 4, 5]);
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
在 FastCar 应用中通常通过 `@fastcar/core` 的 `@WorkerPool` / `@WorkerTask` 注解使用(参考 fastcar-framework skill)。
|
|
171
|
+
|
|
172
|
+
## 文件监听 (@fastcar/watchfile)
|
|
173
|
+
|
|
174
|
+
动态监听文件或目录变更。
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import { Watch, WatchSingleton } from "@fastcar/watchfile";
|
|
178
|
+
|
|
179
|
+
const watcher = Watch({
|
|
180
|
+
pollInterval: 1000, // 轮询间隔 1 秒
|
|
181
|
+
notifyTime: 300, // 通知防抖 300ms
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// 或使用单例
|
|
185
|
+
const singleWatcher = WatchSingleton({
|
|
186
|
+
pollInterval: 1000,
|
|
187
|
+
notifyTime: 300,
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## COS SDK (@fastcar/cossdk)
|
|
192
|
+
|
|
193
|
+
对象存储客户端,支持文件上传、下载、权限管理、重定向等。
|
|
194
|
+
|
|
195
|
+
### 初始化
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
import { COSSDK, getSign } from "@fastcar/cossdk";
|
|
199
|
+
|
|
200
|
+
const account = {
|
|
201
|
+
appid: "your-appid",
|
|
202
|
+
serectkey: "your-secret",
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const sign = getSign(
|
|
206
|
+
{
|
|
207
|
+
appid: account.appid,
|
|
208
|
+
expireTime: Math.floor((Date.now() + 5 * 60 * 1000) / 1000),
|
|
209
|
+
dir_path: "/", // 授权路径
|
|
210
|
+
mode: 7, // 1可读 2可写 4可查
|
|
211
|
+
},
|
|
212
|
+
account.serectkey
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
const cos = new COSSDK({
|
|
216
|
+
domain: "http://localhost",
|
|
217
|
+
sign,
|
|
218
|
+
});
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### 常用操作
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
// 生成/初始化账号信息
|
|
225
|
+
await cos.genAccountInfo();
|
|
226
|
+
await cos.initAccount();
|
|
227
|
+
|
|
228
|
+
// 上传文件
|
|
229
|
+
const blob = new Blob(["hello world"], { type: "text/plain" });
|
|
230
|
+
const file = new File([blob], "client.txt");
|
|
231
|
+
await cos.uploadfile("/test/text.txt", file);
|
|
232
|
+
|
|
233
|
+
// 下载文件
|
|
234
|
+
const res = await cos.getFile("/test/hello/test.txt");
|
|
235
|
+
console.log(res.data);
|
|
236
|
+
|
|
237
|
+
// 带鉴权下载
|
|
238
|
+
await cos.getFile("/test.txt", true);
|
|
239
|
+
|
|
240
|
+
// 删除文件
|
|
241
|
+
await cos.deleteFile("/hello.txt");
|
|
242
|
+
|
|
243
|
+
// 查询文件列表
|
|
244
|
+
const list = await cos.queryFilelist("/test");
|
|
245
|
+
|
|
246
|
+
// 创建文件夹
|
|
247
|
+
await cos.createDir("/new-folder", "public");
|
|
248
|
+
|
|
249
|
+
// 设置权限
|
|
250
|
+
await cos.setPermissions({ filename: "/test/b.txt", permission: "public" });
|
|
251
|
+
await cos.getPermissions({ filename: "/test/b.txt" });
|
|
252
|
+
await cos.delPermissions({ filename: "/test/b.txt" });
|
|
253
|
+
|
|
254
|
+
// 重命名
|
|
255
|
+
await cos.rename("/test/old.txt", "/test/new.txt");
|
|
256
|
+
|
|
257
|
+
// 设置重定向
|
|
258
|
+
await cos.setRedirect({ redirectUrl: "/test/hello.txt", flag: false, bucket: "test" });
|
|
259
|
+
|
|
260
|
+
// 查询重定向
|
|
261
|
+
await cos.getRedirect();
|
|
262
|
+
await cos.queryRedirect({ bucketUrl: "http://xxx" });
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## 完整模块列表
|
|
266
|
+
|
|
267
|
+
| 模块 | 安装命令 | 用途 |
|
|
268
|
+
|------|----------|------|
|
|
269
|
+
| @fastcar/cache | `npm i @fastcar/cache` | 应用内缓存 |
|
|
270
|
+
| @fastcar/timer | `npm i @fastcar/timer` | 定时任务 |
|
|
271
|
+
| @fastcar/timewheel | `npm i @fastcar/timewheel` | 时间轮延时任务 |
|
|
272
|
+
| @fastcar/workerpool | `npm i @fastcar/workerpool` | 工作线程池 |
|
|
273
|
+
| @fastcar/watchfile | `npm i @fastcar/watchfile` | 文件监听 |
|
|
274
|
+
| @fastcar/cossdk | `npm i @fastcar/cossdk` | 对象存储 SDK |
|
|
275
|
+
|
|
276
|
+
## 快速开始
|
|
277
|
+
|
|
278
|
+
```bash
|
|
279
|
+
# 安装所需工具包
|
|
280
|
+
npm i @fastcar/cache @fastcar/timer @fastcar/timewheel @fastcar/workerpool
|
|
281
|
+
npm i @fastcar/watchfile @fastcar/cossdk
|
|
282
|
+
```
|