bareserver 0.1.3 → 0.1.5
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/index.js +113 -15
- package/package.json +5 -5
package/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const { Readable } = require('node:stream');
|
|
2
2
|
const { STATUS_CODES } = require('node:http');
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const path = require('node:path');
|
|
3
5
|
|
|
4
6
|
let native;
|
|
5
7
|
try {
|
|
@@ -111,7 +113,7 @@ class BareRequest extends Readable {
|
|
|
111
113
|
if (this._rawHeaders !== null) return this._rawHeaders;
|
|
112
114
|
const s = this._raw.rawHeadersStr;
|
|
113
115
|
if (!s) return (this._rawHeaders = []);
|
|
114
|
-
const parts = s.split('\
|
|
116
|
+
const parts = s.split('\n');
|
|
115
117
|
if (parts.length > 0 && parts[parts.length - 1] === '') {
|
|
116
118
|
parts.pop();
|
|
117
119
|
}
|
|
@@ -126,12 +128,12 @@ class BareRequest extends Readable {
|
|
|
126
128
|
let start = 0;
|
|
127
129
|
const len = s.length;
|
|
128
130
|
while (start < len) {
|
|
129
|
-
const
|
|
130
|
-
if (
|
|
131
|
-
const
|
|
132
|
-
const valEnd =
|
|
133
|
-
const k = s.slice(start,
|
|
134
|
-
const v = s.slice(
|
|
131
|
+
const nlKey = s.indexOf('\n', start);
|
|
132
|
+
if (nlKey === -1) break;
|
|
133
|
+
const nlVal = s.indexOf('\n', nlKey + 1);
|
|
134
|
+
const valEnd = nlVal === -1 ? len : nlVal;
|
|
135
|
+
const k = s.slice(start, nlKey).toLowerCase();
|
|
136
|
+
const v = s.slice(nlKey + 1, valEnd);
|
|
135
137
|
obj[k] = v;
|
|
136
138
|
start = valEnd + 1;
|
|
137
139
|
}
|
|
@@ -146,13 +148,13 @@ class BareRequest extends Readable {
|
|
|
146
148
|
let start = 0;
|
|
147
149
|
const len = s.length;
|
|
148
150
|
while (start < len) {
|
|
149
|
-
const
|
|
150
|
-
if (
|
|
151
|
-
const
|
|
152
|
-
const valEnd =
|
|
153
|
-
const k = s.slice(start,
|
|
151
|
+
const nlKey = s.indexOf('\n', start);
|
|
152
|
+
if (nlKey === -1) break;
|
|
153
|
+
const nlVal = s.indexOf('\n', nlKey + 1);
|
|
154
|
+
const valEnd = nlVal === -1 ? len : nlVal;
|
|
155
|
+
const k = s.slice(start, nlKey);
|
|
154
156
|
if (k.toLowerCase() === target) {
|
|
155
|
-
return s.slice(
|
|
157
|
+
return s.slice(nlKey + 1, valEnd);
|
|
156
158
|
}
|
|
157
159
|
start = valEnd + 1;
|
|
158
160
|
}
|
|
@@ -228,6 +230,28 @@ class BareRequest extends Readable {
|
|
|
228
230
|
if (!ct) return false;
|
|
229
231
|
return ct.includes(type);
|
|
230
232
|
}
|
|
233
|
+
|
|
234
|
+
accepts(...types) {
|
|
235
|
+
const acc = this.get('accept');
|
|
236
|
+
if (!acc || acc === '*/*') return types[0] || false;
|
|
237
|
+
const flat = types.flat();
|
|
238
|
+
for (const t of flat) {
|
|
239
|
+
if (acc.includes(t)) return t;
|
|
240
|
+
}
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
get xhr() {
|
|
245
|
+
const val = this.get('x-requested-with') || '';
|
|
246
|
+
return val.toLowerCase() === 'xmlhttprequest';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
get subdomains() {
|
|
250
|
+
const hostname = this.hostname;
|
|
251
|
+
if (!hostname) return [];
|
|
252
|
+
const parts = hostname.split('.');
|
|
253
|
+
return parts.slice(0, -2).reverse();
|
|
254
|
+
}
|
|
231
255
|
}
|
|
232
256
|
|
|
233
257
|
class BareResponse {
|
|
@@ -415,8 +439,82 @@ class BareResponse {
|
|
|
415
439
|
this._server.native.respond(this._raw.reqId, this.statusCode, this.contentType, JSON.stringify(body), this._getHeadersArray());
|
|
416
440
|
}
|
|
417
441
|
|
|
418
|
-
|
|
419
|
-
|
|
442
|
+
attachment(filename) {
|
|
443
|
+
if (filename) {
|
|
444
|
+
const ext = path.extname(filename);
|
|
445
|
+
if (ext) this.type(ext.slice(1));
|
|
446
|
+
this.set('content-disposition', `attachment; filename="${path.basename(filename)}"`);
|
|
447
|
+
} else {
|
|
448
|
+
this.set('content-disposition', 'attachment');
|
|
449
|
+
}
|
|
450
|
+
return this;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
sendFile(filePath, options, callback) {
|
|
454
|
+
if (typeof options === 'function') {
|
|
455
|
+
callback = options;
|
|
456
|
+
options = {};
|
|
457
|
+
}
|
|
458
|
+
fs.readFile(filePath, (err, data) => {
|
|
459
|
+
if (err) {
|
|
460
|
+
if (callback) return callback(err);
|
|
461
|
+
return this.status(404).send('Not Found');
|
|
462
|
+
}
|
|
463
|
+
const ext = path.extname(filePath);
|
|
464
|
+
if (ext) this.type(ext.slice(1));
|
|
465
|
+
this.send(data);
|
|
466
|
+
if (callback) callback();
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
download(filePath, filename, options, callback) {
|
|
471
|
+
if (typeof filename === 'function') {
|
|
472
|
+
callback = filename;
|
|
473
|
+
filename = undefined;
|
|
474
|
+
options = {};
|
|
475
|
+
} else if (typeof options === 'function') {
|
|
476
|
+
callback = options;
|
|
477
|
+
options = {};
|
|
478
|
+
}
|
|
479
|
+
const name = filename || path.basename(filePath);
|
|
480
|
+
this.attachment(name);
|
|
481
|
+
return this.sendFile(filePath, options, callback);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
write(chunk, encoding, callback) {
|
|
485
|
+
if (this.responded) return false;
|
|
486
|
+
if (typeof encoding === 'function') {
|
|
487
|
+
callback = encoding;
|
|
488
|
+
encoding = undefined;
|
|
489
|
+
}
|
|
490
|
+
if (!this._chunks) this._chunks = [];
|
|
491
|
+
if (chunk !== undefined && chunk !== null) {
|
|
492
|
+
this._chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk), encoding));
|
|
493
|
+
}
|
|
494
|
+
if (callback) callback();
|
|
495
|
+
return true;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
end(data, encoding, callback) {
|
|
499
|
+
if (this.responded) return;
|
|
500
|
+
if (typeof data === 'function') {
|
|
501
|
+
callback = data;
|
|
502
|
+
data = undefined;
|
|
503
|
+
} else if (typeof encoding === 'function') {
|
|
504
|
+
callback = encoding;
|
|
505
|
+
encoding = undefined;
|
|
506
|
+
}
|
|
507
|
+
if (this._chunks && this._chunks.length > 0) {
|
|
508
|
+
if (data !== undefined && data !== null) {
|
|
509
|
+
this._chunks.push(Buffer.isBuffer(data) ? data : Buffer.from(String(data), encoding));
|
|
510
|
+
}
|
|
511
|
+
const combined = Buffer.concat(this._chunks);
|
|
512
|
+
this._chunks = null;
|
|
513
|
+
this.send(combined);
|
|
514
|
+
} else {
|
|
515
|
+
this.send(data || '');
|
|
516
|
+
}
|
|
517
|
+
if (callback) callback();
|
|
420
518
|
}
|
|
421
519
|
}
|
|
422
520
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bareserver",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
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.
|
|
15
|
-
"bareserver-darwin-x64": "0.1.
|
|
16
|
-
"bareserver-linux-x64-gnu": "0.1.
|
|
17
|
-
"bareserver-win32-x64-msvc": "0.1.
|
|
14
|
+
"bareserver-darwin-arm64": "0.1.5",
|
|
15
|
+
"bareserver-darwin-x64": "0.1.5",
|
|
16
|
+
"bareserver-linux-x64-gnu": "0.1.5",
|
|
17
|
+
"bareserver-win32-x64-msvc": "0.1.5"
|
|
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)",
|