@techiev2/vajra 1.0.2 → 1.1.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
@@ -14,15 +14,16 @@ Vajra draws from the Rigvedic thunderbolt weapon of Indra — crafted from the b
14
14
  Like the Vajra, this server delivers maximum power in minimal form.
15
15
 
16
16
 
17
- [![npm version](https://img.shields.io/npm/v/vajra.svg?style=flat-square)](https://www.npmjs.com/package/vajra)
18
- [![npm downloads](https://img.shields.io/npm/dm/vajra.svg?style=flat-square)](https://www.npmjs.com/package/vajra)
19
- [![Node.js version](https://img.shields.io/node/v/vajra.svg?style=flat-square)](https://nodejs.org)
20
- [![License](https://img.shields.io/npm/l/vajra.svg?style=flat-square)](LICENSE)
17
+ [![npm version](https://img.shields.io/npm/v/@techiev2/vajra.svg?style=flat-square)](https://www.npmjs.com/package/@techiev2/vajra)
18
+ [![npm downloads](https://img.shields.io/npm/dm/vajra.svg?style=flat-square)](https://www.npmjs.com/package/@techiev2/vajra)
19
+ [![Node.js version](https://img.shields.io/node/v/@techiev2/vajra.svg?style=flat-square)](https://nodejs.org)
20
+ [![License](https://img.shields.io/npm/l/@techiev2/vajra.svg?style=flat-square)](LICENSE)
21
21
 
22
22
  ## Features
23
23
 
24
24
  - Zero external dependencies
25
25
  - Built-in routing with named parameters (`:id`)
26
+ - Asynchronous batched logging for performance
26
27
  - Global middleware support with `next()` chaining
27
28
  - JSON, urlencoded, and **multipart/form-data** body parsing
28
29
  - Fast HTML templating with loops, nested objects, and simple array headers
@@ -59,14 +60,17 @@ async function getUsers(query = {}) {
59
60
  return (await fetch(`https://jsonplaceholder.typicode.com/users?${encode(query)}`)).json()
60
61
  }
61
62
 
62
- const { get, post, use, start, setProperty } = Vajra.create();
63
+ const { get, post, use, start, setProperty, log } = Vajra.create();
63
64
 
64
65
  setProperty({ viewsRoot: `${import.meta.url}/views` })
65
66
  // Or as a key-value pair
66
67
  // setProperty('viewsRoot', `${import.meta.url}/views`)
67
68
 
68
69
  use((req, res, next) => {
69
- console.log(`${req.method} ${req.url}`);
70
+ // Vajra provides an async batched logger to provide a balance between 100% log coverage and performance.
71
+ // If you prefer blocking immediate logs, you can switch to console.log
72
+ // or any other library of your choice.
73
+ log(`${req.method} ${req.url}`);
70
74
  next();
71
75
  });
72
76
 
@@ -156,6 +160,7 @@ app.setProperty('viewRoot', './views');
156
160
  - `use(middleware)`
157
161
  - `start({ port, host }, callback?)`
158
162
  - `setProperty(key, value)` or `setProperty({ key: value })`
163
+ - `log(message)`
159
164
 
160
165
 
161
166
  #### Response helpers:
package/examples/api.js CHANGED
@@ -5,12 +5,12 @@ async function getUsers(query = {}) {
5
5
  return (await fetch(`https://jsonplaceholder.typicode.com/users?${encode(query)}`)).json()
6
6
  }
7
7
 
8
- const { get, post, use, start, setProperty } = Vajra.create();
8
+ const { get, post, use, start, setProperty, log } = Vajra.create();
9
9
 
10
10
  setProperty({ viewRoot: `${import.meta.dirname}/views` })
11
11
 
12
12
  use((req, res, next) => {
13
- console.log(`${req.method} ${req.url}`);
13
+ log(`${req.method} ${req.url}`)
14
14
  next();
15
15
  });
16
16
 
package/index.js CHANGED
@@ -25,8 +25,11 @@ const MAX_MB = 2; const MAX_FILE_SIZE = MAX_MB * 1024 * 1024
25
25
  export default class Vajra {
26
26
  static #app; static #routes = {}; static #middlewares = []; static #straightRoutes = {}; static #MAX_FILE_SIZE; static #onCreate; static #props = {}
27
27
  static create({ maxFileSize } = { maxFileSize: 2 }) {
28
- Vajra.#MAX_FILE_SIZE = !+MAX_FILE_SIZE ? +maxFileSize * 1024 * 1024 : MAX_FILE_SIZE
29
28
  Vajra.#app = createServer()
29
+ const _queue = []; const LOG_QUEUE_SIZE = 100; const logOut = () => { if (_queue.length) { process.stdout.write(`${_queue.join('').trim()}\n`) }; _queue.length = 0 };
30
+ const flushAndShutDown = () => { logOut(); Vajra.#app.close(() => { process.exit(0); }); }; 'SIGINT_SIGTERM_SIGABRT'.split('_').map((evt) => process.on(evt, flushAndShutDown));
31
+ process.on('exit', logOut); function log(message) { _queue.push(`${message}\n`); if (_queue.length >= LOG_QUEUE_SIZE) { logOut(); _queue.length = 0; } }
32
+ Vajra.#MAX_FILE_SIZE = !+MAX_FILE_SIZE ? +maxFileSize * 1024 * 1024 : MAX_FILE_SIZE
30
33
  Vajra.#app.on('request', async (req, res) => {
31
34
  res.sent = false;
32
35
  res.status = (/**@type{code} Number*/ code) => {
@@ -104,8 +107,6 @@ export default class Vajra {
104
107
  if (typeof fn !== "function") { throw new Error(`${fn} is not a function. Can't use as middleware`) }
105
108
  Vajra.#middlewares.push(fn); return defaults
106
109
  }
107
- const defaults = Object.freeze(
108
- Object.assign({}, { use, setProperty, start }, Object.fromEntries('get__post__put__patch__delete__head__options'.split('__').map((method) => [method, (...args) => register(method, ...args)]))
109
- )); return Object.assign({}, { start }, defaults)
110
+ const defaults = Object.freeze(Object.assign({}, { use, setProperty, start, log }, Object.fromEntries('get__post__put__patch__delete__head__options'.split('__').map((method) => [method, (...args) => register(method, ...args)])))); return Object.assign({}, { start }, defaults)
110
111
  }
111
- }
112
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@techiev2/vajra",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Blazing-fast, zero-dependency Node.js server with routing, middleware, multipart uploads, and templating. 111 lines · ~95k req/s · ~52 MB idle.",
5
5
  "keywords": [
6
6
  "http-server",