@remix-run/static-middleware 0.0.0
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/LICENSE +22 -0
- package/README.md +89 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/static.d.ts +38 -0
- package/dist/lib/static.d.ts.map +1 -0
- package/dist/lib/static.js +44 -0
- package/package.json +51 -0
- package/src/index.ts +3 -0
- package/src/lib/static.ts +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Shopify Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# static-middleware
|
|
2
|
+
|
|
3
|
+
Middleware for serving static files from the filesystem for use with [`@remix-run/fetch-router`](https://github.com/remix-run/remix/tree/main/packages/fetch-router).
|
|
4
|
+
|
|
5
|
+
Serves static files from a directory with support for ETags, range requests, and conditional requests.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- [ETag support](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) (weak and strong)
|
|
10
|
+
- [Range request support](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests) (HTTP 206 Partial Content)
|
|
11
|
+
- [Conditional request support](https://developer.mozilla.org/en-US/docs/Web/HTTP/Conditional_requests) (If-None-Match, If-Modified-Since)
|
|
12
|
+
- Path traversal protection
|
|
13
|
+
- Automatic fall through to next middleware/handler if file not found
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @remix-run/static-middleware
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Static middleware is useful for serving static files from a directory.
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { createRouter } from '@remix-run/fetch-router'
|
|
27
|
+
import { staticFiles } from '@remix-run/static-middleware'
|
|
28
|
+
|
|
29
|
+
let router = createRouter({
|
|
30
|
+
middleware: [staticFiles('./public')],
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
router.get('/', () => new Response('Home'))
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### With Cache Control
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
let router = createRouter({
|
|
40
|
+
middleware: [
|
|
41
|
+
staticFiles('./public', {
|
|
42
|
+
cacheControl: 'public, max-age=31536000, immutable', // 1 year
|
|
43
|
+
}),
|
|
44
|
+
],
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Filter Files
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
let router = createRouter({
|
|
52
|
+
middleware: [
|
|
53
|
+
staticFiles('./public', {
|
|
54
|
+
filter(path) {
|
|
55
|
+
// Don't serve hidden files
|
|
56
|
+
return !path.startsWith('.')
|
|
57
|
+
},
|
|
58
|
+
}),
|
|
59
|
+
],
|
|
60
|
+
})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Multiple Directories
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
let router = createRouter({
|
|
67
|
+
middleware: [
|
|
68
|
+
staticFiles('./public'),
|
|
69
|
+
staticFiles('./assets', {
|
|
70
|
+
cacheControl: 'public, max-age=31536000',
|
|
71
|
+
}),
|
|
72
|
+
],
|
|
73
|
+
})
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Security
|
|
77
|
+
|
|
78
|
+
- Prevents path traversal attacks (e.g., `../../../etc/passwd`)
|
|
79
|
+
- Only serves files with GET and HEAD requests
|
|
80
|
+
- Respects the configured root directory boundary
|
|
81
|
+
|
|
82
|
+
## Related Packages
|
|
83
|
+
|
|
84
|
+
- [`fetch-router`](https://github.com/remix-run/remix/tree/main/packages/fetch-router) - Router for the web Fetch API
|
|
85
|
+
- [`lazy-file`](https://github.com/remix-run/remix/tree/main/packages/lazy-file) - Used internally for streaming file contents
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
See [LICENSE](https://github.com/remix-run/remix/blob/main/LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,YAAY,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { staticFiles } from "./lib/static.js";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type FileResponseOptions } from '@remix-run/fetch-router/response-helpers';
|
|
2
|
+
import type { Middleware } from '@remix-run/fetch-router';
|
|
3
|
+
export type StaticFilesOptions = FileResponseOptions & {
|
|
4
|
+
/**
|
|
5
|
+
* Filter function to determine which files should be served.
|
|
6
|
+
*
|
|
7
|
+
* @param path The relative path being requested
|
|
8
|
+
* @returns Whether to serve the file
|
|
9
|
+
*/
|
|
10
|
+
filter?: (path: string) => boolean;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Creates a middleware that serves static files from the filesystem.
|
|
14
|
+
*
|
|
15
|
+
* Uses the URL pathname to resolve files, removing the leading slash to make it
|
|
16
|
+
* a relative path. The middleware always falls through to the handler if the file
|
|
17
|
+
* is not found or an error occurs.
|
|
18
|
+
*
|
|
19
|
+
* @param root The root directory to serve files from (absolute or relative to cwd)
|
|
20
|
+
* @param options (optional) configuration for file responses
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* let router = createRouter({
|
|
24
|
+
* middleware: [staticFiles('./public')],
|
|
25
|
+
* })
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* // With cache control
|
|
29
|
+
* let router = createRouter({
|
|
30
|
+
* middleware: [
|
|
31
|
+
* staticFiles('./public', {
|
|
32
|
+
* cacheControl: 'public, max-age=3600',
|
|
33
|
+
* }),
|
|
34
|
+
* ],
|
|
35
|
+
* })
|
|
36
|
+
*/
|
|
37
|
+
export declare function staticFiles(root: string, options?: StaticFilesOptions): Middleware;
|
|
38
|
+
//# sourceMappingURL=static.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"static.d.ts","sourceRoot":"","sources":["../../src/lib/static.ts"],"names":[],"mappings":"AAEA,OAAO,EAAQ,KAAK,mBAAmB,EAAE,MAAM,0CAA0C,CAAA;AACzF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAEzD,MAAM,MAAM,kBAAkB,GAAG,mBAAmB,GAAG;IACrD;;;;;OAKG;IACH,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAA;CACnC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,UAAU,CAsBtF"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { findFile } from '@remix-run/lazy-file/fs';
|
|
2
|
+
import { file } from '@remix-run/fetch-router/response-helpers';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a middleware that serves static files from the filesystem.
|
|
5
|
+
*
|
|
6
|
+
* Uses the URL pathname to resolve files, removing the leading slash to make it
|
|
7
|
+
* a relative path. The middleware always falls through to the handler if the file
|
|
8
|
+
* is not found or an error occurs.
|
|
9
|
+
*
|
|
10
|
+
* @param root The root directory to serve files from (absolute or relative to cwd)
|
|
11
|
+
* @param options (optional) configuration for file responses
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* let router = createRouter({
|
|
15
|
+
* middleware: [staticFiles('./public')],
|
|
16
|
+
* })
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // With cache control
|
|
20
|
+
* let router = createRouter({
|
|
21
|
+
* middleware: [
|
|
22
|
+
* staticFiles('./public', {
|
|
23
|
+
* cacheControl: 'public, max-age=3600',
|
|
24
|
+
* }),
|
|
25
|
+
* ],
|
|
26
|
+
* })
|
|
27
|
+
*/
|
|
28
|
+
export function staticFiles(root, options = {}) {
|
|
29
|
+
let { filter, ...fileOptions } = options;
|
|
30
|
+
return async (context, next) => {
|
|
31
|
+
if (context.request.method !== 'GET' && context.request.method !== 'HEAD') {
|
|
32
|
+
return next();
|
|
33
|
+
}
|
|
34
|
+
let relativePath = context.url.pathname.replace(/^\/+/, '');
|
|
35
|
+
if (filter && !filter(relativePath)) {
|
|
36
|
+
return next();
|
|
37
|
+
}
|
|
38
|
+
let fileToServe = await findFile(root, relativePath);
|
|
39
|
+
if (!fileToServe) {
|
|
40
|
+
return next();
|
|
41
|
+
}
|
|
42
|
+
return file(fileToServe, context.request, fileOptions);
|
|
43
|
+
};
|
|
44
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@remix-run/static-middleware",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Middleware for serving static files from the filesystem",
|
|
5
|
+
"author": "Michael Jackson <mjijackson@gmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/remix-run/remix.git",
|
|
10
|
+
"directory": "packages/static-middleware"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/remix-run/remix/tree/main/packages/static-middleware#readme",
|
|
13
|
+
"files": [
|
|
14
|
+
"LICENSE",
|
|
15
|
+
"README.md",
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"!src/**/*.test.ts"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^24.6.0",
|
|
30
|
+
"typescript": "^5.9.3",
|
|
31
|
+
"@remix-run/fetch-router": "0.9.0",
|
|
32
|
+
"@remix-run/lazy-file": "3.8.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@remix-run/fetch-router": "^0.9.0",
|
|
36
|
+
"@remix-run/lazy-file": "^3.8.0"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"fetch",
|
|
40
|
+
"router",
|
|
41
|
+
"middleware",
|
|
42
|
+
"static-files",
|
|
43
|
+
"file-server"
|
|
44
|
+
],
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsc -p tsconfig.build.json",
|
|
47
|
+
"clean": "git clean -fdX",
|
|
48
|
+
"test": "node --disable-warning=ExperimentalWarning --test './src/**/*.test.ts'",
|
|
49
|
+
"typecheck": "tsc --noEmit"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { findFile } from '@remix-run/lazy-file/fs'
|
|
2
|
+
|
|
3
|
+
import { file, type FileResponseOptions } from '@remix-run/fetch-router/response-helpers'
|
|
4
|
+
import type { Middleware } from '@remix-run/fetch-router'
|
|
5
|
+
|
|
6
|
+
export type StaticFilesOptions = FileResponseOptions & {
|
|
7
|
+
/**
|
|
8
|
+
* Filter function to determine which files should be served.
|
|
9
|
+
*
|
|
10
|
+
* @param path The relative path being requested
|
|
11
|
+
* @returns Whether to serve the file
|
|
12
|
+
*/
|
|
13
|
+
filter?: (path: string) => boolean
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Creates a middleware that serves static files from the filesystem.
|
|
18
|
+
*
|
|
19
|
+
* Uses the URL pathname to resolve files, removing the leading slash to make it
|
|
20
|
+
* a relative path. The middleware always falls through to the handler if the file
|
|
21
|
+
* is not found or an error occurs.
|
|
22
|
+
*
|
|
23
|
+
* @param root The root directory to serve files from (absolute or relative to cwd)
|
|
24
|
+
* @param options (optional) configuration for file responses
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* let router = createRouter({
|
|
28
|
+
* middleware: [staticFiles('./public')],
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // With cache control
|
|
33
|
+
* let router = createRouter({
|
|
34
|
+
* middleware: [
|
|
35
|
+
* staticFiles('./public', {
|
|
36
|
+
* cacheControl: 'public, max-age=3600',
|
|
37
|
+
* }),
|
|
38
|
+
* ],
|
|
39
|
+
* })
|
|
40
|
+
*/
|
|
41
|
+
export function staticFiles(root: string, options: StaticFilesOptions = {}): Middleware {
|
|
42
|
+
let { filter, ...fileOptions } = options
|
|
43
|
+
|
|
44
|
+
return async (context, next) => {
|
|
45
|
+
if (context.request.method !== 'GET' && context.request.method !== 'HEAD') {
|
|
46
|
+
return next()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let relativePath = context.url.pathname.replace(/^\/+/, '')
|
|
50
|
+
|
|
51
|
+
if (filter && !filter(relativePath)) {
|
|
52
|
+
return next()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let fileToServe = await findFile(root, relativePath)
|
|
56
|
+
|
|
57
|
+
if (!fileToServe) {
|
|
58
|
+
return next()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return file(fileToServe, context.request, fileOptions)
|
|
62
|
+
}
|
|
63
|
+
}
|