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
8
Versions
764
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.37.0 to 0.37.1-beta-20250110141708

2

dist/cjs/client/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");function h(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("eventemitter3"),require("jose"),require("js-cookie"),require("../Token.js"),require("../utils/allSettled.js");var c=/*#__PURE__*/h(e);let l="privy:caid";exports.PrivyInternal=class{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!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:h={onRequest:this._beforeRequest.bind(this)}}){let c=new Request(`${this.baseUrl}${i.getPathWithParams(e.path,s)}${o.toSearchParams(r)}`,{method:e.method,body:JSON.stringify(t),headers:n}),l=await h.onRequest(c),d=await this._fetch(c,l),u=await d.json();if(d.status>299)throw new a.PrivyApiError(u);return u}async _beforeRequestWithoutInitialize(e){let 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");let 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{let e=await this._storage.get(l);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(l,this._analyticsId)}catch(e){console.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(l)}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(){let e=await this.session.getRefreshToken()??void 0,t=e??"key",s=this._cache.get(t);if(s)return await s;let i=this._refreshSession(e);this._cache.set(t,i);try{return await i}finally{this._cache.delete(t)}}async _refreshSession(e){let 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{let 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(){let[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{let e=`privy:__storage__test-${t.v4()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return console.error(e),!1}}constructor(e){this._sdkVersion="js-sdk-core:0.37.0",this._cache=new Map,this._storage=e.storage,this._analyticsId=null,this._getOrGenerateClientAnalyticsId(),this.baseUrl=e.baseUrl??"https://auth.privy.io",this.appId=e.appId,this.appClientId=e.appClientId,this._sdkVersion=e.sdkVersion??this._sdkVersion,this.callbacks=e.callbacks,this.nativeAppIdentifier=e.nativeAppIdentifier,this.session=new r.Session({storage:this._storage,isUsingServerCookies:!1,appId:e.appId}),this._fetch=c.default(fetch,{retries:3,retryDelay:500}),this.session.on("error_storing_tokens",(e=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:e})}))}};
"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");function h(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("eventemitter3"),require("jose"),require("js-cookie"),require("../Token.js"),require("../utils/allSettled.js");var c=/*#__PURE__*/h(e);let l="privy:caid";exports.PrivyInternal=class{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!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:h={onRequest:this._beforeRequest.bind(this)}}){let c=new Request(`${this.baseUrl}${i.getPathWithParams(e.path,s)}${o.toSearchParams(r)}`,{method:e.method,body:JSON.stringify(t),headers:n}),l=await h.onRequest(c),d=await this._fetch(c,l),u=await d.json();if(d.status>299)throw new a.PrivyApiError(u);return u}async _beforeRequestWithoutInitialize(e){let 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");let 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{let e=await this._storage.get(l);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(l,this._analyticsId)}catch(e){console.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(l)}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(){let e=await this.session.getRefreshToken()??void 0,t=e??"key",s=this._cache.get(t);if(s)return await s;let i=this._refreshSession(e);this._cache.set(t,i);try{return await i}finally{this._cache.delete(t)}}async _refreshSession(e){let 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{let 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(){let[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{let e=`privy:__storage__test-${t.v4()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return console.error(e),!1}}constructor(e){this._sdkVersion="js-sdk-core:0.37.1-beta-20250110141708",this._cache=new Map,this._storage=e.storage,this._analyticsId=null,this._getOrGenerateClientAnalyticsId(),this.baseUrl=e.baseUrl??"https://auth.privy.io",this.appId=e.appId,this.appClientId=e.appClientId,this._sdkVersion=e.sdkVersion??this._sdkVersion,this.callbacks=e.callbacks,this.nativeAppIdentifier=e.nativeAppIdentifier,this.session=new r.Session({storage:this._storage,isUsingServerCookies:!1,appId:e.appId}),this._fetch=c.default(fetch,{retries:3,retryDelay:500}),this.session.on("error_storing_tokens",(e=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:e})}))}};
{
"name": "@privy-io/js-sdk-core",
"version": "0.37.0",
"version": "0.37.1-beta-20250110141708",
"description": "Vanilla JS client for the Privy Auth API",

@@ -72,3 +72,3 @@ "keywords": [

"@privy-io/api-base": "^1.4.1",
"@privy-io/public-api": "2.15.9",
"@privy-io/public-api": "2.15.10-beta-20250110141708",
"eventemitter3": "^5.0.1",

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

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