mm_response 1.0.1 → 1.1.1
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 +13 -1
- package/data/response.const.json +38 -0
- package/index.js +2 -4
- package/lib/reply.js +66 -66
- package/lib/response.js +9 -9
- package/package.json +44 -39
- package/tool/reply/index.js +38 -0
- package/tool/reply/tool.json +68 -0
package/README.md
CHANGED
|
@@ -9,7 +9,8 @@ Web 响应渲染中间件。为 `mm_web_server` 提供可挂载的响应写出
|
|
|
9
9
|
- 支持 XML 响应:`{ type: 'xml' }` 或 `{ type: 'application/xml' | 'text/xml' }`
|
|
10
10
|
- 支持图片、视频、音频响应:`body` 可为 Buffer / Uint8Array、可读流或物理文件路径
|
|
11
11
|
- 错误统一写出 `500 { "error": 错误信息 }`
|
|
12
|
-
-
|
|
12
|
+
- 支持 JSON 响应:`{ type: 'json' }` 或 `{ type: 'application/json' }`,字符串响应体视为已序列化的 JSON 文本原样写出
|
|
13
|
+
- 导出 `isReply` / `getMediaType` / `isJsonType` / `sendHtml` / `sendText` / `sendJson` / `sendJsonBody` / `sendEmpty` / `sendXml` / `sendFile` / `sendMedia` / `sendImage` / `sendVideo` / `sendAudio` / `sendBody` / `sendResult`,可单独复用
|
|
13
14
|
|
|
14
15
|
## 安装
|
|
15
16
|
|
|
@@ -55,6 +56,7 @@ await web.start();
|
|
|
55
56
|
| `sendHtml(res, status, text)` | 写 `text/html` |
|
|
56
57
|
| `sendText(res, status, text)` | 写 `text/plain` |
|
|
57
58
|
| `sendJson(res, status, payload)` | 写 `application/json` |
|
|
59
|
+
| `sendJsonBody(res, status, body)` | 写 `application/json`,字符串原样写出,其余按 JSON 序列化 |
|
|
58
60
|
| `sendEmpty(res, status)` | 写空响应 |
|
|
59
61
|
| `sendXml(res, status, text, content_type?)` | 写 XML,缺省 `application/xml; charset=utf-8` |
|
|
60
62
|
| `sendFile(res, status, file_path, content_type)` | 流式写文件并带 `content-length`,文件不存在时为 `404` |
|
|
@@ -77,6 +79,7 @@ await web.start();
|
|
|
77
79
|
| 普通对象 | `200` + `application/json` |
|
|
78
80
|
| `{ status, body }` | 按 `status` 写出 |
|
|
79
81
|
| `{ status, body, type: 'html' }` | `text/html` |
|
|
82
|
+
| `{ body, type: 'json' \| 'application/json' }` | `application/json; charset=utf-8`,字符串原样写出 |
|
|
80
83
|
| `{ body, type: 'xml' }` | `application/xml; charset=utf-8` |
|
|
81
84
|
| `{ body, type: 'application/xml' \| 'text/xml' }` | 使用给定内容类型并补 `charset=utf-8` |
|
|
82
85
|
| `{ body, type: 'image' }` | 图片,`content_type` 优先,否则按文件路径扩展名推断,缺省 `image/png` |
|
|
@@ -148,3 +151,12 @@ ISC License
|
|
|
148
151
|
## 作者
|
|
149
152
|
|
|
150
153
|
邱文武
|
|
154
|
+
|
|
155
|
+
## 工具契约(LLM 可调用)
|
|
156
|
+
|
|
157
|
+
本模块 `tool/` 下登记以下工具契约,实现位于 `lib/tools/<name>/`,`tool/<name>/index.js` 仅为薄适配层。
|
|
158
|
+
其它模块可直接 `require('<pkg>').tools.<name>` 复用实现,无需经过工具注册面。
|
|
159
|
+
|
|
160
|
+
| 工具名 | 标题 | 说明 |
|
|
161
|
+
| --- | --- | --- |
|
|
162
|
+
| `reply` | 响应内容协商 | 媒体类型推导与 reply 结构判定 |
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"MEDIA_TYPES": {
|
|
3
|
+
"png": "image/png",
|
|
4
|
+
"jpg": "image/jpeg",
|
|
5
|
+
"jpeg": "image/jpeg",
|
|
6
|
+
"gif": "image/gif",
|
|
7
|
+
"webp": "image/webp",
|
|
8
|
+
"svg": "image/svg+xml",
|
|
9
|
+
"bmp": "image/bmp",
|
|
10
|
+
"ico": "image/x-icon",
|
|
11
|
+
"avif": "image/avif",
|
|
12
|
+
"mp4": "video/mp4",
|
|
13
|
+
"m4v": "video/mp4",
|
|
14
|
+
"webm": "video/webm",
|
|
15
|
+
"ogv": "video/ogg",
|
|
16
|
+
"mov": "video/quicktime",
|
|
17
|
+
"avi": "video/x-msvideo",
|
|
18
|
+
"mkv": "video/x-matroska",
|
|
19
|
+
"mp3": "audio/mpeg",
|
|
20
|
+
"wav": "audio/wav",
|
|
21
|
+
"ogg": "audio/ogg",
|
|
22
|
+
"oga": "audio/ogg",
|
|
23
|
+
"m4a": "audio/mp4",
|
|
24
|
+
"aac": "audio/aac",
|
|
25
|
+
"flac": "audio/flac",
|
|
26
|
+
"opus": "audio/opus",
|
|
27
|
+
"weba": "audio/webm",
|
|
28
|
+
"mid": "audio/midi",
|
|
29
|
+
"midi": "audio/midi"
|
|
30
|
+
},
|
|
31
|
+
"DEFAULT_IMAGE_TYPE": "image/png",
|
|
32
|
+
"DEFAULT_VIDEO_TYPE": "video/mp4",
|
|
33
|
+
"DEFAULT_AUDIO_TYPE": "audio/mpeg",
|
|
34
|
+
"JSON_TYPE": "application/json; charset=utf-8",
|
|
35
|
+
"XML_TYPE": "application/xml; charset=utf-8",
|
|
36
|
+
"XML_MIME_TYPES": ["application/xml", "text/xml"],
|
|
37
|
+
"OCTET_STREAM_TYPE": "application/octet-stream"
|
|
38
|
+
}
|
package/index.js
CHANGED
package/lib/reply.js
CHANGED
|
@@ -1,58 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const fs = require('fs');
|
|
4
|
+
const { constAdmin } = require('mm_consts');
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
const MEDIA_TYPES = {
|
|
7
|
-
// 图片
|
|
8
|
-
png: 'image/png',
|
|
9
|
-
jpg: 'image/jpeg',
|
|
10
|
-
jpeg: 'image/jpeg',
|
|
11
|
-
gif: 'image/gif',
|
|
12
|
-
webp: 'image/webp',
|
|
13
|
-
svg: 'image/svg+xml',
|
|
14
|
-
bmp: 'image/bmp',
|
|
15
|
-
ico: 'image/x-icon',
|
|
16
|
-
avif: 'image/avif',
|
|
17
|
-
// 视频
|
|
18
|
-
mp4: 'video/mp4',
|
|
19
|
-
m4v: 'video/mp4',
|
|
20
|
-
webm: 'video/webm',
|
|
21
|
-
ogv: 'video/ogg',
|
|
22
|
-
mov: 'video/quicktime',
|
|
23
|
-
avi: 'video/x-msvideo',
|
|
24
|
-
mkv: 'video/x-matroska',
|
|
25
|
-
// 音频
|
|
26
|
-
mp3: 'audio/mpeg',
|
|
27
|
-
wav: 'audio/wav',
|
|
28
|
-
ogg: 'audio/ogg',
|
|
29
|
-
oga: 'audio/ogg',
|
|
30
|
-
m4a: 'audio/mp4',
|
|
31
|
-
aac: 'audio/aac',
|
|
32
|
-
flac: 'audio/flac',
|
|
33
|
-
opus: 'audio/opus',
|
|
34
|
-
weba: 'audio/webm',
|
|
35
|
-
mid: 'audio/midi',
|
|
36
|
-
midi: 'audio/midi'
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// 缺省图片内容类型
|
|
40
|
-
const DEFAULT_IMAGE_TYPE = 'image/png';
|
|
41
|
-
|
|
42
|
-
// 缺省视频内容类型
|
|
43
|
-
const DEFAULT_VIDEO_TYPE = 'video/mp4';
|
|
44
|
-
|
|
45
|
-
// 缺省音频内容类型
|
|
46
|
-
const DEFAULT_AUDIO_TYPE = 'audio/mpeg';
|
|
47
|
-
|
|
48
|
-
// 缺省 XML 内容类型
|
|
49
|
-
const XML_TYPE = 'application/xml; charset=utf-8';
|
|
50
|
-
|
|
51
|
-
// XML 内容类型集合
|
|
52
|
-
const XML_MIME_TYPES = ['application/xml', 'text/xml'];
|
|
53
|
-
|
|
54
|
-
// 未知二进制内容类型
|
|
55
|
-
const OCTET_STREAM_TYPE = 'application/octet-stream';
|
|
6
|
+
require('mm_consts').loadSync('response', require('path').join(__dirname, '..', 'data'));
|
|
56
7
|
|
|
57
8
|
/**
|
|
58
9
|
* 判断值是否为响应描述对象(含 status 或 body 字段)
|
|
@@ -88,8 +39,8 @@ function isStream(value) {
|
|
|
88
39
|
* @returns {string} 小写扩展名(不含点号),无扩展名时为空串
|
|
89
40
|
*/
|
|
90
41
|
function getExtname(file_path) {
|
|
91
|
-
|
|
92
|
-
|
|
42
|
+
let name = String(file_path || '');
|
|
43
|
+
let index = name.lastIndexOf('.');
|
|
93
44
|
if (index === -1) return '';
|
|
94
45
|
return name.slice(index + 1).toLowerCase();
|
|
95
46
|
}
|
|
@@ -100,7 +51,8 @@ function getExtname(file_path) {
|
|
|
100
51
|
* @returns {string} 内容类型,未知扩展名时返回 application/octet-stream
|
|
101
52
|
*/
|
|
102
53
|
function getMediaType(file_path) {
|
|
103
|
-
|
|
54
|
+
let consts = constAdmin('response');
|
|
55
|
+
return consts.get('MEDIA_TYPES')[getExtname(file_path)] || consts.get('OCTET_STREAM_TYPE');
|
|
104
56
|
}
|
|
105
57
|
|
|
106
58
|
/**
|
|
@@ -119,7 +71,17 @@ function isMediaType(type) {
|
|
|
119
71
|
* @returns {boolean} 是否为 XML 内容类型
|
|
120
72
|
*/
|
|
121
73
|
function isXmlType(type) {
|
|
122
|
-
return XML_MIME_TYPES.includes(type);
|
|
74
|
+
return constAdmin('response').get('XML_MIME_TYPES').includes(type);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 判断是否为 JSON 内容类型标记(json 或含 json 的 MIME 形式)
|
|
79
|
+
* @param {*} type 内容类型标记
|
|
80
|
+
* @returns {boolean} 是否为 JSON 内容类型
|
|
81
|
+
*/
|
|
82
|
+
function isJsonType(type) {
|
|
83
|
+
if (typeof type !== 'string') return false;
|
|
84
|
+
return type.includes('json');
|
|
123
85
|
}
|
|
124
86
|
|
|
125
87
|
/**
|
|
@@ -157,8 +119,22 @@ function sendText(res, status, text) {
|
|
|
157
119
|
*/
|
|
158
120
|
function sendJson(res, status, payload) {
|
|
159
121
|
if (res.writableEnded) return;
|
|
160
|
-
|
|
161
|
-
res.writeHead(status, { 'content-type': '
|
|
122
|
+
let text = JSON.stringify(payload);
|
|
123
|
+
res.writeHead(status, { 'content-type': constAdmin('response').get('JSON_TYPE') });
|
|
124
|
+
res.end(text);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 发送 JSON 响应体:字符串视为已序列化的 JSON 文本原样写出,其余按 JSON 序列化
|
|
129
|
+
* @param {Object} res 响应对象
|
|
130
|
+
* @param {number} status 状态码
|
|
131
|
+
* @param {*} body 响应体
|
|
132
|
+
* @returns {void}
|
|
133
|
+
*/
|
|
134
|
+
function sendJsonBody(res, status, body) {
|
|
135
|
+
if (res.writableEnded) return;
|
|
136
|
+
let text = typeof body === 'string' ? body : JSON.stringify(body);
|
|
137
|
+
res.writeHead(status, { 'content-type': constAdmin('response').get('JSON_TYPE') });
|
|
162
138
|
res.end(text);
|
|
163
139
|
}
|
|
164
140
|
|
|
@@ -184,7 +160,7 @@ function sendEmpty(res, status) {
|
|
|
184
160
|
*/
|
|
185
161
|
function sendXml(res, status, text, content_type) {
|
|
186
162
|
if (res.writableEnded) return;
|
|
187
|
-
res.writeHead(status, { 'content-type': content_type || XML_TYPE });
|
|
163
|
+
res.writeHead(status, { 'content-type': content_type || constAdmin('response').get('XML_TYPE') });
|
|
188
164
|
res.end(text);
|
|
189
165
|
}
|
|
190
166
|
|
|
@@ -262,7 +238,7 @@ function sendMedia(res, status, body, content_type) {
|
|
|
262
238
|
* @returns {void}
|
|
263
239
|
*/
|
|
264
240
|
function sendImage(res, status, body, content_type) {
|
|
265
|
-
|
|
241
|
+
let type = content_type || (typeof body === 'string' ? getMediaType(body) : constAdmin('response').get('DEFAULT_IMAGE_TYPE'));
|
|
266
242
|
sendMedia(res, status, body, type);
|
|
267
243
|
}
|
|
268
244
|
|
|
@@ -275,7 +251,7 @@ function sendImage(res, status, body, content_type) {
|
|
|
275
251
|
* @returns {void}
|
|
276
252
|
*/
|
|
277
253
|
function sendVideo(res, status, body, content_type) {
|
|
278
|
-
|
|
254
|
+
let type = content_type || (typeof body === 'string' ? getMediaType(body) : constAdmin('response').get('DEFAULT_VIDEO_TYPE'));
|
|
279
255
|
sendMedia(res, status, body, type);
|
|
280
256
|
}
|
|
281
257
|
|
|
@@ -288,7 +264,7 @@ function sendVideo(res, status, body, content_type) {
|
|
|
288
264
|
* @returns {void}
|
|
289
265
|
*/
|
|
290
266
|
function sendAudio(res, status, body, content_type) {
|
|
291
|
-
|
|
267
|
+
let type = content_type || (typeof body === 'string' ? getMediaType(body) : constAdmin('response').get('DEFAULT_AUDIO_TYPE'));
|
|
292
268
|
sendMedia(res, status, body, type);
|
|
293
269
|
}
|
|
294
270
|
|
|
@@ -297,11 +273,15 @@ function sendAudio(res, status, body, content_type) {
|
|
|
297
273
|
* @param {Object} res 响应对象
|
|
298
274
|
* @param {number} status 状态码
|
|
299
275
|
* @param {*} body 响应体
|
|
300
|
-
* @param {string} [type] 内容类型标记(html / xml / image / video / audio,或对应 MIME 形式的内容类型),缺省按 body 类型推断
|
|
276
|
+
* @param {string} [type] 内容类型标记(json / html / xml / image / video / audio,或对应 MIME 形式的内容类型),缺省按 body 类型推断
|
|
301
277
|
* @param {string} [content_type] 内容类型,type 为 image / video / audio 时优先使用
|
|
302
278
|
* @returns {void}
|
|
303
279
|
*/
|
|
304
280
|
function sendBody(res, status, body, type, content_type) {
|
|
281
|
+
if (isJsonType(type)) {
|
|
282
|
+
sendJsonBody(res, status, body);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
305
285
|
if (type === 'html') {
|
|
306
286
|
sendHtml(res, status, body);
|
|
307
287
|
return;
|
|
@@ -331,7 +311,7 @@ function sendBody(res, status, body, type, content_type) {
|
|
|
331
311
|
return;
|
|
332
312
|
}
|
|
333
313
|
if (isBinary(body)) {
|
|
334
|
-
sendMedia(res, status, body, OCTET_STREAM_TYPE);
|
|
314
|
+
sendMedia(res, status, body, constAdmin('response').get('OCTET_STREAM_TYPE'));
|
|
335
315
|
return;
|
|
336
316
|
}
|
|
337
317
|
if (typeof body === 'string') {
|
|
@@ -341,9 +321,27 @@ function sendBody(res, status, body, type, content_type) {
|
|
|
341
321
|
sendJson(res, status, body);
|
|
342
322
|
}
|
|
343
323
|
|
|
324
|
+
/**
|
|
325
|
+
* 解析非描述对象结果的 HTTP 状态码:
|
|
326
|
+
* code >= 50000 表示服务端故障(脚本异常等),映射为 HTTP 500,
|
|
327
|
+
* 与「路由不存在」(10000) 等业务级错误区分,保证 5xx 真实可见、可被监控告警。
|
|
328
|
+
* (与 mm_web_server 的 _statusOf 同一约定:错误码分段契约,两处实现互为兜底)
|
|
329
|
+
* @param {*} result 事件处理结果
|
|
330
|
+
* @returns {number} HTTP 状态码
|
|
331
|
+
*/
|
|
332
|
+
function statusOfResult(result) {
|
|
333
|
+
if (result !== null && typeof result === 'object') {
|
|
334
|
+
let code = result.error && result.error.code;
|
|
335
|
+
if (typeof code === 'number' && code >= 50000) {
|
|
336
|
+
return 500;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return 200;
|
|
340
|
+
}
|
|
341
|
+
|
|
344
342
|
/**
|
|
345
343
|
* 写出事件处理结果:
|
|
346
|
-
* 已写出则跳过;无结果为 404
|
|
344
|
+
* 已写出则跳过;无结果为 404;非描述对象按错误码约定写出状态;描述对象按 status/body/type/content_type 写出
|
|
347
345
|
* @param {Object} res 响应对象
|
|
348
346
|
* @param {*} result 事件处理结果
|
|
349
347
|
* @returns {void}
|
|
@@ -357,11 +355,11 @@ function sendResult(res, result) {
|
|
|
357
355
|
}
|
|
358
356
|
|
|
359
357
|
if (!isReply(result)) {
|
|
360
|
-
sendBody(res,
|
|
358
|
+
sendBody(res, statusOfResult(result), result);
|
|
361
359
|
return;
|
|
362
360
|
}
|
|
363
361
|
|
|
364
|
-
|
|
362
|
+
let status = result.status === undefined ? 200 : result.status;
|
|
365
363
|
if (result.body === undefined || result.body === null) {
|
|
366
364
|
sendEmpty(res, result.status === undefined ? 204 : status);
|
|
367
365
|
return;
|
|
@@ -373,9 +371,11 @@ function sendResult(res, result) {
|
|
|
373
371
|
module.exports = {
|
|
374
372
|
isReply,
|
|
375
373
|
getMediaType,
|
|
374
|
+
isJsonType,
|
|
376
375
|
sendHtml,
|
|
377
376
|
sendText,
|
|
378
377
|
sendJson,
|
|
378
|
+
sendJsonBody,
|
|
379
379
|
sendEmpty,
|
|
380
380
|
sendXml,
|
|
381
381
|
sendFile,
|
package/lib/response.js
CHANGED
|
@@ -2,12 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
const { sendJson, sendResult } = require('./reply');
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* HTML 文档开头标记正则(忽略前导空白,用于判定字符串是否为页面渲染结果)
|
|
7
|
-
* @type {RegExp}
|
|
8
|
-
*/
|
|
9
|
-
const HTML_PATTERN = /^\s*<(!doctype\s+html|html|head|body)(\s|>)/i;
|
|
10
|
-
|
|
11
5
|
/**
|
|
12
6
|
* Response —— 响应渲染中间件(mm_response)。
|
|
13
7
|
*
|
|
@@ -26,6 +20,12 @@ const HTML_PATTERN = /^\s*<(!doctype\s+html|html|head|body)(\s|>)/i;
|
|
|
26
20
|
* await web.start();
|
|
27
21
|
*/
|
|
28
22
|
class Response {
|
|
23
|
+
/**
|
|
24
|
+
* HTML 文档开头标记正则(忽略前导空白,用于判定字符串是否为页面渲染结果)
|
|
25
|
+
* @type {RegExp}
|
|
26
|
+
*/
|
|
27
|
+
static #html_pattern = /^\s*<(!doctype\s+html|html|head|body)(\s|>)/i;
|
|
28
|
+
|
|
29
29
|
/**
|
|
30
30
|
* 已挂载的 WebServer 实例,未挂载时为 null
|
|
31
31
|
* @type {Object|null}
|
|
@@ -75,13 +75,13 @@ class Response {
|
|
|
75
75
|
* @returns {void}
|
|
76
76
|
*/
|
|
77
77
|
_respond(ctx, result, options) {
|
|
78
|
-
|
|
78
|
+
let res = ctx && ctx.res;
|
|
79
79
|
if (!res || ctx.handled === true || res.writableEnded) return;
|
|
80
80
|
if (res.headersSent) {
|
|
81
81
|
res.end();
|
|
82
82
|
return;
|
|
83
83
|
}
|
|
84
|
-
|
|
84
|
+
let error = ctx.error || (options && options.error);
|
|
85
85
|
if (error) {
|
|
86
86
|
sendJson(res, 500, { error: error.message || 'internal error' });
|
|
87
87
|
return;
|
|
@@ -105,7 +105,7 @@ class Response {
|
|
|
105
105
|
* @returns {boolean} 是否为 HTML 文档字符串
|
|
106
106
|
*/
|
|
107
107
|
_isHtml(value) {
|
|
108
|
-
return typeof value === 'string' &&
|
|
108
|
+
return typeof value === 'string' && Response.#html_pattern.test(value);
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
|
package/package.json
CHANGED
|
@@ -1,41 +1,46 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
|
|
2
|
+
"name": "mm_response",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "Web 响应渲染中间件:为 mm_web_server 提供可挂载的响应写出器与响应工具函数。",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"index.js",
|
|
8
|
+
"lib",
|
|
9
|
+
"example",
|
|
10
|
+
"tool",
|
|
11
|
+
"data"
|
|
12
|
+
],
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=22.5.0"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"start": "node example/demo.js",
|
|
18
|
+
"demo": "node example/demo.js",
|
|
19
|
+
"test": "node --test \"tests/*.test.js\"",
|
|
20
|
+
"coverage": "node --test --experimental-test-coverage --test-coverage-include=\"index.js\" --test-coverage-include=\"lib/**\" --test-coverage-lines=100 --test-coverage-branches=100 --test-coverage-functions=100 \"tests/*.test.js\""
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_response.git"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"mm_mod",
|
|
28
|
+
"mm_event_api",
|
|
29
|
+
"web",
|
|
30
|
+
"response",
|
|
31
|
+
"html",
|
|
32
|
+
"content-type"
|
|
33
|
+
],
|
|
34
|
+
"layer": "action",
|
|
35
|
+
"group": "base",
|
|
36
|
+
"author": "邱文武",
|
|
37
|
+
"license": "ISC",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://gitee.com/qiuwenwu91/mm_response/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://gitee.com/qiuwenwu91/mm_response#readme",
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"mm_consts": "^1.1.3",
|
|
44
|
+
"mm_files": "^1.4.2"
|
|
45
|
+
}
|
|
41
46
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* reply —— 响应内容协商工具
|
|
4
|
+
* 直接复用模块 lib/reply 的纯函数,不复制媒体类型映射表。
|
|
5
|
+
*/
|
|
6
|
+
const { getMediaType, isJsonType, isReply } = require('../../index.js');
|
|
7
|
+
|
|
8
|
+
async function main(params, ctx, options) {
|
|
9
|
+
void options;
|
|
10
|
+
const p = params || {};
|
|
11
|
+
const action = p.action || 'media_type';
|
|
12
|
+
const done = (data) => { ctx.output = data; return data; };
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
if (action === 'media_type') {
|
|
16
|
+
if (!p.file_path) throw new TypeError('file_path 不能为空');
|
|
17
|
+
const media_type = getMediaType(p.file_path);
|
|
18
|
+
return done({ code: 0, msg: '成功', media_type });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (action === 'json_type') {
|
|
22
|
+
if (!p.type) throw new TypeError('type 不能为空');
|
|
23
|
+
const is_json = isJsonType(p.type);
|
|
24
|
+
return done({ code: 0, msg: is_json ? 'JSON 类型' : '非 JSON 类型', is_json });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (action === 'is_reply') {
|
|
28
|
+
const yes = isReply(p.value);
|
|
29
|
+
return done({ code: 0, msg: yes ? '标准 reply 结构' : '非标准 reply 结构', is_reply: yes });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
33
|
+
} catch (error) {
|
|
34
|
+
return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = { main };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "reply",
|
|
3
|
+
"title": "响应内容协商",
|
|
4
|
+
"description": "基于 mm_response 的纯函数能力:按文件名推导 Content-Type、判断内容类型是否 JSON、判断对象是否已是标准 reply 结构。适用场景:写响应前先确认媒体类型、校验回包结构、区分文本与二进制回包。",
|
|
5
|
+
"category": "HTTP 响应",
|
|
6
|
+
"group": "http",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"properties": {
|
|
12
|
+
"action": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "操作类型: media_type(按文件名取 Content-Type)/json_type(判断类型是否 JSON)/is_reply(判断是否标准 reply 结构)",
|
|
15
|
+
"enum": [
|
|
16
|
+
"media_type",
|
|
17
|
+
"json_type",
|
|
18
|
+
"is_reply"
|
|
19
|
+
],
|
|
20
|
+
"default": "media_type"
|
|
21
|
+
},
|
|
22
|
+
"file_path": {
|
|
23
|
+
"type": "string",
|
|
24
|
+
"description": "文件路径或扩展名(media_type 必填),如 report.mp4 或 png"
|
|
25
|
+
},
|
|
26
|
+
"type": {
|
|
27
|
+
"type": "string",
|
|
28
|
+
"description": "内容类型标记(json_type 必填),如 application/json 或 json"
|
|
29
|
+
},
|
|
30
|
+
"value": {
|
|
31
|
+
"description": "待判断的对象(is_reply 必填)"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"required": [
|
|
35
|
+
"action"
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
"returns": {
|
|
39
|
+
"type": "object",
|
|
40
|
+
"properties": {
|
|
41
|
+
"code": {
|
|
42
|
+
"type": "number",
|
|
43
|
+
"description": "0 表示成功"
|
|
44
|
+
},
|
|
45
|
+
"msg": {
|
|
46
|
+
"type": "string",
|
|
47
|
+
"description": "结果描述"
|
|
48
|
+
},
|
|
49
|
+
"media_type": {
|
|
50
|
+
"type": "string",
|
|
51
|
+
"description": "推导出的 Content-Type"
|
|
52
|
+
},
|
|
53
|
+
"is_json": {
|
|
54
|
+
"type": "boolean",
|
|
55
|
+
"description": "是否为 JSON 内容类型"
|
|
56
|
+
},
|
|
57
|
+
"is_reply": {
|
|
58
|
+
"type": "boolean",
|
|
59
|
+
"description": "是否标准 reply 结构"
|
|
60
|
+
},
|
|
61
|
+
"error": {
|
|
62
|
+
"type": "object",
|
|
63
|
+
"description": "异常对象 { name, message }"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
"handler": "manager"
|
|
68
|
+
}
|