vona-module-a-static 5.0.10 → 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.
- package/dist/index.js +164 -3
- package/package.json +2 -2
- package/dist/.metadata/index.js +0 -23
- package/dist/.metadata/this.js +0 -2
- package/dist/bean/event.resolvePath.js +0 -13
- package/dist/bean/middlewareSystem.static.js +0 -130
- package/dist/lib/beanStaticBase.js +0 -10
- package/dist/lib/index.js +0 -1
- package/dist/types/index.js +0 -1
- package/dist/types/static.js +0 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,164 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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 };
|
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.
|
|
4
|
+
"version": "5.0.11",
|
|
5
5
|
"title": "a-static",
|
|
6
6
|
"vonaModule": {
|
|
7
7
|
"dependencies": {},
|
|
@@ -43,6 +43,6 @@
|
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"clean": "rimraf dist tsconfig.build.tsbuildinfo",
|
|
46
|
-
"tsc:publish": "npm run clean && tsc -p tsconfig.build.json"
|
|
46
|
+
"tsc:publish": "npm run clean && vona :bin:buildModule && tsc -p tsconfig.build.json"
|
|
47
47
|
}
|
|
48
48
|
}
|
package/dist/.metadata/index.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
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 */
|
package/dist/.metadata/this.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
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 };
|
|
@@ -1,130 +0,0 @@
|
|
|
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
|
-
}
|
package/dist/lib/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./beanStaticBase.js";
|
package/dist/types/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./static.js";
|
package/dist/types/static.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const SymbolStaticGetFullPathInner = Symbol('SymbolStaticGetFullPathInner');
|