UNPKG

7.36 kBTypeScriptView Raw
1// Type definitions for send 0.17
2// Project: https://github.com/pillarjs/send
3// Definitions by: Mike Jerred <https://github.com/MikeJerred>
4// Piotr Błażejewicz <https://github.com/peterblazejewicz>
5// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6
7/// <reference types="node" />
8
9import * as stream from "stream";
10import * as fs from "fs";
11import * as m from "mime";
12
13/**
14 * Create a new SendStream for the given path to send to a res.
15 * The req is the Node.js HTTP request and the path is a urlencoded path to send (urlencoded, not the actual file-system path).
16 */
17declare function send(req: stream.Readable, path: string, options?: send.SendOptions): send.SendStream;
18
19declare namespace send {
20 const mime: typeof m;
21 interface SendOptions {
22 /**
23 * Enable or disable accepting ranged requests, defaults to true.
24 * Disabling this will not send Accept-Ranges and ignore the contents of the Range request header.
25 */
26 acceptRanges?: boolean | undefined;
27
28 /**
29 * Enable or disable setting Cache-Control response header, defaults to true.
30 * Disabling this will ignore the maxAge option.
31 */
32 cacheControl?: boolean | undefined;
33
34 /**
35 * Set how "dotfiles" are treated when encountered.
36 * A dotfile is a file or directory that begins with a dot (".").
37 * Note this check is done on the path itself without checking if the path actually exists on the disk.
38 * If root is specified, only the dotfiles above the root are checked (i.e. the root itself can be within a dotfile when when set to "deny").
39 * 'allow' No special treatment for dotfiles.
40 * 'deny' Send a 403 for any request for a dotfile.
41 * 'ignore' Pretend like the dotfile does not exist and 404.
42 * The default value is similar to 'ignore', with the exception that this default will not ignore the files within a directory that begins with a dot, for backward-compatibility.
43 */
44 dotfiles?: "allow" | "deny" | "ignore" | undefined;
45
46 /**
47 * Byte offset at which the stream ends, defaults to the length of the file minus 1.
48 * The end is inclusive in the stream, meaning end: 3 will include the 4th byte in the stream.
49 */
50 end?: number | undefined;
51
52 /**
53 * Enable or disable etag generation, defaults to true.
54 */
55 etag?: boolean | undefined;
56
57 /**
58 * If a given file doesn't exist, try appending one of the given extensions, in the given order.
59 * By default, this is disabled (set to false).
60 * An example value that will serve extension-less HTML files: ['html', 'htm'].
61 * This is skipped if the requested file already has an extension.
62 */
63 extensions?: string[] | string | boolean | undefined;
64
65 /**
66 * Enable or disable the immutable directive in the Cache-Control response header, defaults to false.
67 * If set to true, the maxAge option should also be specified to enable caching.
68 * The immutable directive will prevent supported clients from making conditional requests during the life of the maxAge option to check if the file has changed.
69 * @default false
70 */
71 immutable?: boolean | undefined;
72
73 /**
74 * By default send supports "index.html" files, to disable this set false or to supply a new index pass a string or an array in preferred order.
75 */
76 index?: string[] | string | boolean | undefined;
77
78 /**
79 * Enable or disable Last-Modified header, defaults to true.
80 * Uses the file system's last modified value.
81 */
82 lastModified?: boolean | undefined;
83
84 /**
85 * Provide a max-age in milliseconds for http caching, defaults to 0.
86 * This can also be a string accepted by the ms module.
87 */
88 maxAge?: string | number | undefined;
89
90 /**
91 * Serve files relative to path.
92 */
93 root?: string | undefined;
94
95 /**
96 * Byte offset at which the stream starts, defaults to 0.
97 * The start is inclusive, meaning start: 2 will include the 3rd byte in the stream.
98 */
99 start?: number | undefined;
100 }
101
102 interface SendStream extends stream.Stream {
103 /**
104 * @deprecated pass etag as option
105 * Enable or disable etag generation.
106 */
107 etag(val: boolean): SendStream;
108
109 /**
110 * @deprecated use dotfiles option
111 * Enable or disable "hidden" (dot) files.
112 */
113 hidden(val: boolean): SendStream;
114
115 /**
116 * @deprecated pass index as option
117 * Set index `paths`, set to a falsy value to disable index support.
118 */
119 index(paths: string[] | string): SendStream;
120
121 /**
122 * @deprecated pass root as option
123 * Set root `path`.
124 */
125 root(paths: string): SendStream;
126
127 /**
128 * @deprecated pass root as option
129 * Set root `path`.
130 */
131 from(paths: string): SendStream;
132
133 /**
134 * @deprecated pass maxAge as option
135 * Set max-age to `maxAge`.
136 */
137 maxage(maxAge: string | number): SendStream;
138
139 /**
140 * Emit error with `status`.
141 */
142 error(status: number, error?: Error): void;
143
144 /**
145 * Check if the pathname ends with "/".
146 */
147 hasTrailingSlash(): boolean;
148
149 /**
150 * Check if this is a conditional GET request.
151 */
152 isConditionalGET(): boolean;
153
154 /**
155 * Strip content-* header fields.
156 */
157 removeContentHeaderFields(): void;
158
159 /**
160 * Respond with 304 not modified.
161 */
162 notModified(): void;
163
164 /**
165 * Raise error that headers already sent.
166 */
167 headersAlreadySent(): void;
168
169 /**
170 * Check if the request is cacheable, aka responded with 2xx or 304 (see RFC 2616 section 14.2{5,6}).
171 */
172 isCachable(): boolean;
173
174 /**
175 * Handle stat() error.
176 */
177 onStatError(error: Error): void;
178
179 /**
180 * Check if the cache is fresh.
181 */
182 isFresh(): boolean;
183
184 /**
185 * Check if the range is fresh.
186 */
187 isRangeFresh(): boolean;
188
189 /**
190 * Redirect to path.
191 */
192 redirect(path: string): void;
193
194 /**
195 * Pipe to `res`.
196 */
197 pipe<T extends NodeJS.WritableStream>(res: T): T;
198
199 /**
200 * Transfer `path`.
201 */
202 send(path: string, stat?: fs.Stats): void;
203
204 /**
205 * Transfer file for `path`.
206 */
207 sendFile(path: string): void;
208
209 /**
210 * Transfer index for `path`.
211 */
212 sendIndex(path: string): void;
213
214 /**
215 * Transfer index for `path`.
216 */
217 stream(path: string, options?: {}): void;
218
219 /**
220 * Set content-type based on `path` if it hasn't been explicitly set.
221 */
222 type(path: string): void;
223
224 /**
225 * Set response header fields, most fields may be pre-defined.
226 */
227 setHeader(path: string, stat: fs.Stats): void;
228 }
229}
230
231export = send;