react-native-cloud-sync 0.3.0 → 0.5.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 (166) hide show
  1. package/README.md +116 -58
  2. package/ios/CloudSyncError.swift +21 -0
  3. package/ios/CloudSyncImpl.swift +515 -95
  4. package/ios/RNCloudSync.mm +73 -1
  5. package/jest-mock.js +1 -0
  6. package/lib/commonjs/hooks.js +309 -3
  7. package/lib/commonjs/hooks.js.map +1 -1
  8. package/lib/commonjs/internal/autoFlush.js +10 -1
  9. package/lib/commonjs/internal/autoFlush.js.map +1 -1
  10. package/lib/commonjs/internal/base64.js +25 -5
  11. package/lib/commonjs/internal/base64.js.map +1 -1
  12. package/lib/commonjs/internal/cloudKitRest.js +91 -15
  13. package/lib/commonjs/internal/cloudKitRest.js.map +1 -1
  14. package/lib/commonjs/internal/googleDriveRest.js +304 -38
  15. package/lib/commonjs/internal/googleDriveRest.js.map +1 -1
  16. package/lib/commonjs/internal/keys.js +34 -17
  17. package/lib/commonjs/internal/keys.js.map +1 -1
  18. package/lib/commonjs/internal/records.js +48 -0
  19. package/lib/commonjs/internal/records.js.map +1 -0
  20. package/lib/commonjs/providers/cloudKit.js +85 -17
  21. package/lib/commonjs/providers/cloudKit.js.map +1 -1
  22. package/lib/commonjs/providers/cloudKitEncrypted.js +30 -7
  23. package/lib/commonjs/providers/cloudKitEncrypted.js.map +1 -1
  24. package/lib/commonjs/providers/googleDrive.js +37 -2
  25. package/lib/commonjs/providers/googleDrive.js.map +1 -1
  26. package/lib/commonjs/providers/icloudDocuments.js +27 -0
  27. package/lib/commonjs/providers/icloudDocuments.js.map +1 -1
  28. package/lib/commonjs/providers/icloudKV.js +13 -3
  29. package/lib/commonjs/providers/icloudKV.js.map +1 -1
  30. package/lib/commonjs/providers/icloudKV.web.js +8 -1
  31. package/lib/commonjs/providers/icloudKV.web.js.map +1 -1
  32. package/lib/commonjs/providers/memory.js +10 -0
  33. package/lib/commonjs/providers/memory.js.map +1 -1
  34. package/lib/commonjs/resolvers.js +27 -13
  35. package/lib/commonjs/resolvers.js.map +1 -1
  36. package/lib/commonjs/specs/NativeRNCloudSync.js +3 -0
  37. package/lib/commonjs/specs/NativeRNCloudSync.js.map +1 -1
  38. package/lib/commonjs/store.js +360 -173
  39. package/lib/commonjs/store.js.map +1 -1
  40. package/lib/commonjs/types.js.map +1 -1
  41. package/lib/module/hooks.js +307 -3
  42. package/lib/module/hooks.js.map +1 -1
  43. package/lib/module/index.js +2 -2
  44. package/lib/module/internal/autoFlush.js +10 -1
  45. package/lib/module/internal/autoFlush.js.map +1 -1
  46. package/lib/module/internal/base64.js +25 -5
  47. package/lib/module/internal/base64.js.map +1 -1
  48. package/lib/module/internal/cloudKitRest.js +89 -13
  49. package/lib/module/internal/cloudKitRest.js.map +1 -1
  50. package/lib/module/internal/googleDriveRest.js +304 -38
  51. package/lib/module/internal/googleDriveRest.js.map +1 -1
  52. package/lib/module/internal/keys.js +33 -17
  53. package/lib/module/internal/keys.js.map +1 -1
  54. package/lib/module/internal/records.js +41 -0
  55. package/lib/module/internal/records.js.map +1 -0
  56. package/lib/module/providers/cloudKit.js +86 -18
  57. package/lib/module/providers/cloudKit.js.map +1 -1
  58. package/lib/module/providers/cloudKitEncrypted.js +31 -8
  59. package/lib/module/providers/cloudKitEncrypted.js.map +1 -1
  60. package/lib/module/providers/googleDrive.js +37 -2
  61. package/lib/module/providers/googleDrive.js.map +1 -1
  62. package/lib/module/providers/icloudDocuments.js +28 -1
  63. package/lib/module/providers/icloudDocuments.js.map +1 -1
  64. package/lib/module/providers/icloudKV.js +13 -3
  65. package/lib/module/providers/icloudKV.js.map +1 -1
  66. package/lib/module/providers/icloudKV.web.js +8 -1
  67. package/lib/module/providers/icloudKV.web.js.map +1 -1
  68. package/lib/module/providers/memory.js +10 -0
  69. package/lib/module/providers/memory.js.map +1 -1
  70. package/lib/module/resolvers.js +27 -12
  71. package/lib/module/resolvers.js.map +1 -1
  72. package/lib/module/specs/NativeRNCloudSync.js +4 -0
  73. package/lib/module/specs/NativeRNCloudSync.js.map +1 -1
  74. package/lib/module/store.js +360 -173
  75. package/lib/module/store.js.map +1 -1
  76. package/lib/module/types.js.map +1 -1
  77. package/lib/typescript/commonjs/errors.d.ts +16 -16
  78. package/lib/typescript/commonjs/errors.d.ts.map +1 -1
  79. package/lib/typescript/commonjs/hooks.d.ts +68 -0
  80. package/lib/typescript/commonjs/hooks.d.ts.map +1 -1
  81. package/lib/typescript/commonjs/index.d.ts +2 -2
  82. package/lib/typescript/commonjs/internal/autoFlush.d.ts.map +1 -1
  83. package/lib/typescript/commonjs/internal/base64.d.ts.map +1 -1
  84. package/lib/typescript/commonjs/internal/cloudKitRest.d.ts +20 -0
  85. package/lib/typescript/commonjs/internal/cloudKitRest.d.ts.map +1 -1
  86. package/lib/typescript/commonjs/internal/googleDriveRest.d.ts +19 -0
  87. package/lib/typescript/commonjs/internal/googleDriveRest.d.ts.map +1 -1
  88. package/lib/typescript/commonjs/internal/keys.d.ts +18 -3
  89. package/lib/typescript/commonjs/internal/keys.d.ts.map +1 -1
  90. package/lib/typescript/commonjs/internal/records.d.ts +24 -0
  91. package/lib/typescript/commonjs/internal/records.d.ts.map +1 -0
  92. package/lib/typescript/commonjs/providers/cloudKit.d.ts +1 -1
  93. package/lib/typescript/commonjs/providers/cloudKit.d.ts.map +1 -1
  94. package/lib/typescript/commonjs/providers/cloudKitBackup.d.ts +1 -1
  95. package/lib/typescript/commonjs/providers/cloudKitBackup.d.ts.map +1 -1
  96. package/lib/typescript/commonjs/providers/cloudKitEncrypted.d.ts.map +1 -1
  97. package/lib/typescript/commonjs/providers/googleDrive.d.ts.map +1 -1
  98. package/lib/typescript/commonjs/providers/googleDriveFiles.d.ts.map +1 -1
  99. package/lib/typescript/commonjs/providers/icloudDocuments.d.ts +1 -1
  100. package/lib/typescript/commonjs/providers/icloudDocuments.d.ts.map +1 -1
  101. package/lib/typescript/commonjs/providers/icloudKV.d.ts.map +1 -1
  102. package/lib/typescript/commonjs/providers/icloudKV.web.d.ts.map +1 -1
  103. package/lib/typescript/commonjs/providers/memory.d.ts.map +1 -1
  104. package/lib/typescript/commonjs/resolvers.d.ts +6 -3
  105. package/lib/typescript/commonjs/resolvers.d.ts.map +1 -1
  106. package/lib/typescript/commonjs/specs/NativeRNCloudSync.d.ts +12 -3
  107. package/lib/typescript/commonjs/specs/NativeRNCloudSync.d.ts.map +1 -1
  108. package/lib/typescript/commonjs/store.d.ts.map +1 -1
  109. package/lib/typescript/commonjs/types.d.ts +31 -3
  110. package/lib/typescript/commonjs/types.d.ts.map +1 -1
  111. package/lib/typescript/module/errors.d.ts +16 -16
  112. package/lib/typescript/module/errors.d.ts.map +1 -1
  113. package/lib/typescript/module/hooks.d.ts +68 -0
  114. package/lib/typescript/module/hooks.d.ts.map +1 -1
  115. package/lib/typescript/module/index.d.ts +2 -2
  116. package/lib/typescript/module/internal/autoFlush.d.ts.map +1 -1
  117. package/lib/typescript/module/internal/base64.d.ts.map +1 -1
  118. package/lib/typescript/module/internal/cloudKitRest.d.ts +20 -0
  119. package/lib/typescript/module/internal/cloudKitRest.d.ts.map +1 -1
  120. package/lib/typescript/module/internal/googleDriveRest.d.ts +19 -0
  121. package/lib/typescript/module/internal/googleDriveRest.d.ts.map +1 -1
  122. package/lib/typescript/module/internal/keys.d.ts +18 -3
  123. package/lib/typescript/module/internal/keys.d.ts.map +1 -1
  124. package/lib/typescript/module/internal/records.d.ts +24 -0
  125. package/lib/typescript/module/internal/records.d.ts.map +1 -0
  126. package/lib/typescript/module/providers/cloudKit.d.ts +1 -1
  127. package/lib/typescript/module/providers/cloudKit.d.ts.map +1 -1
  128. package/lib/typescript/module/providers/cloudKitBackup.d.ts +1 -1
  129. package/lib/typescript/module/providers/cloudKitBackup.d.ts.map +1 -1
  130. package/lib/typescript/module/providers/cloudKitEncrypted.d.ts.map +1 -1
  131. package/lib/typescript/module/providers/googleDrive.d.ts.map +1 -1
  132. package/lib/typescript/module/providers/googleDriveFiles.d.ts.map +1 -1
  133. package/lib/typescript/module/providers/icloudDocuments.d.ts +1 -1
  134. package/lib/typescript/module/providers/icloudDocuments.d.ts.map +1 -1
  135. package/lib/typescript/module/providers/icloudKV.d.ts.map +1 -1
  136. package/lib/typescript/module/providers/icloudKV.web.d.ts.map +1 -1
  137. package/lib/typescript/module/providers/memory.d.ts.map +1 -1
  138. package/lib/typescript/module/resolvers.d.ts +6 -3
  139. package/lib/typescript/module/resolvers.d.ts.map +1 -1
  140. package/lib/typescript/module/specs/NativeRNCloudSync.d.ts +12 -3
  141. package/lib/typescript/module/specs/NativeRNCloudSync.d.ts.map +1 -1
  142. package/lib/typescript/module/store.d.ts.map +1 -1
  143. package/lib/typescript/module/types.d.ts +31 -3
  144. package/lib/typescript/module/types.d.ts.map +1 -1
  145. package/package.json +36 -23
  146. package/plugin/build/index.js +45 -13
  147. package/react-native-cloud-sync.podspec +10 -3
  148. package/src/hooks.ts +357 -3
  149. package/src/index.ts +2 -2
  150. package/src/internal/autoFlush.ts +10 -1
  151. package/src/internal/base64.ts +26 -5
  152. package/src/internal/cloudKitRest.ts +103 -14
  153. package/src/internal/googleDriveRest.ts +338 -37
  154. package/src/internal/keys.ts +42 -16
  155. package/src/internal/records.ts +38 -0
  156. package/src/providers/cloudKit.ts +96 -20
  157. package/src/providers/cloudKitEncrypted.ts +37 -8
  158. package/src/providers/googleDrive.ts +38 -2
  159. package/src/providers/icloudDocuments.ts +40 -2
  160. package/src/providers/icloudKV.ts +13 -3
  161. package/src/providers/icloudKV.web.ts +8 -2
  162. package/src/providers/memory.ts +8 -0
  163. package/src/resolvers.ts +28 -13
  164. package/src/specs/NativeRNCloudSync.ts +16 -2
  165. package/src/store.ts +433 -190
  166. package/src/types.ts +32 -3
package/src/resolvers.ts CHANGED
@@ -17,7 +17,7 @@ import type { ResolveCandidate, ResolveFn } from './types'
17
17
  * ```
18
18
  */
19
19
  export function resolveByTimestamp(field = 'updatedAt'): ResolveFn {
20
- return (candidates: ResolveCandidate[]): string | null => {
20
+ return declare(false, (candidates: ResolveCandidate[]): string | null => {
21
21
  if (candidates.length === 0) return null
22
22
 
23
23
  let best: ResolveCandidate | null = null
@@ -36,7 +36,19 @@ export function resolveByTimestamp(field = 'updatedAt'): ResolveFn {
36
36
  // Nothing was datable: fall back to preference order rather than guessing.
37
37
  if (best == null) return candidates[0].value
38
38
  return best.value
39
- }
39
+ })
40
+ }
41
+
42
+ /** Tags a resolver with whether it reads `modifiedAt` - see {@link ResolveFn.usesModifiedAt}. */
43
+ function declare(usesModifiedAt: boolean, fn: (c: ResolveCandidate[]) => string | null): ResolveFn {
44
+ const resolver: ResolveFn = fn
45
+ resolver.usesModifiedAt = usesModifiedAt
46
+ return resolver
47
+ }
48
+
49
+ /** Unknown counts as "uses it": over-fetching is slower, under-fetching is wrong. */
50
+ function needsModifiedAt(resolver: ResolveFn): boolean {
51
+ return resolver.usesModifiedAt !== false
40
52
  }
41
53
 
42
54
  function timestampOf(value: string, field: string): number {
@@ -58,8 +70,8 @@ function timestampOf(value: string, field: string): number {
58
70
  }
59
71
 
60
72
  /** Always takes the first provider that has a value. The default behaviour. */
61
- export const resolveByPreferenceOrder: ResolveFn = candidates =>
62
- candidates.length > 0 ? candidates[0].value : null
73
+ export const resolveByPreferenceOrder: ResolveFn = declare(false, candidates =>
74
+ candidates.length > 0 ? candidates[0].value : null)
63
75
 
64
76
  /**
65
77
  * Resolves by the *server's* last-modified time rather than a timestamp inside
@@ -91,7 +103,7 @@ export function resolveByModifiedAt(
91
103
  ): ResolveFn {
92
104
  const fallback = options.fallback ?? resolveByPreferenceOrder
93
105
 
94
- return (candidates: ResolveCandidate[]): string | null => {
106
+ return declare(true, (candidates: ResolveCandidate[]): string | null => {
95
107
  if (candidates.length === 0) return null
96
108
 
97
109
  const dated = candidates.filter(
@@ -109,7 +121,7 @@ export function resolveByModifiedAt(
109
121
  for (const candidate of dated) if (candidate.modifiedAt > best.modifiedAt) best = candidate
110
122
 
111
123
  return best.value
112
- }
124
+ })
113
125
  }
114
126
 
115
127
  /**
@@ -153,7 +165,7 @@ export function resolveByUnion<T = unknown>(
153
165
  ): ResolveFn {
154
166
  const keyOf = options.key ?? ((item: T) => JSON.stringify(item))
155
167
 
156
- return (candidates: ResolveCandidate[]): string | null => {
168
+ return declare(false, (candidates: ResolveCandidate[]): string | null => {
157
169
  if (candidates.length === 0) return null
158
170
 
159
171
  const merged = new Map<string | number, T>()
@@ -174,7 +186,7 @@ export function resolveByUnion<T = unknown>(
174
186
  if (!sawArray) return candidates[0].value
175
187
 
176
188
  return JSON.stringify([...merged.values()])
177
- }
189
+ })
178
190
  }
179
191
 
180
192
  function arrayOf<T>(value: string): T[] | null {
@@ -190,19 +202,22 @@ function arrayOf<T>(value: string): T[] | null {
190
202
  /**
191
203
  * Tries each resolver in turn and takes the first non-null answer.
192
204
  *
193
- * The practical combination for a mixed fleet: server time where the provider
194
- * reports it, an embedded timestamp where it does not.
205
+ * Only resolvers that can return `null` for a non-empty candidate list let a
206
+ * later one run. {@link resolveByModifiedAt} never does - with nothing dated it
207
+ * defers to its own `fallback` - so for "server time where the provider
208
+ * reports it, an embedded timestamp where it does not", pass the second one as
209
+ * that fallback instead:
195
210
  *
196
211
  * ```ts
197
- * resolveFirstOf(resolveByModifiedAt(), resolveByTimestamp('updatedAt'))
212
+ * resolveByModifiedAt({ fallback: resolveByTimestamp('updatedAt') })
198
213
  * ```
199
214
  */
200
215
  export function resolveFirstOf(...resolvers: ResolveFn[]): ResolveFn {
201
- return (candidates: ResolveCandidate[]): string | null => {
216
+ return declare(resolvers.some(needsModifiedAt), (candidates: ResolveCandidate[]): string | null => {
202
217
  for (const resolve of resolvers) {
203
218
  const answer = resolve(candidates)
204
219
  if (answer != null) return answer
205
220
  }
206
221
  return null
207
- }
222
+ })
208
223
  }
@@ -1,6 +1,10 @@
1
- import type { TurboModule } from 'react-native'
1
+ import type { EventSubscription, TurboModule } from 'react-native'
2
2
  import { TurboModuleRegistry } from 'react-native'
3
- import type { EventEmitter } from 'react-native/Libraries/Types/CodegenTypes'
3
+
4
+ // Same shape as React Native's own codegen `EventEmitter`. Declared here so the
5
+ // spec neither deep-imports a path newer type packages drop nor needs the
6
+ // `CodegenTypes` namespace older codegen cannot parse.
7
+ type EventEmitter<T> = (handler: (arg: T) => void | Promise<void>) => EventSubscription
4
8
 
5
9
  /**
6
10
  * Codegen cannot express an index signature, so dynamic dictionaries cross the
@@ -53,6 +57,16 @@ export interface Spec extends TurboModule {
53
57
 
54
58
  // ------------------------------------------- iCloud key-value store (KVS)
55
59
 
60
+ /**
61
+ * Account status for the key-value store, from `ubiquityIdentityToken`.
62
+ *
63
+ * Unlike `getAccountStatus` this never touches CloudKit, so it is safe in an
64
+ * app with only the key-value-store entitlement - constructing a
65
+ * `CKContainer` there raises an uncatchable `CKException`. Reports only
66
+ * `available` or `noAccount`.
67
+ */
68
+ kvGetAccountStatus: () => Promise<string>
69
+
56
70
  kvGetItem: (key: string) => Promise<string | null>
57
71
  kvSetItem: (key: string, value: string) => Promise<void>
58
72
  kvRemoveItem: (key: string) => Promise<void>