balda-js 0.0.1 → 0.0.2
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/lib/cli.d.ts +6 -0
- package/lib/cli.js +929 -0
- package/lib/cli.js.map +1 -0
- package/lib/index.cjs +3384 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.cts +1492 -0
- package/lib/index.d.ts +1492 -0
- package/lib/index.js +3327 -0
- package/lib/index.js.map +1 -0
- package/package.json +1 -1
- package/.husky/pre-commit +0 -19
- package/.nvmrc +0 -1
- package/docs/README.md +0 -135
- package/docs/blog/authors.yml +0 -6
- package/docs/blog/tags.yml +0 -4
- package/docs/cli.md +0 -109
- package/docs/docs/core-concepts/controllers.md +0 -393
- package/docs/docs/core-concepts/middleware.md +0 -302
- package/docs/docs/core-concepts/request-response.md +0 -486
- package/docs/docs/core-concepts/routing.md +0 -388
- package/docs/docs/core-concepts/server.md +0 -332
- package/docs/docs/cron/overview.md +0 -70
- package/docs/docs/examples/rest-api.md +0 -595
- package/docs/docs/getting-started/configuration.md +0 -168
- package/docs/docs/getting-started/installation.md +0 -125
- package/docs/docs/getting-started/quick-start.md +0 -273
- package/docs/docs/intro.md +0 -46
- package/docs/docs/plugins/cookie.md +0 -424
- package/docs/docs/plugins/cors.md +0 -295
- package/docs/docs/plugins/file.md +0 -382
- package/docs/docs/plugins/helmet.md +0 -388
- package/docs/docs/plugins/json.md +0 -338
- package/docs/docs/plugins/log.md +0 -592
- package/docs/docs/plugins/overview.md +0 -390
- package/docs/docs/plugins/rate-limiter.md +0 -347
- package/docs/docs/plugins/static.md +0 -352
- package/docs/docs/plugins/swagger.md +0 -411
- package/docs/docs/plugins/urlencoded.md +0 -76
- package/docs/docs/testing/examples.md +0 -384
- package/docs/docs/testing/mock-server.md +0 -311
- package/docs/docs/testing/overview.md +0 -76
- package/docs/docusaurus.config.ts +0 -144
- package/docs/intro.md +0 -78
- package/docs/package.json +0 -46
- package/docs/sidebars.ts +0 -72
- package/docs/static/.nojekyll +0 -0
- package/docs/static/img/docusaurus-social-card.jpg +0 -0
- package/docs/static/img/docusaurus.png +0 -0
- package/docs/static/img/favicon.ico +0 -0
- package/docs/static/img/logo.svg +0 -1
- package/docs/static/img/undraw_docusaurus_mountain.svg +0 -37
- package/docs/static/img/undraw_docusaurus_react.svg +0 -170
- package/docs/static/img/undraw_docusaurus_tree.svg +0 -40
- package/docs/tsconfig.json +0 -8
- package/speed_test.sh +0 -3
- package/test/benchmark/index.ts +0 -17
- package/test/cli/cli.ts +0 -7
- package/test/commands/test.ts +0 -42
- package/test/controllers/file_upload.ts +0 -29
- package/test/controllers/urlencoded.ts +0 -13
- package/test/controllers/users.ts +0 -111
- package/test/cron/index.ts +0 -6
- package/test/cron/test_cron.ts +0 -8
- package/test/cron/test_cron_imported.ts +0 -8
- package/test/native_env.ts +0 -16
- package/test/resources/test.txt +0 -1
- package/test/server/index.ts +0 -3
- package/test/server/instance.ts +0 -63
- package/test/suite/upload.test.ts +0 -23
- package/test/suite/urlencoded.test.ts +0 -23
- package/test/suite/users.test.ts +0 -76
- package/todo.md +0 -9
- package/tsconfig.json +0 -24
- package/vitest.config.ts +0 -17
@@ -1,352 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 4
|
3
|
-
---
|
4
|
-
|
5
|
-
# Static Plugin
|
6
|
-
|
7
|
-
The Static plugin serves static files from a specified directory. It automatically handles file serving, MIME type detection, and provides a clean API for serving static assets like images, CSS, JavaScript, and other files.
|
8
|
-
|
9
|
-
## Features
|
10
|
-
|
11
|
-
- **Automatic MIME Type Detection**: Detects content types based on file extensions
|
12
|
-
- **Path-Based Routing**: Serves files from any API path
|
13
|
-
- **Security**: Only serves files from the specified directory
|
14
|
-
- **Performance**: Efficient file serving with proper headers
|
15
|
-
- **Swagger Integration**: Optional Swagger documentation for static routes
|
16
|
-
|
17
|
-
## Basic Configuration
|
18
|
-
|
19
|
-
### Simple Setup
|
20
|
-
|
21
|
-
```typescript
|
22
|
-
import { Server } from 'balda-js';
|
23
|
-
|
24
|
-
const server = new Server({
|
25
|
-
port: 3000,
|
26
|
-
plugins: {
|
27
|
-
static: 'public'
|
28
|
-
}
|
29
|
-
});
|
30
|
-
```
|
31
|
-
|
32
|
-
### With Custom Path
|
33
|
-
|
34
|
-
```typescript
|
35
|
-
const server = new Server({
|
36
|
-
port: 3000,
|
37
|
-
plugins: {
|
38
|
-
static: {
|
39
|
-
path: 'assets',
|
40
|
-
root: './public'
|
41
|
-
}
|
42
|
-
}
|
43
|
-
});
|
44
|
-
```
|
45
|
-
|
46
|
-
## Configuration Options
|
47
|
-
|
48
|
-
### Basic Configuration
|
49
|
-
|
50
|
-
```typescript
|
51
|
-
// Serve from 'public' directory at '/public' path
|
52
|
-
static: 'public'
|
53
|
-
|
54
|
-
// Serve from 'assets' directory at '/assets' path
|
55
|
-
static: 'assets'
|
56
|
-
|
57
|
-
// Serve from 'static' directory at '/static' path
|
58
|
-
static: 'static'
|
59
|
-
```
|
60
|
-
|
61
|
-
### Advanced Configuration
|
62
|
-
|
63
|
-
```typescript
|
64
|
-
const server = new Server({
|
65
|
-
port: 3000,
|
66
|
-
plugins: {
|
67
|
-
static: {
|
68
|
-
path: 'assets',
|
69
|
-
root: './public',
|
70
|
-
swagger: {
|
71
|
-
name: 'Static Assets',
|
72
|
-
description: 'Serve static files and assets',
|
73
|
-
service: 'StaticFiles'
|
74
|
-
}
|
75
|
-
}
|
76
|
-
}
|
77
|
-
});
|
78
|
-
```
|
79
|
-
|
80
|
-
## Usage Examples
|
81
|
-
|
82
|
-
### Basic File Serving
|
83
|
-
|
84
|
-
```typescript
|
85
|
-
// Directory structure:
|
86
|
-
// public/
|
87
|
-
// ├── index.html
|
88
|
-
// ├── css/
|
89
|
-
// │ └── style.css
|
90
|
-
// ├── js/
|
91
|
-
// │ └── app.js
|
92
|
-
// └── images/
|
93
|
-
// └── logo.png
|
94
|
-
|
95
|
-
const server = new Server({
|
96
|
-
port: 3000,
|
97
|
-
plugins: {
|
98
|
-
static: 'public'
|
99
|
-
}
|
100
|
-
});
|
101
|
-
|
102
|
-
// Files will be available at:
|
103
|
-
// http://localhost:3000/public/index.html
|
104
|
-
// http://localhost:3000/public/css/style.css
|
105
|
-
// http://localhost:3000/public/js/app.js
|
106
|
-
// http://localhost:3000/public/images/logo.png
|
107
|
-
```
|
108
|
-
|
109
|
-
### Custom Path Configuration
|
110
|
-
|
111
|
-
```typescript
|
112
|
-
const server = new Server({
|
113
|
-
port: 3000,
|
114
|
-
plugins: {
|
115
|
-
static: 'assets' // Serves from 'assets' directory
|
116
|
-
}
|
117
|
-
});
|
118
|
-
|
119
|
-
// Files will be available at:
|
120
|
-
// http://localhost:3000/assets/index.html
|
121
|
-
// http://localhost:3000/assets/css/style.css
|
122
|
-
```
|
123
|
-
|
124
|
-
### Multiple Static Directories
|
125
|
-
|
126
|
-
```typescript
|
127
|
-
const server = new Server({
|
128
|
-
port: 3000,
|
129
|
-
plugins: {
|
130
|
-
static: ['public', 'assets', 'uploads']
|
131
|
-
}
|
132
|
-
});
|
133
|
-
|
134
|
-
// Files will be available at:
|
135
|
-
// http://localhost:3000/public/...
|
136
|
-
// http://localhost:3000/assets/...
|
137
|
-
// http://localhost:3000/uploads/...
|
138
|
-
```
|
139
|
-
|
140
|
-
## File Types and MIME Types
|
141
|
-
|
142
|
-
The static plugin automatically detects MIME types for common file extensions:
|
143
|
-
|
144
|
-
### Supported File Types
|
145
|
-
|
146
|
-
| Extension | MIME Type | Description |
|
147
|
-
|-----------|-----------|-------------|
|
148
|
-
| `.html` | `text/html` | HTML files |
|
149
|
-
| `.css` | `text/css` | CSS stylesheets |
|
150
|
-
| `.js` | `application/javascript` | JavaScript files |
|
151
|
-
| `.png` | `image/png` | PNG images |
|
152
|
-
| `.jpg`, `.jpeg` | `image/jpeg` | JPEG images |
|
153
|
-
| `.gif` | `image/gif` | GIF images |
|
154
|
-
| `.svg` | `image/svg+xml` | SVG images |
|
155
|
-
| `.json` | `application/json` | JSON files |
|
156
|
-
| `.txt` | `text/plain` | Text files |
|
157
|
-
| `.ico` | `image/x-icon` | Favicon files |
|
158
|
-
| `.webp` | `image/webp` | WebP images |
|
159
|
-
| `.mp4` | `video/mp4` | MP4 videos |
|
160
|
-
| `.mp3` | `audio/mpeg` | MP3 audio |
|
161
|
-
| `.wav` | `audio/wav` | WAV audio |
|
162
|
-
| `.ogg` | `audio/ogg` | OGG audio |
|
163
|
-
| `.webm` | `video/webm` | WebM videos |
|
164
|
-
|
165
|
-
### Custom MIME Types
|
166
|
-
|
167
|
-
For unsupported file types, the plugin defaults to `application/octet-stream`.
|
168
|
-
|
169
|
-
## Security Considerations
|
170
|
-
|
171
|
-
### Directory Traversal Protection
|
172
|
-
|
173
|
-
The static plugin automatically protects against directory traversal attacks:
|
174
|
-
|
175
|
-
```typescript
|
176
|
-
// ❌ This will NOT work (directory traversal attempt)
|
177
|
-
// GET /public/../../../etc/passwd
|
178
|
-
|
179
|
-
// ✅ This will work (valid file path)
|
180
|
-
// GET /public/css/style.css
|
181
|
-
```
|
182
|
-
|
183
|
-
### File Access Control
|
184
|
-
|
185
|
-
```typescript
|
186
|
-
// Only files within the specified directory are accessible
|
187
|
-
const server = new Server({
|
188
|
-
port: 3000,
|
189
|
-
plugins: {
|
190
|
-
static: 'public' // Only files in ./public are served
|
191
|
-
}
|
192
|
-
});
|
193
|
-
|
194
|
-
// Files outside the public directory are not accessible
|
195
|
-
// GET /public/../secret.txt -> 404 Not Found
|
196
|
-
```
|
197
|
-
|
198
|
-
## Performance Optimization
|
199
|
-
|
200
|
-
### Caching Headers
|
201
|
-
|
202
|
-
Consider adding caching middleware for static assets:
|
203
|
-
|
204
|
-
```typescript
|
205
|
-
@controller('/public')
|
206
|
-
export class StaticController {
|
207
|
-
@get('/*')
|
208
|
-
async serveStatic(req: Request, res: Response) {
|
209
|
-
// Add caching headers for static assets
|
210
|
-
res.setHeader('Cache-Control', 'public, max-age=31536000'); // 1 year
|
211
|
-
res.setHeader('ETag', generateETag(req.url));
|
212
|
-
|
213
|
-
// Serve the file
|
214
|
-
return staticFileHandler(req, res, 'public');
|
215
|
-
}
|
216
|
-
}
|
217
|
-
```
|
218
|
-
|
219
|
-
### Compression
|
220
|
-
|
221
|
-
For better performance, consider enabling compression:
|
222
|
-
|
223
|
-
```typescript
|
224
|
-
const server = new Server({
|
225
|
-
port: 3000,
|
226
|
-
plugins: {
|
227
|
-
static: 'public',
|
228
|
-
// Add compression plugin for static assets
|
229
|
-
compression: {
|
230
|
-
filter: (req) => {
|
231
|
-
// Compress static assets
|
232
|
-
return req.url.startsWith('/public/');
|
233
|
-
}
|
234
|
-
}
|
235
|
-
}
|
236
|
-
});
|
237
|
-
```
|
238
|
-
|
239
|
-
## Error Handling
|
240
|
-
|
241
|
-
### File Not Found
|
242
|
-
|
243
|
-
```typescript
|
244
|
-
// Request: GET /public/nonexistent.html
|
245
|
-
// Response: 404 Not Found
|
246
|
-
{
|
247
|
-
"error": "Route not found",
|
248
|
-
"message": "GET /public/nonexistent.html not found"
|
249
|
-
}
|
250
|
-
```
|
251
|
-
|
252
|
-
### Method Not Allowed
|
253
|
-
|
254
|
-
```typescript
|
255
|
-
// Request: POST /public/index.html
|
256
|
-
// Response: 405 Method Not Allowed
|
257
|
-
{
|
258
|
-
"error": "Method not allowed",
|
259
|
-
"message": "POST not allowed for /public/index.html"
|
260
|
-
}
|
261
|
-
```
|
262
|
-
|
263
|
-
## Integration with Other Plugins
|
264
|
-
|
265
|
-
### With CORS
|
266
|
-
|
267
|
-
```typescript
|
268
|
-
const server = new Server({
|
269
|
-
port: 3000,
|
270
|
-
plugins: {
|
271
|
-
cors: {
|
272
|
-
origin: '*',
|
273
|
-
methods: ['GET', 'HEAD']
|
274
|
-
},
|
275
|
-
static: 'public'
|
276
|
-
}
|
277
|
-
});
|
278
|
-
```
|
279
|
-
|
280
|
-
### With Helmet
|
281
|
-
|
282
|
-
```typescript
|
283
|
-
const server = new Server({
|
284
|
-
port: 3000,
|
285
|
-
plugins: {
|
286
|
-
helmet: {
|
287
|
-
contentSecurityPolicy: {
|
288
|
-
directives: {
|
289
|
-
defaultSrc: ["'self'"],
|
290
|
-
styleSrc: ["'self'", "'unsafe-inline'"],
|
291
|
-
scriptSrc: ["'self'"],
|
292
|
-
imgSrc: ["'self'", "data:", "https:"]
|
293
|
-
}
|
294
|
-
}
|
295
|
-
},
|
296
|
-
static: 'public'
|
297
|
-
}
|
298
|
-
});
|
299
|
-
```
|
300
|
-
|
301
|
-
## Swagger Documentation
|
302
|
-
|
303
|
-
### Basic Swagger Integration
|
304
|
-
|
305
|
-
```typescript
|
306
|
-
const server = new Server({
|
307
|
-
port: 3000,
|
308
|
-
plugins: {
|
309
|
-
static: {
|
310
|
-
path: 'public',
|
311
|
-
swagger: {
|
312
|
-
name: 'Static Files',
|
313
|
-
description: 'Serve static files and assets',
|
314
|
-
service: 'StaticFiles'
|
315
|
-
}
|
316
|
-
}
|
317
|
-
}
|
318
|
-
});
|
319
|
-
```
|
320
|
-
|
321
|
-
### Custom Swagger Configuration
|
322
|
-
|
323
|
-
```typescript
|
324
|
-
const server = new Server({
|
325
|
-
port: 3000,
|
326
|
-
plugins: {
|
327
|
-
static: {
|
328
|
-
path: 'assets',
|
329
|
-
swagger: {
|
330
|
-
name: 'Asset Files',
|
331
|
-
description: 'Serve application assets',
|
332
|
-
service: 'Assets',
|
333
|
-
responses: {
|
334
|
-
"200": {
|
335
|
-
type: "object",
|
336
|
-
properties: {
|
337
|
-
file: { type: "string" },
|
338
|
-
size: { type: "number" }
|
339
|
-
}
|
340
|
-
},
|
341
|
-
"404": {
|
342
|
-
type: "object",
|
343
|
-
properties: {
|
344
|
-
error: { type: "string" }
|
345
|
-
}
|
346
|
-
}
|
347
|
-
}
|
348
|
-
}
|
349
|
-
}
|
350
|
-
}
|
351
|
-
});
|
352
|
-
```
|