Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ dependencies {
implementation "com.facebook.react:react-android"
implementation "com.facebook.react:hermes-android"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.squareup.okhttp3:okhttp:4.9.2"
}
39 changes: 38 additions & 1 deletion android/src/main/java/com/webworker/WebWorkerNative.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ object WebWorkerNative {

/** Called for worker console output (log, error, warn, info) */
fun onConsole(workerId: String, level: String, message: String)

/** Called for worker network requests */
fun onFetch(
workerId: String,
requestId: String,
url: String,
method: String,
headerKeys: Array<String>,
headerValues: Array<String>,
body: ByteArray?,
timeout: Double,
redirect: String
)
}

/**
Expand Down Expand Up @@ -100,6 +113,21 @@ object WebWorkerNative {
return nativeIsWorkerRunning(workerId)
}

/**
* Send fetch response back to C++
*/
fun handleFetchResponse(
workerId: String,
requestId: String,
status: Int,
headerKeys: Array<String>,
headerValues: Array<String>,
body: ByteArray?,
error: String?
) {
nativeHandleFetchResponse(workerId, requestId, status, headerKeys, headerValues, body, error)
}

/**
* Clean up all workers and release resources.
*/
Expand All @@ -119,4 +147,13 @@ object WebWorkerNative {
private external fun nativeHasWorker(workerId: String): Boolean
private external fun nativeIsWorkerRunning(workerId: String): Boolean
private external fun nativeCleanup()
}
private external fun nativeHandleFetchResponse(
workerId: String,
requestId: String,
status: Int,
headerKeys: Array<String>,
headerValues: Array<String>,
body: ByteArray?,
error: String?
)
}
98 changes: 98 additions & 0 deletions android/src/main/java/com/webworker/WebworkerModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.annotations.ReactModule
import okhttp3.Call
import okhttp3.Callback
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import java.io.IOException
import java.util.concurrent.TimeUnit

/**
* React Native TurboModule for WebWorker support.
Expand All @@ -16,6 +27,8 @@ import com.facebook.react.module.annotations.ReactModule
class WebworkerModule(reactContext: ReactApplicationContext) :
NativeWebworkerSpec(reactContext), WebWorkerNative.WorkerCallback {

private val client = OkHttpClient()

init {
// Initialize the native core with this module as the callback receiver
WebWorkerNative.initialize(this)
Expand Down Expand Up @@ -52,6 +65,91 @@ class WebworkerModule(reactContext: ReactApplicationContext) :
})
}

override fun onFetch(
workerId: String,
requestId: String,
url: String,
method: String,
headerKeys: Array<String>,
headerValues: Array<String>,
body: ByteArray?,
timeout: Double,
redirect: String
) {
val requestBuilder = Request.Builder()
.url(url)

// Headers
var contentType: MediaType? = null
for (i in headerKeys.indices) {
val key = headerKeys[i]
val value = headerValues[i]
requestBuilder.addHeader(key, value)
if (key.equals("Content-Type", ignoreCase = true)) {
contentType = value.toMediaTypeOrNull()
}
}

// Body
val requestBody = if (body != null && body.isNotEmpty()) {
body.toRequestBody(contentType)
} else if (method.equals("POST", ignoreCase = true) || method.equals("PUT", ignoreCase = true) || method.equals("PATCH", ignoreCase = true)) {
ByteArray(0).toRequestBody(null)
} else {
null
}

requestBuilder.method(method, requestBody)

// Configure client based on options
val requestClient = if (timeout > 0 || redirect != "follow") {
val builder = client.newBuilder()
if (timeout > 0) {
val timeoutMs = timeout.toLong()
builder.callTimeout(timeoutMs, TimeUnit.MILLISECONDS)
builder.readTimeout(timeoutMs, TimeUnit.MILLISECONDS)
builder.connectTimeout(timeoutMs, TimeUnit.MILLISECONDS)
}
if (redirect == "error" || redirect == "manual") {
builder.followRedirects(false)
builder.followSslRedirects(false)
}
builder.build()
} else {
client
}

requestClient.newCall(requestBuilder.build()).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
WebWorkerNative.handleFetchResponse(
workerId, requestId, 0, emptyArray(), emptyArray(), null, e.message
)
}

override fun onResponse(call: Call, response: Response) {
val responseBody = response.body?.bytes()
val headers = response.headers
val keys = mutableListOf<String>()
val values = mutableListOf<String>()

for (i in 0 until headers.size) {
keys.add(headers.name(i))
values.add(headers.value(i))
}

WebWorkerNative.handleFetchResponse(
workerId,
requestId,
response.code,
keys.toTypedArray(),
values.toTypedArray(),
responseBody,
null
)
}
})
}

// ============================================================================
// TurboModule Methods - mirror iOS implementation
// ============================================================================
Expand Down
Loading
Loading