-
-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use random internal server port
- Loading branch information
1 parent
cfc120c
commit 310a2c0
Showing
10 changed files
with
243 additions
and
218 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
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 |
---|---|---|
@@ -1,16 +1,58 @@ | ||
import { invariant } from 'outvariant' | ||
import { remoteHandlersContext } from './setupRemoteServer' | ||
|
||
export const remoteContext = { | ||
variableName: 'MSW_REMOTE_CONTEXT_ID', | ||
getContextId() { | ||
const store = remoteHandlersContext.getStore() | ||
|
||
invariant( | ||
store != null, | ||
'Failed to call ".getContextId()" on remote context: no context found. Did you call this outside of the `remote.boundary()` scope?', | ||
) | ||
|
||
return store.contextId | ||
}, | ||
export const MSW_REMOTE_SERVER_URL = 'MSW_REMOTE_SERVER_URL' | ||
export const MSW_REMOTE_BOUNDARY_ID = 'MSW_REMOTE_BOUNDARY_ID' | ||
|
||
export interface RemoteContext { | ||
serverUrl: URL | ||
boundary: { | ||
id: string | ||
} | ||
} | ||
|
||
export function getRemoteContext(): RemoteContext { | ||
const store = remoteHandlersContext.getStore() | ||
|
||
invariant( | ||
store, | ||
'Failed to retrieve remote context: no context found. Did you forget to call this within a `remote.boundary()`?', | ||
) | ||
|
||
return { | ||
serverUrl: store.serverUrl, | ||
boundary: { | ||
id: store.boundaryId, | ||
}, | ||
} | ||
} | ||
|
||
export function getRemoteContextFromEnvironment(): RemoteContext { | ||
const serverUrl = process.env[MSW_REMOTE_SERVER_URL] | ||
const boundaryId = process.env[MSW_REMOTE_BOUNDARY_ID] | ||
|
||
invariant( | ||
serverUrl, | ||
'Failed to retrieve the remote context from environment: server URL is missing', | ||
) | ||
invariant( | ||
boundaryId, | ||
'Failed to retrieve the remote context from environment: boundary ID is missing', | ||
) | ||
|
||
return { | ||
serverUrl: new URL(serverUrl), | ||
boundary: { | ||
id: boundaryId, | ||
}, | ||
} | ||
} | ||
|
||
export function getRemoteEnvironment() { | ||
const remoteContext = getRemoteContext() | ||
|
||
return { | ||
[MSW_REMOTE_SERVER_URL]: remoteContext.serverUrl.toString(), | ||
[MSW_REMOTE_BOUNDARY_ID]: remoteContext.boundary.id, | ||
} | ||
} |
Oops, something went wrong.