cbl-ionic 1.0.0 → 1.0.2
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.
- package/CblIonic.podspec +2 -2
- package/README.md +5 -0
- package/android/build.gradle +7 -2
- package/android/src/main/java/cbl/js/kotiln/URLEndpointListenerManager.kt +143 -0
- package/android/src/main/java/com/couchbase/ionic/CblIonicPluginPlugin.kt +164 -18
- package/android/src/main/java/com/couchbase/ionic/PluginHelper.kt +27 -38
- package/android/src/main/java/com/couchbase/ionic/QueryHelper.kt +14 -4
- package/android/src/main/java/com/couchbase/ionic/ReplicatorHelper.kt +25 -7
- package/dist/esm/cblite-js/cblite/core-types.d.ts +143 -4
- package/dist/esm/cblite-js/cblite/src/collection-config.d.ts +34 -0
- package/dist/esm/cblite-js/cblite/src/collection-config.js +40 -0
- package/dist/esm/cblite-js/cblite/src/collection-config.js.map +1 -1
- package/dist/esm/cblite-js/cblite/src/collection.d.ts +1 -2
- package/dist/esm/cblite-js/cblite/src/document.d.ts +3 -4
- package/dist/esm/cblite-js/cblite/src/document.js +5 -5
- package/dist/esm/cblite-js/cblite/src/document.js.map +1 -1
- package/dist/esm/cblite-js/cblite/src/index.d.ts +1 -0
- package/dist/esm/cblite-js/cblite/src/index.js +1 -0
- package/dist/esm/cblite-js/cblite/src/index.js.map +1 -1
- package/dist/esm/cblite-js/cblite/src/query.js +2 -7
- package/dist/esm/cblite-js/cblite/src/query.js.map +1 -1
- package/dist/esm/cblite-js/cblite/src/replicator-configuration.d.ts +1 -0
- package/dist/esm/cblite-js/cblite/src/replicator-configuration.js +20 -0
- package/dist/esm/cblite-js/cblite/src/replicator-configuration.js.map +1 -1
- package/dist/esm/cblite-js/cblite/src/replicator.js +2 -2
- package/dist/esm/cblite-js/cblite/src/replicator.js.map +1 -1
- package/dist/esm/cblite-js/cblite/src/url-endpoint-listener.d.ts +84 -0
- package/dist/esm/cblite-js/cblite/src/url-endpoint-listener.js +109 -0
- package/dist/esm/cblite-js/cblite/src/url-endpoint-listener.js.map +1 -0
- package/dist/esm/couchbase-lite/capacitor-engine.d.ts +13 -1
- package/dist/esm/couchbase-lite/capacitor-engine.js +19 -1
- package/dist/esm/couchbase-lite/capacitor-engine.js.map +1 -1
- package/dist/plugin.cjs.js +196 -15
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +196 -15
- package/dist/plugin.js.map +1 -1
- package/ios/Plugin/CblIonicPlugin.swift +115 -4
- package/ios/Plugin/CblIonicPluginPlugin.m +7 -0
- package/ios/Plugin/cbl-js-swift/CollectionManager.swift +22 -0
- package/ios/Plugin/cbl-js-swift/MapHelper.swift +0 -23
- package/ios/Plugin/cbl-js-swift/ReplicatorHelper.swift +116 -3
- package/ios/Plugin/cbl-js-swift/URLEndpointListenerManager.swift +154 -0
- package/package.json +5 -4
|
@@ -785,10 +785,19 @@ public class CblIonicPluginPlugin: CAPPlugin {
|
|
|
785
785
|
}
|
|
786
786
|
|
|
787
787
|
var data: [String: Any] = [:]
|
|
788
|
-
let
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
788
|
+
let documentJson = doc.toJSON()
|
|
789
|
+
if !documentJson.isEmpty {
|
|
790
|
+
guard let jsonData = documentJson.data(using: .utf8),
|
|
791
|
+
let jsonDict = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] else {
|
|
792
|
+
call.reject("DOCUMENT_ERROR", "Failed to parse document JSON", nil)
|
|
793
|
+
return
|
|
794
|
+
}
|
|
795
|
+
data["_data"] = jsonDict
|
|
796
|
+
} else {
|
|
797
|
+
data["_data"] = [:]
|
|
798
|
+
}
|
|
799
|
+
data["_id"] = docId
|
|
800
|
+
data["_sequence"] = doc.sequence
|
|
792
801
|
data["_revId"] = doc.revisionID
|
|
793
802
|
DispatchQueue.main.async {
|
|
794
803
|
call.resolve(data)
|
|
@@ -1582,4 +1591,106 @@ public class CblIonicPluginPlugin: CAPPlugin {
|
|
|
1582
1591
|
}
|
|
1583
1592
|
}
|
|
1584
1593
|
}
|
|
1594
|
+
|
|
1595
|
+
// MARK: - URL Endpoint Listener
|
|
1596
|
+
|
|
1597
|
+
@objc func URLEndpointListener_createListener(_ call: CAPPluginCall) {
|
|
1598
|
+
guard let collectionsArray = call.getArray("collections") as? [[String: String]],
|
|
1599
|
+
let port = call.getInt("port"),
|
|
1600
|
+
let networkInterface = call.getString("networkInterface")
|
|
1601
|
+
else {
|
|
1602
|
+
call.reject("Missing required parameters")
|
|
1603
|
+
return
|
|
1604
|
+
}
|
|
1605
|
+
let authenticatorConfig = call.getObject("authenticatorConfig") as? [String: Any]
|
|
1606
|
+
let tlsIdentityConfig = call.getObject("tlsIdentityConfig") as? [String: Any]
|
|
1607
|
+
let disableTLS = call.getBool("disableTLS")
|
|
1608
|
+
let enableDeltaSync = call.getBool("enableDeltaSync")
|
|
1609
|
+
var collections: [Collection] = []
|
|
1610
|
+
do {
|
|
1611
|
+
for dict in collectionsArray {
|
|
1612
|
+
guard let dbName = dict["databaseName"],
|
|
1613
|
+
let scopeName = dict["scopeName"],
|
|
1614
|
+
let collectionName = dict["name"],
|
|
1615
|
+
let db = DatabaseManager.shared.getDatabase(dbName),
|
|
1616
|
+
let scope = try? db.scope(name: scopeName),
|
|
1617
|
+
let collection = try? scope.collection(name: collectionName) else {
|
|
1618
|
+
call.reject("Invalid collection parameters")
|
|
1619
|
+
return
|
|
1620
|
+
}
|
|
1621
|
+
collections.append(collection)
|
|
1622
|
+
}
|
|
1623
|
+
let listenerId = try URLEndpointListenerManager.shared.createListener(
|
|
1624
|
+
collections: collections,
|
|
1625
|
+
port: UInt16(port),
|
|
1626
|
+
networkInterface: networkInterface,
|
|
1627
|
+
disableTLS: disableTLS,
|
|
1628
|
+
enableDeltaSync: enableDeltaSync,
|
|
1629
|
+
authenticatorConfig: authenticatorConfig,
|
|
1630
|
+
tlsIdentityConfig: tlsIdentityConfig
|
|
1631
|
+
)
|
|
1632
|
+
call.resolve(["listenerId": listenerId])
|
|
1633
|
+
} catch {
|
|
1634
|
+
call.reject("Failed to create listener: \(error.localizedDescription)")
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
@objc func URLEndpointListener_startListener(_ call: CAPPluginCall) {
|
|
1639
|
+
guard let listenerId = call.getString("listenerId") else {
|
|
1640
|
+
call.reject("Missing required parameter: 'listenerId'")
|
|
1641
|
+
return
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1644
|
+
do {
|
|
1645
|
+
try URLEndpointListenerManager.shared.startListener(listenerId: listenerId)
|
|
1646
|
+
call.resolve()
|
|
1647
|
+
} catch {
|
|
1648
|
+
call.reject("Failed to start listener: \(error.localizedDescription)")
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
@objc func URLEndpointListener_stopListener(_ call: CAPPluginCall) {
|
|
1652
|
+
|
|
1653
|
+
guard let listenerId = call.getString("listenerId") else {
|
|
1654
|
+
call.reject("Missing required parameter: 'listenerId'")
|
|
1655
|
+
return
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
do {
|
|
1659
|
+
try URLEndpointListenerManager.shared.stopListener(listenerId: listenerId)
|
|
1660
|
+
call.resolve()
|
|
1661
|
+
} catch {
|
|
1662
|
+
call.reject("Failed to stop listener: \(error.localizedDescription)")
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
@objc func URLEndpointListener_getStatus(_ call: CAPPluginCall) {
|
|
1666
|
+
guard let listenerId = call.getString("listenerId") else {
|
|
1667
|
+
call.reject("Missing required parameter: 'listenerId'")
|
|
1668
|
+
return
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
do {
|
|
1672
|
+
let status = try URLEndpointListenerManager.shared.getListenerStatus(listenerId: listenerId)
|
|
1673
|
+
|
|
1674
|
+
let result: [String: Any] = [
|
|
1675
|
+
"connectionsCount": status.connectionCount,
|
|
1676
|
+
"activeConnectionCount": status.activeConnectionCount
|
|
1677
|
+
]
|
|
1678
|
+
call.resolve(result)
|
|
1679
|
+
} catch {
|
|
1680
|
+
call.reject("Failed to get listener status: \(error.localizedDescription)")
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
@objc func URLEndpointListener_deleteIdentity(_ call: CAPPluginCall) {
|
|
1684
|
+
guard let label = call.getString("label") else {
|
|
1685
|
+
call.reject("Missing required parameter: 'label'")
|
|
1686
|
+
return
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
do {
|
|
1690
|
+
try URLEndpointListenerManager.shared.deleteIdentity(label: label)
|
|
1691
|
+
call.resolve()
|
|
1692
|
+
} catch {
|
|
1693
|
+
call.reject("Failed to delete identity: \(error.localizedDescription)")
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1585
1696
|
}
|
|
@@ -83,4 +83,11 @@ CAP_PLUGIN(CblIonicPlugin, "CblIonicPlugin",
|
|
|
83
83
|
CAP_PLUGIN_METHOD(replicator_RemoveChangeListener, CAPPluginReturnPromise);
|
|
84
84
|
CAP_PLUGIN_METHOD(replicator_AddDocumentChangeListener, CAPPluginReturnCallback);
|
|
85
85
|
CAP_PLUGIN_METHOD(replicator_Cleanup, CAPPluginReturnPromise);
|
|
86
|
+
|
|
87
|
+
// URL Endpoints functions
|
|
88
|
+
CAP_PLUGIN_METHOD(URLEndpointListener_createListener, CAPPluginReturnPromise);
|
|
89
|
+
CAP_PLUGIN_METHOD(URLEndpointListener_startListener, CAPPluginReturnPromise);
|
|
90
|
+
CAP_PLUGIN_METHOD(URLEndpointListener_stopListener, CAPPluginReturnPromise);
|
|
91
|
+
CAP_PLUGIN_METHOD(URLEndpointListener_getStatus, CAPPluginReturnPromise);
|
|
92
|
+
CAP_PLUGIN_METHOD(URLEndpointListener_deleteIdentity, CAPPluginReturnPromise);
|
|
86
93
|
)
|
|
@@ -24,6 +24,28 @@ public struct CollectionDto: Codable {
|
|
|
24
24
|
public struct ConfigDto: Codable {
|
|
25
25
|
let channels: [String]
|
|
26
26
|
let documentIds: [String]
|
|
27
|
+
let pushFilter: String?
|
|
28
|
+
let pullFilter: String?
|
|
29
|
+
|
|
30
|
+
enum CodingKeys: String, CodingKey {
|
|
31
|
+
case channels, documentIds, pushFilter, pullFilter
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public init(from decoder: Decoder) throws {
|
|
35
|
+
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
36
|
+
channels = try container.decodeIfPresent([String].self, forKey: .channels) ?? []
|
|
37
|
+
documentIds = try container.decodeIfPresent([String].self, forKey: .documentIds) ?? []
|
|
38
|
+
pushFilter = try container.decodeIfPresent(String.self, forKey: .pushFilter)
|
|
39
|
+
pullFilter = try container.decodeIfPresent(String.self, forKey: .pullFilter)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public func encode(to encoder: Encoder) throws {
|
|
43
|
+
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
44
|
+
try container.encode(channels, forKey: .channels)
|
|
45
|
+
try container.encode(documentIds, forKey: .documentIds)
|
|
46
|
+
try container.encodeIfPresent(pushFilter, forKey: .pushFilter)
|
|
47
|
+
try container.encodeIfPresent(pullFilter, forKey: .pullFilter)
|
|
48
|
+
}
|
|
27
49
|
}
|
|
28
50
|
|
|
29
51
|
public struct DocumentDto: Codable {
|
|
@@ -7,29 +7,6 @@ import CouchbaseLiteSwift
|
|
|
7
7
|
|
|
8
8
|
public struct MapHelper {
|
|
9
9
|
|
|
10
|
-
public static func documentToMap(_ document: Document) ->
|
|
11
|
-
[String: Any] {
|
|
12
|
-
var docMap = [String: Any]()
|
|
13
|
-
let documentAsMap = document.toDictionary()
|
|
14
|
-
for (key, value) in documentAsMap {
|
|
15
|
-
if let blobEntry = value as? Blob {
|
|
16
|
-
var properties = blobEntry.properties
|
|
17
|
-
if let data = blobEntry.content {
|
|
18
|
-
var content: [Int] = []
|
|
19
|
-
data.regions.forEach { region in
|
|
20
|
-
for byte in region {
|
|
21
|
-
content.append(Int(byte))
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
docMap[key] = properties.merging(["raw": content]) { (_, new) in new }
|
|
25
|
-
}
|
|
26
|
-
} else {
|
|
27
|
-
docMap[key] = value
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return docMap
|
|
31
|
-
}
|
|
32
|
-
|
|
33
10
|
public static func resultToMap(_ result: Result,
|
|
34
11
|
databaseName: String) ->
|
|
35
12
|
[String: Any] {
|
|
@@ -8,6 +8,97 @@ import JavaScriptCore
|
|
|
8
8
|
import CouchbaseLiteSwift
|
|
9
9
|
|
|
10
10
|
public struct ReplicatorHelper {
|
|
11
|
+
private static let jsContextQueue = DispatchQueue(label: "com.couchbase.jscontext")
|
|
12
|
+
|
|
13
|
+
private static let jsContext: JSContext = {
|
|
14
|
+
let context = JSContext()!
|
|
15
|
+
|
|
16
|
+
// Setup console.log for debugging
|
|
17
|
+
let logFunction: @convention(block) (String) -> Void = { message in
|
|
18
|
+
print("JSFilter: \(message)")
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
context.setObject(logFunction, forKeyedSubscript: "log" as NSString)
|
|
22
|
+
context.evaluateScript("var console = { log: log };")
|
|
23
|
+
|
|
24
|
+
// Handle JavaScript exceptions
|
|
25
|
+
context.exceptionHandler = { context, exception in
|
|
26
|
+
if let exc = exception {
|
|
27
|
+
print("JSFilter Exception: \(exc.toString() ?? "unknown error")")
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return context
|
|
32
|
+
}()
|
|
33
|
+
|
|
34
|
+
private static func evaluateFilter(_ filterFunction: String, document: Document, flags: DocumentFlags) -> Bool {
|
|
35
|
+
return jsContextQueue.sync {
|
|
36
|
+
autoreleasepool {
|
|
37
|
+
// Convert document to dictionary
|
|
38
|
+
var docDict = document.toDictionary()
|
|
39
|
+
docDict["id"] = document.id
|
|
40
|
+
|
|
41
|
+
// Create flags array
|
|
42
|
+
let flagsArray: [String] = [
|
|
43
|
+
flags.contains(.deleted) ? "DELETED" : nil,
|
|
44
|
+
flags.contains(.accessRemoved) ? "ACCESS_REMOVED" : nil
|
|
45
|
+
].compactMap { $0 }
|
|
46
|
+
|
|
47
|
+
// Set objects directly in JSContext
|
|
48
|
+
jsContext.setObject(docDict, forKeyedSubscript: "currentDocument" as NSString)
|
|
49
|
+
jsContext.setObject(flagsArray, forKeyedSubscript: "currentFlags" as NSString)
|
|
50
|
+
jsContext.setObject(filterFunction, forKeyedSubscript: "filterFunctionString" as NSString)
|
|
51
|
+
|
|
52
|
+
// Create and execute the filter script
|
|
53
|
+
let script = """
|
|
54
|
+
(function() {
|
|
55
|
+
try {
|
|
56
|
+
// Enum for flags
|
|
57
|
+
const ReplicatedDocumentFlag = {
|
|
58
|
+
DELETED: 'DELETED',
|
|
59
|
+
ACCESS_REMOVED: 'ACCESS_REMOVED'
|
|
60
|
+
};
|
|
61
|
+
// Create the filter function from string
|
|
62
|
+
const filterFunc = eval('(' + filterFunctionString + ')');
|
|
63
|
+
|
|
64
|
+
const result = filterFunc(currentDocument, currentFlags);
|
|
65
|
+
// Ensure we return a boolean
|
|
66
|
+
return !!result;
|
|
67
|
+
} catch (e) {
|
|
68
|
+
console.log('Filter error: ' + e.toString());
|
|
69
|
+
console.log('Stack: ' + (e.stack || 'No stack trace'));
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
})()
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
// Execute the script
|
|
76
|
+
guard let result = jsContext.evaluateScript(script) else {
|
|
77
|
+
print("JSFilter: Script evaluation returned nil")
|
|
78
|
+
return false
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Clear references
|
|
82
|
+
jsContext.setObject(nil, forKeyedSubscript: "currentDocument" as NSString)
|
|
83
|
+
jsContext.setObject(nil, forKeyedSubscript: "currentFlags" as NSString)
|
|
84
|
+
jsContext.setObject(nil, forKeyedSubscript: "filterFunctionString" as NSString)
|
|
85
|
+
|
|
86
|
+
// Convert result to boolean
|
|
87
|
+
return result.toBool()
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Create a ReplicationFilter from a JavaScript function string
|
|
93
|
+
private static func createFilter(from functionString: String?) -> ReplicationFilter? {
|
|
94
|
+
guard let functionString = functionString, !functionString.isEmpty else {
|
|
95
|
+
return nil
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return { (document, flags) -> Bool in
|
|
99
|
+
return evaluateFilter(functionString, document: document, flags: flags)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
11
102
|
|
|
12
103
|
public static func replicatorConfigFromJson(_ data: [String: Any], collectionConfiguration: [CollectionConfigItem]) throws -> ReplicatorConfiguration {
|
|
13
104
|
guard let target = data["target"] as? [String: Any],
|
|
@@ -20,7 +111,8 @@ public struct ReplicatorHelper {
|
|
|
20
111
|
let autoPurgeEnabled = data["autoPurgeEnabled"] as? Bool,
|
|
21
112
|
let heartbeat = data["heartbeat"] as? NSNumber,
|
|
22
113
|
let maxAttempts = data["maxAttempts"] as? NSNumber,
|
|
23
|
-
let maxAttemptWaitTime = data["maxAttemptWaitTime"] as? NSNumber
|
|
114
|
+
let maxAttemptWaitTime = data["maxAttemptWaitTime"] as? NSNumber,
|
|
115
|
+
let pinnedServerCertificate = data["pinnedServerCertificate"] as? String
|
|
24
116
|
else {
|
|
25
117
|
throw ReplicatorError.fatalError(message: "Invalid JSON data")
|
|
26
118
|
}
|
|
@@ -29,12 +121,23 @@ public struct ReplicatorHelper {
|
|
|
29
121
|
|
|
30
122
|
//set values from data
|
|
31
123
|
var replConfig = ReplicatorConfiguration(target: endpoint)
|
|
32
|
-
replConfig.acceptParentDomainCookie = acceptSelfSignedCerts
|
|
33
124
|
replConfig.acceptParentDomainCookie = acceptParentDomainCookies
|
|
34
125
|
replConfig.allowReplicatingInBackground = allowReplicationInBackground
|
|
126
|
+
replConfig.acceptOnlySelfSignedServerCertificate = acceptSelfSignedCerts
|
|
35
127
|
replConfig.continuous = continuous
|
|
36
128
|
replConfig.enableAutoPurge = autoPurgeEnabled
|
|
37
|
-
|
|
129
|
+
|
|
130
|
+
if !pinnedServerCertificate.isEmpty {
|
|
131
|
+
guard let certData = Data(base64Encoded: pinnedServerCertificate) else {
|
|
132
|
+
throw ReplicatorError.fatalError(message: "Invalid pinned server certificate")
|
|
133
|
+
}
|
|
134
|
+
guard let pinnedCert = SecCertificateCreateWithData(nil, certData as CFData) else {
|
|
135
|
+
throw ReplicatorError.fatalError(message: "Invalid pinned server certificate")
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
replConfig.pinnedServerCertificate = pinnedCert
|
|
139
|
+
}
|
|
140
|
+
|
|
38
141
|
replConfig.heartbeat = TimeInterval(exactly: heartbeat.int64Value) ?? 300
|
|
39
142
|
replConfig.maxAttemptWaitTime = TimeInterval(exactly: maxAttemptWaitTime.int64Value) ?? 0
|
|
40
143
|
replConfig.maxAttempts = maxAttempts.uintValue
|
|
@@ -94,6 +197,16 @@ public struct ReplicatorHelper {
|
|
|
94
197
|
if item.config.documentIds.count > 0 {
|
|
95
198
|
collectionConfig.documentIDs = item.config.documentIds
|
|
96
199
|
}
|
|
200
|
+
|
|
201
|
+
// Process push and pull filters
|
|
202
|
+
if let pushFilterStr = item.config.pushFilter, !pushFilterStr.isEmpty {
|
|
203
|
+
collectionConfig.pushFilter = createFilter(from: pushFilterStr)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if let pullFilterStr = item.config.pullFilter, !pullFilterStr.isEmpty {
|
|
207
|
+
collectionConfig.pullFilter = createFilter(from: pullFilterStr)
|
|
208
|
+
}
|
|
209
|
+
|
|
97
210
|
replicationConfig.addCollections(collections, config: collectionConfig)
|
|
98
211
|
}
|
|
99
212
|
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
//
|
|
2
|
+
// URLEndpointListenerManager.swift
|
|
3
|
+
// CbliteSwiftJsLib
|
|
4
|
+
//
|
|
5
|
+
|
|
6
|
+
import Foundation
|
|
7
|
+
import CouchbaseLiteSwift
|
|
8
|
+
|
|
9
|
+
enum URLEndpointListenerError: Error {
|
|
10
|
+
|
|
11
|
+
case unableToFindListener(listenerId: String)
|
|
12
|
+
case databaseNotFound(databaseName: String)
|
|
13
|
+
case wrongCertData(certBase64: String)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public class URLEndpointListenerManager {
|
|
17
|
+
private var listeners = [String: URLEndpointListener]()
|
|
18
|
+
|
|
19
|
+
// Singleton instance
|
|
20
|
+
public static let shared = URLEndpointListenerManager()
|
|
21
|
+
|
|
22
|
+
private init() {}
|
|
23
|
+
|
|
24
|
+
public func createListener(
|
|
25
|
+
collections: [Collection],
|
|
26
|
+
port: UInt16? = nil,
|
|
27
|
+
networkInterface: String? = nil,
|
|
28
|
+
disableTLS: Bool? = nil,
|
|
29
|
+
enableDeltaSync: Bool? = nil,
|
|
30
|
+
authenticatorConfig: [String: Any]? = nil,
|
|
31
|
+
tlsIdentityConfig: [String: Any]? = nil
|
|
32
|
+
) throws -> String {
|
|
33
|
+
var config = URLEndpointListenerConfiguration(collections: collections)
|
|
34
|
+
if let port = port {
|
|
35
|
+
config.port = port
|
|
36
|
+
}
|
|
37
|
+
if let networkInterface = networkInterface {
|
|
38
|
+
config.networkInterface = networkInterface
|
|
39
|
+
}
|
|
40
|
+
if let disableTLS = disableTLS {
|
|
41
|
+
config.disableTLS = disableTLS
|
|
42
|
+
}
|
|
43
|
+
if let enableDeltaSync = enableDeltaSync {
|
|
44
|
+
config.enableDeltaSync = enableDeltaSync
|
|
45
|
+
}
|
|
46
|
+
if let tlsConfig = tlsIdentityConfig {
|
|
47
|
+
let mode = tlsConfig["mode"] as? String ?? "selfSigned"
|
|
48
|
+
let label = tlsConfig["label"] as? String ?? UUID().uuidString
|
|
49
|
+
if mode == "selfSigned" {
|
|
50
|
+
var attrs = tlsConfig["attributes"] as? [String: String] ?? [:]
|
|
51
|
+
|
|
52
|
+
// If we have certAttrCommonName, use the constant key instead
|
|
53
|
+
if let commonName = attrs.removeValue(forKey: "certAttrCommonName") {
|
|
54
|
+
attrs[certAttrCommonName] = commonName
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let expiration: Date? = {
|
|
58
|
+
if let expStr = tlsConfig["expiration"] as? String {
|
|
59
|
+
let formatter = ISO8601DateFormatter()
|
|
60
|
+
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
|
61
|
+
return formatter.date(from: expStr)
|
|
62
|
+
}
|
|
63
|
+
return nil
|
|
64
|
+
}()
|
|
65
|
+
|
|
66
|
+
let identity = try TLSIdentity.createIdentity(
|
|
67
|
+
forServer: true,
|
|
68
|
+
attributes: attrs,
|
|
69
|
+
expiration: expiration,
|
|
70
|
+
label: label
|
|
71
|
+
)
|
|
72
|
+
config.tlsIdentity = identity
|
|
73
|
+
|
|
74
|
+
}
|
|
75
|
+
if mode == "imported" {
|
|
76
|
+
let certBase64 = tlsConfig["certBase64"] as? String ?? ""
|
|
77
|
+
let password = tlsConfig["password"] as? String
|
|
78
|
+
|
|
79
|
+
guard let certData = Data(base64Encoded: certBase64) else {
|
|
80
|
+
throw URLEndpointListenerError.wrongCertData(certBase64: certBase64)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let identity = try TLSIdentity.importIdentity(withData: certData, password: password, label: label)
|
|
84
|
+
config.tlsIdentity = identity
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
}
|
|
88
|
+
if let authenticator = Self.listenerAuthenticatorFromConfig(authenticatorConfig) {
|
|
89
|
+
config.authenticator = authenticator
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let listener = URLEndpointListener(config: config)
|
|
93
|
+
let listenerId = UUID().uuidString
|
|
94
|
+
listeners[listenerId] = listener
|
|
95
|
+
|
|
96
|
+
return listenerId
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public func startListener(listenerId: String) throws {
|
|
100
|
+
guard let listener = listeners[listenerId] else {
|
|
101
|
+
throw URLEndpointListenerError.unableToFindListener(listenerId: listenerId)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
try listener.start()
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public func stopListener(listenerId: String) throws {
|
|
108
|
+
guard let listener = listeners[listenerId] else {
|
|
109
|
+
throw URLEndpointListenerError.unableToFindListener(listenerId: listenerId)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
listener.stop()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public func getListenerStatus(listenerId: String) throws -> CouchbaseLiteSwift.URLEndpointListener.ConnectionStatus {
|
|
116
|
+
guard let listener = listeners[listenerId] else {
|
|
117
|
+
throw URLEndpointListenerError.unableToFindListener(listenerId: listenerId)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return listener.status
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public func getListenerUrls(listenerId: String) throws -> [String] {
|
|
124
|
+
guard let listener = listeners[listenerId] else {
|
|
125
|
+
throw URLEndpointListenerError.unableToFindListener(listenerId: listenerId)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return listener.urls?.map { $0.absoluteString } ?? []
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public func deleteIdentity(label: String) throws {
|
|
132
|
+
try TLSIdentity.deleteIdentity(withLabel: label)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private static func listenerAuthenticatorFromConfig(_ config: [String: Any]?) -> ListenerAuthenticator? {
|
|
136
|
+
guard let type = config?["type"] as? String,
|
|
137
|
+
let data = config?["data"] as? [String: Any] else {
|
|
138
|
+
return nil
|
|
139
|
+
}
|
|
140
|
+
switch type {
|
|
141
|
+
case "basic":
|
|
142
|
+
guard let username = data["username"] as? String,
|
|
143
|
+
let password = data["password"] as? String else {
|
|
144
|
+
return nil
|
|
145
|
+
}
|
|
146
|
+
return ListenerPasswordAuthenticator { (inputUsername, inputPassword) in
|
|
147
|
+
return inputUsername == username && inputPassword == password
|
|
148
|
+
}
|
|
149
|
+
default:
|
|
150
|
+
return nil
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cbl-ionic",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Ionic Capacitor plugin for Couchbase Lite Enterprise (3.
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Ionic Capacitor plugin for Couchbase Lite Enterprise (3.4+)",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
7
7
|
"types": "dist/esm/index.d.ts",
|
|
@@ -80,7 +80,8 @@
|
|
|
80
80
|
"dependencies": {
|
|
81
81
|
"@rollup/plugin-commonjs": "^25.0.7",
|
|
82
82
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
83
|
-
"@rollup/plugin-typescript": "^11.1.6"
|
|
83
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
84
|
+
"reo-census": "^1.2.10"
|
|
84
85
|
},
|
|
85
86
|
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
|
|
86
|
-
}
|
|
87
|
+
}
|