jsdom / whatwg-url

An implementation of the WHATWG URL Standard in JavaScript
https://jsdom.github.io/whatwg-url/
MIT License
371 stars 94 forks source link

JSDOM (whatwg-url dependency) breaks in Electron 14.0.0: ReferenceError: SharedArrayBuffer is not defined #211

Closed Nashorn closed 3 years ago

Nashorn commented 3 years ago

After upgrading to Electron 14.0.0, JSDOM breaks at the require() statement (If require() is commented, the app loads fine): const jsdom = require("jsdom");

With the following error:

Uncaught (in promise) ReferenceError: SharedArrayBuffer is not defined
    at Object.<anonymous> (/Users/smithjv/Documents/Development/<APP_NAME>/app/node_modules/whatwg-url/node_modules/webidl-conversions/lib/index.js:348)
    at Object.<anonymous> (/Users/smithjv/Documents/Development/<APP_NAME>/app/node_modules/whatwg-url/node_modules/webidl-conversions/lib/index.js:491)
    at Module._compile (internal/modules/cjs/loader.js:1083)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1113)
    at Module.load (internal/modules/cjs/loader.js:940)
    at Module._load (internal/modules/cjs/loader.js:781)
    at Function.f._load (electron/js2c/asar_bundle.js:5)
    at Function.o._load (electron/js2c/renderer_init.js:29)
    at Module.require (internal/modules/cjs/loader.js:964)
    at require (internal/modules/cjs/helpers.js:88)

It appears to be originating from the dependency 'whatwg-url'. This was fixed in webidl-conversions@7.0.0 but whatwg-url was not updated to use the latest dependency.

Nashorn commented 3 years ago

It appears that the ReferenceError is due to SharedArrayBuffer not being defined/available in Electron 14.0.0 so you should test for "undefined", not typeof "function":

### FIX:

const sabByteLengthGetter =
    typeof SharedArrayBuffer != "undefined" ?
      Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, "byteLength").get :
      null;
domenic commented 3 years ago

Dupe of https://github.com/jsdom/webidl-conversions/issues/38

Nashorn commented 3 years ago

The fix for this is very simple and does not break in nodejs: Instead of:

const sabByteLengthGetter =
    Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, "byteLength").get;

Use:

const sabByteLengthGetter =
    typeof SharedArrayBuffer != "undefined" ?
      Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, "byteLength").get :
      null;

and there are many of us who are running jsdom in a nodejs environment within Electron shell. I don't understand why you would not just apply the above condition. Could this ticket be reopened?

Nashorn commented 3 years ago

Soultion for anyone who upgrades Electron to v14 (as JSDOM breaks in latest Nodejs) is to use: Linkedom (https://github.com/WebReflection/linkedom). Far superior, lite, blazing fast, low on memory and is a direct jsdom replacement.