Only assign `vmContext.global.crypto` if not already global (#143)

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 `#<Object>` which has only a getter"_ gets thrown.
This commit is contained in:
Michael[tm] Smith 2022-11-19 03:27:50 +09:00 committed by GitHub
parent 6e2a56d726
commit 6b493ff807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 1 deletions

View File

@ -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);
// 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;