@tinyhttp/send
Advanced tools
Comparing version 0.0.3 to 0.0.4
# @tinyhttp/send | ||
## 0.0.4 | ||
### Patch Changes | ||
- Add jsdoc comments to sendFile | ||
## 0.0.3 | ||
@@ -4,0 +10,0 @@ |
@@ -128,13 +128,22 @@ import { format, parse } from 'es-content-type'; | ||
const sendFile = (_, res) => (path, options, cb) => { | ||
/** | ||
* Sends a file by piping a stream to response. | ||
* | ||
* It also checks for extension to set a proper `Content-Type` header. | ||
* | ||
* Path argument must be absolute. To use a relative path, specify the `root` option first. | ||
* | ||
* @param _ Request | ||
* @param res Response | ||
*/ | ||
const sendFile = (_, res) => (path, opts, cb) => { | ||
const { root, ...options } = opts; | ||
if (!path) { | ||
if (typeof path !== 'string') { | ||
if (typeof path !== 'string') | ||
throw new TypeError('path must be a string to res.sendFile'); | ||
} | ||
throw new TypeError('path argument is required to res.sendFile'); | ||
} | ||
if (!isAbsolute(path)) { | ||
if (!isAbsolute(path)) | ||
throw new TypeError('path must be absolute'); | ||
} | ||
const stream = createReadStream(path, options); | ||
const stream = createReadStream(root ? root + path : path, options); | ||
stream.on('error', (err) => void cb(err)); | ||
@@ -141,0 +150,0 @@ stream.on('end', () => void cb()); |
/// <reference types="node" /> | ||
import { IncomingMessage as I, ServerResponse as S } from 'http'; | ||
export declare type SendFileOptions = Partial<{ | ||
export declare type ReadStreamOptions = Partial<{ | ||
flags: string; | ||
encoding: BufferEncoding; | ||
fd: number; | ||
mode: number; | ||
autoClose: boolean; | ||
emitClose: boolean; | ||
start: number; | ||
end: number; | ||
highWaterMark: number; | ||
}>; | ||
export declare type SendFileOptions = ReadStreamOptions & Partial<{ | ||
root: string; | ||
}>; | ||
export declare type ReadStreamOptions = { | ||
flags?: string; | ||
encoding?: BufferEncoding; | ||
fd?: number; | ||
mode?: number; | ||
autoClose?: boolean; | ||
emitClose?: boolean; | ||
start?: number; | ||
end?: number; | ||
highWaterMark?: number; | ||
}; | ||
export declare const sendFile: <Request_1 extends I = I, Response_1 extends S = S>(_: Request_1, res: Response_1) => (path: string, options?: any, cb?: (err?: any) => void) => Response_1; | ||
/** | ||
* Sends a file by piping a stream to response. | ||
* | ||
* It also checks for extension to set a proper `Content-Type` header. | ||
* | ||
* Path argument must be absolute. To use a relative path, specify the `root` option first. | ||
* | ||
* @param _ Request | ||
* @param res Response | ||
*/ | ||
export declare const sendFile: <Request_1 extends I = I, Response_1 extends S = S>(_: Request_1, res: Response_1) => (path: string, opts: SendFileOptions, cb?: (err?: any) => void) => Response_1; |
{ | ||
"name": "@tinyhttp/send", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "json, send, sendFile, status and sendStatus methods for tinyhttp", |
@@ -49,3 +49,3 @@ # @tinyhttp/send | ||
### `status(number)` | ||
### `status(number)` [![][doc-badge]](https://tinyhttp.v1rtl.site/docs#resstatus) | ||
@@ -61,3 +61,3 @@ Sets the HTTP status for the response. It is a chainable alias of Node’s `response.statusCode`. | ||
### `sendStatus` | ||
### `sendStatus` [![][doc-badge]](https://tinyhttp.v1rtl.site/docs#ressendstatus) | ||
@@ -77,2 +77,14 @@ Sets the response HTTP status code to statusCode and send its string representation as the response body. | ||
### `sendFile` [![][doc-badge]](https://tinyhttp.v1rtl.site/docs#ressendfile) | ||
Sends a file by piping a stream to response. It also checks for extension to set a proper `Content-Type` header. | ||
> Path argument must be absolute. To use a relative path, specify the `root` option first. | ||
##### Example | ||
```js | ||
res.sendFile('song.mp3', { root: process.cwd() }, (err) => console.log(err)) | ||
``` | ||
## Example | ||
@@ -79,0 +91,0 @@ |
@@ -6,31 +6,40 @@ import { IncomingMessage as I, ServerResponse as S } from 'http' | ||
export type SendFileOptions = Partial<{ | ||
root: string | ||
export type ReadStreamOptions = Partial<{ | ||
flags: string | ||
encoding: BufferEncoding | ||
fd: number | ||
mode: number | ||
autoClose: boolean | ||
emitClose: boolean | ||
start: number | ||
end: number | ||
highWaterMark: number | ||
}> | ||
export type ReadStreamOptions = { | ||
flags?: string | ||
encoding?: BufferEncoding | ||
fd?: number | ||
mode?: number | ||
autoClose?: boolean | ||
emitClose?: boolean | ||
start?: number | ||
end?: number | ||
highWaterMark?: number | ||
} | ||
export type SendFileOptions = ReadStreamOptions & | ||
Partial<{ | ||
root: string | ||
}> | ||
export const sendFile = <Request extends I = I, Response extends S = S>(_: Request, res: Response) => (path: string, options?: any, cb?: (err?: any) => void) => { | ||
/** | ||
* Sends a file by piping a stream to response. | ||
* | ||
* It also checks for extension to set a proper `Content-Type` header. | ||
* | ||
* Path argument must be absolute. To use a relative path, specify the `root` option first. | ||
* | ||
* @param _ Request | ||
* @param res Response | ||
*/ | ||
export const sendFile = <Request extends I = I, Response extends S = S>(_: Request, res: Response) => (path: string, opts: SendFileOptions, cb?: (err?: any) => void) => { | ||
const { root, ...options } = opts | ||
if (!path) { | ||
if (typeof path !== 'string') { | ||
throw new TypeError('path must be a string to res.sendFile') | ||
} | ||
if (typeof path !== 'string') throw new TypeError('path must be a string to res.sendFile') | ||
throw new TypeError('path argument is required to res.sendFile') | ||
} | ||
if (!isAbsolute(path)) { | ||
throw new TypeError('path must be absolute') | ||
} | ||
if (!isAbsolute(path)) throw new TypeError('path must be absolute') | ||
const stream = createReadStream(path, options) | ||
const stream = createReadStream(root ? root + path : path, options) | ||
@@ -37,0 +46,0 @@ stream.on('error', (err) => void cb(err)) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25141
552
106