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

remove global window and localStorage dependecy #74

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/lnc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class LNC {
}

private get wasm() {
return window[this._namespace] as WasmGlobal;
return globalThis[this._namespace] as WasmGlobal;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this actually looks a bit dangerous because this could overwrite something. if this._namespace is already a defined property.

}

get isReady() {
Expand Down Expand Up @@ -195,7 +195,11 @@ export default class LNC {
);

// add an event listener to disconnect if the page is unloaded
window.addEventListener('unload', this.wasm.wasmClientDisconnect);
if (typeof window !== "undefined") {
window.addEventListener('unload', this.wasm.wasmClientDisconnect);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

here we can add an eventListener only when the window is available. other contexts might need to manually register some custom code to disconnect.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good call on this one

} else {
log.info("No unload event listener added. window is not available");
}

// repeatedly check if the connection was successful
return new Promise<void>((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const generateSalt = () => {
const validChars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let array = new Uint8Array(32);
window.crypto.getRandomValues(array);
globalThis.crypto.getRandomValues(array);
array = array.map((x) => validChars.charCodeAt(x % validChars.length));
const salt = String.fromCharCode.apply(null, array as any);
return salt;
Expand Down
4 changes: 2 additions & 2 deletions lib/util/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export class Logger {
// by default, log nothing (assuming prod)
let level = LogLevel.none;

if (localStorage && localStorage.getItem('debug')) {
if (globalThis.localStorage && globalThis.localStorage.getItem('debug')) {
// if a 'debug' key is found in localStorage, use the level in storage or 'debug' by default
const storageLevel = localStorage.getItem('debug-level') || 'debug';
const storageLevel = globalThis.localStorage.getItem('debug-level') || 'debug';
level = LogLevel[storageLevel as keyof typeof LogLevel];
}

Expand Down