-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #44 from younesaassila/v1.6.0
🔖 Release version 1.6.0
- Loading branch information
Showing
31 changed files
with
856 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { WebRequest } from "webextension-polyfill"; | ||
import { TTV_LOL_API_URL_REGEX } from "../../common/ts/regexes"; | ||
import store from "../../store"; | ||
import type { StreamStatus } from "../../types"; | ||
|
||
export default function onApiHeadersReceived( | ||
details: WebRequest.OnHeadersReceivedDetailsType | ||
): WebRequest.BlockingResponseOrPromise { | ||
const streamId = getStreamIdFromUrl(details.url); | ||
if (!streamId) return {}; | ||
|
||
const isServerError = 500 <= details.statusCode && details.statusCode < 600; | ||
if (isServerError) { | ||
// Add error to stream status. | ||
const status = getStreamStatusFromStreamId(streamId); | ||
const errors = status.errors || []; | ||
errors.push({ | ||
timestamp: Date.now(), | ||
status: details.statusCode, | ||
}); | ||
|
||
store.state.streamStatuses[streamId] = { | ||
...status, | ||
errors: errors, | ||
proxyCountry: undefined, // Reset proxy country on error. | ||
}; | ||
console.log(`${streamId}: ${status.errors.length + 1} errors`); | ||
console.log(`${streamId}: Redirect canceled (Error ${details.statusCode})`); | ||
|
||
return { | ||
cancel: true, // This forces Twitch to retry the request (up to 2 times). | ||
}; | ||
} else { | ||
// Clear errors if server is not returning 5xx. | ||
const status = getStreamStatusFromStreamId(streamId); | ||
store.state.streamStatuses[streamId] = { | ||
...status, | ||
errors: [], | ||
}; | ||
|
||
return {}; | ||
} | ||
} | ||
|
||
function getStreamIdFromUrl(url: string): string | undefined { | ||
const match = TTV_LOL_API_URL_REGEX.exec(url); | ||
if (!match) return; | ||
const [, streamId] = match; | ||
return streamId; | ||
} | ||
|
||
function getStreamStatusFromStreamId(streamId: string): StreamStatus { | ||
const status = store.state.streamStatuses[streamId]; | ||
const defaultStatus = { | ||
redirected: true, | ||
reason: "", | ||
errors: [], | ||
} as StreamStatus; | ||
|
||
return status || defaultStatus; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { WebRequest } from "webextension-polyfill"; | ||
import filterResponseDataWrapper from "../../common/ts/filterResponseDataWrapper"; | ||
import { TTV_LOL_API_URL_REGEX } from "../../common/ts/regexes"; | ||
import store from "../../store"; | ||
|
||
const PROXY_COUNTRY_REGEX = /USER-COUNTRY="([A-Z]+)"/i; | ||
|
||
export default function onBeforeSendApiHeaders( | ||
details: WebRequest.OnBeforeSendHeadersDetailsType | ||
): WebRequest.BlockingResponseOrPromise { | ||
const requestHeaders = details.requestHeaders || []; | ||
requestHeaders.push({ | ||
name: "X-Donate-To", | ||
value: "https://ttv.lol/donate", | ||
}); | ||
|
||
const response = { | ||
requestHeaders: requestHeaders, | ||
} as WebRequest.BlockingResponse; | ||
|
||
filterResponseDataWrapper(details, text => { | ||
const streamId = getStreamIdFromUrl(details.url); | ||
const proxyCountry = extractProxyCountryFromManifest(text); | ||
if (!streamId || !proxyCountry) return text; | ||
|
||
setStreamStatusProxyCountry(streamId, proxyCountry); | ||
|
||
return text; | ||
}); | ||
|
||
return response; | ||
} | ||
|
||
function getStreamIdFromUrl(url: string): string | undefined { | ||
const match = TTV_LOL_API_URL_REGEX.exec(url); | ||
if (!match) return; | ||
const [, streamId] = match; | ||
return streamId; | ||
} | ||
|
||
function extractProxyCountryFromManifest(text: string): string | undefined { | ||
const match = PROXY_COUNTRY_REGEX.exec(text); | ||
if (!match) return; | ||
const [, proxyCountry] = match; | ||
return proxyCountry; | ||
} | ||
|
||
function setStreamStatusProxyCountry( | ||
streamId: string, | ||
proxyCountry: string | ||
): void { | ||
const status = store.state.streamStatuses[streamId]; | ||
if (!status) return; | ||
|
||
store.state.streamStatuses[streamId] = { | ||
...status, | ||
proxyCountry: proxyCountry, | ||
}; | ||
} |
Oops, something went wrong.