Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: Guard against window not existing. #332

Merged
merged 1 commit into from Jun 1, 2023
Merged

all: Guard against window not existing. #332

merged 1 commit into from Jun 1, 2023

Conversation

icio
Copy link
Contributor

@icio icio commented Jun 1, 2023

While we don't intend to support RavelinJS in node/deno or other non-browser execution environments, it would be friendlier those users using SSR if the library didn't error when being imported. With this change, we'll only error if they try to initialise.

before:

$ node -e "require('./dist/core+track.js')"                
/home/icio/src/github.com/unravelin/ravelinjs/dist/core+track.js:438
  var _XDomainRequest = window.XDomainRequest;
                        ^

ReferenceError: window is not defined
    at /home/icio/src/github.com/unravelin/ravelinjs/dist/core+track.js:438:25
    at /home/icio/src/github.com/unravelin/ravelinjs/dist/core+track.js:3:83
    at Object.<anonymous> (/home/icio/src/github.com/unravelin/ravelinjs/dist/core+track.js:6:3)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Module._load (node:internal/modules/cjs/loader:922:12)
    at Module.require (node:internal/modules/cjs/loader:1105:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at [eval]:1:1

Node.js v18.13.0

after:

$ (set -x; for SRC in ./dist/*.js; do node -e "require('"$SRC"')"; done) && echo ok
+ SRC=./dist/core+encrypt.js
+ node -e 'require('\''./dist/core+encrypt.js'\'')'
+ SRC=./dist/core+encrypt+promise.js
+ node -e 'require('\''./dist/core+encrypt+promise.js'\'')'
+ SRC=./dist/core.js
+ node -e 'require('\''./dist/core.js'\'')'
+ SRC=./dist/core+promise.js
+ node -e 'require('\''./dist/core+promise.js'\'')'
+ SRC=./dist/core+track+encrypt.js
+ node -e 'require('\''./dist/core+track+encrypt.js'\'')'
+ SRC=./dist/core+track+encrypt+promise.js
+ node -e 'require('\''./dist/core+track+encrypt+promise.js'\'')'
+ SRC=./dist/core+track.js
+ node -e 'require('\''./dist/core+track.js'\'')'
+ SRC=./dist/core+track+promise.js
+ node -e 'require('\''./dist/core+track+promise.js'\'')'
ok

While we don't intend to support RavelinJS in node/deno or other non-browser
execution environments, it would be friendlier those users using SSR if the
library didn't error when being imported. With this change, we'll only error if
they try to initialise.
@icio
Copy link
Contributor Author

icio commented Jun 1, 2023

The send spec tests are failing in the integration tests, to be fixed by #331. I think the unit tests adequately cover these changes, though.

@@ -11,6 +11,6 @@ function Ravelin(cfg) {
this.encrypt = new Encrypt(this.core, cfg);
}

Ravelin.Promise = window.Promise || Promise;
Ravelin.Promise = typeof Promise !== 'undefined' && Promise || PolyfillPromise;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be easier to do a null check? Same goes for the other ones?

Suggested change
Ravelin.Promise = typeof Promise !== 'undefined' && Promise || PolyfillPromise;
Ravelin.Promise = Promise != null && Promise || PolyfillPromise;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In older browsers before Promise because a thing, you'd get an equivalent of a ReferenceError because it's not a defined global.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UGH I hate older browsers

Copy link

@ericmatteucci ericmatteucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@icio icio merged commit cc0d1c3 into v1 Jun 1, 2023
0 of 3 checks passed
@icio icio deleted the guard-window branch June 1, 2023 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants