@engjts/nexus 0.1.8 → 0.1.9
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 +1 -1
- package/BENCHMARK_REPORT.md +0 -343
- package/documentation/01-getting-started.md +0 -240
- package/documentation/02-context.md +0 -335
- package/documentation/03-routing.md +0 -397
- package/documentation/04-middleware.md +0 -483
- package/documentation/05-validation.md +0 -514
- package/documentation/06-error-handling.md +0 -465
- package/documentation/07-performance.md +0 -364
- package/documentation/08-adapters.md +0 -470
- package/documentation/09-api-reference.md +0 -548
- package/documentation/10-examples.md +0 -582
- package/documentation/11-deployment.md +0 -477
- package/documentation/12-sentry.md +0 -620
- package/documentation/13-sentry-data-storage.md +0 -996
- package/documentation/14-sentry-data-reference.md +0 -457
- package/documentation/15-sentry-summary.md +0 -409
- package/documentation/16-alerts-system.md +0 -745
- package/documentation/17-alert-adapters.md +0 -696
- package/documentation/18-alerts-implementation-summary.md +0 -385
- package/documentation/19-class-based-routing.md +0 -840
- package/documentation/20-websocket-realtime.md +0 -813
- package/documentation/21-cache-system.md +0 -510
- package/documentation/22-job-queue.md +0 -772
- package/documentation/23-sentry-plugin.md +0 -551
- package/documentation/24-testing-utilities.md +0 -1287
- package/documentation/25-api-versioning.md +0 -533
- package/documentation/26-context-store.md +0 -607
- package/documentation/27-dependency-injection.md +0 -329
- package/documentation/28-lifecycle-hooks.md +0 -521
- package/documentation/29-package-structure.md +0 -196
- package/documentation/30-plugin-system.md +0 -414
- package/documentation/31-jwt-authentication.md +0 -597
- package/documentation/32-cli.md +0 -268
- package/documentation/ALERTS-COMPLETE-SUMMARY.md +0 -429
- package/documentation/ALERTS-INDEX.md +0 -330
- package/documentation/ALERTS-QUICK-REFERENCE.md +0 -286
- package/documentation/README.md +0 -178
- package/documentation/index.html +0 -34
- package/modern_framework_paper.md +0 -1870
- package/public/css/style.css +0 -87
- package/public/index.html +0 -34
- package/public/js/app.js +0 -27
- package/src/advanced/cache/InMemoryCacheStore.ts +0 -68
- package/src/advanced/cache/MultiTierCache.ts +0 -194
- package/src/advanced/cache/RedisCacheStore.ts +0 -341
- package/src/advanced/cache/index.ts +0 -5
- package/src/advanced/cache/types.ts +0 -40
- package/src/advanced/graphql/SimpleDataLoader.ts +0 -42
- package/src/advanced/graphql/index.ts +0 -22
- package/src/advanced/graphql/server.ts +0 -252
- package/src/advanced/graphql/types.ts +0 -42
- package/src/advanced/jobs/InMemoryQueueStore.ts +0 -68
- package/src/advanced/jobs/JobQueue.ts +0 -556
- package/src/advanced/jobs/RedisQueueStore.ts +0 -367
- package/src/advanced/jobs/index.ts +0 -5
- package/src/advanced/jobs/types.ts +0 -70
- package/src/advanced/observability/APMManager.ts +0 -163
- package/src/advanced/observability/AlertManager.ts +0 -109
- package/src/advanced/observability/MetricRegistry.ts +0 -151
- package/src/advanced/observability/ObservabilityCenter.ts +0 -304
- package/src/advanced/observability/StructuredLogger.ts +0 -154
- package/src/advanced/observability/TracingManager.ts +0 -117
- package/src/advanced/observability/adapters.ts +0 -304
- package/src/advanced/observability/createObservabilityMiddleware.ts +0 -63
- package/src/advanced/observability/index.ts +0 -11
- package/src/advanced/observability/types.ts +0 -174
- package/src/advanced/playground/extractPathParams.ts +0 -6
- package/src/advanced/playground/generateFieldExample.ts +0 -31
- package/src/advanced/playground/generatePlaygroundHTML.ts +0 -1956
- package/src/advanced/playground/generateSummary.ts +0 -19
- package/src/advanced/playground/getTagFromPath.ts +0 -9
- package/src/advanced/playground/index.ts +0 -8
- package/src/advanced/playground/playground.ts +0 -250
- package/src/advanced/playground/types.ts +0 -49
- package/src/advanced/playground/zodToExample.ts +0 -16
- package/src/advanced/playground/zodToParams.ts +0 -15
- package/src/advanced/postman/buildAuth.ts +0 -31
- package/src/advanced/postman/buildBody.ts +0 -15
- package/src/advanced/postman/buildQueryParams.ts +0 -27
- package/src/advanced/postman/buildRequestItem.ts +0 -36
- package/src/advanced/postman/buildResponses.ts +0 -11
- package/src/advanced/postman/buildUrl.ts +0 -33
- package/src/advanced/postman/capitalize.ts +0 -4
- package/src/advanced/postman/generateCollection.ts +0 -59
- package/src/advanced/postman/generateEnvironment.ts +0 -34
- package/src/advanced/postman/generateExampleFromZod.ts +0 -21
- package/src/advanced/postman/generateFieldExample.ts +0 -45
- package/src/advanced/postman/generateName.ts +0 -20
- package/src/advanced/postman/generateUUID.ts +0 -11
- package/src/advanced/postman/getTagFromPath.ts +0 -10
- package/src/advanced/postman/index.ts +0 -28
- package/src/advanced/postman/postman.ts +0 -156
- package/src/advanced/postman/slugify.ts +0 -7
- package/src/advanced/postman/types.ts +0 -140
- package/src/advanced/realtime/index.ts +0 -18
- package/src/advanced/realtime/websocket.ts +0 -231
- package/src/advanced/sentry/index.ts +0 -1236
- package/src/advanced/sentry/types.ts +0 -355
- package/src/advanced/static/generateDirectoryListing.ts +0 -47
- package/src/advanced/static/generateETag.ts +0 -7
- package/src/advanced/static/getMimeType.ts +0 -9
- package/src/advanced/static/index.ts +0 -32
- package/src/advanced/static/isSafePath.ts +0 -13
- package/src/advanced/static/publicDir.ts +0 -21
- package/src/advanced/static/serveStatic.ts +0 -225
- package/src/advanced/static/spa.ts +0 -24
- package/src/advanced/static/types.ts +0 -159
- package/src/advanced/swagger/SwaggerGenerator.ts +0 -66
- package/src/advanced/swagger/buildOperation.ts +0 -61
- package/src/advanced/swagger/buildParameters.ts +0 -61
- package/src/advanced/swagger/buildRequestBody.ts +0 -21
- package/src/advanced/swagger/buildResponses.ts +0 -54
- package/src/advanced/swagger/capitalize.ts +0 -5
- package/src/advanced/swagger/convertPath.ts +0 -9
- package/src/advanced/swagger/createSwagger.ts +0 -12
- package/src/advanced/swagger/generateOperationId.ts +0 -21
- package/src/advanced/swagger/generateSpec.ts +0 -105
- package/src/advanced/swagger/generateSummary.ts +0 -24
- package/src/advanced/swagger/generateSwaggerUI.ts +0 -70
- package/src/advanced/swagger/generateThemeCss.ts +0 -53
- package/src/advanced/swagger/index.ts +0 -25
- package/src/advanced/swagger/swagger.ts +0 -237
- package/src/advanced/swagger/types.ts +0 -206
- package/src/advanced/swagger/zodFieldToOpenAPI.ts +0 -94
- package/src/advanced/swagger/zodSchemaToOpenAPI.ts +0 -50
- package/src/advanced/swagger/zodToOpenAPI.ts +0 -22
- package/src/advanced/testing/factory.ts +0 -509
- package/src/advanced/testing/harness.ts +0 -612
- package/src/advanced/testing/index.ts +0 -430
- package/src/advanced/testing/load-test.ts +0 -618
- package/src/advanced/testing/mock-server.ts +0 -498
- package/src/advanced/testing/mock.ts +0 -670
- package/src/cli/bin.ts +0 -9
- package/src/cli/cli.ts +0 -158
- package/src/cli/commands/add.ts +0 -178
- package/src/cli/commands/build.ts +0 -73
- package/src/cli/commands/create.ts +0 -166
- package/src/cli/commands/dev.ts +0 -85
- package/src/cli/commands/generate.ts +0 -99
- package/src/cli/commands/help.ts +0 -95
- package/src/cli/commands/init.ts +0 -91
- package/src/cli/commands/version.ts +0 -38
- package/src/cli/index.ts +0 -6
- package/src/cli/templates/generators.ts +0 -359
- package/src/cli/templates/index.ts +0 -680
- package/src/cli/utils/exec.ts +0 -52
- package/src/cli/utils/file-system.ts +0 -78
- package/src/cli/utils/logger.ts +0 -111
- package/src/core/adapter.ts +0 -88
- package/src/core/application.ts +0 -1453
- package/src/core/context-pool.ts +0 -79
- package/src/core/context.ts +0 -856
- package/src/core/index.ts +0 -94
- package/src/core/middleware.ts +0 -272
- package/src/core/performance/buffer-pool.ts +0 -108
- package/src/core/performance/middleware-optimizer.ts +0 -162
- package/src/core/plugin/PluginManager.ts +0 -435
- package/src/core/plugin/builder.ts +0 -358
- package/src/core/plugin/index.ts +0 -50
- package/src/core/plugin/types.ts +0 -214
- package/src/core/router/file-router.ts +0 -623
- package/src/core/router/index.ts +0 -260
- package/src/core/router/radix-tree.ts +0 -242
- package/src/core/serializer.ts +0 -397
- package/src/core/store/index.ts +0 -30
- package/src/core/store/registry.ts +0 -178
- package/src/core/store/request-store.ts +0 -240
- package/src/core/store/types.ts +0 -233
- package/src/core/types.ts +0 -616
- package/src/database/adapter.ts +0 -35
- package/src/database/adapters/index.ts +0 -1
- package/src/database/adapters/mysql.ts +0 -669
- package/src/database/database.ts +0 -70
- package/src/database/dialect.ts +0 -388
- package/src/database/index.ts +0 -12
- package/src/database/migrations.ts +0 -86
- package/src/database/optimizer.ts +0 -125
- package/src/database/query-builder.ts +0 -404
- package/src/database/realtime.ts +0 -53
- package/src/database/schema.ts +0 -71
- package/src/database/transactions.ts +0 -56
- package/src/database/types.ts +0 -87
- package/src/deployment/cluster.ts +0 -471
- package/src/deployment/config.ts +0 -454
- package/src/deployment/docker.ts +0 -599
- package/src/deployment/graceful-shutdown.ts +0 -373
- package/src/deployment/index.ts +0 -56
- package/src/index.ts +0 -281
- package/src/security/adapter.ts +0 -318
- package/src/security/auth/JWTPlugin.ts +0 -234
- package/src/security/auth/JWTProvider.ts +0 -316
- package/src/security/auth/adapter.ts +0 -12
- package/src/security/auth/jwt.ts +0 -234
- package/src/security/auth/middleware.ts +0 -188
- package/src/security/csrf.ts +0 -220
- package/src/security/headers.ts +0 -108
- package/src/security/index.ts +0 -60
- package/src/security/rate-limit/adapter.ts +0 -7
- package/src/security/rate-limit/memory.ts +0 -108
- package/src/security/rate-limit/middleware.ts +0 -181
- package/src/security/sanitization.ts +0 -75
- package/src/security/types.ts +0 -240
- package/src/security/utils.ts +0 -52
- package/tsconfig.json +0 -39
package/package.json
CHANGED
package/BENCHMARK_REPORT.md
DELETED
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
# 📊 Benchmark Report: Nexus vs Express vs Fastify
|
|
2
|
-
|
|
3
|
-
**Tanggal Test:** 7 Desember 2025
|
|
4
|
-
**Environment:** macOS
|
|
5
|
-
**Node.js Runtime:** tsx (TypeScript execution)
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## ⚙️ Konfigurasi Benchmark
|
|
10
|
-
|
|
11
|
-
| Parameter | Value |
|
|
12
|
-
|-----------|-------|
|
|
13
|
-
| Duration | 10 detik per test |
|
|
14
|
-
| Connections | 100 concurrent |
|
|
15
|
-
| Pipelining | 10 requests per connection |
|
|
16
|
-
| Tool | autocannon v7.15.0 |
|
|
17
|
-
|
|
18
|
-
---
|
|
19
|
-
|
|
20
|
-
## 🏆 Hasil Overall
|
|
21
|
-
|
|
22
|
-
| Rank | Server | Avg Req/sec | Avg Latency | Total Errors |
|
|
23
|
-
|------|--------|-------------|-------------|--------------|
|
|
24
|
-
| 🥇 | **Fastify** | 71,578 | 16.5 ms | 0 |
|
|
25
|
-
| 🥈 | **Nexus** | 49,313 | 19.2 ms | 910 |
|
|
26
|
-
| 🥉 | **Express** | 20,277 | 51.6 ms | 0 |
|
|
27
|
-
|
|
28
|
-
---
|
|
29
|
-
|
|
30
|
-
## 📌 Hasil Per Endpoint
|
|
31
|
-
|
|
32
|
-
### 1. Simple JSON Response (`GET /json`)
|
|
33
|
-
|
|
34
|
-
Response: `{ "message": "Hello, World!" }`
|
|
35
|
-
|
|
36
|
-
| Server | Req/sec | Latency (avg) | Latency (p99) | Throughput |
|
|
37
|
-
|--------|---------|---------------|---------------|------------|
|
|
38
|
-
| **Fastify** | 98,004 | 9.69 ms | 16 ms | 18.51 MB/s |
|
|
39
|
-
| **Nexus** | 96,131 | 9.99 ms | 15 ms | 16.69 MB/s |
|
|
40
|
-
| Express | 27,071 | 36.42 ms | 74 ms | 6.76 MB/s |
|
|
41
|
-
|
|
42
|
-
**🏆 Winner:** Fastify (+1.9% vs Nexus)
|
|
43
|
-
|
|
44
|
-
> ✅ **Nexus sangat kompetitif!** Hanya 1.9% lebih lambat dari Fastify untuk simple JSON.
|
|
45
|
-
|
|
46
|
-
---
|
|
47
|
-
|
|
48
|
-
### 2. Echo with Query Parameter (`GET /echo?name=Benchmark`)
|
|
49
|
-
|
|
50
|
-
Response: `{ "echo": "Hello, Benchmark!" }`
|
|
51
|
-
|
|
52
|
-
| Server | Req/sec | Latency (avg) | Latency (p99) | Throughput |
|
|
53
|
-
|--------|---------|---------------|---------------|------------|
|
|
54
|
-
| **Fastify** | 92,023 | 10.44 ms | 19 ms | 17.47 MB/s |
|
|
55
|
-
| **Nexus** | 71,066 | 13.70 ms | 23 ms | 12.40 MB/s |
|
|
56
|
-
| Express | 22,747 | 43.39 ms | 87 ms | 5.71 MB/s |
|
|
57
|
-
|
|
58
|
-
**🏆 Winner:** Fastify (+29.5% vs Nexus)
|
|
59
|
-
|
|
60
|
-
> ⚠️ **Gap mulai terlihat** saat ada query parameter parsing.
|
|
61
|
-
|
|
62
|
-
---
|
|
63
|
-
|
|
64
|
-
### 3. Large Array Response (`GET /users`)
|
|
65
|
-
|
|
66
|
-
Response: Array of 100 user objects
|
|
67
|
-
|
|
68
|
-
| Server | Req/sec | Latency (avg) | Latency (p99) | Throughput |
|
|
69
|
-
|--------|---------|---------------|---------------|------------|
|
|
70
|
-
| **Fastify** | 31,896 | 30.83 ms | 54 ms | 218.99 MB/s |
|
|
71
|
-
| **Nexus** | 30,047 | 32.75 ms | 68 ms | 205.84 MB/s |
|
|
72
|
-
| Express | 15,349 | 64.47 ms | 127 ms | 106.37 MB/s |
|
|
73
|
-
|
|
74
|
-
**🏆 Winner:** Fastify (+6.2% vs Nexus)
|
|
75
|
-
|
|
76
|
-
> ✅ **Nexus performs well** untuk payload besar, hanya 6% lebih lambat.
|
|
77
|
-
|
|
78
|
-
---
|
|
79
|
-
|
|
80
|
-
### 4. POST with JSON Body (`POST /data`)
|
|
81
|
-
|
|
82
|
-
Request Body: `{ "test": "data", "numbers": [1,2,3,4,5] }`
|
|
83
|
-
|
|
84
|
-
| Server | Req/sec | Latency (avg) | Latency (p99) | Errors |
|
|
85
|
-
|--------|---------|---------------|---------------|--------|
|
|
86
|
-
| **Fastify** | 64,388 | 15.03 ms | 28 ms | 0 |
|
|
87
|
-
| Express | 15,941 | 62.10 ms | 103 ms | 0 |
|
|
88
|
-
| Nexus | 8 | 20.44 ms | 27 ms | **910** |
|
|
89
|
-
|
|
90
|
-
**🏆 Winner:** Fastify
|
|
91
|
-
|
|
92
|
-
> 🚨 **CRITICAL ISSUE:** Nexus mengalami 910 timeout errors dan hanya mampu handle 8 req/sec!
|
|
93
|
-
|
|
94
|
-
---
|
|
95
|
-
|
|
96
|
-
## 🐛 Issue Analysis: Nexus POST Body Parsing
|
|
97
|
-
|
|
98
|
-
### Problem yang Ditemukan
|
|
99
|
-
|
|
100
|
-
Saat benchmark POST endpoint, Nexus mengalami:
|
|
101
|
-
- **910 timeout errors**
|
|
102
|
-
- **0 successful 2xx responses** (82 non-2xx responses)
|
|
103
|
-
- Throughput drop dari ~70k req/s ke **8 req/s**
|
|
104
|
-
|
|
105
|
-
### Root Cause Analysis
|
|
106
|
-
|
|
107
|
-
Berdasarkan kode server Nexus yang kita gunakan:
|
|
108
|
-
|
|
109
|
-
```typescript
|
|
110
|
-
app.post('/data', async (ctx) => {
|
|
111
|
-
const body = await ctx.body(); // ← Potential bottleneck
|
|
112
|
-
return ctx.json({ received: body, processed: true });
|
|
113
|
-
});
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
**Kemungkinan penyebab:**
|
|
117
|
-
|
|
118
|
-
1. **Async Body Parsing Bottleneck**
|
|
119
|
-
- `ctx.body()` mungkin tidak di-optimize untuk high concurrency
|
|
120
|
-
- Setiap request harus wait untuk body stream selesai di-parse
|
|
121
|
-
- Dengan 100 connections × 10 pipelining = 1000 concurrent requests, ini bisa cause blocking
|
|
122
|
-
|
|
123
|
-
2. **Stream Handling Issue**
|
|
124
|
-
- Body parsing mungkin tidak properly buffer incoming data
|
|
125
|
-
- Potential memory pressure saat banyak request bersamaan
|
|
126
|
-
|
|
127
|
-
3. **Missing Content-Type Handling**
|
|
128
|
-
- Mungkin perlu explicit JSON parser middleware
|
|
129
|
-
|
|
130
|
-
4. **Connection Pool Exhaustion**
|
|
131
|
-
- Async operations yang pending bisa exhaust available resources
|
|
132
|
-
|
|
133
|
-
### Rekomendasi untuk Nexus
|
|
134
|
-
|
|
135
|
-
```typescript
|
|
136
|
-
// Option 1: Pre-parse body dengan middleware
|
|
137
|
-
app.use(async (ctx, next) => {
|
|
138
|
-
if (ctx.method === 'POST' || ctx.method === 'PUT') {
|
|
139
|
-
ctx.parsedBody = await ctx.body();
|
|
140
|
-
}
|
|
141
|
-
return next();
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
// Option 2: Gunakan built-in JSON middleware jika ada
|
|
145
|
-
app.use(nexus.json()); // seperti express.json()
|
|
146
|
-
|
|
147
|
-
// Option 3: Batasi concurrent body parsing
|
|
148
|
-
import { Semaphore } from 'async-mutex';
|
|
149
|
-
const bodySemaphore = new Semaphore(50);
|
|
150
|
-
|
|
151
|
-
app.post('/data', async (ctx) => {
|
|
152
|
-
const [, release] = await bodySemaphore.acquire();
|
|
153
|
-
try {
|
|
154
|
-
const body = await ctx.body();
|
|
155
|
-
return ctx.json({ received: body, processed: true });
|
|
156
|
-
} finally {
|
|
157
|
-
release();
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
---
|
|
163
|
-
|
|
164
|
-
## 📈 Performance Gap Analysis: Mengapa Nexus Kalah dari Fastify?
|
|
165
|
-
|
|
166
|
-
### 1. JSON Serialization
|
|
167
|
-
|
|
168
|
-
**Fastify** menggunakan `fast-json-stringify` yang pre-compile JSON schema:
|
|
169
|
-
|
|
170
|
-
```javascript
|
|
171
|
-
// Fastify internal - schema-based serialization
|
|
172
|
-
const stringify = fastJson({
|
|
173
|
-
type: 'object',
|
|
174
|
-
properties: {
|
|
175
|
-
message: { type: 'string' }
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
// Output langsung tanpa JSON.stringify overhead
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
**Improvement untuk Nexus:**
|
|
182
|
-
- Implement schema-based JSON serialization
|
|
183
|
-
- Cache stringify functions per route
|
|
184
|
-
- Gunakan library seperti `fast-json-stringify` atau `@msgpack/msgpack`
|
|
185
|
-
|
|
186
|
-
---
|
|
187
|
-
|
|
188
|
-
### 2. Router Performance
|
|
189
|
-
|
|
190
|
-
**Fastify** menggunakan `find-my-way`, radix tree-based router yang O(log n):
|
|
191
|
-
|
|
192
|
-
```
|
|
193
|
-
/users/:id → Radix tree lookup (sangat cepat)
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
**Improvement untuk Nexus:**
|
|
197
|
-
- Evaluate current router implementation
|
|
198
|
-
- Consider menggunakan `find-my-way` atau `trek-router`
|
|
199
|
-
- Implement route caching untuk static routes
|
|
200
|
-
- Pre-compile regex patterns saat startup
|
|
201
|
-
|
|
202
|
-
---
|
|
203
|
-
|
|
204
|
-
### 3. Request/Response Object Overhead
|
|
205
|
-
|
|
206
|
-
**Fastify** minimize object creation dan property access:
|
|
207
|
-
|
|
208
|
-
```javascript
|
|
209
|
-
// Fastify - minimal wrapper
|
|
210
|
-
reply.send({ data }); // Direct write ke socket
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
**Improvement untuk Nexus:**
|
|
214
|
-
- Reduce Context object properties
|
|
215
|
-
- Lazy-load properties yang jarang dipakai
|
|
216
|
-
- Pool dan reuse context objects
|
|
217
|
-
- Avoid spread operators di hot paths
|
|
218
|
-
|
|
219
|
-
---
|
|
220
|
-
|
|
221
|
-
### 4. Body Parsing Strategy
|
|
222
|
-
|
|
223
|
-
**Fastify** parse body secara lazy dan streaming:
|
|
224
|
-
|
|
225
|
-
```javascript
|
|
226
|
-
// Hanya parse jika diperlukan
|
|
227
|
-
fastify.addContentTypeParser('application/json',
|
|
228
|
-
{ parseAs: 'buffer' },
|
|
229
|
-
(req, body, done) => {
|
|
230
|
-
// Efficient buffer-based parsing
|
|
231
|
-
}
|
|
232
|
-
);
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
**Improvement untuk Nexus:**
|
|
236
|
-
- Implement streaming body parser
|
|
237
|
-
- Add body size limits untuk prevent DoS
|
|
238
|
-
- Cache parsed bodies
|
|
239
|
-
- Support different content types efficiently
|
|
240
|
-
|
|
241
|
-
---
|
|
242
|
-
|
|
243
|
-
### 5. Middleware Chain Optimization
|
|
244
|
-
|
|
245
|
-
**Improvement untuk Nexus:**
|
|
246
|
-
```typescript
|
|
247
|
-
// Instead of array-based middleware
|
|
248
|
-
// Use compiled function chain
|
|
249
|
-
const compiledHandler = compileMiddleware([
|
|
250
|
-
middleware1,
|
|
251
|
-
middleware2,
|
|
252
|
-
routeHandler
|
|
253
|
-
]);
|
|
254
|
-
|
|
255
|
-
function compileMiddleware(middlewares) {
|
|
256
|
-
return middlewares.reduceRight(
|
|
257
|
-
(next, mw) => (ctx) => mw(ctx, next),
|
|
258
|
-
(ctx) => ctx
|
|
259
|
-
);
|
|
260
|
-
}
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
---
|
|
264
|
-
|
|
265
|
-
### 6. HTTP Header Handling
|
|
266
|
-
|
|
267
|
-
**Improvement untuk Nexus:**
|
|
268
|
-
- Pre-allocate common headers
|
|
269
|
-
- Use header caching
|
|
270
|
-
- Minimize header parsing overhead
|
|
271
|
-
|
|
272
|
-
```typescript
|
|
273
|
-
// Cache static headers
|
|
274
|
-
const COMMON_HEADERS = {
|
|
275
|
-
'content-type': 'application/json; charset=utf-8',
|
|
276
|
-
'x-powered-by': 'Nexus'
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
// Apply once, not per-request
|
|
280
|
-
response.writeHead(200, COMMON_HEADERS);
|
|
281
|
-
```
|
|
282
|
-
|
|
283
|
-
---
|
|
284
|
-
|
|
285
|
-
## 🎯 Priority Improvements untuk Nexus
|
|
286
|
-
|
|
287
|
-
### High Priority (Impact Besar)
|
|
288
|
-
|
|
289
|
-
| # | Improvement | Expected Gain | Effort |
|
|
290
|
-
|---|-------------|---------------|--------|
|
|
291
|
-
| 1 | Fix POST body parsing | +10,000% untuk POST | Medium |
|
|
292
|
-
| 2 | Schema-based JSON stringify | +15-25% | Medium |
|
|
293
|
-
| 3 | Optimize router | +10-20% | High |
|
|
294
|
-
|
|
295
|
-
### Medium Priority
|
|
296
|
-
|
|
297
|
-
| # | Improvement | Expected Gain | Effort |
|
|
298
|
-
|---|-------------|---------------|--------|
|
|
299
|
-
| 4 | Query string parser optimization | +5-15% | Low |
|
|
300
|
-
| 5 | Context object pooling | +5-10% | Medium |
|
|
301
|
-
| 6 | Header caching | +3-5% | Low |
|
|
302
|
-
|
|
303
|
-
### Low Priority (Nice to Have)
|
|
304
|
-
|
|
305
|
-
| # | Improvement | Expected Gain | Effort |
|
|
306
|
-
|---|-------------|---------------|--------|
|
|
307
|
-
| 7 | HTTP/2 support | Varies | High |
|
|
308
|
-
| 8 | Cluster mode built-in | Linear scaling | Medium |
|
|
309
|
-
| 9 | Native addon for hot paths | +20-30% | Very High |
|
|
310
|
-
|
|
311
|
-
---
|
|
312
|
-
|
|
313
|
-
## 📋 Summary
|
|
314
|
-
|
|
315
|
-
### Kelebihan Nexus Saat Ini
|
|
316
|
-
- ✅ Performa GET request sangat bagus (hampir setara Fastify)
|
|
317
|
-
- ✅ Simple JSON response hanya 1.9% lebih lambat dari Fastify
|
|
318
|
-
- ✅ Large payload handling bagus (6% gap)
|
|
319
|
-
- ✅ Developer Experience yang baik
|
|
320
|
-
|
|
321
|
-
### Area yang Perlu Diperbaiki
|
|
322
|
-
- 🔴 **CRITICAL:** POST body parsing broken under load
|
|
323
|
-
- 🟡 Query parameter parsing bisa lebih cepat (+29% gap)
|
|
324
|
-
- 🟡 JSON serialization bisa di-optimize
|
|
325
|
-
|
|
326
|
-
### Rekomendasi Utama
|
|
327
|
-
1. **Immediate:** Fix body parsing untuk POST/PUT requests
|
|
328
|
-
2. **Short-term:** Implement schema-based JSON serialization
|
|
329
|
-
3. **Medium-term:** Optimize router dengan radix tree
|
|
330
|
-
4. **Long-term:** Consider V8 fast-path optimizations
|
|
331
|
-
|
|
332
|
-
---
|
|
333
|
-
|
|
334
|
-
## 🔗 Resources
|
|
335
|
-
|
|
336
|
-
- [Fastify Benchmarks](https://fastify.dev/benchmarks/)
|
|
337
|
-
- [fast-json-stringify](https://github.com/fastify/fast-json-stringify)
|
|
338
|
-
- [find-my-way Router](https://github.com/delvedor/find-my-way)
|
|
339
|
-
- [Node.js Performance Best Practices](https://nodejs.org/en/docs/guides/dont-block-the-event-loop/)
|
|
340
|
-
|
|
341
|
-
---
|
|
342
|
-
|
|
343
|
-
*Report generated by benchmark suite on December 7, 2025*
|
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
# Getting Started with Nexus Framework
|
|
2
|
-
|
|
3
|
-
## Installation
|
|
4
|
-
|
|
5
|
-
### Prerequisites
|
|
6
|
-
|
|
7
|
-
- Node.js >= 18.0.0
|
|
8
|
-
- TypeScript >= 5.0.0
|
|
9
|
-
|
|
10
|
-
### Setup
|
|
11
|
-
|
|
12
|
-
1. **Clone or initialize your project**
|
|
13
|
-
|
|
14
|
-
```bash
|
|
15
|
-
mkdir my-nexus-app
|
|
16
|
-
cd my-nexus-app
|
|
17
|
-
npm init -y
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
2. **Install dependencies**
|
|
21
|
-
|
|
22
|
-
```bash
|
|
23
|
-
npm install typescript @types/node zod
|
|
24
|
-
npm install -D ts-node
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
3. **Configure TypeScript**
|
|
28
|
-
|
|
29
|
-
Create `tsconfig.json`:
|
|
30
|
-
|
|
31
|
-
```json
|
|
32
|
-
{
|
|
33
|
-
"compilerOptions": {
|
|
34
|
-
"target": "ES2022",
|
|
35
|
-
"module": "commonjs",
|
|
36
|
-
"strict": true,
|
|
37
|
-
"esModuleInterop": true,
|
|
38
|
-
"skipLibCheck": true,
|
|
39
|
-
"outDir": "./dist"
|
|
40
|
-
},
|
|
41
|
-
"include": ["src/**/*"]
|
|
42
|
-
}
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
## Your First Application
|
|
46
|
-
|
|
47
|
-
### Basic Server
|
|
48
|
-
|
|
49
|
-
Create `src/index.ts`:
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
import { createApp } from './nexus';
|
|
53
|
-
|
|
54
|
-
const app = createApp();
|
|
55
|
-
|
|
56
|
-
app.get('/hello', async (ctx) => {
|
|
57
|
-
return { message: 'Hello, Nexus!' };
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
app.listen(3000, () => {
|
|
61
|
-
console.log('Server running on http://localhost:3000');
|
|
62
|
-
});
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### Run the Application
|
|
66
|
-
|
|
67
|
-
```bash
|
|
68
|
-
npx ts-node src/index.ts
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
Visit `http://localhost:3000/hello` to see your first response!
|
|
72
|
-
|
|
73
|
-
## Core Concepts
|
|
74
|
-
|
|
75
|
-
### 1. Unified Context
|
|
76
|
-
|
|
77
|
-
Unlike Express.js which uses separate `req` and `res` objects, Nexus uses a single **Context** object:
|
|
78
|
-
|
|
79
|
-
```typescript
|
|
80
|
-
app.get('/user', async (ctx) => {
|
|
81
|
-
// Access request data
|
|
82
|
-
const userId = ctx.query.id;
|
|
83
|
-
const token = ctx.headers.authorization;
|
|
84
|
-
|
|
85
|
-
// Return response (auto-converted to JSON)
|
|
86
|
-
return { userId, authenticated: !!token };
|
|
87
|
-
});
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
### 2. Async-First
|
|
91
|
-
|
|
92
|
-
All handlers are async functions with **automatic error handling**:
|
|
93
|
-
|
|
94
|
-
```typescript
|
|
95
|
-
app.get('/user/:id', async (ctx) => {
|
|
96
|
-
// Errors are automatically caught
|
|
97
|
-
const user = await database.getUser(ctx.params.id);
|
|
98
|
-
return { user };
|
|
99
|
-
});
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
No need for try-catch or `next(error)` calls!
|
|
103
|
-
|
|
104
|
-
### 3. Type Safety
|
|
105
|
-
|
|
106
|
-
Full TypeScript support with **type inference**:
|
|
107
|
-
|
|
108
|
-
```typescript
|
|
109
|
-
import { z } from 'zod';
|
|
110
|
-
|
|
111
|
-
app.post('/users', {
|
|
112
|
-
schema: {
|
|
113
|
-
body: z.object({
|
|
114
|
-
name: z.string(),
|
|
115
|
-
email: z.string().email()
|
|
116
|
-
})
|
|
117
|
-
},
|
|
118
|
-
handler: async (ctx) => {
|
|
119
|
-
// ctx.body is typed as { name: string, email: string }
|
|
120
|
-
const user = await createUser(ctx.body);
|
|
121
|
-
return { user };
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
## HTTP Methods
|
|
127
|
-
|
|
128
|
-
Nexus supports all standard HTTP methods:
|
|
129
|
-
|
|
130
|
-
```typescript
|
|
131
|
-
app.get('/resource', async (ctx) => { /* ... */ });
|
|
132
|
-
app.post('/resource', async (ctx) => { /* ... */ });
|
|
133
|
-
app.put('/resource/:id', async (ctx) => { /* ... */ });
|
|
134
|
-
app.patch('/resource/:id', async (ctx) => { /* ... */ });
|
|
135
|
-
app.delete('/resource/:id', async (ctx) => { /* ... */ });
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
## Response Types
|
|
139
|
-
|
|
140
|
-
### JSON (Default)
|
|
141
|
-
|
|
142
|
-
```typescript
|
|
143
|
-
app.get('/data', async (ctx) => {
|
|
144
|
-
return { key: 'value' }; // Auto-serialized to JSON
|
|
145
|
-
});
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
### HTML
|
|
149
|
-
|
|
150
|
-
```typescript
|
|
151
|
-
app.get('/page', async (ctx) => {
|
|
152
|
-
return ctx.html('<h1>Welcome</h1>');
|
|
153
|
-
});
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
### Text
|
|
157
|
-
|
|
158
|
-
```typescript
|
|
159
|
-
app.get('/text', async (ctx) => {
|
|
160
|
-
return ctx.text('Plain text response');
|
|
161
|
-
});
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### Redirect
|
|
165
|
-
|
|
166
|
-
```typescript
|
|
167
|
-
app.get('/old', async (ctx) => {
|
|
168
|
-
return ctx.redirect('/new', 301);
|
|
169
|
-
});
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
### Stream
|
|
173
|
-
|
|
174
|
-
```typescript
|
|
175
|
-
import { createReadStream } from 'fs';
|
|
176
|
-
|
|
177
|
-
app.get('/file', async (ctx) => {
|
|
178
|
-
const stream = createReadStream('./large-file.dat');
|
|
179
|
-
return ctx.stream(stream);
|
|
180
|
-
});
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
## Project Structure
|
|
184
|
-
|
|
185
|
-
Recommended structure for your application:
|
|
186
|
-
|
|
187
|
-
```
|
|
188
|
-
my-app/
|
|
189
|
-
├── src/
|
|
190
|
-
│ ├── index.ts # Application entry point
|
|
191
|
-
│ ├── routes/ # Route handlers
|
|
192
|
-
│ │ ├── users.ts
|
|
193
|
-
│ │ └── posts.ts
|
|
194
|
-
│ ├── middleware/ # Custom middleware
|
|
195
|
-
│ │ └── auth.ts
|
|
196
|
-
│ └── config/ # Configuration
|
|
197
|
-
│ └── app.ts
|
|
198
|
-
├── package.json
|
|
199
|
-
└── tsconfig.json
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
## Next Steps
|
|
203
|
-
|
|
204
|
-
- 📖 [Context Documentation](./02-context.md) - Learn about the Context API
|
|
205
|
-
- 🛤️ [Routing Guide](./03-routing.md) - Advanced routing patterns
|
|
206
|
-
- 🔌 [Middleware System](./04-middleware.md) - Create custom middleware
|
|
207
|
-
- ✅ [Validation](./05-validation.md) - Schema validation with Zod
|
|
208
|
-
- ⚡ [Performance](./07-performance.md) - Optimization features
|
|
209
|
-
|
|
210
|
-
## Quick Reference
|
|
211
|
-
|
|
212
|
-
```typescript
|
|
213
|
-
import { createApp, z, logger, cors } from './nexus';
|
|
214
|
-
|
|
215
|
-
const app = createApp({
|
|
216
|
-
debug: true,
|
|
217
|
-
contextPoolSize: 100
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
// Global middleware
|
|
221
|
-
app.use(logger());
|
|
222
|
-
app.use(cors());
|
|
223
|
-
|
|
224
|
-
// Routes
|
|
225
|
-
app.get('/path', handler);
|
|
226
|
-
app.post('/path', { schema, handler });
|
|
227
|
-
app.route({ method, path, handler, middlewares, schema });
|
|
228
|
-
|
|
229
|
-
// Error handling
|
|
230
|
-
app.onError((error, ctx) => {
|
|
231
|
-
return { statusCode: 500, body: 'Error' };
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
// Start server
|
|
235
|
-
app.listen(3000);
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
---
|
|
239
|
-
|
|
240
|
-
**Need help?** Check the [API Reference](./09-api-reference.md) for detailed documentation.
|