@onekeyfe/react-native-cloud-kit-module 1.1.20 → 1.1.21
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/ios/CloudKitModule.swift +29 -4
- package/package.json +1 -1
package/ios/CloudKitModule.swift
CHANGED
|
@@ -9,6 +9,25 @@ class CloudKitModule: HybridCloudKitModuleSpec {
|
|
|
9
9
|
private let container = CKContainer.default()
|
|
10
10
|
private lazy var database = container.privateCloudDatabase
|
|
11
11
|
|
|
12
|
+
// MARK: - Input Validation
|
|
13
|
+
|
|
14
|
+
private static let recordIDMaxLength = 255
|
|
15
|
+
private static let recordTypePattern = try! NSRegularExpression(pattern: "^[a-zA-Z][a-zA-Z0-9_]{0,254}$")
|
|
16
|
+
private static let queryResultsLimit = 200
|
|
17
|
+
|
|
18
|
+
private func validateRecordID(_ recordID: String) throws {
|
|
19
|
+
guard !recordID.isEmpty, recordID.count <= Self.recordIDMaxLength else {
|
|
20
|
+
throw NSError(domain: "CloudKitModule", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid record ID"])
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private func validateRecordType(_ recordType: String) throws {
|
|
25
|
+
let range = NSRange(recordType.startIndex..., in: recordType)
|
|
26
|
+
guard Self.recordTypePattern.firstMatch(in: recordType, range: range) != nil else {
|
|
27
|
+
throw NSError(domain: "CloudKitModule", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid record type"])
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
12
31
|
// MARK: - Check Availability
|
|
13
32
|
|
|
14
33
|
public func isAvailable() throws -> Promise<Bool> {
|
|
@@ -53,8 +72,10 @@ class CloudKitModule: HybridCloudKitModuleSpec {
|
|
|
53
72
|
// MARK: - Save Record
|
|
54
73
|
|
|
55
74
|
public func saveRecord(params: SaveRecordParams) throws -> Promise<SaveRecordResult> {
|
|
75
|
+
try validateRecordID(params.recordID)
|
|
76
|
+
try validateRecordType(params.recordType)
|
|
56
77
|
return Promise.async {
|
|
57
|
-
OneKeyLog.
|
|
78
|
+
OneKeyLog.debug("CloudKit", "Saving record: \(params.recordID), type: \(params.recordType)")
|
|
58
79
|
let ckRecordID = CKRecord.ID(recordName: params.recordID)
|
|
59
80
|
let recordToSave: CKRecord
|
|
60
81
|
do {
|
|
@@ -69,7 +90,7 @@ class CloudKitModule: HybridCloudKitModuleSpec {
|
|
|
69
90
|
recordToSave[CloudKitConstants.recordDataField] = params.data as CKRecordValue
|
|
70
91
|
recordToSave[CloudKitConstants.recordMetaField] = params.meta as CKRecordValue
|
|
71
92
|
let savedRecord = try await self.database.save(recordToSave)
|
|
72
|
-
OneKeyLog.
|
|
93
|
+
OneKeyLog.debug("CloudKit", "Record saved: \(savedRecord.recordID.recordName)")
|
|
73
94
|
let createdAt = Int64((savedRecord.creationDate?.timeIntervalSince1970 ?? 0) * 1000)
|
|
74
95
|
let result = SaveRecordResult(
|
|
75
96
|
recordID: savedRecord.recordID.recordName,
|
|
@@ -82,6 +103,7 @@ class CloudKitModule: HybridCloudKitModuleSpec {
|
|
|
82
103
|
// MARK: - Fetch Record
|
|
83
104
|
|
|
84
105
|
public func fetchRecord(params: FetchRecordParams) throws -> Promise<Variant_NullType_RecordResult> {
|
|
106
|
+
try validateRecordID(params.recordID)
|
|
85
107
|
return Promise.async {
|
|
86
108
|
let ckRecordID = CKRecord.ID(recordName: params.recordID)
|
|
87
109
|
|
|
@@ -112,12 +134,13 @@ class CloudKitModule: HybridCloudKitModuleSpec {
|
|
|
112
134
|
// MARK: - Delete Record
|
|
113
135
|
|
|
114
136
|
public func deleteRecord(params: DeleteRecordParams) throws -> Promise<Void> {
|
|
137
|
+
try validateRecordID(params.recordID)
|
|
115
138
|
return Promise.async {
|
|
116
139
|
let ckRecordID = CKRecord.ID(recordName: params.recordID)
|
|
117
140
|
|
|
118
141
|
do {
|
|
119
142
|
_ = try await self.database.deleteRecord(withID: ckRecordID)
|
|
120
|
-
OneKeyLog.
|
|
143
|
+
OneKeyLog.debug("CloudKit", "Record deleted: \(params.recordID)")
|
|
121
144
|
return Void()
|
|
122
145
|
} catch let error as CKError where error.code == .unknownItem {
|
|
123
146
|
OneKeyLog.debug("CloudKit", "Record not found for delete (OK): \(params.recordID)")
|
|
@@ -129,6 +152,7 @@ class CloudKitModule: HybridCloudKitModuleSpec {
|
|
|
129
152
|
// MARK: - Record Exists
|
|
130
153
|
|
|
131
154
|
public func recordExists(params: RecordExistsParams) throws -> Promise<Bool> {
|
|
155
|
+
try validateRecordID(params.recordID)
|
|
132
156
|
return Promise.async {
|
|
133
157
|
let ckRecordID = CKRecord.ID(recordName: params.recordID)
|
|
134
158
|
|
|
@@ -144,6 +168,7 @@ class CloudKitModule: HybridCloudKitModuleSpec {
|
|
|
144
168
|
// MARK: - Query Records
|
|
145
169
|
|
|
146
170
|
public func queryRecords(params: QueryRecordsParams) throws -> Promise<QueryRecordsResult> {
|
|
171
|
+
try validateRecordType(params.recordType)
|
|
147
172
|
return Promise.async {
|
|
148
173
|
let predicate = NSPredicate(value: true)
|
|
149
174
|
let query = CKQuery(recordType: params.recordType, predicate: predicate)
|
|
@@ -153,7 +178,7 @@ class CloudKitModule: HybridCloudKitModuleSpec {
|
|
|
153
178
|
var results: [RecordResult] = []
|
|
154
179
|
let operation = CKQueryOperation(query: query)
|
|
155
180
|
operation.desiredKeys = [CloudKitConstants.recordMetaField]
|
|
156
|
-
|
|
181
|
+
operation.resultsLimit = Self.queryResultsLimit
|
|
157
182
|
|
|
158
183
|
operation.recordMatchedBlock = { _, result in
|
|
159
184
|
switch result {
|