Skip to content

Commit

Permalink
fix fake-expo-secure-store etc (#4220)
Browse files Browse the repository at this point in the history
* init fix

* remove fake-expo-secure-store
  • Loading branch information
peterpme authored Jun 23, 2023
1 parent 973b611 commit 9b1dd91
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 99 deletions.
38 changes: 8 additions & 30 deletions packages/common/src/browser/AsyncStorage.native.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
import RNAsyncStorage from "@react-native-async-storage/async-storage";

interface SecureDB {
get: (key: string) => Promise<any>;
set: (key: string, value: any) => Promise<void>;
remove: (key: string) => Promise<void>;
reset: () => Promise<void>;
}

export class SecureStorage implements SecureDB {
async get(key: string): Promise<string | null> {
console.log("z1:native.ts:getItem");
// @ts-ignore
return RNAsyncStorage.getItem(key);
}

async set(key: string, value: string): Promise<void> {
console.log("z1:native.ts:setItem");
// @ts-ignore
return RNAsyncStorage.setItem(key, value);
}

async remove(key: string): Promise<void> {
// @ts-ignore
return RNAsyncStorage.removeItem(key);
}

async reset(): Promise<void> {
// @ts-ignore
return RNAsyncStorage.clear();
}
}
const AsyncStorage = {
getItem: (key: string) => RNAsyncStorage.getItem(key),
setItem: (key: string, value: any) => RNAsyncStorage.setItem(key, value),
removeItem: (key: string) => RNAsyncStorage.removeItem(key),
clear: () => RNAsyncStorage.clear(),
};

export default AsyncStorage;
38 changes: 14 additions & 24 deletions packages/common/src/browser/AsyncStorage.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
// NOTE this is just a stub for the native AsyncStorage wrapper
interface SecureDB {
get: (key: string) => Promise<any>;
set: (key: string, value: any) => Promise<void>;
remove: (key: string) => Promise<void>;
reset: () => Promise<void>;
}
// interface S {
// getItem: (key: string) => Promise<any>;
// setItem: (key: string, value: any) => Promise<void>;
// removeItem: (key: string) => Promise<void>;
// clear: () => Promise<void>;
// }

export class SecureStorage implements SecureDB {
async get(key: string): Promise<string | null> {
console.log("z1:web.ts:getItem");
return localStorage.getItem(key);
}
const AsyncStorage = {
getItem: (key: string) => Promise.resolve(localStorage.getItem(key)),
setItem: (key: string, value: any) =>
Promise.resolve(localStorage.setItem(key, value)),
removeItem: (key: string) => Promise.resolve(localStorage.removeItem(key)),
clear: () => Promise.resolve(localStorage.clear()),
};

async set(key: string, value: string): Promise<void> {
console.log("z1:web.ts:setItem");
return localStorage.setItem(key, value);
}

async remove(key: string): Promise<void> {
return localStorage.removeItem(key);
}

async reset(): Promise<void> {
return localStorage.clear();
}
}
export default AsyncStorage;
16 changes: 7 additions & 9 deletions packages/common/src/browser/mobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import type { RpcRequestMsg, RpcResponseData } from "../types";
import { generateUniqueId, IS_MOBILE, isServiceWorker } from "../utils";
import { useStore } from "../zustand-store";

import { SecureStorage } from "./AsyncStorage";
import AsyncStorage from "./AsyncStorage";
import { BrowserRuntimeCommon } from "./common";

console.log("bbb:AsyncStorage", AsyncStorage);

const logger = getLogger("common/mobile");

type Result = any;
Expand Down Expand Up @@ -294,8 +296,7 @@ export function startMobileIfNeeded() {

const handleRemoveLocalStorage = async (key: string) => {
try {
// @ts-expect-error
await SecureStorage.remove(key);
await AsyncStorage.removeItem(key);
return ["success", undefined];
} catch (error) {
return ["error", error];
Expand All @@ -304,8 +305,7 @@ export function startMobileIfNeeded() {

const handleGetLocalStorage = async (key: string) => {
try {
// @ts-expect-error
const value = await SecureStorage.get(key);
const value = await AsyncStorage.getItem(key);
const str = String(value);
const json = JSON.parse(str);
return [json, undefined];
Expand All @@ -315,15 +315,13 @@ export function startMobileIfNeeded() {
};

const handleSetLocalStorage = async (key: string, value: any) => {
// @ts-expect-error
await SecureStorage.set(key, JSON.stringify(value));
await AsyncStorage.setItem(key, JSON.stringify(value));
return ["success", undefined];
};

const handleClearLocalStorage = async () => {
try {
// @ts-expect-error
await SecureStorage.reset();
await AsyncStorage.clear();
return ["success", undefined];
} catch (error) {
return ["error", error];
Expand Down
24 changes: 0 additions & 24 deletions packages/common/src/fake-expo-secure-store.ts

This file was deleted.

12 changes: 6 additions & 6 deletions packages/common/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"outDir": "dist/esm/",
"rootDir": "./src",
"baseUrl": "src",
"paths": {
// replace 'expo-secure-store' with ''./src/fake-expo-secure-store'
// when building the esmodules output, as react-native code is
// incompatible with regualar react code.
"@react-native-async-storage/async-storage": ["fake-expo-secure-store"]
},
// "paths": {
// // replace 'expo-secure-store' with ''./src/fake-expo-secure-store'
// // when building the esmodules output, as react-native code is
// // incompatible with regualar react code.
// "@react-native-async-storage/async-storage": ["fake-expo-secure-store"]
// },
"types": ["chrome", "firefox-webext-browser", "jest"]
}
}
12 changes: 6 additions & 6 deletions packages/common/tsconfig.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"outDir": "dist/esm/",
"rootDir": "./src",
"baseUrl": "src",
"paths": {
// replace 'expo-secure-store' with ''./src/fake-expo-secure-store'
// when building the esmodules output, as react-native code is
// incompatible with regualar react code.
"@react-native-async-storage/async-storage": ["fake-expo-secure-store"]
},
// "paths": {
// // replace 'expo-secure-store' with ''./src/fake-expo-secure-store'
// // when building the esmodules output, as react-native code is
// // incompatible with regualar react code.
// "@react-native-async-storage/async-storage": ["fake-expo-secure-store"]
// },
"types": ["chrome", "firefox-webext-browser", "jest"]
}
}

1 comment on commit 9b1dd91

@vercel
Copy link

@vercel vercel bot commented on 9b1dd91 Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.