From 6b493ff807767fb744b328c14abbe6dfc213dadf Mon Sep 17 00:00:00 2001 From: "Michael[tm] Smith" Date: Sat, 19 Nov 2022 03:27:50 +0900 Subject: [PATCH] Only assign `vmContext.global.crypto` if not already global (#143) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/matrix-org/matrix-public-archive/issues/141 Node.js v19 has `crypto` set on the global already, so this change causes `vmContext.global.crypto` to be assigned only if `vmContext.global.crypto` isn’t already defined. Otherwise, without this change, the room directory fails to render in Node.js v19+, and instead _"TypeError: Cannot set property crypto of `#` which has only a getter"_ gets thrown. --- server/hydrogen-render/render-hydrogen-to-string-unsafe.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/hydrogen-render/render-hydrogen-to-string-unsafe.js b/server/hydrogen-render/render-hydrogen-to-string-unsafe.js index 980a2bd..6930446 100644 --- a/server/hydrogen-render/render-hydrogen-to-string-unsafe.js +++ b/server/hydrogen-render/render-hydrogen-to-string-unsafe.js @@ -47,7 +47,11 @@ function createDomAndSetupVmContext() { vmContext.global.DOMParser = dom.DOMParser; // Make sure `webcrypto` exists since it was only introduced in Node.js v17 assert(crypto.webcrypto); - vmContext.global.crypto = crypto.webcrypto; + // Only assign vmContext.global.crypto if it's undefined + // (Node.js v19 has crypto set on the global already) + if (!vmContext.global.crypto) { + vmContext.global.crypto = crypto.webcrypto; + } // So require(...) works in the vm vmContext.global.require = require;