@aomex/serve-static 1.7.0 → 2.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.
- package/CHANGELOG.md +11 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [2.0.0](https://github.com/aomex/aomex/compare/v1.7.0...v2.0.0) (2024-07-26)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **serve-static:** 增加formatPath参数 ([226d8cd](https://github.com/aomex/aomex/commit/226d8cd914b907679bf0b68e0b00e21bc25cf42e))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
# [1.7.0](https://github.com/aomex/aomex/compare/v1.6.0...v1.7.0) (2024-07-13)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package @aomex/serve-static
|
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,11 @@ interface SendOptions {
|
|
|
6
6
|
*/
|
|
7
7
|
root: string;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* 对请求的路径进行修改操作以满足文件查找
|
|
10
|
+
*/
|
|
11
|
+
formatPath?: (pathname: string, ctx: WebContext) => string;
|
|
12
|
+
/**
|
|
13
|
+
* 没有指定文件名时,尝试拼接该属性提供的文件。默认值:`index.html`
|
|
10
14
|
*/
|
|
11
15
|
indexFile?: string | false;
|
|
12
16
|
/**
|
package/dist/index.js
CHANGED
|
@@ -14,10 +14,14 @@ var send = async (ctx, options) => {
|
|
|
14
14
|
noStore = false
|
|
15
15
|
} = {},
|
|
16
16
|
indexFile = "index.html",
|
|
17
|
-
useCompressedFile = true
|
|
17
|
+
useCompressedFile = true,
|
|
18
|
+
formatPath
|
|
18
19
|
} = options;
|
|
19
20
|
const root = path.normalize(path.resolve(options.root ?? ""));
|
|
20
21
|
let { pathname: staticPath } = request;
|
|
22
|
+
if (formatPath) {
|
|
23
|
+
staticPath = formatPath(staticPath, ctx);
|
|
24
|
+
}
|
|
21
25
|
try {
|
|
22
26
|
staticPath = decodeURIComponent(staticPath);
|
|
23
27
|
} catch {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/send.ts","../src/serve-static.ts"],"sourcesContent":["import { stat } from 'node:fs/promises';\nimport path from 'node:path';\nimport type { WebContext } from '@aomex/web';\nimport { createReadStream, type Stats } from 'node:fs';\n\nexport interface SendOptions {\n /**\n * 服务的根目录,请勿设置成根目录,否则源码可能被访问。\n */\n root: string;\n /**\n *
|
|
1
|
+
{"version":3,"sources":["../src/send.ts","../src/serve-static.ts"],"sourcesContent":["import { stat } from 'node:fs/promises';\nimport path from 'node:path';\nimport type { WebContext } from '@aomex/web';\nimport { createReadStream, type Stats } from 'node:fs';\n\nexport interface SendOptions {\n /**\n * 服务的根目录,请勿设置成根目录,否则源码可能被访问。\n */\n root: string;\n /**\n * 对请求的路径进行修改操作以满足文件查找\n */\n formatPath?: (pathname: string, ctx: WebContext) => string;\n /**\n * 没有指定文件名时,尝试拼接该属性提供的文件。默认值:`index.html`\n */\n indexFile?: string | false;\n /**\n * 使用压缩过后的文件,如果未找到则继续使用源文件。支持后缀:\n * - .gz\n * - .br\n *\n * 默认值:`true`\n *\n */\n useCompressedFile?: boolean;\n /**\n * 设置 Cache-Control 报文\n *\n * @link https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Cache-Control\n */\n cacheControl?: {\n /**\n * 是否共享缓存。默认值:`public`\n *\n * - `public` 表明响应可以被任何对象(包括:发送请求的客户端,代理服务器,等等)缓存,即使是通常不可缓存的内容。(例如:1.该响应没有max-age指令或Expires消息头;2. 该响应对应的请求方法是 POST 。)\n * - `private` 表明响应只能被单个用户缓存,不能作为共享缓存(即代理服务器不能缓存它)。私有缓存可以缓存响应内容,比如:对应用户的本地浏览器。\n *\n */\n publicOrPrivate?: 'public' | 'private';\n /**\n * 缓存的内容将在N毫秒后失效, 这个选项只在HTTP 1.1可用, 并如果和Last-Modified一起使用时, 优先级较高\n *\n * 默认值:`0`\n *\n */\n maxAge?: number;\n /**\n * 表示响应正文不会随时间而改变。资源(如果未过期)在服务器上不发生改变,因此客户端不应发送重新验证请求头(例如If-None-Match或 If-Modified-Since)来检查更新,即使用户显式地刷新页面。\n *\n * 默认值:`false`\n */\n immutable?: boolean;\n /**\n * 在发布缓存副本之前,强制要求缓存把请求提交给原始服务器进行验证 (协商缓存验证)。\n *\n * 默认值:`false`\n */\n noCache?: boolean;\n /**\n * 缓存不应存储有关客户端请求或服务器响应的任何内容,即不使用任何缓存。\n *\n * 默认值:`false`\n */\n noStore?: boolean;\n };\n}\n\nconst DOT_FILE_PATTERN = /(?:^\\.|[\\\\/]\\.)/;\n\nexport const send = async (ctx: WebContext, options: SendOptions): Promise<boolean> => {\n const { request, response } = ctx;\n const {\n cacheControl: {\n maxAge = 0,\n immutable = false,\n publicOrPrivate = 'public',\n noCache = false,\n noStore = false,\n } = {},\n indexFile = 'index.html',\n useCompressedFile = true,\n formatPath,\n } = options;\n const root = path.normalize(path.resolve(options.root ?? ''));\n\n let { pathname: staticPath } = request;\n if (formatPath) {\n staticPath = formatPath(staticPath, ctx);\n }\n try {\n staticPath = decodeURIComponent(staticPath);\n } catch {\n return false;\n }\n // 去掉 .. 路径再和根目录合并,防止非法访问\n staticPath = path.normalize(staticPath.slice(path.parse(staticPath).root.length));\n staticPath = path.join(root, staticPath);\n\n if (DOT_FILE_PATTERN.test(staticPath.substring(root.length))) return false;\n\n let stats: Stats;\n try {\n stats = await stat(staticPath);\n if (stats.isDirectory()) {\n if (!indexFile) return false;\n staticPath = path.join(staticPath, indexFile);\n stats = await stat(staticPath);\n }\n } catch {\n return false;\n }\n\n if (stats.isDirectory()) return false;\n\n let compressExt: string = '';\n if (useCompressedFile) {\n if (\n request.accept.encodings('br', 'identity') === 'br' &&\n (await isFileExists(staticPath + '.br'))\n ) {\n compressExt = '.br';\n response.setHeader('Content-Encoding', 'br');\n } else if (\n request.accept.encodings('gzip', 'identity') === 'gzip' &&\n (await isFileExists(staticPath + '.gz'))\n ) {\n compressExt = '.gz';\n response.setHeader('Content-Encoding', 'gz');\n }\n }\n\n if (!compressExt) {\n response.setHeader('Content-Length', stats.size);\n }\n if (!response.hasHeader('Last-Modified')) {\n response.setHeader('Last-Modified', stats.mtime.toUTCString());\n }\n if (!response.hasHeader('Cache-Control')) {\n const directives: string[] = [];\n directives.push(publicOrPrivate);\n noCache && directives.push('no-cache');\n noStore && directives.push('no-store');\n directives.push(`max-age=${(maxAge / 1000) | 0}`);\n immutable && directives.push('immutable');\n response.setHeader('Cache-Control', directives.join(', '));\n }\n\n response.contentType = path.extname(staticPath) || 'text';\n ctx.send(createReadStream(staticPath + compressExt));\n return true;\n};\n\nconst isFileExists = async (file: string) => {\n try {\n const stats = await stat(file);\n if (stats.isFile()) return true;\n return false;\n } catch {\n return false;\n }\n};\n","import { middleware } from '@aomex/core';\nimport type { WebMiddleware } from '@aomex/web';\nimport { send, type SendOptions } from './send';\n\nexport const serveStatic = (options: SendOptions): WebMiddleware => {\n return middleware.web(async (ctx, next) => {\n const method = ctx.request.method;\n if (method === 'GET' || method === 'HEAD') {\n if (await send(ctx, options)) return;\n }\n\n return next();\n });\n};\n"],"mappings":";AAAA,SAAS,YAAY;AACrB,OAAO,UAAU;AAEjB,SAAS,wBAAoC;AAkE7C,IAAM,mBAAmB;AAElB,IAAM,OAAO,OAAO,KAAiB,YAA2C;AACrF,QAAM,EAAE,SAAS,SAAS,IAAI;AAC9B,QAAM;AAAA,IACJ,cAAc;AAAA,MACZ,SAAS;AAAA,MACT,YAAY;AAAA,MACZ,kBAAkB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EACjD;AACA,MAAI,CAAC,SAAS,UAAU,eAAe,GAAG;AACxC,aAAS,UAAU,iBAAiB,MAAM,MAAM,YAAY,CAAC;AAAA,EAC/D;AACA,MAAI,CAAC,SAAS,UAAU,eAAe,GAAG;AACxC,UAAM,aAAuB,CAAC;AAC9B,eAAW,KAAK,eAAe;AAC/B,eAAW,WAAW,KAAK,UAAU;AACrC,eAAW,WAAW,KAAK,UAAU;AACrC,eAAW,KAAK,WAAY,SAAS,MAAQ,CAAC,EAAE;AAChD,iBAAa,WAAW,KAAK,WAAW;AACxC,aAAS,UAAU,iBAAiB,WAAW,KAAK,IAAI,CAAC;AAAA,EAC3D;AAEA,WAAS,cAAc,KAAK,QAAQ,UAAU,KAAK;AACnD,MAAI,KAAK,iBAAiB,aAAa,WAAW,CAAC;AACnD,SAAO;AACT;AAEA,IAAM,eAAe,OAAO,SAAiB;AAC3C,MAAI;AACF,UAAM,QAAQ,MAAM,KAAK,IAAI;AAC7B,QAAI,MAAM,OAAO,EAAG,QAAO;AAC3B,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;;;AClKA,SAAS,kBAAkB;AAIpB,IAAM,cAAc,CAAC,YAAwC;AAClE,SAAO,WAAW,IAAI,OAAO,KAAK,SAAS;AACzC,UAAM,SAAS,IAAI,QAAQ;AAC3B,QAAI,WAAW,SAAS,WAAW,QAAQ;AACzC,UAAI,MAAM,KAAK,KAAK,OAAO,EAAG;AAAA,IAChC;AAEA,WAAO,KAAK;AAAA,EACd,CAAC;AACH;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aomex/serve-static",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "静态文件服务",
|
|
5
5
|
"repository": "git@github.com:aomex/aomex.git",
|
|
6
6
|
"homepage": "https://aomex.js.org",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"@aomex/web": "^1.0.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@aomex/core": "^
|
|
34
|
-
"@aomex/web": "^
|
|
33
|
+
"@aomex/core": "^2.0.0",
|
|
34
|
+
"@aomex/web": "^2.0.0"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {}
|
|
37
37
|
}
|