@celljs/serve-static 4.0.0 → 4.2.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.
- package/README.md +5 -1
- package/esm/node/embedded-assets.d.ts +1 -1
- package/esm/node/embedded-assets.js +15 -11
- package/esm/node/embedded-assets.js.map +1 -1
- package/esm/node/mime.d.ts +6 -0
- package/esm/node/mime.js +28 -0
- package/esm/node/mime.js.map +1 -0
- package/esm/node/module.d.ts +1 -1
- package/esm/node/serve-static-middleware.d.ts +18 -1
- package/esm/node/serve-static-middleware.js +74 -30
- package/esm/node/serve-static-middleware.js.map +1 -1
- package/esm/node/serve-static.d.ts +9 -2
- package/esm/node/serve-static.js +258 -65
- package/esm/node/serve-static.js.map +1 -1
- package/lib/node/embedded-assets.d.ts +1 -1
- package/lib/node/embedded-assets.d.ts.map +1 -1
- package/lib/node/embedded-assets.js +15 -14
- package/lib/node/embedded-assets.js.map +1 -1
- package/lib/node/mime.d.ts +7 -0
- package/lib/node/mime.d.ts.map +1 -0
- package/lib/node/mime.js +31 -0
- package/lib/node/mime.js.map +1 -0
- package/lib/node/module.d.ts +1 -1
- package/lib/node/serve-static-middleware.d.ts +18 -1
- package/lib/node/serve-static-middleware.d.ts.map +1 -1
- package/lib/node/serve-static-middleware.js +72 -28
- package/lib/node/serve-static-middleware.js.map +1 -1
- package/lib/node/serve-static-middleware.spec.d.ts +2 -0
- package/lib/node/serve-static-middleware.spec.d.ts.map +1 -0
- package/lib/node/serve-static-middleware.spec.js +232 -0
- package/lib/node/serve-static-middleware.spec.js.map +1 -0
- package/lib/node/serve-static.d.ts +9 -2
- package/lib/node/serve-static.d.ts.map +1 -1
- package/lib/node/serve-static.js +259 -69
- package/lib/node/serve-static.js.map +1 -1
- package/lib/node/serve-static.spec.d.ts +2 -0
- package/lib/node/serve-static.spec.d.ts.map +1 -0
- package/lib/node/serve-static.spec.js +139 -0
- package/lib/node/serve-static.spec.js.map +1 -0
- package/package.json +7 -9
- package/src/node/embedded-assets.ts +16 -12
- package/src/node/mime.ts +27 -0
- package/src/node/serve-static-middleware.spec.ts +284 -0
- package/src/node/serve-static-middleware.ts +79 -31
- package/src/node/serve-static.spec.ts +151 -0
- package/src/node/serve-static.ts +259 -74
package/README.md
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
# Cell - Serve Static Component
|
|
1
|
+
# Cell - Serve Static Component
|
|
2
|
+
|
|
3
|
+
Serves the frontend build (`cell.serve-static.root`, default `${paths.public}`) with Cell's own file sender: Content-Type, a weak ETag, Last-Modified, Cache-Control, `304` for fresh requests, `412` for failed `If-Match` / `If-Unmodified-Since`, single byte ranges (`206` / `416`), dotfiles ignored, and paths that leave the root refused. With `spa: true` unknown paths get `/index.html`.
|
|
4
|
+
|
|
5
|
+
`cell.serve-static.options`: `maxAge` (milliseconds), `immutable`, `htmlMaxAge` (Cache-Control of HTML files, default 0), `headers` (added to every file), `index` (default `index.html`, `false` for none), `extensions` (tried when a path has none), `dotfiles` (`ignore` default, `allow`, `deny`), `redirect` (`301` from a directory to its path with `/`), `acceptRanges`, `cacheControl`, `etag`, `lastModified` (default `true`), `fallthrough` (default `true`).
|
|
@@ -16,5 +16,5 @@ export declare namespace EmbeddedAssets {
|
|
|
16
16
|
/** The counterpart of `serveStatic`: same options, same `next()` when a file is not there. */
|
|
17
17
|
function serve(assets: Map<string, EmbeddedAsset>, opts: ServeStaticOption & {
|
|
18
18
|
maxAge?: number;
|
|
19
|
-
}): (req: any, res: any, next: (err?: unknown) => void) => void;
|
|
19
|
+
}): (req: any, res: any, next: (err?: unknown) => void, url?: string) => void;
|
|
20
20
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { MimeUtil } from './mime.js';
|
|
2
2
|
/**
|
|
3
3
|
* Frontend files that a `cell build --single` bundle registers before the application starts, by URL
|
|
4
4
|
* path (`/index.html`, `/assets/index-abc.js`). When they are there, they are served from memory
|
|
@@ -7,22 +7,28 @@ import send from 'send';
|
|
|
7
7
|
export var EmbeddedAssets;
|
|
8
8
|
(function (EmbeddedAssets) {
|
|
9
9
|
EmbeddedAssets.KEY = Symbol.for('@celljs/serve-static/assets');
|
|
10
|
-
|
|
10
|
+
/** Decoded bodies, per table of assets: a table that is registered again is decoded again. */
|
|
11
|
+
const decoded = new WeakMap();
|
|
11
12
|
function get() {
|
|
12
13
|
return globalThis[EmbeddedAssets.KEY];
|
|
13
14
|
}
|
|
14
15
|
EmbeddedAssets.get = get;
|
|
15
|
-
function content(path, asset) {
|
|
16
|
-
let
|
|
16
|
+
function content(assets, path, asset) {
|
|
17
|
+
let bodies = decoded.get(assets);
|
|
18
|
+
if (!bodies) {
|
|
19
|
+
bodies = new Map();
|
|
20
|
+
decoded.set(assets, bodies);
|
|
21
|
+
}
|
|
22
|
+
let body = bodies.get(path);
|
|
17
23
|
if (!body) {
|
|
18
24
|
body = Buffer.from(asset.body, 'base64');
|
|
19
|
-
|
|
25
|
+
bodies.set(path, body);
|
|
20
26
|
}
|
|
21
27
|
return body;
|
|
22
28
|
}
|
|
23
29
|
/** The counterpart of `serveStatic`: same options, same `next()` when a file is not there. */
|
|
24
30
|
function serve(assets, opts) {
|
|
25
|
-
return (req, res, next) => {
|
|
31
|
+
return (req, res, next, url = req.url) => {
|
|
26
32
|
var _a;
|
|
27
33
|
var _b, _c;
|
|
28
34
|
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
@@ -31,7 +37,7 @@ export var EmbeddedAssets;
|
|
|
31
37
|
}
|
|
32
38
|
let path;
|
|
33
39
|
try {
|
|
34
|
-
path = decodeURIComponent(new URL(
|
|
40
|
+
path = decodeURIComponent(new URL(url.replace(opts.baseHref, '/'), 'http://localhost').pathname);
|
|
35
41
|
}
|
|
36
42
|
catch (_d) {
|
|
37
43
|
next();
|
|
@@ -45,9 +51,7 @@ export var EmbeddedAssets;
|
|
|
45
51
|
next();
|
|
46
52
|
return;
|
|
47
53
|
}
|
|
48
|
-
|
|
49
|
-
const charset = send.mime.charsets.lookup(type);
|
|
50
|
-
res.setHeader('Content-Type', charset ? `${type}; charset=${charset}` : type);
|
|
54
|
+
res.setHeader('Content-Type', MimeUtil.contentType(path));
|
|
51
55
|
res.setHeader('ETag', asset.etag);
|
|
52
56
|
if (opts.cacheControl !== false) {
|
|
53
57
|
const maxAge = Math.floor(Math.min(Math.max(0, Number((_c = (_b = opts.maxAge) !== null && _b !== void 0 ? _b : opts.maxage) !== null && _c !== void 0 ? _c : 0)), 31536000000) / 1000);
|
|
@@ -59,7 +63,7 @@ export var EmbeddedAssets;
|
|
|
59
63
|
res.end();
|
|
60
64
|
return;
|
|
61
65
|
}
|
|
62
|
-
const body = content(path, asset);
|
|
66
|
+
const body = content(assets, path, asset);
|
|
63
67
|
res.statusCode = 200;
|
|
64
68
|
res.setHeader('Content-Length', body.length);
|
|
65
69
|
res.end(req.method === 'HEAD' ? undefined : body);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"embedded-assets.js","sourceRoot":"","sources":["../../src/node/embedded-assets.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"embedded-assets.js","sourceRoot":"","sources":["../../src/node/embedded-assets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAUlC;;;;GAIG;AACH,MAAM,KAAW,cAAc,CAgE9B;AAhED,WAAiB,cAAc;IACd,kBAAG,GAAG,MAAM,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;IAE7D,8FAA8F;IAC9F,MAAM,OAAO,GAAG,IAAI,OAAO,EAAmD,CAAC;IAE/E,SAAgB,GAAG;QACf,OAAQ,UAAqE,CAAC,kBAAG,CAAC,CAAC;IACvF,CAAC;IAFe,eAAA,GAAG,MAElB,CAAA;IAED,SAAS,OAAO,CAAC,MAAkC,EAAE,IAAY,EAAE,KAAoB;QACnF,IAAI,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,C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AAC,IAAI,CAAC,CAAC;QACtD,CAAC,CAAC;IACN,CAAC;IAtCe,eAAA,KAAK,QAsCpB,CAAA;AACL,CAAC,EAhEgB,cAAc,KAAd,cAAc,QAgE9B"}
|
package/esm/node/mime.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Types of the files a frontend build has; anything else is `application/octet-stream`. */
|
|
2
|
+
const TYPES = {
|
|
3
|
+
html: 'text/html', htm: 'text/html', css: 'text/css', txt: 'text/plain', md: 'text/markdown', csv: 'text/csv',
|
|
4
|
+
js: 'application/javascript', mjs: 'application/javascript', cjs: 'application/javascript',
|
|
5
|
+
json: 'application/json', map: 'application/json', webmanifest: 'application/manifest+json',
|
|
6
|
+
xml: 'application/xml', pdf: 'application/pdf', zip: 'application/zip', wasm: 'application/wasm',
|
|
7
|
+
svg: 'image/svg+xml', png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif',
|
|
8
|
+
webp: 'image/webp', avif: 'image/avif', ico: 'image/x-icon', bmp: 'image/bmp',
|
|
9
|
+
woff: 'font/woff', woff2: 'font/woff2', ttf: 'font/ttf', otf: 'font/otf', eot: 'application/vnd.ms-fontobject',
|
|
10
|
+
mp4: 'video/mp4', webm: 'video/webm', mp3: 'audio/mpeg', wav: 'audio/wav', ogg: 'audio/ogg'
|
|
11
|
+
};
|
|
12
|
+
const DEFAULT_TYPE = 'application/octet-stream';
|
|
13
|
+
export var MimeUtil;
|
|
14
|
+
(function (MimeUtil) {
|
|
15
|
+
/** `/assets/app.js` → `application/javascript`. */
|
|
16
|
+
function lookup(path) {
|
|
17
|
+
var _a;
|
|
18
|
+
return (_a = TYPES[path.replace(/^.*[./\\]/, '').toLowerCase()]) !== null && _a !== void 0 ? _a : DEFAULT_TYPE;
|
|
19
|
+
}
|
|
20
|
+
MimeUtil.lookup = lookup;
|
|
21
|
+
/** The Content-Type header: with `charset=UTF-8` for text, JavaScript and JSON. */
|
|
22
|
+
function contentType(path) {
|
|
23
|
+
const type = lookup(path);
|
|
24
|
+
return /^text\/|^application\/(?:javascript|json)$/.test(type) ? `${type}; charset=UTF-8` : type;
|
|
25
|
+
}
|
|
26
|
+
MimeUtil.contentType = contentType;
|
|
27
|
+
})(MimeUtil || (MimeUtil = {}));
|
|
28
|
+
//# sourceMappingURL=mime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mime.js","sourceRoot":"","sources":["../../src/node/mime.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,MAAM,KAAK,GAA2B;IAClC,IAAI,EAAE,WAAW,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,YAAY,EAAE,EAAE,EAAE,eAAe,EAAE,GAAG,EAAE,UAAU;IAC7G,EAAE,EAAE,wBAAwB,EAAE,GAAG,EAAE,wBAAwB,EAAE,GAAG,EAAE,wBAAwB;IAC1F,IAAI,EAAE,kBAAkB,EAAE,GAAG,EAAE,kBAAkB,EAAE,WAAW,EAAE,2BAA2B;IAC3F,GAAG,EAAE,iBAAiB,EAAE,GAAG,EAAE,iBAAiB,EAAE,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,kBAAkB;IAChG,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,EAAE,GAAG,EAAE,WAAW;IAC/F,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,EAAE,GAAG,EAAE,cAAc,EAAE,GAAG,EAAE,WAAW;IAC7E,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,+BAA+B;IAC9G,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,YAAY,EAAE,GAAG,EAAE,YAAY,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,WAAW;CAC9F,CAAC;AAEF,MAAM,YAAY,GAAG,0BAA0B,CAAC;AAEhD,MAAM,KAAW,QAAQ,CAYxB;AAZD,WAAiB,QAAQ;IAErB,mDAAmD;IACnD,SAAgB,MAAM,CAAC,IAAY;;QAC/B,aAAO,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,mCAAI,YAAY,CAAC;IAC9E,CAAC;IAFe,SAAA,MAAM,SAErB,CAAA;IAED,mFAAmF;IACnF,SAAgB,WAAW,CAAC,IAAY;QACpC,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;QAC1B,OAAO,4CAA4C,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,iBAAiB,CAAC,CAAC,CAAC,IAAI,CAAC;IACrG,CAAC;IAHe,SAAA,WAAW,cAG1B,CAAA;AACL,CAAC,EAZgB,QAAQ,KAAR,QAAQ,QAYxB"}
|
package/esm/node/module.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { Middleware, Context, RequestMatcher } from '@celljs/web/lib/node';
|
|
2
|
+
import { OutgoingMessage } from 'http';
|
|
3
|
+
import type { ServeStaticOption } from './type.js';
|
|
4
|
+
type Serve = (req: any, res: any, next: (err?: any) => void, url?: string) => void;
|
|
2
5
|
export declare class ServeStaticMiddleware implements Middleware {
|
|
3
6
|
protected config: {
|
|
4
7
|
spa: boolean;
|
|
@@ -13,7 +16,21 @@ export declare class ServeStaticMiddleware implements Middleware {
|
|
|
13
16
|
* Whether there is a frontend to serve: embedded in the bundle (`--single`) or on disk. On
|
|
14
17
|
* Cloudflare Workers the platform serves the frontend and the worker has no copy.
|
|
15
18
|
*/
|
|
16
|
-
protected hasFrontend
|
|
19
|
+
protected hasFrontend: boolean;
|
|
20
|
+
/** Options of the file server: a copy of `cell.serve-static.options`, which stays untouched. */
|
|
21
|
+
protected options: ServeStaticOption & Record<string, any>;
|
|
22
|
+
protected serve: Serve;
|
|
23
|
+
/**
|
|
24
|
+
* Sets up the file server once. Cloudflare Workers allow I/O (`existsSync`) only while a request is
|
|
25
|
+
* handled; the application, and with it this component, starts on the first request there.
|
|
26
|
+
*/
|
|
27
|
+
protected init(): void;
|
|
28
|
+
/**
|
|
29
|
+
* Adds the configured `headers` to every file, and gives HTML files their own Cache-Control
|
|
30
|
+
* (`htmlMaxAge` in milliseconds, default 0) so that a new build of the page is picked up.
|
|
31
|
+
*/
|
|
32
|
+
protected createSetHeaders(headers: Record<string, string> | undefined, htmlMaxAge: unknown): (res: OutgoingMessage, path: string) => void;
|
|
17
33
|
handle(ctx: Context, next: () => Promise<void>): Promise<void>;
|
|
18
34
|
priority: number;
|
|
19
35
|
}
|
|
36
|
+
export {};
|
|
@@ -8,21 +8,56 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
10
|
import { Middleware, RequestMatcher, HTTP_MIDDLEWARE_PRIORITY } from '@celljs/web/lib/node';
|
|
11
|
-
import { serveStatic
|
|
11
|
+
import { serveStatic } from './serve-static.js';
|
|
12
|
+
import { MimeUtil } from './mime.js';
|
|
12
13
|
import { EmbeddedAssets } from './embedded-assets.js';
|
|
13
|
-
import { Value, Component, Autowired } from '@celljs/core';
|
|
14
|
+
import { Value, Component, Autowired, PostConstruct } from '@celljs/core';
|
|
14
15
|
import { HttpMethod, MediaType, HttpHeaders } from '@celljs/http';
|
|
15
16
|
import { SERVER_PATH } from '@celljs/web';
|
|
16
17
|
import { existsSync } from 'fs';
|
|
18
|
+
const SPA_INDEX = '/index.html';
|
|
17
19
|
let ServeStaticMiddleware = class ServeStaticMiddleware {
|
|
18
20
|
constructor() {
|
|
19
21
|
this.priority = HTTP_MIDDLEWARE_PRIORITY + 500;
|
|
20
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Sets up the file server once. Cloudflare Workers allow I/O (`existsSync`) only while a request is
|
|
25
|
+
* handled; the application, and with it this component, starts on the first request there.
|
|
26
|
+
*/
|
|
27
|
+
init() {
|
|
28
|
+
var _a, _b;
|
|
29
|
+
var _c;
|
|
30
|
+
const { headers, htmlMaxAge, ...options } = (_a = this.config.options) !== null && _a !== void 0 ? _a : {};
|
|
31
|
+
this.options = { ...options, baseHref: this.path };
|
|
32
|
+
(_b = (_c = this.options).setHeaders) !== null && _b !== void 0 ? _b : (_c.setHeaders = this.createSetHeaders(headers, htmlMaxAge));
|
|
33
|
+
const embedded = EmbeddedAssets.get();
|
|
34
|
+
const root = this.config.root;
|
|
35
|
+
// No frontend (no `paths.public`, or nothing built there): every request goes on to the chain.
|
|
36
|
+
this.hasFrontend = !!embedded || (typeof root === 'string' && existsSync(root));
|
|
37
|
+
if (embedded) {
|
|
38
|
+
this.serve = EmbeddedAssets.serve(embedded, this.options);
|
|
39
|
+
}
|
|
40
|
+
else if (this.hasFrontend) {
|
|
41
|
+
this.serve = serveStatic(root, this.options);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Adds the configured `headers` to every file, and gives HTML files their own Cache-Control
|
|
46
|
+
* (`htmlMaxAge` in milliseconds, default 0) so that a new build of the page is picked up.
|
|
47
|
+
*/
|
|
48
|
+
createSetHeaders(headers, htmlMaxAge) {
|
|
49
|
+
const htmlCacheControl = `public, max-age=${Math.floor(Math.max(0, Number(htmlMaxAge !== null && htmlMaxAge !== void 0 ? htmlMaxAge : 0) || 0) / 1000)}`;
|
|
50
|
+
return (res, path) => {
|
|
51
|
+
if (headers) {
|
|
52
|
+
Object.keys(headers).forEach(key => res.setHeader(key, headers[key]));
|
|
53
|
+
}
|
|
54
|
+
if (MimeUtil.lookup(path) === MediaType.TEXT_HTML) {
|
|
55
|
+
res.setHeader(HttpHeaders.CACHE_CONTROL, htmlCacheControl);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
21
59
|
async handle(ctx, next) {
|
|
22
|
-
var _a;
|
|
23
|
-
this.config.options.baseHref = this.path;
|
|
24
60
|
const method = ctx.request.method;
|
|
25
|
-
(_a = this.hasFrontend) !== null && _a !== void 0 ? _a : (this.hasFrontend = !!EmbeddedAssets.get() || existsSync(this.config.root));
|
|
26
61
|
if (!this.hasFrontend || !(method === HttpMethod.GET || method === HttpMethod.HEAD) || ctx.request.query['static'] === 'skip') {
|
|
27
62
|
await next();
|
|
28
63
|
return;
|
|
@@ -35,40 +70,43 @@ let ServeStaticMiddleware = class ServeStaticMiddleware {
|
|
|
35
70
|
await next();
|
|
36
71
|
return;
|
|
37
72
|
}
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
const response = ctx.response;
|
|
75
|
+
// A file that is sent ends the response without calling back: the request is done then.
|
|
76
|
+
const done = () => {
|
|
77
|
+
stopWaiting();
|
|
78
|
+
resolve();
|
|
79
|
+
};
|
|
80
|
+
const stopWaiting = () => {
|
|
81
|
+
response.off('finish', done);
|
|
82
|
+
response.off('close', done);
|
|
83
|
+
};
|
|
84
|
+
// The rest of the chain answers instead: its outcome is this middleware's outcome.
|
|
85
|
+
const passOn = () => {
|
|
86
|
+
stopWaiting();
|
|
87
|
+
next().then(resolve, reject);
|
|
88
|
+
};
|
|
89
|
+
response.once('finish', done);
|
|
90
|
+
response.once('close', done);
|
|
91
|
+
// The SPA fallback serves /index.html for the request without changing request.url.
|
|
92
|
+
const attempt = (url) => this.serve(ctx.request, response, (err) => {
|
|
93
|
+
if (url !== SPA_INDEX) {
|
|
56
94
|
if (!this.config.spa) {
|
|
57
|
-
|
|
95
|
+
passOn();
|
|
58
96
|
return;
|
|
59
97
|
}
|
|
60
|
-
|
|
61
|
-
executor(resolve, reject);
|
|
98
|
+
attempt(SPA_INDEX);
|
|
62
99
|
}
|
|
63
100
|
else if (err) {
|
|
101
|
+
stopWaiting();
|
|
64
102
|
reject(err);
|
|
65
103
|
}
|
|
66
104
|
else {
|
|
67
|
-
|
|
105
|
+
passOn();
|
|
68
106
|
}
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
|
|
107
|
+
}, url);
|
|
108
|
+
attempt(ctx.request.url);
|
|
109
|
+
});
|
|
72
110
|
}
|
|
73
111
|
};
|
|
74
112
|
__decorate([
|
|
@@ -83,6 +121,12 @@ __decorate([
|
|
|
83
121
|
Autowired(RequestMatcher),
|
|
84
122
|
__metadata("design:type", Object)
|
|
85
123
|
], ServeStaticMiddleware.prototype, "requestMatcher", void 0);
|
|
124
|
+
__decorate([
|
|
125
|
+
PostConstruct(),
|
|
126
|
+
__metadata("design:type", Function),
|
|
127
|
+
__metadata("design:paramtypes", []),
|
|
128
|
+
__metadata("design:returntype", void 0)
|
|
129
|
+
], ServeStaticMiddleware.prototype, "init", null);
|
|
86
130
|
ServeStaticMiddleware = __decorate([
|
|
87
131
|
Component(Middleware)
|
|
88
132
|
], ServeStaticMiddleware);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serve-static-middleware.js","sourceRoot":"","sources":["../../src/node/serve-static-middleware.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,UAAU,EAAW,cAAc,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AACrG,OAAO,
|
|
1
|
+
{"version":3,"file":"serve-static-middleware.js","sourceRoot":"","sources":["../../src/node/serve-static-middleware.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,UAAU,EAAW,cAAc,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AACrG,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC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QAAQ,EAAE,CAAC,GAAS,EAAE,EAAE;gBAC7E,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;oBACpB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;wBACnB,MAAM,EAAE,CAAC;wBACT,OAAO;oBACX,CAAC;oBACD,OAAO,CAAC,SAAS,CAAC,CAAC;gBACvB,CAAC;qBAAM,IAAI,GAAG,EAAE,CAAC;oBACb,WAAW,EAAE,CAAC;oBACd,MAAM,CAAC,GAAG,CAAC,CAAC;gBAChB,CAAC;qBAAM,CAAC;oBACJ,MAAM,EAAE,CAAC;gBACb,CAAC;YACL,CAAC,EAAE,GAAG,CAAC,CAAC;YACR,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC7B,CAAC,CAAC,CAAC;IACP,CAAC;CAGJ,CAAA;AAhHa;IADT,KAAK,CAAC,sBAAsB,CAAC;;qDAC+D;AAGnF;IADT,KAAK,CAAC,WAAW,CAAC;;mDACI;AAGJ;IADlB,SAAS,CAAC,cAAc,CAAC;;6DACwB;AAkBxC;IADT,aAAa,EAAE;;;;iDAcf;AAxCQ,qBAAqB;IADjC,SAAS,CAAC,UAAU,CAAC;GACT,qBAAqB,CAmHjC"}
|
|
@@ -1,3 +1,10 @@
|
|
|
1
1
|
import type { ServeStaticOption } from './type.js';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
type Next = (err?: unknown) => void;
|
|
3
|
+
/**
|
|
4
|
+
* Serves files under `root` with the options and behavior of the `send` package: Content-Type, ETag,
|
|
5
|
+
* Last-Modified, Cache-Control, 304 for fresh requests, 412 for failed preconditions, single byte ranges,
|
|
6
|
+
* `index` files, `extensions`, and dotfiles ignored by default. `next()` is called when there is no
|
|
7
|
+
* file to send, `next(err)` when sending fails.
|
|
8
|
+
*/
|
|
9
|
+
export declare const serveStatic: (root: string, opts: ServeStaticOption & Record<string, any>) => (req: any, res: any, next: Next, url?: string) => void;
|
|
10
|
+
export {};
|