bareserver 0.1.4 → 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 +100 -2
- 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 {
|
|
@@ -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)",
|