Extension API 1.6 (#2120)

* Non-Extension Index changes for 1.6

* Changelog

* Minor fixes

* Implement extension store

* Test build fix

* Docs

* Simplify fetching manga and chapters

* Use EMPTY JsonObject

* Update docs/Configuring-Suwayomi‐Server.md

Co-authored-by: Constantin Piber <59023762+cpiber@users.noreply.github.com>

* Improve Fetch Extension Store

* Fixes

* Simplify deprecated isNsfw in SourceQuery

* Simplify ContentRating in Source.kt

* Simplify isNsfw in SourceType

* No magic numbers for ContentRating, improves safety for future versions of extension api

* Fix SearchTest

* Lint

* Lint

* Optimize imports and fix unchecked cast warning

* Proper extension store queries

* Optimize import fixes

* Add ContentRatingFilter

* Improve extension store sync

* fix: re-sync (#2121)

* Lint

* Add ExtenionStores to the fetchExtensions result since its possible for the stores to change.

* Use a single version of ContentRating

* Exclude ServerConfig.extensionStores from GraphQL

* Use syncDbToPrefs in ExtensionStoreMutation

* Optimize Imports

* Update server/server-config/src/main/kotlin/suwayomi/tachidesk/server/ServerConfig.kt

Co-authored-by: Constantin Piber <59023762+cpiber@users.noreply.github.com>

* Remove replaceWith and add specific description for GQL APIs

* Include OkHttp ZSTD

* Update to latest Mihon extension lib

* Fix latest Mihon Extension Lib

* Lint

* Optimize imports

* Lint

* Review fixes

* Add a index to extesnion table store url

* Lint

---------

Co-authored-by: Constantin Piber <59023762+cpiber@users.noreply.github.com>
This commit is contained in:
Mitchell Syer
2026-06-27 13:39:28 -04:00
committed by GitHub
parent c8f5d83e9c
commit 2d535b44d8
84 changed files with 2576 additions and 1007 deletions

View File

@@ -0,0 +1,15 @@
package eu.kanade.tachiyomi.network
import okhttp3.Response
/**
* Exception that handles HTTP codes considered not successful by OkHttp.
* Use it to have a standardized error message in the app across the extensions.
*
* @see Response.isSuccessful
* @since tachiyomix 1.6
* @param code [Int] the HTTP status code
*/
class HttpException(
val code: Int,
) : IllegalStateException("HTTP error $code")

View File

@@ -2,26 +2,25 @@ package eu.kanade.tachiyomi.network
import android.content.Context
import app.cash.quickjs.QuickJs
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import eu.kanade.tachiyomi.util.lang.withIOContext
/**
* Util for evaluating JavaScript in sources.
*/
@Suppress("UNUSED", "UNCHECKED_CAST")
class JavaScriptEngine(
@Suppress("UNUSED_PARAMETER") context: Context,
context: Context,
) {
/**
* Evaluate arbitrary JavaScript code and get the result as a primitive type
* (e.g., String, Int).
*
* @since extensions-lib 1.4
* @since tachiyomix 1.4
* @param script JavaScript to execute.
* @return Result of JavaScript code as a primitive type.
*/
@Suppress("UNUSED", "UNCHECKED_CAST")
suspend fun <T> evaluate(script: String): T =
withContext(Dispatchers.IO) {
withIOContext {
QuickJs.create().use {
it.evaluate(script) as T
}

View File

@@ -9,7 +9,6 @@ package eu.kanade.tachiyomi.network
import android.content.Context
import eu.kanade.tachiyomi.network.interceptor.CloudflareInterceptor
import eu.kanade.tachiyomi.network.interceptor.IgnoreGzipInterceptor
import eu.kanade.tachiyomi.network.interceptor.UncaughtExceptionInterceptor
import eu.kanade.tachiyomi.network.interceptor.UserAgentInterceptor
import io.github.oshai.kotlinlogging.KotlinLogging
@@ -22,7 +21,6 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.brotli.BrotliInterceptor
import okhttp3.logging.HttpLoggingInterceptor
import suwayomi.tachidesk.manga.impl.util.source.GetCatalogueSource
import java.net.CookieHandler
@@ -84,8 +82,6 @@ class NetworkHelper(
),
).addInterceptor(UncaughtExceptionInterceptor())
.addInterceptor(UserAgentInterceptor(::defaultUserAgentProvider))
.addNetworkInterceptor(IgnoreGzipInterceptor())
.addNetworkInterceptor(BrotliInterceptor)
// if (preferences.verboseLogging().get()) {
val httpLoggingInterceptor =
@@ -128,5 +124,7 @@ class NetworkHelper(
// val client by lazy { baseClientBuilder.cache(Cache(cacheDir, cacheSize)).build() }
val client by lazy { baseClientBuilder.build() }
@Deprecated("The regular client handles Cloudflare by default")
@Suppress("UNUSED")
val cloudflareClient by lazy { client }
}

View File

@@ -15,11 +15,14 @@ import rx.Observable
import rx.Producer
import rx.Subscription
import java.io.IOException
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.concurrent.atomics.AtomicBoolean
import kotlin.concurrent.atomics.ExperimentalAtomicApi
import kotlin.coroutines.resumeWithException
val jsonMime = "application/json; charset=utf-8".toMediaType()
@OptIn(ExperimentalAtomicApi::class)
@Deprecated("Use suspend APIs instead")
fun Call.asObservable(): Observable<Response> {
return Observable.unsafeCreate { subscriber ->
// Since Call is a one-shot type, clone it for each new subscriber.
@@ -27,9 +30,11 @@ fun Call.asObservable(): Observable<Response> {
// Wrap the call in a helper which handles both unsubscription and backpressure.
val requestArbiter =
object : AtomicBoolean(), Producer, Subscription {
object : Producer, Subscription {
val boolean = AtomicBoolean(false)
override fun request(n: Long) {
if (n == 0L || !compareAndSet(false, true)) return
if (n == 0L || !boolean.compareAndSet(expectedValue = false, newValue = true)) return
try {
val response = call.execute()
@@ -37,15 +42,15 @@ fun Call.asObservable(): Observable<Response> {
subscriber.onNext(response)
subscriber.onCompleted()
}
} catch (error: Exception) {
} catch (e: Exception) {
if (!subscriber.isUnsubscribed) {
subscriber.onError(error)
subscriber.onError(e)
}
}
}
override fun unsubscribe() {
// call.cancel()
call.cancel()
}
override fun isUnsubscribed(): Boolean = call.isCanceled()
@@ -56,50 +61,50 @@ fun Call.asObservable(): Observable<Response> {
}
}
fun Call.asObservableSuccess(): Observable<Response> =
asObservable()
.doOnNext { response ->
if (!response.isSuccessful) {
response.close()
throw HttpException(response.code)
@Deprecated("Use suspend APIs instead")
fun Call.asObservableSuccess(): Observable<Response> {
@Suppress("DEPRECATION")
return asObservable().doOnNext { response ->
if (!response.isSuccessful) {
response.close()
throw HttpException(response.code)
}
}
}
// Based on https://github.com/square/okhttp/blob/master/okhttp-coroutines/src/main/kotlin/okhttp3/coroutines/ExecuteAsync.kt
// and https://github.com/gildor/kotlin-coroutines-okhttp
private suspend fun Call.await(callStack: Array<StackTraceElement>): Response {
return suspendCancellableCoroutine { continuation ->
continuation.invokeOnCancellation {
try {
this.cancel()
} catch (_: Throwable) {
// ignore
}
}
// Based on https://github.com/gildor/kotlin-coroutines-okhttp
private suspend fun Call.await(callStack: Array<StackTraceElement>): Response {
return suspendCancellableCoroutine { continuation ->
val callback =
this.enqueue(
object : Callback {
override fun onResponse(
call: Call,
response: Response,
) {
continuation.resume(response) { _, resourceToClose, _ ->
response.body.close()
resourceToClose.close()
}
}
override fun onFailure(
call: Call,
e: IOException,
) {
// Don't bother with resuming the continuation if it is already cancelled.
if (continuation.isCancelled) return
val exception = IOException(e.message, e).apply { stackTrace = callStack }
continuation.resumeWithException(exception)
}
}
enqueue(callback)
continuation.invokeOnCancellation {
try {
cancel()
} catch (ex: Throwable) {
// Ignore cancel exception
}
}
override fun onResponse(
call: Call,
response: Response,
) {
continuation.resume(response) { _, value, _ ->
value.close()
}
}
},
)
}
}
@@ -109,7 +114,7 @@ suspend fun Call.await(): Response {
}
/**
* @since extensions-lib 1.5
* Similar to [await] but throws [HttpException] if [Response.isSuccessful] returns false
*/
suspend fun Call.awaitSuccess(): Response {
val callStack = Exception().stackTrace.run { copyOfRange(1, size) }
@@ -150,7 +155,3 @@ fun <T> decodeFromJsonResponse(
response.body.source().use {
json.decodeFromBufferedSource(deserializer, it)
}
class HttpException(
val code: Int,
) : IllegalStateException("HTTP error $code")

View File

@@ -35,7 +35,11 @@ class ProgressResponseBody(
val bytesRead = super.read(sink, byteCount)
// read() returns the number of bytes read, or -1 if this source is exhausted.
totalBytesRead += if (bytesRead != -1L) bytesRead else 0
progressListener.update(totalBytesRead, responseBody.contentLength(), bytesRead == -1L)
progressListener.update(
totalBytesRead,
responseBody.contentLength(),
bytesRead == -1L,
)
return bytesRead
}
}

View File

@@ -6,6 +6,7 @@ import okhttp3.CacheControl
import okhttp3.FormBody
import okhttp3.Headers
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Request
import okhttp3.RequestBody
import java.util.concurrent.TimeUnit.MINUTES
@@ -18,13 +19,7 @@ fun GET(
url: String,
headers: Headers = DEFAULT_HEADERS,
cache: CacheControl = DEFAULT_CACHE_CONTROL,
): Request =
Request
.Builder()
.url(url)
.headers(headers)
.cacheControl(cache)
.build()
): Request = GET(url.toHttpUrl(), headers, cache)
/**
* @since extensions-lib 1.4

View File

@@ -1,21 +0,0 @@
package eu.kanade.tachiyomi.network.interceptor
import okhttp3.Interceptor
import okhttp3.Response
/**
* To use [okhttp3.brotli.BrotliInterceptor] as a network interceptor,
* add [IgnoreGzipInterceptor] right before it.
*
* This nullifies the transparent gzip of [okhttp3.internal.http.BridgeInterceptor]
* so gzip and Brotli are explicitly handled by the [okhttp3.brotli.BrotliInterceptor].
*/
class IgnoreGzipInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
if (request.header("Accept-Encoding") == "gzip") {
request = request.newBuilder().removeHeader("Accept-Encoding").build()
}
return chain.proceed(request)
}
}