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,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import { normalizeError } from "./errors.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* React bindings.
|
|
8
|
+
*
|
|
9
|
+
* A separate entry point (`react-native-cloud-sync/hooks`) so the main one
|
|
10
|
+
* stays free of a React import - the store is usable from a saga, a background
|
|
11
|
+
* task or a plain module, and none of those should pull in a renderer.
|
|
12
|
+
*
|
|
13
|
+
* These exist because every app was writing the same three of them, and each
|
|
14
|
+
* one has the same two bugs: a response that arrives after the key changed
|
|
15
|
+
* overwrites the newer one, and a `setState` after unmount. Both are handled
|
|
16
|
+
* here once.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Binds one key to component state.
|
|
21
|
+
*
|
|
22
|
+
* ```tsx
|
|
23
|
+
* const { value, setValue, loading } = useCloudItem<Settings>(store, 'settings')
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function useCloudItem(store, key, options = {}) {
|
|
27
|
+
const {
|
|
28
|
+
initialValue = null,
|
|
29
|
+
parse = JSON.parse,
|
|
30
|
+
serialize = JSON.stringify,
|
|
31
|
+
watch = true
|
|
32
|
+
} = options;
|
|
33
|
+
const [value, setLocalValue] = useState(initialValue);
|
|
34
|
+
const [loading, setLoading] = useState(true);
|
|
35
|
+
const [error, setError] = useState(null);
|
|
36
|
+
const mounted = useRef(true);
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
mounted.current = true;
|
|
39
|
+
return () => {
|
|
40
|
+
mounted.current = false;
|
|
41
|
+
};
|
|
42
|
+
}, []);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Increments on every read. A response whose ticket is stale - because the
|
|
46
|
+
* key changed, or a refresh overtook it - is dropped instead of being written
|
|
47
|
+
* into state, which is how these hooks usually end up showing another key's
|
|
48
|
+
* value.
|
|
49
|
+
*/
|
|
50
|
+
const ticket = useRef(0);
|
|
51
|
+
|
|
52
|
+
// Held in a ref so `read` does not change identity every render and restart
|
|
53
|
+
// the effect below on each one.
|
|
54
|
+
const parseRef = useRef(parse);
|
|
55
|
+
parseRef.current = parse;
|
|
56
|
+
const read = useCallback(async () => {
|
|
57
|
+
const mine = ticket.current + 1;
|
|
58
|
+
ticket.current = mine;
|
|
59
|
+
setLoading(true);
|
|
60
|
+
try {
|
|
61
|
+
const raw = await store.getItem(key);
|
|
62
|
+
if (!mounted.current || ticket.current !== mine) return;
|
|
63
|
+
setLocalValue(raw == null ? null : parseRef.current(raw));
|
|
64
|
+
setError(null);
|
|
65
|
+
} catch (e) {
|
|
66
|
+
if (!mounted.current || ticket.current !== mine) return;
|
|
67
|
+
// The value is deliberately left as-is rather than cleared: a failed read
|
|
68
|
+
// is not evidence the key is gone, and blanking the screen on a network
|
|
69
|
+
// blip is worse than showing what was last known alongside the error.
|
|
70
|
+
setError(normalizeError(e));
|
|
71
|
+
} finally {
|
|
72
|
+
if (mounted.current && ticket.current === mine) setLoading(false);
|
|
73
|
+
}
|
|
74
|
+
}, [store, key]);
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
void read();
|
|
77
|
+
}, [read]);
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (!watch) return;
|
|
80
|
+
return store.onRemoteChange(e => {
|
|
81
|
+
// An empty `keys` list means "something changed but the provider could
|
|
82
|
+
// not say what" - an account or quota event. Re-read rather than assume
|
|
83
|
+
// it was not us.
|
|
84
|
+
if (e.keys.length > 0 && !e.keys.includes(key)) return;
|
|
85
|
+
void read();
|
|
86
|
+
});
|
|
87
|
+
}, [store, key, watch, read]);
|
|
88
|
+
const setValue = useCallback(async next => {
|
|
89
|
+
// Optimistic: the store queues retryable failures, so the write usually
|
|
90
|
+
// lands eventually even when this call rejects. Reverting on error would
|
|
91
|
+
// discard a value the outbox is still going to deliver.
|
|
92
|
+
setLocalValue(next);
|
|
93
|
+
// Invalidate any read in flight, so its older answer cannot land on top.
|
|
94
|
+
ticket.current += 1;
|
|
95
|
+
try {
|
|
96
|
+
await store.setItem(key, serialize(next));
|
|
97
|
+
if (mounted.current) setError(null);
|
|
98
|
+
} catch (e) {
|
|
99
|
+
if (mounted.current) setError(normalizeError(e));
|
|
100
|
+
throw e;
|
|
101
|
+
}
|
|
102
|
+
}, [store, key, serialize]);
|
|
103
|
+
const remove = useCallback(async () => {
|
|
104
|
+
setLocalValue(null);
|
|
105
|
+
ticket.current += 1;
|
|
106
|
+
try {
|
|
107
|
+
await store.removeItem(key);
|
|
108
|
+
if (mounted.current) setError(null);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
if (mounted.current) setError(normalizeError(e));
|
|
111
|
+
throw e;
|
|
112
|
+
}
|
|
113
|
+
}, [store, key]);
|
|
114
|
+
return {
|
|
115
|
+
value,
|
|
116
|
+
loading,
|
|
117
|
+
error,
|
|
118
|
+
setValue,
|
|
119
|
+
remove,
|
|
120
|
+
refresh: read
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Tracks a provider's account state, including the identity-switch event.
|
|
125
|
+
*
|
|
126
|
+
* ```tsx
|
|
127
|
+
* const { status, identityChanged } = useAccountStatus(icloudKV)
|
|
128
|
+
* if (status === 'noAccount') return <SignInPrompt />
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export function useAccountStatus(provider) {
|
|
132
|
+
const [status, setStatus] = useState(null);
|
|
133
|
+
const [identityChanged, setIdentityChanged] = useState(false);
|
|
134
|
+
const [error, setError] = useState(null);
|
|
135
|
+
const mounted = useRef(true);
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
mounted.current = true;
|
|
138
|
+
return () => {
|
|
139
|
+
mounted.current = false;
|
|
140
|
+
};
|
|
141
|
+
}, []);
|
|
142
|
+
const refresh = useCallback(async () => {
|
|
143
|
+
try {
|
|
144
|
+
const next = await provider.getAccountStatus();
|
|
145
|
+
if (!mounted.current) return;
|
|
146
|
+
setStatus(next);
|
|
147
|
+
setError(null);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
if (mounted.current) setError(normalizeError(e));
|
|
150
|
+
}
|
|
151
|
+
}, [provider]);
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
void refresh();
|
|
154
|
+
}, [refresh]);
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
if (provider.onAccountChange == null) return;
|
|
157
|
+
return provider.onAccountChange(e => {
|
|
158
|
+
setStatus(e.status);
|
|
159
|
+
if (e.identityChanged) setIdentityChanged(true);
|
|
160
|
+
});
|
|
161
|
+
}, [provider]);
|
|
162
|
+
return {
|
|
163
|
+
status,
|
|
164
|
+
identityChanged,
|
|
165
|
+
error,
|
|
166
|
+
refresh
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Drives a "pending sync" indicator.
|
|
171
|
+
*
|
|
172
|
+
* `pendingWrites()` is a synchronous read of a small JSON blob, so polling it
|
|
173
|
+
* is cheap - and there is no event to subscribe to, because entries are added
|
|
174
|
+
* from whichever call site failed rather than from one place that could emit.
|
|
175
|
+
*/
|
|
176
|
+
export function usePendingWrites(store, pollIntervalMs = 2000) {
|
|
177
|
+
const [pending, setPending] = useState(() => store.pendingWrites());
|
|
178
|
+
const sync = useCallback(() => {
|
|
179
|
+
setPending(store.pendingWrites());
|
|
180
|
+
}, [store]);
|
|
181
|
+
useEffect(() => {
|
|
182
|
+
sync();
|
|
183
|
+
if (pollIntervalMs <= 0) return;
|
|
184
|
+
const timer = setInterval(sync, pollIntervalMs);
|
|
185
|
+
return () => clearInterval(timer);
|
|
186
|
+
}, [sync, pollIntervalMs]);
|
|
187
|
+
const flush = useCallback(async () => {
|
|
188
|
+
try {
|
|
189
|
+
await store.flushOutbox();
|
|
190
|
+
} finally {
|
|
191
|
+
// Refresh either way: a flush that partially failed still changed the
|
|
192
|
+
// queue, and the indicator should show what is actually left.
|
|
193
|
+
sync();
|
|
194
|
+
}
|
|
195
|
+
}, [store, sync]);
|
|
196
|
+
const discard = useCallback(filter => {
|
|
197
|
+
store.discardPendingWrites(filter);
|
|
198
|
+
sync();
|
|
199
|
+
}, [store, sync]);
|
|
200
|
+
return {
|
|
201
|
+
pending,
|
|
202
|
+
flush,
|
|
203
|
+
discard
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Subscribes to remote-change events for the life of the component.
|
|
209
|
+
*
|
|
210
|
+
* The listener is held in a ref, so passing an inline arrow function does not
|
|
211
|
+
* tear down and rebuild the subscription on every render.
|
|
212
|
+
*/
|
|
213
|
+
export function useRemoteChange(store, listener) {
|
|
214
|
+
const ref = useRef(listener);
|
|
215
|
+
ref.current = listener;
|
|
216
|
+
useEffect(() => store.onRemoteChange(e => ref.current(e)), [store]);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Per-provider storage usage, refreshed on demand.
|
|
221
|
+
*
|
|
222
|
+
* Not polled: `getQuota` is a network round trip per provider, and a number
|
|
223
|
+
* that moves this slowly does not need watching.
|
|
224
|
+
*/
|
|
225
|
+
export function useQuota(store) {
|
|
226
|
+
const [quota, setQuota] = useState([]);
|
|
227
|
+
const [loading, setLoading] = useState(false);
|
|
228
|
+
const mounted = useRef(true);
|
|
229
|
+
useEffect(() => {
|
|
230
|
+
mounted.current = true;
|
|
231
|
+
return () => {
|
|
232
|
+
mounted.current = false;
|
|
233
|
+
};
|
|
234
|
+
}, []);
|
|
235
|
+
const refresh = useCallback(async () => {
|
|
236
|
+
setLoading(true);
|
|
237
|
+
try {
|
|
238
|
+
const next = await store.getQuota();
|
|
239
|
+
if (mounted.current) setQuota(next);
|
|
240
|
+
} finally {
|
|
241
|
+
if (mounted.current) setLoading(false);
|
|
242
|
+
}
|
|
243
|
+
}, [store]);
|
|
244
|
+
return useMemo(() => ({
|
|
245
|
+
quota,
|
|
246
|
+
loading,
|
|
247
|
+
refresh
|
|
248
|
+
}), [quota, loading, refresh]);
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useCallback","useEffect","useMemo","useRef","useState","normalizeError","useCloudItem","store","key","options","initialValue","parse","JSON","serialize","stringify","watch","value","setLocalValue","loading","setLoading","error","setError","mounted","current","ticket","parseRef","read","mine","raw","getItem","e","onRemoteChange","keys","length","includes","setValue","next","setItem","remove","removeItem","refresh","useAccountStatus","provider","status","setStatus","identityChanged","setIdentityChanged","getAccountStatus","onAccountChange","usePendingWrites","pollIntervalMs","pending","setPending","pendingWrites","sync","timer","setInterval","clearInterval","flush","flushOutbox","discard","filter","discardPendingWrites","useRemoteChange","listener","ref","useQuota","quota","setQuota","getQuota"],"sourceRoot":"../../src","sources":["hooks.ts"],"mappings":";;AAAA,SAASA,WAAW,EAAEC,SAAS,EAAEC,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAGzE,SAASC,cAAc,QAA6B,aAAU;;AAS9D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAmCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAC1BC,KAAiB,EACjBC,GAAW,EACXC,OAA+B,GAAG,CAAC,CAAC,EACb;EACvB,MAAM;IACJC,YAAY,GAAG,IAAI;IACnBC,KAAK,GAAGC,IAAI,CAACD,KAA2B;IACxCE,SAAS,GAAGD,IAAI,CAACE,SAAiC;IAClDC,KAAK,GAAG;EACV,CAAC,GAAGN,OAAO;EAEX,MAAM,CAACO,KAAK,EAAEC,aAAa,C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,CAAC;EAC5BF,SAAS,CAAC,MAAM;IACdqB,OAAO,CAACC,OAAO,GAAG,IAAI;IACtB,OAAO,MAAM;MACXD,OAAO,CAACC,OAAO,GAAG,KAAK;IACzB,CAAC;EACH,CAAC,EAAE,EAAE,CAAC;EAEN,MAAMiB,OAAO,GAAGxC,WAAW,CAAC,YAA2B;IACrDmB,UAAU,CAAC,IAAI,CAAC;IAChB,IAAI;MACF,MAAMiB,IAAI,GAAG,MAAM7B,KAAK,CAAC8D,QAAQ,CAAC,CAAC;MACnC,IAAI/C,OAAO,CAACC,OAAO,EAAE6C,QAAQ,CAAChC,IAAI,CAAC;IACrC,CAAC,SACO;MACN,IAAId,OAAO,CAACC,OAAO,EAAEJ,UAAU,CAAC,KAAK,CAAC;IACxC;EACF,CAAC,EAAE,CAACZ,KAAK,CAAC,CAAC;EAEX,OAAOL,OAAO,CAAC,OAAO;IAAEiE,KAAK;IAAEjD,OAAO;IAAEsB;EAAQ,CAAC,CAAC,EAAE,CAAC2B,KAAK,EAAEjD,OAAO,EAAEsB,OAAO,CAAC,CAAC;AAChF","ignoreList":[]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* react-native-cloud-sync
|
|
5
|
+
*
|
|
6
|
+
* iCloud key-value store, CloudKit and Google Drive behind one API, on iOS,
|
|
7
|
+
* Android and the web, on both React Native architectures.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// Providers, each usable directly.
|
|
11
|
+
export { icloudKV, getAllItems as icloudKVGetAllItems, sync as icloudKVSync } from './providers/icloudKV';
|
|
12
|
+
export { cloudKit, createCloudKitProvider, zones as cloudKitZones, assets as cloudKitAssets } from "./providers/cloudKit.js";
|
|
13
|
+
export { cloudKitBackup } from "./providers/cloudKitBackup.js";
|
|
14
|
+
export { cloudKitEncrypted } from "./providers/cloudKitEncrypted.js";
|
|
15
|
+
export { icloudDocuments } from "./providers/icloudDocuments.js";
|
|
16
|
+
export { googleDrive, createGoogleDriveProvider } from "./providers/googleDrive.js";
|
|
17
|
+
export { googleDriveFiles } from "./providers/googleDriveFiles.js";
|
|
18
|
+
export { createMemoryProvider } from "./providers/memory.js";
|
|
19
|
+
|
|
20
|
+
// The facade over them.
|
|
21
|
+
export { createCloudStore } from "./store.js";
|
|
22
|
+
export { resolveByTimestamp, resolveByModifiedAt, resolveByPreferenceOrder, resolveFirstOf } from "./resolvers.js";
|
|
23
|
+
|
|
24
|
+
// Key rules, for callers whose keys come from somewhere they do not control.
|
|
25
|
+
export { checkKey, sanitizeKey } from "./internal/keys.js";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The abort-signal shape the cancellable APIs accept.
|
|
29
|
+
*
|
|
30
|
+
* Structural rather than the DOM `AbortSignal`, so any polyfill satisfies it
|
|
31
|
+
* and consumers do not need `lib.dom` in their tsconfig.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
// Configuration for the REST-backed paths.
|
|
35
|
+
export { configureCloudKit, configureGoogleDrive, configureGoogleDriveFiles, isCloudKitConfigured, isGoogleDriveConfigured, isGoogleDriveFilesConfigured } from "./config.js";
|
|
36
|
+
// The `GoogleDriveFileAdapter` contract is base64 in and base64 out, while every
|
|
37
|
+
// modern filesystem API is byte-oriented - so the codec that bridges them ships
|
|
38
|
+
// here rather than making each adapter author find one. Dependency-free and
|
|
39
|
+
// Hermes-safe, which `Buffer`/`atob` are not.
|
|
40
|
+
export { base64ToBytes, bytesToBase64 } from "./internal/base64.js";
|
|
41
|
+
|
|
42
|
+
// Errors - the part callers branch on.
|
|
43
|
+
export { CloudSyncError, ErrorCode, isCancelled, isCloudSyncError, isRetryable, requiresUserAction } from "./errors.js";
|
|
44
|
+
export { DEFAULT_TIERING } from "./types.js";
|
|
45
|
+
export { setLogsEnabled } from "./internal/logger.js";
|
|
46
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["icloudKV","getAllItems","icloudKVGetAllItems","sync","icloudKVSync","cloudKit","createCloudKitProvider","zones","cloudKitZones","assets","cloudKitAssets","cloudKitBackup","cloudKitEncrypted","icloudDocuments","googleDrive","createGoogleDriveProvider","googleDriveFiles","createMemoryProvider","createCloudStore","resolveByTimestamp","resolveByModifiedAt","resolveByPreferenceOrder","resolveFirstOf","checkKey","sanitizeKey","configureCloudKit","configureGoogleDrive","configureGoogleDriveFiles","isCloudKitConfigured","isGoogleDriveConfigured","isGoogleDriveFilesConfigured","base64ToBytes","bytesToBase64","CloudSyncError","ErrorCode","isCancelled","isCloudSyncError","isRetryable","requiresUserAction","DEFAULT_TIERING","setLogsEnabled"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA,SACEA,QAAQ,EACRC,WAAW,IAAIC,mBAAmB,EAClCC,IAAI,IAAIC,YAAY,QACf,sBAAsB;AAC7B,SACEC,QAAQ,EACRC,sBAAsB,EACtBC,KAAK,IAAIC,aAAa,EACtBC,MAAM,IAAIC,cAAc,QACnB,yBAAsB;AAC7B,SAASC,cAAc,QAAQ,+BAA4B;AAC3D,SAASC,iBAAiB,QAAQ,kCAA+B;AACjE,SAASC,eAAe,QAAQ,gCAA6B;AAC7D,SAASC,WAAW,EAAEC,yBAAyB,QAAQ,4BAAyB;AAChF,SAASC,gBAAgB,QAAQ,iCAA8B;AAC/D,SAASC,oBAAoB,QAAQ,uBAAoB;;AAEzD;AACA,SACEC,gBAAgB,QAMX,YAAS;AAChB,SACEC,kBAAkB,EAClBC,mBAAmB,EACnBC,wBAAwB,EACxBC,cAAc,QACT,gBAAa;;AAEpB;AACA,SAASC,QAAQ,EAAEC,WAAW,QAAQ,oBAAiB;;AAEvD;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA,SACEC,iBAAiB,EACjBC,oBAAoB,EACpBC,yBAAyB,EACzBC,oBAAoB,EACpBC,uBAAuB,EACvBC,4BAA4B,QACvB,aAAU;AAKjB;AACA;AACA;AACA;AACA,SAASC,aAAa,EAAEC,aAAa,QAAQ,sBAAmB;;AAEhE;AACA,SACEC,cAAc,EACdC,SAAS,EACTC,WAAW,EACXC,gBAAgB,EAChBC,WAAW,EACXC,kBAAkB,QACb,aAAU;AA8BjB,SAASC,eAAe,QAAQ,YAAS;AAEzC,SAASC,cAAc,QAAQ,sBAAmB","ignoreList":[]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { AppState } from 'react-native';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* When the store should try to drain its outbox by itself.
|
|
7
|
+
*
|
|
8
|
+
* A durable queue with no trigger is only half a feature: every retryable
|
|
9
|
+
* failure is captured correctly and then sits there until the app remembers to
|
|
10
|
+
* call `flushOutbox()`. Most apps wire that to the same two moments, so the
|
|
11
|
+
* store can do it for them.
|
|
12
|
+
*
|
|
13
|
+
* Deliberately *not* network-aware. Detecting a reconnect needs
|
|
14
|
+
* `@react-native-community/netinfo`, and taking a dependency on it to save one
|
|
15
|
+
* line in the host app is a bad trade - it would be installed for every user of
|
|
16
|
+
* this package whether or not they wanted it. Call `flushOutbox()` from your
|
|
17
|
+
* own NetInfo listener for that; foreground plus a slow interval already covers
|
|
18
|
+
* the common case, since an app that regained connectivity is nearly always
|
|
19
|
+
* about to be foregrounded anyway.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const DEFAULT_INTERVAL_MS = 60_000;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Wires `flush` to app foreground and to a timer. Returns an unsubscribe.
|
|
26
|
+
*
|
|
27
|
+
* Failures are swallowed on purpose: this is a background retry nobody is
|
|
28
|
+
* waiting on, and the store already reports every failure through `onError`
|
|
29
|
+
* and `onDropped`. Letting a rejection escape a timer callback would surface as
|
|
30
|
+
* an unhandled rejection warning for something the app deliberately deferred.
|
|
31
|
+
*/
|
|
32
|
+
export function attachAutoFlush(flush, config = {}) {
|
|
33
|
+
const onForeground = config.onForeground ?? true;
|
|
34
|
+
const intervalMs = config.intervalMs ?? DEFAULT_INTERVAL_MS;
|
|
35
|
+
const safeFlush = () => {
|
|
36
|
+
void flush().catch(() => undefined);
|
|
37
|
+
};
|
|
38
|
+
let timer = null;
|
|
39
|
+
let appStateSub = null;
|
|
40
|
+
if (intervalMs > 0) {
|
|
41
|
+
timer = setInterval(safeFlush, intervalMs);
|
|
42
|
+
// A pending retry must never be the reason a process stays alive.
|
|
43
|
+
timer.unref?.();
|
|
44
|
+
}
|
|
45
|
+
if (onForeground) {
|
|
46
|
+
let previous = AppState.currentState;
|
|
47
|
+
appStateSub = AppState.addEventListener('change', next => {
|
|
48
|
+
// Only the transition *into* active, not every state event - iOS emits
|
|
49
|
+
// `inactive` on the way through, and flushing twice per foreground would
|
|
50
|
+
// double the request rate for no benefit.
|
|
51
|
+
if (next === 'active' && previous !== 'active') safeFlush();
|
|
52
|
+
previous = next;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return () => {
|
|
56
|
+
if (timer != null) clearInterval(timer);
|
|
57
|
+
timer = null;
|
|
58
|
+
appStateSub?.remove();
|
|
59
|
+
appStateSub = null;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=autoFlush.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["AppState","DEFAULT_INTERVAL_MS","attachAutoFlush","flush","config","onForeground","intervalMs","safeFlush","catch","undefined","timer","appStateSub","setInterval","unref","previous","currentState","addEventListener","next","clearInterval","remove"],"sourceRoot":"../../../src","sources":["internal/autoFlush.ts"],"mappings":";;AAAA,SAASA,QAAQ,QAAQ,cAAc;;AAIvC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAoBA,MAAMC,mBAAmB,GAAG,MAAM;;AAElC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAC7BC,KAA6B,EAC7BC,MAAuB,GAAG,CAAC,CAAC,EACf;EACb,MAAMC,YAAY,GAAGD,MAAM,CAACC,YAAY,IAAI,IAAI;EAChD,MAAMC,UAAU,GAAGF,MAAM,CAACE,UAAU,IAAIL,mBAAmB;EAE3D,MAAMM,SAAS,GAAGA,CAAA,KAAY;IAC5B,KAAKJ,KAAK,CAAC,CAAC,CAACK,KAAK,CAAC,MAAMC,SAAS,CAAC;EACrC,CAAC;EAED,IAAIC,KAA4C,GAAG,IAAI;EACvD,IAAIC,WAA0C,GAAG,IAAI;EAErD,IAAIL,UAAU,GAAG,CAAC,EAAE;IAClBI,KAAK,GAAGE,WAAW,CAACL,SAAS,EAAED,UAAU,CAAC;IAC1C;IACAI,KAAK,CAACG,KAAK,GAAG,CAAC;EACjB;EAEA,IAAIR,YAAY,EAAE;IAChB,IAAIS,QAAQ,GAAGd,QAAQ,CAACe,YAAY;IACpCJ,WAAW,GAAGX,QAAQ,CAACgB,gBAAgB,CAAC,QAAQ,EAAGC,IAAI,IAAK;MAC1D;MACA;MACA;MACA,IAAIA,IAAI,KAAK,QAAQ,IAAIH,QAAQ,KAAK,QAAQ,EAAEP,SAAS,CAAC,CAAC;MAC3DO,QAAQ,GAAGG,IAAI;IACjB,CAAC,CAAC;EACJ;EAEA,OAAO,MAAM;IACX,IAAIP,KAAK,IAAI,IAAI,EAAEQ,aAAa,CAACR,KAAK,CAAC;IACvCA,KAAK,GAAG,IAAI;IACZC,WAAW,EAAEQ,MAAM,CAAC,CAAC;IACrBR,WAAW,GAAG,IAAI;EACpB,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Base64 encode/decode for the Drive binary-file path.
|
|
5
|
+
*
|
|
6
|
+
* Hand-rolled rather than `Buffer` or `atob`/`btoa`: neither is guaranteed to
|
|
7
|
+
* exist in a Hermes runtime, and this package ships with zero runtime
|
|
8
|
+
* dependencies (see `googleDriveFiles` for why - the file adapter itself is
|
|
9
|
+
* injected, in base64, for the same reason). A chunk is a few MB at most, so a
|
|
10
|
+
* lookup-table implementation is fast enough without needing a native module.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
14
|
+
|
|
15
|
+
/** `CHARS[c]` -> its 6-bit value, indexed by char code. -1 for anything else. */
|
|
16
|
+
const DECODE_TABLE = (() => {
|
|
17
|
+
const table = new Int16Array(128).fill(-1);
|
|
18
|
+
for (let i = 0; i < CHARS.length; i++) table[CHARS.charCodeAt(i)] = i;
|
|
19
|
+
return table;
|
|
20
|
+
})();
|
|
21
|
+
export function bytesToBase64(bytes) {
|
|
22
|
+
const chunks = [];
|
|
23
|
+
const len = bytes.length;
|
|
24
|
+
for (let i = 0; i < len; i += 3) {
|
|
25
|
+
const b0 = bytes[i];
|
|
26
|
+
const hasB1 = i + 1 < len;
|
|
27
|
+
const hasB2 = i + 2 < len;
|
|
28
|
+
const b1 = hasB1 ? bytes[i + 1] : 0;
|
|
29
|
+
const b2 = hasB2 ? bytes[i + 2] : 0;
|
|
30
|
+
chunks.push(CHARS[b0 >> 2], CHARS[(b0 & 0x03) << 4 | b1 >> 4], hasB1 ? CHARS[(b1 & 0x0f) << 2 | b2 >> 6] : '=', hasB2 ? CHARS[b2 & 0x3f] : '=');
|
|
31
|
+
}
|
|
32
|
+
return chunks.join('');
|
|
33
|
+
}
|
|
34
|
+
export function base64ToBytes(base64) {
|
|
35
|
+
// Strip whitespace some fs libraries insert into long base64 strings, and
|
|
36
|
+
// any padding - length is derived from the real character count instead.
|
|
37
|
+
const clean = base64.replace(/[\r\n\s]/g, '').replace(/=+$/, '');
|
|
38
|
+
const len = clean.length;
|
|
39
|
+
const outLen = Math.floor(len * 3 / 4);
|
|
40
|
+
const bytes = new Uint8Array(outLen);
|
|
41
|
+
let p = 0;
|
|
42
|
+
for (let i = 0; i < len; i += 4) {
|
|
43
|
+
const e0 = DECODE_TABLE[clean.charCodeAt(i)] ?? -1;
|
|
44
|
+
const e1 = i + 1 < len ? DECODE_TABLE[clean.charCodeAt(i + 1)] ?? -1 : -1;
|
|
45
|
+
const e2 = i + 2 < len ? DECODE_TABLE[clean.charCodeAt(i + 2)] ?? -1 : -1;
|
|
46
|
+
const e3 = i + 3 < len ? DECODE_TABLE[clean.charCodeAt(i + 3)] ?? -1 : -1;
|
|
47
|
+
if (p < outLen) bytes[p++] = e0 << 2 | e1 >> 4;
|
|
48
|
+
if (p < outLen) bytes[p++] = (e1 & 0x0f) << 4 | e2 >> 2;
|
|
49
|
+
if (p < outLen) bytes[p++] = (e2 & 0x03) << 6 | e3;
|
|
50
|
+
}
|
|
51
|
+
return bytes;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=base64.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["CHARS","DECODE_TABLE","table","Int16Array","fill","i","length","charCodeAt","bytesToBase64","bytes","chunks","len","b0","hasB1","hasB2","b1","b2","push","join","base64ToBytes","base64","clean","replace","outLen","Math","floor","Uint8Array","p","e0","e1","e2","e3"],"sourceRoot":"../../../src","sources":["internal/base64.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,MAAMA,KAAK,GAAG,kEAAkE;;AAEhF;AACA,MAAMC,YAAY,GAAG,CAAC,MAAM;EAC1B,MAAMC,KAAK,GAAG,IAAIC,UAAU,CAAC,GAAG,CAAC,CAACC,IAAI,CAAC,CAAC,CAAC,CAAC;EAC1C,KAAK,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGL,KAAK,CAACM,MAAM,EAAED,CAAC,EAAE,EAAEH,KAAK,CAACF,KAAK,CAACO,UAAU,CAACF,CAAC,CAAC,CAAC,GAAGA,CAAC;EACrE,OAAOH,KAAK;AACd,C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,CAAC,GAAIC,EAAE;EACtD;EAEA,OAAOtB,KAAK;AACd","ignoreList":[]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* UTF-8 byte length.
|
|
5
|
+
*
|
|
6
|
+
* `String.length` counts UTF-16 code units, which under-reports for any
|
|
7
|
+
* non-ASCII payload - the exact way a "just under the limit" value turns into a
|
|
8
|
+
* server-side rejection.
|
|
9
|
+
*
|
|
10
|
+
* Lives in its own module because both the size-tiering path and the key
|
|
11
|
+
* validator need it, and neither should have to import a REST client to get it.
|
|
12
|
+
*/
|
|
13
|
+
export function byteLength(s) {
|
|
14
|
+
if (typeof TextEncoder !== 'undefined') return new TextEncoder().encode(s).length;
|
|
15
|
+
|
|
16
|
+
// Hermes without TextEncoder, and old JSC. `encodeURIComponent` percent-escapes
|
|
17
|
+
// every non-ASCII byte, so counting the escapes counts the UTF-8 bytes.
|
|
18
|
+
let bytes = 0;
|
|
19
|
+
const encoded = encodeURIComponent(s);
|
|
20
|
+
for (let i = 0; i < encoded.length; i += 1) if (encoded[i] === '%') {
|
|
21
|
+
bytes += 1;
|
|
22
|
+
i += 2;
|
|
23
|
+
} else {
|
|
24
|
+
bytes += 1;
|
|
25
|
+
}
|
|
26
|
+
return bytes;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=bytes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["byteLength","s","TextEncoder","encode","length","bytes","encoded","encodeURIComponent","i"],"sourceRoot":"../../../src","sources":["internal/bytes.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,UAAUA,CAACC,CAAS,EAAU;EAC5C,IAAI,OAAOC,WAAW,KAAK,WAAW,EAAE,OAAO,IAAIA,WAAW,CAAC,CAAC,CAACC,MAAM,CAACF,CAAC,CAAC,CAACG,MAAM;;EAEjF;EACA;EACA,IAAIC,KAAK,GAAG,CAAC;EACb,MAAMC,OAAO,GAAGC,kBAAkB,CAACN,CAAC,CAAC;EACrC,KAAK,IAAIO,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGF,OAAO,CAACF,MAAM,EAAEI,CAAC,IAAI,CAAC,EACxC,IAAIF,OAAO,CAACE,CAAC,CAAC,KAAK,GAAG,EAAE;IACtBH,KAAK,IAAI,CAAC;IACVG,CAAC,IAAI,CAAC;EACR,CAAC,MACI;IACHH,KAAK,IAAI,CAAC;EACZ;EAEF,OAAOA,KAAK;AACd","ignoreList":[]}
|