MCPcopy Create free account
hub / github.com/ClydeTime/Surge / fetch

Method fetch

Script/Env.js:179–259  ·  view source on GitHub ↗
(request = {} || "", option = {})

Source from the content-addressed store, hash-verified

177 }
178
179 async fetch(request = {} || "", option = {}) {
180 // 初始化参数
181 switch (request.constructor) {
182 case Object:
183 request = { ...request, ...option }
184 break
185 case String:
186 request = { "url": request, ...option }
187 break
188 }
189 // 自动判断请求方法
190 if (!request.method) {
191 request.method = request.body ?? request.bodyBytes ? "POST" : "GET"
192 }
193 // 移除请求头中的部分参数, 让其自动生成
194 delete request.headers?.Host
195 delete request.headers?.[":authority"]
196 delete request.headers?.['Content-Length']
197 delete request.headers?.['content-length']
198 // 定义请求方法(小写)
199 const method = request.method.toLocaleLowerCase()
200 // 判断平台
201 switch (this.platform()) {
202 case 'Loon':
203 case 'Surge':
204 case 'Stash':
205 case 'Egern':
206 case 'Shadowrocket':
207 default:
208 // 转换请求参数
209 if (request.policy) {
210 if (this.isLoon()) request.node = request.policy
211 if (this.isStash()) this.lodash_set(request, "headers.X-Stash-Selected-Proxy", encodeURI(request.policy))
212 }
213 // 禁止重定向
214 if (request.followRedirect) {
215 if (this.isSurge() || this.isLoon()) request['auto-redirect'] = false
216 if (this.isQuanX()) request.opts ? request.opts.redirection = false : request.opts = { redirection: false }
217 }
218 // 转换请求体
219 if (request.bodyBytes && !request.body) {
220 request.body = request.bodyBytes
221 delete request.bodyBytes
222 }
223 // 发送请求
224 return await new Promise((resolve, reject) => {
225 $httpClient[method](request, (error, response, body) => {
226 if (error) reject(error)
227 else {
228 response.ok = /^2\d\d$/.test(response.status)
229 response.statusCode = response.status
230 if (body) {
231 response.body = body
232 if (request["binary-mode"] == true) response.bodyBytes = body
233 }
234 resolve(response)
235 }
236 })

Callers

nothing calls this directly

Calls 6

platformMethod · 0.45
isLoonMethod · 0.45
isStashMethod · 0.45
lodash_setMethod · 0.45
isSurgeMethod · 0.45
isQuanXMethod · 0.45

Tested by

no test coverage detected