@celljs/serve-static 4.1.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.js +2 -4
- 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.js +3 -2
- 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 +256 -64
- package/esm/node/serve-static.js.map +1 -1
- package/lib/node/embedded-assets.d.ts.map +1 -1
- package/lib/node/embedded-assets.js +2 -7
- 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.map +1 -1
- package/lib/node/serve-static-middleware.js +2 -1
- package/lib/node/serve-static-middleware.js.map +1 -1
- package/lib/node/serve-static-middleware.spec.js +1 -1
- package/lib/node/serve-static-middleware.spec.js.map +1 -1
- 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 +257 -68
- 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 -8
- package/src/node/embedded-assets.ts +3 -5
- package/src/node/mime.ts +27 -0
- package/src/node/serve-static-middleware.spec.ts +2 -2
- package/src/node/serve-static-middleware.ts +4 -3
- package/src/node/serve-static.spec.ts +151 -0
- package/src/node/serve-static.ts +258 -74
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const bun_test_1 = require("bun:test");
|
|
4
|
+
const chai_1 = require("chai");
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const node_http_1 = require("node:http");
|
|
7
|
+
const node_os_1 = require("node:os");
|
|
8
|
+
const node_path_1 = require("node:path");
|
|
9
|
+
const serve_static_1 = require("./serve-static");
|
|
10
|
+
const mime_1 = require("./mime");
|
|
11
|
+
let root;
|
|
12
|
+
let server;
|
|
13
|
+
let base;
|
|
14
|
+
/** Options of the next request; next() answers 599 "next" with the status of the error, if any. */
|
|
15
|
+
let options = {};
|
|
16
|
+
(0, bun_test_1.beforeAll)(async () => {
|
|
17
|
+
root = (0, node_fs_1.mkdtempSync)((0, node_path_1.join)((0, node_os_1.tmpdir)(), 'cell-send-'));
|
|
18
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, 'app.js'), '0123456789');
|
|
19
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, 'index.html'), '<html></html>');
|
|
20
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, '.env'), 'SECRET=1');
|
|
21
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, 'page.html'), 'page');
|
|
22
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, 'empty.txt'), '');
|
|
23
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.join)(root, 'docs'));
|
|
24
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, 'docs', 'index.html'), 'docs');
|
|
25
|
+
server = (0, node_http_1.createServer)((req, res) => {
|
|
26
|
+
if (req.headers['x-gone']) {
|
|
27
|
+
// The client is gone before the file is sent.
|
|
28
|
+
res.destroy();
|
|
29
|
+
}
|
|
30
|
+
(0, serve_static_1.serveStatic)(root, { baseHref: '/', root, ...options })(req, res, (err) => {
|
|
31
|
+
if (res.writableEnded) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
res.statusCode = 599;
|
|
35
|
+
res.end(`next${err ? ` ${err.statusCode}` : ''}`);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
39
|
+
base = `http://127.0.0.1:${server.address().port}`;
|
|
40
|
+
});
|
|
41
|
+
(0, bun_test_1.afterAll)(() => {
|
|
42
|
+
server.close();
|
|
43
|
+
(0, node_fs_1.rmSync)(root, { recursive: true, force: true });
|
|
44
|
+
});
|
|
45
|
+
async function get(path, headers = {}, opts = {}, method = 'GET') {
|
|
46
|
+
options = opts;
|
|
47
|
+
const res = await fetch(base + path, { headers, method, redirect: 'manual' });
|
|
48
|
+
return { status: res.status, headers: res.headers, body: await res.text() };
|
|
49
|
+
}
|
|
50
|
+
(0, bun_test_1.describe)('serveStatic', () => {
|
|
51
|
+
(0, bun_test_1.it)('sends a file with type, length, validators and cache headers', async () => {
|
|
52
|
+
const res = await get('/app.js', {}, { maxAge: 60000, immutable: true });
|
|
53
|
+
(0, chai_1.expect)(res.status).to.equal(200);
|
|
54
|
+
(0, chai_1.expect)(res.body).to.equal('0123456789');
|
|
55
|
+
(0, chai_1.expect)(res.headers.get('content-type')).to.equal('application/javascript; charset=UTF-8');
|
|
56
|
+
(0, chai_1.expect)(res.headers.get('content-length')).to.equal('10');
|
|
57
|
+
(0, chai_1.expect)(res.headers.get('accept-ranges')).to.equal('bytes');
|
|
58
|
+
(0, chai_1.expect)(res.headers.get('cache-control')).to.equal('public, max-age=60, immutable');
|
|
59
|
+
(0, chai_1.expect)(res.headers.get('etag')).to.match(/^W\/"a-[0-9a-f]+"$/);
|
|
60
|
+
(0, chai_1.expect)(res.headers.get('last-modified')).to.match(/GMT$/);
|
|
61
|
+
(0, chai_1.expect)((await get('/empty.txt')).headers.get('content-length')).to.equal('0');
|
|
62
|
+
});
|
|
63
|
+
(0, bun_test_1.it)('answers 304 to a fresh copy and 412 to a failed precondition', async () => {
|
|
64
|
+
const { headers } = await get('/app.js');
|
|
65
|
+
const etag = headers.get('etag');
|
|
66
|
+
const notModified = await get('/app.js', { 'if-none-match': etag });
|
|
67
|
+
(0, chai_1.expect)(notModified.status).to.equal(304);
|
|
68
|
+
(0, chai_1.expect)(notModified.headers.get('content-type')).to.equal(null);
|
|
69
|
+
(0, chai_1.expect)((await get('/app.js', { 'if-modified-since': headers.get('last-modified') })).status).to.equal(304);
|
|
70
|
+
(0, chai_1.expect)((await get('/app.js', { 'if-none-match': '"other"' })).status).to.equal(200);
|
|
71
|
+
(0, chai_1.expect)((await get('/app.js', { 'if-none-match': etag, 'cache-control': 'no-cache' })).status).to.equal(200);
|
|
72
|
+
(0, chai_1.expect)((await get('/app.js', { 'if-match': '"other"' })).status).to.equal(412);
|
|
73
|
+
(0, chai_1.expect)((await get('/app.js', { 'if-match': etag })).status).to.equal(200);
|
|
74
|
+
});
|
|
75
|
+
(0, bun_test_1.it)('sends a byte range', async () => {
|
|
76
|
+
const partial = await get('/app.js', { range: 'bytes=2-4' });
|
|
77
|
+
(0, chai_1.expect)(partial.status).to.equal(206);
|
|
78
|
+
(0, chai_1.expect)(partial.body).to.equal('234');
|
|
79
|
+
(0, chai_1.expect)(partial.headers.get('content-range')).to.equal('bytes 2-4/10');
|
|
80
|
+
(0, chai_1.expect)((await get('/app.js', { range: 'bytes=-3' })).body).to.equal('789');
|
|
81
|
+
(0, chai_1.expect)((await get('/app.js', { range: 'bytes=8-' })).body).to.equal('89');
|
|
82
|
+
const unsatisfiable = await get('/app.js', { range: 'bytes=20-30' });
|
|
83
|
+
(0, chai_1.expect)(unsatisfiable.status).to.equal(416);
|
|
84
|
+
(0, chai_1.expect)(unsatisfiable.headers.get('content-range')).to.equal('bytes */10');
|
|
85
|
+
(0, chai_1.expect)((await get('/app.js', { range: 'bytes=0-1,5-6' })).status).to.equal(200);
|
|
86
|
+
(0, chai_1.expect)((await get('/app.js', { range: 'bytes=0-1', 'if-range': '"stale"' })).status).to.equal(200);
|
|
87
|
+
(0, chai_1.expect)((await get('/app.js', { range: 'bytes=0-1' }, { acceptRanges: false })).status).to.equal(200);
|
|
88
|
+
});
|
|
89
|
+
(0, bun_test_1.it)('answers HEAD without a body', async () => {
|
|
90
|
+
const res = await get('/app.js', {}, {}, 'HEAD');
|
|
91
|
+
(0, chai_1.expect)(res.status).to.equal(200);
|
|
92
|
+
(0, chai_1.expect)(res.headers.get('content-length')).to.equal('10');
|
|
93
|
+
(0, chai_1.expect)(res.body).to.equal('');
|
|
94
|
+
});
|
|
95
|
+
(0, bun_test_1.it)('sends index files, tries extensions, and redirects directories when asked', async () => {
|
|
96
|
+
(0, chai_1.expect)((await get('/')).body).to.equal('<html></html>');
|
|
97
|
+
(0, chai_1.expect)((await get('/docs/')).body).to.equal('docs');
|
|
98
|
+
(0, chai_1.expect)((await get('/docs')).body).to.equal('next');
|
|
99
|
+
const redirect = await get('/docs?x=1', {}, { redirect: true });
|
|
100
|
+
(0, chai_1.expect)(redirect.status).to.equal(301);
|
|
101
|
+
(0, chai_1.expect)(redirect.headers.get('location')).to.equal('/docs/?x=1');
|
|
102
|
+
(0, chai_1.expect)((await get('/page')).body).to.equal('next');
|
|
103
|
+
(0, chai_1.expect)((await get('/page', {}, { extensions: ['html'] })).body).to.equal('page');
|
|
104
|
+
(0, chai_1.expect)((await get('/', {}, { index: false })).body).to.equal('next');
|
|
105
|
+
});
|
|
106
|
+
(0, bun_test_1.it)('keeps dotfiles and paths outside the root out', async () => {
|
|
107
|
+
(0, chai_1.expect)((await get('/.env')).body).to.equal('next');
|
|
108
|
+
(0, chai_1.expect)((await get('/.env', {}, { dotfiles: 'allow' })).body).to.equal('SECRET=1');
|
|
109
|
+
(0, chai_1.expect)((await get('/.env', {}, { dotfiles: 'deny', fallthrough: false })).body).to.equal('next 403');
|
|
110
|
+
(0, chai_1.expect)((await get('/%2e%2e/etc/passwd')).body).to.equal('next');
|
|
111
|
+
(0, chai_1.expect)((await get('/a%00b')).body).to.equal('next');
|
|
112
|
+
(0, chai_1.expect)((await get('/missing.js', {}, { fallthrough: false })).body).to.equal('next 404');
|
|
113
|
+
});
|
|
114
|
+
(0, bun_test_1.it)('lets setHeaders win over the defaults', async () => {
|
|
115
|
+
const res = await get('/index.html', {}, { maxAge: 60000, setHeaders: (r) => r.setHeader('Cache-Control', 'no-store') });
|
|
116
|
+
(0, chai_1.expect)(res.headers.get('cache-control')).to.equal('no-store');
|
|
117
|
+
});
|
|
118
|
+
(0, bun_test_1.it)('does not fail when the client is gone before the file is sent', async () => {
|
|
119
|
+
const error = await get('/app.js', { 'x-gone': '1' }).then(() => undefined, (e) => e);
|
|
120
|
+
(0, chai_1.expect)(error).to.be.instanceOf(Error);
|
|
121
|
+
(0, chai_1.expect)((await get('/app.js')).status).to.equal(200);
|
|
122
|
+
});
|
|
123
|
+
(0, bun_test_1.it)('passes other methods on', async () => {
|
|
124
|
+
(0, chai_1.expect)((await get('/app.js', {}, {}, 'POST')).body).to.equal('next');
|
|
125
|
+
(0, chai_1.expect)((await get('/app.js', {}, { fallthrough: false }, 'POST')).status).to.equal(405);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
(0, bun_test_1.describe)('MimeUtil', () => {
|
|
129
|
+
(0, bun_test_1.it)('knows the files of a frontend build', () => {
|
|
130
|
+
(0, chai_1.expect)(mime_1.MimeUtil.contentType('/a/index.html')).to.equal('text/html; charset=UTF-8');
|
|
131
|
+
(0, chai_1.expect)(mime_1.MimeUtil.contentType('x.CSS')).to.equal('text/css; charset=UTF-8');
|
|
132
|
+
(0, chai_1.expect)(mime_1.MimeUtil.contentType('x.json')).to.equal('application/json; charset=UTF-8');
|
|
133
|
+
(0, chai_1.expect)(mime_1.MimeUtil.contentType('x.svg')).to.equal('image/svg+xml');
|
|
134
|
+
(0, chai_1.expect)(mime_1.MimeUtil.contentType('x.woff2')).to.equal('font/woff2');
|
|
135
|
+
(0, chai_1.expect)(mime_1.MimeUtil.contentType('LICENSE')).to.equal('application/octet-stream');
|
|
136
|
+
(0, chai_1.expect)(mime_1.MimeUtil.lookup('js')).to.equal('application/javascript');
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
//# sourceMappingURL=serve-static.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serve-static.spec.js","sourceRoot":"","sources":["../../src/node/serve-static.spec.ts"],"names":[],"mappings":";;AAAA,uCAA6D;AAC7D,+BAA8B;AAC9B,qCAAwE;AACxE,yCAAiD;AAEjD,qCAAiC;AACjC,yCAAiC;AACjC,iDAA6C;AAC7C,iCAAkC;AAElC,IAAI,IAAY,CAAC;AACjB,IAAI,MAAc,CAAC;AACnB,IAAI,IAAY,CAAC;AACjB,mGAAmG;AACnG,IAAI,OAAO,GAA4B,EAAE,CAAC;AAE1C,IAAA,oBAAS,EAAC,KAAK,IAAI,EAAE;IACjB,IAAI,GAAG,IAAA,qBAAW,EAAC,IAAA,gBAAI,EAAC,IAAA,gBAAM,GAAE,EAAE,YAAY,CAAC,CAAC,CAAC;IACjD,IAAA,uBAAa,EAAC,IAAA,gBAAI,EAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,YAAY,CAAC,CAAC;IAClD,IAAA,uBAAa,EAAC,IAAA,gBAAI,EAAC,IAAI,EAAE,YAAY,CAAC,EAAE,eAAe,CAAC,CAAC;IACzD,IAAA,uBAAa,EAAC,IAAA,gBAAI,EAAC,IAAI,EAAE,MAAM,CAAC,EAAE,UAAU,CAAC,CAAC;IAC9C,IAAA,uBAAa,EAAC,IAAA,gBAAI,EAAC,IAAI,EAAE,WAAW,CAAC,EAAE,MAAM,CAAC,CAAC;IAC/C,IAAA,uBAAa,EAAC,IAAA,gBAAI,EAAC,IAAI,EAAE,WAAW,CAAC,EAAE,EAAE,CAAC,CAAC;IAC3C,IAAA,mBAAS,EAAC,IAAA,gBAAI,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;IAC9B,IAAA,uBAAa,EAAC,IAAA,gBAAI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AC,eAAQ,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,0BAA0B,CAAC,CAAC;QAC7E,IAAA,aAAM,EAAC,eAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAC;IACrE,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/serve-static",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cell-component"
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
"url": "https://github.com/cellbang/cell.git"
|
|
15
15
|
},
|
|
16
16
|
"license": "MIT",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=18.0.0"
|
|
19
|
+
},
|
|
17
20
|
"main": "lib/node/index.js",
|
|
18
21
|
"typings": "lib/node/index.d.ts",
|
|
19
22
|
"files": [
|
|
@@ -29,13 +32,9 @@
|
|
|
29
32
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
30
33
|
},
|
|
31
34
|
"dependencies": {
|
|
32
|
-
"@celljs/core": "4.
|
|
33
|
-
"@celljs/http": "4.
|
|
34
|
-
"@celljs/web": "4.
|
|
35
|
-
"send": "^0.19.2"
|
|
36
|
-
},
|
|
37
|
-
"devDependencies": {
|
|
38
|
-
"@types/send": "^0.17.0"
|
|
35
|
+
"@celljs/core": "4.2.0",
|
|
36
|
+
"@celljs/http": "4.2.0",
|
|
37
|
+
"@celljs/web": "4.2.0"
|
|
39
38
|
},
|
|
40
39
|
"publishConfig": {
|
|
41
40
|
"access": "public"
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { MimeUtil } from './mime';
|
|
2
2
|
import type { ServeStaticOption } from './type';
|
|
3
3
|
|
|
4
4
|
/** A frontend file inside a server bundle built with `cell build --single`. */
|
|
@@ -59,9 +59,7 @@ export namespace EmbeddedAssets {
|
|
|
59
59
|
next();
|
|
60
60
|
return;
|
|
61
61
|
}
|
|
62
|
-
|
|
63
|
-
const charset = (send.mime as any).charsets.lookup(type) as string | undefined;
|
|
64
|
-
res.setHeader('Content-Type', charset ? `${type}; charset=${charset}` : type);
|
|
62
|
+
res.setHeader('Content-Type', MimeUtil.contentType(path));
|
|
65
63
|
res.setHeader('ETag', asset.etag);
|
|
66
64
|
if (opts.cacheControl !== false) {
|
|
67
65
|
const maxAge = Math.floor(Math.min(Math.max(0, Number(opts.maxAge ?? opts.maxage ?? 0)), 31536000000) / 1000);
|
|
@@ -79,4 +77,4 @@ export namespace EmbeddedAssets {
|
|
|
79
77
|
res.end(req.method === 'HEAD' ? undefined : body);
|
|
80
78
|
};
|
|
81
79
|
}
|
|
82
|
-
}
|
|
80
|
+
}
|
package/src/node/mime.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Types of the files a frontend build has; anything else is `application/octet-stream`. */
|
|
2
|
+
const TYPES: Record<string, string> = {
|
|
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
|
+
|
|
13
|
+
const DEFAULT_TYPE = 'application/octet-stream';
|
|
14
|
+
|
|
15
|
+
export namespace MimeUtil {
|
|
16
|
+
|
|
17
|
+
/** `/assets/app.js` → `application/javascript`. */
|
|
18
|
+
export function lookup(path: string): string {
|
|
19
|
+
return TYPES[path.replace(/^.*[./\\]/, '').toLowerCase()] ?? DEFAULT_TYPE;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The Content-Type header: with `charset=UTF-8` for text, JavaScript and JSON. */
|
|
23
|
+
export function contentType(path: string): string {
|
|
24
|
+
const type = lookup(path);
|
|
25
|
+
return /^text\/|^application\/(?:javascript|json)$/.test(type) ? `${type}; charset=UTF-8` : type;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -123,7 +123,7 @@ describe('ServeStaticMiddleware', () => {
|
|
|
123
123
|
expect(result.nextCalls).to.equal(0);
|
|
124
124
|
});
|
|
125
125
|
|
|
126
|
-
it('成功:handle() 在响应结束后 settle
|
|
126
|
+
it('成功:handle() 在响应结束后 settle(文件发送成功时不回调 next)', async () => {
|
|
127
127
|
const result = await request(config(root), '/app.js');
|
|
128
128
|
expect(result.status).to.equal(200);
|
|
129
129
|
expect(result.settled).to.equal('resolved');
|
|
@@ -281,4 +281,4 @@ describe('ServeStaticMiddleware', () => {
|
|
|
281
281
|
expect(result.nextCalls).to.equal(0);
|
|
282
282
|
});
|
|
283
283
|
});
|
|
284
|
-
});
|
|
284
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Middleware, Context, RequestMatcher, HTTP_MIDDLEWARE_PRIORITY } from '@celljs/web/lib/node';
|
|
2
|
-
import {serveStatic
|
|
2
|
+
import { serveStatic } from './serve-static';
|
|
3
|
+
import { MimeUtil } from './mime';
|
|
3
4
|
import { EmbeddedAssets } from './embedded-assets';
|
|
4
5
|
import { Value, Component, Autowired, PostConstruct } from '@celljs/core';
|
|
5
6
|
import { HttpMethod, MediaType, HttpHeaders } from '@celljs/http';
|
|
@@ -65,7 +66,7 @@ export class ServeStaticMiddleware implements Middleware {
|
|
|
65
66
|
if (headers) {
|
|
66
67
|
Object.keys(headers).forEach(key => res.setHeader(key, headers[key]));
|
|
67
68
|
}
|
|
68
|
-
if (
|
|
69
|
+
if (MimeUtil.lookup(path) === MediaType.TEXT_HTML) {
|
|
69
70
|
res.setHeader(HttpHeaders.CACHE_CONTROL, htmlCacheControl);
|
|
70
71
|
}
|
|
71
72
|
};
|
|
@@ -128,4 +129,4 @@ export class ServeStaticMiddleware implements Middleware {
|
|
|
128
129
|
}
|
|
129
130
|
priority = HTTP_MIDDLEWARE_PRIORITY + 500;
|
|
130
131
|
|
|
131
|
-
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { createServer, Server } from 'node:http';
|
|
5
|
+
import type { AddressInfo } from 'node:net';
|
|
6
|
+
import { tmpdir } from 'node:os';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import { serveStatic } from './serve-static';
|
|
9
|
+
import { MimeUtil } from './mime';
|
|
10
|
+
|
|
11
|
+
let root: string;
|
|
12
|
+
let server: Server;
|
|
13
|
+
let base: string;
|
|
14
|
+
/** Options of the next request; next() answers 599 "next" with the status of the error, if any. */
|
|
15
|
+
let options: Record<string, unknown> = {};
|
|
16
|
+
|
|
17
|
+
beforeAll(async () => {
|
|
18
|
+
root = mkdtempSync(join(tmpdir(), 'cell-send-'));
|
|
19
|
+
writeFileSync(join(root, 'app.js'), '0123456789');
|
|
20
|
+
writeFileSync(join(root, 'index.html'), '<html></html>');
|
|
21
|
+
writeFileSync(join(root, '.env'), 'SECRET=1');
|
|
22
|
+
writeFileSync(join(root, 'page.html'), 'page');
|
|
23
|
+
writeFileSync(join(root, 'empty.txt'), '');
|
|
24
|
+
mkdirSync(join(root, 'docs'));
|
|
25
|
+
writeFileSync(join(root, 'docs', 'index.html'), 'docs');
|
|
26
|
+
server = createServer((req, res) => {
|
|
27
|
+
if (req.headers['x-gone']) {
|
|
28
|
+
// The client is gone before the file is sent.
|
|
29
|
+
res.destroy();
|
|
30
|
+
}
|
|
31
|
+
serveStatic(root, { baseHref: '/', root, ...options } as any)(req, res, (err?: any) => {
|
|
32
|
+
if (res.writableEnded) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
res.statusCode = 599;
|
|
36
|
+
res.end(`next${err ? ` ${err.statusCode}` : ''}`);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
40
|
+
base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterAll(() => {
|
|
44
|
+
server.close();
|
|
45
|
+
rmSync(root, { recursive: true, force: true });
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
async function get(path: string, headers: Record<string, string> = {}, opts: Record<string, unknown> = {}, method = 'GET') {
|
|
49
|
+
options = opts;
|
|
50
|
+
const res = await fetch(base + path, { headers, method, redirect: 'manual' });
|
|
51
|
+
return { status: res.status, headers: res.headers, body: await res.text() };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
describe('serveStatic', () => {
|
|
55
|
+
it('sends a file with type, length, validators and cache headers', async () => {
|
|
56
|
+
const res = await get('/app.js', {}, { maxAge: 60000, immutable: true });
|
|
57
|
+
expect(res.status).to.equal(200);
|
|
58
|
+
expect(res.body).to.equal('0123456789');
|
|
59
|
+
expect(res.headers.get('content-type')).to.equal('application/javascript; charset=UTF-8');
|
|
60
|
+
expect(res.headers.get('content-length')).to.equal('10');
|
|
61
|
+
expect(res.headers.get('accept-ranges')).to.equal('bytes');
|
|
62
|
+
expect(res.headers.get('cache-control')).to.equal('public, max-age=60, immutable');
|
|
63
|
+
expect(res.headers.get('etag')).to.match(/^W\/"a-[0-9a-f]+"$/);
|
|
64
|
+
expect(res.headers.get('last-modified')).to.match(/GMT$/);
|
|
65
|
+
expect((await get('/empty.txt')).headers.get('content-length')).to.equal('0');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('answers 304 to a fresh copy and 412 to a failed precondition', async () => {
|
|
69
|
+
const { headers } = await get('/app.js');
|
|
70
|
+
const etag = headers.get('etag')!;
|
|
71
|
+
const notModified = await get('/app.js', { 'if-none-match': etag });
|
|
72
|
+
expect(notModified.status).to.equal(304);
|
|
73
|
+
expect(notModified.headers.get('content-type')).to.equal(null);
|
|
74
|
+
expect((await get('/app.js', { 'if-modified-since': headers.get('last-modified')! })).status).to.equal(304);
|
|
75
|
+
expect((await get('/app.js', { 'if-none-match': '"other"' })).status).to.equal(200);
|
|
76
|
+
expect((await get('/app.js', { 'if-none-match': etag, 'cache-control': 'no-cache' })).status).to.equal(200);
|
|
77
|
+
expect((await get('/app.js', { 'if-match': '"other"' })).status).to.equal(412);
|
|
78
|
+
expect((await get('/app.js', { 'if-match': etag })).status).to.equal(200);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('sends a byte range', async () => {
|
|
82
|
+
const partial = await get('/app.js', { range: 'bytes=2-4' });
|
|
83
|
+
expect(partial.status).to.equal(206);
|
|
84
|
+
expect(partial.body).to.equal('234');
|
|
85
|
+
expect(partial.headers.get('content-range')).to.equal('bytes 2-4/10');
|
|
86
|
+
expect((await get('/app.js', { range: 'bytes=-3' })).body).to.equal('789');
|
|
87
|
+
expect((await get('/app.js', { range: 'bytes=8-' })).body).to.equal('89');
|
|
88
|
+
const unsatisfiable = await get('/app.js', { range: 'bytes=20-30' });
|
|
89
|
+
expect(unsatisfiable.status).to.equal(416);
|
|
90
|
+
expect(unsatisfiable.headers.get('content-range')).to.equal('bytes */10');
|
|
91
|
+
expect((await get('/app.js', { range: 'bytes=0-1,5-6' })).status).to.equal(200);
|
|
92
|
+
expect((await get('/app.js', { range: 'bytes=0-1', 'if-range': '"stale"' })).status).to.equal(200);
|
|
93
|
+
expect((await get('/app.js', { range: 'bytes=0-1' }, { acceptRanges: false })).status).to.equal(200);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('answers HEAD without a body', async () => {
|
|
97
|
+
const res = await get('/app.js', {}, {}, 'HEAD');
|
|
98
|
+
expect(res.status).to.equal(200);
|
|
99
|
+
expect(res.headers.get('content-length')).to.equal('10');
|
|
100
|
+
expect(res.body).to.equal('');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('sends index files, tries extensions, and redirects directories when asked', async () => {
|
|
104
|
+
expect((await get('/')).body).to.equal('<html></html>');
|
|
105
|
+
expect((await get('/docs/')).body).to.equal('docs');
|
|
106
|
+
expect((await get('/docs')).body).to.equal('next');
|
|
107
|
+
const redirect = await get('/docs?x=1', {}, { redirect: true });
|
|
108
|
+
expect(redirect.status).to.equal(301);
|
|
109
|
+
expect(redirect.headers.get('location')).to.equal('/docs/?x=1');
|
|
110
|
+
expect((await get('/page')).body).to.equal('next');
|
|
111
|
+
expect((await get('/page', {}, { extensions: ['html'] })).body).to.equal('page');
|
|
112
|
+
expect((await get('/', {}, { index: false })).body).to.equal('next');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('keeps dotfiles and paths outside the root out', async () => {
|
|
116
|
+
expect((await get('/.env')).body).to.equal('next');
|
|
117
|
+
expect((await get('/.env', {}, { dotfiles: 'allow' })).body).to.equal('SECRET=1');
|
|
118
|
+
expect((await get('/.env', {}, { dotfiles: 'deny', fallthrough: false })).body).to.equal('next 403');
|
|
119
|
+
expect((await get('/%2e%2e/etc/passwd')).body).to.equal('next');
|
|
120
|
+
expect((await get('/a%00b')).body).to.equal('next');
|
|
121
|
+
expect((await get('/missing.js', {}, { fallthrough: false })).body).to.equal('next 404');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('lets setHeaders win over the defaults', async () => {
|
|
125
|
+
const res = await get('/index.html', {}, { maxAge: 60000, setHeaders: (r: any) => r.setHeader('Cache-Control', 'no-store') });
|
|
126
|
+
expect(res.headers.get('cache-control')).to.equal('no-store');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('does not fail when the client is gone before the file is sent', async () => {
|
|
130
|
+
const error = await get('/app.js', { 'x-gone': '1' }).then(() => undefined, (e: Error) => e);
|
|
131
|
+
expect(error).to.be.instanceOf(Error);
|
|
132
|
+
expect((await get('/app.js')).status).to.equal(200);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('passes other methods on', async () => {
|
|
136
|
+
expect((await get('/app.js', {}, {}, 'POST')).body).to.equal('next');
|
|
137
|
+
expect((await get('/app.js', {}, { fallthrough: false }, 'POST')).status).to.equal(405);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('MimeUtil', () => {
|
|
142
|
+
it('knows the files of a frontend build', () => {
|
|
143
|
+
expect(MimeUtil.contentType('/a/index.html')).to.equal('text/html; charset=UTF-8');
|
|
144
|
+
expect(MimeUtil.contentType('x.CSS')).to.equal('text/css; charset=UTF-8');
|
|
145
|
+
expect(MimeUtil.contentType('x.json')).to.equal('application/json; charset=UTF-8');
|
|
146
|
+
expect(MimeUtil.contentType('x.svg')).to.equal('image/svg+xml');
|
|
147
|
+
expect(MimeUtil.contentType('x.woff2')).to.equal('font/woff2');
|
|
148
|
+
expect(MimeUtil.contentType('LICENSE')).to.equal('application/octet-stream');
|
|
149
|
+
expect(MimeUtil.lookup('js')).to.equal('application/javascript');
|
|
150
|
+
});
|
|
151
|
+
});
|