MCPcopy Create free account
hub / github.com/violentmonkey/violentmonkey / _request

Function _request

src/background/sync/base.js:355–385  ·  view source on GitHub ↗
(options)

Source from the content-addressed store, hash-verified

353 },
354 _requestDelay: 0,
355 async _request(options) {
356 options = Object.assign({}, NO_CACHE, options);
357 options.headers = Object.assign({}, this.headers, options.headers);
358 let { url } = options;
359 if (url.startsWith('/')) url = (options.prefix ?? this.urlPrefix) + url;
360 let delay = this._requestDelay;
361 let attempts = 5;
362 while (attempts > 0) {
363 attempts -= 1;
364 if (delay >= 200) await makePause(delay);
365 try {
366 const res = await request(url, options);
367 this._requestDelay >>= 1;
368 return res.data;
369 } catch (err) {
370 if (err?.status !== 429 || attempts <= 0) throw err;
371 const retryAfter = err.headers?.get('retry-after');
372 const serverDelay =
373 retryAfter &&
374 (isNaN(+retryAfter)
375 ? new Date(retryAfter).getTime() - Date.now()
376 : +retryAfter * 1000);
377 if (serverDelay) {
378 delay = serverDelay;
379 } else {
380 delay = Math.max(1000, delay * 2);
381 this._requestDelay = delay;
382 }
383 }
384 }
385 },
386 _requestProcessing: false,
387 async _handleRequests() {
388 if (this._requestProcessing) return;

Callers

nothing calls this directly

Calls 3

makePauseFunction · 0.90
requestFunction · 0.90
getMethod · 0.45

Tested by

no test coverage detected