Bug: Cannot convert BigInt value to a number.
See original GitHub issueThis is a bug report
Problem
In our frontend platform at Huddle01 we are facing this bug on running yarn build
.
On checking various packages we found that when we commented the usage of waku functions and deleted the waku package then this error wasn’t showing up.
Here is all the code related to waku functionality that we are using.
wakuUtils.js
import protons from "protons";
const proto = protons(`
message SimpleChatMessage {
uint64 timestamp = 1;
string text = 2;
string sender = 3;
}
`);
const selectFleetEnv = () => {
// Works with react-scripts
if (process?.env?.NODE_ENV === "development") {
return ["fleets", "wakuv2.test", "waku-websocket"];
} else {
return ["fleets", "wakuv2.prod", "waku-websocket"];
}
};
const processMsgs = (msg) => {
if (!msg.payload) return;
const { timestamp, text, sender } = proto.SimpleChatMessage.decode(
msg.payload
);
const time = new Date(timestamp).toLocaleTimeString();
const utf8Text = Buffer.from(text).toString("utf-8");
return { timestamp: time, text: utf8Text, sender };
};
export { proto, selectFleetEnv, processMsgs };
Parts of code where we used waku in our live stream chats:
// creating waku object
useEffect(() => {
if (!isLivestreamChat) return;
if (!!waku) return;
if (wakuStatus !== "None") return;
setWakuStatus("Starting");
// Create Waku
Waku.create({
libp2p: {
config: {
pubsub: {
enabled: true,
emitSelf: true,
},
},
},
bootstrap: getBootstrapNodes.bind({}, selectFleetEnv()),
}).then((waku) => {
setWaku(waku);
setWakuStatus("Started");
waku.waitForConnectedPeer().then(() => {
setWakuStatus("Ready");
console.log(wakuStatus);
});
});
console.log(wakuStatus);
}, [waku, wakuStatus]);
const processWakuHistory = (retrievedMessages) => {
const messages = retrievedMessages
.map((msg) => processMsgs(msg))
.filter(Boolean);
setWakuMsgs((waku) => {
return messages.concat(waku);
});
if (wakuMsgs.length >= 20) return true;
};
// fetching history of chats from waku-store
useEffect(() => {
if (wakuStatus !== "Ready") return;
waku.store
.queryHistory([ContentTopic], { callback: processWakuHistory })
.catch((e) => {
console.log("Failed to retrieve messages", e);
});
dispatch(setWakuStoreRetrieved(true));
}, [waku, wakuStatus]);
// sending message using waku-relay
const handleWakuMessage = async (msgInput) => {
if (!isLivestreamChat) return;
if (wakuStatus !== "Ready") return;
if (!msgInput) return;
const text = msgInput.trim();
const payload = proto.SimpleChatMessage.encode({
timestamp: new Date(),
text: text,
sender:
localStorage.getItem("livestream_viewer") ||
window.ethereum.selectedAddress,
});
WakuMessage.fromBytes(payload, ContentTopic).then((wakuMessage) =>
waku.relay.send(wakuMessage)
);
console.log(payload);
};
// observer for receiving messages
const processIncomingMessage = useCallback((wakuMessage) => {
const message = processMsgs(wakuMessage);
setWakuMsgs((messages) => {
return messages.concat(message);
});
}, []);
// receive message using waku-relay
useEffect(() => {
if (!waku) return;
waku.relay.addObserver(processIncomingMessage, [ContentTopic]);
return function cleanUp() {
waku.relay.deleteObserver(processIncomingMessage, [ContentTopic]);
};
}, [waku, wakuStatus, processIncomingMessage]);
Notes
- js-waku version: 0.14.2
Issue Analytics
- State:
- Created 2 years ago
- Comments:19 (13 by maintainers)
Top Results From Across the Web
TypeError: can't convert BigInt to number - MDN Web Docs
The JavaScript exception "can't convert BigInt to number" occurs when an arithmetic operation involves a mix of BigInt and Number values.
Read more >Uncaught TypeError: Cannot convert a BigInt value to ... - GitHub
Using this with typescript / react-scripts, I am getting an error in the production / transpiled code (even though it works in my...
Read more >Cannot convert a BigInt value to a number - DFINITY Forums
When my project is packaged into the subnet, an error message appears { key: "covariant", value: function(A) { -> var t = Math.pow(BigInt(2) ......
Read more >How to convert BigInt to Number in JavaScript? - Stack Overflow
I found myself in the situation where I wanted to convert a BigInt value to a Number value. Knowing that my value is...
Read more >What is a BigInt in JavaScript? - Educative.io
BigInt is a built-in object that provides a way to store numbers larger than ... type conversion is not performed and returns false...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@D4nte I added this thing like this:
and on doing
yarn build && npx serve -s build
, it ran without problems. But I am still kind of confused as to how did doing this help.@ritvij14 please have a look at at what
browserslist
does. My understanding is that it set the browser target for which the code is transpiled to.">0.2%"
for example means: transpile code from browser that has a market share of more than 0.2% (I think).This included all browsers that do not support BigInt such as ie 98 and below and android 4.4.3 and below. Which mean the transpiled code did not contain definition of BigInt. Hence the error.
By restricting the browser target to more recent browser, it allowed the use of
BigInt
.Keeping this issue open to track documentation effort.