mm_response 1.0.1 → 1.1.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 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
- - 导出 `isReply` / `getMediaType` / `sendHtml` / `sendText` / `sendJson` / `sendEmpty` / `sendXml` / `sendFile` / `sendMedia` / `sendImage` / `sendVideo` / `sendAudio` / `sendBody` / `sendResult`,可单独复用
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` |
package/lib/reply.js CHANGED
@@ -45,6 +45,9 @@ const DEFAULT_VIDEO_TYPE = 'video/mp4';
45
45
  // 缺省音频内容类型
46
46
  const DEFAULT_AUDIO_TYPE = 'audio/mpeg';
47
47
 
48
+ // 缺省 JSON 内容类型
49
+ const JSON_TYPE = 'application/json; charset=utf-8';
50
+
48
51
  // 缺省 XML 内容类型
49
52
  const XML_TYPE = 'application/xml; charset=utf-8';
50
53
 
@@ -122,6 +125,16 @@ function isXmlType(type) {
122
125
  return XML_MIME_TYPES.includes(type);
123
126
  }
124
127
 
128
+ /**
129
+ * 判断是否为 JSON 内容类型标记(json 或含 json 的 MIME 形式)
130
+ * @param {*} type 内容类型标记
131
+ * @returns {boolean} 是否为 JSON 内容类型
132
+ */
133
+ function isJsonType(type) {
134
+ if (typeof type !== 'string') return false;
135
+ return type.includes('json');
136
+ }
137
+
125
138
  /**
126
139
  * 发送 HTML 响应
127
140
  * @param {Object} res 响应对象
@@ -158,7 +171,21 @@ function sendText(res, status, text) {
158
171
  function sendJson(res, status, payload) {
159
172
  if (res.writableEnded) return;
160
173
  const text = JSON.stringify(payload);
161
- res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' });
174
+ res.writeHead(status, { 'content-type': JSON_TYPE });
175
+ res.end(text);
176
+ }
177
+
178
+ /**
179
+ * 发送 JSON 响应体:字符串视为已序列化的 JSON 文本原样写出,其余按 JSON 序列化
180
+ * @param {Object} res 响应对象
181
+ * @param {number} status 状态码
182
+ * @param {*} body 响应体
183
+ * @returns {void}
184
+ */
185
+ function sendJsonBody(res, status, body) {
186
+ if (res.writableEnded) return;
187
+ const text = typeof body === 'string' ? body : JSON.stringify(body);
188
+ res.writeHead(status, { 'content-type': JSON_TYPE });
162
189
  res.end(text);
163
190
  }
164
191
 
@@ -297,11 +324,15 @@ function sendAudio(res, status, body, content_type) {
297
324
  * @param {Object} res 响应对象
298
325
  * @param {number} status 状态码
299
326
  * @param {*} body 响应体
300
- * @param {string} [type] 内容类型标记(html / xml / image / video / audio,或对应 MIME 形式的内容类型),缺省按 body 类型推断
327
+ * @param {string} [type] 内容类型标记(json / html / xml / image / video / audio,或对应 MIME 形式的内容类型),缺省按 body 类型推断
301
328
  * @param {string} [content_type] 内容类型,type 为 image / video / audio 时优先使用
302
329
  * @returns {void}
303
330
  */
304
331
  function sendBody(res, status, body, type, content_type) {
332
+ if (isJsonType(type)) {
333
+ sendJsonBody(res, status, body);
334
+ return;
335
+ }
305
336
  if (type === 'html') {
306
337
  sendHtml(res, status, body);
307
338
  return;
@@ -373,9 +404,11 @@ function sendResult(res, result) {
373
404
  module.exports = {
374
405
  isReply,
375
406
  getMediaType,
407
+ isJsonType,
376
408
  sendHtml,
377
409
  sendText,
378
410
  sendJson,
411
+ sendJsonBody,
379
412
  sendEmpty,
380
413
  sendXml,
381
414
  sendFile,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_response",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Web 响应渲染中间件:为 mm_web_server 提供可挂载的响应写出器与响应工具函数。",
5
5
  "main": "index.js",
6
6
  "files": [