react-native-mosquito-transport 0.0.47 → 0.0.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/CONTRIBUTING.md +37 -19
  2. package/MosquitoTransport.podspec +20 -0
  3. package/android/build.gradle +32 -42
  4. package/android/src/main/AndroidManifest.xml +1 -3
  5. package/android/src/main/java/com/mosquitotransport/MosquitoTransportModule.kt +101 -0
  6. package/android/src/main/java/com/mosquitotransport/MosquitoTransportPackage.kt +31 -0
  7. package/android/src/main/java/com/mosquitotransport/utils/FileUploader.kt +106 -0
  8. package/android/src/main/java/com/mosquitotransport/utils/UploadCallback.kt +7 -0
  9. package/ios/MosquitoTransport-Bridging-Header.h +1 -0
  10. package/ios/MosquitoTransport.h +6 -0
  11. package/ios/MosquitoTransport.mm +81 -0
  12. package/ios/{Mosquitodb.swift → MosquitoTransport.swift} +39 -42
  13. package/package.json +11 -3
  14. package/src/NativeMosquitoTransport.js +24 -0
  15. package/src/helpers/fs_manager.js +1 -1
  16. package/src/helpers/utils.js +2 -4
  17. package/src/helpers/variables.js +2 -1
  18. package/src/index.d.ts +55 -24
  19. package/src/index.js +20 -12
  20. package/src/products/auth/accessor.js +119 -95
  21. package/src/products/auth/index.js +11 -9
  22. package/src/products/database/index.js +1 -0
  23. package/src/products/http_callable/index.js +6 -5
  24. package/src/products/storage/index.js +65 -60
  25. package/.jshintignore +0 -4
  26. package/.jshintrc +0 -16
  27. package/TODO +0 -27
  28. package/android/gradle.properties +0 -5
  29. package/android/src/main/java/com/mosquitodb/MosquitodbModule.java +0 -82
  30. package/android/src/main/java/com/mosquitodb/MosquitodbPackage.java +0 -28
  31. package/android/src/main/java/com/mosquitodb/utils/FileUploader.java +0 -101
  32. package/android/src/main/java/com/mosquitodb/utils/UploadCallback.java +0 -7
  33. package/example/.bundle/config +0 -2
  34. package/example/.node-version +0 -1
  35. package/example/.watchmanconfig +0 -1
  36. package/example/Gemfile +0 -6
  37. package/example/android/app/build.gradle +0 -170
  38. package/example/android/app/debug.keystore +0 -0
  39. package/example/android/app/proguard-rules.pro +0 -10
  40. package/example/android/app/src/debug/AndroidManifest.xml +0 -13
  41. package/example/android/app/src/debug/java/com/mosquitodbexample/ReactNativeFlipper.java +0 -75
  42. package/example/android/app/src/main/AndroidManifest.xml +0 -25
  43. package/example/android/app/src/main/java/com/mosquitodbexample/MainActivity.java +0 -35
  44. package/example/android/app/src/main/java/com/mosquitodbexample/MainApplication.java +0 -62
  45. package/example/android/app/src/main/res/drawable/rn_edit_text_material.xml +0 -36
  46. package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
  47. package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png +0 -0
  48. package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
  49. package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png +0 -0
  50. package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
  51. package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png +0 -0
  52. package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
  53. package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png +0 -0
  54. package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
  55. package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
  56. package/example/android/app/src/main/res/values/strings.xml +0 -3
  57. package/example/android/app/src/main/res/values/styles.xml +0 -9
  58. package/example/android/app/src/release/java/com/mosquitodbexample/ReactNativeFlipper.java +0 -20
  59. package/example/android/build.gradle +0 -21
  60. package/example/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  61. package/example/android/gradle/wrapper/gradle-wrapper.properties +0 -5
  62. package/example/android/gradle.properties +0 -44
  63. package/example/android/gradlew +0 -234
  64. package/example/android/gradlew.bat +0 -89
  65. package/example/android/settings.gradle +0 -4
  66. package/example/app.json +0 -4
  67. package/example/babel.config.js +0 -17
  68. package/example/index.js +0 -5
  69. package/example/ios/.xcode.env +0 -11
  70. package/example/ios/File.swift +0 -6
  71. package/example/ios/MosquitodbExample/AppDelegate.h +0 -6
  72. package/example/ios/MosquitodbExample/AppDelegate.mm +0 -36
  73. package/example/ios/MosquitodbExample/Images.xcassets/AppIcon.appiconset/Contents.json +0 -53
  74. package/example/ios/MosquitodbExample/Images.xcassets/Contents.json +0 -6
  75. package/example/ios/MosquitodbExample/Info.plist +0 -55
  76. package/example/ios/MosquitodbExample/LaunchScreen.storyboard +0 -47
  77. package/example/ios/MosquitodbExample/main.m +0 -10
  78. package/example/ios/MosquitodbExample-Bridging-Header.h +0 -3
  79. package/example/ios/MosquitodbExample.xcodeproj/project.pbxproj +0 -703
  80. package/example/ios/MosquitodbExample.xcodeproj/xcshareddata/xcschemes/MosquitodbExample.xcscheme +0 -88
  81. package/example/ios/MosquitodbExample.xcworkspace/contents.xcworkspacedata +0 -10
  82. package/example/ios/MosquitodbExample.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  83. package/example/ios/MosquitodbExample.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings +0 -8
  84. package/example/ios/MosquitodbExample.xcworkspace/xcuserdata/anthony.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  85. package/example/ios/MosquitodbExample.xcworkspace/xcuserdata/anthony.xcuserdatad/WorkspaceSettings.xcsettings +0 -14
  86. package/example/ios/MosquitodbExampleTests/Info.plist +0 -24
  87. package/example/ios/MosquitodbExampleTests/MosquitodbExampleTests.m +0 -66
  88. package/example/ios/Podfile +0 -60
  89. package/example/metro.config.js +0 -40
  90. package/example/package.json +0 -22
  91. package/example/react-native.config.js +0 -10
  92. package/example/src/App.tsx +0 -31
  93. package/ios/Mosquitodb-Bridging-Header.h +0 -2
  94. package/ios/Mosquitodb.m +0 -22
  95. package/ios/Mosquitodb.xcodeproj/project.pbxproj +0 -283
  96. package/ios/Mosquitodb.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -4
  97. package/react-native-mosquitodb.podspec +0 -35
package/CONTRIBUTING.md CHANGED
@@ -6,15 +6,30 @@ We want this community to be friendly and respectful to each other. Please follo
6
6
 
7
7
  ## Development workflow
8
8
 
9
- To get started with the project, run `yarn` in the root directory to install the required dependencies for each package:
9
+ This project is a monorepo managed using [Yarn workspaces](https://yarnpkg.com/features/workspaces). It contains the following packages:
10
+
11
+ - The library package in the root directory.
12
+ - An example app in the `example/` directory.
13
+
14
+ To get started with the project, make sure you have the correct version of [Node.js](https://nodejs.org/) installed. See the [`.nvmrc`](./.nvmrc) file for the version used in this project.
15
+
16
+ Run `yarn` in the root directory to install the required dependencies for each package:
10
17
 
11
18
  ```sh
12
19
  yarn
13
20
  ```
14
21
 
15
- > While it's possible to use [`npm`](https://github.com/npm/cli), the tooling is built around [`yarn`](https://classic.yarnpkg.com/), so you'll have an easier time if you use `yarn` for development.
22
+ > Since the project relies on Yarn workspaces, you cannot use [`npm`](https://github.com/npm/cli) for development without manually migrating.
23
+
24
+ The [example app](/example/) demonstrates usage of the library. You need to run it to test any changes you make.
25
+
26
+ It is configured to use the local version of the library, so any changes you make to the library's source code will be reflected in the example app. Changes to the library's JavaScript code will be reflected in the example app without a rebuild, but native code changes will require a rebuild of the example app.
27
+
28
+ If you want to use Android Studio or Xcode to edit the native code, you can open the `example/android` or `example/ios` directories respectively in those editors. To edit the Objective-C or Swift files, open `example/ios/MosquitoTransportExample.xcworkspace` in Xcode and find the source files at `Pods > Development Pods > react-native-mosquito-transport`.
16
29
 
17
- While developing, you can run the [example app](/example/) to test your changes. Any changes you make in your library's JavaScript code will be reflected in the example app without a rebuild. If you change any native code, then you'll need to rebuild the example app.
30
+ To edit the Java or Kotlin files, open `example/android` in Android studio and find the source files at `react-native-mosquito-transport` under `Android`.
31
+
32
+ You can use various commands from the root directory to work with the project.
18
33
 
19
34
  To start the packager:
20
35
 
@@ -34,10 +49,23 @@ To run the example app on iOS:
34
49
  yarn example ios
35
50
  ```
36
51
 
37
- Make sure your code passes TypeScript and ESLint. Run the following to verify:
52
+ To confirm that the app is running with the new architecture, you can check the Metro logs for a message like this:
53
+
54
+ ```sh
55
+ Running "MosquitoTransportExample" with {"fabric":true,"initialProps":{"concurrentRoot":true},"rootTag":1}
56
+ ```
57
+
58
+ Note the `"fabric":true` and `"concurrentRoot":true` properties.
59
+
60
+ Make sure your code passes TypeScript:
38
61
 
39
62
  ```sh
40
63
  yarn typecheck
64
+ ```
65
+
66
+ To check for linting errors, run the following:
67
+
68
+ ```sh
41
69
  yarn lint
42
70
  ```
43
71
 
@@ -53,10 +81,6 @@ Remember to add tests for your change if possible. Run the unit tests by:
53
81
  yarn test
54
82
  ```
55
83
 
56
- To edit the Objective-C or Swift files, open `example/ios/MosquitodbExample.xcworkspace` in XCode and find the source files at `Pods > Development Pods > react-native-mosquitodb`.
57
-
58
- To edit the Java or Kotlin files, open `example/android` in Android studio and find the source files at `react-native-mosquitodb` under `Android`.
59
-
60
84
 
61
85
  ### Commit message convention
62
86
 
@@ -65,19 +89,12 @@ We follow the [conventional commits specification](https://www.conventionalcommi
65
89
  - `fix`: bug fixes, e.g. fix crash due to deprecated method.
66
90
  - `feat`: new features, e.g. add new method to the module.
67
91
  - `refactor`: code refactor, e.g. migrate from class components to hooks.
68
- - `docs`: changes into documentation, e.g. add usage example for the module..
92
+ - `docs`: changes into documentation, e.g. add usage example for the module.
69
93
  - `test`: adding or updating tests, e.g. add integration tests using detox.
70
94
  - `chore`: tooling changes, e.g. change CI config.
71
95
 
72
96
  Our pre-commit hooks verify that your commit message matches this format when committing.
73
97
 
74
- ### Linting and tests
75
-
76
- [ESLint](https://eslint.org/), [Prettier](https://prettier.io/), [TypeScript](https://www.typescriptlang.org/)
77
-
78
- We use [TypeScript](https://www.typescriptlang.org/) for type checking, [ESLint](https://eslint.org/) with [Prettier](https://prettier.io/) for linting and formatting the code, and [Jest](https://jestjs.io/) for testing.
79
-
80
- Our pre-commit hooks verify that the linter and tests pass when committing.
81
98
 
82
99
  ### Publishing to npm
83
100
 
@@ -89,14 +106,15 @@ To publish new versions, run the following:
89
106
  yarn release
90
107
  ```
91
108
 
109
+
92
110
  ### Scripts
93
111
 
94
112
  The `package.json` file contains various scripts for common tasks:
95
113
 
96
- - `yarn bootstrap`: setup project by installing all dependencies and pods.
114
+ - `yarn`: setup project by installing dependencies.
97
115
  - `yarn typecheck`: type-check files with TypeScript.
98
- - `yarn lint`: lint files with ESLint.
99
- - `yarn test`: run unit tests with Jest.
116
+ - `yarn lint`: lint files with [ESLint](https://eslint.org/).
117
+ - `yarn test`: run unit tests with [Jest](https://jestjs.io/).
100
118
  - `yarn example start`: start the Metro server for the example app.
101
119
  - `yarn example android`: run the example app on Android.
102
120
  - `yarn example ios`: run the example app on iOS.
@@ -0,0 +1,20 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "MosquitoTransport"
7
+ s.version = package["version"]
8
+ s.summary = package["description"]
9
+ s.homepage = package["homepage"]
10
+ s.license = package["license"]
11
+ s.authors = package["author"]
12
+
13
+ s.platforms = { :ios => min_ios_version_supported }
14
+ s.source = { :git => "https://github.com/deflexable/react-native-mosquito-transport.git", :tag => "#{s.version}" }
15
+
16
+ s.source_files = "ios/**/*.{h,m,mm,swift,cpp}"
17
+ s.private_header_files = "ios/**/*.h"
18
+
19
+ install_modules_dependencies(s)
20
+ end
@@ -1,50 +1,58 @@
1
1
  buildscript {
2
+ ext.MosquitoTransport = [
3
+ kotlinVersion: "2.0.21",
4
+ minSdkVersion: 24,
5
+ compileSdkVersion: 36,
6
+ targetSdkVersion: 36
7
+ ]
8
+
9
+ ext.getExtOrDefault = { prop ->
10
+ if (rootProject.ext.has(prop)) {
11
+ return rootProject.ext.get(prop)
12
+ }
13
+
14
+ return MosquitoTransport[prop]
15
+ }
16
+
2
17
  repositories {
3
18
  google()
4
19
  mavenCentral()
5
20
  }
6
21
 
7
22
  dependencies {
8
- classpath "com.android.tools.build:gradle:7.2.1"
23
+ classpath "com.android.tools.build:gradle:8.7.2"
24
+ // noinspection DifferentKotlinGradleVersion
25
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
9
26
  }
10
27
  }
11
28
 
12
- def isNewArchitectureEnabled() {
13
- return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
14
- }
15
29
 
16
30
  apply plugin: "com.android.library"
31
+ apply plugin: "kotlin-android"
17
32
 
18
-
19
- def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
20
-
21
- if (isNewArchitectureEnabled()) {
22
- apply plugin: "com.facebook.react"
23
- }
24
-
25
- def getExtOrDefault(name) {
26
- return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Mosquitodb_" + name]
27
- }
28
-
29
- def getExtOrIntegerDefault(name) {
30
- return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Mosquitodb_" + name]).toInteger()
31
- }
33
+ apply plugin: "com.facebook.react"
32
34
 
33
35
  android {
34
- compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
36
+ namespace "com.mosquitotransport"
37
+
38
+ compileSdkVersion getExtOrDefault("compileSdkVersion")
35
39
 
36
40
  defaultConfig {
37
- minSdkVersion getExtOrIntegerDefault("minSdkVersion")
38
- targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
39
- buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
41
+ minSdkVersion getExtOrDefault("minSdkVersion")
42
+ targetSdkVersion getExtOrDefault("targetSdkVersion")
43
+ }
44
+
45
+ buildFeatures {
46
+ buildConfig true
40
47
  }
48
+
41
49
  buildTypes {
42
50
  release {
43
51
  minifyEnabled false
44
52
  }
45
53
  }
46
54
 
47
- lintOptions {
55
+ lint {
48
56
  disable "GradleCompatible"
49
57
  }
50
58
 
@@ -52,26 +60,8 @@ android {
52
60
  sourceCompatibility JavaVersion.VERSION_1_8
53
61
  targetCompatibility JavaVersion.VERSION_1_8
54
62
  }
55
-
56
63
  }
57
64
 
58
- repositories {
59
- mavenCentral()
60
- google()
61
- }
62
-
63
-
64
65
  dependencies {
65
- // For < 0.71, this will be from the local maven repo
66
- // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
67
- //noinspection GradleDynamicVersion
68
- implementation "com.facebook.react:react-native:+"
69
- }
70
-
71
- if (isNewArchitectureEnabled()) {
72
- react {
73
- jsRootDir = file("../src/")
74
- libraryName = "Mosquitodb"
75
- codegenJavaPackageName = "com.mosquitodb"
76
- }
66
+ implementation "com.facebook.react:react-android"
77
67
  }
@@ -1,4 +1,2 @@
1
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
- package="com.mosquitodb">
3
-
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
4
2
  </manifest>
@@ -0,0 +1,101 @@
1
+ package com.mosquitotransport
2
+
3
+ import android.util.ArrayMap
4
+ import androidx.annotation.Nullable
5
+ import com.facebook.react.bridge.*
6
+ import com.facebook.react.module.annotations.ReactModule
7
+ import com.mosquitotransport.utils.FileUploader
8
+ import com.mosquitotransport.utils.UploadCallback
9
+ import javax.annotation.Nonnull
10
+ import android.os.SystemClock
11
+
12
+ class MosquitoTransportModule(reactContext: ReactApplicationContext) :
13
+ NativeMosquitoTransportSpec(reactContext) {
14
+
15
+ private val context = reactContext
16
+
17
+ @Nonnull
18
+ override fun getName(): String {
19
+ return NAME
20
+ }
21
+
22
+ companion object {
23
+ const val NAME = NativeMosquitoTransportSpec.NAME
24
+ }
25
+
26
+ override fun addListener(eventType: String) {
27
+ // No implementation needed for TurboModule
28
+ // This implements the abstract method required by NativeMosquitoTransportSpec
29
+ }
30
+
31
+ override fun removeListeners(count: Double) {
32
+ // No implementation needed for TurboModule
33
+ // This implements the abstract method required by NativeMosquitoTransportSpec
34
+ }
35
+
36
+ private fun emitNewEvent(eventName: String, @Nullable params: WritableMap?) {
37
+ context.emitDeviceEvent(eventName, params)
38
+ }
39
+
40
+ override fun getSystemUptime(promise: Promise) {
41
+ val uptime = SystemClock.elapsedRealtime()
42
+ promise.resolve(uptime)
43
+ }
44
+
45
+ private val uploaderMap = ArrayMap<String, FileUploader>()
46
+
47
+ override fun uploadFile(readable: ReadableMap) {
48
+ val processId = readable.getString("processID")
49
+ val uploader = FileUploader()
50
+
51
+ uploader.uploadFile(readable, object : UploadCallback {
52
+ override fun onProgress(sentBytes: Long, totalBytes: Long) {
53
+ val progress: WritableMap = WritableNativeMap().apply {
54
+ putInt("sentBytes", sentBytes.toInt())
55
+ putInt("totalBytes", totalBytes.toInt())
56
+ putString("processID", processId)
57
+ }
58
+ emitNewEvent("mt-uploading-progress", progress)
59
+ }
60
+
61
+ override fun onComplete(responseCode: Int, responseBody: String) {
62
+ val statusData: WritableMap = WritableNativeMap().apply {
63
+ putString("processID", processId)
64
+ putString("result", responseBody)
65
+ }
66
+ emitNewEvent("mt-uploading-status", statusData)
67
+ }
68
+
69
+ override fun onError(e: Exception) {
70
+ val statusData: WritableMap = WritableNativeMap().apply {
71
+ putString("processID", processId)
72
+ putString("error", "internal_error")
73
+ putString("errorDes", e.localizedMessage)
74
+ }
75
+ emitNewEvent("mt-uploading-status", statusData)
76
+ }
77
+ })
78
+
79
+ uploaderMap[processId] = uploader
80
+ }
81
+
82
+ override fun cancelUpload(processID: String) {
83
+ uploaderMap[processID]?.cancelUpload()
84
+ }
85
+
86
+ override fun downloadFile(readable: ReadableMap) {
87
+
88
+ }
89
+
90
+ override fun cancelDownload(processID: String) {
91
+
92
+ }
93
+
94
+ override fun pauseDownload(processID: String) {
95
+
96
+ }
97
+
98
+ override fun resumeDownload(processID: String) {
99
+
100
+ }
101
+ }
@@ -0,0 +1,31 @@
1
+ package com.mosquitotransport
2
+
3
+ import com.facebook.react.BaseReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.module.model.ReactModuleInfo
7
+ import com.facebook.react.module.model.ReactModuleInfoProvider
8
+ import java.util.HashMap
9
+
10
+ class MosquitoTransportPackage : BaseReactPackage() {
11
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12
+ return if (name == MosquitoTransportModule.NAME) {
13
+ MosquitoTransportModule(reactContext)
14
+ } else {
15
+ null
16
+ }
17
+ }
18
+
19
+ override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
20
+ mapOf(
21
+ MosquitoTransportModule.NAME to ReactModuleInfo(
22
+ name = MosquitoTransportModule.NAME,
23
+ className = MosquitoTransportModule.NAME,
24
+ canOverrideExistingModule = false,
25
+ needsEagerInit = false,
26
+ isCxxModule = false,
27
+ isTurboModule = true
28
+ )
29
+ )
30
+ }
31
+ }
@@ -0,0 +1,106 @@
1
+ package com.mosquitotransport.utils
2
+
3
+ import android.net.Uri
4
+ import com.facebook.react.bridge.ReadableMap
5
+ import com.facebook.react.bridge.ReadableMapKeySetIterator
6
+ import java.io.*
7
+ import java.net.HttpURLConnection
8
+ import java.net.URL
9
+ import java.util.Objects
10
+
11
+ class FileUploader {
12
+
13
+ @Volatile
14
+ private var isCancelled = false
15
+
16
+ fun cancelUpload() {
17
+ isCancelled = true
18
+ }
19
+
20
+ fun uploadFile(config: ReadableMap, callback: UploadCallback) {
21
+ Thread {
22
+ try {
23
+ val fileUri = Uri.parse(config.getString("file"))
24
+ val file = File(Objects.requireNonNull(fileUri.path))
25
+
26
+ if (!file.exists()) {
27
+ throw FileNotFoundException("File not found: ${file.absolutePath}")
28
+ }
29
+
30
+ val totalBytes = file.length()
31
+ val connection = (URL(config.getString("url")).openConnection() as HttpURLConnection).apply {
32
+ doOutput = true
33
+ requestMethod = "POST"
34
+ }
35
+
36
+ config.getMap("extraHeaders")?.let { headers ->
37
+ val iterator = headers.keySetIterator()
38
+ while (iterator.hasNextKey()) {
39
+ val key = iterator.nextKey()
40
+ connection.setRequestProperty(key, headers.getString(key))
41
+ }
42
+ }
43
+
44
+ connection.setRequestProperty("Content-Type", "buffer/upload")
45
+ connection.setRequestProperty("hash-upload", config.getString("createHash"))
46
+ connection.setRequestProperty("Mosquito-Destination", config.getString("destination"))
47
+
48
+ if (config.hasKey("authToken")) {
49
+ connection.setRequestProperty("mtoken", config.getString("authToken"))
50
+ }
51
+
52
+ connection.setFixedLengthStreamingMode(totalBytes.toInt())
53
+
54
+ val buffer = ByteArray(8192)
55
+ var sentBytes = 0L
56
+
57
+ FileInputStream(file).use { input ->
58
+ connection.outputStream.use { output ->
59
+ var bytesRead: Int
60
+
61
+ while (input.read(buffer).also { bytesRead = it } != -1) {
62
+ if (isCancelled) {
63
+ connection.disconnect()
64
+ callback.onError(IOException("Upload cancelled"))
65
+ return@Thread
66
+ }
67
+
68
+ output.write(buffer, 0, bytesRead)
69
+ sentBytes += bytesRead
70
+ callback.onProgress(sentBytes, totalBytes)
71
+ }
72
+
73
+ output.flush()
74
+ }
75
+ }
76
+
77
+ val responseCode = connection.responseCode
78
+ val responseStream =
79
+ if (responseCode in 200..399) connection.inputStream
80
+ else connection.errorStream
81
+
82
+ val responseBody = readStream(responseStream)
83
+
84
+ callback.onComplete(responseCode, responseBody)
85
+ connection.disconnect()
86
+
87
+ } catch (e: Exception) {
88
+ callback.onError(e)
89
+ }
90
+ }.start()
91
+ }
92
+
93
+ @Throws(IOException::class)
94
+ private fun readStream(stream: InputStream?): String {
95
+ if (stream == null) return ""
96
+
97
+ return BufferedReader(InputStreamReader(stream)).use { reader ->
98
+ buildString {
99
+ var line: String?
100
+ while (reader.readLine().also { line = it } != null) {
101
+ append(line).append("\n")
102
+ }
103
+ }.trim()
104
+ }
105
+ }
106
+ }
@@ -0,0 +1,7 @@
1
+ package com.mosquitotransport.utils
2
+
3
+ interface UploadCallback {
4
+ fun onProgress(sentBytes: Long, totalBytes: Long)
5
+ fun onComplete(responseCode: Int, responseBody: String)
6
+ fun onError(e: Exception)
7
+ }
@@ -0,0 +1 @@
1
+ #import <React/RCTEventEmitter.h>
@@ -0,0 +1,6 @@
1
+ #import <MosquitoTransportSpec/MosquitoTransportSpec.h>
2
+ #import <React/RCTEventEmitter.h>
3
+
4
+ @interface MosquitoTransport : RCTEventEmitter <NativeMosquitoTransportSpec>
5
+
6
+ @end
@@ -0,0 +1,81 @@
1
+ #import "MosquitoTransport.h"
2
+ #import <React/RCTEventEmitter.h>
3
+ #if __has_include("MosquitoTransport-Swift.h")
4
+ #import "MosquitoTransport-Swift.h"
5
+ #else
6
+ #import "MosquitoTransport/MosquitoTransport-Swift.h"
7
+ #endif
8
+
9
+ @interface MosquitoTransport () <NativeMosquitoTransportImplDelegate>
10
+ @end
11
+
12
+ @implementation MosquitoTransport {
13
+ NativeMosquitoTransportImpl *nativeMosquitoTransport;
14
+ }
15
+
16
+ RCT_EXPORT_MODULE()
17
+
18
+ - (instancetype) init {
19
+ self = [super init];
20
+ if (self) {
21
+ nativeMosquitoTransport = [NativeMosquitoTransportImpl new];
22
+ // Critical: Register ourselves as the Objective-C bridge's event emitter
23
+ nativeMosquitoTransport.delegate = self;
24
+ }
25
+ return self;
26
+ }
27
+
28
+ // event listeners
29
+ - (void)sendEvent:(NSString *)name body:(id)body {
30
+ [super sendEventWithName:name body:body];
31
+ }
32
+
33
+ - (NSArray<NSString *> *)supportedEvents {
34
+ return @[
35
+ @"mt-uploading-progress",
36
+ @"mt-uploading-status",
37
+ @"mt-download-progress",
38
+ @"mt-download-status"
39
+ ];
40
+ }
41
+
42
+ - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
43
+ (const facebook::react::ObjCTurboModule::InitParams &)params
44
+ {
45
+ return std::make_shared<facebook::react::NativeMosquitoTransportSpecJSI>(params);
46
+ }
47
+
48
+ + (BOOL)requiresMainQueueSetup {
49
+ return NO;
50
+ }
51
+
52
+ RCT_EXPORT_METHOD(getSystemUptime:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
53
+ NSTimeInterval uptime = [NSProcessInfo processInfo].systemUptime;
54
+ resolve(@(uptime * 1000));
55
+ }
56
+
57
+ - (void)uploadFile:(NSDictionary *)options {
58
+ [nativeMosquitoTransport uploadFile:options];
59
+ }
60
+
61
+ - (void)cancelUpload:(NSString *)processID {
62
+ [nativeMosquitoTransport cancelUpload:processID];
63
+ }
64
+
65
+ - (void)downloadFile:(NSDictionary *)options {
66
+ [nativeMosquitoTransport downloadFile:options];
67
+ }
68
+
69
+ - (void)cancelDownload:(NSString *)processID {
70
+ [nativeMosquitoTransport cancelDownload:processID];
71
+ }
72
+
73
+ - (void)pauseDownload:(NSString *)processID {
74
+ [nativeMosquitoTransport pauseDownload:processID];
75
+ }
76
+
77
+ - (void)resumeDownload:(NSString *)processID {
78
+ [nativeMosquitoTransport resumeDownload:processID];
79
+ }
80
+
81
+ @end