@thinkbun/plugin 1.0.6 → 1.0.7
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 +126 -126
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,240 +1,240 @@
|
|
|
1
1
|
# ThinkBun Plugin
|
|
2
2
|
|
|
3
|
-
ThinkBun
|
|
3
|
+
The official plugin collection for ThinkBun framework, providing common functional extensions including enhanced logging and server cluster support.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Subproject Overview
|
|
6
6
|
|
|
7
|
-
Plugin
|
|
7
|
+
The Plugin package provides official plugin implementations for the ThinkBun framework. These plugins can be directly integrated into ThinkBun applications, providing additional functionality. The current version includes a logger plugin and a server plugin, with more official plugins to be added in the future.
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## Relationship with Main Project
|
|
10
10
|
|
|
11
|
-
Plugin
|
|
11
|
+
The Plugin package depends on the ThinkBun Core package, providing extension functionality for it. These plugins follow the plugin interface and lifecycle defined by the Core package, seamlessly integrating with the framework.
|
|
12
12
|
|
|
13
|
-
##
|
|
13
|
+
## Core Plugins
|
|
14
14
|
|
|
15
15
|
### LoggerPlugin
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
Enhances application logging functionality with support for colored output, scope identifiers, and different log levels.
|
|
18
18
|
|
|
19
|
-
####
|
|
19
|
+
#### Features
|
|
20
20
|
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
21
|
+
- Colored log output for improved readability
|
|
22
|
+
- Support for log scope identifiers
|
|
23
|
+
- Custom log levels
|
|
24
|
+
- Seamless integration with framework logging system
|
|
25
25
|
|
|
26
|
-
####
|
|
26
|
+
#### Installation
|
|
27
27
|
|
|
28
28
|
```bash
|
|
29
|
-
#
|
|
29
|
+
# Using Bun
|
|
30
30
|
bun add @thinkbun/plugin
|
|
31
31
|
|
|
32
|
-
#
|
|
32
|
+
# Using npm
|
|
33
33
|
npm install @thinkbun/plugin
|
|
34
34
|
|
|
35
|
-
#
|
|
35
|
+
# Using yarn
|
|
36
36
|
yarn add @thinkbun/plugin
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
####
|
|
39
|
+
#### Usage
|
|
40
40
|
|
|
41
41
|
```typescript
|
|
42
42
|
import { createApplication } from '@thinkbun/core';
|
|
43
43
|
import { LoggerPlugin } from '@thinkbun/plugin';
|
|
44
44
|
|
|
45
|
-
//
|
|
45
|
+
// Create application instance
|
|
46
46
|
const app = await createApplication({
|
|
47
47
|
APP_PATH: './src',
|
|
48
48
|
ROOT_PATH: __dirname,
|
|
49
49
|
});
|
|
50
50
|
|
|
51
|
-
//
|
|
51
|
+
// Register LoggerPlugin
|
|
52
52
|
app.loader.plugins.push(LoggerPlugin);
|
|
53
53
|
|
|
54
|
-
//
|
|
54
|
+
// Setup application
|
|
55
55
|
await app.setup();
|
|
56
56
|
|
|
57
|
-
//
|
|
58
|
-
app.logger.info('
|
|
59
|
-
app.logger.error('
|
|
60
|
-
app.logger.debug('
|
|
61
|
-
app.logger.warn('
|
|
57
|
+
// Use enhanced logging
|
|
58
|
+
app.logger.info('Application started');
|
|
59
|
+
app.logger.error('An error occurred', 'ErrorScope');
|
|
60
|
+
app.logger.debug('Debug information', ['Debug', 'Detail']);
|
|
61
|
+
app.logger.warn('Warning message');
|
|
62
62
|
```
|
|
63
63
|
|
|
64
|
-
####
|
|
64
|
+
#### Log Methods
|
|
65
65
|
|
|
66
|
-
- `app.logger.info(message, scope)`:
|
|
67
|
-
- `app.logger.error(message, scope)`:
|
|
68
|
-
- `app.logger.debug(message, scope)`:
|
|
69
|
-
- `app.logger.warn(message, scope)`:
|
|
66
|
+
- `app.logger.info(message, scope)`: Output information-level log
|
|
67
|
+
- `app.logger.error(message, scope)`: Output error-level log
|
|
68
|
+
- `app.logger.debug(message, scope)`: Output debug-level log
|
|
69
|
+
- `app.logger.warn(message, scope)`: Output warning-level log
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
**Parameters**:
|
|
72
72
|
|
|
73
|
-
- `message`:
|
|
74
|
-
- `scope`:
|
|
73
|
+
- `message`: Log message content
|
|
74
|
+
- `scope`: Optional, log scope, can be a string or string array
|
|
75
75
|
|
|
76
76
|
### ServerPlugin
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
Provides server startup and cluster support functionality, optimizing application performance and reliability.
|
|
79
79
|
|
|
80
|
-
####
|
|
80
|
+
#### Features
|
|
81
81
|
|
|
82
|
-
-
|
|
83
|
-
-
|
|
84
|
-
-
|
|
85
|
-
-
|
|
86
|
-
-
|
|
82
|
+
- High-performance server based on Bun.serve
|
|
83
|
+
- Automatic cluster management, fully utilizing multi-core CPUs
|
|
84
|
+
- Automatic worker process restart
|
|
85
|
+
- Graceful shutdown process
|
|
86
|
+
- Configurable server parameters
|
|
87
87
|
|
|
88
|
-
####
|
|
88
|
+
#### Installation
|
|
89
89
|
|
|
90
90
|
```bash
|
|
91
|
-
#
|
|
91
|
+
# Using Bun
|
|
92
92
|
bun add @thinkbun/plugin
|
|
93
93
|
|
|
94
|
-
#
|
|
94
|
+
# Using npm
|
|
95
95
|
npm install @thinkbun/plugin
|
|
96
96
|
|
|
97
|
-
#
|
|
97
|
+
# Using yarn
|
|
98
98
|
yarn add @thinkbun/plugin
|
|
99
99
|
```
|
|
100
100
|
|
|
101
|
-
####
|
|
101
|
+
#### Usage
|
|
102
102
|
|
|
103
103
|
```typescript
|
|
104
104
|
import { createApplication } from '@thinkbun/core';
|
|
105
105
|
import { ServerPlugin } from '@thinkbun/plugin';
|
|
106
106
|
|
|
107
|
-
//
|
|
107
|
+
// Create application instance
|
|
108
108
|
const app = await createApplication({
|
|
109
109
|
APP_PATH: './src',
|
|
110
110
|
ROOT_PATH: __dirname,
|
|
111
111
|
});
|
|
112
112
|
|
|
113
|
-
//
|
|
113
|
+
// Register ServerPlugin
|
|
114
114
|
app.loader.plugins.push(ServerPlugin);
|
|
115
115
|
|
|
116
|
-
//
|
|
116
|
+
// Setup application
|
|
117
117
|
await app.setup();
|
|
118
118
|
|
|
119
|
-
//
|
|
119
|
+
// Run application (ServerPlugin will automatically start the server)
|
|
120
120
|
await app.run();
|
|
121
121
|
```
|
|
122
122
|
|
|
123
|
-
####
|
|
123
|
+
#### Configuration Options
|
|
124
124
|
|
|
125
|
-
|
|
125
|
+
Configure server parameters in the application configuration file:
|
|
126
126
|
|
|
127
127
|
```typescript
|
|
128
128
|
// src/config/index.ts
|
|
129
129
|
export default {
|
|
130
|
-
port: 3000, //
|
|
131
|
-
host: '0.0.0.0', //
|
|
132
|
-
workers: 4, //
|
|
133
|
-
//
|
|
130
|
+
port: 3000, // Server port
|
|
131
|
+
host: '0.0.0.0', // Server host
|
|
132
|
+
workers: 4, // Number of worker processes, defaults to auto-detecting CPU cores
|
|
133
|
+
// Other configurations...
|
|
134
134
|
};
|
|
135
135
|
```
|
|
136
136
|
|
|
137
|
-
####
|
|
137
|
+
#### Cluster Mode
|
|
138
138
|
|
|
139
|
-
ServerPlugin
|
|
139
|
+
ServerPlugin automatically supports cluster mode:
|
|
140
140
|
|
|
141
|
-
-
|
|
142
|
-
-
|
|
143
|
-
-
|
|
144
|
-
-
|
|
141
|
+
- Master process manages worker process creation and restarts
|
|
142
|
+
- Worker processes handle actual HTTP requests
|
|
143
|
+
- When a worker process exits unexpectedly, a new worker process is automatically created
|
|
144
|
+
- Supports graceful shutdown, ensuring requests are completed before exiting
|
|
145
145
|
|
|
146
|
-
##
|
|
146
|
+
## Plugin Lifecycle
|
|
147
147
|
|
|
148
|
-
### LoggerPlugin
|
|
148
|
+
### LoggerPlugin Lifecycle
|
|
149
149
|
|
|
150
|
-
|
|
|
151
|
-
|
|
|
152
|
-
| `setup` | `pre`
|
|
153
|
-
| `run` | -
|
|
154
|
-
| `close` | -
|
|
150
|
+
| Phase | Execution Order | Functionality |
|
|
151
|
+
| ------ | --------------- | -------------------------------------- |
|
|
152
|
+
| `setup` | `pre` | Initialize logging, replace default logger |
|
|
153
|
+
| `run` | - | None |
|
|
154
|
+
| `close` | - | None |
|
|
155
155
|
|
|
156
|
-
### ServerPlugin
|
|
156
|
+
### ServerPlugin Lifecycle
|
|
157
157
|
|
|
158
|
-
|
|
|
159
|
-
|
|
|
160
|
-
| `setup` | -
|
|
161
|
-
| `run` | `post`
|
|
162
|
-
| `close` | -
|
|
158
|
+
| Phase | Execution Order | Functionality |
|
|
159
|
+
| ------ | --------------- | ---------------------------------------- |
|
|
160
|
+
| `setup` | - | None |
|
|
161
|
+
| `run` | `post` | Start server, create worker processes |
|
|
162
|
+
| `close` | - | Gracefully shutdown server and workers |
|
|
163
163
|
|
|
164
|
-
##
|
|
164
|
+
## Standalone Running/Testing
|
|
165
165
|
|
|
166
|
-
###
|
|
166
|
+
### Running Examples
|
|
167
167
|
|
|
168
168
|
```bash
|
|
169
|
-
#
|
|
169
|
+
# Install dependencies
|
|
170
170
|
bun install
|
|
171
171
|
|
|
172
|
-
#
|
|
172
|
+
# Run tests
|
|
173
173
|
bun test
|
|
174
174
|
```
|
|
175
175
|
|
|
176
|
-
###
|
|
176
|
+
### Testing
|
|
177
177
|
|
|
178
178
|
```bash
|
|
179
|
-
#
|
|
179
|
+
# Run unit tests
|
|
180
180
|
bun test
|
|
181
181
|
|
|
182
|
-
#
|
|
182
|
+
# Run tests and generate coverage report
|
|
183
183
|
bun test --coverage
|
|
184
184
|
```
|
|
185
185
|
|
|
186
|
-
##
|
|
186
|
+
## Custom Plugin Development
|
|
187
187
|
|
|
188
|
-
Plugin
|
|
188
|
+
The Plugin package not only provides official plugins but also demonstrates how to develop custom plugins. Here's an example of developing a custom plugin:
|
|
189
189
|
|
|
190
190
|
```typescript
|
|
191
191
|
import { type Plugin } from '@thinkbun/core';
|
|
192
192
|
|
|
193
193
|
export const CustomPlugin: Plugin = {
|
|
194
194
|
name: 'CustomPlugin',
|
|
195
|
-
enforce: 'pre', //
|
|
195
|
+
enforce: 'pre', // Plugin execution order, can be 'pre', 'post', or omitted
|
|
196
196
|
setup(app) {
|
|
197
|
-
//
|
|
198
|
-
console.log('CustomPlugin
|
|
197
|
+
// Plugin initialization logic
|
|
198
|
+
console.log('CustomPlugin initialized');
|
|
199
199
|
},
|
|
200
200
|
run(app) {
|
|
201
|
-
//
|
|
202
|
-
console.log('CustomPlugin
|
|
201
|
+
// Application runtime logic
|
|
202
|
+
console.log('CustomPlugin running');
|
|
203
203
|
},
|
|
204
204
|
close(app) {
|
|
205
|
-
//
|
|
206
|
-
console.log('CustomPlugin
|
|
205
|
+
// Cleanup logic when application closes
|
|
206
|
+
console.log('CustomPlugin closed');
|
|
207
207
|
},
|
|
208
208
|
};
|
|
209
209
|
```
|
|
210
210
|
|
|
211
|
-
##
|
|
211
|
+
## Plugin Execution Order
|
|
212
212
|
|
|
213
|
-
|
|
213
|
+
Plugin execution order is determined by the `enforce` property:
|
|
214
214
|
|
|
215
|
-
- `pre`:
|
|
216
|
-
- `post`:
|
|
217
|
-
-
|
|
215
|
+
- `pre`: Execute before core functionality
|
|
216
|
+
- `post`: Execute after core functionality
|
|
217
|
+
- Omitted: Default execution order
|
|
218
218
|
|
|
219
|
-
##
|
|
219
|
+
## Best Practices
|
|
220
220
|
|
|
221
|
-
1.
|
|
222
|
-
-
|
|
223
|
-
-
|
|
221
|
+
1. **Plugin Registration**:
|
|
222
|
+
- Place plugin registration in the `plugins.ts` file for centralized management
|
|
223
|
+
- Register plugins in dependency order and execution order
|
|
224
224
|
|
|
225
|
-
2.
|
|
226
|
-
-
|
|
227
|
-
-
|
|
225
|
+
2. **Plugin Configuration**:
|
|
226
|
+
- Place plugin configuration in the application configuration file
|
|
227
|
+
- Support environment-specific configuration
|
|
228
228
|
|
|
229
|
-
3.
|
|
230
|
-
-
|
|
231
|
-
-
|
|
229
|
+
3. **Error Handling**:
|
|
230
|
+
- Add appropriate error handling in plugins
|
|
231
|
+
- Provide clear error messages
|
|
232
232
|
|
|
233
|
-
4.
|
|
234
|
-
-
|
|
235
|
-
-
|
|
233
|
+
4. **Performance Considerations**:
|
|
234
|
+
- Avoid executing time-consuming operations in plugins
|
|
235
|
+
- Use plugin lifecycle stages appropriately
|
|
236
236
|
|
|
237
|
-
## API
|
|
237
|
+
## API Reference
|
|
238
238
|
|
|
239
239
|
### LoggerPlugin
|
|
240
240
|
|
|
@@ -257,54 +257,54 @@ interface Plugin {
|
|
|
257
257
|
}
|
|
258
258
|
```
|
|
259
259
|
|
|
260
|
-
##
|
|
260
|
+
## FAQ
|
|
261
261
|
|
|
262
|
-
### Q:
|
|
262
|
+
### Q: How to customize log colors?
|
|
263
263
|
|
|
264
|
-
A:
|
|
264
|
+
A: Currently LoggerPlugin supports predefined colors. You can customize by modifying the source code:
|
|
265
265
|
|
|
266
266
|
```typescript
|
|
267
|
-
//
|
|
267
|
+
// Modify colorMap object in logger.ts
|
|
268
268
|
const colorMap = {
|
|
269
269
|
app: 'yellow',
|
|
270
270
|
PluginManager: 'yellow',
|
|
271
|
-
//
|
|
271
|
+
// Add custom color mappings
|
|
272
272
|
CustomScope: 'cyan',
|
|
273
273
|
};
|
|
274
274
|
```
|
|
275
275
|
|
|
276
|
-
### Q:
|
|
276
|
+
### Q: How to configure server port?
|
|
277
277
|
|
|
278
|
-
A:
|
|
278
|
+
A: Set the `port` option in the application configuration file:
|
|
279
279
|
|
|
280
280
|
```typescript
|
|
281
281
|
// src/config/index.ts
|
|
282
282
|
export default {
|
|
283
|
-
port: 8080, //
|
|
283
|
+
port: 8080, // Set to a different port
|
|
284
284
|
};
|
|
285
285
|
```
|
|
286
286
|
|
|
287
|
-
### Q:
|
|
287
|
+
### Q: How to disable cluster mode?
|
|
288
288
|
|
|
289
|
-
A:
|
|
289
|
+
A: Set `workers` to 1 in the application configuration file:
|
|
290
290
|
|
|
291
291
|
```typescript
|
|
292
292
|
// src/config/index.ts
|
|
293
293
|
export default {
|
|
294
|
-
workers: 1, //
|
|
294
|
+
workers: 1, // Disable cluster mode, use only one process
|
|
295
295
|
};
|
|
296
296
|
```
|
|
297
297
|
|
|
298
|
-
##
|
|
298
|
+
## License
|
|
299
299
|
|
|
300
300
|
[MIT License](LICENSE)
|
|
301
301
|
|
|
302
|
-
##
|
|
302
|
+
## Contact
|
|
303
303
|
|
|
304
|
-
- 📧
|
|
305
|
-
- 📦 GitHub
|
|
306
|
-
- 💬 Discord
|
|
304
|
+
- 📧 Email: contact@thinkbun.dev
|
|
305
|
+
- 📦 GitHub: [https://github.com/thinkbun/thinkbun](https://github.com/thinkbun/thinkbun)
|
|
306
|
+
- 💬 Discord: [ThinkBun Community](https://discord.gg/thinkbun)
|
|
307
307
|
|
|
308
308
|
---
|
|
309
309
|
|
|
310
|
-
**ThinkBun Plugin** -
|
|
310
|
+
**ThinkBun Plugin** - Providing powerful extension functionality for ThinkBun framework 🧩
|