vona-module-a-static 5.0.9 → 5.0.10

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.
@@ -0,0 +1,58 @@
1
+ import type { BeanScopeUtil } from 'vona';
2
+ import type { EventOn } from 'vona-module-a-event';
3
+ /** event: end */
4
+ /** event: begin */
5
+ import type { EventResolvePath } from '../bean/event.resolvePath.ts';
6
+ /** event: end */
7
+ /** event: begin */
8
+ import type { TypeEventResolvePathData, TypeEventResolvePathResult } from '../bean/event.resolvePath.ts';
9
+ /** middlewareSystem: end */
10
+ /** event: begin */
11
+ import type { IMiddlewareSystemOptionsStatic } from '../bean/middlewareSystem.static.ts';
12
+ /** event: end */
13
+ /** scope: begin */
14
+ import { BeanScopeBase } from 'vona';
15
+ import 'vona';
16
+ import 'vona';
17
+ import 'vona';
18
+ export * from '../bean/event.resolvePath.ts';
19
+ declare module 'vona-module-a-aspect' {
20
+ interface IMiddlewareSystemRecord {
21
+ 'a-static:static': IMiddlewareSystemOptionsStatic;
22
+ }
23
+ }
24
+ declare module 'vona-module-a-static' {
25
+ interface MiddlewareSystemStatic {
26
+ }
27
+ }
28
+ /** middlewareSystem: begin */
29
+ export * from '../bean/middlewareSystem.static.ts';
30
+ declare module 'vona' {
31
+ }
32
+ declare module 'vona-module-a-static' {
33
+ interface EventResolvePath {
34
+ }
35
+ }
36
+ export interface IModuleEvent {
37
+ resolvePath: EventResolvePath;
38
+ }
39
+ declare module 'vona-module-a-event' {
40
+ interface IEventRecord {
41
+ 'a-static:resolvePath': EventOn<TypeEventResolvePathData, TypeEventResolvePathResult>;
42
+ }
43
+ }
44
+ export declare class ScopeModuleAStatic extends BeanScopeBase {
45
+ }
46
+ export interface ScopeModuleAStatic {
47
+ util: BeanScopeUtil;
48
+ event: IModuleEvent;
49
+ }
50
+ declare module 'vona' {
51
+ interface IBeanScopeRecord {
52
+ 'a-static': ScopeModuleAStatic;
53
+ }
54
+ interface IBeanScopeContainer {
55
+ static: ScopeModuleAStatic;
56
+ }
57
+ }
58
+ /** scope: end */
@@ -0,0 +1,23 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ /** event: end */
8
+ /** scope: begin */
9
+ import { BeanScopeBase } from 'vona';
10
+ import { Scope } from 'vona-module-a-bean';
11
+ import 'vona';
12
+ import 'vona';
13
+ import 'vona';
14
+ export * from "../bean/event.resolvePath.js";
15
+ /** middlewareSystem: begin */
16
+ export * from "../bean/middlewareSystem.static.js";
17
+ let ScopeModuleAStatic = class ScopeModuleAStatic extends BeanScopeBase {
18
+ };
19
+ ScopeModuleAStatic = __decorate([
20
+ Scope()
21
+ ], ScopeModuleAStatic);
22
+ export { ScopeModuleAStatic };
23
+ /** scope: end */
@@ -0,0 +1,2 @@
1
+ export declare const __ThisModule__ = "a-static";
2
+ export { ScopeModuleAStatic as ScopeModule } from './index.ts';
@@ -0,0 +1,2 @@
1
+ export const __ThisModule__ = 'a-static';
2
+ export { ScopeModuleAStatic as ScopeModule } from "./index.js";
@@ -0,0 +1,10 @@
1
+ import type { IMiddlewareSystemOptionsStatic } from './middlewareSystem.static.ts';
2
+ import { BeanEventBase } from 'vona-module-a-event';
3
+ export interface TypeEventResolvePathData {
4
+ dir: string;
5
+ filename: string;
6
+ options: IMiddlewareSystemOptionsStatic;
7
+ }
8
+ export type TypeEventResolvePathResult = string | true | undefined;
9
+ export declare class EventResolvePath extends BeanEventBase<TypeEventResolvePathData, TypeEventResolvePathResult> {
10
+ }
@@ -0,0 +1,13 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { BeanEventBase, Event } from 'vona-module-a-event';
8
+ let EventResolvePath = class EventResolvePath extends BeanEventBase {
9
+ };
10
+ EventResolvePath = __decorate([
11
+ Event()
12
+ ], EventResolvePath);
13
+ export { EventResolvePath };
@@ -0,0 +1,27 @@
1
+ import type { Next, VonaContext } from 'vona';
2
+ import type { IDecoratorMiddlewareSystemOptions, IMiddlewareSystemExecute } from 'vona-module-a-aspect';
3
+ import { BeanBase } from 'vona';
4
+ interface IStaticDirItem {
5
+ prefix: string;
6
+ dir: string;
7
+ }
8
+ export interface IMiddlewareSystemOptionsStatic extends IDecoratorMiddlewareSystemOptions {
9
+ apiStaticPrefix: string;
10
+ dir?: string;
11
+ dirs: (string | IStaticDirItem)[];
12
+ dynamic: boolean;
13
+ preload: boolean;
14
+ buffer?: boolean;
15
+ maxFiles: number;
16
+ maxAge?: number;
17
+ alias: Record<string, string>;
18
+ getFullPath: typeof getFullPath;
19
+ }
20
+ export declare class MiddlewareSystemStatic extends BeanBase implements IMiddlewareSystemExecute {
21
+ private _composer;
22
+ execute(options: IMiddlewareSystemOptionsStatic, next: Next): Promise<any>;
23
+ _getComposer(options: IMiddlewareSystemOptionsStatic): any;
24
+ _createMiddlewares(options: IMiddlewareSystemOptionsStatic): ((ctx: any, next: any) => any)[];
25
+ }
26
+ declare function getFullPath(ctx: VonaContext, dir: string, filename: string, options: IMiddlewareSystemOptionsStatic): Promise<string | true | undefined>;
27
+ export {};
@@ -0,0 +1,130 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import assert from 'node:assert';
8
+ import { existsSync, mkdirSync } from 'node:fs';
9
+ import path from 'node:path';
10
+ import staticCache from '@cabloy/koa-static-cache';
11
+ import { isObject, isString } from '@cabloy/utils';
12
+ import range from 'koa-range';
13
+ import { BeanBase, compose } from 'vona';
14
+ import { MiddlewareSystem } from 'vona-module-a-aspect';
15
+ import { LRU } from 'ylru';
16
+ import { __ThisModule__ } from "../.metadata/this.js";
17
+ import { SymbolStaticGetFullPathInner } from "../types/static.js";
18
+ let MiddlewareSystemStatic = class MiddlewareSystemStatic extends BeanBase {
19
+ _composer;
20
+ async execute(options, next) {
21
+ return this._getComposer(options)(this.ctx, next);
22
+ }
23
+ _getComposer(options) {
24
+ if (!this._composer) {
25
+ const middlewares = this._createMiddlewares(options);
26
+ this._composer = compose(middlewares);
27
+ }
28
+ return this._composer;
29
+ }
30
+ _createMiddlewares(options) {
31
+ options = Object.assign({
32
+ maxAge: this.app.meta.isProd ? 31536000 : 0,
33
+ buffer: !!this.app.meta.isProd,
34
+ dir: this.app.config.server.publicDir,
35
+ prefix: '/',
36
+ }, options);
37
+ const dirs = (options.dirs || []).concat(options.dir);
38
+ const prefixs = [];
39
+ function rangeMiddleware(ctx, next) {
40
+ // if match static file, and use range middleware.
41
+ const isMatch = prefixs.some(p => ctx.path.startsWith(p));
42
+ if (isMatch) {
43
+ return range(ctx, next);
44
+ }
45
+ return next();
46
+ }
47
+ const middlewares = [rangeMiddleware];
48
+ for (const dirObj of dirs) {
49
+ assert(isObject(dirObj) || isString(dirObj), '`config.static.dir` must be `string | Array<string|object>`.');
50
+ let newOptions;
51
+ if (isString(dirObj)) {
52
+ // copy origin options to new options ensure the safety of objects
53
+ newOptions = Object.assign({}, options, { dir: dirObj });
54
+ }
55
+ else {
56
+ assert(isString(dirObj.dir), '`config.static.dir` should contains `[].dir` property when object style.');
57
+ newOptions = Object.assign({}, options, dirObj);
58
+ }
59
+ if (newOptions.dynamic && !newOptions.files) {
60
+ newOptions.files = new LRU(newOptions.maxFiles);
61
+ }
62
+ if (newOptions.prefix) {
63
+ prefixs.push(newOptions.prefix);
64
+ }
65
+ // ensure directory exists
66
+ if (!existsSync(newOptions.dir)) {
67
+ mkdirSync(newOptions.dir, { recursive: true });
68
+ }
69
+ middlewares.push(staticCache(newOptions));
70
+ }
71
+ return middlewares;
72
+ }
73
+ };
74
+ MiddlewareSystemStatic = __decorate([
75
+ MiddlewareSystem({
76
+ dependencies: 'a-instance:appReady',
77
+ apiStaticPrefix: '/api/static/',
78
+ dirs: [],
79
+ dynamic: true,
80
+ preload: false,
81
+ maxFiles: 1000,
82
+ alias: {
83
+ '/favicon.ico': '/api/static/home/index/img/vona.png',
84
+ },
85
+ getFullPath,
86
+ // maxAge: undefined,
87
+ // buffer: false,
88
+ // dir: '',
89
+ // prefix: '/', // should be always '/', so as to support ssr site
90
+ })
91
+ ], MiddlewareSystemStatic);
92
+ export { MiddlewareSystemStatic };
93
+ async function getFullPath(ctx, dir, filename, options) {
94
+ if (process.env.META_MODE === 'local') {
95
+ ctx[SymbolStaticGetFullPathInner] = () => {
96
+ return _getFullPathInner(ctx, dir, filename, options);
97
+ };
98
+ }
99
+ const scopeSelf = ctx.app.bean.scope(__ThisModule__);
100
+ return scopeSelf.event.resolvePath.emit({ dir, filename, options }, ({ dir, filename, options }) => {
101
+ return _getFullPathInner(ctx, dir, filename, options);
102
+ });
103
+ }
104
+ async function _getFullPathInner(ctx, dir, filename, options) {
105
+ // trim prefix
106
+ if (options.apiStaticPrefix !== '/') {
107
+ const filePrefix = path.normalize(options.apiStaticPrefix.replace(/^\//, ''));
108
+ if (filename.indexOf(filePrefix) !== 0)
109
+ return;
110
+ filename = filename.slice(filePrefix.length);
111
+ }
112
+ // check
113
+ const parts = filename.split(path.sep);
114
+ const wordFirst = parts.shift();
115
+ // public
116
+ if (wordFirst === 'public') {
117
+ const fullPath = path.normalize(path.join(dir, parts.join(path.sep)));
118
+ // files that can be accessd should be under options.dir
119
+ if (fullPath.indexOf(dir) !== 0)
120
+ return;
121
+ return fullPath;
122
+ }
123
+ // static
124
+ const moduleRelativeName = `${wordFirst}-${parts.shift()}`;
125
+ const module = ctx.app.meta.modules[moduleRelativeName];
126
+ if (!module)
127
+ return;
128
+ const fullPath = ctx.app.util.getAssetPathPhysical(moduleRelativeName, 'static', parts.join(path.sep));
129
+ return fullPath;
130
+ }
@@ -0,0 +1,3 @@
1
+ export * from './.metadata/index.ts';
2
+ export * from './lib/index.ts';
3
+ export * from './types/index.ts';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./.metadata/index.js";
2
+ export * from "./lib/index.js";
3
+ export * from "./types/index.js";
@@ -0,0 +1,4 @@
1
+ import { BeanBase } from 'vona';
2
+ export declare class BeanStaticBase extends BeanBase {
3
+ protected __get__(prop: string): ((path: string) => string) | undefined;
4
+ }
@@ -0,0 +1,10 @@
1
+ import { BeanBase, SymbolModuleBelong } from 'vona';
2
+ export class BeanStaticBase extends BeanBase {
3
+ __get__(prop) {
4
+ if (prop === 'get') {
5
+ return (path) => {
6
+ return this.app.util.combineStaticPath(path, this[SymbolModuleBelong]);
7
+ };
8
+ }
9
+ }
10
+ }
@@ -0,0 +1 @@
1
+ export * from './beanStaticBase.ts';
@@ -0,0 +1 @@
1
+ export * from "./beanStaticBase.js";
@@ -0,0 +1 @@
1
+ export * from './static.ts';
@@ -0,0 +1 @@
1
+ export * from "./static.js";
@@ -0,0 +1 @@
1
+ export declare const SymbolStaticGetFullPathInner: unique symbol;
@@ -0,0 +1 @@
1
+ export const SymbolStaticGetFullPathInner = Symbol('SymbolStaticGetFullPathInner');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-static",
3
3
  "type": "module",
4
- "version": "5.0.9",
4
+ "version": "5.0.10",
5
5
  "title": "a-static",
6
6
  "vonaModule": {
7
7
  "dependencies": {},
@@ -37,8 +37,12 @@
37
37
  "koa-range": "^0.3.0",
38
38
  "ylru": "^2.0.0"
39
39
  },
40
+ "devDependencies": {
41
+ "clean-package": "^2.2.0",
42
+ "rimraf": "^6.0.1"
43
+ },
40
44
  "scripts": {
41
- "clean": "rimraf dist tsconfig.tsbuildinfo",
42
- "tsc:publish": "npm run clean && tsc"
45
+ "clean": "rimraf dist tsconfig.build.tsbuildinfo",
46
+ "tsc:publish": "npm run clean && tsc -p tsconfig.build.json"
43
47
  }
44
48
  }