jazz-react-native 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,sDAAsD;AACtD,OAAO,EAKH,eAAe,EAMf,iBAAiB,GAEpB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAE1D,OAAO,OAAO,MAAM,iCAAiC,CAAC;AACtD,OAAO,KAAK,OAAO,MAAM,cAAc,CAAC;AAExC,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAwC3D,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACrC,OAA+D;IAE/D,MAAM,WAAW,GAAG,mBAAmB,CAAC;QACpC,SAAS,EAAE,IAAI,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC;QACtC,EAAE,EAAE,OAAO,CAAC,IAAI,GAAG,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;QACjD,IAAI,EAAE,QAAQ;QACd,WAAW,EAAE,IAAI;KACpB,CAAC,CAAC;IACH,IAAI,oBAAoB,GAAG,IAAI,CAAC;IAEhC,IAAI,0BAA0B,GAAG,OAAO,CAAC,mBAAmB,IAAI,GAAG,CAAC;IAEpE,MAAM,wBAAwB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC,KAAK,EAAE,EAAE;QAChE,IAAI,KAAK,CAAC,WAAW,EAAE,CAAC;YACpB,0BAA0B,GAAG,OAAO,CAAC,mBAAmB,IAAI,GAAG,CAAC;QACpE,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,MAAM,OAAO,GACT,MAAM,IAAI,OAAO;QACb,CAAC,CAAC,MAAM,iBAAiB,CAAC;YACpB,aAAa,EAAE,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,CAAC,SAAS,GAAG,GAAG,CAAC,CAAC,CAAC,EAAE,GACxD,KAAK,CAAC,EACV,IAAI,YAAY,EAAE,CAAC;AACvB,CAAC;AAED,6BAA6B;AAC7B,MAAM,UAAU,eAAe,CAC3B,SAAiB;IAQjB,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IACrC,MAAM,KAAK,GAAG,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IAEnC,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,QAAQ,EAAE,CAAC;QAClC,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,IAAI,SAA6B,CAAC;IAClC,IAAI,OAA0B,CAAC;IAC/B,IAAI,YAAsC,CAAC;IAE3C,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACrB,SAAS,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACrB,OAAO,GAAG,KAAK,CAAC,CAAC,CAAU,CAAC;QAC5B,YAAY,GAAG,KAAK,CAAC,CAAC,CAAiB,CAAC;IAC5C,CAAC;SAAM,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO,GAAG,KAAK,CAAC,CAAC,CAAU,CAAC;QAC5B,YAAY,GAAG,KAAK,CAAC,CAAC,CAAiB,CAAC;IAC5C,CAAC;IAED,IAAI,CAAC,OAAO,IAAI,CAAC,YAAY,EAAE,CAAC;QAC5B,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,CAAC;AAChD,CAAC;AAED,SAAS;AAET,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC"}
@@ -0,0 +1,21 @@
1
+ import React from "react";
2
+ import { ImageDefinition } from "jazz-tools";
3
+ /** @category Media */
4
+ export declare function useProgressiveImg({ image, maxWidth, }: {
5
+ image: ImageDefinition | null | undefined;
6
+ maxWidth?: number;
7
+ }): {
8
+ src: string | undefined;
9
+ res: `${number}x${number}` | "placeholder" | undefined;
10
+ originalSize: import("jazz-tools").co<[number, number]> | undefined;
11
+ };
12
+ /** @category Media */
13
+ export declare function ProgressiveImg({ children, image, maxWidth, }: {
14
+ children: (result: {
15
+ src: string | undefined;
16
+ res: `${number}x${number}` | "placeholder" | undefined;
17
+ originalSize: readonly [number, number] | undefined;
18
+ }) => React.ReactNode;
19
+ image: ImageDefinition | null | undefined;
20
+ maxWidth?: number;
21
+ }): React.ReactNode;
package/dist/media.js ADDED
@@ -0,0 +1,44 @@
1
+ import { useEffect, useState } from "react";
2
+ /** @category Media */
3
+ export function useProgressiveImg({ image, maxWidth, }) {
4
+ const [current, setCurrent] = useState(undefined);
5
+ useEffect(() => {
6
+ let lastHighestRes;
7
+ if (!image)
8
+ return;
9
+ const unsub = image.subscribe({}, (update) => {
10
+ const highestRes = update?.highestResAvailable({ maxWidth });
11
+ if (highestRes) {
12
+ if (highestRes.res !== lastHighestRes) {
13
+ lastHighestRes = highestRes.res;
14
+ const blob = highestRes.stream.toBlob();
15
+ if (blob) {
16
+ const blobURI = URL.createObjectURL(blob);
17
+ setCurrent({ src: blobURI, res: highestRes.res });
18
+ return () => {
19
+ setTimeout(() => URL.revokeObjectURL(blobURI), 200);
20
+ };
21
+ }
22
+ }
23
+ }
24
+ else {
25
+ setCurrent({
26
+ src: update?.placeholderDataURL,
27
+ res: "placeholder",
28
+ });
29
+ }
30
+ });
31
+ return unsub;
32
+ }, [image?.id, maxWidth]);
33
+ return {
34
+ src: current?.src,
35
+ res: current?.res,
36
+ originalSize: image?.originalSize,
37
+ };
38
+ }
39
+ /** @category Media */
40
+ export function ProgressiveImg({ children, image, maxWidth, }) {
41
+ const result = useProgressiveImg({ image, maxWidth });
42
+ return result && children(result);
43
+ }
44
+ //# sourceMappingURL=media.js.map
@@ -0,0 +1 @@
1
+ {"version":3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C,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACjB,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC;IAE1B,OAAO;QACH,GAAG,EAAE,OAAO,EAAE,GAAG;QACjB,GAAG,EAAE,OAAO,EAAE,GAAG;QACjB,YAAY,EAAE,KAAK,EAAE,YAAY;KACpC,CAAC;AACN,CAAC;AAED,sBAAsB;AACtB,MAAM,UAAU,cAAc,CAAC,EAC3B,QAAQ,EACR,KAAK,EACL,QAAQ,GASX;IACG,MAAM,MAAM,GAAG,iBAAiB,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;IACtD,OAAO,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;AACtC,CAAC"}
@@ -0,0 +1,15 @@
1
+ export interface NativeStorage {
2
+ get(key: string): Promise<string | undefined>;
3
+ set(key: string, value: string): Promise<void>;
4
+ delete(key: string): Promise<void>;
5
+ clearAll(): Promise<void>;
6
+ }
7
+ export declare class NativeStorageContext {
8
+ private static instance;
9
+ private storageInstance;
10
+ private constructor();
11
+ static getInstance(): NativeStorageContext;
12
+ initialize(db: NativeStorage): void;
13
+ getStorage(): NativeStorage;
14
+ }
15
+ export default NativeStorageContext;
@@ -0,0 +1,24 @@
1
+ export class NativeStorageContext {
2
+ static instance;
3
+ storageInstance = null;
4
+ constructor() { }
5
+ static getInstance() {
6
+ if (!NativeStorageContext.instance) {
7
+ NativeStorageContext.instance = new NativeStorageContext();
8
+ }
9
+ return NativeStorageContext.instance;
10
+ }
11
+ initialize(db) {
12
+ if (!this.storageInstance) {
13
+ this.storageInstance = db;
14
+ }
15
+ }
16
+ getStorage() {
17
+ if (!this.storageInstance) {
18
+ throw new Error("Storage instance is not initialized.");
19
+ }
20
+ return this.storageInstance;
21
+ }
22
+ }
23
+ export default NativeStorageContext;
24
+ //# sourceMappingURL=native-storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"native-storage.js","sourceRoot":"","sources":["../src/native-storage.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,oBAAoB;IACrB,MAAM,CAAC,QAAQ,CAAuB;IACtC,eAAe,GAAyB,IAAI,CAAC;IAErD,gBAAuB,CAAC;IAEjB,MAAM,CAAC,WAAW;QACrB,IAAI,CAAC,oBAAoB,CAAC,QAAQ,EAAE,CAAC;YACjC,oBAAoB,CAAC,QAAQ,GAAG,IAAI,oBAAoB,EAAE,CAAC;QAC/D,CAAC;QACD,OAAO,oBAAoB,CAAC,QAAQ,CAAC;IACzC,CAAC;IAEM,UAAU,CAAC,EAAiB;QAC/B,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,CAAC;YACxB,IAAI,CAAC,eAAe,GAAG,EAAE,CAAC;QAC9B,CAAC;IACL,CAAC;IAEM,UAAU;QACb,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;QAC5D,CAAC;QACD,OAAO,IAAI,CAAC,eAAe,CAAC;IAChC,CAAC;CACJ;AAED,eAAe,oBAAoB,CAAC"}
@@ -0,0 +1,46 @@
1
+ import React from "react";
2
+ import { Account, AccountClass, AnonymousJazzAgent, AuthMethod, CoValue, CoValueClass, DeeplyLoaded, DepthsIn, ID } from "jazz-tools";
3
+ import { NativeStorage } from "./index.js";
4
+ /** @category Context & Hooks */
5
+ export declare function createJazzRNApp<Acc extends Account>({ nativeStorage, AccountSchema, }: {
6
+ nativeStorage: NativeStorage;
7
+ AccountSchema?: AccountClass<Acc>;
8
+ }): JazzReactApp<Acc>;
9
+ /** @category Context & Hooks */
10
+ export interface JazzReactApp<Acc extends Account> {
11
+ /** @category Provider Component */
12
+ Provider: React.FC<{
13
+ children: React.ReactNode;
14
+ auth: AuthMethod | "guest";
15
+ peer: `wss://${string}` | `ws://${string}`;
16
+ storage?: "indexedDB" | "singleTabOPFS";
17
+ }>;
18
+ /** @category Hooks */
19
+ useAccount(): {
20
+ me: Acc;
21
+ logOut: () => void;
22
+ };
23
+ /** @category Hooks */
24
+ useAccount<D extends DepthsIn<Acc>>(depth: D): {
25
+ me: DeeplyLoaded<Acc, D> | undefined;
26
+ logOut: () => void;
27
+ };
28
+ /** @category Hooks */
29
+ useAccountOrGuest(): {
30
+ me: Acc | AnonymousJazzAgent;
31
+ };
32
+ useAccountOrGuest<D extends DepthsIn<Acc>>(depth: D): {
33
+ me: DeeplyLoaded<Acc, D> | undefined | AnonymousJazzAgent;
34
+ };
35
+ /** @category Hooks */
36
+ useCoState<V extends CoValue, D>(Schema: {
37
+ new (...args: any[]): V;
38
+ } & CoValueClass, id: ID<V> | undefined, depth?: D & DepthsIn<V>): DeeplyLoaded<V, D> | undefined;
39
+ /** @category Hooks */
40
+ useAcceptInvite<V extends CoValue>({ invitedObjectSchema, onAccept, forValueHint, }: {
41
+ invitedObjectSchema: CoValueClass<V>;
42
+ onAccept: (projectID: ID<V>) => void;
43
+ forValueHint?: string;
44
+ }): void;
45
+ }
46
+ export * from "./media.js";
@@ -0,0 +1,130 @@
1
+ import React, { useEffect, useState } from "react";
2
+ import { Account, subscribeToCoValue, } from "jazz-tools";
3
+ import { createJazzRNContext, NativeStorageContext, parseInviteLink, } from "./index.js";
4
+ import { Linking } from "react-native";
5
+ /** @category Context & Hooks */
6
+ export function createJazzRNApp({ nativeStorage, AccountSchema = Account, }) {
7
+ const JazzContext = React.createContext(undefined);
8
+ NativeStorageContext.getInstance().initialize(nativeStorage);
9
+ function Provider({ children, auth, peer, storage, }) {
10
+ const [ctx, setCtx] = useState();
11
+ const [sessionCount, setSessionCount] = useState(0);
12
+ useEffect(() => {
13
+ const promiseWithDoneCallback = createJazzRNContext(auth === "guest"
14
+ ? {
15
+ peer,
16
+ storage,
17
+ }
18
+ : {
19
+ AccountSchema,
20
+ auth: auth,
21
+ peer,
22
+ storage,
23
+ }).then((context) => {
24
+ setCtx({
25
+ ...context,
26
+ logOut: () => {
27
+ context.logOut();
28
+ setCtx(undefined);
29
+ setSessionCount(sessionCount + 1);
30
+ },
31
+ });
32
+ return context.done;
33
+ });
34
+ return () => {
35
+ void promiseWithDoneCallback.then((done) => done());
36
+ };
37
+ }, [AccountSchema, auth, peer, storage, sessionCount]);
38
+ return (<JazzContext.Provider value={ctx}>
39
+ {ctx && children}
40
+ </JazzContext.Provider>);
41
+ }
42
+ function useAccount(depth) {
43
+ const context = React.useContext(JazzContext);
44
+ if (!context) {
45
+ throw new Error("useAccount must be used within a JazzProvider");
46
+ }
47
+ if (!("me" in context)) {
48
+ throw new Error("useAccount can't be used in a JazzProvider with auth === 'guest' - consider using useAccountOrGuest()");
49
+ }
50
+ const me = useCoState(context?.me.constructor, context?.me.id, depth);
51
+ return {
52
+ me: depth === undefined ? me || context.me : me,
53
+ logOut: context.logOut,
54
+ };
55
+ }
56
+ function useAccountOrGuest(depth) {
57
+ const context = React.useContext(JazzContext);
58
+ if (!context) {
59
+ throw new Error("useAccountOrGuest must be used within a JazzProvider");
60
+ }
61
+ const contextMe = "me" in context ? context.me : undefined;
62
+ const me = useCoState(contextMe?.constructor, contextMe?.id, depth);
63
+ if ("me" in context) {
64
+ return {
65
+ me: depth === undefined ? me || context.me : me,
66
+ };
67
+ }
68
+ else {
69
+ return { me: context.guest };
70
+ }
71
+ }
72
+ function useCoState(
73
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
+ Schema, id, depth = []) {
75
+ const [state, setState] = useState({ value: undefined });
76
+ const context = React.useContext(JazzContext);
77
+ if (!context) {
78
+ throw new Error("useCoState must be used within a JazzProvider");
79
+ }
80
+ useEffect(() => {
81
+ if (!id)
82
+ return;
83
+ return subscribeToCoValue(Schema, id, "me" in context ? context.me : context.guest, depth, (value) => {
84
+ setState({ value });
85
+ });
86
+ }, [Schema, id, context]);
87
+ return state.value;
88
+ }
89
+ function useAcceptInvite({ invitedObjectSchema, onAccept, forValueHint, }) {
90
+ const context = React.useContext(JazzContext);
91
+ if (!context) {
92
+ throw new Error("useAcceptInvite must be used within a JazzProvider");
93
+ }
94
+ if (!("me" in context)) {
95
+ throw new Error("useAcceptInvite can't be used in a JazzProvider with auth === 'guest'.");
96
+ }
97
+ useEffect(() => {
98
+ const handleDeepLink = ({ url }) => {
99
+ const result = parseInviteLink(url);
100
+ if (result && result.valueHint === forValueHint) {
101
+ context.me
102
+ .acceptInvite(result.valueID, result.inviteSecret, invitedObjectSchema)
103
+ .then(() => {
104
+ onAccept(result.valueID);
105
+ })
106
+ .catch((e) => {
107
+ console.error("Failed to accept invite", e);
108
+ });
109
+ }
110
+ };
111
+ const linkingListener = Linking.addEventListener("url", handleDeepLink);
112
+ void Linking.getInitialURL().then((url) => {
113
+ if (url)
114
+ handleDeepLink({ url });
115
+ });
116
+ return () => {
117
+ linkingListener.remove();
118
+ };
119
+ }, [context, onAccept, invitedObjectSchema, forValueHint]);
120
+ }
121
+ return {
122
+ Provider,
123
+ useAccount,
124
+ useAccountOrGuest,
125
+ useCoState,
126
+ useAcceptInvite,
127
+ };
128
+ }
129
+ export * from "./media.js";
130
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEnD,OAAO,EACH,OAAO,EASP,kBAAkB,GACrB,MAAM,YAAY,CAAC;AACpB,OAAO,EAGH,mBAAmB,EACnB,oBAAoB,EAEpB,eAAe,GAClB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,gCAAgC;AAChC,MAAM,UAAU,eAAe,CAAsB,EACjD,aAAa,EACb,aAAa,GAAG,OAAuC,GAI1D;IACG,MAAM,WAAW,GAAG,KAAK,CAAC,aAAa,CAErC,SAAS,CAAC,CAAC;IAEb,oBAAoB,CAAC,WAAW,EAAE,CAAC,UAAU,CAAC,aAAa,CAAC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AG,CAAC,EAAE,GAAG,EAAmB,EAAE,EAAE;gBAChD,MAAM,MAAM,GAAG,eAAe,CAAI,GAAG,CAAC,CAAC;gBACvC,IAAI,MAAM,IAAI,MAAM,CAAC,SAAS,KAAK,YAAY,EAAE,CAAC;oBAC9C,OAAO,CAAC,EAAE;yBACL,YAAY,CACT,MAAM,CAAC,OAAO,EACd,MAAM,CAAC,YAAY,EACnB,mBAAmB,CACtB;yBACA,IAAI,CAAC,GAAG,EAAE;wBACP,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;oBAC7B,CAAC,CAAC;yBACD,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE;wBACT,OAAO,CAAC,KAAK,CAAC,yBAAyB,EAAE,CAAC,CAAC,CAAC;oBAChD,CAAC,CAAC,CAAC;gBACX,CAAC;YACL,CAAC,CAAC;YAEF,MAAM,eAAe,GAAG,OAAO,CAAC,gBAAgB,CAC5C,KAAK,EACL,cAAc,CACjB,CAAC;YAEF,KAAK,OAAO,CAAC,aAAa,EAAE,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;gBACtC,IAAI,GAAG;oBAAE,cAAc,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;YACrC,CAAC,CAAC,CAAC;YAEH,OAAO,GAAG,EAAE;gBACR,eAAe,CAAC,MAAM,EAAE,CAAC;YAC7B,CAAC,CAAC;QACN,CAAC,EAAE,CAAC,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,YAAY,CAAC,CAAC,CAAC;IAC/D,CAAC;IAED,OAAO;QACH,QAAQ;QACR,UAAU;QACV,iBAAiB;QACjB,UAAU;QACV,eAAe;KAClB,CAAC;AACN,CAAC;AAsDD,cAAc,YAAY,CAAC"}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "jazz-react-native",
3
+ "version": "0.8.3",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "react-native": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "react-native": "./dist/index.js",
12
+ "default": "./dist/index.js",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "license": "MIT",
17
+ "dependencies": {
18
+ "@scure/bip39": "^1.3.0",
19
+ "typescript": "^5.3.3",
20
+ "cojson": "0.8.3",
21
+ "cojson-transport-ws": "0.8.3",
22
+ "jazz-tools": "0.8.3"
23
+ },
24
+ "peerDependencies": {
25
+ "@react-native-community/netinfo": "^11.4.1",
26
+ "expo-linking": "~6.3.1",
27
+ "react-native": "0.74.2",
28
+ "react-native-mmkv": "3.0.1"
29
+ },
30
+ "devDependencies": {
31
+ "@react-native-community/netinfo": "^11.3.1",
32
+ "expo-linking": "~6.3.1",
33
+ "react-native": "~0.74.5",
34
+ "react-native-mmkv": "3.0.1"
35
+ },
36
+ "lint-staged": {
37
+ "*.{ts,tsx}": "eslint --fix",
38
+ "*.{js,jsx,mdx,json}": "prettier --write"
39
+ },
40
+ "gitHead": "33c27053293b4801b968c61d5c4c989f93a67d13",
41
+ "scripts": {
42
+ "dev": "tsc --watch --sourceMap --outDir dist",
43
+ "lint": "eslint . --ext ts,tsx",
44
+ "format": "prettier --write './src/**/*.{ts,tsx}'",
45
+ "build": "npm run lint && rm -rf ./dist && tsc --sourceMap --outDir dist"
46
+ }
47
+ }
@@ -0,0 +1,219 @@
1
+ import { AgentSecret } from "cojson";
2
+ import { Account, AuthMethod, AuthResult, ID } from "jazz-tools";
3
+ import NativeStorageContext, { NativeStorage } from "../native-storage.js";
4
+
5
+ type StorageData = {
6
+ accountID: ID<Account>;
7
+ accountSecret: AgentSecret;
8
+ };
9
+
10
+ /** @category Auth Providers */
11
+ // eslint-disable-next-line @typescript-eslint/no-namespace
12
+ export namespace RNDemoAuth {
13
+ export interface Driver {
14
+ onReady: (next: {
15
+ signUp: (username: string) => Promise<void>;
16
+ getExistingUsers: () => Promise<string[]>;
17
+ logInAs: (existingUser: string) => Promise<void>;
18
+ }) => void;
19
+ onSignedIn: (next: { logOut: () => void }) => void;
20
+ onError: (error: string | Error) => void;
21
+ }
22
+ }
23
+
24
+ const localStorageKey = "demo-auth-logged-in-secret";
25
+
26
+ export class RNDemoAuth implements AuthMethod {
27
+ private constructor(
28
+ private driver: RNDemoAuth.Driver,
29
+ private storage: NativeStorage,
30
+ ) {}
31
+
32
+ public static async init(
33
+ driver: RNDemoAuth.Driver,
34
+ seedAccounts?: {
35
+ [name: string]: {
36
+ accountID: ID<Account>;
37
+ accountSecret: AgentSecret;
38
+ };
39
+ },
40
+ ) {
41
+ const storage = NativeStorageContext.getInstance().getStorage();
42
+ for (const [name, credentials] of Object.entries(seedAccounts || {})) {
43
+ const storageData = JSON.stringify(
44
+ credentials satisfies StorageData,
45
+ );
46
+ if (
47
+ !(
48
+ (await storage.get("demo-auth-existing-users"))?.split(
49
+ ",",
50
+ ) as string[] | undefined
51
+ )?.includes(name)
52
+ ) {
53
+ const existingUsers = await storage.get(
54
+ "demo-auth-existing-users",
55
+ );
56
+ if (existingUsers) {
57
+ await storage.set(
58
+ "demo-auth-existing-users",
59
+ existingUsers + "," + name,
60
+ );
61
+ } else {
62
+ await storage.set("demo-auth-existing-users", name);
63
+ }
64
+ }
65
+ await storage.set("demo-auth-existing-users-" + name, storageData);
66
+ }
67
+ return new RNDemoAuth(driver, storage);
68
+ }
69
+
70
+ async start() {
71
+ try {
72
+ if (await this.storage.get(localStorageKey)) {
73
+ const localStorageData = JSON.parse(
74
+ (await this.storage.get(localStorageKey)) ?? "{}",
75
+ ) as StorageData;
76
+
77
+ const accountID = localStorageData.accountID as ID<Account>;
78
+ const secret = localStorageData.accountSecret;
79
+
80
+ return {
81
+ type: "existing",
82
+ credentials: { accountID, secret },
83
+ onSuccess: () => {
84
+ this.driver.onSignedIn({ logOut });
85
+ },
86
+ onError: (error: string | Error) => {
87
+ this.driver.onError(error);
88
+ },
89
+ logOut: async () => {
90
+ void (await this.storage.delete(localStorageKey));
91
+ },
92
+ } satisfies AuthResult;
93
+ } else {
94
+ return new Promise<AuthResult>((resolve) => {
95
+ this.driver.onReady({
96
+ // @ts-expect-error asd
97
+ signUp: (username: string) => {
98
+ resolve({
99
+ type: "new",
100
+ creationProps: { name: username },
101
+ saveCredentials: async (credentials: {
102
+ accountID: ID<Account>;
103
+ secret: AgentSecret;
104
+ }) => {
105
+ const storageData = JSON.stringify({
106
+ accountID: credentials.accountID,
107
+ accountSecret: credentials.secret,
108
+ } satisfies StorageData);
109
+
110
+ // Retrieve the list of existing users
111
+ const existingUsers =
112
+ await this.storage.get(
113
+ "demo-auth-existing-users",
114
+ );
115
+ const existingUsernames = existingUsers
116
+ ? existingUsers.split(",")
117
+ : [];
118
+
119
+ // Determine if the username already exists and generate a unique username
120
+ let uniqueUsername = username;
121
+ let counter = 1;
122
+ while (
123
+ existingUsernames.includes(
124
+ uniqueUsername,
125
+ )
126
+ ) {
127
+ counter++;
128
+ uniqueUsername = `${username}-${counter}`;
129
+ }
130
+
131
+ // Save credentials using the unique username
132
+ await this.storage.set(
133
+ localStorageKey,
134
+ storageData,
135
+ );
136
+ await this.storage.set(
137
+ "demo-auth-existing-users-" +
138
+ uniqueUsername,
139
+ storageData,
140
+ );
141
+
142
+ // Update the list of existing users
143
+ const updatedUsers = existingUsers
144
+ ? `${existingUsers},${uniqueUsername}`
145
+ : uniqueUsername;
146
+ await this.storage.set(
147
+ "demo-auth-existing-users",
148
+ updatedUsers,
149
+ );
150
+ },
151
+ onSuccess: () => {
152
+ this.driver.onSignedIn({ logOut });
153
+ },
154
+ onError: (error: string | Error) => {
155
+ // @ts-expect-error asd
156
+ console.error("onError", error.cause);
157
+ this.driver.onError(error);
158
+ },
159
+ logOut: async () => {
160
+ void (await this.storage.delete(
161
+ localStorageKey,
162
+ ));
163
+ },
164
+ });
165
+ },
166
+ getExistingUsers: async () => {
167
+ return (
168
+ (
169
+ await this.storage.get(
170
+ "demo-auth-existing-users",
171
+ )
172
+ )?.split(",") ?? []
173
+ );
174
+ },
175
+ logInAs: async (existingUser) => {
176
+ const storageData = JSON.parse(
177
+ (await this.storage.get(
178
+ "demo-auth-existing-users-" + existingUser,
179
+ )) ?? "{}",
180
+ ) as StorageData;
181
+
182
+ await this.storage.set(
183
+ localStorageKey,
184
+ JSON.stringify(storageData),
185
+ );
186
+
187
+ resolve({
188
+ type: "existing",
189
+ credentials: {
190
+ accountID: storageData.accountID,
191
+ secret: storageData.accountSecret,
192
+ },
193
+ onSuccess: () => {
194
+ this.driver.onSignedIn({ logOut });
195
+ },
196
+ onError: (error: string | Error) => {
197
+ this.driver.onError(error);
198
+ },
199
+ logOut: async () => {
200
+ void (await this.storage.delete(
201
+ localStorageKey,
202
+ ));
203
+ },
204
+ });
205
+ },
206
+ });
207
+ });
208
+ }
209
+ } catch (error) {
210
+ console.error("error", error);
211
+ throw error;
212
+ }
213
+ }
214
+ }
215
+
216
+ async function logOut() {
217
+ const storage = NativeStorageContext.getInstance().getStorage();
218
+ void (await storage.delete(localStorageKey));
219
+ }