(url, options = {}, timeout = 5000)
| 142 | }) |
| 143 | |
| 144 | function fetchContent(url, options = {}, timeout = 5000) { |
| 145 | return new Promise((resolve, reject) => { |
| 146 | const client = url.startsWith('https://') ? https : http |
| 147 | |
| 148 | const request = client.get(url, options, response => { |
| 149 | if (response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) { |
| 150 | const redirectUrl = new URL(response.headers.location, url) |
| 151 | resolve(fetchContent(redirectUrl.toString(), options, timeout)) |
| 152 | } else { |
| 153 | let data = '' |
| 154 | response.setEncoding('utf8') |
| 155 | response.on('data', chunk => { |
| 156 | data += chunk |
| 157 | }) |
| 158 | response.on('end', () => { |
| 159 | resolve(data) |
| 160 | }) |
| 161 | } |
| 162 | }) |
| 163 | |
| 164 | request.on('error', error => { |
| 165 | reject(error) |
| 166 | }) |
| 167 | |
| 168 | request.on('timeout', () => { |
| 169 | request.abort() |
| 170 | reject(new Error('请求超时')) |
| 171 | }) |
| 172 | |
| 173 | request.setTimeout(timeout) |
| 174 | }) |
| 175 | } |
| 176 | function convertToValidFileName(str) { |
| 177 | // 替换非法字符为下划线 |
| 178 | const invalidCharsRegex = /[\/:*?"<>|]/g |
no test coverage detected