Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Network Module [1] #44211

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 packages/react-native/ReactAndroid/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ dependencies {
testImplementation(libs.junit)
testImplementation(libs.assertj)
testImplementation(libs.mockito)
testImplementation(libs.mockito.kotlin)
testImplementation(libs.robolectric)
testImplementation(libs.thoughtworks)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.modules.network

import okhttp3.CookieJar

public interface CookieJarContainer : CookieJar {

public fun setCookieJar(cookieJar: CookieJar?)

public fun removeCookieJar()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,47 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.react.modules.network

package com.facebook.react.modules.network;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FilterOutputStream
import java.io.IOException
import java.io.OutputStream

/**
* An OutputStream that counts the number of bytes written.
*
* @author Chris Nokleberg
* @since 1.0
*/
class CountingOutputStream extends FilterOutputStream {

private long mCount;

/**
* Constructs a new {@code FilterOutputStream} with {@code out} as its target stream.
*
* @param out the target stream that this stream writes to.
*/
public CountingOutputStream(OutputStream out) {
super(out);
mCount = 0;
}
internal open class CountingOutputStream
/**
* Constructs a new `FilterOutputStream` with `out` as its target stream.
*
* @param out the target stream that this stream writes to.
*/
(out: OutputStream?) : FilterOutputStream(out) {

/** Returns the number of bytes written. */
public long getCount() {
return mCount;
}
var count: Long = 0
private set

@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
mCount += len;
@Throws(IOException::class)
override fun write(b: ByteArray, off: Int, len: Int) {
out.write(b, off, len)
count += len.toLong()
}

@Override
public void write(int b) throws IOException {
out.write(b);
mCount++;
@Throws(IOException::class)
override fun write(b: Int) {
out.write(b)
count++
}

// Overriding close() because FilterOutputStream's close() method pre-JDK8 has bad behavior:
// it silently ignores any exception thrown by flush(). Instead, just close the delegate stream.
// It should flush itself if necessary.
@Override
public void close() throws IOException {
out.close();
@Throws(IOException::class)
override fun close() {
out.close()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.modules.network

import okhttp3.OkHttpClient
import okhttp3.OkHttpClient.Builder

public fun interface CustomClientBuilder {
public fun apply(builder: OkHttpClient.Builder)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.modules.network

import java.io.IOException
import okhttp3.MediaType
import okhttp3.RequestBody
import okio.BufferedSink
import okio.Okio
import okio.Okio.sink
import okio.Sink

internal class ProgressRequestBody(
private val requestBody: RequestBody,
private val progressListener: ProgressListener
) : RequestBody() {

private var _contentLength = 0L

override fun contentType(): MediaType? = requestBody.contentType()

@Throws(IOException::class)
override fun contentLength(): Long {
if (_contentLength == 0L) {
_contentLength = requestBody.contentLength()
}
return _contentLength
}

@Throws(IOException::class)
override fun writeTo(sink: BufferedSink) {
// In 99% of cases, this method is called strictly once.
// The only case when it is called more than once is internal okhttp upload re-try.
// We need to re-create CountingOutputStream in this case as progress should be re-evaluated.
val sinkWrapper = Okio.buffer(outputStreamSink(sink))

// contentLength changes for input streams, since we're using inputStream.available(),
// so get the length before writing to the sink
contentLength()
requestBody.writeTo(sinkWrapper)
sinkWrapper.flush()
}

private fun outputStreamSink(sink: BufferedSink): Sink =
Okio.sink(
object : CountingOutputStream(sink.outputStream()) {
@Throws(IOException::class)
override fun write(b: ByteArray, off: Int, len: Int) {
super.write(b, off, len)
sendProgressUpdate()
}

@Throws(IOException::class)
override fun write(b: Int) {
super.write(b)
sendProgressUpdate()
}

@Throws(IOException::class)
private fun sendProgressUpdate() {
val bytesWritten = count
val contentLength = contentLength()
progressListener.onProgress(
bytesWritten, contentLength, bytesWritten == contentLength)
}
})
}
Loading
Loading