@aomex/redoc-ui 3.30.6 → 3.30.7
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.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
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
|
+
## [3.30.7](https://github.com/aomex/aomex/compare/v3.30.6...v3.30.7) (2026-11-01)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **swagger:** 增强路径安全性,防止路径穿越访问目录外文件 ([806ba28](https://github.com/aomex/aomex/commit/806ba28708e14f3f93de9789b7cb32fac44ce84e))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## [3.30.6](https://github.com/aomex/aomex/compare/v3.30.5...v3.30.6) (2026-10-19)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package @aomex/redoc-ui
|
package/dist/index.js
CHANGED
|
@@ -63,7 +63,7 @@ var redocUI = (opts) => {
|
|
|
63
63
|
return middleware.web(async (ctx, next) => {
|
|
64
64
|
const { pathname, method } = ctx.request;
|
|
65
65
|
if (method !== "GET" && method !== "HEAD") return next();
|
|
66
|
-
if (!pathname.startsWith(uriPrefix)) return next();
|
|
66
|
+
if (pathname !== uriPrefix && !pathname.startsWith(uriPrefix + "/")) return next();
|
|
67
67
|
if (enable && !await enable(ctx)) return next();
|
|
68
68
|
if (pathname === uriPrefix || pathname === htmlPath) {
|
|
69
69
|
await loading();
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { compose, I18n, middleware, OpenAPI } from '@aomex/common';\nimport { serveStatic } from '@aomex/serve-static';\nimport type { WebContext, WebMiddleware } from '@aomex/web';\nimport { readFile } from 'node:fs/promises';\nimport path from 'node:path';\nimport YAML from 'yaml';\n\nexport interface RedocUIOptions {\n /**\n * 文档内容,有三种方式可以提供openapi文档:\n * 1. 文件路径,支持json和yaml格式\n * 2. 传入函数直接生成\n * 3. 利用`@aomex/openapi`动态生成\n *\n * ```typescript\n * import { generateOpenapi } from '@aomex/openapi';\n *\n * redocUI({ openapi: './openapi.json' });\n * redocUI({ openapi: './openapi.yaml' });\n * redocUI({\n * openapi: () => {\n * return {\n * version: '3.0.0',\n * info: { title: 'aomex', version: '1.0.1' },\n * paths: {}\n * };\n * }\n * });\n * redocUI({\n * openapi: async () => {\n * return generateOpenapi({ routers: './src/routers' });\n * }\n * });\n * ```\n */\n openapi: string | (() => Promise<OpenAPI.Document> | OpenAPI.Document);\n /**\n * 为了更好地区分常规接口和redoc文档,建议设置一个请求路径前缀。默认值:`'/redoc'`\n *\n */\n uriPrefix?: string;\n /**\n * 是否允许访问文档服务,每次请求都会询问\n */\n enable?: (ctx?: WebContext) => boolean | Promise<boolean>;\n /**\n * 放在<head></head>内的标签集合\n *\n * ```javascript\n * [{\n * tag: 'link',\n * props: {\n * rel: 'shortcut icon',\n * type: 'image/x-icon',\n * href: 'http://host/favicon.ico',\n * },\n * }]\n * ```\n */\n headTags?: {\n /**\n * 标签名,如:meta, link\n */\n tag: string;\n /**\n * 标签内属性\n */\n props: Record<string, any>;\n /**\n * 是否自闭合标签,类似 <br />。默认值:`true`\n */\n selfClosing?: boolean;\n }[];\n}\n\nexport const redocUI = (opts: RedocUIOptions): WebMiddleware => {\n let { uriPrefix = '/redoc', openapi, enable, headTags = [] } = opts;\n uriPrefix = uriPrefix.split('/').map(encodeURIComponent).join('/');\n if (!uriPrefix.startsWith('/')) uriPrefix = '/' + uriPrefix;\n\n const htmlPath = path.posix.join(uriPrefix, 'index.html');\n const openapiPath = path.posix.join(uriPrefix, 'openapi.json');\n const publicDir = path.join(import.meta.dirname, '..', 'public');\n const htmlFile = path.join(publicDir, 'index.html');\n\n const i18n = new I18n({\n resources: { zh_CN: {}, en_US: {} },\n defaultLanguage: 'zh_CN',\n });\n\n const staticFn = compose([\n serveStatic({\n root: publicDir,\n useCompressedFile: false,\n indexFile: false,\n formatPath(pathname) {\n return pathname.slice(uriPrefix.length);\n },\n cacheControl: {\n maxAge: 3600_000,\n },\n }),\n ]);\n\n let html = '';\n let specs: string;\n let promise: Promise<any>;\n\n const loading = () => {\n if (promise) return promise;\n\n promise = new Promise(async (resolve) => {\n let docs: OpenAPI.Document;\n if (typeof openapi === 'function') {\n docs = await openapi();\n specs = JSON.stringify(docs);\n } else {\n const content = await readFile(path.resolve(openapi), 'utf8');\n if (openapi.endsWith('.yaml') || openapi.endsWith('.yml')) {\n docs = YAML.parse(content);\n specs = JSON.stringify(docs);\n } else {\n docs = JSON.parse(content);\n specs = JSON.stringify(docs);\n }\n }\n\n html = await readFile(htmlFile, 'utf8');\n html = html\n .replaceAll('#title#', docs.info.title)\n .replaceAll('#prefix#', '.' + uriPrefix)\n .replaceAll('#lang#', i18n.language)\n .replaceAll(\n '#headTags#',\n headTags\n .map(({ tag, props, selfClosing = true }) => {\n return `<${tag} ${Object.entries(props)\n .map(([key, value]) => `${key}=\"${value}\"`)\n .join(' ')} ${selfClosing ? '/>' : `></${tag}>`}`;\n })\n .join('\\n'),\n );\n resolve(undefined);\n });\n return promise;\n };\n\n return middleware.web(async (ctx, next) => {\n const { pathname, method } = ctx.request;\n if (method !== 'GET' && method !== 'HEAD') return next();\n if (!pathname.startsWith(uriPrefix)) return next();\n if (enable && !(await enable(ctx))) return next();\n\n if (pathname === uriPrefix || pathname === htmlPath) {\n await loading();\n ctx.send(html);\n return;\n }\n\n if (pathname === openapiPath) {\n await loading();\n ctx.send(specs);\n ctx.response.contentType = 'json';\n return;\n }\n\n return staticFn(ctx, next);\n });\n};\n"],"mappings":";AAAA,SAAS,SAAS,MA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,QAAQ,SAAS,MAAM,GAAG;AACzD,iBAAO,KAAK,MAAM,OAAO;AACzB,kBAAQ,KAAK,UAAU,IAAI;AAAA,QAC7B,OAAO;AACL,iBAAO,KAAK,MAAM,OAAO;AACzB,kBAAQ,KAAK,UAAU,IAAI;AAAA,QAC7B;AAAA,MACF;AAEA,aAAO,MAAM,SAAS,UAAU,MAAM;AACtC,aAAO,KACJ,WAAW,WAAW,KAAK,KAAK,KAAK,EACrC,WAAW,YAAY,MAAM,SAAS,EACtC,WAAW,UAAU,KAAK,QAAQ,EAClC;AAAA,QACC;AAAA,QACA,SACG,IAAI,CAAC,EAAE,KAAK,OAAO,cAAc,KAAK,MAAM;AAC3C,iBAAO,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK,EACnC,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM,GAAG,GAAG,KAAK,KAAK,GAAG,EACzC,KAAK,GAAG,CAAC,IAAI,cAAc,OAAO,MAAM,GAAG,GAAG;AAAA,QACnD,CAAC,EACA,KAAK,IAAI;AAAA,MACd;AACF,cAAQ,MAAS;AAAA,IACnB,CAAC;AACD,WAAO;AAAA,EACT;AAEA,SAAO,WAAW,IAAI,OAAO,KAAK,SAAS;AACzC,UAAM,EAAE,UAAU,OAAO,IAAI,IAAI;AACjC,QAAI,WAAW,SAAS,WAAW,OAAQ,QAAO,KAAK;
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { compose, I18n, middleware, OpenAPI } from '@aomex/common';\nimport { serveStatic } from '@aomex/serve-static';\nimport type { WebContext, WebMiddleware } from '@aomex/web';\nimport { readFile } from 'node:fs/promises';\nimport path from 'node:path';\nimport YAML from 'yaml';\n\nexport interface RedocUIOptions {\n /**\n * 文档内容,有三种方式可以提供openapi文档:\n * 1. 文件路径,支持json和yaml格式\n * 2. 传入函数直接生成\n * 3. 利用`@aomex/openapi`动态生成\n *\n * ```typescript\n * import { generateOpenapi } from '@aomex/openapi';\n *\n * redocUI({ openapi: './openapi.json' });\n * redocUI({ openapi: './openapi.yaml' });\n * redocUI({\n * openapi: () => {\n * return {\n * version: '3.0.0',\n * info: { title: 'aomex', version: '1.0.1' },\n * paths: {}\n * };\n * }\n * });\n * redocUI({\n * openapi: async () => {\n * return generateOpenapi({ routers: './src/routers' });\n * }\n * });\n * ```\n */\n openapi: string | (() => Promise<OpenAPI.Document> | OpenAPI.Document);\n /**\n * 为了更好地区分常规接口和redoc文档,建议设置一个请求路径前缀。默认值:`'/redoc'`\n *\n */\n uriPrefix?: string;\n /**\n * 是否允许访问文档服务,每次请求都会询问\n */\n enable?: (ctx?: WebContext) => boolean | Promise<boolean>;\n /**\n * 放在<head></head>内的标签集合\n *\n * ```javascript\n * [{\n * tag: 'link',\n * props: {\n * rel: 'shortcut icon',\n * type: 'image/x-icon',\n * href: 'http://host/favicon.ico',\n * },\n * }]\n * ```\n */\n headTags?: {\n /**\n * 标签名,如:meta, link\n */\n tag: string;\n /**\n * 标签内属性\n */\n props: Record<string, any>;\n /**\n * 是否自闭合标签,类似 <br />。默认值:`true`\n */\n selfClosing?: boolean;\n }[];\n}\n\nexport const redocUI = (opts: RedocUIOptions): WebMiddleware => {\n let { uriPrefix = '/redoc', openapi, enable, headTags = [] } = opts;\n uriPrefix = uriPrefix.split('/').map(encodeURIComponent).join('/');\n if (!uriPrefix.startsWith('/')) uriPrefix = '/' + uriPrefix;\n\n const htmlPath = path.posix.join(uriPrefix, 'index.html');\n const openapiPath = path.posix.join(uriPrefix, 'openapi.json');\n const publicDir = path.join(import.meta.dirname, '..', 'public');\n const htmlFile = path.join(publicDir, 'index.html');\n\n const i18n = new I18n({\n resources: { zh_CN: {}, en_US: {} },\n defaultLanguage: 'zh_CN',\n });\n\n const staticFn = compose([\n serveStatic({\n root: publicDir,\n useCompressedFile: false,\n indexFile: false,\n formatPath(pathname) {\n return pathname.slice(uriPrefix.length);\n },\n cacheControl: {\n maxAge: 3600_000,\n },\n }),\n ]);\n\n let html = '';\n let specs: string;\n let promise: Promise<any>;\n\n const loading = () => {\n if (promise) return promise;\n\n promise = new Promise(async (resolve) => {\n let docs: OpenAPI.Document;\n if (typeof openapi === 'function') {\n docs = await openapi();\n specs = JSON.stringify(docs);\n } else {\n const content = await readFile(path.resolve(openapi), 'utf8');\n if (openapi.endsWith('.yaml') || openapi.endsWith('.yml')) {\n docs = YAML.parse(content);\n specs = JSON.stringify(docs);\n } else {\n docs = JSON.parse(content);\n specs = JSON.stringify(docs);\n }\n }\n\n html = await readFile(htmlFile, 'utf8');\n html = html\n .replaceAll('#title#', docs.info.title)\n .replaceAll('#prefix#', '.' + uriPrefix)\n .replaceAll('#lang#', i18n.language)\n .replaceAll(\n '#headTags#',\n headTags\n .map(({ tag, props, selfClosing = true }) => {\n return `<${tag} ${Object.entries(props)\n .map(([key, value]) => `${key}=\"${value}\"`)\n .join(' ')} ${selfClosing ? '/>' : `></${tag}>`}`;\n })\n .join('\\n'),\n );\n resolve(undefined);\n });\n return promise;\n };\n\n return middleware.web(async (ctx, next) => {\n const { pathname, method } = ctx.request;\n if (method !== 'GET' && method !== 'HEAD') return next();\n // 必须精确匹配前缀或前缀+/,避免 /redoc../ 这类路径穿越\n if (pathname !== uriPrefix && !pathname.startsWith(uriPrefix + '/')) return next();\n if (enable && !(await enable(ctx))) return next();\n\n if (pathname === uriPrefix || pathname === htmlPath) {\n await loading();\n ctx.send(html);\n return;\n }\n\n if (pathname === openapiPath) {\n await loading();\n ctx.send(specs);\n ctx.response.contentType = 'json';\n return;\n }\n\n return staticFn(ctx, next);\n });\n};\n"],"mappings":";AAAA,SAAS,SAAS,MAAM,kBAA2B;AACnD,SAAS,mBAAmB;AAE5B,SAAS,gBAAgB;AACzB,OAAO,UAAU;AACjB,OAAO,UAAU;AAsEV,IAAM,UAAU,CAAC,SAAwC;AAC9D,MAAI,EAAE,YAAY,UAAU,SAAS,QAAQ,WAAW,CAAC,EAAE,IAAI;AAC/D,cAAY,UAAU,MAAM,GAAG,EAAE,IAAI,kBAAkB,EA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ACJ,WAAW,WAAW,KAAK,KAAK,KAAK,EACrC,WAAW,YAAY,MAAM,SAAS,EACtC,WAAW,UAAU,KAAK,QAAQ,EAClC;AAAA,QACC;AAAA,QACA,SACG,IAAI,CAAC,EAAE,KAAK,OAAO,cAAc,KAAK,MAAM;AAC3C,iBAAO,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK,EACnC,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM,GAAG,GAAG,KAAK,KAAK,GAAG,EACzC,KAAK,GAAG,CAAC,IAAI,cAAc,OAAO,MAAM,GAAG,GAAG;AAAA,QACnD,CAAC,EACA,KAAK,IAAI;AAAA,MACd;AACF,cAAQ,MAAS;AAAA,IACnB,CAAC;AACD,WAAO;AAAA,EACT;AAEA,SAAO,WAAW,IAAI,OAAO,KAAK,SAAS;AACzC,UAAM,EAAE,UAAU,OAAO,IAAI,IAAI;AACjC,QAAI,WAAW,SAAS,WAAW,OAAQ,QAAO,KAAK;AAEvD,QAAI,aAAa,aAAa,CAAC,SAAS,WAAW,YAAY,GAAG,EAAG,QAAO,KAAK;AACjF,QAAI,UAAU,CAAE,MAAM,OAAO,GAAG,EAAI,QAAO,KAAK;AAEhD,QAAI,aAAa,aAAa,aAAa,UAAU;AACnD,YAAM,QAAQ;AACd,UAAI,KAAK,IAAI;AACb;AAAA,IACF;AAEA,QAAI,aAAa,aAAa;AAC5B,YAAM,QAAQ;AACd,UAAI,KAAK,KAAK;AACd,UAAI,SAAS,cAAc;AAC3B;AAAA,IACF;AAEA,WAAO,SAAS,KAAK,IAAI;AAAA,EAC3B,CAAC;AACH;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aomex/redoc-ui",
|
|
3
|
-
"version": "3.30.
|
|
3
|
+
"version": "3.30.7",
|
|
4
4
|
"description": "Redoc文档展示UI",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -34,13 +34,13 @@
|
|
|
34
34
|
"@aomex/web": "^3.0.0"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@aomex/serve-static": "3.30.
|
|
37
|
+
"@aomex/serve-static": "3.30.7",
|
|
38
38
|
"redoc": "^2.5.0",
|
|
39
39
|
"yaml": "^2.8.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@aomex/common": "3.30.
|
|
43
|
-
"@aomex/web": "3.30.
|
|
42
|
+
"@aomex/common": "3.30.7",
|
|
43
|
+
"@aomex/web": "3.30.7"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {}
|
|
46
46
|
}
|