react-native-cloud-sync 0.1.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/LICENSE +21 -0
- package/README.md +360 -0
- package/app.plugin.js +3 -0
- package/ios/CloudSyncError.swift +144 -0
- package/ios/CloudSyncImpl.swift +1263 -0
- package/ios/RNCloudSync.h +25 -0
- package/ios/RNCloudSync.mm +477 -0
- package/jest-mock.js +136 -0
- package/lib/commonjs/config.js +85 -0
- package/lib/commonjs/config.js.map +1 -0
- package/lib/commonjs/errors.js +244 -0
- package/lib/commonjs/errors.js.map +1 -0
- package/lib/commonjs/hooks.js +257 -0
- package/lib/commonjs/hooks.js.map +1 -0
- package/lib/commonjs/index.js +244 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/internal/autoFlush.js +65 -0
- package/lib/commonjs/internal/autoFlush.js.map +1 -0
- package/lib/commonjs/internal/base64.js +58 -0
- package/lib/commonjs/internal/base64.js.map +1 -0
- package/lib/commonjs/internal/bytes.js +32 -0
- package/lib/commonjs/internal/bytes.js.map +1 -0
- package/lib/commonjs/internal/cloudKitRest.js +527 -0
- package/lib/commonjs/internal/cloudKitRest.js.map +1 -0
- package/lib/commonjs/internal/enums.js +23 -0
- package/lib/commonjs/internal/enums.js.map +1 -0
- package/lib/commonjs/internal/googleDriveRest.js +624 -0
- package/lib/commonjs/internal/googleDriveRest.js.map +1 -0
- package/lib/commonjs/internal/keys.js +139 -0
- package/lib/commonjs/internal/keys.js.map +1 -0
- package/lib/commonjs/internal/logger.js +29 -0
- package/lib/commonjs/internal/logger.js.map +1 -0
- package/lib/commonjs/internal/nativeModule.js +125 -0
- package/lib/commonjs/internal/nativeModule.js.map +1 -0
- package/lib/commonjs/internal/timeout.js +72 -0
- package/lib/commonjs/internal/timeout.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/providers/cloudKit.js +398 -0
- package/lib/commonjs/providers/cloudKit.js.map +1 -0
- package/lib/commonjs/providers/cloudKitBackup.js +86 -0
- package/lib/commonjs/providers/cloudKitBackup.js.map +1 -0
- package/lib/commonjs/providers/cloudKitEncrypted.js +150 -0
- package/lib/commonjs/providers/cloudKitEncrypted.js.map +1 -0
- package/lib/commonjs/providers/googleDrive.js +219 -0
- package/lib/commonjs/providers/googleDrive.js.map +1 -0
- package/lib/commonjs/providers/googleDriveFiles.js +68 -0
- package/lib/commonjs/providers/googleDriveFiles.js.map +1 -0
- package/lib/commonjs/providers/icloudDocuments.js +121 -0
- package/lib/commonjs/providers/icloudDocuments.js.map +1 -0
- package/lib/commonjs/providers/icloudKV.js +171 -0
- package/lib/commonjs/providers/icloudKV.js.map +1 -0
- package/lib/commonjs/providers/icloudKV.web.js +42 -0
- package/lib/commonjs/providers/icloudKV.web.js.map +1 -0
- package/lib/commonjs/providers/memory.js +201 -0
- package/lib/commonjs/providers/memory.js.map +1 -0
- package/lib/commonjs/resolvers.js +128 -0
- package/lib/commonjs/resolvers.js.map +1 -0
- package/lib/commonjs/specs/NativeRNCloudSync.js +14 -0
- package/lib/commonjs/specs/NativeRNCloudSync.js.map +1 -0
- package/lib/commonjs/store.js +1064 -0
- package/lib/commonjs/store.js.map +1 -0
- package/lib/commonjs/testing.js +32 -0
- package/lib/commonjs/testing.js.map +1 -0
- package/lib/commonjs/types.js +86 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/config.js +72 -0
- package/lib/module/config.js.map +1 -0
- package/lib/module/errors.js +231 -0
- package/lib/module/errors.js.map +1 -0
- package/lib/module/hooks.js +250 -0
- package/lib/module/hooks.js.map +1 -0
- package/lib/module/index.js +46 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/internal/autoFlush.js +62 -0
- package/lib/module/internal/autoFlush.js.map +1 -0
- package/lib/module/internal/base64.js +53 -0
- package/lib/module/internal/base64.js.map +1 -0
- package/lib/module/internal/bytes.js +28 -0
- package/lib/module/internal/bytes.js.map +1 -0
- package/lib/module/internal/cloudKitRest.js +518 -0
- package/lib/module/internal/cloudKitRest.js.map +1 -0
- package/lib/module/internal/enums.js +18 -0
- package/lib/module/internal/enums.js.map +1 -0
- package/lib/module/internal/googleDriveRest.js +620 -0
- package/lib/module/internal/googleDriveRest.js.map +1 -0
- package/lib/module/internal/keys.js +134 -0
- package/lib/module/internal/keys.js.map +1 -0
- package/lib/module/internal/logger.js +23 -0
- package/lib/module/internal/logger.js.map +1 -0
- package/lib/module/internal/nativeModule.js +117 -0
- package/lib/module/internal/nativeModule.js.map +1 -0
- package/lib/module/internal/timeout.js +67 -0
- package/lib/module/internal/timeout.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/providers/cloudKit.js +392 -0
- package/lib/module/providers/cloudKit.js.map +1 -0
- package/lib/module/providers/cloudKitBackup.js +82 -0
- package/lib/module/providers/cloudKitBackup.js.map +1 -0
- package/lib/module/providers/cloudKitEncrypted.js +146 -0
- package/lib/module/providers/cloudKitEncrypted.js.map +1 -0
- package/lib/module/providers/googleDrive.js +214 -0
- package/lib/module/providers/googleDrive.js.map +1 -0
- package/lib/module/providers/googleDriveFiles.js +64 -0
- package/lib/module/providers/googleDriveFiles.js.map +1 -0
- package/lib/module/providers/icloudDocuments.js +117 -0
- package/lib/module/providers/icloudDocuments.js.map +1 -0
- package/lib/module/providers/icloudKV.js +165 -0
- package/lib/module/providers/icloudKV.js.map +1 -0
- package/lib/module/providers/icloudKV.web.js +36 -0
- package/lib/module/providers/icloudKV.web.js.map +1 -0
- package/lib/module/providers/memory.js +197 -0
- package/lib/module/providers/memory.js.map +1 -0
- package/lib/module/resolvers.js +120 -0
- package/lib/module/resolvers.js.map +1 -0
- package/lib/module/specs/NativeRNCloudSync.js +12 -0
- package/lib/module/specs/NativeRNCloudSync.js.map +1 -0
- package/lib/module/store.js +1061 -0
- package/lib/module/store.js.map +1 -0
- package/lib/module/testing.js +17 -0
- package/lib/module/testing.js.map +1 -0
- package/lib/module/types.js +82 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/commonjs/config.d.ts +33 -0
- package/lib/typescript/commonjs/config.d.ts.map +1 -0
- package/lib/typescript/commonjs/errors.d.ts +140 -0
- package/lib/typescript/commonjs/errors.d.ts.map +1 -0
- package/lib/typescript/commonjs/hooks.d.ts +109 -0
- package/lib/typescript/commonjs/hooks.d.ts.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +37 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/autoFlush.d.ts +45 -0
- package/lib/typescript/commonjs/internal/autoFlush.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/base64.d.ts +12 -0
- package/lib/typescript/commonjs/internal/base64.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/bytes.d.ts +12 -0
- package/lib/typescript/commonjs/internal/bytes.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/cloudKitRest.d.ts +149 -0
- package/lib/typescript/commonjs/internal/cloudKitRest.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/enums.d.ts +10 -0
- package/lib/typescript/commonjs/internal/enums.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/googleDriveRest.d.ts +232 -0
- package/lib/typescript/commonjs/internal/googleDriveRest.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/keys.d.ts +28 -0
- package/lib/typescript/commonjs/internal/keys.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/logger.d.ts +10 -0
- package/lib/typescript/commonjs/internal/logger.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/nativeModule.d.ts +29 -0
- package/lib/typescript/commonjs/internal/nativeModule.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/timeout.d.ts +36 -0
- package/lib/typescript/commonjs/internal/timeout.d.ts.map +1 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/providers/cloudKit.d.ts +86 -0
- package/lib/typescript/commonjs/providers/cloudKit.d.ts.map +1 -0
- package/lib/typescript/commonjs/providers/cloudKitBackup.d.ts +65 -0
- package/lib/typescript/commonjs/providers/cloudKitBackup.d.ts.map +1 -0
- package/lib/typescript/commonjs/providers/cloudKitEncrypted.d.ts +37 -0
- package/lib/typescript/commonjs/providers/cloudKitEncrypted.d.ts.map +1 -0
- package/lib/typescript/commonjs/providers/googleDrive.d.ts +35 -0
- package/lib/typescript/commonjs/providers/googleDrive.d.ts.map +1 -0
- package/lib/typescript/commonjs/providers/googleDriveFiles.d.ts +59 -0
- package/lib/typescript/commonjs/providers/googleDriveFiles.d.ts.map +1 -0
- package/lib/typescript/commonjs/providers/icloudDocuments.d.ts +101 -0
- package/lib/typescript/commonjs/providers/icloudDocuments.d.ts.map +1 -0
- package/lib/typescript/commonjs/providers/icloudKV.d.ts +26 -0
- package/lib/typescript/commonjs/providers/icloudKV.d.ts.map +1 -0
- package/lib/typescript/commonjs/providers/icloudKV.web.d.ts +12 -0
- package/lib/typescript/commonjs/providers/icloudKV.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/providers/memory.d.ts +90 -0
- package/lib/typescript/commonjs/providers/memory.d.ts.map +1 -0
- package/lib/typescript/commonjs/resolvers.d.ts +60 -0
- package/lib/typescript/commonjs/resolvers.d.ts.map +1 -0
- package/lib/typescript/commonjs/specs/NativeRNCloudSync.d.ts +133 -0
- package/lib/typescript/commonjs/specs/NativeRNCloudSync.d.ts.map +1 -0
- package/lib/typescript/commonjs/store.d.ts +118 -0
- package/lib/typescript/commonjs/store.d.ts.map +1 -0
- package/lib/typescript/commonjs/testing.d.ts +16 -0
- package/lib/typescript/commonjs/testing.d.ts.map +1 -0
- package/lib/typescript/commonjs/types.d.ts +367 -0
- package/lib/typescript/commonjs/types.d.ts.map +1 -0
- package/lib/typescript/module/config.d.ts +33 -0
- package/lib/typescript/module/config.d.ts.map +1 -0
- package/lib/typescript/module/errors.d.ts +140 -0
- package/lib/typescript/module/errors.d.ts.map +1 -0
- package/lib/typescript/module/hooks.d.ts +109 -0
- package/lib/typescript/module/hooks.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +37 -0
- package/lib/typescript/module/index.d.ts.map +1 -0
- package/lib/typescript/module/internal/autoFlush.d.ts +45 -0
- package/lib/typescript/module/internal/autoFlush.d.ts.map +1 -0
- package/lib/typescript/module/internal/base64.d.ts +12 -0
- package/lib/typescript/module/internal/base64.d.ts.map +1 -0
- package/lib/typescript/module/internal/bytes.d.ts +12 -0
- package/lib/typescript/module/internal/bytes.d.ts.map +1 -0
- package/lib/typescript/module/internal/cloudKitRest.d.ts +149 -0
- package/lib/typescript/module/internal/cloudKitRest.d.ts.map +1 -0
- package/lib/typescript/module/internal/enums.d.ts +10 -0
- package/lib/typescript/module/internal/enums.d.ts.map +1 -0
- package/lib/typescript/module/internal/googleDriveRest.d.ts +232 -0
- package/lib/typescript/module/internal/googleDriveRest.d.ts.map +1 -0
- package/lib/typescript/module/internal/keys.d.ts +28 -0
- package/lib/typescript/module/internal/keys.d.ts.map +1 -0
- package/lib/typescript/module/internal/logger.d.ts +10 -0
- package/lib/typescript/module/internal/logger.d.ts.map +1 -0
- package/lib/typescript/module/internal/nativeModule.d.ts +29 -0
- package/lib/typescript/module/internal/nativeModule.d.ts.map +1 -0
- package/lib/typescript/module/internal/timeout.d.ts +36 -0
- package/lib/typescript/module/internal/timeout.d.ts.map +1 -0
- package/lib/typescript/module/package.json +1 -0
- package/lib/typescript/module/providers/cloudKit.d.ts +86 -0
- package/lib/typescript/module/providers/cloudKit.d.ts.map +1 -0
- package/lib/typescript/module/providers/cloudKitBackup.d.ts +65 -0
- package/lib/typescript/module/providers/cloudKitBackup.d.ts.map +1 -0
- package/lib/typescript/module/providers/cloudKitEncrypted.d.ts +37 -0
- package/lib/typescript/module/providers/cloudKitEncrypted.d.ts.map +1 -0
- package/lib/typescript/module/providers/googleDrive.d.ts +35 -0
- package/lib/typescript/module/providers/googleDrive.d.ts.map +1 -0
- package/lib/typescript/module/providers/googleDriveFiles.d.ts +59 -0
- package/lib/typescript/module/providers/googleDriveFiles.d.ts.map +1 -0
- package/lib/typescript/module/providers/icloudDocuments.d.ts +101 -0
- package/lib/typescript/module/providers/icloudDocuments.d.ts.map +1 -0
- package/lib/typescript/module/providers/icloudKV.d.ts +26 -0
- package/lib/typescript/module/providers/icloudKV.d.ts.map +1 -0
- package/lib/typescript/module/providers/icloudKV.web.d.ts +12 -0
- package/lib/typescript/module/providers/icloudKV.web.d.ts.map +1 -0
- package/lib/typescript/module/providers/memory.d.ts +90 -0
- package/lib/typescript/module/providers/memory.d.ts.map +1 -0
- package/lib/typescript/module/resolvers.d.ts +60 -0
- package/lib/typescript/module/resolvers.d.ts.map +1 -0
- package/lib/typescript/module/specs/NativeRNCloudSync.d.ts +133 -0
- package/lib/typescript/module/specs/NativeRNCloudSync.d.ts.map +1 -0
- package/lib/typescript/module/store.d.ts +118 -0
- package/lib/typescript/module/store.d.ts.map +1 -0
- package/lib/typescript/module/testing.d.ts +16 -0
- package/lib/typescript/module/testing.d.ts.map +1 -0
- package/lib/typescript/module/types.d.ts +367 -0
- package/lib/typescript/module/types.d.ts.map +1 -0
- package/package.json +186 -0
- package/plugin/build/index.d.ts +45 -0
- package/plugin/build/index.js +93 -0
- package/react-native-cloud-sync.podspec +48 -0
- package/src/config.ts +94 -0
- package/src/errors.ts +263 -0
- package/src/hooks.ts +337 -0
- package/src/index.ts +113 -0
- package/src/internal/autoFlush.ts +87 -0
- package/src/internal/base64.ts +63 -0
- package/src/internal/bytes.ts +28 -0
- package/src/internal/cloudKitRest.ts +654 -0
- package/src/internal/enums.ts +35 -0
- package/src/internal/googleDriveRest.ts +765 -0
- package/src/internal/keys.ts +160 -0
- package/src/internal/logger.ts +23 -0
- package/src/internal/nativeModule.ts +149 -0
- package/src/internal/timeout.ts +83 -0
- package/src/providers/cloudKit.ts +510 -0
- package/src/providers/cloudKitBackup.ts +124 -0
- package/src/providers/cloudKitEncrypted.ts +175 -0
- package/src/providers/googleDrive.ts +251 -0
- package/src/providers/googleDriveFiles.ts +117 -0
- package/src/providers/icloudDocuments.ts +175 -0
- package/src/providers/icloudKV.ts +206 -0
- package/src/providers/icloudKV.web.ts +41 -0
- package/src/providers/memory.ts +302 -0
- package/src/resolvers.ts +133 -0
- package/src/specs/NativeRNCloudSync.ts +184 -0
- package/src/store.ts +1288 -0
- package/src/testing.ts +22 -0
- package/src/types.ts +426 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { Platform } from 'react-native'
|
|
2
|
+
|
|
3
|
+
import { getNativeModule, hasNativeModule, requireNativeModule } from '../internal/nativeModule'
|
|
4
|
+
import { normalizeError, unsupportedPlatform } from '../errors'
|
|
5
|
+
import { toAccountStatus } from '../internal/enums'
|
|
6
|
+
import type { AccountStatus, CloudProvider } from '../types'
|
|
7
|
+
|
|
8
|
+
const NAME = 'cloudKitEncrypted' as const
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A record type of its own, deliberately.
|
|
12
|
+
*
|
|
13
|
+
* CloudKit records encryption in the *schema*: a field is encrypted or plain,
|
|
14
|
+
* never both. Writing an encrypted `value` onto the same `KVBlob` type that
|
|
15
|
+
* `cloudKit` uses for a plain one is a field-type conflict the server rejects,
|
|
16
|
+
* so the two providers must not share a record type.
|
|
17
|
+
*/
|
|
18
|
+
const RECORD_TYPE = 'EncryptedKVBlob'
|
|
19
|
+
|
|
20
|
+
function isNative(): boolean {
|
|
21
|
+
const os: string | undefined = Platform?.OS
|
|
22
|
+
return (os === 'ios' || os === 'macos') && hasNativeModule()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function assertNative(): void {
|
|
26
|
+
if (!isNative())
|
|
27
|
+
throw unsupportedPlatform(
|
|
28
|
+
NAME,
|
|
29
|
+
'CloudKit end-to-end encryption is not a feature this package could add over REST. The key '
|
|
30
|
+
+ 'lives in the user\'s iCloud Keychain and never reaches Apple\'s servers, so CloudKit Web '
|
|
31
|
+
+ 'Services - the Android and web path - has nothing to decrypt with. Use the store facade '
|
|
32
|
+
+ 'with a `codec` for cross-platform encryption you hold the key to.'
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* CloudKit's own end-to-end encryption, via `CKRecord.encryptedValues`.
|
|
38
|
+
*
|
|
39
|
+
* The genuinely native answer to "can I store sensitive data here". Values
|
|
40
|
+
* written through this provider are encrypted on device with a key from the
|
|
41
|
+
* user's iCloud Keychain; Apple stores ciphertext and cannot read it, whether
|
|
42
|
+
* or not the user has turned on Advanced Data Protection. Nothing to configure,
|
|
43
|
+
* no key for you to manage, no passphrase for the user to lose.
|
|
44
|
+
*
|
|
45
|
+
* What it costs, and none of these are avoidable:
|
|
46
|
+
*
|
|
47
|
+
* - **Apple platforms only.** The decryption key is in the iCloud Keychain,
|
|
48
|
+
* which CloudKit Web Services cannot reach, so a value written here is
|
|
49
|
+
* permanently unreadable from Android and web. Not a gap to be closed
|
|
50
|
+
* later - it is what end-to-end means. Reach for the store facade's
|
|
51
|
+
* `codec` if the same data has to be readable off-Apple.
|
|
52
|
+
* - **Not queryable.** CloudKit cannot index an encrypted field, so no
|
|
53
|
+
* server-side filtering or sorting on the value. `getAllKeys()` still
|
|
54
|
+
* works, because it queries record *names*, which are not encrypted.
|
|
55
|
+
* - **Record names are not encrypted either.** Your keys are visible to
|
|
56
|
+
* Apple; only values are protected. Do not put anything sensitive in a key.
|
|
57
|
+
* - **Its own record type** (`EncryptedKVBlob`), so it can coexist with
|
|
58
|
+
* `cloudKit` rather than colliding with its schema.
|
|
59
|
+
*
|
|
60
|
+
* Usable through the facade like any other provider, as long as every provider
|
|
61
|
+
* in the list can actually serve the data:
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* const secrets = createCloudStore({ providers: ['cloudKitEncrypted'] })
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* Mixing it with `googleDrive` in one store is almost always a mistake - the
|
|
68
|
+
* Drive copy would be plaintext, which quietly undoes the encryption.
|
|
69
|
+
*/
|
|
70
|
+
export const cloudKitEncrypted: CloudProvider = {
|
|
71
|
+
name: NAME,
|
|
72
|
+
|
|
73
|
+
isAvailable: async () => {
|
|
74
|
+
// Not an error on Android or web: `isAvailable` is the method callers use
|
|
75
|
+
// to branch without a try/catch, and this provider genuinely is not
|
|
76
|
+
// available there.
|
|
77
|
+
if (!isNative()) return false
|
|
78
|
+
try {
|
|
79
|
+
return await requireNativeModule().isAvailable()
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return false
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
getAccountStatus: async (): Promise<AccountStatus> => {
|
|
87
|
+
if (!isNative()) return 'couldNotDetermine'
|
|
88
|
+
try {
|
|
89
|
+
const m = getNativeModule()
|
|
90
|
+
if (m == null) return 'couldNotDetermine'
|
|
91
|
+
return toAccountStatus(await m.getAccountStatus())
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
throw normalizeError(e, NAME)
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
getItem: async (key: string) => {
|
|
99
|
+
assertNative()
|
|
100
|
+
try {
|
|
101
|
+
return await requireNativeModule().ckGetRecord(RECORD_TYPE, key, null, true)
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
throw normalizeError(e, NAME)
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
setItem: async (key: string, value: string) => {
|
|
109
|
+
assertNative()
|
|
110
|
+
try {
|
|
111
|
+
await requireNativeModule().ckSaveRecord(RECORD_TYPE, key, value, null, true)
|
|
112
|
+
}
|
|
113
|
+
catch (e) {
|
|
114
|
+
throw normalizeError(e, NAME)
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
removeItem: async (key: string) => {
|
|
119
|
+
assertNative()
|
|
120
|
+
try {
|
|
121
|
+
await requireNativeModule().ckDeleteRecord(key, null)
|
|
122
|
+
}
|
|
123
|
+
catch (e) {
|
|
124
|
+
throw normalizeError(e, NAME)
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Record names, which CloudKit does not encrypt - so listing works even
|
|
130
|
+
* though the values cannot be queried.
|
|
131
|
+
*/
|
|
132
|
+
getAllKeys: async () => {
|
|
133
|
+
assertNative()
|
|
134
|
+
try {
|
|
135
|
+
return await requireNativeModule().ckQueryRecordNames(RECORD_TYPE, null)
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
throw normalizeError(e, NAME)
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
multiGet: async (keys: string[]) => {
|
|
143
|
+
assertNative()
|
|
144
|
+
try {
|
|
145
|
+
const out: (string | null)[] = []
|
|
146
|
+
for (const key of keys)
|
|
147
|
+
out.push(await requireNativeModule().ckGetRecord(RECORD_TYPE, key, null, true))
|
|
148
|
+
return out
|
|
149
|
+
}
|
|
150
|
+
catch (e) {
|
|
151
|
+
throw normalizeError(e, NAME)
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
multiSet: async (entries: [string, string][]) => {
|
|
156
|
+
assertNative()
|
|
157
|
+
try {
|
|
158
|
+
for (const [key, value] of entries)
|
|
159
|
+
await requireNativeModule().ckSaveRecord(RECORD_TYPE, key, value, null, true)
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
throw normalizeError(e, NAME)
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
multiRemove: async (keys: string[]) => {
|
|
167
|
+
assertNative()
|
|
168
|
+
try {
|
|
169
|
+
for (const key of keys) await requireNativeModule().ckDeleteRecord(key, null)
|
|
170
|
+
}
|
|
171
|
+
catch (e) {
|
|
172
|
+
throw normalizeError(e, NAME)
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { getGoogleDriveClient, isGoogleDriveConfigured } from '../config'
|
|
2
|
+
import { GoogleDriveClient, type GoogleDriveConfig } from '../internal/googleDriveRest'
|
|
3
|
+
import { normalizeError } from '../errors'
|
|
4
|
+
import type { AccountStatus, CloudProvider, ProviderName, RemoteChangeEvent } from '../types'
|
|
5
|
+
|
|
6
|
+
const NAME = 'googleDrive' as const
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* How often the change cursor is polled once something subscribes.
|
|
10
|
+
*
|
|
11
|
+
* 30s is a compromise: fast enough that a second device feels responsive,
|
|
12
|
+
* slow enough that a backgrounded app is not spending battery on a feed that
|
|
13
|
+
* is usually empty.
|
|
14
|
+
*/
|
|
15
|
+
const DEFAULT_POLL_INTERVAL_MS = 30_000
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Builds a Drive provider over whichever client `getClient` returns.
|
|
19
|
+
*
|
|
20
|
+
* Written as a factory so the package-level singleton and the per-instance
|
|
21
|
+
* providers from {@link createGoogleDriveProvider} share one implementation
|
|
22
|
+
* instead of two copies that drift.
|
|
23
|
+
*/
|
|
24
|
+
function makeDriveProvider(
|
|
25
|
+
name: ProviderName,
|
|
26
|
+
getClient: () => GoogleDriveClient,
|
|
27
|
+
isConfigured: () => boolean,
|
|
28
|
+
pollIntervalMs = DEFAULT_POLL_INTERVAL_MS
|
|
29
|
+
): CloudProvider {
|
|
30
|
+
const provider: CloudProvider = {
|
|
31
|
+
name,
|
|
32
|
+
|
|
33
|
+
isAvailable: async () => {
|
|
34
|
+
if (!isConfigured()) return false
|
|
35
|
+
try {
|
|
36
|
+
// Only checks that a token can be obtained. Deliberately does not list
|
|
37
|
+
// files - `isAvailable` is safe to call on a render path, and a full
|
|
38
|
+
// listing is a paginated network round trip.
|
|
39
|
+
return await getClient().hasToken()
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
getAccountStatus: (): Promise<AccountStatus> =>
|
|
47
|
+
Promise.resolve(isConfigured() ? 'available' : 'noAccount'),
|
|
48
|
+
|
|
49
|
+
getItem: async (key: string) => {
|
|
50
|
+
try {
|
|
51
|
+
return await getClient().getItem(key)
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
throw normalizeError(e, name)
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
getItemWithMeta: async (key: string) => {
|
|
59
|
+
try {
|
|
60
|
+
return await getClient().getItemWithMeta(key)
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
throw normalizeError(e, name)
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
setItem: async (key: string, value: string) => {
|
|
68
|
+
try {
|
|
69
|
+
await getClient().setItem(key, value)
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
throw normalizeError(e, name)
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
removeItem: async (key: string) => {
|
|
77
|
+
try {
|
|
78
|
+
await getClient().removeItem(key)
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
throw normalizeError(e, name)
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
getAllKeys: async () => {
|
|
86
|
+
try {
|
|
87
|
+
return await getClient().getAllKeys()
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
throw normalizeError(e, name)
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Storage usage for the Google account.
|
|
96
|
+
*
|
|
97
|
+
* Drive reports no per-folder figure, so this is the whole account's -
|
|
98
|
+
* which is the number that matters anyway, since that is the limit a write
|
|
99
|
+
* actually hits. `totalBytes` is absent on pooled/unlimited accounts.
|
|
100
|
+
*/
|
|
101
|
+
getQuota: async () => {
|
|
102
|
+
try {
|
|
103
|
+
return { ...(await getClient().getQuota()), provider: name }
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
throw normalizeError(e, name)
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
// Deliberately no multiGet/multiSet: Drive stores one file per key and has
|
|
111
|
+
// no batch endpoint for content (the JSON-RPC batch endpoint was retired in
|
|
112
|
+
// 2020 and never carried media bodies). Advertising a batch API that
|
|
113
|
+
// secretly looped would hide N round trips behind a call that looks like
|
|
114
|
+
// one, so the store's own sequential fallback is used - same cost, honest
|
|
115
|
+
// about it.
|
|
116
|
+
|
|
117
|
+
clearCaches: () => {
|
|
118
|
+
// Memoised file ids belong to the account that was signed in when they
|
|
119
|
+
// were recorded. Handing them to the next user is a cross-account leak.
|
|
120
|
+
if (isConfigured()) getClient().clearCache()
|
|
121
|
+
stopWatching()
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Fires when another device changed something in `appDataFolder`.
|
|
126
|
+
*
|
|
127
|
+
* Drive has no push channel a mobile client can subscribe to - its webhooks
|
|
128
|
+
* need a public HTTPS endpoint to deliver to - so this polls Drive's change
|
|
129
|
+
* cursor. That is the honest implementation, and it is cheap: a poll with
|
|
130
|
+
* nothing to report is one request returning an empty list.
|
|
131
|
+
*
|
|
132
|
+
* Polling starts with the first subscriber and stops with the last, so an
|
|
133
|
+
* app that never subscribes never makes a request. Interval comes from
|
|
134
|
+
* `changePollIntervalMs` (default 30s).
|
|
135
|
+
*/
|
|
136
|
+
onRemoteChange: (listener) => {
|
|
137
|
+
changeListeners.add(listener)
|
|
138
|
+
startWatching()
|
|
139
|
+
return () => {
|
|
140
|
+
changeListeners.delete(listener)
|
|
141
|
+
if (changeListeners.size === 0) stopWatching()
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// ---------------------------------------------------------- change polling
|
|
147
|
+
|
|
148
|
+
const changeListeners = new Set<(e: RemoteChangeEvent) => void>()
|
|
149
|
+
let timer: ReturnType<typeof setInterval> | null = null
|
|
150
|
+
let cursor: string | null = null
|
|
151
|
+
let polling = false
|
|
152
|
+
|
|
153
|
+
function startWatching(): void {
|
|
154
|
+
if (timer != null || !isConfigured()) return
|
|
155
|
+
|
|
156
|
+
timer = setInterval(() => {
|
|
157
|
+
void poll()
|
|
158
|
+
}, pollIntervalMs)
|
|
159
|
+
// Never hold the process open for a background poll - this is a cache
|
|
160
|
+
// refresh, not work anyone is waiting on.
|
|
161
|
+
timer.unref?.()
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function stopWatching(): void {
|
|
165
|
+
if (timer != null) clearInterval(timer)
|
|
166
|
+
timer = null
|
|
167
|
+
cursor = null
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function poll(): Promise<void> {
|
|
171
|
+
// Skip rather than queue: a poll that outruns its interval on a slow link
|
|
172
|
+
// would otherwise pile up requests behind it.
|
|
173
|
+
if (polling || !isConfigured()) return
|
|
174
|
+
polling = true
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
const client = getClient()
|
|
178
|
+
if (cursor == null) {
|
|
179
|
+
// First tick only establishes the starting cursor. Reporting everything
|
|
180
|
+
// that ever happened as "just changed" would make every subscriber
|
|
181
|
+
// re-read the whole store on launch.
|
|
182
|
+
cursor = await client.getStartPageToken()
|
|
183
|
+
return
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const { names, nextToken } = await client.pollChanges(cursor)
|
|
187
|
+
cursor = nextToken
|
|
188
|
+
if (names.length === 0) return
|
|
189
|
+
|
|
190
|
+
const event: RemoteChangeEvent = { keys: names, reason: 'serverChange', provider: name }
|
|
191
|
+
for (const l of changeListeners) l(event)
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
// A failed poll is not the app's problem: the next tick tries again, and
|
|
195
|
+
// the cursor is only advanced on success so nothing is skipped.
|
|
196
|
+
}
|
|
197
|
+
finally {
|
|
198
|
+
polling = false
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return provider
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Google Drive `appDataFolder`, using the credentials from
|
|
207
|
+
* `configureGoogleDrive()`.
|
|
208
|
+
*
|
|
209
|
+
* The only provider that works identically on iOS, Android and web, which makes
|
|
210
|
+
* it the sensible continuous backend for cross-platform apps - CloudKit's
|
|
211
|
+
* Android/web path needs a periodic Apple ID re-auth, this does not.
|
|
212
|
+
*/
|
|
213
|
+
export const googleDrive: CloudProvider = makeDriveProvider(
|
|
214
|
+
NAME,
|
|
215
|
+
getGoogleDriveClient,
|
|
216
|
+
isGoogleDriveConfigured
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* A Drive provider with its own credentials and its own name.
|
|
221
|
+
*
|
|
222
|
+
* `configureGoogleDrive` sets one client for the whole process, which is right
|
|
223
|
+
* for the overwhelmingly common single-account app but leaves no way to reach
|
|
224
|
+
* two accounts at once - a profile switcher, a "copy my data to another
|
|
225
|
+
* account" flow, or a test that needs two isolated instances. Those need a
|
|
226
|
+
* provider that owns its config rather than reading a module-level singleton.
|
|
227
|
+
*
|
|
228
|
+
* ```ts
|
|
229
|
+
* const work = createGoogleDriveProvider({
|
|
230
|
+
* name: 'drive:work',
|
|
231
|
+
* getAccessToken: () => workToken(),
|
|
232
|
+
* })
|
|
233
|
+
* const store = createCloudStore({ providers: ['drive:work'] })
|
|
234
|
+
* store.registerProvider(work)
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
export function createGoogleDriveProvider(
|
|
238
|
+
config: GoogleDriveConfig & {
|
|
239
|
+
name?: ProviderName
|
|
240
|
+
/** How often `onRemoteChange` polls Drive's change cursor. Default 30000. */
|
|
241
|
+
changePollIntervalMs?: number
|
|
242
|
+
}
|
|
243
|
+
): CloudProvider {
|
|
244
|
+
const client = new GoogleDriveClient(config)
|
|
245
|
+
return makeDriveProvider(
|
|
246
|
+
config.name ?? NAME,
|
|
247
|
+
() => client,
|
|
248
|
+
() => true,
|
|
249
|
+
config.changePollIntervalMs
|
|
250
|
+
)
|
|
251
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { getGoogleDriveClient, getGoogleDriveFileAdapter } from '../config'
|
|
2
|
+
import { normalizeError } from '../errors'
|
|
3
|
+
import { base64ToBytes, bytesToBase64 } from '../internal/base64'
|
|
4
|
+
import type { DriveChunkSink, DriveChunkSource } from '../internal/googleDriveRest'
|
|
5
|
+
import type { AbortLike } from '../internal/timeout'
|
|
6
|
+
|
|
7
|
+
const NAME = 'googleDrive' as const
|
|
8
|
+
|
|
9
|
+
export interface DriveFileProgressEvent {
|
|
10
|
+
bytesTransferred: number
|
|
11
|
+
bytesTotal: number
|
|
12
|
+
/** `bytesTransferred / bytesTotal`, or 0 while `bytesTotal` is still 0. */
|
|
13
|
+
fraction: number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface DriveFileSaveOptions {
|
|
17
|
+
/** Called repeatedly while the upload is in flight. */
|
|
18
|
+
onProgress?: (e: DriveFileProgressEvent) => void
|
|
19
|
+
/**
|
|
20
|
+
* Cancels the transfer, rejecting with `ERR_CANCELLED`.
|
|
21
|
+
*
|
|
22
|
+
* Checked between chunks, so a cancel takes effect during the transfer rather
|
|
23
|
+
* than only once the whole file has moved - which is the entire point when
|
|
24
|
+
* the file is several hundred megabytes. Bytes already accepted stay in the
|
|
25
|
+
* resumable session; a later `save` of the same name starts a fresh one.
|
|
26
|
+
*
|
|
27
|
+
* Typed structurally rather than as the DOM `AbortSignal`, so any polyfill
|
|
28
|
+
* works and consumers do not need `lib.dom` in their tsconfig.
|
|
29
|
+
*/
|
|
30
|
+
signal?: AbortLike
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface DriveFileFetchOptions {
|
|
34
|
+
/** Called repeatedly while the download is in flight. */
|
|
35
|
+
onProgress?: (e: DriveFileProgressEvent) => void
|
|
36
|
+
/** Cancels the transfer, rejecting with `ERR_CANCELLED`. Checked between chunks. */
|
|
37
|
+
signal?: AbortLike
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Binary files in Drive's `appDataFolder`, for anything too large to hold in
|
|
42
|
+
* memory as a JS string the way `googleDrive.setItem` does - the Android/web
|
|
43
|
+
* equivalent of `cloudKitAssets`' `CKAsset` on Apple platforms.
|
|
44
|
+
*
|
|
45
|
+
* Uploads use Drive's resumable protocol, reading the source in fixed chunks;
|
|
46
|
+
* downloads write in the same fixed chunks. Neither ever holds more than one
|
|
47
|
+
* chunk (8 MiB by default) in memory. Both go through a `GoogleDriveFileAdapter`
|
|
48
|
+
* (`configureGoogleDriveFiles`) for the actual disk I/O - this package has no
|
|
49
|
+
* filesystem dependency of its own, so the host app supplies one using
|
|
50
|
+
* whichever fs library it already has. See the CloudKit/Google Drive guides
|
|
51
|
+
* for the file adapter's exact contract and setup examples.
|
|
52
|
+
*/
|
|
53
|
+
export const googleDriveFiles = {
|
|
54
|
+
/** Uploads `fileUri` as `name`, overwriting whatever was there. */
|
|
55
|
+
save: async (
|
|
56
|
+
options: { name: string; fileUri: string } & DriveFileSaveOptions
|
|
57
|
+
): Promise<void> => {
|
|
58
|
+
const adapter = getGoogleDriveFileAdapter()
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
const size = await adapter.statSize(options.fileUri)
|
|
62
|
+
const source: DriveChunkSource = {
|
|
63
|
+
size,
|
|
64
|
+
read: async (start, length) =>
|
|
65
|
+
base64ToBytes(await adapter.readChunk(options.fileUri, start, length)),
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
await getGoogleDriveClient().uploadFile(
|
|
69
|
+
options.name, source, forward(options.onProgress), options.signal
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
throw normalizeError(e, NAME)
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Downloads `name` into `destinationUri`, resolving `destinationUri` back
|
|
79
|
+
* once complete, or `null` when no such file exists - `destinationUri` is
|
|
80
|
+
* never touched in that case.
|
|
81
|
+
*/
|
|
82
|
+
fetch: async (
|
|
83
|
+
options: { name: string; destinationUri: string } & DriveFileFetchOptions
|
|
84
|
+
): Promise<string | null> => {
|
|
85
|
+
const adapter = getGoogleDriveFileAdapter()
|
|
86
|
+
|
|
87
|
+
const sink: DriveChunkSink = {
|
|
88
|
+
write: async (bytes, isFirstChunk) => {
|
|
89
|
+
const base64 = bytesToBase64(bytes)
|
|
90
|
+
if (isFirstChunk) await adapter.writeChunk(options.destinationUri, base64)
|
|
91
|
+
else await adapter.appendChunk(options.destinationUri, base64)
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
const found = await getGoogleDriveClient().downloadFile(
|
|
97
|
+
options.name, sink, forward(options.onProgress), options.signal
|
|
98
|
+
)
|
|
99
|
+
return found ? options.destinationUri : null
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
throw normalizeError(e, NAME)
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function forward(
|
|
108
|
+
onProgress: DriveFileSaveOptions['onProgress']
|
|
109
|
+
): ((bytesTransferred: number, bytesTotal: number) => void) | undefined {
|
|
110
|
+
if (onProgress == null) return undefined
|
|
111
|
+
return (bytesTransferred, bytesTotal) =>
|
|
112
|
+
onProgress({
|
|
113
|
+
bytesTransferred,
|
|
114
|
+
bytesTotal,
|
|
115
|
+
fraction: bytesTotal > 0 ? bytesTransferred / bytesTotal : 0,
|
|
116
|
+
})
|
|
117
|
+
}
|