@aomex/access-log 4.12.0 → 4.14.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 +15 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,21 @@
|
|
|
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
|
+
# [4.14.0](https://github.com/aomex/aomex/compare/v4.13.0...v4.14.0) (2026-12-03)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* **web:** 移除废弃的305状态码 ([e0a163e](https://github.com/aomex/aomex/commit/e0a163e25c481811d58930b3f7cba4e79cfe356a))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# [4.13.0](https://github.com/aomex/aomex/compare/v4.12.0...v4.13.0) (2026-12-01)
|
|
14
|
+
|
|
15
|
+
**Note:** Version bump only for package @aomex/access-log
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
6
21
|
# [4.12.0](https://github.com/aomex/aomex/compare/v4.11.0...v4.12.0) (2026-12-01)
|
|
7
22
|
|
|
8
23
|
**Note:** Version bump only for package @aomex/access-log
|
package/dist/index.js
CHANGED
|
@@ -60,7 +60,7 @@ const replaceToken = async (opts) => {
|
|
|
60
60
|
replace(styleText(c, statusCode.toString()));
|
|
61
61
|
break;
|
|
62
62
|
case "[contentLength]":
|
|
63
|
-
replace(styleText("gray", statuses.
|
|
63
|
+
replace(styleText("gray", statuses.isEmpty(statusCode) || contentLength === 0 ? "0" : contentLength == null ? "-" : bytes(contentLength).toLowerCase()));
|
|
64
64
|
break;
|
|
65
65
|
case "[contentType]":
|
|
66
66
|
replace(styleText("gray", contentType));
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/http-access-log-token.ts","../src/counter.ts","../src/replace-token.ts","../src/http-access-log.ts"],"sourcesContent":["export enum HttpAccessLogToken {\n method = '[method]',\n url = '[url]',\n statusCode = '[statusCode]',\n contentLength = '[contentLength]',\n contentType = '[contentType]',\n duration = '[duration]',\n ip = '[ip]',\n}\n","import stream, { type TransformCallback } from 'node:stream';\n\nexport class Counter extends stream.Transform {\n public length: number = 0;\n\n override _transform(\n chunk: any,\n _encoding: BufferEncoding,\n callback: TransformCallback,\n ): void {\n this.length += chunk.length;\n this.push(chunk);\n callback();\n }\n}\n","import prettyTime from 'pretty-time';\nimport { bytes } from '@aomex/internal-tools';\nimport { statuses } from '@aomex/web';\nimport { HttpAccessLogToken } from './http-access-log-token';\nimport { styleText } from 'node:util';\n\nexport const colorCodes = <const>{\n 7: 'magenta',\n 5: 'red',\n 4: 'yellow',\n 3: 'cyan',\n 2: 'green',\n 1: 'green',\n 0: 'yellow',\n};\n\nconst pattern = /\\[[^\\[\\]]+\\]/g;\n\nexport const replaceToken = async (opts: {\n message: string;\n startTime: [number, number];\n tokens?: { [key: string]: () => string | Promise<string> };\n request: {\n method: string;\n url: string;\n ip: string;\n };\n response?: {\n statusCode?: number;\n contentType?: string;\n contentLength?: number;\n };\n}): Promise<string> => {\n const { message, startTime, request, response = {}, tokens = {} } = opts;\n const { method, url } = request;\n const { contentLength, contentType = '-', statusCode = 404 } = response;\n let formatted = message;\n\n for (let match of message.matchAll(pattern)) {\n const tokenWithBrackets = match[0];\n const token = tokenWithBrackets.slice(1, -1);\n\n const replace = (data: string) => {\n formatted = formatted.replaceAll(tokenWithBrackets, data);\n };\n\n if (tokens[token]) {\n replace(await tokens[token]!());\n }\n\n switch (`[${token}]`) {\n case HttpAccessLogToken.method:\n replace(styleText('bold', method));\n break;\n case HttpAccessLogToken.url:\n replace(styleText('gray', url));\n break;\n case HttpAccessLogToken.statusCode:\n const c =\n colorCodes[((statusCode / 100) | 0) as keyof typeof colorCodes] ||\n colorCodes[0]!;\n replace(styleText(c, statusCode.toString()));\n break;\n case HttpAccessLogToken.contentLength:\n replace(\n styleText(\n 'gray',\n statuses.
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/http-access-log-token.ts","../src/counter.ts","../src/replace-token.ts","../src/http-access-log.ts"],"sourcesContent":["export enum HttpAccessLogToken {\n method = '[method]',\n url = '[url]',\n statusCode = '[statusCode]',\n contentLength = '[contentLength]',\n contentType = '[contentType]',\n duration = '[duration]',\n ip = '[ip]',\n}\n","import stream, { type TransformCallback } from 'node:stream';\n\nexport class Counter extends stream.Transform {\n public length: number = 0;\n\n override _transform(\n chunk: any,\n _encoding: BufferEncoding,\n callback: TransformCallback,\n ): void {\n this.length += chunk.length;\n this.push(chunk);\n callback();\n }\n}\n","import prettyTime from 'pretty-time';\nimport { bytes } from '@aomex/internal-tools';\nimport { statuses } from '@aomex/web';\nimport { HttpAccessLogToken } from './http-access-log-token';\nimport { styleText } from 'node:util';\n\nexport const colorCodes = <const>{\n 7: 'magenta',\n 5: 'red',\n 4: 'yellow',\n 3: 'cyan',\n 2: 'green',\n 1: 'green',\n 0: 'yellow',\n};\n\nconst pattern = /\\[[^\\[\\]]+\\]/g;\n\nexport const replaceToken = async (opts: {\n message: string;\n startTime: [number, number];\n tokens?: { [key: string]: () => string | Promise<string> };\n request: {\n method: string;\n url: string;\n ip: string;\n };\n response?: {\n statusCode?: number;\n contentType?: string;\n contentLength?: number;\n };\n}): Promise<string> => {\n const { message, startTime, request, response = {}, tokens = {} } = opts;\n const { method, url } = request;\n const { contentLength, contentType = '-', statusCode = 404 } = response;\n let formatted = message;\n\n for (let match of message.matchAll(pattern)) {\n const tokenWithBrackets = match[0];\n const token = tokenWithBrackets.slice(1, -1);\n\n const replace = (data: string) => {\n formatted = formatted.replaceAll(tokenWithBrackets, data);\n };\n\n if (tokens[token]) {\n replace(await tokens[token]!());\n }\n\n switch (`[${token}]`) {\n case HttpAccessLogToken.method:\n replace(styleText('bold', method));\n break;\n case HttpAccessLogToken.url:\n replace(styleText('gray', url));\n break;\n case HttpAccessLogToken.statusCode:\n const c =\n colorCodes[((statusCode / 100) | 0) as keyof typeof colorCodes] ||\n colorCodes[0]!;\n replace(styleText(c, statusCode.toString()));\n break;\n case HttpAccessLogToken.contentLength:\n replace(\n styleText(\n 'gray',\n statuses.isEmpty(statusCode) || contentLength === 0\n ? '0'\n : contentLength == null\n ? '-'\n : bytes(contentLength).toLowerCase(),\n ),\n );\n break;\n case HttpAccessLogToken.contentType:\n replace(styleText('gray', contentType));\n break;\n case HttpAccessLogToken.duration:\n replace(styleText('gray', prettyTime(process.hrtime(startTime))));\n break;\n case HttpAccessLogToken.ip:\n replace(styleText('gray', request.ip));\n break;\n }\n }\n\n return formatted;\n};\n","import { middleware } from '@aomex/common';\nimport { Logger, ConsoleTransport, type Transport } from '@aomex/logger';\nimport type { WebContext, WebMiddleware } from '@aomex/web';\nimport { HttpAccessLogToken } from './http-access-log-token';\nimport { Counter } from './counter';\nimport stream from 'node:stream';\nimport { replaceToken } from './replace-token';\n\nexport interface HttpAccessLogOptions {\n /**\n * 是否禁用请求日志。默认:`false`\n */\n disabled?: boolean;\n /**\n * 响应输出格式,建议使用 **HttpAccessLogToken** 拼接。默认值:`[ip] [method] [url] [statusCode] [duration] [contentLength]`\n */\n format?: string;\n /**\n * 日志输出端口。默认使用 `ConsoleTransport`;传空数组相当于禁用。\n */\n transports?: Transport[];\n /**\n * 自定义关键词\n * ```\n * httpAccessLog({\n * format: ' [request] [time] [href]',\n * customTokens: {\n * href: (ctx) => ctx.request.href,\n * },\n * })\n * ```\n */\n customTokens?: { [key: string]: (ctx: WebContext) => string | Promise<string> };\n}\n\n/**\n * HTTP 访问日志。挂在 `WebApp.mount` 里。\n */\nexport const httpAccessLog = (options: HttpAccessLogOptions = {}): WebMiddleware => {\n const { disabled = false, customTokens } = options;\n const format =\n options.format ||\n `${HttpAccessLogToken.ip} ${HttpAccessLogToken.method} ${HttpAccessLogToken.url} ${HttpAccessLogToken.statusCode} ${HttpAccessLogToken.duration} ${HttpAccessLogToken.contentLength}`;\n\n const transportList = options.transports ?? [new ConsoleTransport()];\n\n const logger = new Logger({\n transports: transportList.map((transport) => ({ transport })),\n });\n\n return middleware.web(async (ctx, next) => {\n if (disabled || transportList.length === 0) return next();\n\n const start = process.hrtime();\n const tokens = customTokens\n ? Object.fromEntries(\n Object.entries(customTokens).map(([key, value]) => {\n return [key, value.bind(null, ctx)];\n }),\n )\n : {};\n\n try {\n await next();\n } finally {\n const {\n response,\n response: { body },\n } = ctx;\n let counter: Counter | undefined;\n if (body instanceof stream.Readable && body.readable) {\n ctx.send(body.pipe((counter = new Counter())));\n }\n\n response.once('close', async () => {\n logger.info(\n await replaceToken({\n message: format,\n tokens,\n startTime: start,\n request: ctx.request,\n response: {\n statusCode: response.statusCode,\n contentType: response.contentType,\n contentLength: counter ? counter.length : response.contentLength,\n },\n }),\n );\n });\n }\n });\n};\n"],"mappings":";;;;;;;;AAAA,IAAY,qBAAL,yBAAA,oBAAA;CACL,mBAAA,YAAA;CACA,mBAAA,SAAA;CACA,mBAAA,gBAAA;CACA,mBAAA,mBAAA;CACA,mBAAA,iBAAA;CACA,mBAAA,cAAA;CACA,mBAAA,QAAA;;AACF,EAAA,CAAA,CAAA;;ACNA,IAAa,UAAb,cAA6B,OAAO,UAAU;CAC5C,SAAwB;CAExB,WACE,OACA,WACA,UACM;EACN,KAAK,UAAU,MAAM;EACrB,KAAK,KAAK,KAAK;EACf,SAAS;CACX;AACF;;ACRA,MAAa,aAAoB;CAC/B,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;AACL;AAEA,MAAM,UAAU;AAEhB,MAAa,eAAe,OAAO,SAcZ;CACrB,MAAM,EAAE,SAAS,WAAW,SAAS,WAAW,CAAC,GAAG,SAAS,CAAC,MAAM;CACpE,MAAM,EAAE,QAAQ,QAAQ;CACx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OAAO,KAAK,SAAS;EACzC,IAAI,YAAY,cAAc,WAAW,GAAG,OAAO,KAAK;EAExD,MAAM,QAAQ,QAAQ,OAAO;EAC7B,MAAM,SAAS,eACX,OAAO,YACL,OAAO,QAAQ,YAAY,CAAC,CAAC,KAAK,CAAC,KAAK,WAAW;GACjD,OAAO,CAAC,KAAK,MAAM,KAAK,MAAM,GAAG,CAAC;EACpC,CAAC,CACH,IACA,CAAC;EAEL,IAAI;GACF,MAAM,KAAK;EACb,UAAU;GACR,MAAM,EACJ,UACA,UAAU,EAAE,WACV;GACJ,IAAI;GACJ,IAAI,gBAAgB,OAAO,YAAY,KAAK,UAC1C,IAAI,KAAK,KAAK,KAAM,UAAU,IAAI,QAAQ,CAAE,CAAC;GAG/C,SAAS,KAAK,SAAS,YAAY;IACjC,OAAO,KACL,MAAM,aAAa;KACjB,SAAS;KACT;KACA,WAAW;KACX,SAAS,IAAI;KACb,UAAU;MACR,YAAY,SAAS;MACrB,aAAa,SAAS;MACtB,eAAe,UAAU,QAAQ,SAAS,SAAS;KACrD;IACF,CAAC,CACH;GACF,CAAC;EACH;CACF,CAAC;AACH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aomex/access-log",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.14.0",
|
|
4
4
|
"description": "访问日志",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -45,14 +45,14 @@
|
|
|
45
45
|
}
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@aomex/internal-tools": "4.
|
|
49
|
-
"@aomex/logger": "4.
|
|
48
|
+
"@aomex/internal-tools": "4.14.0",
|
|
49
|
+
"@aomex/logger": "4.14.0",
|
|
50
50
|
"pretty-time": "^1.1.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@aomex/common": "4.
|
|
54
|
-
"@aomex/web": "4.
|
|
55
|
-
"@aomex/websocket": "4.
|
|
53
|
+
"@aomex/common": "4.14.0",
|
|
54
|
+
"@aomex/web": "4.14.0",
|
|
55
|
+
"@aomex/websocket": "4.14.0",
|
|
56
56
|
"@types/pretty-time": "^1.1.5"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {}
|