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,4 +1,5 @@
1
1
  import { CloudSyncError, ErrorCode, isCancelled } from '../errors'
2
+ import { clampRetryAfterMs } from './records'
2
3
  import { throwIfAborted, withTimeout, type AbortLike } from './timeout'
3
4
 
4
5
  /**
@@ -148,6 +149,95 @@ interface DriveFile {
148
149
  /** The `fields` mask every listing uses, so a read also learns modification time. */
149
150
  const FILE_FIELDS = 'id,name,modifiedTime,version'
150
151
 
152
+ /**
153
+ * The next offset a resumable session expects, from a 308's `Range` header
154
+ * ("bytes=0-8388607" -> 8388608). Undefined when there is no usable header.
155
+ */
156
+ function parsePersistedRange(range: string | null): number | undefined {
157
+ if (range == null) return undefined
158
+ const match = /bytes=0-(\d+)/.exec(range)
159
+ return match ? Number(match[1]) + 1 : undefined
160
+ }
161
+
162
+ /**
163
+ * `Retry-After` in milliseconds, or undefined when absent or not a number of
164
+ * seconds. Checked for absence first: `Number(null)` is 0, which would tell
165
+ * the backoff "retry immediately" whenever Drive simply sent no header.
166
+ */
167
+ function retryAfterMs(res: Response): number | undefined {
168
+ const header = res.headers.get('Retry-After')
169
+ if (header == null || header.trim() === '') return undefined
170
+ const seconds = Number(header)
171
+ return Number.isFinite(seconds) ? clampRetryAfterMs(seconds * 1000) : undefined
172
+ }
173
+
174
+ /**
175
+ * The only origins the bearer token is ever sent to.
176
+ *
177
+ * Every Drive endpoint lives on `www.googleapis.com`. The one URL this client
178
+ * does not build itself - the resumable-session URI - comes from a `Location`
179
+ * header or, across restarts, from the host's `sessionStore`. Neither is a
180
+ * place to take a destination for an OAuth token on trust: a tampered store
181
+ * entry or a misbehaving proxy would otherwise receive `Authorization: Bearer`
182
+ * for the user's Drive. Upload sessions are allowed on any single-label
183
+ * `*.googleapis.com` host, but only under `/upload/`.
184
+ */
185
+ const DRIVE_API_PREFIX = 'https://www.googleapis.com/'
186
+ const DRIVE_UPLOAD_URL = /^https:\/\/[a-z0-9-]+\.googleapis\.com\/upload\//
187
+
188
+ function isTrustedDriveUrl(url: string): boolean {
189
+ return url.startsWith(DRIVE_API_PREFIX) || DRIVE_UPLOAD_URL.test(url)
190
+ }
191
+
192
+ /** A path segment for a server-issued file id. */
193
+ function idPath(id: string): string {
194
+ return encodeURIComponent(id)
195
+ }
196
+
197
+ /**
198
+ * A multipart boundary that does not occur in `value`.
199
+ *
200
+ * A boundary that does appear in the content ends the part early: whatever
201
+ * follows it is parsed as another part, so a value containing
202
+ * `--<boundary>` would be truncated (or reshaped) on the way to Drive.
203
+ * `Math.random` is predictable enough that a crafted value can aim for it, so
204
+ * the boundary comes from `crypto.getRandomValues` where available and is
205
+ * checked against the content either way.
206
+ */
207
+ function multipartBoundary(value: string): string {
208
+ for (;;) {
209
+ const boundary = `rncs-${randomToken()}`
210
+ if (!value.includes(boundary)) return boundary
211
+ }
212
+ }
213
+
214
+ function randomToken(): string {
215
+ const cryptoImpl = (globalThis as { crypto?: { getRandomValues?: (a: Uint8Array) => Uint8Array } }).crypto
216
+ if (typeof cryptoImpl?.getRandomValues === 'function') {
217
+ const bytes = cryptoImpl.getRandomValues(new Uint8Array(16))
218
+ let out = ''
219
+ for (const b of bytes) out += b.toString(16).padStart(2, '0')
220
+ return out
221
+ }
222
+ let out = ''
223
+ for (let i = 0; i < 4; i += 1) out += Math.random().toString(36).slice(2)
224
+ return out
225
+ }
226
+
227
+ /**
228
+ * Names per `getItemsWithMeta` listing query. Drive caps the length of `q`;
229
+ * 50 quoted names stays well inside it even for long keys.
230
+ */
231
+ const NAMES_PER_QUERY = 50
232
+
233
+ /**
234
+ * Escapes a value for a single-quoted Drive query literal. Backslash FIRST, so
235
+ * it doesn't double-escape the backslash the quote-escaping just added.
236
+ */
237
+ function escapeQueryLiteral(value: string): string {
238
+ return value.replace(/\\/g, '\\\\').replace(/'/g, '\\\'')
239
+ }
240
+
151
241
  /** True for the 404 the client tags when a file id no longer resolves. */
152
242
  function isNotFound(e: unknown): boolean {
153
243
  return e instanceof CloudSyncError && e.serverErrorCode === 'NOT_FOUND'
@@ -156,7 +246,13 @@ function isNotFound(e: unknown): boolean {
156
246
  /** True for a transfer failure worth retrying after re-querying the real offset. */
157
247
  function isRetryableTransfer(e: unknown): boolean {
158
248
  return e instanceof CloudSyncError
159
- && (e.code === ErrorCode.NETWORK_UNAVAILABLE || e.code === ErrorCode.RATE_LIMITED)
249
+ && (
250
+ e.code === ErrorCode.NETWORK_UNAVAILABLE
251
+ || e.code === ErrorCode.RATE_LIMITED
252
+ // A chunk that ran out of time is the most common way a large upload on
253
+ // a slow link fails, and Drive may well have kept part of it.
254
+ || e.code === ErrorCode.TIMEOUT
255
+ )
160
256
  }
161
257
 
162
258
  export class GoogleDriveClient {
@@ -207,6 +303,15 @@ export class GoogleDriveClient {
207
303
  * caller inspects the response itself rather than getting a thrown rejection.
208
304
  */
209
305
  private async request(url: string, init: RequestInit, extraOkStatuses: number[] = []): Promise<Response> {
306
+ // Checked before the token is even fetched, so it can never be attached to
307
+ // a request bound anywhere else.
308
+ if (!isTrustedDriveUrl(url))
309
+ throw new CloudSyncError(
310
+ ErrorCode.UNKNOWN,
311
+ '[RNCloudSync] Refusing to send Google Drive credentials to a URL outside googleapis.com.',
312
+ { provider: 'googleDrive', serverErrorCode: 'UNTRUSTED_URL' }
313
+ )
314
+
210
315
  const token = await this.requireToken()
211
316
  const headers = new Headers(init.headers)
212
317
  headers.set('Authorization', `Bearer ${token}`)
@@ -268,19 +373,33 @@ export class GoogleDriveClient {
268
373
  { provider: 'googleDrive' }
269
374
  )
270
375
  }
271
- if (res.status === 429) {
376
+ if (res.status === 429)
272
377
  // The request-frequency case, distinct from the per-user 403 variants
273
378
  // above - this one really is a plain 429, no reason string to parse.
274
- const retryAfter = Number(res.headers.get('Retry-After'))
275
379
  throw new CloudSyncError(
276
380
  ErrorCode.RATE_LIMITED,
277
381
  '[RNCloudSync] Google Drive rate limited the request.',
278
382
  {
279
383
  provider: 'googleDrive',
280
- retryAfterMs: Number.isFinite(retryAfter) ? retryAfter * 1000 : undefined,
384
+ retryAfterMs: retryAfterMs(res),
281
385
  }
282
386
  )
283
- }
387
+
388
+ if (res.status === 500 || res.status === 502 || res.status === 503 || res.status === 504)
389
+ // Google documents every one of these as transient ("backend error") and
390
+ // asks clients to back off and retry - for a resumable upload, to query
391
+ // the offset and resume. Reporting them as a permanent UNKNOWN made the
392
+ // outbox drop writes and the chunk retry give up on a passing hiccup.
393
+ throw new CloudSyncError(
394
+ ErrorCode.RATE_LIMITED,
395
+ `[RNCloudSync] Google Drive returned HTTP ${res.status}; retry later.`,
396
+ {
397
+ provider: 'googleDrive',
398
+ serverErrorCode: `HTTP_${res.status}`,
399
+ retryAfterMs: retryAfterMs(res),
400
+ }
401
+ )
402
+
284
403
  // Tagged so callers can tell "this file id is gone" from a generic failure.
285
404
  // Without the tag a stale memoised id was indistinguishable from a real
286
405
  // error, so it could never be recovered from.
@@ -331,8 +450,7 @@ export class GoogleDriveClient {
331
450
  // would otherwise terminate the quoted literal and produce a malformed
332
451
  // query - and a literal backslash needs the same treatment, escaped FIRST
333
452
  // so it doesn't double-escape the backslash the quote-escaping just added.
334
- const escaped = name.replace(/\\/g, '\\\\').replace(/'/g, '\\\'')
335
- const q = encodeURIComponent(`name='${escaped}' and trashed=false`)
453
+ const q = encodeURIComponent(`name='${escapeQueryLiteral(name)}' and trashed=false`)
336
454
  const url = `${FILES_URL}?spaces=appDataFolder&q=${q}&fields=files(${FILE_FIELDS})&pageSize=10`
337
455
 
338
456
  const res = await this.request(url, { method: 'GET' })
@@ -389,11 +507,19 @@ export class GoogleDriveClient {
389
507
  }
390
508
 
391
509
  async getItem(name: string): Promise<string | null> {
392
- const id = await this.findFileId(name)
510
+ return await this.readItem(name, false)
511
+ }
512
+
513
+ /**
514
+ * `refresh` skips the memoised id and lists the name again, which is also
515
+ * what refreshes the cached metadata.
516
+ */
517
+ private async readItem(name: string, refresh: boolean): Promise<string | null> {
518
+ const id = await this.findFileId(name, refresh)
393
519
  if (id == null) return null
394
520
 
395
521
  try {
396
- const res = await this.request(`${FILES_URL}/${id}?alt=media`, { method: 'GET' })
522
+ const res = await this.request(`${FILES_URL}/${idPath(id)}?alt=media`, { method: 'GET' })
397
523
  return await res.text()
398
524
  }
399
525
  catch (e) {
@@ -406,7 +532,7 @@ export class GoogleDriveClient {
406
532
  const fresh = await this.findFileId(name, true)
407
533
  if (fresh == null) return null
408
534
 
409
- const res = await this.request(`${FILES_URL}/${fresh}?alt=media`, { method: 'GET' })
535
+ const res = await this.request(`${FILES_URL}/${idPath(fresh)}?alt=media`, { method: 'GET' })
410
536
  return await res.text()
411
537
  }
412
538
 
@@ -421,12 +547,94 @@ export class GoogleDriveClient {
421
547
  async getItemWithMeta(
422
548
  name: string
423
549
  ): Promise<{ value: string; modifiedAt?: number; version?: string } | null> {
424
- const value = await this.getItem(name)
550
+ // Always re-listed. The cached metadata is whatever the last listing saw,
551
+ // which can be arbitrarily old once another device has written - and a
552
+ // stale `modifiedAt` is exactly what makes a modified-time resolver rank
553
+ // a newer copy below an older one. One extra request is the price.
554
+ const value = await this.readItem(name, true)
425
555
  if (value == null) return null
426
556
  const meta = this.metaCache.get(name)
427
557
  return { value, modifiedAt: meta?.modifiedAt, version: meta?.version }
428
558
  }
429
559
 
560
+ /**
561
+ * {@link getItemWithMeta} for many names, with the metadata from one listing
562
+ * per batch of names rather than one per name.
563
+ *
564
+ * Drive has no batch endpoint for content, so each value is still its own
565
+ * download - but the id lookup, which is also where `modifiedTime` comes
566
+ * from, collapses into `(name='a' or name='b' ...)` queries. Results line up
567
+ * with `names`; a name with no file is `null`.
568
+ */
569
+ async getItemsWithMeta(
570
+ names: string[]
571
+ ): Promise<({ value: string; modifiedAt?: number; version?: string } | null)[]> {
572
+ if (names.length === 0) return []
573
+
574
+ const wanted = [...new Set(names)]
575
+ const byName = new Map<string, DriveFile[]>()
576
+
577
+ for (let i = 0; i < wanted.length; i += NAMES_PER_QUERY) {
578
+ const batch = wanted.slice(i, i + NAMES_PER_QUERY)
579
+ const clause = batch.map(n => `name='${escapeQueryLiteral(n)}'`).join(' or ')
580
+ let pageToken: string | undefined
581
+ do {
582
+ const params = new URLSearchParams({
583
+ spaces: 'appDataFolder',
584
+ q: `(${clause}) and trashed=false`,
585
+ fields: `nextPageToken,files(${FILE_FIELDS})`,
586
+ pageSize: '1000',
587
+ })
588
+ if (pageToken != null) params.set('pageToken', pageToken)
589
+ const res = await this.request(`${FILES_URL}?${params.toString()}`, { method: 'GET' })
590
+ const json = (await res.json()) as { files?: DriveFile[]; nextPageToken?: string }
591
+ for (const f of json.files ?? []) {
592
+ // `name=` is exact server-side, but filter anyway, as `findFileId` does.
593
+ if (!batch.includes(f.name)) continue
594
+ const group = byName.get(f.name)
595
+ if (group == null) byName.set(f.name, [f])
596
+ else group.push(f)
597
+ }
598
+ pageToken = json.nextPageToken
599
+ } while (pageToken != null)
600
+ }
601
+
602
+ const results = new Map<string, { value: string; modifiedAt?: number; version?: string } | null>()
603
+ for (const name of wanted) {
604
+ const files = byName.get(name)
605
+ if (files == null) {
606
+ this.idCache.delete(name)
607
+ this.metaCache.delete(name)
608
+ results.set(name, null)
609
+ continue
610
+ }
611
+
612
+ const file = files.length === 1 ? files[0] : this.pickAmongDuplicates(name, files)
613
+ this.remember(file)
614
+
615
+ let value: string | null
616
+ try {
617
+ const res = await this.request(`${FILES_URL}/${idPath(file.id)}?alt=media`, { method: 'GET' })
618
+ value = await res.text()
619
+ }
620
+ catch (e) {
621
+ // Deleted between the listing and the download: re-resolve this one
622
+ // name the slow way rather than failing the whole batch.
623
+ if (!isNotFound(e)) throw e
624
+ value = await this.readItem(name, true)
625
+ }
626
+
627
+ if (value == null) {
628
+ results.set(name, null)
629
+ continue
630
+ }
631
+ const meta = this.metaCache.get(name)
632
+ results.set(name, { value, modifiedAt: meta?.modifiedAt, version: meta?.version })
633
+ }
634
+
635
+ return names.map(n => results.get(n) ?? null)
636
+ }
637
+
430
638
  /**
431
639
  * Storage usage for the whole Google account.
432
640
  *
@@ -453,11 +661,18 @@ export class GoogleDriveClient {
453
661
 
454
662
  if (id != null)
455
663
  try {
456
- await this.request(`${UPLOAD_URL}/${id}?uploadType=media`, {
664
+ const res = await this.request(`${UPLOAD_URL}/${idPath(id)}?uploadType=media&fields=${FILE_FIELDS}`, {
457
665
  method: 'PATCH',
458
666
  headers: { 'Content-Type': 'application/json' },
459
667
  body: value,
460
668
  })
669
+ // The update bumped `modifiedTime`/`version`. Leaving the pre-write
670
+ // metadata cached made the next `getItemWithMeta` report this device's
671
+ // own fresh write with the old timestamp - which is exactly what a
672
+ // modified-time resolver then ranks below an older copy elsewhere.
673
+ const json = (await res.json().catch(() => null)) as Partial<DriveFile> | null
674
+ if (json?.id != null) this.remember({ ...json, id: json.id, name })
675
+ else this.metaCache.delete(name)
461
676
  return
462
677
  }
463
678
  catch (e) {
@@ -470,7 +685,7 @@ export class GoogleDriveClient {
470
685
 
471
686
  // Multipart create: metadata part pins the file into appDataFolder, second
472
687
  // part carries the content.
473
- const boundary = `rncs-${Math.random().toString(36).slice(2)}`
688
+ const boundary = multipartBoundary(value)
474
689
  const metadata = JSON.stringify({ name, parents: ['appDataFolder'] })
475
690
  const body
476
691
  = `--${boundary}\r\n`
@@ -578,7 +793,12 @@ export class GoogleDriveClient {
578
793
  ): Promise<{ sessionUrl: string; offset: number; completed: boolean; id?: string }> {
579
794
  const persisted = await this.config.sessionStore?.get(name)
580
795
 
581
- if (persisted != null && persisted.size === total)
796
+ if (
797
+ persisted != null
798
+ && persisted.size === total
799
+ && typeof persisted.sessionUrl === 'string'
800
+ && isTrustedDriveUrl(persisted.sessionUrl)
801
+ )
582
802
  try {
583
803
  const { offset, id } = await this.queryUploadOffset(persisted.sessionUrl, total)
584
804
  return { sessionUrl: persisted.sessionUrl, offset, completed: offset >= total, id }
@@ -588,14 +808,24 @@ export class GoogleDriveClient {
588
808
  }
589
809
 
590
810
  const existingId = await this.findFileId(name)
591
- const sessionUrl = await this.startResumableSession(name, existingId)
811
+ let sessionUrl: string
812
+ try {
813
+ sessionUrl = await this.startResumableSession(name, existingId)
814
+ }
815
+ catch (e) {
816
+ // A memoised id for a file another device has since deleted. Look the
817
+ // name up again (and create it if it really is gone) rather than failing
818
+ // every later upload of this name off the stale memo.
819
+ if (existingId == null || !isNotFound(e)) throw e
820
+ sessionUrl = await this.startResumableSession(name, await this.findFileId(name, true))
821
+ }
592
822
  await this.config.sessionStore?.set(name, { sessionUrl, size: total })
593
823
  return { sessionUrl, offset: 0, completed: false }
594
824
  }
595
825
 
596
826
  private async startResumableSession(name: string, existingId: string | null): Promise<string> {
597
827
  const url = existingId != null
598
- ? `${UPLOAD_URL}/${existingId}?uploadType=resumable`
828
+ ? `${UPLOAD_URL}/${idPath(existingId)}?uploadType=resumable`
599
829
  : `${UPLOAD_URL}?uploadType=resumable&fields=id`
600
830
  const metadata = existingId != null ? {} : { name, parents: ['appDataFolder'] }
601
831
 
@@ -612,6 +842,13 @@ export class GoogleDriveClient {
612
842
  '[RNCloudSync] Google Drive did not return a resumable session URI.',
613
843
  { provider: 'googleDrive' }
614
844
  )
845
+ // Persisted and later sent the bearer token, so it has to be a Drive URL.
846
+ if (!isTrustedDriveUrl(location))
847
+ throw new CloudSyncError(
848
+ ErrorCode.UNKNOWN,
849
+ '[RNCloudSync] Google Drive returned a resumable session URI outside googleapis.com.',
850
+ { provider: 'googleDrive', serverErrorCode: 'UNTRUSTED_URL' }
851
+ )
615
852
 
616
853
  return location
617
854
  }
@@ -652,6 +889,14 @@ export class GoogleDriveClient {
652
889
  onProgress?: (bytesTransferred: number, bytesTotal: number) => void
653
890
  ): Promise<{ nextOffset: number; id?: string }> {
654
891
  const bytes = await source.read(offset, length)
892
+ // A source that hands back nothing for a non-empty range would otherwise
893
+ // never advance the offset, and `uploadFile` would loop forever.
894
+ if (length > 0 && bytes.length === 0)
895
+ throw new CloudSyncError(
896
+ ErrorCode.UNKNOWN,
897
+ `[RNCloudSync] The upload source returned no bytes at offset ${offset} of ${total}.`,
898
+ { provider: 'googleDrive' }
899
+ )
655
900
  const range = total === 0 ? 'bytes */0' : `bytes ${offset}-${offset + bytes.length - 1}/${total}`
656
901
 
657
902
  const res = await this.request(sessionUrl, {
@@ -663,8 +908,13 @@ export class GoogleDriveClient {
663
908
  body: bytes as BodyInit,
664
909
  }, [308])
665
910
 
666
- const nextOffset = offset + bytes.length
667
911
  if (res.status === 308) {
912
+ // Drive may persist fewer bytes than were sent, and says so in `Range`.
913
+ // Resuming from `offset + bytes.length` regardless would skip whatever it
914
+ // dropped and leave a hole in the uploaded file. Only fall back to the
915
+ // sent length when the header is missing entirely.
916
+ const persisted = parsePersistedRange(res.headers.get('Range'))
917
+ const nextOffset = persisted ?? offset + bytes.length
668
918
  onProgress?.(nextOffset, total)
669
919
  return { nextOffset }
670
920
  }
@@ -696,11 +946,7 @@ export class GoogleDriveClient {
696
946
  return { offset: total, id: json?.id ?? undefined }
697
947
  }
698
948
 
699
- const range = res.headers.get('Range') // "bytes=0-8388607"
700
- if (range == null) return { offset: 0 }
701
-
702
- const match = /bytes=0-(\d+)/.exec(range)
703
- return { offset: match ? Number(match[1]) + 1 : 0 }
949
+ return { offset: parsePersistedRange(res.headers.get('Range')) ?? 0 }
704
950
  }
705
951
 
706
952
  /**
@@ -716,10 +962,22 @@ export class GoogleDriveClient {
716
962
  signal?: AbortLike
717
963
  ): Promise<boolean> {
718
964
  throwIfAborted(signal, 'googleDrive')
719
- const id = await this.findFileId(name)
965
+ let id = await this.findFileId(name)
720
966
  if (id == null) return false
721
967
 
722
- const total = await this.fileSize(id)
968
+ let total: number
969
+ try {
970
+ total = await this.fileSize(id)
971
+ }
972
+ catch (e) {
973
+ // Same stale-memo case `getItem` recovers from: another device deleted
974
+ // (or replaced) the file. Without this every later download of the name
975
+ // failed off the cached id until the app restarted.
976
+ if (!isNotFound(e)) throw e
977
+ id = await this.findFileId(name, true)
978
+ if (id == null) return false
979
+ total = await this.fileSize(id)
980
+ }
723
981
 
724
982
  if (total === 0) {
725
983
  await sink.write(new Uint8Array(0), true)
@@ -732,11 +990,18 @@ export class GoogleDriveClient {
732
990
  while (offset < total) {
733
991
  throwIfAborted(signal, 'googleDrive')
734
992
  const end = Math.min(offset + this.chunkBytes, total) - 1
735
- const res = await this.request(`${FILES_URL}/${id}?alt=media`, {
993
+ const res = await this.request(`${FILES_URL}/${idPath(id)}?alt=media`, {
736
994
  method: 'GET',
737
995
  headers: { Range: `bytes=${offset}-${end}` },
738
996
  })
739
997
  const bytes = new Uint8Array(await res.arrayBuffer())
998
+ // Same guard as the upload side: an empty body would never advance.
999
+ if (bytes.length === 0)
1000
+ throw new CloudSyncError(
1001
+ ErrorCode.UNKNOWN,
1002
+ `[RNCloudSync] Google Drive returned no bytes at offset ${offset} of ${total}.`,
1003
+ { provider: 'googleDrive' }
1004
+ )
740
1005
  await sink.write(bytes, isFirstChunk)
741
1006
  isFirstChunk = false
742
1007
  offset += bytes.length
@@ -747,9 +1012,20 @@ export class GoogleDriveClient {
747
1012
  }
748
1013
 
749
1014
  private async fileSize(id: string): Promise<number> {
750
- const res = await this.request(`${FILES_URL}/${id}?fields=size`, { method: 'GET' })
1015
+ const res = await this.request(`${FILES_URL}/${idPath(id)}?fields=size`, { method: 'GET' })
751
1016
  const json = (await res.json()) as { size?: string }
752
- return json.size != null ? Number(json.size) : 0
1017
+ if (json.size == null) return 0
1018
+ const size = Number(json.size)
1019
+ // A size that is not a non-negative integer would make the chunk loop
1020
+ // either never run - reporting a download that wrote nothing as complete -
1021
+ // or compute nonsense ranges.
1022
+ if (!Number.isSafeInteger(size) || size < 0)
1023
+ throw new CloudSyncError(
1024
+ ErrorCode.UNKNOWN,
1025
+ '[RNCloudSync] Google Drive reported an invalid file size.',
1026
+ { provider: 'googleDrive' }
1027
+ )
1028
+ return size
753
1029
  }
754
1030
 
755
1031
  async removeItem(name: string): Promise<void> {
@@ -758,7 +1034,7 @@ export class GoogleDriveClient {
758
1034
  if (id == null) return
759
1035
 
760
1036
  try {
761
- await this.request(`${FILES_URL}/${id}`, { method: 'DELETE' })
1037
+ await this.request(`${FILES_URL}/${idPath(id)}`, { method: 'DELETE' })
762
1038
  }
763
1039
  catch (e) {
764
1040
  // Deleted from under us: the end state the caller asked for already holds.
@@ -771,7 +1047,7 @@ export class GoogleDriveClient {
771
1047
  }
772
1048
 
773
1049
  async getAllKeys(): Promise<string[]> {
774
- const names: string[] = []
1050
+ const byName = new Map<string, DriveFile[]>()
775
1051
  let pageToken: string | undefined
776
1052
 
777
1053
  // Paginate rather than assuming one page holds everything.
@@ -788,13 +1064,31 @@ export class GoogleDriveClient {
788
1064
  const json = (await res.json()) as { files?: DriveFile[]; nextPageToken?: string }
789
1065
 
790
1066
  for (const f of json.files ?? []) {
791
- names.push(f.name)
792
- this.remember(f)
1067
+ const group = byName.get(f.name)
1068
+ if (group == null) byName.set(f.name, [f])
1069
+ else group.push(f)
793
1070
  }
794
1071
  pageToken = json.nextPageToken
795
1072
  } while (pageToken != null)
796
1073
 
797
- return names
1074
+ // Drive names are not unique. Memoising whichever duplicate happened to be
1075
+ // listed last bypassed `pickAmongDuplicates` entirely - the next `getItem`
1076
+ // served that arbitrary file from the cache - and the same key came back
1077
+ // once per copy.
1078
+ for (const [name, files] of byName)
1079
+ if (files.length === 1) {
1080
+ this.remember(files[0])
1081
+ }
1082
+ else if (this.config.onDuplicateName !== 'error') {
1083
+ this.remember(this.pickAmongDuplicates(name, files))
1084
+ }
1085
+ else {
1086
+ // Leave it uncached so the next read raises the CONFLICT itself.
1087
+ this.idCache.delete(name)
1088
+ this.metaCache.delete(name)
1089
+ }
1090
+
1091
+ return [...byName.keys()]
798
1092
  }
799
1093
 
800
1094
  /**
@@ -805,7 +1099,10 @@ export class GoogleDriveClient {
805
1099
  */
806
1100
  async getStartPageToken(): Promise<string> {
807
1101
  const res = await this.request(
808
- `${CHANGES_URL}/startPageToken?spaces=appDataFolder`,
1102
+ // No `spaces` here: `changes.getStartPageToken` takes only the shared
1103
+ // drive parameters. The cursor covers every space; `changes.list` is
1104
+ // where `spaces=appDataFolder` scopes the feed.
1105
+ `${CHANGES_URL}/startPageToken`,
809
1106
  { method: 'GET' }
810
1107
  )
811
1108
  const json = (await res.json()) as { startPageToken?: string }
@@ -863,10 +1160,14 @@ export class GoogleDriveClient {
863
1160
  // A file that went away must not keep answering reads from the id
864
1161
  // cache, and one that changed must be re-read rather than served
865
1162
  // from a stale memo.
866
- if (change.removed === true || change.file?.trashed === true) {
867
- this.idCache.delete(name)
868
- this.metaCache.delete(name)
869
- }
1163
+ // Dropped on every change, not only a removal. A changed file keeps
1164
+ // its id but not its cached modification time, and a change to a
1165
+ // *different* file under a cached name - another device created a
1166
+ // same-named copy - has to go back through the listing or the
1167
+ // duplicate resolution never runs and this device keeps reading its
1168
+ // old copy forever. One listing on the next read is the cost.
1169
+ this.idCache.delete(name)
1170
+ this.metaCache.delete(name)
870
1171
  continue
871
1172
  }
872
1173
 
@@ -128,33 +128,59 @@ export function assertKey(key: string, providers: readonly ProviderName[]): void
128
128
  * Rewrites an arbitrary string into a key that passes {@link checkKey}.
129
129
  *
130
130
  * For callers whose keys come from somewhere they do not control - a file name,
131
- * a user-entered label, an id from another system. Illegal characters become
132
- * `-`, and anything too long is truncated and suffixed with a short hash of the
133
- * original so two different long keys cannot collide.
131
+ * a user-entered label, an id from another system.
132
+ *
133
+ * A key that is already legal and fits in `maxBytes` comes back unchanged.
134
+ * Anything else has its illegal characters replaced with `-`, is truncated to
135
+ * leave room, and gets a 64-bit hash of the *original* string appended as
136
+ * `--<14 base36 chars>`. The hash is what keeps distinct inputs distinct:
137
+ * replacement alone maps `"a b"` and `"a-b"` onto the same key, and truncation
138
+ * alone maps every long key with a shared prefix onto one.
139
+ *
140
+ * A legal input that happens to end in that suffix shape is hashed too, so a
141
+ * passed-through key can never equal a rewritten one. The mapping is therefore
142
+ * injective up to a 64-bit hash collision.
143
+ *
144
+ * Throws a `RangeError` when `maxBytes` is smaller than the suffix itself
145
+ * ({@link SANITIZED_SUFFIX_BYTES}), since no output could then both fit and
146
+ * stay distinct.
134
147
  */
135
148
  export function sanitizeKey(raw: string, maxBytes = KV_MAX_KEY_BYTES): string {
149
+ if (!Number.isFinite(maxBytes) || maxBytes < SANITIZED_SUFFIX_BYTES)
150
+ throw new RangeError(
151
+ `[RNCloudSync] sanitizeKey needs maxBytes >= ${SANITIZED_SUFFIX_BYTES} (got ${maxBytes}).`
152
+ )
153
+
154
+ const legal = raw.length > 0
155
+ && SAFE_KEY.test(raw)
156
+ && !raw.startsWith(RESERVED_PREFIX)
157
+ && byteLength(raw) <= maxBytes
158
+ && !HASHED_SUFFIX.test(raw)
159
+ if (legal) return raw
160
+
161
+ const suffix = `--${hash64(raw)}`
162
+ // Only ASCII survives the replacement, so characters and bytes agree here.
136
163
  const cleaned = raw
137
164
  .replace(/[^A-Za-z0-9._-]/g, '-')
138
165
  .replace(/^_+/, '')
166
+ return cleaned.slice(0, maxBytes - suffix.length) + suffix
167
+ }
168
+
169
+ /** `--` plus two zero-padded 7-character base36 halves. */
170
+ export const SANITIZED_SUFFIX_BYTES = 16
139
171
 
140
- const base = cleaned.length > 0 ? cleaned : 'key'
141
- if (byteLength(base) <= maxBytes) return base
172
+ const HASHED_SUFFIX = /--[0-9a-z]{14}$/
142
173
 
143
- // Truncating alone would map every long key with a shared prefix onto the
144
- // same short key, silently merging unrelated values.
145
- const suffix = `-${hash(raw)}`
146
- const room = Math.max(1, maxBytes - suffix.length)
147
- let head = base.slice(0, room)
148
- while (byteLength(head) > room) head = head.slice(0, -1)
149
- return head + suffix
174
+ /** Two independently seeded FNV-1a passes. Not cryptographic - only separates inputs. */
175
+ function hash64(s: string): string {
176
+ return fnv(s, 0x811c9dc5) + fnv(s, 0x050c5d1f)
150
177
  }
151
178
 
152
- /** FNV-1a, base36. Not cryptographic - only needs to separate collisions. */
153
- function hash(s: string): string {
154
- let h = 0x811c9dc5
179
+ function fnv(s: string, seed: number): string {
180
+ let h = seed
155
181
  for (let i = 0; i < s.length; i += 1) {
156
182
  h ^= s.charCodeAt(i)
157
183
  h = Math.imul(h, 0x01000193) >>> 0
158
184
  }
159
- return h.toString(36)
185
+ return h.toString(36).padStart(7, '0')
160
186
  }