question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make app mutation optional

See original GitHub issue

As discussed in #59, a reference to the Koa app is currently accepted so the middleware initializer can modify it for performance reasons. This makes sense, but it sort of couples the middleware to the app, making it tricky to use with other middleware consumers such as koa-router.

For example, if I want to use koa-router to define different behavior for different url paths-- some paths using sessions, others not. Even if I only attach the middleware for some paths, the ctx.session and ctx.sessionOpts getters will still exist on all context objects all paths, even though they shouldn’t.

In fact, omitting the middleware doesn’t even seem to remove much of the functionality at all. Without it, I could still get and commit session data from cookies as normal. Looking at the implementation, it seems like the middleware itself only really handles external storage initialization and the autoCommit option:

https://github.com/koajs/session/blob/10bb12246699101a0c87a2f3e2e09b1a79e10e33/index.js#L37-L50

I suppose the answer here is to just not access those properties where they’re not supposed to exist, but it feels messy and likely to confuse other developers who might work on my code who aren’t familiar with the internals of koa-session.

I’d like to use koa-session since it is clearly the most popular and well-maintained module in this space, but at the moment this is a huge blocker for me. I’d be willing to take the performance hit in order to avoid these problems, though I’m aware not everyone feels that way.

Would you guys be open to the possibility of making the app argument optional in the initializer, falling back to the slower way of doing things if it is not provided? I imagine it will be a decent amount of work to make this change, but I’m happy to do it if you’d be receptive to it.

If not I’ll just save my time and use koa-session2. :\

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
zavr-1commented, Dec 22, 2019

@sripberger you can use this forke https://github.com/idiocc/session you’re welcome

1reaction
zavr-1commented, Dec 22, 2019

@sripberger is 100% right the middleware should not be coupled to the app, it’s really easy to change the emit logic to use ctx.app. Otherwise, it’s confusing, how the session can be accessed via ctx.session on routes where session was not even installed. what are performance benefits anyway, the current logic defines session symbol on the context constructor but how much gain is there from that it would be a breaking change though https://github.com/koajs/session/blob/f81d7130ff4fd44ad97bd0b807034bd46964db8a/test/cookie.test.js#L668-L695

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mutations in Apollo Client - Apollo GraphQL Docs
This article assumes you're familiar with building basic GraphQL mutations. If you need a refresher, we recommend that you read this guide.
Read more >
Provision from schema (optional) - AWS AppSync
After you create an AWS AppSync API, go to the Schema page and choose Create Resources. ... For example, create a blog post...
Read more >
Querying with GraphQL optional arguments in Elm - Thoughtbot
Building a GraphQL query in Elm. For an evergreen To Do app, our GraphQL mutation to create a new Todo might look something...
Read more >
Mutations | Redux Toolkit
Mutation endpoints are defined by returning an object inside the endpoints section of createApi , and defining the fields using the build.
Read more >
GraphQL-js mutation optional arguments - Stack Overflow
mutation : new GraphQLObjectType({ name: 'adsF', fields: { updateCount: { type: GraphQLInt, args: { count: { type: GraphQLInt } // I want to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found