react-native-hls-cache 0.1.0

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 (52) hide show
  1. package/HlsCache.podspec +29 -0
  2. package/LICENSE +20 -0
  3. package/README.md +233 -0
  4. package/android/CMakeLists.txt +24 -0
  5. package/android/build.gradle +118 -0
  6. package/android/src/main/AndroidManifest.xml +2 -0
  7. package/android/src/main/cpp/cpp-adapter.cpp +6 -0
  8. package/android/src/main/java/com/margelo/nitro/hlscache/HlsCache.kt +32 -0
  9. package/android/src/main/java/com/margelo/nitro/hlscache/HlsCachePackage.kt +22 -0
  10. package/ios/ClientConnectionHandler.swift +234 -0
  11. package/ios/DataSource.swift +394 -0
  12. package/ios/HlsCache.swift +121 -0
  13. package/ios/NetworkDownloader.swift +220 -0
  14. package/ios/PrefetchManager.swift +235 -0
  15. package/ios/VideoCacheStorage.swift +235 -0
  16. package/ios/VideoProxyServer.swift +185 -0
  17. package/lib/module/HlsCache.nitro.js +4 -0
  18. package/lib/module/HlsCache.nitro.js.map +1 -0
  19. package/lib/module/index.js +71 -0
  20. package/lib/module/index.js.map +1 -0
  21. package/lib/module/package.json +1 -0
  22. package/lib/typescript/package.json +1 -0
  23. package/lib/typescript/src/HlsCache.nitro.d.ts +13 -0
  24. package/lib/typescript/src/HlsCache.nitro.d.ts.map +1 -0
  25. package/lib/typescript/src/index.d.ts +36 -0
  26. package/lib/typescript/src/index.d.ts.map +1 -0
  27. package/nitro.json +23 -0
  28. package/nitrogen/generated/android/c++/JHybridHlsCacheSpec.cpp +89 -0
  29. package/nitrogen/generated/android/c++/JHybridHlsCacheSpec.hpp +68 -0
  30. package/nitrogen/generated/android/hlscache+autolinking.cmake +81 -0
  31. package/nitrogen/generated/android/hlscache+autolinking.gradle +27 -0
  32. package/nitrogen/generated/android/hlscacheOnLoad.cpp +54 -0
  33. package/nitrogen/generated/android/hlscacheOnLoad.hpp +34 -0
  34. package/nitrogen/generated/android/kotlin/com/margelo/nitro/hlscache/HybridHlsCacheSpec.kt +75 -0
  35. package/nitrogen/generated/android/kotlin/com/margelo/nitro/hlscache/hlscacheOnLoad.kt +35 -0
  36. package/nitrogen/generated/ios/HlsCache+autolinking.rb +60 -0
  37. package/nitrogen/generated/ios/HlsCache-Swift-Cxx-Bridge.cpp +49 -0
  38. package/nitrogen/generated/ios/HlsCache-Swift-Cxx-Bridge.hpp +160 -0
  39. package/nitrogen/generated/ios/HlsCache-Swift-Cxx-Umbrella.hpp +46 -0
  40. package/nitrogen/generated/ios/HlsCacheAutolinking.mm +33 -0
  41. package/nitrogen/generated/ios/HlsCacheAutolinking.swift +26 -0
  42. package/nitrogen/generated/ios/c++/HybridHlsCacheSpecSwift.cpp +11 -0
  43. package/nitrogen/generated/ios/c++/HybridHlsCacheSpecSwift.hpp +118 -0
  44. package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
  45. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
  46. package/nitrogen/generated/ios/swift/HybridHlsCacheSpec.swift +60 -0
  47. package/nitrogen/generated/ios/swift/HybridHlsCacheSpec_cxx.swift +237 -0
  48. package/nitrogen/generated/shared/c++/HybridHlsCacheSpec.cpp +26 -0
  49. package/nitrogen/generated/shared/c++/HybridHlsCacheSpec.hpp +69 -0
  50. package/package.json +174 -0
  51. package/src/HlsCache.nitro.ts +15 -0
  52. package/src/index.tsx +75 -0
@@ -0,0 +1,237 @@
1
+ ///
2
+ /// HybridHlsCacheSpec_cxx.swift
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ import NitroModules
9
+
10
+ /**
11
+ * A class implementation that bridges HybridHlsCacheSpec over to C++.
12
+ * In C++, we cannot use Swift protocols - so we need to wrap it in a class to make it strongly defined.
13
+ *
14
+ * Also, some Swift types need to be bridged with special handling:
15
+ * - Enums need to be wrapped in Structs, otherwise they cannot be accessed bi-directionally (Swift bug: https://github.com/swiftlang/swift/issues/75330)
16
+ * - Other HybridObjects need to be wrapped/unwrapped from the Swift TCxx wrapper
17
+ * - Throwing methods need to be wrapped with a Result<T, Error> type, as exceptions cannot be propagated to C++
18
+ */
19
+ open class HybridHlsCacheSpec_cxx {
20
+ /**
21
+ * The Swift <> C++ bridge's namespace (`margelo::nitro::hlscache::bridge::swift`)
22
+ * from `HlsCache-Swift-Cxx-Bridge.hpp`.
23
+ * This contains specialized C++ templates, and C++ helper functions that can be accessed from Swift.
24
+ */
25
+ public typealias bridge = margelo.nitro.hlscache.bridge.swift
26
+
27
+ /**
28
+ * Holds an instance of the `HybridHlsCacheSpec` Swift protocol.
29
+ */
30
+ private var __implementation: any HybridHlsCacheSpec
31
+
32
+ /**
33
+ * Holds a weak pointer to the C++ class that wraps the Swift class.
34
+ */
35
+ private var __cxxPart: bridge.std__weak_ptr_HybridHlsCacheSpec_
36
+
37
+ /**
38
+ * Create a new `HybridHlsCacheSpec_cxx` that wraps the given `HybridHlsCacheSpec`.
39
+ * All properties and methods bridge to C++ types.
40
+ */
41
+ public init(_ implementation: any HybridHlsCacheSpec) {
42
+ self.__implementation = implementation
43
+ self.__cxxPart = .init()
44
+ /* no base class */
45
+ }
46
+
47
+ /**
48
+ * Get the actual `HybridHlsCacheSpec` instance this class wraps.
49
+ */
50
+ @inline(__always)
51
+ public func getHybridHlsCacheSpec() -> any HybridHlsCacheSpec {
52
+ return __implementation
53
+ }
54
+
55
+ /**
56
+ * Casts this instance to a retained unsafe raw pointer.
57
+ * This acquires one additional strong reference on the object!
58
+ */
59
+ public func toUnsafe() -> UnsafeMutableRawPointer {
60
+ return Unmanaged.passRetained(self).toOpaque()
61
+ }
62
+
63
+ /**
64
+ * Casts an unsafe pointer to a `HybridHlsCacheSpec_cxx`.
65
+ * The pointer has to be a retained opaque `Unmanaged<HybridHlsCacheSpec_cxx>`.
66
+ * This removes one strong reference from the object!
67
+ */
68
+ public class func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> HybridHlsCacheSpec_cxx {
69
+ return Unmanaged<HybridHlsCacheSpec_cxx>.fromOpaque(pointer).takeRetainedValue()
70
+ }
71
+
72
+ /**
73
+ * Gets (or creates) the C++ part of this Hybrid Object.
74
+ * The C++ part is a `std::shared_ptr<HybridHlsCacheSpec>`.
75
+ */
76
+ public func getCxxPart() -> bridge.std__shared_ptr_HybridHlsCacheSpec_ {
77
+ let cachedCxxPart = self.__cxxPart.lock()
78
+ if Bool(fromCxx: cachedCxxPart) {
79
+ return cachedCxxPart
80
+ } else {
81
+ let newCxxPart = bridge.create_std__shared_ptr_HybridHlsCacheSpec_(self.toUnsafe())
82
+ __cxxPart = bridge.weakify_std__shared_ptr_HybridHlsCacheSpec_(newCxxPart)
83
+ return newCxxPart
84
+ }
85
+ }
86
+
87
+
88
+
89
+ /**
90
+ * Get the memory size of the Swift class (plus size of any other allocations)
91
+ * so the JS VM can properly track it and garbage-collect the JS object if needed.
92
+ */
93
+ @inline(__always)
94
+ public var memorySize: Int {
95
+ return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
96
+ }
97
+
98
+ /**
99
+ * Compares this object with the given [other] object for reference equality.
100
+ */
101
+ @inline(__always)
102
+ public func equals(other: HybridHlsCacheSpec_cxx) -> Bool {
103
+ return self.__implementation === other.__implementation
104
+ }
105
+
106
+ /**
107
+ * Call dispose() on the Swift class.
108
+ * This _may_ be called manually from JS.
109
+ */
110
+ @inline(__always)
111
+ public func dispose() {
112
+ self.__implementation.dispose()
113
+ }
114
+
115
+ /**
116
+ * Call toString() on the Swift class.
117
+ */
118
+ @inline(__always)
119
+ public func toString() -> String {
120
+ return self.__implementation.toString()
121
+ }
122
+
123
+ // Properties
124
+
125
+
126
+ // Methods
127
+ @inline(__always)
128
+ public final func startServer(port: bridge.std__optional_double_, maxCacheSize: bridge.std__optional_double_, headOnlyCache: bridge.std__optional_bool_) -> bridge.Result_void_ {
129
+ do {
130
+ try self.__implementation.startServer(port: { () -> Double? in
131
+ if bridge.has_value_std__optional_double_(port) {
132
+ let __unwrapped = bridge.get_std__optional_double_(port)
133
+ return __unwrapped
134
+ } else {
135
+ return nil
136
+ }
137
+ }(), maxCacheSize: { () -> Double? in
138
+ if bridge.has_value_std__optional_double_(maxCacheSize) {
139
+ let __unwrapped = bridge.get_std__optional_double_(maxCacheSize)
140
+ return __unwrapped
141
+ } else {
142
+ return nil
143
+ }
144
+ }(), headOnlyCache: { () -> Bool? in
145
+ if bridge.has_value_std__optional_bool_(headOnlyCache) {
146
+ let __unwrapped = bridge.get_std__optional_bool_(headOnlyCache)
147
+ return __unwrapped
148
+ } else {
149
+ return nil
150
+ }
151
+ }())
152
+ return bridge.create_Result_void_()
153
+ } catch (let __error) {
154
+ let __exceptionPtr = __error.toCpp()
155
+ return bridge.create_Result_void_(__exceptionPtr)
156
+ }
157
+ }
158
+
159
+ @inline(__always)
160
+ public final func convertUrl(url: std.string, isCacheable: bridge.std__optional_bool_) -> bridge.Result_std__string_ {
161
+ do {
162
+ let __result = try self.__implementation.convertUrl(url: String(url), isCacheable: { () -> Bool? in
163
+ if bridge.has_value_std__optional_bool_(isCacheable) {
164
+ let __unwrapped = bridge.get_std__optional_bool_(isCacheable)
165
+ return __unwrapped
166
+ } else {
167
+ return nil
168
+ }
169
+ }())
170
+ let __resultCpp = std.string(__result)
171
+ return bridge.create_Result_std__string_(__resultCpp)
172
+ } catch (let __error) {
173
+ let __exceptionPtr = __error.toCpp()
174
+ return bridge.create_Result_std__string_(__exceptionPtr)
175
+ }
176
+ }
177
+
178
+ @inline(__always)
179
+ public final func clearCache() -> bridge.Result_std__shared_ptr_Promise_void___ {
180
+ do {
181
+ let __result = try self.__implementation.clearCache()
182
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in
183
+ let __promise = bridge.create_std__shared_ptr_Promise_void__()
184
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise)
185
+ __result
186
+ .then({ __result in __promiseHolder.resolve() })
187
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
188
+ return __promise
189
+ }()
190
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp)
191
+ } catch (let __error) {
192
+ let __exceptionPtr = __error.toCpp()
193
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr)
194
+ }
195
+ }
196
+
197
+ @inline(__always)
198
+ public final func prefetch(url: std.string, segmentCount: bridge.std__optional_double_) -> bridge.Result_std__string_ {
199
+ do {
200
+ let __result = try self.__implementation.prefetch(url: String(url), segmentCount: { () -> Double? in
201
+ if bridge.has_value_std__optional_double_(segmentCount) {
202
+ let __unwrapped = bridge.get_std__optional_double_(segmentCount)
203
+ return __unwrapped
204
+ } else {
205
+ return nil
206
+ }
207
+ }())
208
+ let __resultCpp = std.string(__result)
209
+ return bridge.create_Result_std__string_(__resultCpp)
210
+ } catch (let __error) {
211
+ let __exceptionPtr = __error.toCpp()
212
+ return bridge.create_Result_std__string_(__exceptionPtr)
213
+ }
214
+ }
215
+
216
+ @inline(__always)
217
+ public final func cancelPrefetch(taskId: std.string) -> bridge.Result_void_ {
218
+ do {
219
+ try self.__implementation.cancelPrefetch(taskId: String(taskId))
220
+ return bridge.create_Result_void_()
221
+ } catch (let __error) {
222
+ let __exceptionPtr = __error.toCpp()
223
+ return bridge.create_Result_void_(__exceptionPtr)
224
+ }
225
+ }
226
+
227
+ @inline(__always)
228
+ public final func cancelAllPrefetch() -> bridge.Result_void_ {
229
+ do {
230
+ try self.__implementation.cancelAllPrefetch()
231
+ return bridge.create_Result_void_()
232
+ } catch (let __error) {
233
+ let __exceptionPtr = __error.toCpp()
234
+ return bridge.create_Result_void_(__exceptionPtr)
235
+ }
236
+ }
237
+ }
@@ -0,0 +1,26 @@
1
+ ///
2
+ /// HybridHlsCacheSpec.cpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #include "HybridHlsCacheSpec.hpp"
9
+
10
+ namespace margelo::nitro::hlscache {
11
+
12
+ void HybridHlsCacheSpec::loadHybridMethods() {
13
+ // load base methods/properties
14
+ HybridObject::loadHybridMethods();
15
+ // load custom methods/properties
16
+ registerHybrids(this, [](Prototype& prototype) {
17
+ prototype.registerHybridMethod("startServer", &HybridHlsCacheSpec::startServer);
18
+ prototype.registerHybridMethod("convertUrl", &HybridHlsCacheSpec::convertUrl);
19
+ prototype.registerHybridMethod("clearCache", &HybridHlsCacheSpec::clearCache);
20
+ prototype.registerHybridMethod("prefetch", &HybridHlsCacheSpec::prefetch);
21
+ prototype.registerHybridMethod("cancelPrefetch", &HybridHlsCacheSpec::cancelPrefetch);
22
+ prototype.registerHybridMethod("cancelAllPrefetch", &HybridHlsCacheSpec::cancelAllPrefetch);
23
+ });
24
+ }
25
+
26
+ } // namespace margelo::nitro::hlscache
@@ -0,0 +1,69 @@
1
+ ///
2
+ /// HybridHlsCacheSpec.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/HybridObject.hpp>)
11
+ #include <NitroModules/HybridObject.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+
16
+
17
+
18
+ #include <optional>
19
+ #include <string>
20
+ #include <NitroModules/Promise.hpp>
21
+
22
+ namespace margelo::nitro::hlscache {
23
+
24
+ using namespace margelo::nitro;
25
+
26
+ /**
27
+ * An abstract base class for `HlsCache`
28
+ * Inherit this class to create instances of `HybridHlsCacheSpec` in C++.
29
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
30
+ * @example
31
+ * ```cpp
32
+ * class HybridHlsCache: public HybridHlsCacheSpec {
33
+ * public:
34
+ * HybridHlsCache(...): HybridObject(TAG) { ... }
35
+ * // ...
36
+ * };
37
+ * ```
38
+ */
39
+ class HybridHlsCacheSpec: public virtual HybridObject {
40
+ public:
41
+ // Constructor
42
+ explicit HybridHlsCacheSpec(): HybridObject(TAG) { }
43
+
44
+ // Destructor
45
+ ~HybridHlsCacheSpec() override = default;
46
+
47
+ public:
48
+ // Properties
49
+
50
+
51
+ public:
52
+ // Methods
53
+ virtual void startServer(std::optional<double> port, std::optional<double> maxCacheSize, std::optional<bool> headOnlyCache) = 0;
54
+ virtual std::string convertUrl(const std::string& url, std::optional<bool> isCacheable) = 0;
55
+ virtual std::shared_ptr<Promise<void>> clearCache() = 0;
56
+ virtual std::string prefetch(const std::string& url, std::optional<double> segmentCount) = 0;
57
+ virtual void cancelPrefetch(const std::string& taskId) = 0;
58
+ virtual void cancelAllPrefetch() = 0;
59
+
60
+ protected:
61
+ // Hybrid Setup
62
+ void loadHybridMethods() override;
63
+
64
+ protected:
65
+ // Tag for logging
66
+ static constexpr auto TAG = "HlsCache";
67
+ };
68
+
69
+ } // namespace margelo::nitro::hlscache
package/package.json ADDED
@@ -0,0 +1,174 @@
1
+ {
2
+ "name": "react-native-hls-cache",
3
+ "version": "0.1.0",
4
+ "description": "HSL",
5
+ "main": "./lib/module/index.js",
6
+ "types": "./lib/typescript/src/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "source": "./src/index.tsx",
10
+ "types": "./lib/typescript/src/index.d.ts",
11
+ "default": "./lib/module/index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "src",
17
+ "lib",
18
+ "android",
19
+ "ios",
20
+ "cpp",
21
+ "nitrogen",
22
+ "nitro.json",
23
+ "*.podspec",
24
+ "react-native.config.js",
25
+ "!ios/build",
26
+ "!android/build",
27
+ "!android/gradle",
28
+ "!android/gradlew",
29
+ "!android/gradlew.bat",
30
+ "!android/local.properties",
31
+ "!**/__tests__",
32
+ "!**/__fixtures__",
33
+ "!**/__mocks__",
34
+ "!**/.*"
35
+ ],
36
+ "scripts": {
37
+ "example": "yarn workspace react-native-hls-cache-example",
38
+ "clean": "del-cli lib",
39
+ "prepare": "bob build",
40
+ "nitrogen": "nitrogen",
41
+ "typecheck": "tsc",
42
+ "release": "release-it --only-version",
43
+ "test": "jest",
44
+ "lint": "eslint \"**/*.{js,ts,tsx}\""
45
+ },
46
+ "keywords": [
47
+ "react-native",
48
+ "ios",
49
+ "android"
50
+ ],
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "git+https://github.com/chanphiromsok/react-native-hls-cache.git"
54
+ },
55
+ "author": "Rom <chanphiromsok2109@gmail.com> (https://github.com/chanphiromsok)",
56
+ "license": "MIT",
57
+ "bugs": {
58
+ "url": "https://github.com/chanphiromsok/react-native-hls-cache/issues"
59
+ },
60
+ "homepage": "https://github.com/chanphiromsok/react-native-hls-cache#readme",
61
+ "publishConfig": {
62
+ "registry": "https://registry.npmjs.org/"
63
+ },
64
+ "devDependencies": {
65
+ "@commitlint/config-conventional": "^19.8.1",
66
+ "@eslint/compat": "^1.3.2",
67
+ "@eslint/eslintrc": "^3.3.1",
68
+ "@eslint/js": "^9.35.0",
69
+ "@react-native/babel-preset": "0.83.0",
70
+ "@react-native/eslint-config": "0.83.0",
71
+ "@release-it/conventional-changelog": "^10.0.1",
72
+ "@types/jest": "^29.5.14",
73
+ "@types/react": "^19.1.12",
74
+ "commitlint": "^19.8.1",
75
+ "del-cli": "^6.0.0",
76
+ "eslint": "^9.35.0",
77
+ "eslint-config-prettier": "^10.1.8",
78
+ "eslint-plugin-prettier": "^5.5.4",
79
+ "jest": "^29.7.0",
80
+ "lefthook": "^2.0.3",
81
+ "nitrogen": "^0.35.2",
82
+ "prettier": "^2.8.8",
83
+ "react": "19.2.0",
84
+ "react-native": "0.83.2",
85
+ "react-native-builder-bob": "^0.40.18",
86
+ "react-native-nitro-modules": "^0.35.2",
87
+ "release-it": "^19.0.4",
88
+ "turbo": "^2.5.6",
89
+ "typescript": "^5.9.2"
90
+ },
91
+ "peerDependencies": {
92
+ "react": "*",
93
+ "react-native": "*",
94
+ "react-native-nitro-modules": "^0.35.2"
95
+ },
96
+ "workspaces": [
97
+ "example"
98
+ ],
99
+ "packageManager": "yarn@4.11.0",
100
+ "react-native-builder-bob": {
101
+ "source": "src",
102
+ "output": "lib",
103
+ "targets": [
104
+ [
105
+ "custom",
106
+ {
107
+ "script": "nitrogen",
108
+ "clean": "nitrogen/"
109
+ }
110
+ ],
111
+ [
112
+ "module",
113
+ {
114
+ "esm": true
115
+ }
116
+ ],
117
+ [
118
+ "typescript",
119
+ {
120
+ "project": "tsconfig.build.json"
121
+ }
122
+ ]
123
+ ]
124
+ },
125
+ "commitlint": {
126
+ "extends": [
127
+ "@commitlint/config-conventional"
128
+ ]
129
+ },
130
+ "release-it": {
131
+ "git": {
132
+ "commitMessage": "chore: release ${version}",
133
+ "tagName": "v${version}"
134
+ },
135
+ "npm": {
136
+ "publish": true
137
+ },
138
+ "github": {
139
+ "release": true
140
+ },
141
+ "plugins": {
142
+ "@release-it/conventional-changelog": {
143
+ "preset": {
144
+ "name": "angular"
145
+ }
146
+ }
147
+ }
148
+ },
149
+ "jest": {
150
+ "preset": "react-native",
151
+ "modulePathIgnorePatterns": [
152
+ "<rootDir>/example/node_modules",
153
+ "<rootDir>/lib/"
154
+ ]
155
+ },
156
+ "prettier": {
157
+ "quoteProps": "consistent",
158
+ "singleQuote": true,
159
+ "tabWidth": 2,
160
+ "trailingComma": "es5",
161
+ "useTabs": false
162
+ },
163
+ "create-react-native-library": {
164
+ "type": "nitro-module",
165
+ "languages": "kotlin-swift",
166
+ "tools": [
167
+ "lefthook",
168
+ "release-it",
169
+ "jest",
170
+ "eslint"
171
+ ],
172
+ "version": "0.57.2"
173
+ }
174
+ }
@@ -0,0 +1,15 @@
1
+ import type { HybridObject } from 'react-native-nitro-modules';
2
+
3
+ export interface HlsCache
4
+ extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
5
+ startServer(
6
+ port?: number,
7
+ maxCacheSize?: number,
8
+ headOnlyCache?: boolean
9
+ ): void;
10
+ convertUrl(url: string, isCacheable?: boolean): string;
11
+ clearCache(): Promise<void>;
12
+ prefetch(url: string, segmentCount?: number): string;
13
+ cancelPrefetch(taskId: string): void;
14
+ cancelAllPrefetch(): void;
15
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,75 @@
1
+ import { NitroModules } from 'react-native-nitro-modules';
2
+ import { Platform } from 'react-native';
3
+ import type { HlsCache } from './HlsCache.nitro';
4
+
5
+ let _instance: HlsCache | null = null;
6
+
7
+ function getInstance(): HlsCache {
8
+ if (_instance == null) {
9
+ _instance = NitroModules.createHybridObject<HlsCache>('HlsCache');
10
+ }
11
+ return _instance;
12
+ }
13
+
14
+ /**
15
+ * Starts the local HLS proxy server.
16
+ *
17
+ * @param port - Local port to bind. Defaults to 9000.
18
+ * @param maxCacheSize - Max disk cache size in bytes. Defaults to 1 GB.
19
+ * @param headOnlyCache - If true, only caches the first ~3 segments per video (optimised for vertical feeds).
20
+ */
21
+ export function startServer(
22
+ port?: number,
23
+ maxCacheSize?: number,
24
+ headOnlyCache?: boolean
25
+ ): void {
26
+ if (Platform.OS !== 'ios') return;
27
+ getInstance().startServer(port, maxCacheSize, headOnlyCache);
28
+ }
29
+
30
+ /**
31
+ * Rewrites a remote URL to route through the local proxy (iOS only).
32
+ * Returns the original URL unchanged on Android/Web or when the server is not running.
33
+ *
34
+ * @param url - Remote HLS URL (e.g. `https://cdn.example.com/video.m3u8`).
35
+ * @param isCacheable - Set to `false` to bypass the proxy. Defaults to `true`.
36
+ */
37
+ export function convertUrl(url: string, isCacheable?: boolean): string {
38
+ if (Platform.OS !== 'ios') return url;
39
+ return getInstance().convertUrl(url, isCacheable);
40
+ }
41
+
42
+ /**
43
+ * Purges all cached video files from disk.
44
+ */
45
+ export function clearCache(): Promise<void> {
46
+ if (Platform.OS !== 'ios') return Promise.resolve();
47
+ return getInstance().clearCache();
48
+ }
49
+
50
+ /**
51
+ * Prefetches an HLS stream into the local cache in the background.
52
+ * Downloads the manifest + first `segmentCount` segments (default: 3).
53
+ *
54
+ * @returns A taskId that can be passed to `cancelPrefetch` to stop the download.
55
+ */
56
+ export function prefetch(url: string, segmentCount?: number): string {
57
+ if (Platform.OS !== 'ios') return '';
58
+ return getInstance().prefetch(url, segmentCount);
59
+ }
60
+
61
+ /**
62
+ * Cancels a specific prefetch task by its taskId.
63
+ */
64
+ export function cancelPrefetch(taskId: string): void {
65
+ if (Platform.OS !== 'ios') return;
66
+ getInstance().cancelPrefetch(taskId);
67
+ }
68
+
69
+ /**
70
+ * Cancels all active prefetch tasks.
71
+ */
72
+ export function cancelAllPrefetch(): void {
73
+ if (Platform.OS !== 'ios') return;
74
+ getInstance().cancelAllPrefetch();
75
+ }