yinzerflow 0.1.18 โ†’ 0.2.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 CHANGED
@@ -1,296 +0,0 @@
1
- # YinzerFlow
2
-
3
- <div align="center">
4
- <h3>A lightweight, modular HTTP server framework for Node.js</h3>
5
- <p>Built with TypeScript. Zero dependencies. Blazing fast.</p>
6
- </div>
7
-
8
- ## Features
9
-
10
- - ๐Ÿš€ **Lightweight & Fast**: Built from scratch with performance in mind
11
- - ๐Ÿงฉ **Modular Architecture**: Easily extensible with a clean component structure
12
- - ๐Ÿ”’ **Type-Safe**: Full TypeScript support with comprehensive type definitions
13
- - ๐Ÿงช **Well-Tested**: Extensive test coverage for reliability
14
- - ๐Ÿ“ฆ **Zero Dependencies**: No bloated node_modules folder
15
- - ๐Ÿช **Request Lifecycle Hooks**: Powerful hooks system for request processing (formerly middleware)
16
- - ๐Ÿ›ฃ๏ธ **Route Groups**: Organize routes with prefixes and shared hooks
17
- - ๐Ÿ”„ **Event-Based Architecture**: Subscribe to framework events for advanced customization
18
- - ๐ŸŒ **Content Type Handling**: Built-in support for JSON, XML, multipart forms, and more
19
- - ๐Ÿ”Œ **Connection Management**: Robust connection tracking with statistics and graceful shutdown
20
-
21
- ## Installation
22
-
23
- ```bash
24
- npm install yinzerflow
25
- # or
26
- yarn add yinzerflow
27
- # or
28
- bun add yinzerflow
29
- ```
30
-
31
- ## Quick Start
32
-
33
- ### JavaScript
34
-
35
- ```javascript
36
- const { YinzerFlow } = require('yinzerflow');
37
-
38
- const app = new YinzerFlow({ port: 3000 });
39
-
40
- app.get('/hello', ({ request }) => {
41
- return { message: 'Hello, World!' };
42
- });
43
-
44
- app.listen();
45
- console.log('Server running on http://localhost:3000');
46
- ```
47
-
48
- ### TypeScript
49
-
50
- ```typescript
51
- import { YinzerFlow } from 'yinzerflow';
52
-
53
- const app = new YinzerFlow({ port: 3000 });
54
-
55
- app.get('/hello', ({ request }) => {
56
- return { message: 'Hello, World!' };
57
- });
58
-
59
- app.listen();
60
- console.log('Server running on http://localhost:3000');
61
- ```
62
-
63
- ## Core Concepts
64
-
65
- ### Routing
66
-
67
- ```typescript
68
- // Basic routes
69
- app.get('/users', getAllUsersHandler);
70
- app.post('/users', createUserHandler);
71
- app.get('/users/:id', getUserByIdHandler);
72
- app.put('/users/:id', updateUserHandler);
73
- app.delete('/users/:id', deleteUserHandler);
74
-
75
- // Route groups
76
- app.group('/api/v1', [
77
- app.get('/products', getProductsHandler),
78
- app.post('/products', createProductHandler)
79
- ], {
80
- beforeGroup: authenticationHook
81
- });
82
- ```
83
-
84
- ### Request Lifecycle Hooks
85
-
86
- ```typescript
87
- // Global hook for all requests
88
- app.beforeAll(({ request }) => {
89
- console.log(`Request received: ${request.method} ${request.path}`);
90
- });
91
-
92
- // Path-specific hooks
93
- app.beforeAll(
94
- ({ request, response }) => {
95
- const token = request.headers['authorization'];
96
- if (!token) {
97
- response.setStatus(401);
98
- return { error: 'Authentication required' };
99
- }
100
- },
101
- { paths: ['/admin/*', '/profile/*'] }
102
- );
103
-
104
- // Exclude specific paths
105
- app.beforeAll(
106
- authHook,
107
- { paths: 'allButExcluded', excluded: ['/login', '/register'] }
108
- );
109
- ```
110
-
111
- ### Content Type Handling
112
-
113
- ```typescript
114
- // Automatically parses JSON requests
115
- app.post('/api/json', ({ request, response }) => {
116
- if (!isJsonData(request.body)) {
117
- response.setStatus(400);
118
- return { error: 'Expected JSON data' };
119
- }
120
-
121
- const { name, email } = request.body;
122
- return { success: true, data: { name, email } };
123
- });
124
-
125
- // Handle file uploads
126
- app.post('/api/upload', ({ request, response }) => {
127
- if (!isMultipartFormData(request.body)) {
128
- response.setStatus(400);
129
- return { error: 'Expected multipart form data' };
130
- }
131
-
132
- const { fields, files } = request.body;
133
- return {
134
- success: true,
135
- message: `Received ${Object.keys(files).length} files`
136
- };
137
- });
138
- ```
139
-
140
- ### Connection Management
141
-
142
- YinzerFlow provides a robust connection management system that allows you to track, monitor, and gracefully handle server connections:
143
-
144
- ```typescript
145
- import { YinzerFlow } from 'yinzerflow';
146
- import { ConnectionEvent } from 'yinzerflow/constants/connection';
147
-
148
- const app = new YinzerFlow({ port: 3000 });
149
-
150
- // Subscribe to connection events
151
- app.connectionManager.on(ConnectionEvent.CONNECTION_ADDED, (socket) => {
152
- console.log('New connection established');
153
- });
154
-
155
- app.connectionManager.on(ConnectionEvent.CONNECTION_ERROR, (socket, error) => {
156
- console.error('Connection error:', error);
157
- });
158
-
159
- // Get connection statistics
160
- app.get('/admin/stats', ({ request }) => {
161
- const stats = app.connectionManager.getStats();
162
- return {
163
- activeConnections: stats.activeConnections,
164
- totalConnections: stats.totalConnections,
165
- connectionErrors: stats.connectionErrors,
166
- uptime: stats.uptime
167
- };
168
- });
169
-
170
- // Graceful shutdown example
171
- process.on('SIGTERM', async () => {
172
- console.log('SIGTERM received, shutting down gracefully');
173
-
174
- // Give connections 5 seconds to finish before force closing
175
- await app.connectionManager.closeAllConnections(5000);
176
- await app.close();
177
-
178
- console.log('Server shut down gracefully');
179
- process.exit(0);
180
- });
181
-
182
- app.listen();
183
- console.log('Server running on http://localhost:3000');
184
- ```
185
-
186
- #### ConnectionManager API
187
-
188
- The `ConnectionManager` class provides the following methods and properties:
189
-
190
- - **Methods**:
191
- - `setServer(server: Server)`: Associates the connection manager with a server instance
192
- - `addConnection(socket: Socket)`: Registers a new socket connection for tracking
193
- - `removeConnection(socket: Socket)`: Removes a socket from tracking when it closes
194
- - `getStats()`: Returns statistics about connections (`IConnectionStats`)
195
- - `closeAllConnections(gracePeriod?: number)`: Gracefully closes all active connections
196
- - `on(event: ConnectionEvent, listener: Function)`: Subscribes to connection events
197
- - `off(event: ConnectionEvent, listener: Function)`: Unsubscribes from connection events
198
-
199
- - **Events**:
200
- - `CONNECTION_ADDED`: Fired when a new connection is established
201
- - `CONNECTION_REMOVED`: Fired when a connection is closed
202
- - `CONNECTION_ERROR`: Fired when a connection encounters an error
203
- - `ALL_CONNECTIONS_CLOSED`: Fired when all connections have been closed
204
- - `SERVER_LISTENING`: Fired when the server starts listening
205
- - `SERVER_CLOSED`: Fired when the server is closed
206
-
207
- - **Statistics**:
208
- - `activeConnections`: Number of currently active connections
209
- - `totalConnections`: Total number of connections since server start
210
- - `connectionErrors`: Number of connection errors encountered
211
- - `uptime`: Server uptime in milliseconds
212
-
213
- #### Graceful Shutdown Pattern
214
-
215
- For production applications, implementing a graceful shutdown pattern is recommended:
216
-
217
- ```typescript
218
- // Graceful shutdown handler
219
- const gracefulShutdown = async (signal: string) => {
220
- console.log(`${signal} received, starting graceful shutdown`);
221
-
222
- // Step 1: Stop accepting new connections (optional)
223
- server.close();
224
-
225
- // Step 2: Allow existing connections to finish (with timeout)
226
- console.log('Closing remaining connections...');
227
- await app.connectionManager.closeAllConnections(10000); // 10 second grace period
228
-
229
- // Step 3: Close the server completely
230
- console.log('Shutting down server...');
231
- await app.close();
232
-
233
- console.log('Shutdown complete');
234
- process.exit(0);
235
- };
236
-
237
- // Register shutdown handlers
238
- process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
239
- process.on('SIGINT', () => gracefulShutdown('SIGINT'));
240
- ```
241
-
242
- This pattern ensures that your server can handle restarts and deployments without dropping active connections.
243
-
244
- ## Examples
245
-
246
- Check out the [examples](/example) directory for more detailed usage examples:
247
-
248
- - [JavaScript Example](/example/javascript) - Basic server implementation in JavaScript
249
- - [TypeScript Example](/example/typescript) - Type-safe server implementation in TypeScript
250
-
251
- ## Documentation
252
-
253
- For detailed documentation, see the [docs](/docs) directory:
254
-
255
- - [Getting Started](/docs/README.md) - Overview and quick start guide
256
- - [Routing System](/docs/routing.md) - Comprehensive guide to the routing system
257
- - [Request Lifecycle Hooks](/docs/hooks.md) - In-depth documentation of the hooks system
258
- - [Content Type Handling](/docs/content-types.md) - Working with different content types
259
- - [Error Handling](/docs/error-handling.md) - Guide to handling errors at different levels
260
- - [Testing Guide](/TESTING.md) - Comprehensive guide to testing the framework
261
-
262
- ## Project Structure
263
-
264
- ```
265
- yinzerflow/
266
- โ”œโ”€โ”€ app/ # Source code
267
- โ”œโ”€โ”€ docs/ # Documentation
268
- โ”œโ”€โ”€ example/ # Usage examples
269
- โ”‚ โ”œโ”€โ”€ javascript/ # JavaScript example
270
- โ”‚ โ””โ”€โ”€ typescript/ # TypeScript example
271
- โ”œโ”€โ”€ package.json # Package configuration
272
- โ”œโ”€โ”€ TESTING.md # Testing documentation
273
- โ””โ”€โ”€ README.md # This file
274
- ```
275
-
276
- ## Contributing
277
-
278
- Contributions are welcome! Please feel free to submit a Pull Request.
279
-
280
- 1. Fork the repository
281
- 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
282
- 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
283
- 4. Push to the branch (`git push origin feature/amazing-feature`)
284
- 5. Open a Pull Request
285
-
286
- ## License
287
-
288
- This project is licensed under the MIT License - see the LICENSE file for details.
289
-
290
- ## Why "YinzerFlow"?
291
-
292
- "Yinzer" is a term for a native or inhabitant of the city of Pittsburgh, Pennsylvania. The name combines the local Pittsburgh dialect with "flow" to represent the smooth flow of HTTP requests through the framework.
293
-
294
- ---
295
-
296
- Built with โค๏ธ in Pittsburgh