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

fix(CookieStore): check if "localStorage" is defined before using it #8

Merged
merged 2 commits into from
May 28, 2021
Merged

fix(CookieStore): check if "localStorage" is defined before using it #8

merged 2 commits into from
May 28, 2021

Conversation

neilime
Copy link

@neilime neilime commented May 7, 2021

Fixes #7

  • Check if localStorage is defined before using it.
  • Prettier format

@neilime neilime changed the title fix(CookieStore): check if is defined before using it fix(CookieStore): check if "localStorage" is defined before using it May 7, 2021
* Retrieve the global local storage if defined
* @returns Storage | undefined
*/
private getLocalStorage(): Storage | undefined {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we get any benefits of keeping this as a private method. We'd rather store it as a module-scoped variable, as localStorage is either defined or not (determined by the environment). Getter makes sense if the storage is eventually defined, but this is not the case.

}
type Store = Map<string, StoreEntry>
type StoreEntry = Map<string, Cookie>
type CookieString = Omit<Cookie, 'expires'> & { expires?: string };
type CookieString = Omit<Cookie, 'expires'> & { expires?: string }

export const PERSISTENCY_KEY = 'MSW_COOKIE_STORE'

Copy link
Member

Choose a reason for hiding this comment

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

I'd define the global variable here, determining if we can use local storage:

const SUPPORTS_LOCAL_STORAGE = typeof localStorage !== 'undefined'

Copy link
Author

@neilime neilime May 7, 2021

Choose a reason for hiding this comment

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

If the localStorage is removed during runtime, it will fail. Using a getter avoid this case. What about using a method "supportsPersistency"

Copy link
Member

Choose a reason for hiding this comment

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

What is the case when the localStorage is removed during runtime? I'd say that's an edge-case scenario that indicates an error in the runtime, but I may be wrong.

Copy link
Author

Choose a reason for hiding this comment

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

I don't have any idea of what can be the case.

But this is technically possible.

By the way, if we store the check in a const as you recommend, I'll need extra time to find a way to test undefined localStorage. Because it cannot work as it is currently done

Copy link
Author

Choose a reason for hiding this comment

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

@kettanaito changes done with const SUPPORTS_LOCAL_STORAGE

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Thank you for working on this, @neilime! Please, could you look into the comments I left?
It'd be great to separate the prettier changes from the actual fix, so we have a cleaner Git history. Thanks!

store.persist()

localStorage = originalLocalStorage
expect(localStorage.getItem(PERSISTENCY_KEY)).toEqual(null)
Copy link
Member

Choose a reason for hiding this comment

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

Despite the cookies not being persisted in the local storage, I think we should still assert that they persist in the internal this.store map when you try to retrieve them.

Copy link
Author

Choose a reason for hiding this comment

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

@neilime
Copy link
Author

neilime commented May 27, 2021

🆙 😄

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Thank you for shipping this improvement, @neilime!

@kettanaito kettanaito merged commit 7a043c6 into mswjs:master May 28, 2021
@neilime neilime deleted the fix/undefined-local-storage branch May 28, 2021 11:11
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.

CookieStore.hydrate fails when running in Node.js env because localStorage is undefined
2 participants