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

feat: support usage in cloudflare workers #27

Merged
merged 7 commits into from
Nov 8, 2023
Merged

Conversation

mattcosta7
Copy link
Contributor

@mattcosta7 mattcosta7 commented Nov 6, 2023

Added a new test, using the miniflare jest-environment as well as some try/catch statements around access of properties not implemented in miniflare/cloudflare to support usage in those environments.

It seems cookies are accessible there, so we should work correctly, avoiding property access that is undefined in those environments.

miniflare will throw on access of request.credentials and as such it defines it, so we cannot safely check 'credentials' in request, and instead I've opted to try/catch around code that accesses properties like that

related to mswjs/msw#1834

@mattcosta7
Copy link
Contributor Author

any thoughts on updating node versions here? 14 support ended a while ago, and msw requires 18+ now.

BREAKING CHANGE: node 18 is now the minimum supported version
@mattcosta7
Copy link
Contributor Author

mattcosta7 commented Nov 6, 2023

May need to understand why CI fails here with an ENOENT: no such file or directory, open '/home/runner/work/cookies/cookies/lib/index.js'

tests are passing locally in node 18 and 20

src/store.ts Outdated Show resolved Hide resolved
src/store.ts Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
src/store.ts Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
@mattcosta7 mattcosta7 requested a review from kettanaito November 7, 2023 15:07
// Ignore errors thrown by `request.credentials` access, such as those in cloudflare workers
// We can't just check that `credentials` is in request, because it is in cloudflare workers test environment
// however access throws an error
if (
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 split this if into two as they handle 2 distinct use cases. What do you think?

Copy link
Contributor Author

@mattcosta7 mattcosta7 Nov 7, 2023

Choose a reason for hiding this comment

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

We only want to preform this omit check in the case that the credentials are accessible

We do still want to deal with cookies though if credentials aren't.

    if ( isPropertyAccessible(request, 'credentials')) {
       if (request.credentials === 'omit') {
          return 
       }
    }

we could do this instead - but i'm not sure the extra nesting gives us much? but I don't have strong feelings.

Copy link
Member

Choose a reason for hiding this comment

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

Why not:

// Ignore the cases when "request.credentials" is not implemented.
if (!isPropertyAccessible(request, 'credentials')) {
  return 
}

// Handle the scenario when "credentials" was set to "omit".
if (request.credentials === 'omit') {
  return
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kettanaito that check is diifferent than the one implemented here.

We do want to allow interacting with cookies when credentials are not implemented. We only want to check for omit when they are implemented.

the logic here is

  • if credentials can be accessed and they are omit, don't interact
  • if credentials is NOT implemented, or credentials are omit, return early

In your change we would return early if credentials are not implemented OR if they are omit. Which would fail to modify cookies in the case credentials are not implemented, which is not what we want in this case (as far as I can tell from a quick skimming of the docs on cloudflare - it is fine to interact with cookies there, there's just not credential check)

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see now. Apologies, took me a minute to wrap my head around this. Please, feel free to proceed with your latest suggestion, it looks great then.

tsconfig.json Outdated Show resolved Hide resolved
@kettanaito kettanaito merged commit a28051e into main Nov 8, 2023
2 checks passed
@kettanaito kettanaito deleted the support-miniflare branch November 8, 2023 13:08
@kettanaito
Copy link
Member

Released: v1.1.0 🎉

This has been released in v1.1.0!

Make sure to always update to the latest version (npm i @mswjs/cookies@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

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.

2 participants