Socket
Socket
Sign inDemoInstall

@toruslabs/base-controllers

Package Overview
Dependencies
Maintainers
4
Versions
146
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@toruslabs/base-controllers - npm Package Compare versions

Comparing version 5.0.3 to 5.1.0

12

dist/types/Embed/IEmbedController.d.ts

@@ -18,2 +18,6 @@ import type { JRPCEngineEndCallback, JRPCEngineNextCallback, JRPCRequest, JRPCResponse } from "@toruslabs/openlogin-jrpc";

export type CONFIRMATION_STRATEGY_TYPE = (typeof CONFIRMATION_STRATEGY)[keyof typeof CONFIRMATION_STRATEGY];
export interface WindowBlockAlertParams {
windowId: string;
finalUrl: string;
}
export interface BaseEmbedControllerState extends BaseState {

@@ -31,9 +35,3 @@ isIFrameFullScreen: boolean;

showCheckout?: boolean;
windowBlockAlert?: {
show: boolean;
params: {
windowId: string;
finalUrl: string;
};
};
windowBlockAlert?: WindowBlockAlertParams[];
web3AuthClientId: string;

@@ -40,0 +38,0 @@ web3AuthNetwork: OPENLOGIN_NETWORK_TYPE;

@@ -41,3 +41,2 @@ export declare const FEATURES_PROVIDER_CHANGE_WINDOW: {

IFRAME_STATUS: string;
CREATE_WINDOW: string;
CLOSE_WINDOW: string;

@@ -54,3 +53,2 @@ USER_LOGGED_IN: string;

IFRAME_STATUS: string;
OPENED_WINDOW: string;
CLOSED_WINDOW: string;

@@ -57,0 +55,0 @@ WINDOW_BLOCKED: string;

@@ -6,4 +6,4 @@ import { BroadcastChannel } from "@toruslabs/broadcast-channel";

private channel;
constructor(channelPrefix: string);
constructor(channelPrefix: string, instanceId: string);
getMessageFromChannel<T>(): Promise<T>;
}

@@ -16,2 +16,10 @@ import { JRPCEngine, SafeEventEmitter } from "@toruslabs/openlogin-jrpc";

communicationWindowManager: SafeEventEmitter;
/**
* Unique instance id to which the popup window will communicate to. (usually sessionId)
*/
instanceId: string;
/**
* Show alert if window is blocked and resolve if window is open successfully
*/
handleWindowBlockAlert(): Promise<null>;
}

@@ -25,3 +33,3 @@ export interface StreamWindowState extends BaseState {

* If window has already been opened, then it will be reused.
* This parameter represents a query parameter in the opened window.
* This parameter represents a query parameter in the opened window and is used to uniquely identify the window.
* We communicate with the popup window using this query parameter in broadcastchannel or via post message.

@@ -49,7 +57,2 @@ * This channel will be strictly used for communication about the window state.

/**
* Storage key for the opened window (key for localStorage)
* Must be stored in hash parameters
*/
dappStorageKey: string;
/**
* Specifies the number of milliseconds before the popup window is closed

@@ -56,0 +59,0 @@ * after receiving the popup_closed event.

@@ -1,3 +0,2 @@

import { BroadcastChannel } from "@toruslabs/broadcast-channel";
import { PopupData, PopupHandlerConfig, PopupHandlerState, SuccessExtraFn } from "./interfaces";
import { PopupHandlerConfig, PopupHandlerState, SuccessExtraFn } from "./interfaces";
import PopupHandler from "./PopupHandler";

@@ -8,7 +7,7 @@ /**

declare class PopupWithBcHandler<ReceiveData, SendData = unknown> extends PopupHandler {
bc: BroadcastChannel<PopupData<ReceiveData>>;
constructor({ config, state, instanceId, }: {
channelPrefix: string;
constructor({ config, state, channelPrefix, }: {
config: Partial<PopupHandlerConfig>;
state: Partial<PopupHandlerState> & Pick<PopupHandlerState, "url">;
instanceId: string;
channelPrefix: string;
});

@@ -15,0 +14,0 @@ /**

export default class RedirectHandler {
private error;
private instanceId?;
private finalQueryParams;
private instanceParameters;
private hashParameters;
constructor();
constructor(instanceId?: string);
handle(): Promise<void>;
}
{
"name": "@toruslabs/base-controllers",
"version": "5.0.3",
"version": "5.1.0",
"homepage": "https://github.com/torusresearch/controllers#readme",

@@ -66,3 +66,3 @@ "license": "ISC",

},
"gitHead": "106d19ac996998cf3199edca74f7afb907a0b6ae"
"gitHead": "8802cce3ff470f27a0ea7d4282d6096f12081f73"
}

@@ -72,3 +72,2 @@ import { createAsyncMiddleware, createScaffoldMiddleware, JRPCMiddleware, mergeMiddleware } from "@toruslabs/openlogin-jrpc";

// Do this in the orchestrator because communicationWindowManager needs to be passed into PopupHandlers
[COMMUNICATION_JRPC_METHODS.OPENED_WINDOW]: handleWindowRpc as JRPCMiddleware<unknown, unknown>,
[COMMUNICATION_JRPC_METHODS.CLOSED_WINDOW]: handleWindowRpc as JRPCMiddleware<unknown, unknown>,

@@ -75,0 +74,0 @@ [COMMUNICATION_JRPC_METHODS.GET_PROVIDER_STATE]: getProviderState as JRPCMiddleware<unknown, unknown>,

@@ -13,10 +13,4 @@ import { JRPCEngineEndCallback, JRPCEngineNextCallback, JRPCRequest, JRPCResponse, SafeEventEmitter } from "@toruslabs/openlogin-jrpc";

const { method, params } = request;
if (method === COMMUNICATION_JRPC_METHODS.OPENED_WINDOW) {
if (method === COMMUNICATION_JRPC_METHODS.CLOSED_WINDOW) {
const { windowId } = params;
// I've been informed that a window has been opened
this.emit(`${windowId}:opened`);
response.result = true;
end();
} else if (method === COMMUNICATION_JRPC_METHODS.CLOSED_WINDOW) {
const { windowId } = params;
// I've been informed that a window has been closed

@@ -23,0 +17,0 @@ this.emit(`${windowId}:closed`);

@@ -23,2 +23,7 @@ import type { JRPCEngineEndCallback, JRPCEngineNextCallback, JRPCRequest, JRPCResponse } from "@toruslabs/openlogin-jrpc";

export interface WindowBlockAlertParams {
windowId: string;
finalUrl: string;
}
export interface BaseEmbedControllerState extends BaseState {

@@ -36,6 +41,3 @@ isIFrameFullScreen: boolean;

showCheckout?: boolean;
windowBlockAlert?: {
show: boolean;
params: { windowId: string; finalUrl: string };
};
windowBlockAlert?: WindowBlockAlertParams[];
web3AuthClientId: string;

@@ -42,0 +44,0 @@ web3AuthNetwork: OPENLOGIN_NETWORK_TYPE;

@@ -36,4 +36,2 @@ export const FEATURES_PROVIDER_CHANGE_WINDOW = { height: 660, width: 375 };

// Tell embed to create the window
CREATE_WINDOW: "create_window",
// Tell embed to close the window

@@ -53,4 +51,2 @@ CLOSE_WINDOW: "close_window",

IFRAME_STATUS: "iframe_status",
// embed has opened the window as requested
OPENED_WINDOW: "opened_window",
// user has closed the window from embed's side

@@ -57,0 +53,0 @@ CLOSED_WINDOW: "closed_window",

@@ -12,6 +12,6 @@ import { BroadcastChannel } from "@toruslabs/broadcast-channel";

constructor(channelPrefix: string) {
constructor(channelPrefix: string, instanceId: string) {
const queryParameters = new URLSearchParams(window.location.search);
const instanceId = queryParameters.get("instanceId");
this.channel = `${channelPrefix}_${instanceId}`;
const windowId = queryParameters.get("windowId");
this.channel = `${channelPrefix}_${instanceId}_${windowId}`;
this.bc = new BroadcastChannel(this.channel, broadcastChannelOptions);

@@ -18,0 +18,0 @@ }

@@ -18,2 +18,12 @@ import { JRPCEngine, SafeEventEmitter } from "@toruslabs/openlogin-jrpc";

communicationWindowManager: SafeEventEmitter;
/**
* Unique instance id to which the popup window will communicate to. (usually sessionId)
*/
instanceId: string;
/**
* Show alert if window is blocked and resolve if window is open successfully
*/
handleWindowBlockAlert(): Promise<null>;
}

@@ -28,3 +38,3 @@

* If window has already been opened, then it will be reused.
* This parameter represents a query parameter in the opened window.
* This parameter represents a query parameter in the opened window and is used to uniquely identify the window.
* We communicate with the popup window using this query parameter in broadcastchannel or via post message.

@@ -53,7 +63,2 @@ * This channel will be strictly used for communication about the window state.

features: string;
/**
* Storage key for the opened window (key for localStorage)
* Must be stored in hash parameters
*/
dappStorageKey: string;

@@ -60,0 +65,0 @@ /**

import BaseController from "../BaseController";
import { FEATURES_DEFAULT_POPUP_WINDOW } from "../enums";
import { IWindow } from "../interfaces";
import { getPopupFeatures } from "../utils";
import { getPopupFeatures, randomId } from "../utils";
import { PopupHandlerConfig, PopupHandlerState } from "./interfaces";

@@ -17,3 +17,3 @@ import StreamWindow from "./StreamWindow";

If you're trying to open a window and it gets blocked (happens if you're in iframe or delay b/w click and opening window),
StreamWindow is invoked and it writes in a channel to display a message to the user
StreamWindow is invoked and it writes in a channel to display a message to the user

@@ -34,3 +34,2 @@ Once user clicks on that modal/dialog, we pre-open the window and pass in the windowId (goes to 3)

this.defaultConfig = {
dappStorageKey: "",
features: getPopupFeatures(FEATURES_DEFAULT_POPUP_WINDOW),

@@ -41,2 +40,4 @@ target: "_blank",

timeout: 30000,
instanceId: "",
handleWindowBlockAlert: null,
};

@@ -56,10 +57,4 @@ this.defaultState = {

// if window is already open
const { target, features, dappStorageKey, communicationEngine, communicationWindowManager } = this.config;
const { target, features, communicationEngine, communicationWindowManager } = this.config;
const { windowId, url } = this.state;
if (dappStorageKey) {
const urlHashParams = new URLSearchParams(url.hash.slice(1));
urlHashParams.append("dappStorageKey", dappStorageKey);
url.hash = urlHashParams.toString();
this.update({ url });
}
// No window has been pre-opened

@@ -69,8 +64,11 @@ if (!windowId) {

let localWindow: IWindow = window.open(url.href, target, features);
let finalWindowId = "";
if (!localWindow) {
// if it's blocked, open StreamWindow
localWindow = new StreamWindow({ config: { communicationEngine, communicationWindowManager }, state: { url } });
localWindow.open();
const streamWindow = new StreamWindow({ config: { communicationEngine, communicationWindowManager }, state: { url } });
streamWindow.open();
finalWindowId = streamWindow.state.windowId;
localWindow = streamWindow;
}
this.update({ window: localWindow });
this.update({ window: localWindow, windowId: finalWindowId || randomId() });
return;

@@ -80,3 +78,3 @@ }

const localWindow = new StreamWindow({ config: { communicationEngine, communicationWindowManager }, state: { url, windowId } });
this.update({ window: localWindow });
this.update({ window: localWindow, windowId: localWindow.state.windowId });
await localWindow.open();

@@ -83,0 +81,0 @@ }

@@ -16,2 +16,3 @@ import { BroadcastChannel } from "@toruslabs/broadcast-channel";

// Always listening channels. Hence, no window id
export default class PopupStoreChannel {

@@ -18,0 +19,0 @@ private handleLogout: PopupStoreChannelHandlers["handleLogout"];

@@ -12,3 +12,3 @@ import { BroadcastChannel } from "@toruslabs/broadcast-channel";

class PopupWithBcHandler<ReceiveData, SendData = unknown> extends PopupHandler {
bc: BroadcastChannel<PopupData<ReceiveData>>;
channelPrefix: string;

@@ -18,10 +18,10 @@ constructor({

state,
instanceId,
channelPrefix,
}: {
config: Partial<PopupHandlerConfig>;
state: Partial<PopupHandlerState> & Pick<PopupHandlerState, "url">;
instanceId: string;
channelPrefix: string;
}) {
super({ config, state });
this.bc = new BroadcastChannel(instanceId, broadcastChannelOptions);
this.channelPrefix = channelPrefix;
}

@@ -35,5 +35,7 @@

handle(successExtraFn?: SuccessExtraFn<ReceiveData>): Promise<ReceiveData> {
const channelName = `${this.channelPrefix}_${this.config.instanceId}_${this.state.windowId}`;
const bc = new BroadcastChannel(channelName, broadcastChannelOptions);
return new Promise((resolve, reject) => {
const closeListener = () => {
this.bc.close();
bc.close();
reject(new UserError("user closed popup"));

@@ -44,4 +46,4 @@ this.removeListener("close", closeListener);

this.bc.addEventListener("message", async (ev) => {
log.info(ev, `receiving data on channel: ${this.bc.name}`);
bc.addEventListener("message", async (ev) => {
log.info(ev, `receiving data on channel: ${bc.name}`);
try {

@@ -58,3 +60,3 @@ const { error, data } = ev;

} finally {
this.bc.close();
bc.close();
this.close();

@@ -65,3 +67,3 @@ }

.then(() => {
log.info(`opened window ${this.bc.name}`);
log.info(`opened window ${bc.name}`);
// Opened window. yay. let the bc events do their job

@@ -86,5 +88,7 @@ return undefined;

handleWithHandshake(payload: SendData, successExtraFn?: SuccessExtraFn<ReceiveData>): Promise<ReceiveData> {
const channelName = `${this.channelPrefix}_${this.config.instanceId}_${this.state.windowId}`;
const bc = new BroadcastChannel(channelName, broadcastChannelOptions);
return new Promise((resolve, reject) => {
const closeListener = () => {
this.bc.close();
bc.close();
reject(new UserError("user closed popup"));

@@ -94,5 +98,5 @@ this.removeListener("close", closeListener);

this.on("close", closeListener);
this.bc.addEventListener("message", async (ev) => {
bc.addEventListener("message", async (ev) => {
try {
log.info(ev, `receiving data on channel: ${this.bc.name}`);
log.info(ev, `receiving data on channel: ${bc.name}`);
const { error, data } = ev;

@@ -107,3 +111,3 @@ if (error) {

// Hack with generic to use the same type for both send and receive
await this.bc.postMessage({
await bc.postMessage({
data: payload,

@@ -115,3 +119,3 @@ } as unknown as PopupData<ReceiveData>);

// Must only close the bc after result is done
this.bc.close();
bc.close();
this.close();

@@ -121,3 +125,3 @@ }

reject(error);
this.bc.close();
bc.close();
this.close();

@@ -128,3 +132,3 @@ }

.then(() => {
log.info(`opened window ${this.bc.name}`);
log.info(`opened window ${bc.name}`);
// Opened window. yay. let the bc events do their job

@@ -131,0 +135,0 @@ return undefined;

@@ -9,4 +9,7 @@ import { BroadcastChannel } from "@toruslabs/broadcast-channel";

export default class RedirectHandler {
private error: string;
// private error: string;
// this is sessionid post login
private instanceId?: string;
private finalQueryParams: Record<string, string> = {};

@@ -16,5 +19,5 @@

private hashParameters: Record<string, string>;
// private hashParameters: Record<string, string>;
constructor() {
constructor(instanceId?: string) {
const { hash } = window.location;

@@ -25,6 +28,7 @@ const queryParameters = new URLSearchParams(window.location.search);

});
const { error, instanceParameters, hashParameters } = handleRedirectParameters(hash, this.finalQueryParams);
this.error = error;
const { instanceParameters } = handleRedirectParameters(hash, this.finalQueryParams);
// this.error = error;
this.instanceParameters = instanceParameters;
this.hashParameters = hashParameters;
// this.hashParameters = hashParameters;
this.instanceId = instanceId;
}

@@ -34,52 +38,56 @@

return new Promise((resolve, reject) => {
const { finalQueryParams, instanceParameters, hashParameters, error } = this;
const { finalQueryParams, instanceParameters } = this;
let bc: BroadcastChannel<PopupData<unknown>>;
try {
if (!finalQueryParams.windowId) {
bc = new BroadcastChannel(`${BROADCAST_CHANNELS.REDIRECT_CHANNEL}_${instanceParameters.instanceId}`, broadcastChannelOptions);
bc.addEventListener("message", async (ev) => {
if (ev.error) {
reject(ev.error);
window.close();
} else {
resolve();
bc.close();
log.info("posted", { finalQueryParams, hashParameters, instanceParameters });
}
});
bc.postMessage({
data: {
instanceParams: instanceParameters,
hashParams: hashParameters,
queryParams: finalQueryParams,
},
error,
});
// used for login case. there's no windowId here
// if (!finalQueryParams.windowId) {
// bc = new BroadcastChannel(`${BROADCAST_CHANNELS.REDIRECT_CHANNEL}_${instanceParameters.instanceId}`, broadcastChannelOptions);
// bc.addEventListener("message", async (ev) => {
// if (ev.error) {
// reject(ev.error);
// window.close();
// } else {
// resolve();
// bc.close();
// log.info("posted", { finalQueryParams, hashParameters, instanceParameters });
// }
// });
// bc.postMessage({
// data: {
// instanceParams: instanceParameters,
// hashParams: hashParameters,
// queryParams: finalQueryParams,
// },
// error,
// });
setTimeout(() => {
// setTimeout(() => {
// resolve();
// window.location.href = window.location.origin + window.location.search + window.location.hash;
// }, 5000);
// } else {
const channelName = `${BROADCAST_CHANNELS.REDIRECT_CHANNEL}_${this.instanceId || instanceParameters.instanceId}_${
finalQueryParams.windowId || instanceParameters.windowId
}`;
bc = new BroadcastChannel(channelName, broadcastChannelOptions);
bc.addEventListener("message", async (ev) => {
const { url, message } = ev.data as { url: string; message: string };
if (url) {
resolve();
window.location.href = window.location.origin + window.location.search + window.location.hash;
}, 5000);
} else {
bc = new BroadcastChannel(`${finalQueryParams.windowId}`, broadcastChannelOptions);
bc.addEventListener("message", async (ev) => {
const { url, message } = ev.data as { url: string; message: string };
if (url) {
resolve();
window.location.href = url;
} else if (message === SETUP_COMPLETE) {
await bc.postMessage({
data: {
windowId: finalQueryParams.windowId,
message: POPUP_LOADED,
},
});
}
if (ev.error && ev.error !== "") {
log.error(ev.error);
resolve();
bc.close();
}
});
}
window.location.href = url;
} else if (message === SETUP_COMPLETE) {
await bc.postMessage({
data: {
windowId: finalQueryParams.windowId,
message: POPUP_LOADED,
},
});
}
if (ev.error && ev.error !== "") {
log.error(ev.error);
resolve();
bc.close();
}
});
// }
} catch (err: unknown) {

@@ -86,0 +94,0 @@ log.info(err as Error, "something went wrong");

@@ -5,3 +5,3 @@ import { BroadcastChannel } from "@toruslabs/broadcast-channel";

import BaseController from "../BaseController";
import { COMMUNICATION_NOTIFICATIONS, POPUP_LOADED, SETUP_COMPLETE } from "../enums";
import { BROADCAST_CHANNELS, COMMUNICATION_NOTIFICATIONS, POPUP_LOADED, SETUP_COMPLETE } from "../enums";
import { IWindow } from "../interfaces";

@@ -37,19 +37,23 @@ import { broadcastChannelOptions, randomId, sleep } from "../utils";

if (!this.state.windowId) {
// since, we're opening window now, no need to tell window anything
this.update({ windowId: randomId() });
communicationWindowManager.once(`${this.state.windowId}:opened`, () => {
resolve(this);
communicationWindowManager.once(`${this.state.windowId}:iframe-opened`, () => {
// this means iframe is full screen now
this.config.handleWindowBlockAlert().then(resolve).catch(reject);
});
// Tell the other party to create a window by prompting the user to click on something
// Tell the other party to maximize the iframe
communicationEngine.emit("notification", {
method: COMMUNICATION_NOTIFICATIONS.CREATE_WINDOW,
method: COMMUNICATION_NOTIFICATIONS.IFRAME_STATUS,
params: {
windowId: this.state.windowId,
url: this.state.url.href,
isFullScreen: true,
rid: this.state.windowId,
},
});
} else {
// this is a pre-opened window. so, we need to tell it to redirect to correct url. it's currently waiting on /redirect and uses `RedirectHandler` code
// Send this window with `windowId` the url to open via bc
const bc = new BroadcastChannel<PopupData<{ message: string }>>(this.state.windowId, broadcastChannelOptions);
const channelName = `${BROADCAST_CHANNELS.REDIRECT_CHANNEL}_${this.config.instanceId}_${this.state.windowId}`;
const bc = new BroadcastChannel<PopupData<{ message: string }>>(channelName, broadcastChannelOptions);

@@ -56,0 +60,0 @@ bc.addEventListener("message", async (ev) => {

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

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