MCPcopy Create free account
hub / github.com/Script-Hub-Org/Script-Hub / fetchContent

Function fetchContent

SurgeModuleTool_macOS.js:144–175  ·  view source on GitHub ↗
(url, options = {}, timeout = 5000)

Source from the content-addressed store, hash-verified

142 })
143
144function 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}
176function convertToValidFileName(str) {
177 // 替换非法字符为下划线
178 const invalidCharsRegex = /[\/:*?"<>|]/g

Callers 1

Calls 1

getMethod · 0.45

Tested by

no test coverage detected