|
| 1 | +/** |
| 2 | + * Logger Helper |
| 3 | + * |
| 4 | + * 提供安全的日志输出函数,用于在控制台中显示大响应体时进行截断。 |
| 5 | + */ |
| 6 | + |
| 7 | +(function (root) { |
| 8 | + 'use strict'; |
| 9 | + |
| 10 | + var DEFAULT_MAX_BYTES = 10 * 1024; // 10KB |
| 11 | + var DEFAULT_HEAD_CHARS = 512; |
| 12 | + var DEFAULT_TAIL_CHARS = 512; |
| 13 | + |
| 14 | + function formatBytes(bytes) { |
| 15 | + if (bytes < 1024) { |
| 16 | + return bytes + ' B'; |
| 17 | + } |
| 18 | + if (bytes < 1024 * 1024) { |
| 19 | + return (bytes / 1024).toFixed(1) + ' KB'; |
| 20 | + } |
| 21 | + if (bytes < 1024 * 1024 * 1024) { |
| 22 | + return (bytes / (1024 * 1024)).toFixed(1) + ' MB'; |
| 23 | + } |
| 24 | + return (bytes / (1024 * 1024 * 1024)).toFixed(1) + ' GB'; |
| 25 | + } |
| 26 | + |
| 27 | + function toStringValue(value) { |
| 28 | + if (value == null) { |
| 29 | + return ''; |
| 30 | + } |
| 31 | + |
| 32 | + if (typeof value === 'string') { |
| 33 | + return value; |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + return JSON.stringify(value); |
| 38 | + } catch (e) { |
| 39 | + try { |
| 40 | + return String(value); |
| 41 | + } catch (stringError) { |
| 42 | + return '[无法序列化的响应体]'; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + function countBytes(text) { |
| 48 | + if (typeof TextEncoder !== 'undefined') { |
| 49 | + return new TextEncoder().encode(text).length; |
| 50 | + } |
| 51 | + // 退化处理:按字符长度估算 |
| 52 | + return text.length * 2; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * 构造安全的日志输出 |
| 57 | + * @param {*} originalBody 原始响应体 |
| 58 | + * @param {Object} [options] |
| 59 | + * @param {number} [options.maxBytes] 最大允许的字节数(默认 10KB) |
| 60 | + * @param {number} [options.headChars] 截断时保留的头部字符数 |
| 61 | + * @param {number} [options.tailChars] 截断时保留的尾部字符数 |
| 62 | + * @returns {*} 原始响应体或截断后的摘要对象 |
| 63 | + */ |
| 64 | + function safeLogResponse(originalBody, options) { |
| 65 | + var opts = options || {}; |
| 66 | + var maxBytes = typeof opts.maxBytes === 'number' ? opts.maxBytes : DEFAULT_MAX_BYTES; |
| 67 | + var headChars = typeof opts.headChars === 'number' ? opts.headChars : DEFAULT_HEAD_CHARS; |
| 68 | + var tailChars = typeof opts.tailChars === 'number' ? opts.tailChars : DEFAULT_TAIL_CHARS; |
| 69 | + |
| 70 | + var textValue = toStringValue(originalBody); |
| 71 | + var bytes = countBytes(textValue); |
| 72 | + |
| 73 | + if (bytes <= maxBytes) { |
| 74 | + return originalBody; |
| 75 | + } |
| 76 | + |
| 77 | + var previewHead = textValue.slice(0, headChars); |
| 78 | + var previewTail = tailChars > 0 ? textValue.slice(-tailChars) : ''; |
| 79 | + |
| 80 | + return { |
| 81 | + truncated: true, |
| 82 | + size: formatBytes(bytes), |
| 83 | + head: previewHead, |
| 84 | + tail: previewTail, |
| 85 | + hint: '响应体过大,已截断显示(查看 head/tail 字段获取片段)' |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + root.CrossRequestHelpers = root.CrossRequestHelpers || {}; |
| 90 | + root.CrossRequestHelpers.safeLogResponse = safeLogResponse; |
| 91 | + |
| 92 | + if (typeof module !== 'undefined' && module.exports) { |
| 93 | + module.exports = { |
| 94 | + safeLogResponse: safeLogResponse, |
| 95 | + __private: { |
| 96 | + formatBytes: formatBytes, |
| 97 | + toStringValue: toStringValue |
| 98 | + } |
| 99 | + }; |
| 100 | + } |
| 101 | +})(typeof window !== 'undefined' ? window : (typeof self !== 'undefined' ? self : globalThis)); |
0 commit comments