@vyriy/static 0.5.4 → 0.5.6
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/README.md +7 -3
- package/package.json +4 -4
- package/server.d.ts +1 -1
- package/server.js +2 -2
- package/types.d.ts +2 -1
- package/with-spa.js +3 -0
- package/with-static.d.ts +2 -2
- package/with-static.js +14 -6
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ vyriy-static public
|
|
|
20
20
|
vyriy-static --port 3000 dist
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
When no directory is provided to the CLI, it
|
|
23
|
+
When no directory is provided to the CLI, it tries `dist`, `build`, `public`, `out`, and then the current directory.
|
|
24
24
|
|
|
25
25
|
CLI flags:
|
|
26
26
|
|
|
@@ -45,7 +45,10 @@ import { createRouter } from '@vyriy/router';
|
|
|
45
45
|
export const assets = useStatic();
|
|
46
46
|
export const app = useSpa({ directory: 'dist', index: 'index.html' });
|
|
47
47
|
|
|
48
|
-
export const router = withStatic(createRouter()
|
|
48
|
+
export const router = withStatic(createRouter())
|
|
49
|
+
.get('/api', () => ({ body: JSON.stringify({ ok: true }) }))
|
|
50
|
+
.static('/dist', { directory: 'dist' })
|
|
51
|
+
.static('/', { directory: 'public' });
|
|
49
52
|
export const spaRouter = withSpa(createRouter(), 'dist');
|
|
50
53
|
|
|
51
54
|
const code = await staticServer({ directory: 'dist' });
|
|
@@ -53,7 +56,8 @@ const code = await staticServer({ directory: 'dist' });
|
|
|
53
56
|
|
|
54
57
|
- `useStatic({ directory, index, error })` serves files from a directory.
|
|
55
58
|
- `useSpa(options)` serves static files and falls back to the configured index file for missing `GET` and `HEAD` paths.
|
|
56
|
-
- `withStatic(router
|
|
59
|
+
- `withStatic(router).static(path, options)` adds prefix-based static mounts.
|
|
60
|
+
- `withStatic(router, options).static(path)` keeps a shared default for static mounts.
|
|
57
61
|
- `withSpa(router, directoryOrOptions)` adds static-first SPA fallback behavior.
|
|
58
62
|
|
|
59
63
|
Defaults are `directory: 'dist'`, `index: 'index.html'`, and `error: '404.html'`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vyriy/static",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.6",
|
|
4
4
|
"description": "Static file and SPA serving helpers for Vyriy servers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
"vyriy-static": "./bin/index.js"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@vyriy/handler": "0.5.
|
|
12
|
-
"@vyriy/router": "0.5.
|
|
13
|
-
"@vyriy/server": "0.5.
|
|
11
|
+
"@vyriy/handler": "0.5.6",
|
|
12
|
+
"@vyriy/router": "0.5.6",
|
|
13
|
+
"@vyriy/server": "0.5.6"
|
|
14
14
|
},
|
|
15
15
|
"agents": "./AGENTS.md",
|
|
16
16
|
"license": "MIT",
|
package/server.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export type StaticBinCommand = {
|
|
|
3
3
|
readonly type: 'help' | 'version';
|
|
4
4
|
} | {
|
|
5
5
|
readonly type: 'serve';
|
|
6
|
-
readonly directory
|
|
6
|
+
readonly directory?: string;
|
|
7
7
|
readonly port?: string;
|
|
8
8
|
};
|
|
9
9
|
export type RunStaticCli = (args?: readonly string[], command?: string, alias?: false | string) => Promise<void>;
|
package/server.js
CHANGED
|
@@ -29,7 +29,7 @@ export const createStaticHelpText = (command = 'vyriy-static', alias = 'vs') =>
|
|
|
29
29
|
return `Vyriy Static Server
|
|
30
30
|
|
|
31
31
|
Usage:
|
|
32
|
-
${command} [directory] Serve a static directory (defaults to
|
|
32
|
+
${command} [directory] Serve a static directory (defaults to dist when it exists)
|
|
33
33
|
${command} --port 3000 dist Serve a directory on a specific port
|
|
34
34
|
${aliasText}\
|
|
35
35
|
${command} --help, -h Show help
|
|
@@ -52,7 +52,7 @@ export const parseStaticBinArgs = (args) => {
|
|
|
52
52
|
}
|
|
53
53
|
return {
|
|
54
54
|
type: 'serve',
|
|
55
|
-
directory: args.find((arg, index) => !arg.startsWith('-') && !isOptionValue(args, index))
|
|
55
|
+
directory: args.find((arg, index) => !arg.startsWith('-') && !isOptionValue(args, index)),
|
|
56
56
|
port: getOptionValue(args, '--port', '-p'),
|
|
57
57
|
};
|
|
58
58
|
};
|
package/types.d.ts
CHANGED
|
@@ -5,10 +5,11 @@ export type StaticOptions = {
|
|
|
5
5
|
readonly index?: string;
|
|
6
6
|
readonly error?: string;
|
|
7
7
|
};
|
|
8
|
+
export type StaticMountOptions = StaticOptions | string;
|
|
8
9
|
export type StaticHandler = Handler<ApiEvent, ApiResult>;
|
|
9
10
|
export type StaticServer = (options?: StaticOptions) => Promise<number>;
|
|
10
11
|
export type StaticRouterApi = Omit<RouterApi, 'delete' | 'fallback' | 'get' | 'patch' | 'post' | 'put'> & {
|
|
11
|
-
static(path: string): StaticRouterApi;
|
|
12
|
+
static(path: string, options?: StaticMountOptions): StaticRouterApi;
|
|
12
13
|
} & {
|
|
13
14
|
[Method in Extract<keyof RouterApi, 'delete' | 'fallback' | 'get' | 'patch' | 'post' | 'put'>]: (...args: Parameters<RouterApi[Method]>) => StaticRouterApi;
|
|
14
15
|
};
|
package/with-spa.js
CHANGED
package/with-static.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { StaticMountOptions, StaticRouterApi } from './types.js';
|
|
2
2
|
import type { RouterApi } from '@vyriy/router';
|
|
3
|
-
export declare const withStatic: (router: RouterApi, options?:
|
|
3
|
+
export declare const withStatic: (router: RouterApi, options?: StaticMountOptions) => StaticRouterApi;
|
package/with-static.js
CHANGED
|
@@ -20,10 +20,10 @@ const toStaticPath = (mount, requestPath) => {
|
|
|
20
20
|
return undefined;
|
|
21
21
|
};
|
|
22
22
|
const isStaticMethod = (method) => method === 'GET' || method === 'HEAD';
|
|
23
|
-
|
|
23
|
+
const toStaticOptions = (options) => typeof options === 'string' ? { directory: options } : (options ?? {});
|
|
24
|
+
export const withStatic = (router, options) => {
|
|
24
25
|
const mounts = [];
|
|
25
26
|
const createStaticHandler = useStatic;
|
|
26
|
-
const staticHandler = createStaticHandler(options);
|
|
27
27
|
const api = {
|
|
28
28
|
delete(path, handler) {
|
|
29
29
|
router.delete(path, handler);
|
|
@@ -37,6 +37,9 @@ export const withStatic = (router, options = {}) => {
|
|
|
37
37
|
router.get(path, handler);
|
|
38
38
|
return api;
|
|
39
39
|
},
|
|
40
|
+
handle() {
|
|
41
|
+
return (event) => api.route(event);
|
|
42
|
+
},
|
|
40
43
|
patch(path, handler) {
|
|
41
44
|
router.patch(path, handler);
|
|
42
45
|
return api;
|
|
@@ -55,11 +58,11 @@ export const withStatic = (router, options = {}) => {
|
|
|
55
58
|
return result;
|
|
56
59
|
}
|
|
57
60
|
for (const mount of mounts) {
|
|
58
|
-
const staticPath = toStaticPath(mount, event.path);
|
|
61
|
+
const staticPath = toStaticPath(mount.path, event.path);
|
|
59
62
|
if (staticPath === undefined) {
|
|
60
63
|
continue;
|
|
61
64
|
}
|
|
62
|
-
const staticResult = await
|
|
65
|
+
const staticResult = await mount.handler({
|
|
63
66
|
...event,
|
|
64
67
|
path: staticPath,
|
|
65
68
|
}, {});
|
|
@@ -67,8 +70,13 @@ export const withStatic = (router, options = {}) => {
|
|
|
67
70
|
}
|
|
68
71
|
return result;
|
|
69
72
|
},
|
|
70
|
-
static(path) {
|
|
71
|
-
|
|
73
|
+
static(path, mountOptions) {
|
|
74
|
+
const staticOptions = mountOptions ?? options;
|
|
75
|
+
mounts.push({
|
|
76
|
+
handler: createStaticHandler(toStaticOptions(staticOptions)),
|
|
77
|
+
path: normalizeMount(path),
|
|
78
|
+
});
|
|
79
|
+
mounts.sort((left, right) => right.path.length - left.path.length);
|
|
72
80
|
return api;
|
|
73
81
|
},
|
|
74
82
|
};
|