* 读取流响应体为文本 * @param {*} stream - 响应流 * @returns {Promise } 文本结果
(stream)
| 179 | * @param {*} stream - 响应流 |
| 180 | * @returns {Promise<string>} 文本结果 |
| 181 | */ |
| 182 | function readStreamBody(stream) { |
| 183 | return new Promise((resolve, reject) => { |
| 184 | if (!stream || typeof stream.on !== 'function') { |
| 185 | resolve('') |
| 186 | return |
| 187 | } |
| 188 | |
| 189 | const chunks = [] |
| 190 | stream.on('data', (chunk) => { |
| 191 | chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk))) |
| 192 | }) |
| 193 | stream.on('end', () => { |
| 194 | resolve(Buffer.concat(chunks).toString('utf8')) |
| 195 | }) |
| 196 | stream.on('error', reject) |
| 197 | }) |
| 198 | } |
| 199 | |
| 200 | /** |
no outgoing calls
no test coverage detected