improve 429 prevention (async limit), add ua

pull/53/head
lartsch 2022-12-29 10:10:59 -05:00
rodzic 95ec910c99
commit f23e7be502
4 zmienionych plików z 35 dodań i 11 usunięć

Wyświetl plik

@ -53,14 +53,19 @@ async function generalRequest(data) {
controller.abort()
}, timeout)
if (data[3]) {
// json body provided, post as body to target
data[2]["User-Agent"] = "FediAct Service"
data[2]["Content-Type"] = "application/json"
var res = await fetch(data[1], {
method: data[0],
signal: controller.signal,
// if json body is provided, there is also header data
headers: data[2],
body: JSON.stringify(data[3])
})
} else if (data[2]) {
// header data provided
data[2]["User-Agent"] = "FediAct Service"
var res = await fetch(data[1], {
method: data[0],
signal: controller.signal,
@ -69,7 +74,8 @@ async function generalRequest(data) {
} else {
var res = await fetch(data[1], {
method: data[0],
signal: controller.signal
signal: controller.signal,
headers: {"User-Agent": "FediAct Service"}
})
}
clearTimeout(timeoutId)

Wyświetl plik

@ -1 +1 @@
var browser,chrome,c;const a=!1,n="[FediAct]",t=1,i="/api/v1/mutes",o="/api/v1/blocks",s="/api/v1/domain_blocks",r=15e3,d=/"access_token":".*?",/gm,u={fediact_homeinstance:null,fediact_token:null};function h(t){a&&console.log(n+" "+t)}async function f(i){return new Promise(async function(e){try{const n=new AbortController;var t=setTimeout(()=>{h("Timed out"),n.abort()},r),a=await fetch(i,{method:"HEAD",signal:n.signal});clearTimeout(t),a.redirected?e(a.url):e(!1)}catch(t){h(t),e(!1)}})}async function l(c){return new Promise(async function(e){try{const i=new AbortController;var t,a,n=setTimeout(()=>{h("Timed out"),i.abort()},r);t=c[3]?(c[2]["Content-Type"]="application/json",await fetch(c[1],{method:c[0],signal:i.signal,headers:c[2],body:JSON.stringify(c[3])})):c[2]?await fetch(c[1],{method:c[0],signal:i.signal,headers:c[2]}):await fetch(c[1],{method:c[0],signal:i.signal}),clearTimeout(n),200<=t.status&&t.status<300&&(a=t.headers.get("content-type"))&&-1!==a.indexOf("application/json")?e(await t.text()):e(!1)}catch(t){h(t),e(!1)}})}async function m(){return new Promise(async function(e){var t="https://"+c.fediact_homeinstance;try{var a=await(await fetch(t)).text()}catch(t){return h(t),void e(!1)}if(a){t=a.match(d);if(t){var a=t[0].search(/"access_token":"/),n=t[0].search(/",/);if(-1<a&&-1<n){t=t[0].substring(a+=16,n);if(16<t.length)return c.fediact_token=t,void e(!0)}}}c.fediact_token=null,h("Token could not be found."),e(!1)})}function g(){return new Promise(async function(t){try{[c.fediact_mutes,c.fediact_blocks,c.fediact_domainblocks]=[[],[],[]];var[e,a,n]=await Promise.all([fetch("https://"+c.fediact_homeinstance+i,{headers:{Authorization:"Bearer "+c.fediact_token}}).then(t=>t.json()),fetch("https://"+c.fediact_homeinstance+o,{headers:{Authorization:"Bearer "+c.fediact_token}}).then(t=>t.json()),fetch("https://"+c.fediact_homeinstance+s,{headers:{Authorization:"Bearer "+c.fediact_token}}).then(t=>t.json())]);e.length&&c.fediact_mutes.push(...e.map(t=>t.acct)),a.length&&c.fediact_blocks.push(...a.map(t=>t.acct)),n.length&&(c.fediact_domainblocks=n),t(!0)}catch{t(!1)}})}async function y(n,i){return new Promise(async function(t){var a=!1;try{if((c=await(browser||chrome).storage.local.get(u)).fediact_homeinstance){if(n||i){!n&&c.fediact_token||await m(),i&&await g();try{await(browser||chrome).storage.local.set(c),a=!0}catch{h(e)}}}else h("Home instance not set")}catch(t){h(t)}t(a)})}async function p(){chrome.tabs.query({},async function(t){for(var e=0;e<t.length;++e)try{chrome.tabs.sendMessage(t[e].id,{updatedfedisettings:!0})}catch(t){continue}})}chrome.runtime.onInstalled.addListener(function(){y(!0,!0)}),chrome.alarms.create("refresh",{periodInMinutes:t}),chrome.alarms.onAlarm.addListener(function(){y(!0,!0)}),chrome.runtime.onMessage.addListener((t,n,e)=>t.externaltoot?(f(t.externaltoot).then(e),!0):t.requestdata?(l(t.requestdata).then(e),!0):t.updatedsettings?(y(!0,!0).then(p),!0):t.updatemutedblocked?(y(!1,!0).then(e),!0):void(t.running&&chrome.tabs.onUpdated.addListener(async function(t,e,a){if(t===n.tab.id&&e.url)try{await chrome.tabs.sendMessage(t,{urlchanged:e.url})}catch(t){h(t)}})));
var browser,chrome,c;const a=!1,n="[FediAct]",t=1,i="/api/v1/mutes",s="/api/v1/blocks",o="/api/v1/domain_blocks",r=15e3,d=/"access_token":".*?",/gm,u={fediact_homeinstance:null,fediact_token:null};function h(t){a&&console.log(n+" "+t)}async function f(i){return new Promise(async function(e){try{const n=new AbortController;var t=setTimeout(()=>{h("Timed out"),n.abort()},r),a=await fetch(i,{method:"HEAD",signal:n.signal});clearTimeout(t),a.redirected?e(a.url):e(!1)}catch(t){h(t),e(!1)}})}async function l(c){return new Promise(async function(e){try{const i=new AbortController;var t,a,n=setTimeout(()=>{h("Timed out"),i.abort()},r);t=c[3]?(c[2]["User-Agent"]="FediAct Service",c[2]["Content-Type"]="application/json",await fetch(c[1],{method:c[0],signal:i.signal,headers:c[2],body:JSON.stringify(c[3])})):c[2]?(c[2]["User-Agent"]="FediAct Service",await fetch(c[1],{method:c[0],signal:i.signal,headers:c[2]})):await fetch(c[1],{method:c[0],signal:i.signal,headers:{"User-Agent":"FediAct Service"}}),clearTimeout(n),200<=t.status&&t.status<300&&(a=t.headers.get("content-type"))&&-1!==a.indexOf("application/json")?e(await t.text()):e(!1)}catch(t){h(t),e(!1)}})}async function m(){return new Promise(async function(e){var t="https://"+c.fediact_homeinstance;try{var a=await(await fetch(t)).text()}catch(t){return h(t),void e(!1)}if(a){t=a.match(d);if(t){var a=t[0].search(/"access_token":"/),n=t[0].search(/",/);if(-1<a&&-1<n){t=t[0].substring(a+=16,n);if(16<t.length)return c.fediact_token=t,void e(!0)}}}c.fediact_token=null,h("Token could not be found."),e(!1)})}function g(){return new Promise(async function(t){try{[c.fediact_mutes,c.fediact_blocks,c.fediact_domainblocks]=[[],[],[]];var[e,a,n]=await Promise.all([fetch("https://"+c.fediact_homeinstance+i,{headers:{Authorization:"Bearer "+c.fediact_token}}).then(t=>t.json()),fetch("https://"+c.fediact_homeinstance+s,{headers:{Authorization:"Bearer "+c.fediact_token}}).then(t=>t.json()),fetch("https://"+c.fediact_homeinstance+o,{headers:{Authorization:"Bearer "+c.fediact_token}}).then(t=>t.json())]);e.length&&c.fediact_mutes.push(...e.map(t=>t.acct)),a.length&&c.fediact_blocks.push(...a.map(t=>t.acct)),n.length&&(c.fediact_domainblocks=n),t(!0)}catch{t(!1)}})}async function y(n,i){return new Promise(async function(t){var a=!1;try{if((c=await(browser||chrome).storage.local.get(u)).fediact_homeinstance){if(n||i){!n&&c.fediact_token||await m(),i&&await g();try{await(browser||chrome).storage.local.set(c),a=!0}catch{h(e)}}}else h("Home instance not set")}catch(t){h(t)}t(a)})}async function p(){chrome.tabs.query({},async function(t){for(var e=0;e<t.length;++e)try{chrome.tabs.sendMessage(t[e].id,{updatedfedisettings:!0})}catch(t){continue}})}chrome.runtime.onInstalled.addListener(function(){y(!0,!0)}),chrome.alarms.create("refresh",{periodInMinutes:t}),chrome.alarms.onAlarm.addListener(function(){y(!0,!0)}),chrome.runtime.onMessage.addListener((t,n,e)=>t.externaltoot?(f(t.externaltoot).then(e),!0):t.requestdata?(l(t.requestdata).then(e),!0):t.updatedsettings?(y(!0,!0).then(p),!0):t.updatemutedblocked?(y(!1,!0).then(e),!0):void(t.running&&chrome.tabs.onUpdated.addListener(async function(t,e,a){if(t===n.tab.id&&e.url)try{await chrome.tabs.sendMessage(t,{urlchanged:e.url})}catch(t){h(t)}})));

Wyświetl plik

@ -17,9 +17,10 @@ const mutesApi = "/api/v1/mutes"
const blocksApi = "/api/v1/blocks"
const domainBlocksApi = "/api/v1/domain_blocks"
const pollsApi = "/api/v1/polls"
const apiDelay = 650
const apiDelay = 600
const maxTootCache = 200
const modalHtml = '<div class="fediactmodal"><div class="fediactmodalinner"><ul class="fediactmodallist"></ul></div></div>'
const maxAsyncRequests = 10
// settings keys with defauls
var settings = {}
@ -124,13 +125,27 @@ var getUrlParameter = function getUrlParameter(sParam) {
return false
}
const asyncLimit = (fn, n) => {
let pendingPromises = []
return async function (...args) {
while (pendingPromises.length >= n) {
await Promise.race(pendingPromises).catch(() => {})
}
const p = fn.apply(this, args)
pendingPromises.push(p)
await p.catch(() => {})
pendingPromises = pendingPromises.filter(pending => pending !== p)
return p
}
}
// promisified xhr for api calls
function makeRequest(method, url, extraheaders, jsonbody) {
return new Promise(async function(resolve) {
// get current time
var currenttime = Date.now()
// try to prevent error 429 too many request by delaying home instance requests
if (~url.indexOf(settings.fediact_homeinstance) && settings.fediact_enabledelay) {
// get current time
var currenttime = Date.now()
// get difference of current time and time of last request
var difference = currenttime - tmpSettings.lasthomerequest
// if difference is smaller than our set api delay value...
@ -162,6 +177,9 @@ function makeRequest(method, url, extraheaders, jsonbody) {
})
}
// wrap so there are never more than 10 concurrent requests
const requestAsyncLimited = asyncLimit(makeRequest, maxAsyncRequests)
// Escape characters used for regex
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
@ -283,7 +301,7 @@ async function executeAction(data, action, polldata) {
return
}
if (requestUrl) {
var response = await makeRequest(method, requestUrl, tmpSettings.tokenheader, jsonbody)
var response = await requestAsyncLimited(method, requestUrl, tmpSettings.tokenheader, jsonbody)
if (response) {
// convert to json object
response = JSON.parse(response)
@ -307,7 +325,7 @@ async function isFollowingHomeInstance(ids) {
requestUrl += "id[]=" + id.toString() + "&"
}
// make the request
var responseFollowing = await makeRequest("GET", requestUrl, tmpSettings.tokenheader, null)
var responseFollowing = await requestAsyncLimited("GET", requestUrl, tmpSettings.tokenheader, null)
// fill response array according to id amount with false
const follows = Array(ids.length).fill(false)
// parse the response
@ -379,7 +397,7 @@ function checkAllMutedBlocked(handle) {
// Return the user id on the users home instance
async function resolveHandleToHome(handle) {
var requestUrl = 'https://' + settings.fediact_homeinstance + accountsApi + "/search?q=" + handle + "&resolve=true&limit=1&exclude_unreviewed=false"
var searchResponse = await makeRequest("GET", requestUrl, tmpSettings.tokenheader, null)
var searchResponse = await requestAsyncLimited("GET", requestUrl, tmpSettings.tokenheader, null)
if (searchResponse) {
searchResponse = JSON.parse(searchResponse)
if (searchResponse[0].id) {
@ -393,7 +411,7 @@ async function resolveHandleToHome(handle) {
// resolve a toot to the users home instance
async function resolveTootToHome(searchstring) {
var requestUrl = 'https://' + settings.fediact_homeinstance + searchApi + "/?q=" + searchstring + "&resolve=true&limit=1&exclude_unreviewed=false"
var response = await makeRequest("GET", requestUrl, tmpSettings.tokenheader, null)
var response = await requestAsyncLimited("GET", requestUrl, tmpSettings.tokenheader, null)
if (response) {
response = JSON.parse(response)
// do we have a status as result?
@ -1471,7 +1489,7 @@ async function checkSite() {
// last check - and probably the most accurate to determine if it actually is mastadon
var requestUrl = location.protocol + '//' + location.hostname + instanceApi
// call instance api to confirm its mastodon and get normalized handle uri
var response = await makeRequest("GET", requestUrl, null, null)
var response = await requestAsyncLimited("GET", requestUrl, null, null)
// todo: add basic check for "mastodon" string in response
if (response) {
var uri = JSON.parse(response).uri

2
src/inject.min.js vendored

File diff suppressed because one or more lines are too long