react-native-cloud-sync 0.4.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 (160) hide show
  1. package/README.md +18 -0
  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 +73 -9
  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 +73 -9
  42. package/lib/module/hooks.js.map +1 -1
  43. package/lib/module/internal/autoFlush.js +10 -1
  44. package/lib/module/internal/autoFlush.js.map +1 -1
  45. package/lib/module/internal/base64.js +25 -5
  46. package/lib/module/internal/base64.js.map +1 -1
  47. package/lib/module/internal/cloudKitRest.js +89 -13
  48. package/lib/module/internal/cloudKitRest.js.map +1 -1
  49. package/lib/module/internal/googleDriveRest.js +304 -38
  50. package/lib/module/internal/googleDriveRest.js.map +1 -1
  51. package/lib/module/internal/keys.js +33 -17
  52. package/lib/module/internal/keys.js.map +1 -1
  53. package/lib/module/internal/records.js +41 -0
  54. package/lib/module/internal/records.js.map +1 -0
  55. package/lib/module/providers/cloudKit.js +86 -18
  56. package/lib/module/providers/cloudKit.js.map +1 -1
  57. package/lib/module/providers/cloudKitEncrypted.js +31 -8
  58. package/lib/module/providers/cloudKitEncrypted.js.map +1 -1
  59. package/lib/module/providers/googleDrive.js +37 -2
  60. package/lib/module/providers/googleDrive.js.map +1 -1
  61. package/lib/module/providers/icloudDocuments.js +28 -1
  62. package/lib/module/providers/icloudDocuments.js.map +1 -1
  63. package/lib/module/providers/icloudKV.js +13 -3
  64. package/lib/module/providers/icloudKV.js.map +1 -1
  65. package/lib/module/providers/icloudKV.web.js +8 -1
  66. package/lib/module/providers/icloudKV.web.js.map +1 -1
  67. package/lib/module/providers/memory.js +10 -0
  68. package/lib/module/providers/memory.js.map +1 -1
  69. package/lib/module/resolvers.js +27 -12
  70. package/lib/module/resolvers.js.map +1 -1
  71. package/lib/module/specs/NativeRNCloudSync.js +4 -0
  72. package/lib/module/specs/NativeRNCloudSync.js.map +1 -1
  73. package/lib/module/store.js +360 -173
  74. package/lib/module/store.js.map +1 -1
  75. package/lib/module/types.js.map +1 -1
  76. package/lib/typescript/commonjs/errors.d.ts +16 -16
  77. package/lib/typescript/commonjs/errors.d.ts.map +1 -1
  78. package/lib/typescript/commonjs/hooks.d.ts.map +1 -1
  79. package/lib/typescript/commonjs/internal/autoFlush.d.ts.map +1 -1
  80. package/lib/typescript/commonjs/internal/base64.d.ts.map +1 -1
  81. package/lib/typescript/commonjs/internal/cloudKitRest.d.ts +20 -0
  82. package/lib/typescript/commonjs/internal/cloudKitRest.d.ts.map +1 -1
  83. package/lib/typescript/commonjs/internal/googleDriveRest.d.ts +19 -0
  84. package/lib/typescript/commonjs/internal/googleDriveRest.d.ts.map +1 -1
  85. package/lib/typescript/commonjs/internal/keys.d.ts +18 -3
  86. package/lib/typescript/commonjs/internal/keys.d.ts.map +1 -1
  87. package/lib/typescript/commonjs/internal/records.d.ts +24 -0
  88. package/lib/typescript/commonjs/internal/records.d.ts.map +1 -0
  89. package/lib/typescript/commonjs/providers/cloudKit.d.ts +1 -1
  90. package/lib/typescript/commonjs/providers/cloudKit.d.ts.map +1 -1
  91. package/lib/typescript/commonjs/providers/cloudKitBackup.d.ts +1 -1
  92. package/lib/typescript/commonjs/providers/cloudKitBackup.d.ts.map +1 -1
  93. package/lib/typescript/commonjs/providers/cloudKitEncrypted.d.ts.map +1 -1
  94. package/lib/typescript/commonjs/providers/googleDrive.d.ts.map +1 -1
  95. package/lib/typescript/commonjs/providers/googleDriveFiles.d.ts.map +1 -1
  96. package/lib/typescript/commonjs/providers/icloudDocuments.d.ts +1 -1
  97. package/lib/typescript/commonjs/providers/icloudDocuments.d.ts.map +1 -1
  98. package/lib/typescript/commonjs/providers/icloudKV.d.ts.map +1 -1
  99. package/lib/typescript/commonjs/providers/icloudKV.web.d.ts.map +1 -1
  100. package/lib/typescript/commonjs/providers/memory.d.ts.map +1 -1
  101. package/lib/typescript/commonjs/resolvers.d.ts +6 -3
  102. package/lib/typescript/commonjs/resolvers.d.ts.map +1 -1
  103. package/lib/typescript/commonjs/specs/NativeRNCloudSync.d.ts +12 -3
  104. package/lib/typescript/commonjs/specs/NativeRNCloudSync.d.ts.map +1 -1
  105. package/lib/typescript/commonjs/store.d.ts.map +1 -1
  106. package/lib/typescript/commonjs/types.d.ts +31 -3
  107. package/lib/typescript/commonjs/types.d.ts.map +1 -1
  108. package/lib/typescript/module/errors.d.ts +16 -16
  109. package/lib/typescript/module/errors.d.ts.map +1 -1
  110. package/lib/typescript/module/hooks.d.ts.map +1 -1
  111. package/lib/typescript/module/internal/autoFlush.d.ts.map +1 -1
  112. package/lib/typescript/module/internal/base64.d.ts.map +1 -1
  113. package/lib/typescript/module/internal/cloudKitRest.d.ts +20 -0
  114. package/lib/typescript/module/internal/cloudKitRest.d.ts.map +1 -1
  115. package/lib/typescript/module/internal/googleDriveRest.d.ts +19 -0
  116. package/lib/typescript/module/internal/googleDriveRest.d.ts.map +1 -1
  117. package/lib/typescript/module/internal/keys.d.ts +18 -3
  118. package/lib/typescript/module/internal/keys.d.ts.map +1 -1
  119. package/lib/typescript/module/internal/records.d.ts +24 -0
  120. package/lib/typescript/module/internal/records.d.ts.map +1 -0
  121. package/lib/typescript/module/providers/cloudKit.d.ts +1 -1
  122. package/lib/typescript/module/providers/cloudKit.d.ts.map +1 -1
  123. package/lib/typescript/module/providers/cloudKitBackup.d.ts +1 -1
  124. package/lib/typescript/module/providers/cloudKitBackup.d.ts.map +1 -1
  125. package/lib/typescript/module/providers/cloudKitEncrypted.d.ts.map +1 -1
  126. package/lib/typescript/module/providers/googleDrive.d.ts.map +1 -1
  127. package/lib/typescript/module/providers/googleDriveFiles.d.ts.map +1 -1
  128. package/lib/typescript/module/providers/icloudDocuments.d.ts +1 -1
  129. package/lib/typescript/module/providers/icloudDocuments.d.ts.map +1 -1
  130. package/lib/typescript/module/providers/icloudKV.d.ts.map +1 -1
  131. package/lib/typescript/module/providers/icloudKV.web.d.ts.map +1 -1
  132. package/lib/typescript/module/providers/memory.d.ts.map +1 -1
  133. package/lib/typescript/module/resolvers.d.ts +6 -3
  134. package/lib/typescript/module/resolvers.d.ts.map +1 -1
  135. package/lib/typescript/module/specs/NativeRNCloudSync.d.ts +12 -3
  136. package/lib/typescript/module/specs/NativeRNCloudSync.d.ts.map +1 -1
  137. package/lib/typescript/module/store.d.ts.map +1 -1
  138. package/lib/typescript/module/types.d.ts +31 -3
  139. package/lib/typescript/module/types.d.ts.map +1 -1
  140. package/package.json +35 -25
  141. package/plugin/build/index.js +45 -13
  142. package/react-native-cloud-sync.podspec +10 -3
  143. package/src/hooks.ts +69 -9
  144. package/src/internal/autoFlush.ts +10 -1
  145. package/src/internal/base64.ts +26 -5
  146. package/src/internal/cloudKitRest.ts +103 -14
  147. package/src/internal/googleDriveRest.ts +338 -37
  148. package/src/internal/keys.ts +42 -16
  149. package/src/internal/records.ts +38 -0
  150. package/src/providers/cloudKit.ts +96 -20
  151. package/src/providers/cloudKitEncrypted.ts +37 -8
  152. package/src/providers/googleDrive.ts +38 -2
  153. package/src/providers/icloudDocuments.ts +40 -2
  154. package/src/providers/icloudKV.ts +13 -3
  155. package/src/providers/icloudKV.web.ts +8 -2
  156. package/src/providers/memory.ts +8 -0
  157. package/src/resolvers.ts +28 -13
  158. package/src/specs/NativeRNCloudSync.ts +16 -2
  159. package/src/store.ts +433 -190
  160. package/src/types.ts +32 -3
@@ -1,5 +1,6 @@
1
1
  import { CloudSyncError, ErrorCode, timedOut } from '../errors'
2
2
  import { byteLength } from './bytes'
3
+ import { clampRetryAfterMs } from './records'
3
4
  import { withTimeout } from './timeout'
4
5
 
5
6
  /** Default request timeout. Generous, because a large record on a slow link is normal. */
@@ -105,6 +106,17 @@ export interface CloudKitAssetDescriptor {
105
106
  const MAX_BATCH = 200
106
107
 
107
108
  export const RECORD_TYPE_DEFAULT = 'KVBlob'
109
+
110
+ /**
111
+ * Record-name prefix for `cloudKitEncrypted`.
112
+ *
113
+ * CloudKit record names are unique per zone regardless of record type, so
114
+ * without it the encrypted and plain providers addressed the same record for
115
+ * the same key: a write to one collided with the other's schema, and a delete
116
+ * through one removed the other's data. The plain `cloudKit` provider rejects
117
+ * keys that start with it, so the two namespaces cannot overlap.
118
+ */
119
+ export const ENCRYPTED_RECORD_PREFIX = 'rncs-enc.'
108
120
  export const VALUE_FIELD = 'value'
109
121
 
110
122
  /** CloudKit's documented per-record ceiling, excluding assets. */
@@ -112,6 +124,7 @@ export const MAX_RECORD_BYTES = 1024 * 1024
112
124
 
113
125
  interface CloudKitRecordResult {
114
126
  recordName?: string
127
+ recordType?: string
115
128
  fields?: Record<string, { value?: unknown }>
116
129
  serverErrorCode?: string
117
130
  reason?: string
@@ -177,7 +190,8 @@ export class CloudKitRestClient {
177
190
 
178
191
  private baseUrl(): string {
179
192
  const { containerIdentifier, environment } = this.config
180
- return `https://api.apple-cloudkit.com/database/1/${containerIdentifier}/${environment}/private`
193
+ return `https://api.apple-cloudkit.com/database/1/${encodeURIComponent(containerIdentifier)}/`
194
+ + `${encodeURIComponent(environment)}/private`
181
195
  }
182
196
 
183
197
  private get fetch(): typeof fetch {
@@ -229,15 +243,18 @@ export class CloudKitRestClient {
229
243
  await this.handleAuthExpired()
230
244
  return new CloudSyncError(ErrorCode.AUTH_EXPIRED, message, base)
231
245
  case 'ACCESS_DENIED':
232
- await this.handleAuthExpired()
233
- return new CloudSyncError(ErrorCode.AUTH_EXPIRED, message, base)
246
+ // A permission failure, not a dead token: CloudKit sends it when the
247
+ // authenticated user lacks rights to the record type, zone or database
248
+ // (security roles set up wrong in the CloudKit Console). Treating it as
249
+ // expired auth signed the user out, and re-signing in changes nothing.
250
+ return new CloudSyncError(ErrorCode.CONTAINER_MISCONFIGURED, message, base)
234
251
  case 'QUOTA_EXCEEDED':
235
252
  return new CloudSyncError(ErrorCode.QUOTA_EXCEEDED, message, base)
236
253
  case 'THROTTLED':
237
254
  case 'TRY_AGAIN_LATER':
238
255
  return new CloudSyncError(ErrorCode.RATE_LIMITED, message, {
239
256
  ...base,
240
- retryAfterMs: retryAfter != null ? retryAfter * 1000 : undefined,
257
+ retryAfterMs: typeof retryAfter === 'number' ? clampRetryAfterMs(retryAfter * 1000) : undefined,
241
258
  })
242
259
  case 'CONFLICT':
243
260
  return new CloudSyncError(ErrorCode.CONFLICT, message, { ...base, serverValue })
@@ -338,12 +355,11 @@ export class CloudKitRestClient {
338
355
  recordName: string,
339
356
  recordType = RECORD_TYPE_DEFAULT
340
357
  ): Promise<{ value: string; modifiedAt?: number; recordChangeTag?: string } | null> {
341
- void recordType
342
358
  const json = await this.post('/records/lookup', {
343
359
  records: [{ recordName }],
344
360
  })
345
361
 
346
- const record = json.records?.find(r => r.recordName === recordName) ?? json.records?.[0]
362
+ const record = lookupResult(json, recordName)
347
363
  if (record == null) return null
348
364
 
349
365
  if (record.serverErrorCode != null) {
@@ -354,6 +370,7 @@ export class CloudKitRestClient {
354
370
  if (record.serverErrorCode === 'NOT_FOUND') return null
355
371
  throw await this.toError(record.serverErrorCode, record.reason, record.retryAfter)
356
372
  }
373
+ assertRecordType(record, recordType)
357
374
 
358
375
  const value = record.fields?.[VALUE_FIELD]?.value
359
376
  if (typeof value !== 'string') return null
@@ -380,10 +397,21 @@ export class CloudKitRestClient {
380
397
  recordNames: string[],
381
398
  recordType = RECORD_TYPE_DEFAULT
382
399
  ): Promise<(string | null)[]> {
383
- void recordType
400
+ return (await this.getRecordsWithMeta(recordNames, recordType)).map(r => r?.value ?? null)
401
+ }
402
+
403
+ /**
404
+ * {@link getRecords} plus each record's server modification time and change
405
+ * tag - the batch form of {@link getRecordWithMeta}. Free for the same
406
+ * reason: `/records/lookup` returns both whether or not anyone asks.
407
+ */
408
+ async getRecordsWithMeta(
409
+ recordNames: string[],
410
+ recordType = RECORD_TYPE_DEFAULT
411
+ ): Promise<({ value: string; modifiedAt?: number; recordChangeTag?: string } | null)[]> {
384
412
  if (recordNames.length === 0) return []
385
413
 
386
- const found = new Map<string, string>()
414
+ const found = new Map<string, { value: string; modifiedAt?: number; recordChangeTag?: string }>()
387
415
 
388
416
  for (const batch of chunk(recordNames, MAX_BATCH)) {
389
417
  const json = await this.post('/records/lookup', {
@@ -398,8 +426,16 @@ export class CloudKitRestClient {
398
426
  if (record.serverErrorCode === 'NOT_FOUND') continue
399
427
  throw await this.toError(record.serverErrorCode, record.reason, record.retryAfter)
400
428
  }
429
+ assertRecordType(record, recordType)
401
430
  const value = record.fields?.[VALUE_FIELD]?.value
402
- if (typeof value === 'string') found.set(record.recordName, value)
431
+ if (typeof value === 'string')
432
+ found.set(record.recordName, {
433
+ value,
434
+ modifiedAt: typeof record.modified?.timestamp === 'number'
435
+ ? record.modified.timestamp
436
+ : undefined,
437
+ recordChangeTag: record.recordChangeTag,
438
+ })
403
439
  }
404
440
  }
405
441
 
@@ -603,20 +639,26 @@ export class CloudKitRestClient {
603
639
  onProgress?: (bytesTransferred: number, bytesTotal: number) => void,
604
640
  recordType = RECORD_TYPE_DEFAULT
605
641
  ): Promise<Uint8Array | null> {
606
- void recordType
607
642
  const json = await this.post('/records/lookup', { records: [{ recordName }] })
608
- const record = json.records?.find(r => r.recordName === recordName) ?? json.records?.[0]
643
+ const record = lookupResult(json, recordName)
609
644
  if (record == null) return null
610
645
 
611
646
  if (record.serverErrorCode != null) {
612
647
  if (record.serverErrorCode === 'NOT_FOUND') return null
613
648
  throw await this.toError(record.serverErrorCode, record.reason, record.retryAfter)
614
649
  }
650
+ assertRecordType(record, recordType)
615
651
 
616
652
  const asset = record.fields?.[fieldName]?.value as CloudKitAssetDescriptor | undefined
617
653
  if (asset?.downloadURL == null) return null
618
654
 
619
- return await this.fetchAssetBytes(asset.downloadURL, onProgress)
655
+ // CloudKit Web Services hands back the download URL as a template with a
656
+ // literal `${f}` where the file name goes; the client is expected to fill
657
+ // it in. Requesting the template verbatim asks for a path that does not
658
+ // exist.
659
+ if (typeof asset.downloadURL !== 'string') return null
660
+ const downloadUrl = asset.downloadURL.replace('${f}', encodeURIComponent(fieldName))
661
+ return await this.fetchAssetBytes(assertHttpsUrl(downloadUrl, 'download'), onProgress)
620
662
  }
621
663
 
622
664
  private async requestAssetUploadUrl(
@@ -627,14 +669,14 @@ export class CloudKitRestClient {
627
669
  })
628
670
 
629
671
  const url = json.tokens?.[0]?.url
630
- if (url == null)
672
+ if (typeof url !== 'string')
631
673
  throw new CloudSyncError(
632
674
  ErrorCode.UNKNOWN,
633
675
  '[RNCloudSync] CloudKit did not return an asset upload URL.',
634
676
  { provider: 'cloudKit' }
635
677
  )
636
678
 
637
- return url
679
+ return assertHttpsUrl(url, 'upload')
638
680
  }
639
681
 
640
682
  private get xhr(): () => XMLHttpRequest {
@@ -826,6 +868,9 @@ export class CloudKitRestClient {
826
868
  do {
827
869
  const json: CloudKitResponse = await this.post('/records/query', {
828
870
  query: { recordType },
871
+ // Names only. Without this every page carries every record's full
872
+ // value - up to 200 records of up to 1 MB each - just to read names.
873
+ desiredKeys: [],
829
874
  ...(continuationMarker != null ? { continuationMarker } : {}),
830
875
  })
831
876
 
@@ -883,6 +928,50 @@ export class CloudKitRestClient {
883
928
  }
884
929
  }
885
930
 
931
+ /**
932
+ * The lookup result for `recordName`.
933
+ *
934
+ * A result naming a *different* record is never taken as this one's: serving
935
+ * it would hand key B's value back as key A's. Only an entry with no
936
+ * `recordName` at all - the shape of a per-record error - may stand in for the
937
+ * requested one.
938
+ */
939
+ function lookupResult(json: CloudKitResponse, recordName: string): CloudKitRecordResult | undefined {
940
+ const records = json.records ?? []
941
+ return records.find(r => r.recordName === recordName)
942
+ ?? records.find(r => r.recordName == null)
943
+ }
944
+
945
+ /**
946
+ * Record names are unique across record types, so a lookup by name can return
947
+ * a record of another type - one another provider (or another app sharing the
948
+ * container) wrote. Reading it as this type would serve foreign data under
949
+ * this key, so a mismatch is an error, as it is on the native path.
950
+ */
951
+ function assertRecordType(record: CloudKitRecordResult, recordType: string): void {
952
+ if (record.recordType != null && record.recordType !== recordType)
953
+ throw new CloudSyncError(
954
+ ErrorCode.INVALID_KEY,
955
+ `[RNCloudSync] Record "${record.recordName ?? ''}" is a ${record.recordType}, not a ${recordType}.`,
956
+ { provider: 'cloudKit' }
957
+ )
958
+ }
959
+
960
+ /**
961
+ * Asset bytes travel to and from URLs the server hands out, not ones this
962
+ * client builds. Plain HTTP would put the user's file on the wire unencrypted
963
+ * (and let anything on-path substitute the download), so only HTTPS is used.
964
+ */
965
+ function assertHttpsUrl(url: string, kind: 'upload' | 'download'): string {
966
+ if (!/^https:\/\/[^/?#]+/i.test(url))
967
+ throw new CloudSyncError(
968
+ ErrorCode.UNKNOWN,
969
+ `[RNCloudSync] CloudKit returned a non-HTTPS asset ${kind} URL; refusing to use it.`,
970
+ { provider: 'cloudKit', serverErrorCode: 'UNTRUSTED_URL' }
971
+ )
972
+ return url
973
+ }
974
+
886
975
  /** Splits `items` into runs of at most `size`. */
887
976
  function chunk<T>(items: T[], size: number): T[][] {
888
977
  if (items.length <= size) return [items]