-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix iron-session worker support (#1091)
- Loading branch information
Showing
9 changed files
with
137 additions
and
15 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import { enableFetchMocks } from 'jest-fetch-mock'; | ||
import { Crypto } from '@peculiar/webcrypto'; | ||
import { WorkOS } from './src/workos'; | ||
import { WebIronSessionProvider } from './src/common/iron-session/web-iron-session-provider'; | ||
|
||
enableFetchMocks(); | ||
|
||
// Assign Node's Crypto to global.crypto if it is not already present | ||
if (!global.crypto) { | ||
global.crypto = new Crypto(); | ||
global.crypto = new Crypto(); | ||
} | ||
|
||
// For tests, we can use the WebIronSessionProvider | ||
WorkOS.prototype.createIronSessionProvider = jest | ||
.fn() | ||
.mockReturnValue(new WebIronSessionProvider()); |
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,24 @@ | ||
import { sealData, unsealData } from 'iron-session/edge'; | ||
import { | ||
IronSessionProvider, | ||
SealDataOptions, | ||
UnsealedDataType, | ||
} from './iron-session-provider'; | ||
|
||
/** | ||
* EdgeIronSessionProvider which uses the base iron-session seal/unseal methods. | ||
*/ | ||
export class EdgeIronSessionProvider extends IronSessionProvider { | ||
/** @override */ | ||
async sealData(data: unknown, options: SealDataOptions): Promise<string> { | ||
return sealData(data, options); | ||
} | ||
|
||
/** @override */ | ||
async unsealData<T = UnsealedDataType>( | ||
seal: string, | ||
options: SealDataOptions, | ||
): Promise<T> { | ||
return unsealData<T>(seal, options); | ||
} | ||
} |
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,27 @@ | ||
export type SealDataOptions = { | ||
password: | ||
| string | ||
| { | ||
[id: string]: string; | ||
}; | ||
ttl?: number | undefined; | ||
}; | ||
|
||
export type UnsealedDataType = Record<string, unknown>; | ||
|
||
/** | ||
* Interface encapsulating the sealData/unsealData methods for separate iron-session implementations. | ||
* | ||
* This allows for different implementations of the iron-session library to be used in | ||
* worker/edge vs. regular web environments, which is required because of the different crypto APIs available. | ||
* Once we drop support for Node 16 and upgrade to iron-session 8+, we can remove this abstraction as iron-session 8+ | ||
* handles this on its own. | ||
*/ | ||
export abstract class IronSessionProvider { | ||
abstract sealData(data: unknown, options: SealDataOptions): Promise<string>; | ||
|
||
abstract unsealData<T = UnsealedDataType>( | ||
seal: string, | ||
options: SealDataOptions, | ||
): Promise<T>; | ||
} |
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,24 @@ | ||
import { sealData, unsealData } from 'iron-session'; | ||
import { | ||
IronSessionProvider, | ||
SealDataOptions, | ||
UnsealedDataType, | ||
} from './iron-session-provider'; | ||
|
||
/** | ||
* WebIronSessionProvider which uses the base iron-session seal/unseal methods. | ||
*/ | ||
export class WebIronSessionProvider extends IronSessionProvider { | ||
/** @override */ | ||
async sealData(data: unknown, options: SealDataOptions): Promise<string> { | ||
return sealData(data, options); | ||
} | ||
|
||
/** @override */ | ||
async unsealData<T = UnsealedDataType>( | ||
seal: string, | ||
options: SealDataOptions, | ||
): Promise<T> { | ||
return unsealData<T>(seal, options); | ||
} | ||
} |
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