@midwayjs/static-file 3.0.0-beta.16

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/CHANGELOG.md ADDED
@@ -0,0 +1,135 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [3.0.0-beta.16](https://github.com/midwayjs/midway/compare/v3.0.0-beta.15...v3.0.0-beta.16) (2022-01-11)
7
+
8
+ **Note:** Version bump only for package @midwayjs/info
9
+
10
+
11
+
12
+
13
+
14
+ # [3.0.0-beta.15](https://github.com/midwayjs/midway/compare/v3.0.0-beta.14...v3.0.0-beta.15) (2022-01-07)
15
+
16
+
17
+ ### Features
18
+
19
+ * add info middleware ([#1530](https://github.com/midwayjs/midway/issues/1530)) ([7077f1d](https://github.com/midwayjs/midway/commit/7077f1d84355633a1c2fced35bfcc2152f42a7ac))
20
+ * add secret filter ([#1531](https://github.com/midwayjs/midway/issues/1531)) ([ce77e48](https://github.com/midwayjs/midway/commit/ce77e4804aaffc18a0a091d3726e36d7ec1514b2))
21
+
22
+
23
+
24
+
25
+
26
+ # [3.0.0-beta.14](https://github.com/midwayjs/midway/compare/v3.0.0-beta.13...v3.0.0-beta.14) (2022-01-04)
27
+
28
+ **Note:** Version bump only for package @midwayjs/info
29
+
30
+
31
+
32
+
33
+
34
+ # [3.0.0-beta.13](https://github.com/midwayjs/midway/compare/v3.0.0-beta.12...v3.0.0-beta.13) (2021-12-30)
35
+
36
+ **Note:** Version bump only for package @midwayjs/info
37
+
38
+
39
+
40
+
41
+
42
+ # [3.0.0-beta.12](https://github.com/midwayjs/midway/compare/v3.0.0-beta.11...v3.0.0-beta.12) (2021-12-28)
43
+
44
+ **Note:** Version bump only for package @midwayjs/info
45
+
46
+
47
+
48
+
49
+
50
+ # [3.0.0-beta.11](https://github.com/midwayjs/midway/compare/v3.0.0-beta.10...v3.0.0-beta.11) (2021-12-21)
51
+
52
+ **Note:** Version bump only for package @midwayjs/info
53
+
54
+
55
+
56
+
57
+
58
+ # [3.0.0-beta.10](https://github.com/midwayjs/midway/compare/v3.0.0-beta.9...v3.0.0-beta.10) (2021-12-20)
59
+
60
+ **Note:** Version bump only for package @midwayjs/info
61
+
62
+
63
+
64
+
65
+
66
+ # [3.0.0-beta.9](https://github.com/midwayjs/midway/compare/v3.0.0-beta.8...v3.0.0-beta.9) (2021-12-09)
67
+
68
+ **Note:** Version bump only for package @midwayjs/info
69
+
70
+
71
+
72
+
73
+
74
+ # [3.0.0-beta.8](https://github.com/midwayjs/midway/compare/v3.0.0-beta.7...v3.0.0-beta.8) (2021-12-08)
75
+
76
+ **Note:** Version bump only for package @midwayjs/info
77
+
78
+
79
+
80
+
81
+
82
+ # [3.0.0-beta.7](https://github.com/midwayjs/midway/compare/v3.0.0-beta.6...v3.0.0-beta.7) (2021-12-03)
83
+
84
+ **Note:** Version bump only for package @midwayjs/info
85
+
86
+
87
+
88
+
89
+
90
+ # [3.0.0-beta.6](https://github.com/midwayjs/midway/compare/v3.0.0-beta.5...v3.0.0-beta.6) (2021-11-26)
91
+
92
+ **Note:** Version bump only for package @midwayjs/info
93
+
94
+
95
+
96
+
97
+
98
+ # [3.0.0-beta.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
99
+
100
+ **Note:** Version bump only for package @midwayjs/info
101
+
102
+
103
+
104
+
105
+
106
+ # [3.0.0-beta.4](https://github.com/midwayjs/midway/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2021-11-24)
107
+
108
+ **Note:** Version bump only for package @midwayjs/info
109
+
110
+
111
+
112
+
113
+
114
+ # [3.0.0-beta.3](https://github.com/midwayjs/midway/compare/v3.0.0-beta.2...v3.0.0-beta.3) (2021-11-18)
115
+
116
+
117
+ ### Features
118
+
119
+ * add component and framework config definition ([#1367](https://github.com/midwayjs/midway/issues/1367)) ([b2fe615](https://github.com/midwayjs/midway/commit/b2fe6157f99659471ff1333eca0b86bb889f61a3))
120
+
121
+
122
+
123
+
124
+
125
+ # [3.0.0-beta.2](https://github.com/midwayjs/midway/compare/v3.0.0-beta.1...v3.0.0-beta.2) (2021-11-16)
126
+
127
+ **Note:** Version bump only for package @midwayjs/info
128
+
129
+
130
+
131
+
132
+
133
+ # [3.0.0-beta.1](https://github.com/midwayjs/midway/compare/v2.12.4...v3.0.0-beta.1) (2021-11-14)
134
+
135
+ **Note:** Version bump only for package @midwayjs/info
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Midway Static File Component
2
+
3
+ Static server plugin for egg, base on [koa-static-cache](https://github.com/koajs/static-cache).
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ $ npm i @midwayjs/static-file --save
9
+ ```
10
+
11
+ available in koa/egg/faas.
12
+
13
+ ## Usage
14
+
15
+ import info component in `configuration.ts` file
16
+
17
+ ```ts
18
+ import * as koa from '@midwayjs/koa';
19
+ import * as staticFile from '@midwayjs/static-file';
20
+ import { join } from 'path';
21
+
22
+ @Configuration({
23
+ imports: [
24
+ koa,
25
+ staticFile,
26
+ ],
27
+ importConfigs: [
28
+ join(__dirname, './config')
29
+ ]
30
+ })
31
+ export class ContainerConfiguration {
32
+ }
33
+
34
+ ```
35
+
36
+ support all configurations in [koa-static-cache](https://github.com/koajs/static-cache). and with default configurations below:
37
+
38
+ - dynamic: `true`
39
+ - preload: `false`
40
+ - maxAge: `31536000` in prod env, `0` in other envs
41
+ - buffer: `true` in prod env, `false` in other envs
42
+
43
+ provides one more option:
44
+
45
+ - maxFiles: the maximum value of cache items, only effective when dynamic is true, default is `1000`.
46
+
47
+ **All static files in `$appDir/public` can be visited with prefix `/public`, and all the files are lazy loaded.**
48
+
49
+ - In non-production environment, assets won't be cached, your modification can take effect immediately.
50
+ - In production environment, `@midwayjs/static-file` will cache the assets after visited, you need to restart the process to update the assets.
51
+ - Dir default is `$appDir/public` but you can also define **multiple directory** by default, static server will use all these directories.
52
+
53
+ ```js
54
+ // {app_root}/src/config/config.default.ts
55
+ export const staticFile = {
56
+ dirs: {
57
+ default: {
58
+ prefix: '/public',
59
+ dir: 'xxx'
60
+ },
61
+ antoherDir: {
62
+ prefix: '/',
63
+ dir: 'xxx'
64
+ }
65
+ }
66
+ };
67
+ ```
68
+
69
+ if you want to override default prefix, just set `dirs.default`.
70
+
71
+ ```js
72
+ // {app_root}/src/config/config.default.ts
73
+ export const staticFile = {
74
+ dirs: {
75
+ default: {
76
+ prefix: '/',
77
+ },
78
+ }
79
+ };
80
+ ```
81
+
82
+
83
+ ## License
84
+
85
+ [MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))
@@ -0,0 +1,6 @@
1
+ import { StaticFileOptions } from '../interface';
2
+ declare const _default: (appInfo: any) => {
3
+ staticFile: StaticFileOptions;
4
+ };
5
+ export default _default;
6
+ //# sourceMappingURL=config.default.d.ts.map
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const path_1 = require("path");
4
+ exports.default = appInfo => {
5
+ return {
6
+ staticFile: {
7
+ dirs: {
8
+ default: {
9
+ prefix: '/public',
10
+ dir: (0, path_1.join)(appInfo.appDir, 'public'),
11
+ },
12
+ },
13
+ dynamic: true,
14
+ preload: false,
15
+ buffer: false,
16
+ maxFiles: 1000,
17
+ },
18
+ };
19
+ };
20
+ //# sourceMappingURL=config.default.js.map
@@ -0,0 +1,5 @@
1
+ export declare const staticFile: {
2
+ maxAge: number;
3
+ buffer: boolean;
4
+ };
5
+ //# sourceMappingURL=config.prod.d.ts.map
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.staticFile = void 0;
4
+ exports.staticFile = {
5
+ maxAge: 31536000,
6
+ buffer: true,
7
+ };
8
+ //# sourceMappingURL=config.prod.js.map
@@ -0,0 +1,6 @@
1
+ import { MidwayApplicationManager } from '@midwayjs/core';
2
+ export declare class StaticFileConfiguration {
3
+ applicationManager: MidwayApplicationManager;
4
+ onReady(): Promise<void>;
5
+ }
6
+ //# sourceMappingURL=configuration.d.ts.map
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.StaticFileConfiguration = void 0;
13
+ const decorator_1 = require("@midwayjs/decorator");
14
+ const static_middleware_1 = require("./middleware/static.middleware");
15
+ const core_1 = require("@midwayjs/core");
16
+ const DefaultConfig = require("./config/config.default");
17
+ let StaticFileConfiguration = class StaticFileConfiguration {
18
+ async onReady() {
19
+ this.applicationManager
20
+ .getApplications(['koa', 'faas', 'egg'])
21
+ .forEach(app => {
22
+ app.getMiddleware().insertFirst(static_middleware_1.StaticMiddleware);
23
+ });
24
+ }
25
+ };
26
+ __decorate([
27
+ (0, decorator_1.Inject)(),
28
+ __metadata("design:type", core_1.MidwayApplicationManager)
29
+ ], StaticFileConfiguration.prototype, "applicationManager", void 0);
30
+ StaticFileConfiguration = __decorate([
31
+ (0, decorator_1.Configuration)({
32
+ namespace: 'info',
33
+ importConfigs: [
34
+ {
35
+ default: DefaultConfig,
36
+ },
37
+ ],
38
+ })
39
+ ], StaticFileConfiguration);
40
+ exports.StaticFileConfiguration = StaticFileConfiguration;
41
+ //# sourceMappingURL=configuration.js.map
@@ -0,0 +1,8 @@
1
+ import { MidwayError } from '@midwayjs/core';
2
+ export declare const StaticFileErrorEnum: {
3
+ readonly DIRECTORY_NOT_EXISTS: "STATIC_FILE_10000";
4
+ };
5
+ export declare class DirectoryNotFoundError extends MidwayError {
6
+ constructor(p: string);
7
+ }
8
+ //# sourceMappingURL=error.d.ts.map
package/dist/error.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DirectoryNotFoundError = exports.StaticFileErrorEnum = void 0;
4
+ const core_1 = require("@midwayjs/core");
5
+ exports.StaticFileErrorEnum = (0, core_1.registerErrorCode)('static_file', {
6
+ DIRECTORY_NOT_EXISTS: 10000,
7
+ });
8
+ class DirectoryNotFoundError extends core_1.MidwayError {
9
+ constructor(p) {
10
+ super(`Path ${p} not exist, please check it.`, exports.StaticFileErrorEnum.DIRECTORY_NOT_EXISTS);
11
+ }
12
+ }
13
+ exports.DirectoryNotFoundError = DirectoryNotFoundError;
14
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1,5 @@
1
+ export { StaticFileConfiguration as Configuration } from './configuration';
2
+ export * from './middleware/static.middleware';
3
+ export * from './interface';
4
+ export * from './error';
5
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.Configuration = void 0;
14
+ var configuration_1 = require("./configuration");
15
+ Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.StaticFileConfiguration; } });
16
+ __exportStar(require("./middleware/static.middleware"), exports);
17
+ __exportStar(require("./interface"), exports);
18
+ __exportStar(require("./error"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,50 @@
1
+ export interface StaticFileOption {
2
+ /**
3
+ * url prefix
4
+ */
5
+ prefix?: string;
6
+ /**
7
+ * the directory you wish to serve
8
+ */
9
+ dir?: string;
10
+ /**
11
+ * dynamic load file which not cached on initialization.
12
+ */
13
+ dynamic?: boolean;
14
+ /**
15
+ * caches the assets on initialization or not, default to true. always work together with options.dynamic.
16
+ */
17
+ preload?: boolean;
18
+ /**
19
+ * store the files in memory instead of streaming from the filesystem on each request.
20
+ */
21
+ buffer?: boolean;
22
+ /**
23
+ * cache control max age for the files, 0 by default.
24
+ */
25
+ maxFiles?: number;
26
+ /**
27
+ * optional cache control header. Overrides options.maxAge.
28
+ */
29
+ cacheControl?: string;
30
+ /**
31
+ * when request's accept-encoding include gzip, files will compressed by gzip.
32
+ */
33
+ gzip?: boolean;
34
+ /**
35
+ * object map of aliases
36
+ */
37
+ alias?: {
38
+ [aliasName: string]: string;
39
+ };
40
+ /**
41
+ * filter files at init dir, for example - skip non build (source) files. If array set - allow only listed files
42
+ */
43
+ filter?: Function | string[];
44
+ }
45
+ export interface StaticFileOptions extends Omit<StaticFileOption, 'prefix' | 'dir'> {
46
+ dirs?: {
47
+ [pathName: string]: StaticFileOption;
48
+ };
49
+ }
50
+ //# sourceMappingURL=interface.d.ts.map
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=interface.js.map
@@ -0,0 +1,12 @@
1
+ import { MidwayMiddlewareService } from '@midwayjs/core';
2
+ export declare class StaticMiddleware {
3
+ private staticFileConfig;
4
+ middlewareService: MidwayMiddlewareService<any, any>;
5
+ logger: any;
6
+ resolve(app: any): Promise<{
7
+ (context: any, next?: any): Promise<any>;
8
+ _name: string;
9
+ }>;
10
+ static getName(): string;
11
+ }
12
+ //# sourceMappingURL=static.middleware.d.ts.map
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.StaticMiddleware = void 0;
13
+ const decorator_1 = require("@midwayjs/decorator");
14
+ const core_1 = require("@midwayjs/core");
15
+ const assert = require("assert");
16
+ const staticCache = require("koa-static-cache");
17
+ const LRU = require("ylru");
18
+ const range = require("koa-range");
19
+ const error_1 = require("../error");
20
+ let StaticMiddleware = class StaticMiddleware {
21
+ async resolve(app) {
22
+ const dirs = Object.values(this.staticFileConfig.dirs);
23
+ if (this.staticFileConfig.dir) {
24
+ dirs.push(this.staticFileConfig.dir);
25
+ }
26
+ const prefixs = [];
27
+ function rangeMiddleware(ctx, next) {
28
+ // if match static file, and use range middleware.
29
+ const isMatch = prefixs.some(p => ctx.path.startsWith(p));
30
+ if (isMatch) {
31
+ return range(ctx, next);
32
+ }
33
+ return next();
34
+ }
35
+ const middlewares = [rangeMiddleware];
36
+ for (const dirObj of dirs) {
37
+ assert(decorator_1.Types.isObject(dirObj) || decorator_1.Types.isString(dirObj), '`config.static.dir` must be `string | Array<string|object>`.');
38
+ const newOptions = Object.assign({}, this.staticFileConfig, dirObj);
39
+ if (newOptions.dynamic && !newOptions.files) {
40
+ newOptions.files = new LRU(newOptions.maxFiles);
41
+ }
42
+ if (newOptions.prefix) {
43
+ prefixs.push(newOptions.prefix);
44
+ }
45
+ // ensure directory exists
46
+ if (!(await decorator_1.FileUtils.exists(newOptions.dir))) {
47
+ throw new error_1.DirectoryNotFoundError(newOptions.dir);
48
+ }
49
+ this.logger.info('[@midwayjs/static] starting static serve %s -> %s', newOptions.prefix, newOptions.dir);
50
+ middlewares.push(staticCache(newOptions));
51
+ }
52
+ return await this.middlewareService.compose(middlewares, app, 'staticFile');
53
+ }
54
+ static getName() {
55
+ return 'staticFile';
56
+ }
57
+ };
58
+ __decorate([
59
+ (0, decorator_1.Config)('staticFile'),
60
+ __metadata("design:type", Object)
61
+ ], StaticMiddleware.prototype, "staticFileConfig", void 0);
62
+ __decorate([
63
+ (0, decorator_1.Inject)(),
64
+ __metadata("design:type", core_1.MidwayMiddlewareService)
65
+ ], StaticMiddleware.prototype, "middlewareService", void 0);
66
+ __decorate([
67
+ (0, decorator_1.Logger)('coreLogger'),
68
+ __metadata("design:type", Object)
69
+ ], StaticMiddleware.prototype, "logger", void 0);
70
+ StaticMiddleware = __decorate([
71
+ (0, decorator_1.Middleware)()
72
+ ], StaticMiddleware);
73
+ exports.StaticMiddleware = StaticMiddleware;
74
+ //# sourceMappingURL=static.middleware.js.map
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { StaticFileOptions } from './dist/index';
2
+ export * from './dist/index';
3
+
4
+ declare module '@midwayjs/core/dist/interface' {
5
+ interface MidwayConfig {
6
+ staticFile?: Partial<StaticFileOptions>;
7
+ }
8
+ }
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@midwayjs/static-file",
3
+ "description": "midway static file component",
4
+ "version": "3.0.0-beta.16",
5
+ "main": "dist/index",
6
+ "typings": "index.d.ts",
7
+ "files": [
8
+ "dist/**/*.js",
9
+ "dist/**/*.d.ts",
10
+ "index.d.ts"
11
+ ],
12
+ "dependencies": {
13
+ "koa-range": "0.3.0",
14
+ "ylru": "1.2.1",
15
+ "koa-static-cache": "5.1.4"
16
+ },
17
+ "devDependencies": {
18
+ "@midwayjs/core": "^3.0.0-beta.16",
19
+ "@midwayjs/decorator": "^3.0.0-beta.16",
20
+ "@midwayjs/koa": "^3.0.0-beta.16",
21
+ "@midwayjs/faas": "^3.0.0-beta.16",
22
+ "@midwayjs/mock": "^3.0.0-beta.16",
23
+ "@midwayjs/serverless-app": "^3.0.0-beta.16"
24
+ },
25
+ "keywords": [
26
+ "midway",
27
+ "koa-static",
28
+ "assets",
29
+ "static"
30
+ ],
31
+ "author": "czy88840616 <czy88840616@gmail.com>",
32
+ "license": "MIT",
33
+ "scripts": {
34
+ "build": "tsc",
35
+ "test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
36
+ "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
37
+ "ci": "npm run test",
38
+ "lint": "mwts check"
39
+ },
40
+ "engines": {
41
+ "node": ">=12"
42
+ },
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "https://github.com/midwayjs/midway.git"
46
+ }
47
+ }