Comparing version 0.3.1 to 0.3.2
/// <reference types="node" /> | ||
import { ServerResponse } from 'http'; | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
import { TPathname } from '../../types'; | ||
export default function sendFile(res: ServerResponse, asset: TPathname, mime?: string): Promise<void>; | ||
export default function sendFile(req: IncomingMessage, res: ServerResponse, asset: TPathname, mime?: string): Promise<void>; |
@@ -19,13 +19,12 @@ "use strict"; | ||
const guess_mime_1 = __importDefault(require("../../util/guess-mime")); | ||
function sendFile(res, asset, mime) { | ||
function sendFile(req, res, asset, mime) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const location = path_1.default.join(process.cwd(), asset); | ||
try { | ||
if (!fs_1.default.statSync(location).isFile()) | ||
throw new Error(); | ||
const stats = getStats(location); | ||
res.setHeader('Content-Type', mime || (0, guess_mime_1.default)(asset)); | ||
res.setHeader('Content-Length', stats.size); | ||
if (req.method === 'HEAD') { | ||
res.end(); | ||
return; | ||
} | ||
catch (error) { | ||
throw ex_1.default.NotFound(); | ||
} | ||
res.setHeader('Content-Type', mime || (0, guess_mime_1.default)(asset)); | ||
try { | ||
@@ -51,1 +50,11 @@ yield new Promise((resolve, reject) => { | ||
exports.default = sendFile; | ||
function getStats(location) { | ||
try { | ||
const stats = fs_1.default.statSync(location); | ||
if (stats.isFile()) | ||
return stats; | ||
} | ||
catch (error) { | ||
} | ||
throw ex_1.default.NotFound(); | ||
} |
@@ -35,13 +35,6 @@ "use strict"; | ||
const asset = path_1.default.join(options.dir, params['**']); | ||
const mime = (0, guess_mime_1.default)(asset, options.mime); | ||
if (isExcluded(options.exclude, asset)) { | ||
throw ex_1.default.NotFound(); | ||
} | ||
if (req.method === 'HEAD') { | ||
res.setHeader('Content-Type', mime); | ||
res.end(); | ||
} | ||
else { | ||
yield (0, send_file_1.default)(res, asset, mime); | ||
} | ||
yield (0, send_file_1.default)(req, res, asset, (0, guess_mime_1.default)(asset, options.mime)); | ||
})); | ||
@@ -48,0 +41,0 @@ } |
@@ -31,5 +31,5 @@ "use strict"; | ||
(0, validate_1.validateType)(mime, 'Static file mime', 'string'); | ||
return (0, create_route_1.default)(url, ({ res }) => __awaiter(this, void 0, void 0, function* () { | ||
yield (0, send_file_1.default)(res, asset, mime); | ||
return (0, create_route_1.default)(url, ({ req, res }) => __awaiter(this, void 0, void 0, function* () { | ||
yield (0, send_file_1.default)(req, res, asset, mime); | ||
})); | ||
} |
{ | ||
"name": "kequapp", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Versatile, non-intrusive webapp framework", | ||
@@ -5,0 +5,0 @@ "main": "dist/main.js", |
@@ -20,4 +20,4 @@ <img alt="kequapp" src="https://github.com/Kequc/kequapp/blob/0.2-wip/logo.png?raw=true" width="142" height="85" /> | ||
* Async await everywhere | ||
* Handle any request and return any response | ||
* Does not modify Node features or functionality | ||
* Handle any request and return any response | ||
* Unit testing tool | ||
@@ -740,3 +740,3 @@ * No dependencies <3 | ||
If a mime type is not provided a `'Content-Type'` header is guessed from the file extension. | ||
If `mime` is not provided a `'Content-Type'` header is guessed from the file extension. | ||
@@ -750,4 +750,4 @@ # # sendFile() | ||
``` | ||
# sendFile(res: Res, asset: Pathname, mime: string): void; | ||
# sendFile(res: Res, asset: Pathname): void; | ||
# sendFile(req: Req, res: Res, asset: Pathname, mime: string): void; | ||
# sendFile(req: Req, res: Res, asset: Pathname): void; | ||
``` | ||
@@ -757,5 +757,5 @@ | ||
This is asyncronous and must be awaited otherwise the application might get confused as it continues processing the request. If a mime type is not provided a `'Content-Type'` header is guessed from the file extension. | ||
This is asyncronous and must be awaited otherwise the application might get confused as it continues processing the request. | ||
The following example is the same as the `staticFile()` example above. | ||
The following is the same as the `staticFile()` example above. | ||
@@ -766,4 +766,4 @@ ```javascript | ||
app.add( | ||
createRoute('/db.json', async ({ res }) => { | ||
await sendFile(res, '/db/my-db.json'); | ||
createRoute('/db.json', async ({ req, res }) => { | ||
await sendFile(req, res, '/db/my-db.json'); | ||
}) | ||
@@ -773,2 +773,4 @@ ); | ||
If `mime` is not provided a `'Content-Type'` header is guessed from the file extension. | ||
# Utilities | ||
@@ -775,0 +777,0 @@ |
111314
2045
884