import { ContainerStorage , StorageDriver , JSONValue , User , ExtraSessionInfoOptions , } from "./types" ; import { encodeUser , decodeUser , _encodeExtraSessionInfoOptions , _decodeExtraSessionInfoOptions , } from "./encoding" ; function scopedKey ( key ) { return `skygear2_${ key }` ; } function keyAccessToken ( name ) { return `${ name }_accessToken` ; } function keyRefreshToken ( name ) { return `${ name }_refreshToken` ; } function keySessionID ( name ) { return `${ name }_sessionID` ; } function keyUser ( name ) { return `${ name }_user` ; } function keyOAuthRedirectAction ( name ) { return `${ name }_oauthRedirectAction` ; } function keyOAuthCodeVerifier ( name ) { return `${ name }_oauthAuthorizationCode` ; } function keyExtraSessionInfoOptions ( name ) { return `${ name }_extra_session_info_options` ; } function keyOIDCCodeVerifier ( name ) { return `${ name }_oidcCodeVerifier` ; } function keyAnonymousKeyID ( name ) { return `${ name }_anonymousKeyID` ; } internal export class _GlobalJSONStorage { driver : StorageDriver ; constructor ( driver ) { this . driver = driver ; } async safeDel ( key ) { key = scopedKey ( key ) ; try { await this . driver . del ( key ) ; } catch { } } async safeGet ( key ) { key = scopedKey ( key ) ; try { return this . driver . get ( key ) ; } catch { return null ; } } async safeGetJSON ( key ) { const jsonString = await this . safeGet ( key ) ; if ( jsonString == null ) { return undefined ; } try { return JSON . parse ( jsonString ) ; } catch { return undefined ; } } async safeSet ( key , value ) { key = scopedKey ( key ) ; try { await this . driver . set ( key , value ) ; } catch { } } async safeSetJSON ( key , value ) { try { const jsonString = JSON . stringify ( value ) ; await this . safeSet ( key , jsonString ) ; } catch { } } } public export class GlobalJSONContainerStorage implements ContainerStorage { private storage : _GlobalJSONStorage ; constructor ( driver ) { this . storage = new _GlobalJSONStorage ( driver ) ; } async setUser ( namespace , user ) { const userJSON = encodeUser ( user ) ; await this . storage . safeSetJSON ( keyUser ( namespace ) , userJSON ) ; } async setAccessToken ( namespace , accessToken ) { await this . storage . safeSet ( keyAccessToken ( namespace ) , accessToken ) ; } async setRefreshToken ( namespace , refreshToken ) { await this . storage . safeSet ( keyRefreshToken ( namespace ) , refreshToken ) ; } async setSessionID ( namespace , sessionID ) { await this . storage . safeSet ( keySessionID ( namespace ) , sessionID ) ; } async setOAuthRedirectAction ( namespace , oauthRedirectAction ) { await this . storage . safeSet ( keyOAuthRedirectAction ( namespace ) , oauthRedirectAction ) ; } async setOAuthCodeVerifier ( namespace , authorizationCode ) { await this . storage . safeSet ( keyOAuthCodeVerifier ( namespace ) , authorizationCode ) ; } async setExtraSessionInfoOptions ( namespace , options ) { await this . storage . safeSetJSON ( keyExtraSessionInfoOptions ( namespace ) , _encodeExtraSessionInfoOptions ( options ) ) ; } async setOIDCCodeVerifier ( namespace , code ) { await this . storage . safeSet ( keyOIDCCodeVerifier ( namespace ) , code ) ; } async setAnonymousKeyID ( namespace , kid ) { await this . storage . safeSet ( keyAnonymousKeyID ( namespace ) , kid ) ; } async getUser ( namespace ) { const userJSON = await this . storage . safeGetJSON ( keyUser ( namespace ) ) ; if ( userJSON ) { return decodeUser ( userJSON ) ; } return null ; } async getAccessToken ( namespace ) { return this . storage . safeGet ( keyAccessToken ( namespace ) ) ; } async getRefreshToken ( namespace ) { return this . storage . safeGet ( keyRefreshToken ( namespace ) ) ; } async getSessionID ( namespace ) { return this . storage . safeGet ( keySessionID ( namespace ) ) ; } async getOAuthRedirectAction ( namespace ) { return this . storage . safeGet ( keyOAuthRedirectAction ( namespace ) ) ; } async getOAuthCodeVerifier ( namespace ) { return this . storage . safeGet ( keyOAuthCodeVerifier ( namespace ) ) ; } async getExtraSessionInfoOptions ( namespace ) { const optionJSON = await this . storage . safeGetJSON ( keyExtraSessionInfoOptions ( namespace ) ) ; if ( optionJSON ) { return _decodeExtraSessionInfoOptions ( optionJSON ) ; } return null ; } async getOIDCCodeVerifier ( namespace ) { return this . storage . safeGet ( keyOIDCCodeVerifier ( namespace ) ) ; } async getAnonymousKeyID ( namespace ) { return this . storage . safeGet ( keyAnonymousKeyID ( namespace ) ) ; } async delUser ( namespace ) { await this . storage . safeDel ( keyUser ( namespace ) ) ; } async delAccessToken ( namespace ) { await this . storage . safeDel ( keyAccessToken ( namespace ) ) ; } async delRefreshToken ( namespace ) { await this . storage . safeDel ( keyRefreshToken ( namespace ) ) ; } async delSessionID ( namespace ) { await this . storage . safeDel ( keySessionID ( namespace ) ) ; } async delOAuthRedirectAction ( namespace ) { await this . storage . safeDel ( keyOAuthRedirectAction ( namespace ) ) ; } async delOAuthCodeVerifier ( namespace ) { await this . storage . safeDel ( keyOAuthCodeVerifier ( namespace ) ) ; } async delOIDCCodeVerifier ( namespace ) { await this . storage . safeDel ( keyOIDCCodeVerifier ( namespace ) ) ; } async delAnonymousKeyID ( namespace ) { await this . storage . safeDel ( keyAnonymousKeyID ( namespace ) ) ; } }