@celljs/serve-static 3.0.0

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.
Files changed (40) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/cell-remote.yml +3 -0
  4. package/cell.yml +8 -0
  5. package/lib/hooks/webpack.d.ts +4 -0
  6. package/lib/hooks/webpack.d.ts.map +1 -0
  7. package/lib/hooks/webpack.js +17 -0
  8. package/lib/hooks/webpack.js.map +1 -0
  9. package/lib/node/index.d.ts +2 -0
  10. package/lib/node/index.d.ts.map +1 -0
  11. package/lib/node/index.js +18 -0
  12. package/lib/node/index.js.map +1 -0
  13. package/lib/node/module.d.ts +4 -0
  14. package/lib/node/module.d.ts.map +1 -0
  15. package/lib/node/module.js +6 -0
  16. package/lib/node/module.js.map +1 -0
  17. package/lib/node/serve-static-middleware.d.ts +15 -0
  18. package/lib/node/serve-static-middleware.d.ts.map +1 -0
  19. package/lib/node/serve-static-middleware.js +87 -0
  20. package/lib/node/serve-static-middleware.js.map +1 -0
  21. package/lib/node/serve-static.d.ts +4 -0
  22. package/lib/node/serve-static.d.ts.map +1 -0
  23. package/lib/node/serve-static.js +93 -0
  24. package/lib/node/serve-static.js.map +1 -0
  25. package/lib/node/type.d.ts +16 -0
  26. package/lib/node/type.d.ts.map +1 -0
  27. package/lib/node/type.js +3 -0
  28. package/lib/node/type.js.map +1 -0
  29. package/lib/package.spec.d.ts +1 -0
  30. package/lib/package.spec.d.ts.map +1 -0
  31. package/lib/package.spec.js +4 -0
  32. package/lib/package.spec.js.map +1 -0
  33. package/package.json +47 -0
  34. package/src/hooks/webpack.ts +15 -0
  35. package/src/node/index.ts +2 -0
  36. package/src/node/module.ts +4 -0
  37. package/src/node/serve-static-middleware.ts +73 -0
  38. package/src/node/serve-static.ts +108 -0
  39. package/src/node/type.ts +15 -0
  40. package/src/package.spec.ts +4 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Cellbang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # Cell - Serve Static Component
@@ -0,0 +1,3 @@
1
+ cell:
2
+ serve-static:
3
+ root: frontend
package/cell.yml ADDED
@@ -0,0 +1,8 @@
1
+ cell:
2
+ serve-static:
3
+ spa: true
4
+ root: ${frontendProjectDistPath}
5
+ options:
6
+ maxAge: 31536000000
7
+ # htmlMaxAge: 31536000000
8
+ immutable: true
@@ -0,0 +1,4 @@
1
+ import { WebpackContext } from '@celljs/cli-service';
2
+ declare const _default: (context: WebpackContext) => Promise<void>;
3
+ export default _default;
4
+ //# sourceMappingURL=webpack.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webpack.d.ts","sourceRoot":"","sources":["../../src/hooks/webpack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAwB,MAAM,qBAAqB,CAAC;kCAE5C,cAAc;AAA7C,wBAYE"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const cli_service_1 = require("@celljs/cli-service");
4
+ exports.default = async (context) => {
5
+ const { dev, configurations } = context;
6
+ const config = cli_service_1.ConfigurationContext.getFrontendConfiguration(configurations);
7
+ if (dev && config) {
8
+ config
9
+ .devServer
10
+ .merge({
11
+ devMiddleware: {
12
+ writeToDisk: true
13
+ }
14
+ });
15
+ }
16
+ };
17
+ //# sourceMappingURL=webpack.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webpack.js","sourceRoot":"","sources":["../../src/hooks/webpack.ts"],"names":[],"mappings":";;AAAA,qDAA2E;AAE3E,kBAAe,KAAK,EAAE,OAAuB,EAAE,EAAE;IAC7C,MAAM,EAAE,GAAG,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC;IACxC,MAAM,MAAM,GAAG,kCAAoB,CAAC,wBAAwB,CAAC,cAAc,CAAC,CAAC;IAC7E,IAAI,GAAG,IAAI,MAAM,EAAE;QACf,MAAM;aACD,SAAS;aACT,KAAK,CAAC;YACH,aAAa,EAAE;gBACX,WAAW,EAAE,IAAI;aACpB;SACJ,CAAC,CAAC;KACV;AACL,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './serve-static-middleware';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AACA,cAAc,2BAA2B,CAAC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./serve-static-middleware"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AACA,4DAA0C"}
@@ -0,0 +1,4 @@
1
+ import './index';
2
+ declare const _default: import("inversify/lib/interfaces/interfaces").interfaces.ContainerModule;
3
+ export default _default;
4
+ //# sourceMappingURL=module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../src/node/module.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,CAAC;;AAEjB,wBAA0B"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const core_1 = require("@celljs/core");
4
+ require("./index");
5
+ exports.default = (0, core_1.autoBind)();
6
+ //# sourceMappingURL=module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.js","sourceRoot":"","sources":["../../src/node/module.ts"],"names":[],"mappings":";;AAAA,uCAAwC;AACxC,mBAAiB;AAEjB,kBAAe,IAAA,eAAQ,GAAE,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { Middleware, Context, RequestMatcher } from '@celljs/web/lib/node';
2
+ export declare class ServeStaticMiddleware implements Middleware {
3
+ protected config: {
4
+ spa: boolean;
5
+ root: string;
6
+ path: string;
7
+ apiPath: string;
8
+ options: any;
9
+ };
10
+ protected path: string;
11
+ protected readonly requestMatcher: RequestMatcher;
12
+ handle(ctx: Context, next: () => Promise<void>): Promise<void>;
13
+ priority: number;
14
+ }
15
+ //# sourceMappingURL=serve-static-middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serve-static-middleware.d.ts","sourceRoot":"","sources":["../../src/node/serve-static-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,cAAc,EAA4B,MAAM,sBAAsB,CAAC;AAMrG,qBACa,qBAAsB,YAAW,UAAU;IAGpD,SAAS,CAAC,MAAM,EAAE;QAAE,GAAG,EAAE,OAAO,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,GAAG,CAAA;KAAC,CAAC;IAG7F,SAAS,CAAC,IAAI,EAAE,MAAM,CAAC;IAGvB,SAAS,CAAC,QAAQ,CAAC,cAAc,EAAE,cAAc,CAAC;IAE5C,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAoDpE,QAAQ,SAAkC;CAE7C"}
@@ -0,0 +1,87 @@
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.ServeStaticMiddleware = void 0;
13
+ const node_1 = require("@celljs/web/lib/node");
14
+ const serve_static_1 = require("./serve-static");
15
+ const core_1 = require("@celljs/core");
16
+ const http_1 = require("@celljs/http");
17
+ const web_1 = require("@celljs/web");
18
+ let ServeStaticMiddleware = class ServeStaticMiddleware {
19
+ constructor() {
20
+ this.priority = node_1.HTTP_MIDDLEWARE_PRIORITY + 500;
21
+ }
22
+ async handle(ctx, next) {
23
+ this.config.options.baseHref = this.path;
24
+ const method = ctx.request.method;
25
+ if (!(method === http_1.HttpMethod.GET || method === http_1.HttpMethod.HEAD) || ctx.request.query['static'] === 'skip') {
26
+ await next();
27
+ return;
28
+ }
29
+ if (this.config.apiPath && await this.requestMatcher.match(this.config.apiPath)) {
30
+ await next();
31
+ return;
32
+ }
33
+ if (this.config.path && !await this.requestMatcher.match(this.config.path)) {
34
+ await next();
35
+ return;
36
+ }
37
+ const executor = (resolve, reject) => {
38
+ const opts = this.config.options;
39
+ if (!opts.setHeaders) {
40
+ opts.setHeaders = (res, path) => {
41
+ if (opts.headers) {
42
+ Object.keys(opts.headers).forEach(key => res.setHeader(key, opts.headers[key]));
43
+ }
44
+ if (serve_static_1.mime.lookup(path) === http_1.MediaType.TEXT_HTML) {
45
+ // Custom Cache-Control for HTML files
46
+ res.setHeader(http_1.HttpHeaders.CACHE_CONTROL, `public, max-age=${opts.htmlMaxAge / 1000}`);
47
+ }
48
+ };
49
+ }
50
+ (0, serve_static_1.serveStatic)(this.config.root, this.config.options)(ctx.request, ctx.response, ((err) => {
51
+ const url = ctx.request.url;
52
+ if (url !== '/index.html') {
53
+ if (!this.config.spa) {
54
+ next().then(resolve).catch(reject);
55
+ return;
56
+ }
57
+ ctx.request.url = '/index.html';
58
+ executor(resolve, reject);
59
+ }
60
+ else if (err) {
61
+ reject(err);
62
+ }
63
+ else {
64
+ next().then(resolve).catch(reject);
65
+ }
66
+ }));
67
+ };
68
+ return new Promise(executor);
69
+ }
70
+ };
71
+ __decorate([
72
+ (0, core_1.Value)('cell["serve-static"]'),
73
+ __metadata("design:type", Object)
74
+ ], ServeStaticMiddleware.prototype, "config", void 0);
75
+ __decorate([
76
+ (0, core_1.Value)(web_1.SERVER_PATH),
77
+ __metadata("design:type", String)
78
+ ], ServeStaticMiddleware.prototype, "path", void 0);
79
+ __decorate([
80
+ (0, core_1.Autowired)(node_1.RequestMatcher),
81
+ __metadata("design:type", Object)
82
+ ], ServeStaticMiddleware.prototype, "requestMatcher", void 0);
83
+ ServeStaticMiddleware = __decorate([
84
+ (0, core_1.Component)(node_1.Middleware)
85
+ ], ServeStaticMiddleware);
86
+ exports.ServeStaticMiddleware = ServeStaticMiddleware;
87
+ //# sourceMappingURL=serve-static-middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serve-static-middleware.js","sourceRoot":"","sources":["../../src/node/serve-static-middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+CAAqG;AACrG,iDAAiD;AACjD,uCAA2D;AAC3D,uCAAkE;AAClE,qCAA0C;AAGnC,IAAM,qBAAqB,GAA3B,MAAM,qBAAqB;IAA3B;QA+DH,aAAQ,GAAG,+BAAwB,GAAG,GAAG,CAAC;IAE9C,CAAC;IAtDG,KAAK,CAAC,MAAM,CAAC,GAAY,EAAE,IAAyB;QAChD,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC;QACzC,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC;QAElC,IAAI,CAAC,CAAC,MAAM,KAAK,iBAAU,CAAC,GAAG,IAAI,MAAM,KAAK,iBAAU,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,MAAM,EAAE;YACtG,MAAM,IAAI,EAAE,CAAC;YACb,OAAO;SACV;QAED,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,OA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ACvB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;wBAClB,IAAI,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;wBACnC,OAAO;qBACV;oBACD,GAAG,CAAC,OAAO,CAAC,GAAG,GAAG,aAAa,CAAC;oBAChC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;iBAC7B;qBAAM,IAAI,GAAG,EAAE;oBACZ,MAAM,CAAC,GAAG,CAAC,CAAC;iBACf;qBAAM;oBACH,IAAI,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;iBACtC;YACL,CAAC,CAAQ,CAAC,CAAC;QACf,CAAC,CAAC;QAEF,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC,CAAC;IACjC,CAAC;CAGJ,CAAA;AA/DG;IAAC,IAAA,YAAK,EAAC,sBAAsB,CAAC;;qDAC+D;AAE7F;IAAC,IAAA,YAAK,EAAC,iBAAW,CAAC;;mDACI;AAEvB;IAAC,IAAA,gBAAS,EAAC,qBAAc,CAAC;;6DACwB;AATzC,qBAAqB;IADjC,IAAA,gBAAS,EAAC,iBAAU,CAAC;GACT,qBAAqB,CAiEjC;AAjEY,sDAAqB"}
@@ -0,0 +1,4 @@
1
+ import type { ServeStaticOption } from './type';
2
+ export declare const serveStatic: (root: string, opts: ServeStaticOption) => (req: any, res: any, next: Function) => any;
3
+ export declare const mime: typeof import("mime");
4
+ //# sourceMappingURL=serve-static.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serve-static.d.ts","sourceRoot":"","sources":["../../src/node/serve-static.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAKhD,eAAO,MAAM,WAAW,SAAU,MAAM,QAAQ,iBAAiB,WAShD,GAAG,OAAO,GAAG,QAAQ,QAAQ,QA0F7C,CAAC;AAEF,eAAO,MAAM,IAAI,uBAAY,CAAC"}
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mime = exports.serveStatic = void 0;
4
+ // utilities
5
+ const send = require("send");
6
+ const path_1 = require("path");
7
+ const serveStatic = (root, opts) => {
8
+ const { fallthrough = true, baseHref, setHeaders, redirect } = opts;
9
+ const onDirectory = redirect ?
10
+ createRedirectDirectoryListener :
11
+ createNotFoundDirectoryListener;
12
+ opts.root = (0, path_1.resolve)(root);
13
+ return (req, res, next) => {
14
+ if (req.method !== 'GET' && req.method !== 'HEAD') {
15
+ if (fallthrough) {
16
+ return next();
17
+ }
18
+ // method not allowed
19
+ res.statusCode = 405;
20
+ res.setHeader('Allow', 'GET, HEAD');
21
+ res.setHeader('Content-Length', '0');
22
+ res.end();
23
+ next();
24
+ return;
25
+ }
26
+ let forwardError = !fallthrough;
27
+ const url = new URL(`${req.url.replace(baseHref, '/')}`, `http://${req.headers.host}`);
28
+ const stream = send(req, url.pathname, opts);
29
+ stream.on('directory', onDirectory(url));
30
+ if (setHeaders) {
31
+ stream.on('headers', setHeaders);
32
+ }
33
+ if (fallthrough) {
34
+ stream.on('file', function onFile() {
35
+ // once file is determined, always forward error
36
+ forwardError = true;
37
+ });
38
+ }
39
+ // forward errors
40
+ stream.on('error', function error(err) {
41
+ if (forwardError || !(err.statusCode < 500)) {
42
+ next(err);
43
+ return;
44
+ }
45
+ next();
46
+ });
47
+ // pipe
48
+ stream.pipe(res);
49
+ };
50
+ function createNotFoundDirectoryListener() {
51
+ return function notFound() {
52
+ // @ts-ignore
53
+ this.error(404);
54
+ };
55
+ }
56
+ function createRedirectDirectoryListener(url) {
57
+ return (res) => {
58
+ // @ts-ignore
59
+ if (this.hasTrailingSlash()) {
60
+ // @ts-ignore
61
+ this.error(404);
62
+ return;
63
+ }
64
+ // reformat the URL
65
+ const loc = encodeURI(url.href);
66
+ const doc = createHtmlDocument('Redirecting', 'Redirecting to <a href="' + loc + '">' +
67
+ loc + '</a>');
68
+ // send redirect response
69
+ res.statusCode = 301;
70
+ res.setHeader('Content-Type', 'text/html; charset=UTF-8');
71
+ res.setHeader('Content-Length', Buffer.byteLength(doc));
72
+ res.setHeader('Content-Security-Policy', "default-src 'none'");
73
+ res.setHeader('X-Content-Type-Options', 'nosniff');
74
+ res.setHeader('Location', loc);
75
+ res.end(doc);
76
+ };
77
+ }
78
+ function createHtmlDocument(title, body) {
79
+ return '<!DOCTYPE html>\n' +
80
+ '<html lang="en">\n' +
81
+ '<head>\n' +
82
+ '<meta charset="utf-8">\n' +
83
+ '<title>' + title + '</title>\n' +
84
+ '</head>\n' +
85
+ '<body>\n' +
86
+ '<pre>' + body + '</pre>\n' +
87
+ '</body>\n' +
88
+ '</html>\n';
89
+ }
90
+ };
91
+ exports.serveStatic = serveStatic;
92
+ exports.mime = send.mime;
93
+ //# sourceMappingURL=serve-static.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serve-static.js","sourceRoot":"","sources":["../../src/node/serve-static.ts"],"names":[],"mappings":";;;AAEA,YAAY;AACZ,6BAA6B;AAC7B,+BAA+B;AAExB,MAAM,WAAW,GAAG,CAAC,IAAY,EAAE,IAAuB,EAAE,EAAE;IAEjE,MAAM,EAAE,WAAW,GAAG,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC;IACpE,MAAM,WAAW,GAAG,QAAQ,CAAC,CAAC;QAC1B,+BAA+B,CAAC,CAAC;QACjC,+BAA+B,CAAC;IAEpC,IAAI,CAAC,IAAI,GAAG,IAAA,cAAO,EAAC,IAAI,CAAC,CAAC;IAE1B,OAAO,CAAC,GAAQ,EAAE,GAAQ,EAAE,IAAc,EAAE,EAAE;QAC1C,IAAI,GAAG,CAAC,MAAM,KAAK,KAAK,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM,EAAE;YAC/C,IAAI,WAAW,EAAE;gBACb,OAAO,IAAI,EAAE,CAA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ACX,WAAW,CAAC;IACpB,CAAC;AAEL,CAAC,CAAC;AAnGW,QAAA,WAAW,eAmGtB;AAEW,QAAA,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC"}
@@ -0,0 +1,16 @@
1
+ export interface ServeStaticOption {
2
+ fallthrough?: boolean;
3
+ maxage?: number;
4
+ setHeaders?: (res: any, path: string, stat: any) => void;
5
+ redirect?: boolean;
6
+ extensions?: false | Array<string>;
7
+ dotfiles?: 'allow' | 'deny' | 'ignore';
8
+ acceptRanges?: any;
9
+ cacheControl?: any;
10
+ immutable?: boolean;
11
+ index?: any;
12
+ lastModified?: any;
13
+ baseHref: string;
14
+ root: string;
15
+ }
16
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../src/node/type.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,iBAAiB;IAChC,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,IAAI,CAAA;IACxD,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,CAAA;IAClC,QAAQ,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,QAAQ,CAAA;IACtC,YAAY,CAAC,EAAE,GAAG,CAAA;IAClB,YAAY,CAAC,EAAE,GAAG,CAAA;IAClB,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,KAAK,CAAC,EAAE,GAAG,CAAA;IACX,YAAY,CAAC,EAAE,GAAG,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../src/node/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=package.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package.spec.d.ts","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ describe('serve static package', () => {
2
+ it('support code coverage statistics', () => { });
3
+ });
4
+ //# sourceMappingURL=package.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package.spec.js","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":"AAAA,QAAQ,CAAC,sBAAsB,EAAE,GAAG,EAAE;IAElC,EAAE,CAAC,kCAAkC,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;AACrD,CAAC,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@celljs/serve-static",
3
+ "version": "3.0.0",
4
+ "description": "",
5
+ "main": "lib/node/index.js",
6
+ "typings": "lib/node/index.d.ts",
7
+ "dependencies": {
8
+ "@celljs/core": "3.0.0",
9
+ "@celljs/http": "3.0.0",
10
+ "@celljs/web": "3.0.0",
11
+ "send": "^0.17.1"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "keywords": [
17
+ "cell-component"
18
+ ],
19
+ "license": "MIT",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/cellbang/cell.git"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/cellbang/cell/issues"
26
+ },
27
+ "homepage": "https://github.com/cellbang/cell",
28
+ "files": [
29
+ "lib",
30
+ "src",
31
+ "cell.yml",
32
+ "cell-remote.yml"
33
+ ],
34
+ "scripts": {
35
+ "lint": "cell-component lint",
36
+ "build": "cell-component build",
37
+ "watch": "cell-component watch",
38
+ "clean": "cell-component clean",
39
+ "test": "cell-component test:js"
40
+ },
41
+ "devDependencies": {
42
+ "@celljs/cli-service": "3.0.0",
43
+ "@celljs/component": "3.0.0",
44
+ "@types/send": "^0.17.0"
45
+ },
46
+ "gitHead": "c0ef8d269e33a795406bcbb240cac4b845c6c24c"
47
+ }
@@ -0,0 +1,15 @@
1
+ import { WebpackContext, ConfigurationContext } from '@celljs/cli-service';
2
+
3
+ export default async (context: WebpackContext) => {
4
+ const { dev, configurations } = context;
5
+ const config = ConfigurationContext.getFrontendConfiguration(configurations);
6
+ if (dev && config) {
7
+ config
8
+ .devServer
9
+ .merge({
10
+ devMiddleware: {
11
+ writeToDisk: true
12
+ }
13
+ });
14
+ }
15
+ };
@@ -0,0 +1,2 @@
1
+
2
+ export * from './serve-static-middleware';
@@ -0,0 +1,4 @@
1
+ import { autoBind } from '@celljs/core';
2
+ import './index';
3
+
4
+ export default autoBind();
@@ -0,0 +1,73 @@
1
+ import { Middleware, Context, RequestMatcher, HTTP_MIDDLEWARE_PRIORITY } from '@celljs/web/lib/node';
2
+ import {serveStatic, mime} from './serve-static';
3
+ import { Value, Component, Autowired } from '@celljs/core';
4
+ import { HttpMethod, MediaType, HttpHeaders } from '@celljs/http';
5
+ import { SERVER_PATH } from '@celljs/web';
6
+ import { OutgoingMessage } from 'http';
7
+ @Component(Middleware)
8
+ export class ServeStaticMiddleware implements Middleware {
9
+
10
+ @Value('cell["serve-static"]')
11
+ protected config: { spa: boolean, root: string, path: string, apiPath: string, options: any};
12
+
13
+ @Value(SERVER_PATH)
14
+ protected path: string;
15
+
16
+ @Autowired(RequestMatcher)
17
+ protected readonly requestMatcher: RequestMatcher;
18
+
19
+ async handle(ctx: Context, next: () => Promise<void>): Promise<void> {
20
+ this.config.options.baseHref = this.path;
21
+ const method = ctx.request.method;
22
+
23
+ if (!(method === HttpMethod.GET || method === HttpMethod.HEAD) || ctx.request.query['static'] === 'skip') {
24
+ await next();
25
+ return;
26
+ }
27
+
28
+ if (this.config.apiPath && await this.requestMatcher.match(this.config.apiPath)) {
29
+ await next();
30
+ return;
31
+ }
32
+
33
+ if (this.config.path && !await this.requestMatcher.match(this.config.path)) {
34
+ await next();
35
+ return;
36
+ }
37
+
38
+ const executor = (resolve: any, reject: any) => {
39
+ const opts = this.config.options;
40
+ if (!opts.setHeaders) {
41
+ opts.setHeaders = (res: OutgoingMessage, path: string) => {
42
+ if (opts.headers) {
43
+ Object.keys(opts.headers).forEach(key => res.setHeader(key, opts.headers[key]));
44
+ }
45
+ if ((mime as any).lookup!(path) === MediaType.TEXT_HTML) {
46
+ // Custom Cache-Control for HTML files
47
+ res.setHeader(HttpHeaders.CACHE_CONTROL, `public, max-age=${opts.htmlMaxAge / 1000}`);
48
+ }
49
+ };
50
+ }
51
+
52
+ serveStatic(this.config.root, this.config.options)(ctx.request as any, ctx.response as any, ((err: any) => {
53
+ const url = ctx.request.url;
54
+ if (url !== '/index.html') {
55
+ if (!this.config.spa) {
56
+ next().then(resolve).catch(reject);
57
+ return;
58
+ }
59
+ ctx.request.url = '/index.html';
60
+ executor(resolve, reject);
61
+ } else if (err) {
62
+ reject(err);
63
+ } else {
64
+ next().then(resolve).catch(reject);
65
+ }
66
+ }) as any);
67
+ };
68
+
69
+ return new Promise(executor);
70
+ }
71
+ priority = HTTP_MIDDLEWARE_PRIORITY + 500;
72
+
73
+ }
@@ -0,0 +1,108 @@
1
+ // types
2
+ import type { ServeStaticOption } from './type';
3
+ // utilities
4
+ import * as send from 'send';
5
+ import { resolve } from 'path';
6
+
7
+ export const serveStatic = (root: string, opts: ServeStaticOption) => {
8
+
9
+ const { fallthrough = true, baseHref, setHeaders, redirect } = opts;
10
+ const onDirectory = redirect ?
11
+ createRedirectDirectoryListener :
12
+ createNotFoundDirectoryListener;
13
+
14
+ opts.root = resolve(root);
15
+
16
+ return (req: any, res: any, next: Function) => {
17
+ if (req.method !== 'GET' && req.method !== 'HEAD') {
18
+ if (fallthrough) {
19
+ return next();
20
+ }
21
+ // method not allowed
22
+ res.statusCode = 405;
23
+ res.setHeader('Allow', 'GET, HEAD');
24
+ res.setHeader('Content-Length', '0');
25
+ res.end();
26
+ next();
27
+ return;
28
+ }
29
+
30
+ let forwardError = !fallthrough;
31
+
32
+ const url: URL = new URL(`${req.url.replace(baseHref, '/')}`, `http://${req.headers.host}`);
33
+
34
+ const stream = send(req, url.pathname, opts) as any;
35
+
36
+ stream.on('directory', onDirectory(url));
37
+
38
+ if (setHeaders) {
39
+ stream.on('headers', setHeaders);
40
+ }
41
+
42
+ if (fallthrough) {
43
+ stream.on('file', function onFile() {
44
+ // once file is determined, always forward error
45
+ forwardError = true;
46
+ });
47
+ }
48
+
49
+ // forward errors
50
+ stream.on('error', function error(err: any) {
51
+ if (forwardError || !(err.statusCode < 500)) {
52
+ next(err);
53
+ return;
54
+ }
55
+
56
+ next();
57
+ });
58
+ // pipe
59
+ stream.pipe(res);
60
+ };
61
+
62
+ function createNotFoundDirectoryListener() {
63
+ return function notFound() {
64
+ // @ts-ignore
65
+ this.error(404);
66
+ };
67
+ }
68
+
69
+ function createRedirectDirectoryListener(url: URL) {
70
+ return (res: any) => {
71
+ // @ts-ignore
72
+ if (this.hasTrailingSlash()) {
73
+ // @ts-ignore
74
+ this.error(404);
75
+ return;
76
+ }
77
+ // reformat the URL
78
+ const loc = encodeURI(url.href);
79
+ const doc = createHtmlDocument('Redirecting', 'Redirecting to <a href="' + loc + '">' +
80
+ loc + '</a>');
81
+
82
+ // send redirect response
83
+ res.statusCode = 301;
84
+ res.setHeader('Content-Type', 'text/html; charset=UTF-8');
85
+ res.setHeader('Content-Length', Buffer.byteLength(doc));
86
+ res.setHeader('Content-Security-Policy', "default-src 'none'");
87
+ res.setHeader('X-Content-Type-Options', 'nosniff');
88
+ res.setHeader('Location', loc);
89
+ res.end(doc);
90
+ };
91
+ }
92
+
93
+ function createHtmlDocument(title: string, body: string) {
94
+ return '<!DOCTYPE html>\n' +
95
+ '<html lang="en">\n' +
96
+ '<head>\n' +
97
+ '<meta charset="utf-8">\n' +
98
+ '<title>' + title + '</title>\n' +
99
+ '</head>\n' +
100
+ '<body>\n' +
101
+ '<pre>' + body + '</pre>\n' +
102
+ '</body>\n' +
103
+ '</html>\n';
104
+ }
105
+
106
+ };
107
+
108
+ export const mime = send.mime;
@@ -0,0 +1,15 @@
1
+ export interface ServeStaticOption {
2
+ fallthrough?: boolean
3
+ maxage?: number
4
+ setHeaders?: (res: any, path: string, stat: any) => void
5
+ redirect?: boolean,
6
+ extensions?: false | Array<string>
7
+ dotfiles?: 'allow' | 'deny' | 'ignore'
8
+ acceptRanges?: any
9
+ cacheControl?: any
10
+ immutable?: boolean
11
+ index?: any
12
+ lastModified?: any
13
+ baseHref: string,
14
+ root: string,
15
+ }
@@ -0,0 +1,4 @@
1
+ describe('serve static package', () => {
2
+
3
+ it('support code coverage statistics', () => {});
4
+ });