Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@privy-io/js-sdk-core

Package Overview
Dependencies
Maintainers
0
Versions
725
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@privy-io/js-sdk-core - npm Package Compare versions

Comparing version 0.35.0-beta-20241122004429 to 0.35.0-beta-20241122173757

2

dist/cjs/PrivyInternal.js

@@ -1,1 +0,1 @@

"use strict";var e=require("fetch-retry"),t=require("uuid"),s=require("@privy-io/api-base"),i=require("@privy-io/public-api"),a=require("./Error.js"),r=require("./Session.js"),n=require("./toAbortSignalTimeout.js"),o=require("./utils/toSearchParams.js");require("eventemitter3"),require("jose"),require("js-cookie"),require("./Token.js"),require("./utils/allSettled.js");const c="privy:caid";exports.PrivyInternal=class{baseUrl;_config;appId;appClientId;_storage;_analyticsId;_sdkVersion="js-sdk-core:0.35.0-beta-20241122004429";_fetch;nativeAppIdentifier;callbacks;_cache=new Map;session;constructor(t){this._storage=t.storage,this._analyticsId=null,this._getOrGenerateClientAnalyticsId(),this.baseUrl=t.baseUrl??"https://auth.privy.io",this.appId=t.appId,this.appClientId=t.appClientId,this._sdkVersion=t.sdkVersion??this._sdkVersion,this.callbacks=t.callbacks,"undefined"==typeof document&&(this.nativeAppIdentifier=t.nativeAppIdentifier),this.session=new r.Session({storage:this._storage,isUsingServerCookies:!1,appId:t.appId}),this._fetch=e(fetch,{retries:3,retryDelay:500}),this.session.on("error_storing_tokens",(e=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:e})}))}setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return Boolean(this._config)}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new a.PrivyClientError({code:"storage_error",error:"Unable to access storage"});this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this.createAnalyticsEvent("sdk_initialize",{})}}async fetch(e,{body:t,params:s,query:r,headers:n,options:c={onRequest:this._beforeRequest.bind(this)}}){const h=`${this.baseUrl}${i.getPathWithParams(e.path,s)}${o.toSearchParams(r)}`,l=new Request(h,{method:e.method,body:JSON.stringify(t),headers:n}),d=await c.onRequest(l),u=await this._fetch(l,d),p=await u.json();if(u.status>299)throw new a.PrivyApiError(p);return p}async _beforeRequestWithoutInitialize(e){const t=await this.session.getToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");const i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:n.default(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(i.AppConfig,{params:{app_id:this.appId},options:{onRequest:this._beforeRequestWithoutInitialize.bind(this)}})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{const e=await this._storage.get(c);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){console.error("Unable to load clientId",e)}try{this._analyticsId=t.v4()}catch(e){console.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(c,this._analyticsId)}catch(e){console.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(c)}catch(e){console.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(i.AnalyticsEvent,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}})}catch(e){}}async refreshSession(){const e=await this.session.getRefreshToken()??void 0,t=e??"key",s=this._cache.get(t);if(s)return await s;const i=this._refreshSession(e);this._cache.set(t,i);try{return await i}catch(e){throw e}finally{this._cache.delete(t)}}async _refreshSession(e){const t=await this.session.getToken();if(!this.session.hasRefreshCredentials(t,e??null))throw await this._initialize(),new a.PrivyApiError({code:s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{const t=await this.fetch(i.RefreshSession,{body:{refresh_token:e},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}}),s=t.session_update_action;return this.callbacks?.setUser?.(t.user),"set"===s&&await this.session.updateWithTokensResponse(t),"clear"===s&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),"ignore"===s&&t.token&&(await this.session.storeToken(t.token),t.identity_token&&await this.session.storeIdentityToken(t.identity_token)),t}catch(e){throw e instanceof a.PrivyApiError&&e.code===s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),e}}async getAccessToken(){const[e,t]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);return!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)?(await this.refreshSession(),await this.session.getToken()):e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{const e="privy:__storage__test",t="blobby";await this._storage.put(e,t);const s=await this._storage.get(e);return await this._storage.del(e),s===t}catch(e){return console.error(e),!1}}};
"use strict";var e=require("fetch-retry"),t=require("uuid"),s=require("@privy-io/api-base"),i=require("@privy-io/public-api"),a=require("./Error.js"),r=require("./Session.js"),n=require("./toAbortSignalTimeout.js"),o=require("./utils/toSearchParams.js");require("eventemitter3"),require("jose"),require("js-cookie"),require("./Token.js"),require("./utils/allSettled.js");const c="privy:caid";exports.PrivyInternal=class{baseUrl;_config;appId;appClientId;_storage;_analyticsId;_sdkVersion="js-sdk-core:0.35.0-beta-20241122173757";_fetch;nativeAppIdentifier;callbacks;_cache=new Map;session;constructor(t){this._storage=t.storage,this._analyticsId=null,this._getOrGenerateClientAnalyticsId(),this.baseUrl=t.baseUrl??"https://auth.privy.io",this.appId=t.appId,this.appClientId=t.appClientId,this._sdkVersion=t.sdkVersion??this._sdkVersion,this.callbacks=t.callbacks,"undefined"==typeof document&&(this.nativeAppIdentifier=t.nativeAppIdentifier),this.session=new r.Session({storage:this._storage,isUsingServerCookies:!1,appId:t.appId}),this._fetch=e(fetch,{retries:3,retryDelay:500}),this.session.on("error_storing_tokens",(e=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:e})}))}setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return Boolean(this._config)}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new a.PrivyClientError({code:"storage_error",error:"Unable to access storage"});this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this.createAnalyticsEvent("sdk_initialize",{})}}async fetch(e,{body:t,params:s,query:r,headers:n,options:c={onRequest:this._beforeRequest.bind(this)}}){const h=`${this.baseUrl}${i.getPathWithParams(e.path,s)}${o.toSearchParams(r)}`,l=new Request(h,{method:e.method,body:JSON.stringify(t),headers:n}),d=await c.onRequest(l),u=await this._fetch(l,d),p=await u.json();if(u.status>299)throw new a.PrivyApiError(p);return p}async _beforeRequestWithoutInitialize(e){const t=await this.session.getToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");const i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:n.default(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(i.AppConfig,{params:{app_id:this.appId},options:{onRequest:this._beforeRequestWithoutInitialize.bind(this)}})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{const e=await this._storage.get(c);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){console.error("Unable to load clientId",e)}try{this._analyticsId=t.v4()}catch(e){console.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(c,this._analyticsId)}catch(e){console.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(c)}catch(e){console.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(i.AnalyticsEvent,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}})}catch(e){}}async refreshSession(){const e=await this.session.getRefreshToken()??void 0,t=e??"key",s=this._cache.get(t);if(s)return await s;const i=this._refreshSession(e);this._cache.set(t,i);try{return await i}catch(e){throw e}finally{this._cache.delete(t)}}async _refreshSession(e){const t=await this.session.getToken();if(!this.session.hasRefreshCredentials(t,e??null))throw await this._initialize(),new a.PrivyApiError({code:s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{const t=await this.fetch(i.RefreshSession,{body:{refresh_token:e},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}}),s=t.session_update_action;return this.callbacks?.setUser?.(t.user),"set"===s&&await this.session.updateWithTokensResponse(t),"clear"===s&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),"ignore"===s&&t.token&&(await this.session.storeToken(t.token),t.identity_token&&await this.session.storeIdentityToken(t.identity_token)),t}catch(e){throw e instanceof a.PrivyApiError&&e.code===s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),e}}async getAccessToken(){const[e,t]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);return!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)?(await this.refreshSession(),await this.session.getToken()):e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{const e="privy:__storage__test",t="blobby";await this._storage.put(e,t);const s=await this._storage.get(e);return await this._storage.del(e),s===t}catch(e){return console.error(e),!1}}};

@@ -68,2 +68,6 @@ import { type PrivySmartWalletConfigurationResponse } from '@privy-io/public-api';

funding_config?: {
options: {
method: string;
provider: string;
}[];
default_recommended_currency: {

@@ -70,0 +74,0 @@ chain: string;

{
"name": "@privy-io/js-sdk-core",
"version": "0.35.0-beta-20241122004429",
"version": "0.35.0-beta-20241122173757",
"description": "Vanilla JS client for the Privy Auth API",

@@ -70,4 +70,4 @@ "keywords": [

"@ethersproject/units": "^5.7.0",
"@privy-io/api-base": "1.4.1-beta-20241122004429",
"@privy-io/public-api": "2.15.2-beta-20241122004429",
"@privy-io/api-base": "1.4.1-beta-20241122173757",
"@privy-io/public-api": "2.15.2-beta-20241122173757",
"eventemitter3": "^5.0.1",

@@ -74,0 +74,0 @@ "fetch-retry": "^5.0.6",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc