-
Notifications
You must be signed in to change notification settings - Fork 980
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(middleware): Add support for Middleware to SSR-Streaming server #9883
Merged
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
a560620
Add code for invoking middleware (WIP)
dac09 17ab5f7
Fix links in suspense apollo provider for backwards compatibility
dac09 84279f7
WIP: Start adding MW classes
dac09 f0e0f38
Use subclassing to create MiddlewareRequest
dac09 07207bc
WIP: Try extending WhatwgRequest instead
dac09 eec9e3e
Bump whatwg versions
dac09 8d64b1c
Add some more tests for MWReq, MWRes
dac09 7cac8aa
Merge branch 'main' of github.com:redwoodjs/redwood into feat/ssr-mid…
dac09 7e51740
Update packages to make tests pass
dac09 29cebfd
CookieJar unsetting a cookie
dac09 65f221a
Add test for multiple set cookies
dac09 4a82d78
Remove old encrypted session from ServerAuthState
dac09 85ba98c
Export middleware classes
dac09 e579c9d
Fix TS errors in apollo
dac09 93eaadd
Improve implementations of mwReq and mwRes
dac09 9c01b3e
Use classes while invoking middleware
dac09 57e7cb4
Update mRes implementation
dac09 8090968
Consistent version of whatwg fetch
dac09 cc16e07
Readd commented line
dac09 9b27895
Add test for constructor and perm redirects
dac09 bfb39ff
Send Response back from rwmw endpoint
dac09 4717aae
Default auth state in serverAuthContext
dac09 47705c2
Comment
dac09 30759dd
Move credentials include
dac09 9dff2d9
Merge branch 'main' into feat/ssr-middleware
dac09 9182742
Update yarn.lock
dac09 0029c2c
Merge branch 'main' into feat/ssr-middleware
dac09 20b18d8
Cleanup comments in AuthProvider
dac09 ac82e57
Apply suggestions from Tobbe's review
dac09 2a5b72d
Apply suggestions from Tobbe's review
dac09 6e89b52
Merge branch 'feat/ssr-middleware' of github.com:dac09/redwood into f…
dac09 5b46aec
Update packages/auth/src/AuthProvider/AuthProvider.tsx
dac09 78879a6
Implement middleware invoke helper
dac09 3d14b51
Merge branch 'feat/ssr-middleware' of github.com:dac09/redwood into f…
dac09 dcc50df
Rename cookieJar.delete to unset
dac09 a203a19
Also invoke middleware in prod fe server
dac09 beef260
Update packages/auth/src/AuthProvider/ServerAuthProvider.tsx
dac09 fda6e53
Merge branch 'main' into feat/ssr-middleware
Tobbe 967a6c2
fix syntax error (extra })
Tobbe 68250c2
Use relative import path
Tobbe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import type { ReactNode } from 'react' | ||
import React from 'react' | ||
|
||
import type { AuthProviderState } from './AuthProviderState' | ||
import { defaultAuthProviderState } from './AuthProviderState' | ||
|
||
export type ServerAuthState = AuthProviderState<never> & { | ||
cookieHeader?: string | ||
} | ||
|
||
const getAuthInitialStateFromServer = () => { | ||
if (globalThis?.__REDWOOD__SERVER__AUTH_STATE__) { | ||
const initialState = { | ||
...defaultAuthProviderState, | ||
encryptedSession: null, | ||
...(globalThis?.__REDWOOD__SERVER__AUTH_STATE__ || {}), | ||
} | ||
// Clear it so we don't accidentally use it again | ||
globalThis.__REDWOOD__SERVER__AUTH_STATE__ = null | ||
return initialState | ||
} | ||
|
||
// Already restored | ||
return null | ||
} | ||
|
||
/** | ||
* On the server, it resolves to the defaultAuthProviderState first. | ||
* | ||
* On the client it restores from the initial server state injected in the ServerAuthProvider | ||
*/ | ||
export const ServerAuthContext = React.createContext<ServerAuthState>( | ||
getAuthInitialStateFromServer() | ||
) | ||
|
||
/** | ||
* Note: This only gets rendered on the server and serves two purposes: | ||
* 1) On the server, it sets the auth state | ||
* 2) On the client, it restores the auth state from the initial server render | ||
*/ | ||
export const ServerAuthProvider = ({ | ||
value, | ||
children, | ||
}: { | ||
value: ServerAuthState | ||
children?: ReactNode[] | ||
}) => { | ||
// @NOTE: we "Sanitize" to remove cookieHeader | ||
// not totally necessary, but it's nice to not have them in the DOM | ||
// @MARK: needs discussion! | ||
const stringifiedAuthState = `__REDWOOD__SERVER__AUTH_STATE__ = ${JSON.stringify( | ||
sanitizeServerAuthState(value) | ||
)};` | ||
|
||
return ( | ||
<> | ||
<script | ||
id="__REDWOOD__SERVER_AUTH_STATE__" | ||
dangerouslySetInnerHTML={{ | ||
__html: stringifiedAuthState, | ||
}} | ||
/> | ||
|
||
<ServerAuthContext.Provider value={value}> | ||
{children} | ||
</ServerAuthContext.Provider> | ||
</> | ||
) | ||
} | ||
function sanitizeServerAuthState(value: ServerAuthState) { | ||
const sanitizedState = { ...value } | ||
// Remove the cookie from being printed onto the DOM | ||
// harmless, but still... | ||
delete sanitizedState.cookieHeader | ||
|
||
return sanitizedState | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
/* to keep the tests a little cleaner by using ! */ | ||
|
||
import { describe, expect, test } from 'vitest' | ||
|
||
import { CookieJar } from './CookieJar' | ||
|
||
describe('CookieJar', () => { | ||
// Grabbed from github | ||
const cookieJar = new CookieJar( | ||
'color_mode=%7B%22color_mode%22%3A%22light%22%2C%22light_theme%22%3A%7B%22name%22%3A%22light%22%2C%22color_mode%22%3A%22light%22%7D%2C%22dark_theme%22%3A%7B%22name%22%3A%22dark_dimmed%22%2C%22color_mode%22%3A%22dark%22%7D%7D; preferred_color_mode=dark; tz=Asia%2FBangkok' | ||
) | ||
|
||
test('instatitates cookie jar from a cookie string', () => { | ||
expect(cookieJar.get('color_mode')).toStrictEqual({ | ||
value: JSON.stringify({ | ||
color_mode: 'light', | ||
light_theme: { name: 'light', color_mode: 'light' }, | ||
dark_theme: { name: 'dark_dimmed', color_mode: 'dark' }, | ||
}), | ||
}) | ||
|
||
expect(cookieJar.get('preferred_color_mode')).toStrictEqual({ | ||
value: 'dark', | ||
}) | ||
|
||
expect(cookieJar.get('tz')).toStrictEqual({ | ||
value: 'Asia/Bangkok', | ||
}) | ||
}) | ||
|
||
describe('Helper methods like JS Map', () => { | ||
test('has', () => { | ||
expect(cookieJar.has('color_mode')).toBe(true) | ||
expect(cookieJar.has('bazinga')).toBe(false) | ||
}) | ||
|
||
test('size', () => { | ||
expect(cookieJar.size).toBe(3) | ||
}) | ||
|
||
test('entries', () => { | ||
const iterator = cookieJar.entries() | ||
|
||
expect(iterator.next().done).toBe(false) | ||
expect(iterator.next().done).toBe(false) | ||
|
||
const finalItem = iterator.next() | ||
expect(finalItem.done).toBe(false) | ||
expect(finalItem.value).toStrictEqual(['tz', { value: 'Asia/Bangkok' }]) | ||
|
||
expect(iterator.next().done).toBe(true) | ||
}) | ||
|
||
// @MARK: API convention worth discussing! | ||
// Unset is a little special, it doesn't actually delete the cookie | ||
// but sets it to expire and sets an empty value | ||
test('unset', () => { | ||
const myJar = new CookieJar('auth_provider=kittens; session=woof-124556') | ||
|
||
myJar.unset('auth_provider') | ||
|
||
const { value: authProviderValue, options: authProviderOptions } = | ||
myJar.get('auth_provider')! | ||
|
||
expect(authProviderValue).toBeFalsy() | ||
expect(authProviderOptions!.expires).toStrictEqual(new Date(0)) | ||
}) | ||
|
||
test('clear All', () => { | ||
const myJar = new CookieJar('auth_provider=kittens; session=woof-124556') | ||
myJar.clear() | ||
expect(myJar.size).toBe(0) | ||
}) | ||
|
||
test('clear by name', () => { | ||
const myJar = new CookieJar('auth_provider=kittens; session=woof-124556') | ||
myJar.clear('session') | ||
expect(myJar.size).toBe(1) | ||
expect(myJar.get('session')).toBeUndefined() | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What needs discussion? Can you and I discuss? I want to resolve this before merging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just want to call this out for awareness. It's harmless right now, but its possible a contribution or new code could leak information here.