koatty 3.13.2 → 4.0.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/.package.json.backup +1 -0
- package/.rollup.config.js +89 -82
- package/.turbo/turbo-build.log +86 -0
- package/CHANGELOG.md +1880 -2065
- package/LICENSE +29 -29
- package/README.md +346 -162
- package/dist/LICENSE +29 -29
- package/dist/README.md +346 -162
- package/dist/index.d.ts +58 -5
- package/dist/index.js +252 -155
- package/dist/index.mjs +369 -272
- package/dist/package.json +107 -106
- package/examples/README.md +172 -0
- package/examples/basic-app/README.md +72 -0
- package/examples/basic-app/config/config.ts +142 -0
- package/examples/basic-app/package.json +20 -0
- package/examples/basic-app/pnpm-workspace.yaml +4 -0
- package/examples/basic-app/static/.gitkeep +0 -0
- package/examples/basic-app/static/favicon.ico +0 -0
- package/examples/basic-app/test-simple.ts +13 -0
- package/examples/basic-app/view/index.html +25 -0
- package/package.json +107 -106
- package/rollup.config-1761147987514.cjs +88 -0
- package/tsconfig.test.json +14 -14
- package/.vscode/launch.json +0 -42
package/LICENSE
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
BSD 3-Clause License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020, Koatty
|
|
4
|
-
All rights reserved.
|
|
5
|
-
|
|
6
|
-
Redistribution and use in source and binary forms, with or without
|
|
7
|
-
modification, are permitted provided that the following conditions are met:
|
|
8
|
-
|
|
9
|
-
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
-
list of conditions and the following disclaimer.
|
|
11
|
-
|
|
12
|
-
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
-
this list of conditions and the following disclaimer in the documentation
|
|
14
|
-
and/or other materials provided with the distribution.
|
|
15
|
-
|
|
16
|
-
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
-
contributors may be used to endorse or promote products derived from
|
|
18
|
-
this software without specific prior written permission.
|
|
19
|
-
|
|
20
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020, Koatty
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,162 +1,346 @@
|
|
|
1
|
-
# Koatty 🚀
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/koatty)
|
|
4
|
-
[](https://opensource.org/licenses/BSD-3-Clause)
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
## Why Koatty? 💡
|
|
9
|
-
|
|
10
|
-
- 🚄 **High Performance**: Built on top of
|
|
11
|
-
- 🧩 **Full-Featured**: Supports gRPC, HTTP, WebSocket,
|
|
12
|
-
- 🧠 **TypeScript First**: Native TypeScript support with elegant OOP design
|
|
13
|
-
- 🌀 **Spring-like IOC Container**: Powerful dependency injection system with autowiring
|
|
14
|
-
- ✂️ **AOP Support**: Aspect-oriented programming with decorator-based interceptors
|
|
15
|
-
- 🔌 **Extensible Architecture**: Plugin system with dependency injection
|
|
16
|
-
- 📦 **Modern Tooling**: CLI scaffolding, testing utilities, and production-ready
|
|
17
|
-
- 🌐 **Protocol Agnostic**: Write once, deploy as HTTP/gRPC/WebSocket services
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
...
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
export
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
1
|
+
# Koatty 🚀
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/koatty)
|
|
4
|
+
[](https://opensource.org/licenses/BSD-3-Clause)
|
|
5
|
+
|
|
6
|
+
Koa + TypeScript + IOC = Koatty. **Koatty** is a progressive Node.js framework for building efficient and scalable server-side applications. It's perfect for crafting enterprise-level APIs, microservices, and full-stack applications with TypeScript excellence.
|
|
7
|
+
|
|
8
|
+
## Why Koatty? 💡
|
|
9
|
+
|
|
10
|
+
- 🚄 **High Performance**: Built on top of Koa with optimized architecture
|
|
11
|
+
- 🧩 **Full-Featured**: Supports gRPC, HTTP, WebSocket, scheduled tasks, and more
|
|
12
|
+
- 🧠 **TypeScript First**: Native TypeScript support with elegant OOP design
|
|
13
|
+
- 🌀 **Spring-like IOC Container**: Powerful dependency injection system with autowiring
|
|
14
|
+
- ✂️ **AOP Support**: Aspect-oriented programming with decorator-based interceptors
|
|
15
|
+
- 🔌 **Extensible Architecture**: Plugin system with dependency injection
|
|
16
|
+
- 📦 **Modern Tooling**: CLI scaffolding, testing utilities, and production-ready configurations
|
|
17
|
+
- 🌐 **Protocol Agnostic**: Write once, deploy as HTTP/gRPC/WebSocket services
|
|
18
|
+
|
|
19
|
+
### ✨ New Features
|
|
20
|
+
|
|
21
|
+
- ✅ **Multi-Protocol Architecture** - Run HTTP, HTTPS, HTTP/2, HTTP/3, gRPC, WebSocket, and GraphQL simultaneously
|
|
22
|
+
- ✅ **Intelligent Metadata Cache** - LRU caching with preloading for 70%+ performance boost
|
|
23
|
+
- ✅ **Protocol-Specific Middleware** - Bind middleware to specific protocols with `@Middleware({ protocol: [...] })`
|
|
24
|
+
- ✅ **Graceful Shutdown** - Enhanced connection pool management and cleanup handlers
|
|
25
|
+
- ✅ **Enhanced gRPC Support** - Timeout detection, duplicate call protection, streaming improvements
|
|
26
|
+
- ✅ **Application Lifecycle Hooks** - Custom decorators with `BindEventHook` API for boot/ready/stop events
|
|
27
|
+
- ✅ **Version Conflict Detection** - Automatic detection and resolution of dependency conflicts
|
|
28
|
+
- ✅ **GraphQL over HTTP/2** - Automatic HTTP/2 upgrade with SSL for multiplexing and compression
|
|
29
|
+
- ✅ **Global Exception Handling** - `@ExceptionHandler()` decorator for centralized error management
|
|
30
|
+
- ✅ **OpenTelemetry Tracing** - Full-stack observability with distributed tracing
|
|
31
|
+
- 💪 **Swagger/OpenAPI 3.0** - Automatic API documentation generation
|
|
32
|
+
|
|
33
|
+
## Core Features ✨
|
|
34
|
+
|
|
35
|
+
### 📡 Multi-Protocol Support
|
|
36
|
+
|
|
37
|
+
Koatty now supports running multiple protocols simultaneously on different ports. Configure multiple servers easily:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
// config/config.ts
|
|
41
|
+
export default {
|
|
42
|
+
...
|
|
43
|
+
server: {
|
|
44
|
+
hostname: '127.0.0.1',
|
|
45
|
+
port: 3000,
|
|
46
|
+
protocol: ["http", "grpc"], // Multiple protocols: 'http' | 'https' | 'http2' | 'http3' | 'grpc' | 'ws' | 'wss' | 'graphql'
|
|
47
|
+
trace: false,
|
|
48
|
+
},
|
|
49
|
+
...
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
**Single Protocol (backward compatible):**
|
|
54
|
+
```typescript
|
|
55
|
+
// config/config.ts
|
|
56
|
+
export default {
|
|
57
|
+
server: {
|
|
58
|
+
protocol: "grpc", // Single protocol
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
**Multi-Protocol Router Configuration:**
|
|
64
|
+
|
|
65
|
+
When using multiple protocols, configure protocol-specific extensions in `config/router.ts`:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
// config/router.ts
|
|
69
|
+
export default {
|
|
70
|
+
ext: {
|
|
71
|
+
// HTTP protocol config (optional)
|
|
72
|
+
...,
|
|
73
|
+
|
|
74
|
+
// gRPC protocol config (optional)
|
|
75
|
+
protoFile: "./resource/proto/Hello.proto",
|
|
76
|
+
poolSize: 10,
|
|
77
|
+
streamConfig: { messageCount: 50 }
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
// WebSocket protocol config (optional)
|
|
81
|
+
maxFrameSize: 1024 * 1024,
|
|
82
|
+
heartbeatInterval: 15000,
|
|
83
|
+
maxConnections: 1000
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**How It Works:**
|
|
89
|
+
- `koatty_serve` automatically creates server instances for each protocol
|
|
90
|
+
- `koatty_router` creates dedicated router instances for each protocol
|
|
91
|
+
- Controllers are automatically registered to appropriate routers based on their decorators
|
|
92
|
+
- HTTP controllers (`@Controller`) work with HTTP/HTTPS/HTTP2
|
|
93
|
+
- gRPC controllers (`@GrpcController`) work with gRPC
|
|
94
|
+
- GraphQL controllers (`@GraphQLController`) work with GraphQL (over HTTP/HTTPS)
|
|
95
|
+
- WebSocket controllers (`@WsController`) work with WebSocket
|
|
96
|
+
|
|
97
|
+
**Important Notes:**
|
|
98
|
+
- **GraphQL Protocol**: GraphQL is an application-layer protocol that runs over HTTP/HTTP2, not a separate transport protocol. When you specify `protocol: "graphql"`, Koatty automatically:
|
|
99
|
+
- Uses **HTTP** as transport by default
|
|
100
|
+
- Uses **HTTP/2** when SSL certificates are configured (recommended for production)
|
|
101
|
+
|
|
102
|
+
- **GraphQL over HTTP/2** (Recommended): HTTP/2 provides significant benefits for GraphQL:
|
|
103
|
+
- **Multiplexing**: Handle multiple queries over a single connection
|
|
104
|
+
- **Header Compression**: Reduce bandwidth for large queries
|
|
105
|
+
- **Server Push**: Prefetch related resources
|
|
106
|
+
- **HTTP/1.1 Fallback**: Automatic downgrade for compatibility
|
|
107
|
+
|
|
108
|
+
To enable HTTP/2 for GraphQL, configure in `config/config.ts`:
|
|
109
|
+
```typescript
|
|
110
|
+
// config/config.ts
|
|
111
|
+
export default {
|
|
112
|
+
server: {
|
|
113
|
+
protocol: "graphql",
|
|
114
|
+
ssl: {
|
|
115
|
+
mode: 'auto',
|
|
116
|
+
key: './ssl/server.key',
|
|
117
|
+
cert: './ssl/server.crt'
|
|
118
|
+
},
|
|
119
|
+
ext: {
|
|
120
|
+
maxConcurrentStreams: 100 // Optional: HTTP/2 config
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
And configure GraphQL schema in `config/router.ts`:
|
|
127
|
+
```typescript
|
|
128
|
+
// config/router.ts
|
|
129
|
+
export default {
|
|
130
|
+
ext: {
|
|
131
|
+
schemaFile: "./resource/graphql/schema.graphql"
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
### 💉 Dependency Injection
|
|
138
|
+
|
|
139
|
+
**Enhanced Features:**
|
|
140
|
+
-✅ **Intelligent Metadata Cache** - LRU caching mechanism, significantly improves performance
|
|
141
|
+
-✅ **Metadata Preloading** - Preload at startup, optimize component registration
|
|
142
|
+
-✅ **Version Conflict Detection** - Automatically detect and resolve dependency version conflicts
|
|
143
|
+
-✅ **Circular Dependency Detection** - Circular dependency detection and resolution suggestions
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
@Service()
|
|
147
|
+
export class UserService {
|
|
148
|
+
async findUser(id: number) {
|
|
149
|
+
return { id, name: 'Koatty User' };
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
@Controller()
|
|
154
|
+
export class IndexController {
|
|
155
|
+
app: App;
|
|
156
|
+
ctx: KoattyContext;
|
|
157
|
+
@Config("server")
|
|
158
|
+
conf: { protocol: string | string[] };
|
|
159
|
+
...
|
|
160
|
+
|
|
161
|
+
@Autowired()
|
|
162
|
+
private userService: UserService;
|
|
163
|
+
|
|
164
|
+
async test(id: number) {
|
|
165
|
+
const info = await this.userService.findUser(id);
|
|
166
|
+
...
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**Performance Improvements:**
|
|
172
|
+
```typescript
|
|
173
|
+
// In Loader.ts - Metadata is now preloaded for optimal performance
|
|
174
|
+
IOC.preloadMetadata(); // Preload all metadata to populate cache
|
|
175
|
+
|
|
176
|
+
// Intelligent caching reduces reflect operations by 70%+
|
|
177
|
+
// Cache hits: ~95% in typical applications
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### 🌐 Multi-Protocol Controllers
|
|
181
|
+
|
|
182
|
+
Different controllers for different protocols:
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
// HTTP Controller
|
|
186
|
+
@Controller('/api')
|
|
187
|
+
export class UserController {
|
|
188
|
+
@GetMapping('/users/:id')
|
|
189
|
+
async getUser(@PathVariable('id') id: string) {
|
|
190
|
+
return { id, name: 'User' };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// gRPC Controller
|
|
195
|
+
@GrpcController('/Hello')
|
|
196
|
+
export class HelloController {
|
|
197
|
+
@PostMapping('/SayHello')
|
|
198
|
+
@Validated()
|
|
199
|
+
async sayHello(@RequestBody() params: SayHelloRequestDto): Promise<SayHelloReplyDto> {
|
|
200
|
+
const res = new SayHelloReplyDto();
|
|
201
|
+
res.message = `Hello, ${params.name}!`;
|
|
202
|
+
return res;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// GraphQL Controller (runs over HTTP/HTTPS)
|
|
207
|
+
@GraphQLController('/graphql')
|
|
208
|
+
export class UserController {
|
|
209
|
+
@GetMapping()
|
|
210
|
+
async getUser(@RequestParam() id: string): Promise<User> {
|
|
211
|
+
return { id, name: 'GraphQL User' };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
@PostMapping()
|
|
215
|
+
async createUser(@RequestParam() input: UserInput): Promise<User> {
|
|
216
|
+
return { id: input.id, name: input.name };
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### ✂️ Aspect-Oriented Programming
|
|
222
|
+
```typescript
|
|
223
|
+
@Aspect()
|
|
224
|
+
export class LogAspect implements IAspect {
|
|
225
|
+
app: App;
|
|
226
|
+
|
|
227
|
+
run() {
|
|
228
|
+
console.log('LogAspect');
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Apply aspect to controller
|
|
233
|
+
@Controller()
|
|
234
|
+
@BeforeEach(LogAspect)
|
|
235
|
+
export class UserController {
|
|
236
|
+
...
|
|
237
|
+
@After(LogAspect)
|
|
238
|
+
test() {
|
|
239
|
+
...
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### 🔌 Plugin System & Middleware
|
|
245
|
+
|
|
246
|
+
**Protocol-Specific Middleware:**
|
|
247
|
+
```typescript
|
|
248
|
+
// Middleware can now be bound to specific protocols
|
|
249
|
+
@Middleware({ protocol: ["http", "https"] })
|
|
250
|
+
export class HttpOnlyMiddleware implements IMiddleware {
|
|
251
|
+
run(options: any, app: App) {
|
|
252
|
+
return async (ctx: KoattyContext, next: Function) => {
|
|
253
|
+
// This middleware only runs for HTTP/HTTPS protocols
|
|
254
|
+
console.log('HTTP request:', ctx.url);
|
|
255
|
+
await next();
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
**Plugin System:**
|
|
262
|
+
```typescript
|
|
263
|
+
// plugin/logger.ts
|
|
264
|
+
export class LoggerPlugin implements IPlugin {
|
|
265
|
+
app: App;
|
|
266
|
+
|
|
267
|
+
run() {
|
|
268
|
+
// Hook into application lifecycle events
|
|
269
|
+
Logger.Debug("LoggerPlugin");
|
|
270
|
+
return Promise.resolve();
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
**Application Lifecycle Events:**
|
|
276
|
+
```typescript
|
|
277
|
+
// Use BindEventHook to customize application behavior
|
|
278
|
+
export function CustomDecorator(): ClassDecorator {
|
|
279
|
+
return (target: Function) => {
|
|
280
|
+
BindEventHook(AppEvent.appBoot, async (app: KoattyApplication) => {
|
|
281
|
+
// Executed during application boot
|
|
282
|
+
console.log('App is booting...');
|
|
283
|
+
}, target);
|
|
284
|
+
|
|
285
|
+
BindEventHook(AppEvent.appReady, async (app: KoattyApplication) => {
|
|
286
|
+
// Executed when app is ready
|
|
287
|
+
console.log('App is ready!');
|
|
288
|
+
}, target);
|
|
289
|
+
|
|
290
|
+
BindEventHook(AppEvent.appStop, async (app: KoattyApplication) => {
|
|
291
|
+
// Executed during graceful shutdown
|
|
292
|
+
console.log('App is stopping...');
|
|
293
|
+
}, target);
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## Benchmarks 📊
|
|
299
|
+
|
|
300
|
+
| Framework | Requests/sec | Latency | Memory Usage |
|
|
301
|
+
| ---------- | ------------ | ------- | ------------ |
|
|
302
|
+
| **Koatty** | 13,321 | 1.43ms | 54MB |
|
|
303
|
+
| Express | 12,456 | 1.45ms | 52MB |
|
|
304
|
+
| NestJS | 11,892 | 1.51ms | 63MB |
|
|
305
|
+
|
|
306
|
+
*Tested on AWS t3.micro with 100 concurrent connections*
|
|
307
|
+
|
|
308
|
+
## Documentation 📚
|
|
309
|
+
|
|
310
|
+
- [中文文档](https://koatty.org/)
|
|
311
|
+
- [Getting Started Guide](https://github.com/Koatty/koatty_doc/blob/master/docs/README-en.md)
|
|
312
|
+
- [API Reference](https://koatty.org/#/?id=api)
|
|
313
|
+
- [Recipes & Best Practices](https://github.com/Koatty/koatty_awesome)
|
|
314
|
+
- [Example](https://github.com/Koatty/koatty_demo)
|
|
315
|
+
|
|
316
|
+
## Quick Start ⚡
|
|
317
|
+
|
|
318
|
+
1. **Install CLI**:
|
|
319
|
+
```bash
|
|
320
|
+
npm install -g koatty_cli
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
2. **Create Project**:
|
|
324
|
+
```bash
|
|
325
|
+
koatty new awesome-app
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
3. **Run Development Server**:
|
|
329
|
+
```bash
|
|
330
|
+
cd awesome-app
|
|
331
|
+
npm run dev
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
## Community 🌍
|
|
335
|
+
|
|
336
|
+
- [GitHub Discussions](https://github.com/Koatty/koatty/discussions)
|
|
337
|
+
|
|
338
|
+
## Contributors ✨
|
|
339
|
+
|
|
340
|
+
Thanks to these amazing developers:
|
|
341
|
+
|
|
342
|
+
<!-- Add contributor list here -->
|
|
343
|
+
|
|
344
|
+
## License 📄
|
|
345
|
+
|
|
346
|
+
BSD-3 © [Koatty Team](https://github.com/Koatty)
|
package/dist/LICENSE
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
BSD 3-Clause License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020, Koatty
|
|
4
|
-
All rights reserved.
|
|
5
|
-
|
|
6
|
-
Redistribution and use in source and binary forms, with or without
|
|
7
|
-
modification, are permitted provided that the following conditions are met:
|
|
8
|
-
|
|
9
|
-
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
-
list of conditions and the following disclaimer.
|
|
11
|
-
|
|
12
|
-
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
-
this list of conditions and the following disclaimer in the documentation
|
|
14
|
-
and/or other materials provided with the distribution.
|
|
15
|
-
|
|
16
|
-
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
-
contributors may be used to endorse or promote products derived from
|
|
18
|
-
this software without specific prior written permission.
|
|
19
|
-
|
|
20
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020, Koatty
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|