bareserver 0.1.0 → 0.1.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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +106 -0
  3. package/index.js +49 -8
  4. package/package.json +5 -5
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tarun
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # bareserver
2
+
3
+ Zero-dependency HTTP server for Node.js with Express syntax, powered by a native Rust core (Tokio and Hyper).
4
+
5
+ ## Features
6
+
7
+ - Express-compatible API (`app.use`, `app.get`, `app.post`, `Router`, middleware chains)
8
+ - Native multi-threaded I/O via Tokio and Hyper
9
+ - Multi-core scaling via native SO_REUSEPORT
10
+ - Radix-tree routing via matchit
11
+ - Zero runtime dependencies for end users (precompiled platform binaries)
12
+ - Built-in CORS and static route bypasses
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install bareserver
18
+ ```
19
+
20
+ Precompiled binaries are provided for:
21
+ - Linux x64 (glibc)
22
+ - macOS ARM64 (Apple Silicon)
23
+ - macOS x64 (Intel)
24
+ - Windows x64
25
+
26
+ No Rust toolchain is required to install or run.
27
+
28
+ ## Usage
29
+
30
+ ```javascript
31
+ const express = require('bareserver');
32
+ const app = express({ port: 3000 });
33
+
34
+ app.use((req, res, next) => {
35
+ res.set('x-powered-by', 'bareserver');
36
+ next();
37
+ });
38
+
39
+ app.get('/', (req, res) => {
40
+ res.send('Hello World');
41
+ });
42
+
43
+ app.get('/json', (req, res) => {
44
+ res.json({ status: 'ok', timestamp: Date.now() });
45
+ });
46
+
47
+ app.get('/users/:id', (req, res) => {
48
+ res.json({ id: req.params.id, query: req.query });
49
+ });
50
+
51
+ app.post('/items', (req, res) => {
52
+ res.status(201).json({ received: req.body });
53
+ });
54
+
55
+ app.listen(3000, () => {
56
+ console.log('Server running on port 3000');
57
+ });
58
+ ```
59
+
60
+ ## Multi-Core Clustering
61
+
62
+ On multi-core systems, use Node's `cluster` module. Sockets bind with `SO_REUSEPORT`, allowing the kernel to balance connections across worker processes.
63
+
64
+ ```javascript
65
+ const cluster = require('cluster');
66
+ const os = require('os');
67
+ const express = require('bareserver');
68
+
69
+ if (cluster.isPrimary) {
70
+ const workers = os.availableParallelism ? os.availableParallelism() : os.cpus().length;
71
+ for (let i = 0; i < workers; i++) {
72
+ cluster.fork();
73
+ }
74
+ } else {
75
+ const app = express({ port: 8080 });
76
+ app.get('/', (req, res) => res.send('Worker ' + process.pid));
77
+ app.listen(8080);
78
+ }
79
+ ```
80
+
81
+ ## Configuration
82
+
83
+ Options passed to `express(options)`:
84
+
85
+ | Option | Type | Default | Description |
86
+ |---|---|---|---|
87
+ | `port` | `number` | `3000` | Port to bind |
88
+ | `maxBodySize` | `number` | `52428800` (50MB) | Maximum request body size in bytes |
89
+ | `keepAlive` | `boolean` | `true` | Enable HTTP keep-alive |
90
+
91
+ Runtime setters:
92
+ - `app.setBodyLimit(bytes)`
93
+ - `app.setKeepAlive(boolean)`
94
+ - `app.cors()`
95
+ - `app.close([callback])`
96
+
97
+ ## Tests
98
+
99
+ ```bash
100
+ npm test
101
+ npm run test:chaos
102
+ ```
103
+
104
+ ## License
105
+
106
+ MIT
package/index.js CHANGED
@@ -41,7 +41,7 @@ class BareRequest extends Readable {
41
41
  this.url = raw.query ? `${raw.path}?${raw.query}` : raw.path;
42
42
  this.originalUrl = this.url;
43
43
  this.baseUrl = '';
44
- this.query = raw.query ? Object.fromEntries(new URLSearchParams(raw.query)) : {};
44
+ this._query = null;
45
45
  const params = raw.params || {};
46
46
  if (raw.params) {
47
47
  for (const k in raw.params) {
@@ -68,6 +68,26 @@ class BareRequest extends Readable {
68
68
  }
69
69
  }
70
70
 
71
+ get query() {
72
+ if (this._query !== null) return this._query;
73
+ const q = this._raw.query;
74
+ if (!q) return (this._query = {});
75
+ const obj = {};
76
+ let start = 0;
77
+ const len = q.length;
78
+ while (start < len) {
79
+ let eq = q.indexOf('=', start);
80
+ if (eq === -1) eq = len;
81
+ let amp = q.indexOf('&', eq);
82
+ if (amp === -1) amp = len;
83
+ const key = q.slice(start, eq);
84
+ const val = eq < amp ? q.slice(eq + 1, amp) : '';
85
+ obj[key] = val;
86
+ start = amp + 1;
87
+ }
88
+ return (this._query = obj);
89
+ }
90
+
71
91
  get headers() {
72
92
  if (this._headers !== null) return this._headers;
73
93
  const obj = {};
@@ -306,19 +326,36 @@ class BareResponse {
306
326
  return this.set(field, [prev, val]);
307
327
  }
308
328
 
329
+ _getHeadersArray() {
330
+ if (!this.extraHeaders) return null;
331
+ const arr = [];
332
+ for (const k in this.extraHeaders) {
333
+ const v = this.extraHeaders[k];
334
+ if (Array.isArray(v)) {
335
+ for (let i = 0; i < v.length; i++) {
336
+ arr.push(k, v[i]);
337
+ }
338
+ } else {
339
+ arr.push(k, v);
340
+ }
341
+ }
342
+ return arr;
343
+ }
344
+
309
345
  send(body) {
310
346
  if (this.responded) return;
311
347
  this.responded = true;
348
+ const headers = this._getHeadersArray();
312
349
 
313
350
  if (Buffer.isBuffer(body)) {
314
351
  this.contentType = this.contentType || 'application/octet-stream';
315
- this._server.native.respond(this._raw.reqId, this.statusCode, this.contentType, body, this.extraHeaders);
352
+ this._server.native.respond(this._raw.reqId, this.statusCode, this.contentType, body, headers);
316
353
  } else if (typeof body === 'object' && body !== null) {
317
354
  this.contentType = this.contentType || 'application/json';
318
- this._server.native.respond(this._raw.reqId, this.statusCode, this.contentType, JSON.stringify(body), this.extraHeaders);
355
+ this._server.native.respond(this._raw.reqId, this.statusCode, this.contentType, JSON.stringify(body), headers);
319
356
  } else {
320
357
  this.contentType = this.contentType || 'text/plain; charset=utf-8';
321
- this._server.native.respond(this._raw.reqId, this.statusCode, this.contentType, String(body ?? ''), this.extraHeaders);
358
+ this._server.native.respond(this._raw.reqId, this.statusCode, this.contentType, String(body ?? ''), headers);
322
359
  }
323
360
  }
324
361
 
@@ -326,7 +363,7 @@ class BareResponse {
326
363
  if (this.responded) return;
327
364
  this.responded = true;
328
365
  this.contentType = 'application/json';
329
- this._server.native.respond(this._raw.reqId, this.statusCode, this.contentType, JSON.stringify(body), this.extraHeaders);
366
+ this._server.native.respond(this._raw.reqId, this.statusCode, this.contentType, JSON.stringify(body), this._getHeadersArray());
330
367
  }
331
368
 
332
369
  end(data) {
@@ -486,14 +523,18 @@ class BareServer {
486
523
  return this;
487
524
  }
488
525
 
489
- static(path, statusOrBody, maybeBody) {
526
+ static(path, statusOrBody, maybeBodyOrHeaders, maybeHeaders) {
490
527
  let status = 200;
491
528
  let body = statusOrBody;
492
529
  let contentType = 'text/plain; charset=utf-8';
530
+ let headers = null;
493
531
 
494
532
  if (typeof statusOrBody === 'number') {
495
533
  status = statusOrBody;
496
- body = maybeBody;
534
+ body = maybeBodyOrHeaders;
535
+ headers = maybeHeaders || null;
536
+ } else if (typeof maybeBodyOrHeaders === 'object' && !Buffer.isBuffer(maybeBodyOrHeaders)) {
537
+ headers = maybeBodyOrHeaders;
497
538
  }
498
539
 
499
540
  let buf;
@@ -506,7 +547,7 @@ class BareServer {
506
547
  buf = Buffer.from(String(body ?? ''));
507
548
  }
508
549
 
509
- this.native.addStaticRoute('GET', path, status, contentType, buf);
550
+ this.native.addStaticRoute('GET', path, status, contentType, buf, headers);
510
551
  return this;
511
552
  }
512
553
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bareserver",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Zero-dependency, native-fast HTTP server for Node.js",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -11,10 +11,10 @@
11
11
  "LICENSE"
12
12
  ],
13
13
  "optionalDependencies": {
14
- "bareserver-darwin-arm64": "0.1.0",
15
- "bareserver-darwin-x64": "0.1.0",
16
- "bareserver-linux-x64-gnu": "0.1.0",
17
- "bareserver-win32-x64-msvc": "0.1.0"
14
+ "bareserver-darwin-arm64": "0.1.2",
15
+ "bareserver-darwin-x64": "0.1.2",
16
+ "bareserver-linux-x64-gnu": "0.1.2",
17
+ "bareserver-win32-x64-msvc": "0.1.2"
18
18
  },
19
19
  "scripts": {
20
20
  "build": "cargo build --release && (cp target/release/libbareserver.dylib index.node 2>/dev/null || cp target/release/libbareserver.so index.node 2>/dev/null || true)",