Skip to content

Commit 57cd209

Browse files
committed
wip
1 parent 8844fc1 commit 57cd209

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

example/tests/benchmark.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const TESTS = [
7676
},
7777
]
7878

79-
const BASE_URL = Platform.OS === 'android' ? 'ws://10.0.2.2' : 'ws://localhost'
79+
export const BASE_URL = Platform.OS === 'android' ? '10.0.2.2' : 'localhost'
8080

8181
export function BenchmarkUI() {
8282
return (
@@ -278,7 +278,7 @@ async function runSingleTest(opts: TestCase): Promise<TestResult> {
278278
const { Ws, port, messageCount, testCase, payload } = opts
279279

280280
return new Promise((resolve) => {
281-
const ws = new Ws(`${BASE_URL}:${port}`)
281+
const ws = new Ws(`ws://${BASE_URL}:${port}`)
282282
let outgoingTime: number
283283
let incomingTime: number
284284
let received = 0

example/tests/filesystem.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
showOpenFilePicker,
88
} from 'react-native-fast-io'
99

10+
import { BASE_URL } from './benchmark'
11+
1012
export function FileSystemUI() {
1113
const [file, setFile] = useState<File | null>(null)
1214

@@ -24,7 +26,7 @@ export function FileSystemUI() {
2426

2527
const body = compression ? file.stream().pipeThrough(new CompressionStream(compression)) : file
2628

27-
await fetch('http://localhost:3002/upload', {
29+
await fetch(`http://${BASE_URL}:3002/upload`, {
2830
method: 'POST',
2931
body,
3032
})
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
package com.margelo.nitro.fastio
22

3-
class HybridDuplexStream : HybridDuplexStreamSpec() {
4-
override var inputStream: HybridInputStreamSpec
5-
get() = throw NotImplementedError("HybridDuplexStream.inputStream getter not implemented")
6-
set(_) = throw NotImplementedError("HybridDuplexStream.inputStream setter not implemented")
3+
import java.io.PipedInputStream
4+
import java.io.PipedOutputStream
75

8-
override var outputStream: HybridOutputStreamSpec
9-
get() = throw NotImplementedError("HybridDuplexStream.outputStream getter not implemented")
10-
set(_) = throw NotImplementedError("HybridDuplexStream.outputStream setter not implemented")
6+
class HybridDuplexStream : HybridDuplexStreamSpec() {
7+
private val pipedInputStream = PipedInputStream(HybridStreamFactory.BUFFER_SIZE)
8+
private val pipedOutputStream = PipedOutputStream(pipedInputStream)
119

10+
override var inputStream = HybridInputStream(pipedInputStream) as HybridInputStreamSpec
11+
override var outputStream = HybridOutputStream(pipedOutputStream) as HybridOutputStreamSpec
12+
1213
override val memorySize: Long
13-
get() = 0L
14+
get() = inputStream.memorySize + outputStream.memorySize
15+
16+
fun close() {
17+
try {
18+
outputStream.close()
19+
inputStream.close()
20+
} catch (e: Exception) {
21+
println("Error closing duplex stream: ${e.message}")
22+
}
23+
}
1424
}

packages/react-native-fast-io/android/src/main/java/com/margelo/nitro/fastio/HybridNetwork.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,44 @@
11
package com.margelo.nitro.fastio
22

3+
import android.util.Log
34
import com.margelo.nitro.core.Promise
5+
import java.net.HttpURLConnection
6+
import java.net.URL
47

58
class HybridNetwork : HybridNetworkSpec() {
69
override fun request(opts: RequestOptions): Promise<Unit> {
7-
throw NotImplementedError("HybridNetwork.request() not implemented")
10+
return Promise.async {
11+
val connection = URL(opts.url).openConnection() as HttpURLConnection
12+
13+
connection.apply {
14+
requestMethod = opts.method.name.uppercase()
15+
doInput = true
16+
17+
opts.body?.let { hybridStream ->
18+
(hybridStream as HybridInputStream).stream.use { input ->
19+
outputStream.use { output ->
20+
val buffer = ByteArray(HybridStreamFactory.BUFFER_SIZE)
21+
var bytesRead: Int
22+
23+
while (input.read(buffer).also { bytesRead = it } != -1) {
24+
output.write(buffer, 0, bytesRead)
25+
output.flush() // Important: flush each chunk
26+
}
27+
}
28+
}
29+
}
30+
31+
connect()
32+
33+
if (responseCode in 200..299) {
34+
// tbd
35+
} else {
36+
throw Error("HTTP Error: $responseCode")
37+
}
38+
}
39+
40+
connection.disconnect()
41+
}
842
}
943

1044
override val memorySize: Long

0 commit comments

Comments
 (0)