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