rocicorp / replicache-transaction

Adapter from Replicache's WriteTransaction interface to some key/value storage.
Other
3 stars 0 forks source link

Error publishing to Cloudflare: "generating random values ... can only be performed while handling a request" #10

Open aboodman opened 1 year ago

aboodman commented 1 year ago

This was reported by a user. Debugging with him what's happening is that replicache-transaction relies on replicache for some helper functions (probably wasn't a great idea). And replicache has some load-time code that calls crypto.randomUUID().

The crypto namespace is implemented by CF workers, but you're not allowed to use it outside of a request, apparently.

Not sure what the right solution is here. I think the user can work around either by undefining crypto, or else dynamically loading the module. But other users of replicache-transaction on cf will hit the same problem.

aboodman commented 1 year ago

@arv wdyt?

aboodman commented 1 year ago

On further investigation, it looks like the load-time call to uuid has been fixed in Replicache 13. Perhaps we should do the same thing on the 12 branch.

arv commented 1 year ago

It is already in v12

Screenshot 2023-05-09 at 12 21 09

https://github.com/rocicorp/replicache-internal/commit/9982954f251c3a8d43e0e697e22a33bc55ff8eb8

Do you know what versions of replicache and replicache-transaction they had?

aboodman commented 1 year ago

Like I said the crypto namespace is implemented, so the undefined check doesn’t work. You’re just not allowed to call it at module init time.

13 doesn’t have this problem because it doesn’t call to crypto.randomUUID until later.

On Tue, May 9, 2023 at 12:26 AM Erik Arvidsson @.***> wrote:

It is already in v12 [image: Screenshot 2023-05-09 at 12 21 09] https://user-images.githubusercontent.com/45845/237068065-0e54fd68-8b9b-4744-a905-447602e09280.png

@.*** https://github.com/rocicorp/replicache-internal/commit/9982954f251c3a8d43e0e697e22a33bc55ff8eb8

Do you know what versions of replicache and replicache-transaction they had?

— Reply to this email directly, view it on GitHub https://github.com/rocicorp/replicache-transaction/issues/10#issuecomment-1539891409, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATUBE6WDUFTTGA6UZLK5TXFILUZANCNFSM6AAAAAAX27AHUQ . You are receiving this because you authored the thread.Message ID: @.***>

-- a (phone)

arv commented 1 year ago

I did some more research here. This is the change that we did on mono to fix this:

https://github.com/rocicorp/mono/commit/8e83d2bcf375ebe89feb77231acde8afb0c91814

and here is the same patch on replicache-internal/v12

https://github.com/rocicorp/replicache-internal/commit/4f1d898baaebb76fc8ed9d29f343b5c96c9ceba6

I hear that you say that we do not call these function in v13 but we call them in v12. I have not been able to find such a place. I put a breakpoint next to the crypto.randomUUID call and that is not hit in startup code (tried in replicache tests and replicache-transactions tests).

What version of replicache-transaction & replicache as the costumer using? Is it possible they imported something from replicache and called it manually?

aboodman commented 1 year ago

CleanShot_2023-05-08_at_16 57 292x

Shoot I agree, it looks like this was fixed in 12. Attached is the stack I witnessed this in. Perhaps I was using an old version of 12?

arv commented 1 year ago

If you see it again you can always check the version property of the replicache module.