Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network,std-client): make nodejs compatible #919

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"async-mutex": "^0.3.2",
"debug": "^4.3.4",
"ethers": "^5.7.2",
"fake-indexeddb": "^4.0.0",
"lodash": "^4.17.21",
"mobx": "^6.7.0",
"nice-grpc-web": "^2.0.1",
Expand All @@ -60,7 +61,6 @@
"@types/jest": "^27.4.1",
"@types/lodash": "^4.14.182",
"@types/node": "^18.15.11",
"fake-indexeddb": "^4.0.0",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"ts-jest": "^29.0.5",
Expand Down
4 changes: 2 additions & 2 deletions packages/network/src/createRelayStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { spawn } from "threads";
import { messagePayload } from "./utils";
import { createChannel, createClient } from "nice-grpc-web";
import { awaitPromise, awaitStreamValue } from "@latticexyz/utils";
import { grpc } from "@improbable-eng/grpc-web";
import grpcweb from "@improbable-eng/grpc-web";
import { ECSRelayServiceDefinition, Message, PushRequest } from "@latticexyz/services/ecs-relay";

/**
Expand All @@ -15,7 +15,7 @@ import { ECSRelayServiceDefinition, Message, PushRequest } from "@latticexyz/ser
*/
export async function createRelayStream(signer: Signer, url: string, id: string) {
const httpClient = createClient(ECSRelayServiceDefinition, createChannel(url));
const wsClient = createClient(ECSRelayServiceDefinition, createChannel(url, grpc.WebsocketTransport()));
const wsClient = createClient(ECSRelayServiceDefinition, createChannel(url, grpcweb.grpc.WebsocketTransport()));

const recoverWorker = await spawn(
new Worker(new URL("./workers/Recover.worker.js", import.meta.url), { type: "module" })
Expand Down
5 changes: 4 additions & 1 deletion packages/network/src/initCache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { arrayToIterator, deferred, mergeIterators, transformIterator } from "@latticexyz/utils";
import fakeIndexedDb from "fake-indexeddb";
Copy link
Member

@holic holic May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a way to update the methods below to use an async import so we don't have to import this into browser bundles.

const indexedDB = typeof self !== 'undefined' && typeof self.indexedDB !== 'undefined'
  ? self.indexedDB
  : await import('fake-indexeddb');


const indexedDB = self.indexedDB;
// Use an indexedDB mock for nodejs environments
// TODO: remove indexedDB requirement for better nodejs support
const indexedDB = typeof self === "undefined" ? fakeIndexedDb : self.indexedDB;
const VERSION = 2;

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/v2/syncUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Observable, concatMap, map, of } from "rxjs";
import { fetchStoreEvents } from "./fetchStoreEvents";
import { NetworkComponentUpdate, NetworkEvent } from "../types";
import { orderBy } from "lodash";
import orderBy from "lodash/orderBy";
import debug from "debug";
import { awaitPromise, range } from "@latticexyz/utils";

Expand Down
12 changes: 8 additions & 4 deletions packages/std-client/src/getBurnerWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
import { isHex, Hex } from "viem";
import { BehaviorSubject } from "rxjs";

const localStorage = typeof window === "undefined" ? undefined : window.localStorage;
const addEventListener = typeof window === "undefined" ? undefined : window.addEventListener;
const removeEventListener = typeof window === "undefined" ? undefined : window.removeEventListener;

function assertPrivateKey(privateKey: string, cacheKey: string): asserts privateKey is Hex {
if (!isHex(privateKey)) {
console.error("Private key found in cache is not valid hex", { privateKey, cacheKey });
Expand All @@ -13,7 +17,7 @@ function assertPrivateKey(privateKey: string, cacheKey: string): asserts private
}

export function getBurnerWallet(cacheKey = "mud:burnerWallet"): BehaviorSubject<Hex> {
const cachedPrivateKey = localStorage.getItem(cacheKey);
const cachedPrivateKey = localStorage?.getItem(cacheKey);

if (cachedPrivateKey != null) {
assertPrivateKey(cachedPrivateKey, cacheKey);
Expand All @@ -25,14 +29,14 @@ export function getBurnerWallet(cacheKey = "mud:burnerWallet"): BehaviorSubject<
: (() => {
const privateKey = generatePrivateKey();
console.log("New burner wallet created:", privateKeyToAccount(privateKey));
localStorage.setItem(cacheKey, privateKey);
localStorage?.setItem(cacheKey, privateKey);
return new BehaviorSubject(privateKey);
})();

window.addEventListener("storage", function listener(event) {
addEventListener?.("storage", function listener(event) {
// Clean up
if (subject.closed) {
window.removeEventListener("storage", listener);
removeEventListener?.("storage", listener);
return;
}

Expand Down
Loading