Skip to content

Commit

Permalink
- remove deprecated features
Browse files Browse the repository at this point in the history
- remove unused dependencies
- replace usage of unnecessary packages
  • Loading branch information
chaitanyapotti committed May 24, 2023
1 parent 38b8c0e commit fc70cfb
Show file tree
Hide file tree
Showing 11 changed files with 1,345 additions and 10,641 deletions.
11,832 changes: 1,229 additions & 10,603 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,29 @@
"@babel/runtime": "7.x"
},
"dependencies": {
"@metamask/obs-store": "^8.1.0",
"@toruslabs/http-helpers": "^4.0.0",
"@toruslabs/openlogin-jrpc": "^4.4.0",
"end-of-stream": "^1.4.4",
"eth-rpc-errors": "^4.0.3",
"events": "^3.3.0",
"fast-deep-equal": "^3.1.3",
"is-stream": "^2.0.1",
"lodash.merge": "^4.6.2",
"loglevel": "^1.8.1",
"once": "^1.4.0",
"pump": "^3.0.0"
"pump": "^3.0.0",
"readable-stream": "^4.4.0"
},
"devDependencies": {
"@babel/runtime": "^7.21.5",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-replace": "^5.0.2",
"@toruslabs/eslint-config-typescript": "^2.0.0",
"@toruslabs/torus-scripts": "^4.0.0",
"@types/create-hash": "^1.2.2",
"@types/lodash.merge": "^4.6.7",
"@types/node": "^18",
"@types/once": "^1.4.0",
"@types/readable-stream": "^2.3.15",
"@typescript-eslint/eslint-plugin": "^5.59.7",
"@typescript-eslint/parser": "^5.59.7",
"cross-env": "^7.0.3",
"empty-module": "0.0.2",
"eslint": "^8.41.0",
"eslint-plugin-import": "~2.27.5",
"eslint-plugin-mocha": "^10.1.0",
Expand Down
2 changes: 0 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import json from "@rollup/plugin-json";
import replace from "@rollup/plugin-replace";

import pkg from "./package.json";

export default {
plugins: [
json(),
replace({
"process.env.TORUS_EMBED_VERSION": `"${pkg.version}"`,
preventAssignment: true,
Expand Down
50 changes: 50 additions & 0 deletions src/ObservableStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { SafeEventEmitter } from "@toruslabs/openlogin-jrpc";

export class ObservableStore<T> extends SafeEventEmitter {
private _state: T;

constructor(initState: T) {
super();
if (initState === undefined) {
this._state = {} as unknown as T;
} else {
this._state = initState;
}
}

getState(): T {
return this._getState();
}

putState(newState: T): void {
this._putState(newState);
this.emit("update", newState);
}

updateState(partialState: Partial<T>): void {
// if non-null object, merge
if (partialState && typeof partialState === "object") {
const state = this.getState();
this.putState({ ...state, ...partialState });
// if not object, use new value
} else {
this.putState(partialState as T);
}
}

subscribe(handler: (state: T) => void): void {
this.on("update", handler);
}

unsubscribe(handler: (state: T) => void): void {
this.removeListener("update", handler);
}

protected _getState(): T {
return this._state;
}

protected _putState(newState: T): void {
this._state = newState;
}
}
51 changes: 51 additions & 0 deletions src/ObservableStoreStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { _IWritable, Duplex as DuplexStream } from "readable-stream";

import { ObservableStore } from "./ObservableStore";

class ObservableStoreStream<T> extends DuplexStream {
handler: (state: T) => void;

obsStore: ObservableStore<T>;

constructor(obsStore: ObservableStore<T>) {
super({
// pass values, not serializations
objectMode: true,
});
// dont buffer outgoing updates
this.resume();
// save handler so we can unsubscribe later
this.handler = (state: T) => this.push(state);
// subscribe to obsStore changes
this.obsStore = obsStore;
this.obsStore.subscribe(this.handler);
}

// emit current state on new destination
pipe<U extends _IWritable>(dest: U, options?: { end?: boolean }): U {
const result = super.pipe(dest, options);
dest.write(this.obsStore.getState() as any);
return result;
}

// write from incoming stream to state
_write(chunk: any, _encoding: string, callback: (error?: Error | null) => void): void {
this.obsStore.putState(chunk);
callback();
}

// noop - outgoing stream is asking us if we have data we arent giving it
_read(_size: number): void {
return undefined;
}

// unsubscribe from event emitter
_destroy(err: Error | null, callback: (error: Error | null) => void): void {
this.obsStore.unsubscribe(this.handler);
super._destroy(err, callback);
}
}

export function storeAsStream<T>(obsStore: ObservableStore<T>): ObservableStoreStream<T> {
return new ObservableStoreStream(obsStore);
}
12 changes: 0 additions & 12 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
BUTTON_POSITION,
BUTTON_POSITION_TYPE,
EMBED_TRANSLATION_ITEM,
LOGIN_PROVIDER,
NetworkInterface,
PAYMENT_PROVIDER_TYPE,
PaymentParams,
Expand Down Expand Up @@ -38,14 +37,6 @@ import {
validatePaymentProvider,
} from "./utils";

const defaultVerifiers = {
[LOGIN_PROVIDER.GOOGLE]: true,
[LOGIN_PROVIDER.FACEBOOK]: true,
[LOGIN_PROVIDER.REDDIT]: true,
[LOGIN_PROVIDER.TWITCH]: true,
[LOGIN_PROVIDER.DISCORD]: true,
};

const UNSAFE_METHODS = [
"eth_sendTransaction",
"eth_signTypedData",
Expand Down Expand Up @@ -156,8 +147,6 @@ class Torus {
async init({
buildEnv = TORUS_BUILD_ENV.PRODUCTION,
enableLogging = false,
// deprecated: use loginConfig instead
enabledVerifiers = defaultVerifiers,
network = {
host: "mainnet",
chainId: null,
Expand Down Expand Up @@ -242,7 +231,6 @@ class Torus {
initStream.write({
name: "init_stream",
data: {
enabledVerifiers,
loginConfig,
whiteLabel: this.whiteLabel,
buttonPosition: this.buttonPosition,
Expand Down
3 changes: 2 additions & 1 deletion src/inpage-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ObservableStore, storeAsStream } from "@metamask/obs-store";
import {
createIdRemapMiddleware,
createStreamMiddleware,
Expand Down Expand Up @@ -29,6 +28,8 @@ import {
} from "./interfaces";
import log from "./loglevel";
import messages from "./messages";
import { ObservableStore } from "./ObservableStore";
import { storeAsStream } from "./ObservableStoreStream";
import { createErrorMiddleware, EMITTED_NOTIFICATIONS, logStreamDisconnectWarning, NOOP } from "./utils";

SafeEventEmitter.defaultMaxListeners = 100;
Expand Down
8 changes: 0 additions & 8 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { JRPCId, JRPCMiddleware, JRPCRequest, JRPCVersion, SafeEventEmitter } from "@toruslabs/openlogin-jrpc";
import type { Duplex } from "readable-stream";

export const LOGIN_PROVIDER = {
GOOGLE: "google",
FACEBOOK: "facebook",
TWITCH: "twitch",
REDDIT: "reddit",
DISCORD: "discord",
} as const;

export const WALLET_VERIFIERS = {
GOOGLE: "google",
FACEBOOK: "facebook",
Expand Down
10 changes: 10 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<head>
<title>Torus Embed Test</title>
</head>
<body>
<script src="../dist/torus.umd.min.js"></script>
<script>
console.log(Torus);
</script>
</body>
2 changes: 1 addition & 1 deletion torus.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
analyzerMode: "disabled",
analyzerMode: "static",
};
7 changes: 0 additions & 7 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ const { EnvironmentPlugin } = require("webpack");
const pkg = require("./package.json");

exports.baseConfig = {
output: {
library: {
export: "default",
},
},
resolve: {
alias: {
"bn.js": path.resolve(__dirname, "node_modules/bn.js"),
lodash: path.resolve(__dirname, "node_modules/lodash-es"),
"js-sha3": path.resolve(__dirname, "node_modules/js-sha3"),
"web3-providers-ipc": path.resolve(__dirname, "node_modules/empty-module"),
"web3-providers-ws": path.resolve(__dirname, "node_modules/empty-module"),
},
},
plugins: [new EnvironmentPlugin({ TORUS_EMBED_VERSION: pkg.version })],
Expand Down

0 comments on commit fc70cfb

Please sign in to comment.