@vyriy/static 0.5.2
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/AGENTS.md +102 -0
- package/LICENSE +21 -0
- package/README.md +49 -0
- package/bin/vyriy-static.js +6 -0
- package/index.d.ts +6 -0
- package/index.js +5 -0
- package/package.json +88 -0
- package/server.d.ts +2 -0
- package/server.js +15 -0
- package/types.d.ts +15 -0
- package/use-spa.d.ts +2 -0
- package/use-spa.js +17 -0
- package/use-static.d.ts +2 -0
- package/use-static.js +170 -0
- package/with-spa.d.ts +3 -0
- package/with-spa.js +41 -0
- package/with-static.d.ts +3 -0
- package/with-static.js +72 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# Project Agent Guide
|
|
2
|
+
|
|
3
|
+
This repository follows a calm engineering style: changes should be explicit, reusable, typed, documented, tested, and easy to reason about.
|
|
4
|
+
|
|
5
|
+
Use this guide as the default behavior for AI agents and contributors working in this repository. Prefer local package conventions when they are more specific than this document.
|
|
6
|
+
|
|
7
|
+
## Core Principles
|
|
8
|
+
|
|
9
|
+
- Prefer simple modules over clever frameworks or hidden conventions.
|
|
10
|
+
- Keep package and project boundaries explicit.
|
|
11
|
+
- Avoid project-specific coupling in reusable code.
|
|
12
|
+
- Extract only proven reusable behavior.
|
|
13
|
+
- Keep public APIs small, typed, documented, and stable.
|
|
14
|
+
- Prefer SSR-friendly and SSG-friendly code paths when working with frontend or shared code.
|
|
15
|
+
- Keep integrations replaceable and avoid hard coupling to a CMS, framework, vendor, or runtime host.
|
|
16
|
+
- Prefer infrastructure assumptions that are easy to deploy, observe, and replace.
|
|
17
|
+
- Prefer the option that is simpler to explain, easier to evolve, and calmer to maintain.
|
|
18
|
+
|
|
19
|
+
## File Shape
|
|
20
|
+
|
|
21
|
+
- Prefer one exported runtime method, component, helper, or class per production file when it stays readable.
|
|
22
|
+
- Prefer one matching test file per production file, for example `feature.ts` and `feature.test.ts`.
|
|
23
|
+
- Use focused folders when behavior naturally splits into several related files.
|
|
24
|
+
- Keep `index.ts` as a public re-export surface only. Do not place implementation logic in it.
|
|
25
|
+
- Use relative import and export specifiers that match the package module style.
|
|
26
|
+
- Use `.js` relative specifiers in TypeScript source for ESM/NodeNext packages.
|
|
27
|
+
- Add `types.ts` when public shared types are part of the package contract.
|
|
28
|
+
- Keep constants near the code that owns them unless they are shared or clarify repeated behavior.
|
|
29
|
+
|
|
30
|
+
## Public Surface
|
|
31
|
+
|
|
32
|
+
- Every new public export must be re-exported from the package or module public entry point.
|
|
33
|
+
- Add or update public-surface tests when exports change.
|
|
34
|
+
- Add JSDoc for public exports when behavior, parameters, return values, or usage expectations need explanation.
|
|
35
|
+
- Avoid exporting internal helpers only to make tests easier.
|
|
36
|
+
- Do not hand-maintain package `exports` maps unless the project has a real custom publishing need.
|
|
37
|
+
|
|
38
|
+
## Tests
|
|
39
|
+
|
|
40
|
+
- Cover public behavior and meaningful edge cases.
|
|
41
|
+
- Prefer behavior-focused tests over private implementation lock-in.
|
|
42
|
+
- Keep tests deterministic.
|
|
43
|
+
- Avoid real network, filesystem, timers, browser, or cloud dependencies unless the behavior specifically requires them.
|
|
44
|
+
- When mocking modules, install mocks before loading the module under test.
|
|
45
|
+
- Use focused validation when changing behavior.
|
|
46
|
+
|
|
47
|
+
Example validation commands:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
yarn test
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
For workspaces, prefer the project convention, for example:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
yarn workspace <package-name> test
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
For Jest-based packages, focused validation may look like:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
yarn jest packages/<package> --runInBand --coverage=false
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Documentation
|
|
66
|
+
|
|
67
|
+
- Keep `README.md` concise and usage-oriented.
|
|
68
|
+
- Start package READMEs with `# <package>`.
|
|
69
|
+
- Document real public exports, supported options, and examples that actually work.
|
|
70
|
+
- Update docs when public behavior changes.
|
|
71
|
+
- Keep generated docs wrappers, such as `doc.mdx`, aligned with the README when the project uses them.
|
|
72
|
+
- For component packages, include visual documentation or stories for supported states and common usage.
|
|
73
|
+
|
|
74
|
+
## Components
|
|
75
|
+
|
|
76
|
+
- Prefer lightweight React components with TypeScript when working in React packages.
|
|
77
|
+
- Keep components SSR-friendly and avoid browser globals during render.
|
|
78
|
+
- Prefer composable props and predictable ergonomics.
|
|
79
|
+
- Put each public component in its own file with a matching test.
|
|
80
|
+
- Add stories or examples when a component has visual states, variants, or interaction states.
|
|
81
|
+
- Keep styling explicit and reusable. Avoid hidden theme assumptions unless they are part of the package contract.
|
|
82
|
+
|
|
83
|
+
## Change Discipline
|
|
84
|
+
|
|
85
|
+
- Keep changes scoped to the requested behavior.
|
|
86
|
+
- Avoid unrelated refactors and metadata churn.
|
|
87
|
+
- Sync implementation, tests, docs, examples, and public re-exports together.
|
|
88
|
+
- Do not introduce new dependencies unless they clearly reduce complexity or are already part of the project direction.
|
|
89
|
+
- Prefer small, reviewable changes over broad rewrites.
|
|
90
|
+
- Preserve existing conventions unless there is a clear reason to change them.
|
|
91
|
+
|
|
92
|
+
## Before Finishing
|
|
93
|
+
|
|
94
|
+
Check that the change is complete:
|
|
95
|
+
|
|
96
|
+
- Public exports are updated.
|
|
97
|
+
- Public-surface tests are updated when exports change.
|
|
98
|
+
- Matching unit tests exist for new behavior.
|
|
99
|
+
- README examples still match the real API.
|
|
100
|
+
- Visual docs, stories, or examples are updated for visible component behavior.
|
|
101
|
+
- TypeScript imports follow the package module style.
|
|
102
|
+
- No unrelated files, formatting churn, or generated artifacts were changed.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vyriy contributors
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @vyriy/static
|
|
2
|
+
|
|
3
|
+
Static file and SPA serving CLI with reusable Lambda-style handlers and router helpers.
|
|
4
|
+
|
|
5
|
+
## CLI
|
|
6
|
+
|
|
7
|
+
Install globally:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install --global @vyriy/static
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Serve a static directory:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
vyriy-static
|
|
17
|
+
vyriy-static dist
|
|
18
|
+
vyriy-static build
|
|
19
|
+
vyriy-static public
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
When no directory is provided, the server tries `dist`, `build`, `public`, `out`, and then the current directory.
|
|
23
|
+
|
|
24
|
+
## API
|
|
25
|
+
|
|
26
|
+
Install as a project dependency:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @vyriy/static
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Use handlers and router helpers from code:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { staticServer, useSpa, useStatic, withSpa, withStatic } from '@vyriy/static';
|
|
36
|
+
import { createRouter } from '@vyriy/router';
|
|
37
|
+
|
|
38
|
+
export const assets = useStatic();
|
|
39
|
+
export const app = useSpa({ directory: 'dist', index: 'index.html' });
|
|
40
|
+
|
|
41
|
+
export const router = withStatic(createRouter(), { directory: 'dist' }).static('/');
|
|
42
|
+
export const spaRouter = withSpa(createRouter(), 'dist');
|
|
43
|
+
|
|
44
|
+
const code = await staticServer({ directory: 'dist' });
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`useStatic({ directory, index, error })` serves files from a directory. `useSpa(options)` serves static files and falls back to the configured index file for missing `GET` and `HEAD` paths. `withStatic(router, options).static(path)` adds prefix-based static mounts. `withSpa(router, directoryOrOptions)` adds static-first SPA fallback behavior.
|
|
48
|
+
|
|
49
|
+
Defaults are `directory: 'dist'`, `index: 'index.html'`, and `error: '404.html'`.
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vyriy/static",
|
|
3
|
+
"version": "0.5.2",
|
|
4
|
+
"description": "Static file and SPA serving helpers for Vyriy servers.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": "./bin/vyriy-static.js",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@vyriy/handler": "0.5.2",
|
|
9
|
+
"@vyriy/router": "0.5.2",
|
|
10
|
+
"@vyriy/server": "0.5.2"
|
|
11
|
+
},
|
|
12
|
+
"agents": "./AGENTS.md",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/evheniy/vyriy",
|
|
17
|
+
"directory": "packages/static"
|
|
18
|
+
},
|
|
19
|
+
"main": "./index.js",
|
|
20
|
+
"types": "./index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./index.d.ts",
|
|
24
|
+
"import": "./index.js",
|
|
25
|
+
"default": "./index.js"
|
|
26
|
+
},
|
|
27
|
+
"./index": {
|
|
28
|
+
"types": "./index.d.ts",
|
|
29
|
+
"import": "./index.js",
|
|
30
|
+
"default": "./index.js"
|
|
31
|
+
},
|
|
32
|
+
"./index.js": {
|
|
33
|
+
"types": "./index.d.ts",
|
|
34
|
+
"import": "./index.js",
|
|
35
|
+
"default": "./index.js"
|
|
36
|
+
},
|
|
37
|
+
"./server": {
|
|
38
|
+
"types": "./server.d.ts",
|
|
39
|
+
"import": "./server.js",
|
|
40
|
+
"default": "./server.js"
|
|
41
|
+
},
|
|
42
|
+
"./server.js": {
|
|
43
|
+
"types": "./server.d.ts",
|
|
44
|
+
"import": "./server.js",
|
|
45
|
+
"default": "./server.js"
|
|
46
|
+
},
|
|
47
|
+
"./use-spa": {
|
|
48
|
+
"types": "./use-spa.d.ts",
|
|
49
|
+
"import": "./use-spa.js",
|
|
50
|
+
"default": "./use-spa.js"
|
|
51
|
+
},
|
|
52
|
+
"./use-spa.js": {
|
|
53
|
+
"types": "./use-spa.d.ts",
|
|
54
|
+
"import": "./use-spa.js",
|
|
55
|
+
"default": "./use-spa.js"
|
|
56
|
+
},
|
|
57
|
+
"./use-static": {
|
|
58
|
+
"types": "./use-static.d.ts",
|
|
59
|
+
"import": "./use-static.js",
|
|
60
|
+
"default": "./use-static.js"
|
|
61
|
+
},
|
|
62
|
+
"./use-static.js": {
|
|
63
|
+
"types": "./use-static.d.ts",
|
|
64
|
+
"import": "./use-static.js",
|
|
65
|
+
"default": "./use-static.js"
|
|
66
|
+
},
|
|
67
|
+
"./with-spa": {
|
|
68
|
+
"types": "./with-spa.d.ts",
|
|
69
|
+
"import": "./with-spa.js",
|
|
70
|
+
"default": "./with-spa.js"
|
|
71
|
+
},
|
|
72
|
+
"./with-spa.js": {
|
|
73
|
+
"types": "./with-spa.d.ts",
|
|
74
|
+
"import": "./with-spa.js",
|
|
75
|
+
"default": "./with-spa.js"
|
|
76
|
+
},
|
|
77
|
+
"./with-static": {
|
|
78
|
+
"types": "./with-static.d.ts",
|
|
79
|
+
"import": "./with-static.js",
|
|
80
|
+
"default": "./with-static.js"
|
|
81
|
+
},
|
|
82
|
+
"./with-static.js": {
|
|
83
|
+
"types": "./with-static.d.ts",
|
|
84
|
+
"import": "./with-static.js",
|
|
85
|
+
"default": "./with-static.js"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
package/server.d.ts
ADDED
package/server.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { server } from '@vyriy/server';
|
|
3
|
+
import { useStatic } from './use-static.js';
|
|
4
|
+
const DIRECTORIES = [
|
|
5
|
+
'dist',
|
|
6
|
+
'build',
|
|
7
|
+
'public',
|
|
8
|
+
'out',
|
|
9
|
+
];
|
|
10
|
+
const resolveDirectory = (directory) => directory ?? DIRECTORIES.find((candidate) => existsSync(candidate)) ?? '.';
|
|
11
|
+
const createStaticHandler = useStatic;
|
|
12
|
+
export const staticServer = async (options = {}) => {
|
|
13
|
+
await Promise.resolve(server(createStaticHandler({ ...options, directory: resolveDirectory(options.directory) })));
|
|
14
|
+
return 0;
|
|
15
|
+
};
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ApiEvent, ApiResult, Handler } from '@vyriy/handler';
|
|
2
|
+
import type { RouterApi } from '@vyriy/router';
|
|
3
|
+
export type StaticServerOptions = StaticOptions;
|
|
4
|
+
export type StaticOptions = {
|
|
5
|
+
readonly directory?: string;
|
|
6
|
+
readonly index?: string;
|
|
7
|
+
readonly error?: string;
|
|
8
|
+
};
|
|
9
|
+
export type StaticHandler = Handler<ApiEvent, ApiResult>;
|
|
10
|
+
export type StaticServer = (options?: StaticServerOptions) => Promise<number>;
|
|
11
|
+
export type StaticRouterApi = Omit<RouterApi, 'delete' | 'fallback' | 'get' | 'patch' | 'post' | 'put'> & {
|
|
12
|
+
static(path: string): StaticRouterApi;
|
|
13
|
+
} & {
|
|
14
|
+
[Method in Extract<keyof RouterApi, 'delete' | 'fallback' | 'get' | 'patch' | 'post' | 'put'>]: (...args: Parameters<RouterApi[Method]>) => StaticRouterApi;
|
|
15
|
+
};
|
package/use-spa.d.ts
ADDED
package/use-spa.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useStatic } from './use-static.js';
|
|
2
|
+
const createStaticHandler = useStatic;
|
|
3
|
+
const isSpaFallbackMethod = (method) => method === 'GET' || method === 'HEAD';
|
|
4
|
+
export const useSpa = (options = {}) => {
|
|
5
|
+
const handler = createStaticHandler(options);
|
|
6
|
+
const index = options.index ?? 'index.html';
|
|
7
|
+
return async (event, context) => {
|
|
8
|
+
const result = await handler(event, context);
|
|
9
|
+
if (result.statusCode !== 404 || !isSpaFallbackMethod(event.httpMethod)) {
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
return handler({
|
|
13
|
+
...event,
|
|
14
|
+
path: `/${index}`,
|
|
15
|
+
}, context);
|
|
16
|
+
};
|
|
17
|
+
};
|
package/use-static.d.ts
ADDED
package/use-static.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { readFile, realpath, stat } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
const DEFAULT_DIRECTORY = 'dist';
|
|
4
|
+
const DEFAULT_INDEX = 'index.html';
|
|
5
|
+
const DEFAULT_ERROR = '404.html';
|
|
6
|
+
const NOT_FOUND_BODY = JSON.stringify({ message: 'Not Found' });
|
|
7
|
+
const METHOD_NOT_ALLOWED_BODY = JSON.stringify({ message: 'Method Not Allowed' });
|
|
8
|
+
const CONTENT_TYPES = {
|
|
9
|
+
'.css': 'text/css; charset=utf-8',
|
|
10
|
+
'.csv': 'text/csv; charset=utf-8',
|
|
11
|
+
'.gif': 'image/gif',
|
|
12
|
+
'.html': 'text/html; charset=utf-8',
|
|
13
|
+
'.ico': 'image/x-icon',
|
|
14
|
+
'.jpeg': 'image/jpeg',
|
|
15
|
+
'.jpg': 'image/jpeg',
|
|
16
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
17
|
+
'.json': 'application/json; charset=utf-8',
|
|
18
|
+
'.map': 'application/json; charset=utf-8',
|
|
19
|
+
'.mjs': 'text/javascript; charset=utf-8',
|
|
20
|
+
'.pdf': 'application/pdf',
|
|
21
|
+
'.png': 'image/png',
|
|
22
|
+
'.svg': 'image/svg+xml; charset=utf-8',
|
|
23
|
+
'.txt': 'text/plain; charset=utf-8',
|
|
24
|
+
'.webp': 'image/webp',
|
|
25
|
+
'.woff': 'font/woff',
|
|
26
|
+
'.woff2': 'font/woff2',
|
|
27
|
+
'.xml': 'application/xml; charset=utf-8',
|
|
28
|
+
};
|
|
29
|
+
const TEXT_TYPES = new Set([
|
|
30
|
+
'.css',
|
|
31
|
+
'.csv',
|
|
32
|
+
'.html',
|
|
33
|
+
'.js',
|
|
34
|
+
'.json',
|
|
35
|
+
'.map',
|
|
36
|
+
'.mjs',
|
|
37
|
+
'.svg',
|
|
38
|
+
'.txt',
|
|
39
|
+
'.xml',
|
|
40
|
+
]);
|
|
41
|
+
const getContentType = (extension) => CONTENT_TYPES[extension.toLowerCase()] ?? 'application/octet-stream';
|
|
42
|
+
const isTextExtension = (extension) => TEXT_TYPES.has(extension.toLowerCase());
|
|
43
|
+
const notFound = (body = NOT_FOUND_BODY, headers) => ({
|
|
44
|
+
statusCode: 404,
|
|
45
|
+
body,
|
|
46
|
+
headers: headers ?? {
|
|
47
|
+
'content-type': 'application/json; charset=utf-8',
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
const methodNotAllowed = () => ({
|
|
51
|
+
statusCode: 405,
|
|
52
|
+
body: METHOD_NOT_ALLOWED_BODY,
|
|
53
|
+
headers: {
|
|
54
|
+
allow: 'GET, HEAD',
|
|
55
|
+
'content-type': 'application/json; charset=utf-8',
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
const isStaticMethod = (method) => method === 'GET' || method === 'HEAD';
|
|
59
|
+
const isInsideDirectory = (directory, candidate) => {
|
|
60
|
+
const relative = path.relative(directory, candidate);
|
|
61
|
+
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
|
|
62
|
+
};
|
|
63
|
+
const normalizeOptions = ({ directory = DEFAULT_DIRECTORY, error = DEFAULT_ERROR, index = DEFAULT_INDEX, }) => ({
|
|
64
|
+
directory,
|
|
65
|
+
error,
|
|
66
|
+
index,
|
|
67
|
+
});
|
|
68
|
+
const createFileResult = async (realFilePath, fileSize, method) => {
|
|
69
|
+
const extension = path.extname(realFilePath);
|
|
70
|
+
const isText = isTextExtension(extension);
|
|
71
|
+
if (method === 'HEAD') {
|
|
72
|
+
return {
|
|
73
|
+
statusCode: 200,
|
|
74
|
+
body: '',
|
|
75
|
+
headers: {
|
|
76
|
+
'content-length': String(fileSize),
|
|
77
|
+
'content-type': getContentType(extension),
|
|
78
|
+
},
|
|
79
|
+
isBase64Encoded: false,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
const content = await readFile(realFilePath);
|
|
83
|
+
return {
|
|
84
|
+
statusCode: 200,
|
|
85
|
+
body: content.toString(isText ? 'utf8' : 'base64'),
|
|
86
|
+
headers: {
|
|
87
|
+
'content-length': String(fileSize),
|
|
88
|
+
'content-type': getContentType(extension),
|
|
89
|
+
},
|
|
90
|
+
isBase64Encoded: !isText,
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
const readStaticFile = async (root, filePath, method) => {
|
|
94
|
+
if (!isInsideDirectory(root, filePath)) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
const realFilePath = await realpath(filePath);
|
|
98
|
+
if (!isInsideDirectory(root, realFilePath)) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
const fileStat = await stat(realFilePath);
|
|
102
|
+
if (!fileStat.isFile()) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
return createFileResult(realFilePath, fileStat.size, method);
|
|
106
|
+
};
|
|
107
|
+
const readErrorFile = async (root, error, method) => {
|
|
108
|
+
try {
|
|
109
|
+
const result = await readStaticFile(root, path.resolve(root, error), method);
|
|
110
|
+
if (!result) {
|
|
111
|
+
return notFound();
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
...result,
|
|
115
|
+
statusCode: 404,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return notFound();
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
export const useStatic = (options = {}) => {
|
|
123
|
+
const { directory, error, index } = normalizeOptions(options);
|
|
124
|
+
let root;
|
|
125
|
+
const getRoot = () => (root ??= realpath(directory));
|
|
126
|
+
return async (event) => {
|
|
127
|
+
if (!isStaticMethod(event.httpMethod)) {
|
|
128
|
+
return methodNotAllowed();
|
|
129
|
+
}
|
|
130
|
+
let decodedPath;
|
|
131
|
+
try {
|
|
132
|
+
decodedPath = decodeURIComponent(event.path);
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return notFound();
|
|
136
|
+
}
|
|
137
|
+
let staticRoot;
|
|
138
|
+
try {
|
|
139
|
+
staticRoot = await getRoot();
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
return notFound();
|
|
143
|
+
}
|
|
144
|
+
const requestedPath = decodedPath.replace(/^\/+/, '') || index;
|
|
145
|
+
const candidate = path.resolve(staticRoot, requestedPath);
|
|
146
|
+
if (!isInsideDirectory(staticRoot, candidate)) {
|
|
147
|
+
return readErrorFile(staticRoot, error, event.httpMethod);
|
|
148
|
+
}
|
|
149
|
+
let filePath = candidate;
|
|
150
|
+
try {
|
|
151
|
+
const fileStat = await stat(filePath);
|
|
152
|
+
if (fileStat.isDirectory()) {
|
|
153
|
+
filePath = path.join(filePath, index);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return readErrorFile(staticRoot, error, event.httpMethod);
|
|
158
|
+
}
|
|
159
|
+
if (!isInsideDirectory(staticRoot, filePath)) {
|
|
160
|
+
return readErrorFile(staticRoot, error, event.httpMethod);
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
const result = await readStaticFile(staticRoot, filePath, event.httpMethod);
|
|
164
|
+
return result ?? readErrorFile(staticRoot, error, event.httpMethod);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return readErrorFile(staticRoot, error, event.httpMethod);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
};
|
package/with-spa.d.ts
ADDED
package/with-spa.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useSpa } from './use-spa.js';
|
|
2
|
+
const createSpaHandler = useSpa;
|
|
3
|
+
const isStaticMethod = (method) => method === 'GET' || method === 'HEAD';
|
|
4
|
+
export const withSpa = (router, directoryOrOptions = {}) => {
|
|
5
|
+
const options = typeof directoryOrOptions === 'string' ? { directory: directoryOrOptions } : directoryOrOptions;
|
|
6
|
+
const spaHandler = createSpaHandler(options);
|
|
7
|
+
const api = {
|
|
8
|
+
delete(path, handler) {
|
|
9
|
+
router.delete(path, handler);
|
|
10
|
+
return api;
|
|
11
|
+
},
|
|
12
|
+
fallback(handler) {
|
|
13
|
+
router.fallback(handler);
|
|
14
|
+
return api;
|
|
15
|
+
},
|
|
16
|
+
get(path, handler) {
|
|
17
|
+
router.get(path, handler);
|
|
18
|
+
return api;
|
|
19
|
+
},
|
|
20
|
+
patch(path, handler) {
|
|
21
|
+
router.patch(path, handler);
|
|
22
|
+
return api;
|
|
23
|
+
},
|
|
24
|
+
post(path, handler) {
|
|
25
|
+
router.post(path, handler);
|
|
26
|
+
return api;
|
|
27
|
+
},
|
|
28
|
+
put(path, handler) {
|
|
29
|
+
router.put(path, handler);
|
|
30
|
+
return api;
|
|
31
|
+
},
|
|
32
|
+
route: async (event) => {
|
|
33
|
+
const result = await router.route(event);
|
|
34
|
+
if (result.statusCode !== 404 || !isStaticMethod(event.httpMethod)) {
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
return spaHandler(event, {});
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
return api;
|
|
41
|
+
};
|
package/with-static.d.ts
ADDED
package/with-static.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { useStatic } from './use-static.js';
|
|
2
|
+
const normalizeMount = (path) => {
|
|
3
|
+
const mount = path.startsWith('/') ? path : `/${path}`;
|
|
4
|
+
return mount === '/' ? mount : mount.replace(/\/+$/, '');
|
|
5
|
+
};
|
|
6
|
+
const toStaticPath = (mount, requestPath) => {
|
|
7
|
+
if (mount === '/') {
|
|
8
|
+
return requestPath;
|
|
9
|
+
}
|
|
10
|
+
if (requestPath === mount) {
|
|
11
|
+
return '/';
|
|
12
|
+
}
|
|
13
|
+
if (requestPath.startsWith(`${mount}/`)) {
|
|
14
|
+
return requestPath.slice(mount.length);
|
|
15
|
+
}
|
|
16
|
+
return undefined;
|
|
17
|
+
};
|
|
18
|
+
const isStaticMethod = (method) => method === 'GET' || method === 'HEAD';
|
|
19
|
+
export const withStatic = (router, options = {}) => {
|
|
20
|
+
const mounts = [];
|
|
21
|
+
const createStaticHandler = useStatic;
|
|
22
|
+
const staticHandler = createStaticHandler(options);
|
|
23
|
+
const api = {
|
|
24
|
+
delete(path, handler) {
|
|
25
|
+
router.delete(path, handler);
|
|
26
|
+
return api;
|
|
27
|
+
},
|
|
28
|
+
fallback(handler) {
|
|
29
|
+
router.fallback(handler);
|
|
30
|
+
return api;
|
|
31
|
+
},
|
|
32
|
+
get(path, handler) {
|
|
33
|
+
router.get(path, handler);
|
|
34
|
+
return api;
|
|
35
|
+
},
|
|
36
|
+
patch(path, handler) {
|
|
37
|
+
router.patch(path, handler);
|
|
38
|
+
return api;
|
|
39
|
+
},
|
|
40
|
+
post(path, handler) {
|
|
41
|
+
router.post(path, handler);
|
|
42
|
+
return api;
|
|
43
|
+
},
|
|
44
|
+
put(path, handler) {
|
|
45
|
+
router.put(path, handler);
|
|
46
|
+
return api;
|
|
47
|
+
},
|
|
48
|
+
route: async (event) => {
|
|
49
|
+
const result = await router.route(event);
|
|
50
|
+
if (result.statusCode !== 404 || !isStaticMethod(event.httpMethod)) {
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
for (const mount of mounts) {
|
|
54
|
+
const staticPath = toStaticPath(mount, event.path);
|
|
55
|
+
if (staticPath === undefined) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const staticResult = await staticHandler({
|
|
59
|
+
...event,
|
|
60
|
+
path: staticPath,
|
|
61
|
+
}, {});
|
|
62
|
+
return staticResult;
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
},
|
|
66
|
+
static(path) {
|
|
67
|
+
mounts.push(normalizeMount(path));
|
|
68
|
+
return api;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
return api;
|
|
72
|
+
};
|