vona-module-a-static 5.0.9 → 5.0.11

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,2 @@
1
+ export declare const __ThisModule__ = "a-static";
2
+ export { ScopeModuleAStatic as ScopeModule } from './index.ts';
@@ -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,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,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,164 @@
1
+ import { BeanInfo, BeanBase, compose, BeanScopeBase, SymbolModuleBelong } from 'vona';
2
+ import { Scope } from 'vona-module-a-bean';
3
+ import { Event, BeanEventBase } from 'vona-module-a-event';
4
+ import assert from 'node:assert';
5
+ import { existsSync, mkdirSync } from 'node:fs';
6
+ import path from 'node:path';
7
+ import staticCache from '@cabloy/koa-static-cache';
8
+ import { isObject, isString } from '@cabloy/utils';
9
+ import range from 'koa-range';
10
+ import { MiddlewareSystem } from 'vona-module-a-aspect';
11
+ import { LRU } from 'ylru';
12
+
13
+ var _dec$2, _dec2$2, _class$2;
14
+ let EventResolvePath = (_dec$2 = Event(), _dec2$2 = BeanInfo({
15
+ module: "a-static"
16
+ }), _dec$2(_class$2 = _dec2$2(_class$2 = class EventResolvePath extends BeanEventBase {}) || _class$2) || _class$2);
17
+
18
+ const __ThisModule__ = 'a-static';
19
+
20
+ const SymbolStaticGetFullPathInner = Symbol('SymbolStaticGetFullPathInner');
21
+
22
+ var _dec$1, _dec2$1, _class$1;
23
+ let MiddlewareSystemStatic = (_dec$1 = MiddlewareSystem({
24
+ dependencies: 'a-instance:appReady',
25
+ apiStaticPrefix: '/api/static/',
26
+ dirs: [],
27
+ dynamic: true,
28
+ preload: false,
29
+ maxFiles: 1000,
30
+ alias: {
31
+ '/favicon.ico': '/api/static/home/index/img/vona.png'
32
+ },
33
+ getFullPath
34
+ // maxAge: undefined,
35
+ // buffer: false,
36
+ // dir: '',
37
+ // prefix: '/', // should be always '/', so as to support ssr site
38
+ }), _dec2$1 = BeanInfo({
39
+ module: "a-static"
40
+ }), _dec$1(_class$1 = _dec2$1(_class$1 = class MiddlewareSystemStatic extends BeanBase {
41
+ constructor(...args) {
42
+ super(...args);
43
+ this._composer = void 0;
44
+ }
45
+ async execute(options, next) {
46
+ return this._getComposer(options)(this.ctx, next);
47
+ }
48
+ _getComposer(options) {
49
+ if (!this._composer) {
50
+ const middlewares = this._createMiddlewares(options);
51
+ this._composer = compose(middlewares);
52
+ }
53
+ return this._composer;
54
+ }
55
+ _createMiddlewares(options) {
56
+ options = Object.assign({
57
+ maxAge: this.app.meta.isProd ? 31536000 : 0,
58
+ buffer: !!this.app.meta.isProd,
59
+ dir: this.app.config.server.publicDir,
60
+ prefix: '/'
61
+ }, options);
62
+ const dirs = (options.dirs || []).concat(options.dir);
63
+ const prefixs = [];
64
+ function rangeMiddleware(ctx, next) {
65
+ // if match static file, and use range middleware.
66
+ const isMatch = prefixs.some(p => ctx.path.startsWith(p));
67
+ if (isMatch) {
68
+ return range(ctx, next);
69
+ }
70
+ return next();
71
+ }
72
+ const middlewares = [rangeMiddleware];
73
+ for (const dirObj of dirs) {
74
+ assert(isObject(dirObj) || isString(dirObj), '`config.static.dir` must be `string | Array<string|object>`.');
75
+ let newOptions;
76
+ if (isString(dirObj)) {
77
+ // copy origin options to new options ensure the safety of objects
78
+ newOptions = Object.assign({}, options, {
79
+ dir: dirObj
80
+ });
81
+ } else {
82
+ assert(isString(dirObj.dir), '`config.static.dir` should contains `[].dir` property when object style.');
83
+ newOptions = Object.assign({}, options, dirObj);
84
+ }
85
+ if (newOptions.dynamic && !newOptions.files) {
86
+ newOptions.files = new LRU(newOptions.maxFiles);
87
+ }
88
+ if (newOptions.prefix) {
89
+ prefixs.push(newOptions.prefix);
90
+ }
91
+
92
+ // ensure directory exists
93
+ if (!existsSync(newOptions.dir)) {
94
+ mkdirSync(newOptions.dir, {
95
+ recursive: true
96
+ });
97
+ }
98
+ middlewares.push(staticCache(newOptions));
99
+ }
100
+ return middlewares;
101
+ }
102
+ }) || _class$1) || _class$1);
103
+ async function getFullPath(ctx, dir, filename, options) {
104
+ if (process.env.META_MODE === 'local') {
105
+ ctx[SymbolStaticGetFullPathInner] = () => {
106
+ return _getFullPathInner(ctx, dir, filename, options);
107
+ };
108
+ }
109
+ const scopeSelf = ctx.app.bean.scope(__ThisModule__);
110
+ return scopeSelf.event.resolvePath.emit({
111
+ dir,
112
+ filename,
113
+ options
114
+ }, ({
115
+ dir,
116
+ filename,
117
+ options
118
+ }) => {
119
+ return _getFullPathInner(ctx, dir, filename, options);
120
+ });
121
+ }
122
+ async function _getFullPathInner(ctx, dir, filename, options) {
123
+ // trim prefix
124
+ if (options.apiStaticPrefix !== '/') {
125
+ const filePrefix = path.normalize(options.apiStaticPrefix.replace(/^\//, ''));
126
+ if (filename.indexOf(filePrefix) !== 0) return;
127
+ filename = filename.slice(filePrefix.length);
128
+ }
129
+ // check
130
+ const parts = filename.split(path.sep);
131
+ const wordFirst = parts.shift();
132
+ // public
133
+ if (wordFirst === 'public') {
134
+ const fullPath = path.normalize(path.join(dir, parts.join(path.sep)));
135
+ // files that can be accessd should be under options.dir
136
+ if (fullPath.indexOf(dir) !== 0) return;
137
+ return fullPath;
138
+ }
139
+ // static
140
+ const moduleRelativeName = `${wordFirst}-${parts.shift()}`;
141
+ const module = ctx.app.meta.modules[moduleRelativeName];
142
+ if (!module) return;
143
+ const fullPath = ctx.app.util.getAssetPathPhysical(moduleRelativeName, 'static', parts.join(path.sep));
144
+ return fullPath;
145
+ }
146
+
147
+ var _dec, _dec2, _class;
148
+ let ScopeModuleAStatic = (_dec = Scope(), _dec2 = BeanInfo({
149
+ module: "a-static"
150
+ }), _dec(_class = _dec2(_class = class ScopeModuleAStatic extends BeanScopeBase {}) || _class) || _class);
151
+
152
+ /** scope: end */
153
+
154
+ class BeanStaticBase extends BeanBase {
155
+ __get__(prop) {
156
+ if (prop === 'get') {
157
+ return path => {
158
+ return this.app.util.combineStaticPath(path, this[SymbolModuleBelong]);
159
+ };
160
+ }
161
+ }
162
+ }
163
+
164
+ export { BeanStaticBase, EventResolvePath, MiddlewareSystemStatic, ScopeModuleAStatic, SymbolStaticGetFullPathInner };
@@ -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 @@
1
+ export * from './beanStaticBase.ts';
@@ -0,0 +1 @@
1
+ export * from './static.ts';
@@ -0,0 +1 @@
1
+ export declare const SymbolStaticGetFullPathInner: unique symbol;
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.11",
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 && vona :bin:buildModule && tsc -p tsconfig.build.json"
43
47
  }
44
48
  }