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

TypeError: Object(...) is not a function at Object.randomInt #797

Open
sambhav-gore opened this issue Jan 23, 2024 · 11 comments
Open

TypeError: Object(...) is not a function at Object.randomInt #797

sambhav-gore opened this issue Jan 23, 2024 · 11 comments

Comments

@sambhav-gore
Copy link

Do you want to request a feature or report a bug?
Report a Bug

What is the current behavior?
When creating a new instance I see the following error in browser console:

pusher-js.js?v=405c2cf0:36109 Uncaught (in promise) TypeError: Object(...) is not a function
    at Object.randomInt (pusher-js.js?v=405c2cf0:36109:57)
    at new pusher_Pusher (pusher-js.js?v=405c2cf0:36654:43)
    at new pusher_with_encryption_PusherWithEncryption (pusher-js.js?v=405c2cf0:36826:13)
    at +page.svelte:17:9

If the current behavior is a bug, please provide the steps to reproduce and
if possible a minimal demo of the problem via https://jsfiddle.net or similar.

What is the expected behavior?
Pusher to intialize and work correctly

Which versions of Pusher, and which browsers / OS are affected by this issue?
Did this work in previous versions of Pusher? If so, which?

I tried many versions 7.4.0, 8.0.2 and 8.4.0-rc2

** MORE DETAILS

It is a sveltekit app which use vite.
It was working fine with sveltekit 1.x vite 4.x but now I need to upgrade.

The biggest difference is that the support for node modules was dropped, I am using the polyfill node modules from vite-plugin-node-polyfills

Following is my entire vite.config.ts

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills'

export default defineConfig({
	plugins: [sveltekit(), nodePolyfills({
		globals: {
			Buffer: true, // can also be 'build', 'dev', or false
			global: true,
			process: false,
		},
	})]
});

I was struggling with this for a while so I tried many different things that I found in answers in stackoverflow.

I will highly appreciate any help. or a working sveltekit example if possible.

@sambhav-gore
Copy link
Author

Error happens at this code in the browser:

randomInt(max) {
            return Object(external_crypto_["randomInt"])(max);
          }

@sambhav-gore
Copy link
Author

the problem seems to be that in browser there is no crypto module available.

I tried various ways to polyfill it but every attempt failed

@benw-pusher
Copy link
Contributor

Did you try https://www.npmjs.com/package/rollup-plugin-polyfill-node? this specifically includes polyfil for the crypto package.

@sambhav-gore
Copy link
Author

Did you try https://www.npmjs.com/package/rollup-plugin-polyfill-node? this specifically includes polyfill for the crypto package.

Yes I did. That didn't work either. It does make an empty crypto which doesn't have the function randomInt.
Almost everywhere it says it is hard to polyfill crypto and should be avoided a best as possible.

If I am not able to fix this problem by next 2 days we will have no choice but to look for a Pusher alternative.

@Gabotron-ES
Copy link

Getting the same error, any fix available?

@Gabotron-ES
Copy link

@sambhav-gore did you find a solution?

@sambhav-gore
Copy link
Author

@Gabotron-ES No I did not. We are planning to move to Ably now because of this.

@Gabotron-ES
Copy link

So because of this bug, pusher is basically unable to work? lol such a a joke

@sambhav-gore
Copy link
Author

@Gabotron-ES well, there is probably a fix or a workaround but I am too tied up with other development work plus my client doesn't have a lot of budget or time for me to research/fix. So basically 2-3 months ago he told me that if it works within next 2 days we fix or we move to a different provider (Ably). Since then we have parked the upgrade work for now which we will be taking up again in next month. Since I am personally happy with Pusher, I will give it one last try in the next month.

@Gabotron-ES
Copy link

I see, good lucj and if you come up with a fix please notify us!

@JavierMartinz
Copy link

Same here... I tried with vite-plugin-node-polyfills but no luck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants