matrix-org / matrix-viewer

View the history of public and world readable Matrix rooms
https://archive.matrix.org
Apache License 2.0
74 stars 11 forks source link

Error with Node.js v19.1.0: `Cannot set property crypto of #<Object> which has only a getter` when opening http://localhost:3050/ #141

Closed sideshowbarker closed 1 year ago

sideshowbarker commented 1 year ago

After install, running npm run start-dev and then opening http://localhost:3050/ in my browser, I get this error:


RethrownError: Failed to render Hydrogen to string. In order to reproduce, feed in these arguments into `renderHydrogenToString(...)`:
    renderHydrogenToString arguments: {"vmRenderScriptFilePath":"/Users/mike/workspace/matrix-public-archive/shared/room-directory-vm-render-script.js","vmRenderContext":{"rooms":[{"room_id":"!OGEhHVWSdvArJzumhm:matrix.org","name":"Matrix HQ","topic":"The Official Matrix HQ - chat about Matrix here! | https://matrix.org | https://spec.matrix.org | To support Matrix.org development: https://patreon.com/matrixdotorg | Code of Conduct: https://matrix.org/legal/code-of-conduct/ | This is an English speaking room","canonical_alias":"#matrix:matrix.org","num_joined_members":43342,"avatar_url":"mxc://matrix.org/DRevoaEiuzbkOznknySKuMmE","world_readable":true,"guest_can_join":true,"join_rule":"public"},{"room_id":"!JdzabNUPPXzlCJAhEl:fabric.pub","name":"Portal Finance","topic":"your self-sovereign identity | https://portaldefi.com","canonical_alias":"#portal:fabric.pub","num_joined_members":37542,"avatar_url":"mxc://fabric.pub/VCmmUNJZRvTUsFtMXmxbevfq","world_readable":true,"guest_can_join":true,"join_rule":"public"},{"room_id":"!cNwjdQwVpRMsIMkUpx:matrix.org","name":"BlackArch","topic":"Official BlackArch Linux Channel | BlackArch Help & Support & Hacking & Socializing & Trolling (only noptrix allowed) | https://www.blackarch.org/faq.html | Tools Count: 2810","canonical_alias":"#BlackArch:matrix.org","num_joined_members":35481,"avatar_url":"mxc://matrix.org/XtmfRVKfSuShgVtRgfNJUSVv","world_readable":false,"guest_can_join":false,"join_rule":"public"},{"room_id":"!AGeUOyHpLMMrLYAkXW:matrix.org","name":"Genshin Impact | 🇮🇩","topic":"Tempat membahas hal seputar Genshin Impact.\n\nGroup : https://t.meGenshinImpact_ID\nChannel : https://t.meGenshinImpact_ID_Info\nDiscord : https://discord.gg/ghhwkBFcfY\nMatrix : https://matrix.to/#/#genshin-impact:matrix.org\n\nAbout :\nhttps://telegra.ph/About-Genshin-Impact-10-21","canonical_alias":"#genshin-impact:matrix.org","num_joined_members":27392,"avatar_url":"mxc://matrix.org/gXRwiJNrCnMvuZxywJjeJwGI","world_readable":false,"guest_can_join":false,"join_rule":"public"},{"room_id":"!QSkCgNYFgpOXsoSLpH:russianfedora.online","name":"pro.cxx (Telegram's gated C/C++ chat)","topic":"Гейтованная [из Telegram](https://t.me/procxx) комната. \nИз-за ограничений бота-мостовода сообщения будут видны в тг только от участников russianfedora.online  (NB: скоро будут убрано ограничение, в связи с исправлением в апстриме и запланированном переезде на новую версию) . Для зеркала без гейта в тг используйте чат #procxx:matrix.org\n\nПростые вопросы, лабы и о IDE — в чат новичков. Не хамим, не переходим на личности, не вбрасываем утверждения без доказательств. Объявления о вакансиях и евенты - в ЛС к @alexfails:matrix.org.","canonical_alias":"#procxx:russianfedora.online","num_joined_members":18800,"avatar_url":"mxc://matrix.org/FabIxhJzjCtWxtlBQpBwqXjJ","world_readable":false,"guest_can_join":false,"join_rule":"public"},{"room_id":"!wOlkWNmgkAZFxbTaqj:matrix.org","name":"Raspberry Pi","topic":"All things Raspberry Pi! | raspberrypi.com | reddit.com/r/raspberry_pi | rpilocator.com","canonical_alias":"#raspberrypi:matrix.org","num_joined_members":18771,"avatar_url":"mxc://matrix.org/jbPiLipNSQdOvLhQKsMDMcMk","world_readable":true,"guest_can_join":true,"join_rule":"public"},{"room_id":"!gWiVYCURkxIapWZaGy:ipfs.io","name":"ipfs","topic":"Join the IPFS Space at #ipfs-space:ipfs.io 🚀 |This room is bridged to https://discord.gg/Z4H6tdECb9 and https://filecoin.io/slack #ipfs","canonical_alias":"#ipfs-chatter:ipfs.io","num_joined_members":17830,"avatar_url":"mxc://ipfs.io/3b30e690afa12b4da473049c4c7cf9ce88fc73e8","world_readable":true,"guest_can_join":true,"join_rule":"public"},{"room_id":"!AEQOsKuwoGVVoVwfDj:matrix.org","name":"Edgeware Public Square","topic":"Bridge from Telegram","canonical_alias":"#Hey:matrix.org","num_joined_members":17479,"avatar_url":"mxc://matrix.org/gQsNrxcMDxMNgsDJQodJuaIV","world_readable":true,"guest_can_join":true,"join_rule":"public"},{"room_id":"!hKwFRvooxscFyToTfI:matrix.org","name":"Filecoin Lobby","topic":"This is a read-only mirror from the Filecoin Slack Community. To participate, please go to https://filecoin.io/slack","canonical_alias":"#fil-lobby:matrix.org","num_joined_members":17461,"avatar_url":"mxc://matrix.org/vKiJMJzJGezyOwZSXgzCbSAf","world_readable":false,"guest_can_join":false,"join_rule":"public"}],"roomFetchError":null,"nextPaginationToken":"g6FtzUQ1oXK+IWhLd0ZSdm9veHNjRnlUb1RmSTptYXRyaXgub3JnoWTD","pageSearchParameters":{"homeserver":"matrix.org","limit":9},"config":{"basePath":"http://localhost:3050","matrixServerUrl":"https://matrix.org/","matrixServerName":"matrix.org"}},"pageOptions":{"title":"Matrix Public Archive","styles":["http://localhost:3050/hydrogen-styles.css","http://localhost:3050/css/styles.css","http://localhost:3050/css/room-directory.css"],"scripts":["http://localhost:3050/js/entry-client-room-directory.es.js"],"locationHref":"http://localhost:3050/","shouldIndex":true,"cspNonce":"c844c468ff2389a14ac3f139e975a157"}}
    at renderHydrogenToString (/Users/mike/workspace/matrix-public-archive/server/hydrogen-render/render-hydrogen-to-string.js:52:11)
    --- Original Error ---
    RethrownError: Child process exited with code 1
        at assembleErrorAfterChildExitsWithErrors (/Users/mike/workspace/matrix-public-archive/server/child-process-runner/run-in-child-process.js:56:29)
        --- Original Error ---
        TypeError: Cannot set property crypto of #<Object> which has only a getter
            at createDomAndSetupVmContext (/Users/mike/workspace/matrix-public-archive/server/hydrogen-render/render-hydrogen-to-string-unsafe.js:50:27)
            at _renderHydrogenToStringUnsafe (/Users/mike/workspace/matrix-public-archive/server/hydrogen-render/render-hydrogen-to-string-unsafe.js:71:30)
            at process.<anonymous> (/Users/mike/workspace/matrix-public-archive/server/child-process-runner/child-fork-script.js:72:26)
            at process.emit (node:events:513:28)
            at emit (node:internal/child_process:944:14)
            at process.processTicksAndRejections (node:internal/process/task_queues:83:21)
MadLittleMods commented 1 year ago

@sideshowbarker Which Node.js version are you using? You should be Node.js v16+, https://github.com/matrix-org/matrix-public-archive#prerequisites

sideshowbarker commented 1 year ago

@sideshowbarker Which Node.js version are you using? You should be Node.js v16+, matrix-org/matrix-public-archive#prerequisites

$ node --version
v19.1.0

Is that maybe too new? Should I try Node.js v16 or v17 instead?

sideshowbarker commented 1 year ago

OK, to answer my own question: I just now tried with Node.js v17.9.1 instead — and it works 🎉

But I guess that means there must be some difference in v19 which breaks this, so I guess I should leave this open?

MadLittleMods commented 1 year ago

It looks like require('crypto').webcrypto.subtle is still available in Node.js v19 so it's not that.

Based on the line in the error stacktrace, it seems like it's complaining about us setting the global.crypto. I guess Node.js v19 has crypto set on the global already.

https://github.com/matrix-org/matrix-public-archive/blob/b41d15a92c0f4d2d5e6977bec95778bffd753754/server/hydrogen-render/render-hydrogen-to-string-unsafe.js#L50

I kinda see this change at https://nodejs.org/docs/latest-v19.x/api/webcrypto.html which mentions const { subtle } = globalThis.crypto;.

We could add an extra check to only set it if vmContext.global.crypto does not exist already. @sideshowbarker would be interested in submitting a PR (and a quick test)?

sideshowbarker commented 1 year ago

We could add an extra check to only set it if vmContext.global.crypto does not exist already. @sideshowbarker would be interested in submitting a PR (and a quick test)?

Yeah — I’ll take a shot at writing up a patch and test for it today

sideshowbarker commented 1 year ago

OK, https://github.com/matrix-org/matrix-public-archive/pull/143 now raised.

The test I added is just a shot in the dark. I’m happy to work on updating it further. As I understand it, the end-to-end test that’s needed here is something like “the room directory renders as expected“ — or maybe “the room directory renders without throwing“ — but I don’t yet understand how to write a granular test specifically for that.