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,6 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  import { CloudSyncError, ErrorCode, isCancelled } from "../errors.js";
4
+ import { clampRetryAfterMs } from "./records.js";
4
5
  import { throwIfAborted, withTimeout } from "./timeout.js";
5
6
 
6
7
  /**
@@ -54,6 +55,93 @@ const DEFAULT_TIMEOUT_MS = 60_000;
54
55
  /** The `fields` mask every listing uses, so a read also learns modification time. */
55
56
  const FILE_FIELDS = 'id,name,modifiedTime,version';
56
57
 
58
+ /**
59
+ * The next offset a resumable session expects, from a 308's `Range` header
60
+ * ("bytes=0-8388607" -> 8388608). Undefined when there is no usable header.
61
+ */
62
+ function parsePersistedRange(range) {
63
+ if (range == null) return undefined;
64
+ const match = /bytes=0-(\d+)/.exec(range);
65
+ return match ? Number(match[1]) + 1 : undefined;
66
+ }
67
+
68
+ /**
69
+ * `Retry-After` in milliseconds, or undefined when absent or not a number of
70
+ * seconds. Checked for absence first: `Number(null)` is 0, which would tell
71
+ * the backoff "retry immediately" whenever Drive simply sent no header.
72
+ */
73
+ function retryAfterMs(res) {
74
+ const header = res.headers.get('Retry-After');
75
+ if (header == null || header.trim() === '') return undefined;
76
+ const seconds = Number(header);
77
+ return Number.isFinite(seconds) ? clampRetryAfterMs(seconds * 1000) : undefined;
78
+ }
79
+
80
+ /**
81
+ * The only origins the bearer token is ever sent to.
82
+ *
83
+ * Every Drive endpoint lives on `www.googleapis.com`. The one URL this client
84
+ * does not build itself - the resumable-session URI - comes from a `Location`
85
+ * header or, across restarts, from the host's `sessionStore`. Neither is a
86
+ * place to take a destination for an OAuth token on trust: a tampered store
87
+ * entry or a misbehaving proxy would otherwise receive `Authorization: Bearer`
88
+ * for the user's Drive. Upload sessions are allowed on any single-label
89
+ * `*.googleapis.com` host, but only under `/upload/`.
90
+ */
91
+ const DRIVE_API_PREFIX = 'https://www.googleapis.com/';
92
+ const DRIVE_UPLOAD_URL = /^https:\/\/[a-z0-9-]+\.googleapis\.com\/upload\//;
93
+ function isTrustedDriveUrl(url) {
94
+ return url.startsWith(DRIVE_API_PREFIX) || DRIVE_UPLOAD_URL.test(url);
95
+ }
96
+
97
+ /** A path segment for a server-issued file id. */
98
+ function idPath(id) {
99
+ return encodeURIComponent(id);
100
+ }
101
+
102
+ /**
103
+ * A multipart boundary that does not occur in `value`.
104
+ *
105
+ * A boundary that does appear in the content ends the part early: whatever
106
+ * follows it is parsed as another part, so a value containing
107
+ * `--<boundary>` would be truncated (or reshaped) on the way to Drive.
108
+ * `Math.random` is predictable enough that a crafted value can aim for it, so
109
+ * the boundary comes from `crypto.getRandomValues` where available and is
110
+ * checked against the content either way.
111
+ */
112
+ function multipartBoundary(value) {
113
+ for (;;) {
114
+ const boundary = `rncs-${randomToken()}`;
115
+ if (!value.includes(boundary)) return boundary;
116
+ }
117
+ }
118
+ function randomToken() {
119
+ const cryptoImpl = globalThis.crypto;
120
+ if (typeof cryptoImpl?.getRandomValues === 'function') {
121
+ const bytes = cryptoImpl.getRandomValues(new Uint8Array(16));
122
+ let out = '';
123
+ for (const b of bytes) out += b.toString(16).padStart(2, '0');
124
+ return out;
125
+ }
126
+ let out = '';
127
+ for (let i = 0; i < 4; i += 1) out += Math.random().toString(36).slice(2);
128
+ return out;
129
+ }
130
+
131
+ /**
132
+ * Names per `getItemsWithMeta` listing query. Drive caps the length of `q`;
133
+ * 50 quoted names stays well inside it even for long keys.
134
+ */
135
+ const NAMES_PER_QUERY = 50;
136
+
137
+ /**
138
+ * Escapes a value for a single-quoted Drive query literal. Backslash FIRST, so
139
+ * it doesn't double-escape the backslash the quote-escaping just added.
140
+ */
141
+ function escapeQueryLiteral(value) {
142
+ return value.replace(/\\/g, '\\\\').replace(/'/g, '\\\'');
143
+ }
144
+
57
145
  /** True for the 404 the client tags when a file id no longer resolves. */
58
146
  function isNotFound(e) {
59
147
  return e instanceof CloudSyncError && e.serverErrorCode === 'NOT_FOUND';
@@ -61,7 +149,10 @@ function isNotFound(e) {
61
149
 
62
150
  /** True for a transfer failure worth retrying after re-querying the real offset. */
63
151
  function isRetryableTransfer(e) {
64
- return e instanceof CloudSyncError && (e.code === ErrorCode.NETWORK_UNAVAILABLE || e.code === ErrorCode.RATE_LIMITED);
152
+ return e instanceof CloudSyncError && (e.code === ErrorCode.NETWORK_UNAVAILABLE || e.code === ErrorCode.RATE_LIMITED
153
+ // A chunk that ran out of time is the most common way a large upload on
154
+ // a slow link fails, and Drive may well have kept part of it.
155
+ || e.code === ErrorCode.TIMEOUT);
65
156
  }
66
157
  export class GoogleDriveClient {
67
158
  /**
@@ -102,6 +193,12 @@ export class GoogleDriveClient {
102
193
  * caller inspects the response itself rather than getting a thrown rejection.
103
194
  */
104
195
  async request(url, init, extraOkStatuses = []) {
196
+ // Checked before the token is even fetched, so it can never be attached to
197
+ // a request bound anywhere else.
198
+ if (!isTrustedDriveUrl(url)) throw new CloudSyncError(ErrorCode.UNKNOWN, '[RNCloudSync] Refusing to send Google Drive credentials to a URL outside googleapis.com.', {
199
+ provider: 'googleDrive',
200
+ serverErrorCode: 'UNTRUSTED_URL'
201
+ });
105
202
  const token = await this.requireToken();
106
203
  const headers = new Headers(init.headers);
107
204
  headers.set('Authorization', `Bearer ${token}`);
@@ -145,15 +242,24 @@ export class GoogleDriveClient {
145
242
  provider: 'googleDrive'
146
243
  });
147
244
  }
148
- if (res.status === 429) {
245
+ if (res.status === 429)
149
246
  // The request-frequency case, distinct from the per-user 403 variants
150
247
  // above - this one really is a plain 429, no reason string to parse.
151
- const retryAfter = Number(res.headers.get('Retry-After'));
152
248
  throw new CloudSyncError(ErrorCode.RATE_LIMITED, '[RNCloudSync] Google Drive rate limited the request.', {
153
249
  provider: 'googleDrive',
154
- retryAfterMs: Number.isFinite(retryAfter) ? retryAfter * 1000 : undefined
250
+ retryAfterMs: retryAfterMs(res)
155
251
  });
156
- }
252
+ if (res.status === 500 || res.status === 502 || res.status === 503 || res.status === 504)
253
+ // Google documents every one of these as transient ("backend error") and
254
+ // asks clients to back off and retry - for a resumable upload, to query
255
+ // the offset and resume. Reporting them as a permanent UNKNOWN made the
256
+ // outbox drop writes and the chunk retry give up on a passing hiccup.
257
+ throw new CloudSyncError(ErrorCode.RATE_LIMITED, `[RNCloudSync] Google Drive returned HTTP ${res.status}; retry later.`, {
258
+ provider: 'googleDrive',
259
+ serverErrorCode: `HTTP_${res.status}`,
260
+ retryAfterMs: retryAfterMs(res)
261
+ });
262
+
157
263
  // Tagged so callers can tell "this file id is gone" from a generic failure.
158
264
  // Without the tag a stale memoised id was indistinguishable from a real
159
265
  // error, so it could never be recovered from.
@@ -198,8 +304,7 @@ export class GoogleDriveClient {
198
304
  // would otherwise terminate the quoted literal and produce a malformed
199
305
  // query - and a literal backslash needs the same treatment, escaped FIRST
200
306
  // so it doesn't double-escape the backslash the quote-escaping just added.
201
- const escaped = name.replace(/\\/g, '\\\\').replace(/'/g, '\\\'');
202
- const q = encodeURIComponent(`name='${escaped}' and trashed=false`);
307
+ const q = encodeURIComponent(`name='${escapeQueryLiteral(name)}' and trashed=false`);
203
308
  const url = `${FILES_URL}?spaces=appDataFolder&q=${q}&fields=files(${FILE_FIELDS})&pageSize=10`;
204
309
  const res = await this.request(url, {
205
310
  method: 'GET'
@@ -249,10 +354,18 @@ export class GoogleDriveClient {
249
354
  });
250
355
  }
251
356
  async getItem(name) {
252
- const id = await this.findFileId(name);
357
+ return await this.readItem(name, false);
358
+ }
359
+
360
+ /**
361
+ * `refresh` skips the memoised id and lists the name again, which is also
362
+ * what refreshes the cached metadata.
363
+ */
364
+ async readItem(name, refresh) {
365
+ const id = await this.findFileId(name, refresh);
253
366
  if (id == null) return null;
254
367
  try {
255
- const res = await this.request(`${FILES_URL}/${id}?alt=media`, {
368
+ const res = await this.request(`${FILES_URL}/${idPath(id)}?alt=media`, {
256
369
  method: 'GET'
257
370
  });
258
371
  return await res.text();
@@ -265,7 +378,7 @@ export class GoogleDriveClient {
265
378
  // forever, and report a genuine absence as absent.
266
379
  const fresh = await this.findFileId(name, true);
267
380
  if (fresh == null) return null;
268
- const res = await this.request(`${FILES_URL}/${fresh}?alt=media`, {
381
+ const res = await this.request(`${FILES_URL}/${idPath(fresh)}?alt=media`, {
269
382
  method: 'GET'
270
383
  });
271
384
  return await res.text();
@@ -280,7 +393,11 @@ export class GoogleDriveClient {
280
393
  * timestamp in its payload.
281
394
  */
282
395
  async getItemWithMeta(name) {
283
- const value = await this.getItem(name);
396
+ // Always re-listed. The cached metadata is whatever the last listing saw,
397
+ // which can be arbitrarily old once another device has written - and a
398
+ // stale `modifiedAt` is exactly what makes a modified-time resolver rank
399
+ // a newer copy below an older one. One extra request is the price.
400
+ const value = await this.readItem(name, true);
284
401
  if (value == null) return null;
285
402
  const meta = this.metaCache.get(name);
286
403
  return {
@@ -290,6 +407,81 @@ export class GoogleDriveClient {
290
407
  };
291
408
  }
292
409
 
410
+ /**
411
+ * {@link getItemWithMeta} for many names, with the metadata from one listing
412
+ * per batch of names rather than one per name.
413
+ *
414
+ * Drive has no batch endpoint for content, so each value is still its own
415
+ * download - but the id lookup, which is also where `modifiedTime` comes
416
+ * from, collapses into `(name='a' or name='b' ...)` queries. Results line up
417
+ * with `names`; a name with no file is `null`.
418
+ */
419
+ async getItemsWithMeta(names) {
420
+ if (names.length === 0) return [];
421
+ const wanted = [...new Set(names)];
422
+ const byName = new Map();
423
+ for (let i = 0; i < wanted.length; i += NAMES_PER_QUERY) {
424
+ const batch = wanted.slice(i, i + NAMES_PER_QUERY);
425
+ const clause = batch.map(n => `name='${escapeQueryLiteral(n)}'`).join(' or ');
426
+ let pageToken;
427
+ do {
428
+ const params = new URLSearchParams({
429
+ spaces: 'appDataFolder',
430
+ q: `(${clause}) and trashed=false`,
431
+ fields: `nextPageToken,files(${FILE_FIELDS})`,
432
+ pageSize: '1000'
433
+ });
434
+ if (pageToken != null) params.set('pageToken', pageToken);
435
+ const res = await this.request(`${FILES_URL}?${params.toString()}`, {
436
+ method: 'GET'
437
+ });
438
+ const json = await res.json();
439
+ for (const f of json.files ?? []) {
440
+ // `name=` is exact server-side, but filter anyway, as `findFileId` does.
441
+ if (!batch.includes(f.name)) continue;
442
+ const group = byName.get(f.name);
443
+ if (group == null) byName.set(f.name, [f]);else group.push(f);
444
+ }
445
+ pageToken = json.nextPageToken;
446
+ } while (pageToken != null);
447
+ }
448
+ const results = new Map();
449
+ for (const name of wanted) {
450
+ const files = byName.get(name);
451
+ if (files == null) {
452
+ this.idCache.delete(name);
453
+ this.metaCache.delete(name);
454
+ results.set(name, null);
455
+ continue;
456
+ }
457
+ const file = files.length === 1 ? files[0] : this.pickAmongDuplicates(name, files);
458
+ this.remember(file);
459
+ let value;
460
+ try {
461
+ const res = await this.request(`${FILES_URL}/${idPath(file.id)}?alt=media`, {
462
+ method: 'GET'
463
+ });
464
+ value = await res.text();
465
+ } catch (e) {
466
+ // Deleted between the listing and the download: re-resolve this one
467
+ // name the slow way rather than failing the whole batch.
468
+ if (!isNotFound(e)) throw e;
469
+ value = await this.readItem(name, true);
470
+ }
471
+ if (value == null) {
472
+ results.set(name, null);
473
+ continue;
474
+ }
475
+ const meta = this.metaCache.get(name);
476
+ results.set(name, {
477
+ value,
478
+ modifiedAt: meta?.modifiedAt,
479
+ version: meta?.version
480
+ });
481
+ }
482
+ return names.map(n => results.get(n) ?? null);
483
+ }
484
+
293
485
  /**
294
486
  * Storage usage for the whole Google account.
295
487
  *
@@ -313,13 +505,23 @@ export class GoogleDriveClient {
313
505
  async setItem(name, value) {
314
506
  const id = await this.findFileId(name);
315
507
  if (id != null) try {
316
- await this.request(`${UPLOAD_URL}/${id}?uploadType=media`, {
508
+ const res = await this.request(`${UPLOAD_URL}/${idPath(id)}?uploadType=media&fields=${FILE_FIELDS}`, {
317
509
  method: 'PATCH',
318
510
  headers: {
319
511
  'Content-Type': 'application/json'
320
512
  },
321
513
  body: value
322
514
  });
515
+ // The update bumped `modifiedTime`/`version`. Leaving the pre-write
516
+ // metadata cached made the next `getItemWithMeta` report this device's
517
+ // own fresh write with the old timestamp - which is exactly what a
518
+ // modified-time resolver then ranks below an older copy elsewhere.
519
+ const json = await res.json().catch(() => null);
520
+ if (json?.id != null) this.remember({
521
+ ...json,
522
+ id: json.id,
523
+ name
524
+ });else this.metaCache.delete(name);
323
525
  return;
324
526
  } catch (e) {
325
527
  // Same stale-memo case. Fall through and create the file again instead
@@ -331,7 +533,7 @@ export class GoogleDriveClient {
331
533
 
332
534
  // Multipart create: metadata part pins the file into appDataFolder, second
333
535
  // part carries the content.
334
- const boundary = `rncs-${Math.random().toString(36).slice(2)}`;
536
+ const boundary = multipartBoundary(value);
335
537
  const metadata = JSON.stringify({
336
538
  name,
337
539
  parents: ['appDataFolder']
@@ -430,7 +632,7 @@ export class GoogleDriveClient {
430
632
  */
431
633
  async resumeOrStartSession(name, total) {
432
634
  const persisted = await this.config.sessionStore?.get(name);
433
- if (persisted != null && persisted.size === total) try {
635
+ if (persisted != null && persisted.size === total && typeof persisted.sessionUrl === 'string' && isTrustedDriveUrl(persisted.sessionUrl)) try {
434
636
  const {
435
637
  offset,
436
638
  id
@@ -445,7 +647,16 @@ export class GoogleDriveClient {
445
647
  // Falls through to starting a fresh session below.
446
648
  }
447
649
  const existingId = await this.findFileId(name);
448
- const sessionUrl = await this.startResumableSession(name, existingId);
650
+ let sessionUrl;
651
+ try {
652
+ sessionUrl = await this.startResumableSession(name, existingId);
653
+ } catch (e) {
654
+ // A memoised id for a file another device has since deleted. Look the
655
+ // name up again (and create it if it really is gone) rather than failing
656
+ // every later upload of this name off the stale memo.
657
+ if (existingId == null || !isNotFound(e)) throw e;
658
+ sessionUrl = await this.startResumableSession(name, await this.findFileId(name, true));
659
+ }
449
660
  await this.config.sessionStore?.set(name, {
450
661
  sessionUrl,
451
662
  size: total
@@ -457,7 +668,7 @@ export class GoogleDriveClient {
457
668
  };
458
669
  }
459
670
  async startResumableSession(name, existingId) {
460
- const url = existingId != null ? `${UPLOAD_URL}/${existingId}?uploadType=resumable` : `${UPLOAD_URL}?uploadType=resumable&fields=id`;
671
+ const url = existingId != null ? `${UPLOAD_URL}/${idPath(existingId)}?uploadType=resumable` : `${UPLOAD_URL}?uploadType=resumable&fields=id`;
461
672
  const metadata = existingId != null ? {} : {
462
673
  name,
463
674
  parents: ['appDataFolder']
@@ -473,6 +684,11 @@ export class GoogleDriveClient {
473
684
  if (location == null) throw new CloudSyncError(ErrorCode.UNKNOWN, '[RNCloudSync] Google Drive did not return a resumable session URI.', {
474
685
  provider: 'googleDrive'
475
686
  });
687
+ // Persisted and later sent the bearer token, so it has to be a Drive URL.
688
+ if (!isTrustedDriveUrl(location)) throw new CloudSyncError(ErrorCode.UNKNOWN, '[RNCloudSync] Google Drive returned a resumable session URI outside googleapis.com.', {
689
+ provider: 'googleDrive',
690
+ serverErrorCode: 'UNTRUSTED_URL'
691
+ });
476
692
  return location;
477
693
  }
478
694
 
@@ -499,6 +715,11 @@ export class GoogleDriveClient {
499
715
  }
500
716
  async putChunk(sessionUrl, source, offset, length, total, onProgress) {
501
717
  const bytes = await source.read(offset, length);
718
+ // A source that hands back nothing for a non-empty range would otherwise
719
+ // never advance the offset, and `uploadFile` would loop forever.
720
+ if (length > 0 && bytes.length === 0) throw new CloudSyncError(ErrorCode.UNKNOWN, `[RNCloudSync] The upload source returned no bytes at offset ${offset} of ${total}.`, {
721
+ provider: 'googleDrive'
722
+ });
502
723
  const range = total === 0 ? 'bytes */0' : `bytes ${offset}-${offset + bytes.length - 1}/${total}`;
503
724
  const res = await this.request(sessionUrl, {
504
725
  method: 'PUT',
@@ -510,8 +731,13 @@ export class GoogleDriveClient {
510
731
  // match even though every fetch implementation accepts it at runtime.
511
732
  body: bytes
512
733
  }, [308]);
513
- const nextOffset = offset + bytes.length;
514
734
  if (res.status === 308) {
735
+ // Drive may persist fewer bytes than were sent, and says so in `Range`.
736
+ // Resuming from `offset + bytes.length` regardless would skip whatever it
737
+ // dropped and leave a hole in the uploaded file. Only fall back to the
738
+ // sent length when the header is missing entirely.
739
+ const persisted = parsePersistedRange(res.headers.get('Range'));
740
+ const nextOffset = persisted ?? offset + bytes.length;
515
741
  onProgress?.(nextOffset, total);
516
742
  return {
517
743
  nextOffset
@@ -551,13 +777,8 @@ export class GoogleDriveClient {
551
777
  id: json?.id ?? undefined
552
778
  };
553
779
  }
554
- const range = res.headers.get('Range'); // "bytes=0-8388607"
555
- if (range == null) return {
556
- offset: 0
557
- };
558
- const match = /bytes=0-(\d+)/.exec(range);
559
780
  return {
560
- offset: match ? Number(match[1]) + 1 : 0
781
+ offset: parsePersistedRange(res.headers.get('Range')) ?? 0
561
782
  };
562
783
  }
563
784
 
@@ -569,9 +790,20 @@ export class GoogleDriveClient {
569
790
  */
570
791
  async downloadFile(name, sink, onProgress, signal) {
571
792
  throwIfAborted(signal, 'googleDrive');
572
- const id = await this.findFileId(name);
793
+ let id = await this.findFileId(name);
573
794
  if (id == null) return false;
574
- const total = await this.fileSize(id);
795
+ let total;
796
+ try {
797
+ total = await this.fileSize(id);
798
+ } catch (e) {
799
+ // Same stale-memo case `getItem` recovers from: another device deleted
800
+ // (or replaced) the file. Without this every later download of the name
801
+ // failed off the cached id until the app restarted.
802
+ if (!isNotFound(e)) throw e;
803
+ id = await this.findFileId(name, true);
804
+ if (id == null) return false;
805
+ total = await this.fileSize(id);
806
+ }
575
807
  if (total === 0) {
576
808
  await sink.write(new Uint8Array(0), true);
577
809
  onProgress?.(0, 0);
@@ -582,13 +814,17 @@ export class GoogleDriveClient {
582
814
  while (offset < total) {
583
815
  throwIfAborted(signal, 'googleDrive');
584
816
  const end = Math.min(offset + this.chunkBytes, total) - 1;
585
- const res = await this.request(`${FILES_URL}/${id}?alt=media`, {
817
+ const res = await this.request(`${FILES_URL}/${idPath(id)}?alt=media`, {
586
818
  method: 'GET',
587
819
  headers: {
588
820
  Range: `bytes=${offset}-${end}`
589
821
  }
590
822
  });
591
823
  const bytes = new Uint8Array(await res.arrayBuffer());
824
+ // Same guard as the upload side: an empty body would never advance.
825
+ if (bytes.length === 0) throw new CloudSyncError(ErrorCode.UNKNOWN, `[RNCloudSync] Google Drive returned no bytes at offset ${offset} of ${total}.`, {
826
+ provider: 'googleDrive'
827
+ });
592
828
  await sink.write(bytes, isFirstChunk);
593
829
  isFirstChunk = false;
594
830
  offset += bytes.length;
@@ -597,18 +833,26 @@ export class GoogleDriveClient {
597
833
  return true;
598
834
  }
599
835
  async fileSize(id) {
600
- const res = await this.request(`${FILES_URL}/${id}?fields=size`, {
836
+ const res = await this.request(`${FILES_URL}/${idPath(id)}?fields=size`, {
601
837
  method: 'GET'
602
838
  });
603
839
  const json = await res.json();
604
- return json.size != null ? Number(json.size) : 0;
840
+ if (json.size == null) return 0;
841
+ const size = Number(json.size);
842
+ // A size that is not a non-negative integer would make the chunk loop
843
+ // either never run - reporting a download that wrote nothing as complete -
844
+ // or compute nonsense ranges.
845
+ if (!Number.isSafeInteger(size) || size < 0) throw new CloudSyncError(ErrorCode.UNKNOWN, '[RNCloudSync] Google Drive reported an invalid file size.', {
846
+ provider: 'googleDrive'
847
+ });
848
+ return size;
605
849
  }
606
850
  async removeItem(name) {
607
851
  const id = await this.findFileId(name);
608
852
  // Already absent is the desired end state, not a failure.
609
853
  if (id == null) return;
610
854
  try {
611
- await this.request(`${FILES_URL}/${id}`, {
855
+ await this.request(`${FILES_URL}/${idPath(id)}`, {
612
856
  method: 'DELETE'
613
857
  });
614
858
  } catch (e) {
@@ -620,7 +864,7 @@ export class GoogleDriveClient {
620
864
  }
621
865
  }
622
866
  async getAllKeys() {
623
- const names = [];
867
+ const byName = new Map();
624
868
  let pageToken;
625
869
 
626
870
  // Paginate rather than assuming one page holds everything.
@@ -637,12 +881,26 @@ export class GoogleDriveClient {
637
881
  });
638
882
  const json = await res.json();
639
883
  for (const f of json.files ?? []) {
640
- names.push(f.name);
641
- this.remember(f);
884
+ const group = byName.get(f.name);
885
+ if (group == null) byName.set(f.name, [f]);else group.push(f);
642
886
  }
643
887
  pageToken = json.nextPageToken;
644
888
  } while (pageToken != null);
645
- return names;
889
+
890
+ // Drive names are not unique. Memoising whichever duplicate happened to be
891
+ // listed last bypassed `pickAmongDuplicates` entirely - the next `getItem`
892
+ // served that arbitrary file from the cache - and the same key came back
893
+ // once per copy.
894
+ for (const [name, files] of byName) if (files.length === 1) {
895
+ this.remember(files[0]);
896
+ } else if (this.config.onDuplicateName !== 'error') {
897
+ this.remember(this.pickAmongDuplicates(name, files));
898
+ } else {
899
+ // Leave it uncached so the next read raises the CONFLICT itself.
900
+ this.idCache.delete(name);
901
+ this.metaCache.delete(name);
902
+ }
903
+ return [...byName.keys()];
646
904
  }
647
905
 
648
906
  /**
@@ -652,7 +910,11 @@ export class GoogleDriveClient {
652
910
  * has to take a starting cursor before it can ask what changed since.
653
911
  */
654
912
  async getStartPageToken() {
655
- const res = await this.request(`${CHANGES_URL}/startPageToken?spaces=appDataFolder`, {
913
+ const res = await this.request(
914
+ // No `spaces` here: `changes.getStartPageToken` takes only the shared
915
+ // drive parameters. The cursor covers every space; `changes.list` is
916
+ // where `spaces=appDataFolder` scopes the feed.
917
+ `${CHANGES_URL}/startPageToken`, {
656
918
  method: 'GET'
657
919
  });
658
920
  const json = await res.json();
@@ -698,10 +960,14 @@ export class GoogleDriveClient {
698
960
  // A file that went away must not keep answering reads from the id
699
961
  // cache, and one that changed must be re-read rather than served
700
962
  // from a stale memo.
701
- if (change.removed === true || change.file?.trashed === true) {
702
- this.idCache.delete(name);
703
- this.metaCache.delete(name);
704
- }
963
+ // Dropped on every change, not only a removal. A changed file keeps
964
+ // its id but not its cached modification time, and a change to a
965
+ // *different* file under a cached name - another device created a
966
+ // same-named copy - has to go back through the listing or the
967
+ // duplicate resolution never runs and this device keeps reading its
968
+ // old copy forever. One listing on the next read is the cost.
969
+ this.idCache.delete(name);
970
+ this.metaCache.delete(name);
705
971
  continue;
706
972
  }
707
973