* * @param {*} defaultOptions * @param {*} preScript * @param {*} afterScript * @param {*} commonContext 负责传递一些业务信息,crossRequest 不关注具体传什么,只负责当中间人
(defaultOptions, preScript, afterScript, commonContext = {})
| 246 | * @param {*} commonContext 负责传递一些业务信息,crossRequest 不关注具体传什么,只负责当中间人 |
| 247 | */ |
| 248 | async function crossRequest(defaultOptions, preScript, afterScript, commonContext = {}) { |
| 249 | let options = Object.assign({}, defaultOptions); |
| 250 | const taskId = options.taskId || Math.random() + ''; |
| 251 | let urlObj = URL.parse(options.url, true), |
| 252 | query = {}; |
| 253 | query = Object.assign(query, urlObj.query); |
| 254 | let context = { |
| 255 | isNode, |
| 256 | get href() { |
| 257 | return urlObj.href; |
| 258 | }, |
| 259 | set href(val) { |
| 260 | throw new Error('context.href 不能被赋值'); |
| 261 | }, |
| 262 | get hostname() { |
| 263 | return urlObj.hostname; |
| 264 | }, |
| 265 | set hostname(val) { |
| 266 | throw new Error('context.hostname 不能被赋值'); |
| 267 | }, |
| 268 | |
| 269 | get caseId() { |
| 270 | return options.caseId; |
| 271 | }, |
| 272 | |
| 273 | set caseId(val) { |
| 274 | throw new Error('context.caseId 不能被赋值'); |
| 275 | }, |
| 276 | |
| 277 | method: options.method, |
| 278 | pathname: urlObj.pathname, |
| 279 | query: query, |
| 280 | requestHeader: options.headers || {}, |
| 281 | requestBody: options.data, |
| 282 | promise: false, |
| 283 | storage: await getStorage(taskId) |
| 284 | }; |
| 285 | |
| 286 | Object.assign(context, commonContext) |
| 287 | |
| 288 | context.utils = Object.freeze({ |
| 289 | _: _, |
| 290 | CryptoJS: CryptoJS, |
| 291 | jsrsasign: jsrsasign, |
| 292 | base64: utils.base64, |
| 293 | md5: utils.md5, |
| 294 | sha1: utils.sha1, |
| 295 | sha224: utils.sha224, |
| 296 | sha256: utils.sha256, |
| 297 | sha384: utils.sha384, |
| 298 | sha512: utils.sha512, |
| 299 | unbase64: utils.unbase64, |
| 300 | axios: axios |
| 301 | }); |
| 302 | |
| 303 | if (preScript) { |
| 304 | context = await sandbox(context, preScript); |
| 305 | defaultOptions.url = options.url = URL.format({ |
no test coverage detected