-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check for an implementation of AsyncLocalStorage (#866)
Add a runtime check for `wrangler dev` local mode to avoid erroring in environments with no `AsyncLocalStorage` class Certain runtime APIs are only available to workers during the "request context", which is any code that returns after receiving a request and before returning a response. Miniflare emulates this behavior by using an [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage) and [checking at runtime](https://github.com/cloudflare/miniflare/blob/master/packages/shared/src/context.ts#L21-L36) to see if you're using those APIs during the request context. In certain environments `AsyncLocalStorage` is unavailable, such as in a [webcontainer](https://github.com/stackblitz/webcontainer-core). This function figures out if we're able to run those "request context" checks and returns [a set of options](https://miniflare.dev/core/standards#global-functionality-limits) that indicate to miniflare whether to run the checks or not.
- Loading branch information
Cass Fridkin
authored
May 2, 2022
1 parent
0a79d75
commit 8b227fc
Showing
4 changed files
with
67 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
Add a runtime check for `wrangler dev` local mode to avoid erroring in environments with no `AsyncLocalStorage` class | ||
|
||
Certain runtime APIs are only available to workers during the "request context", | ||
which is any code that returns after receiving a request and before returning | ||
a response. | ||
|
||
Miniflare emulates this behavior by using an [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage) and | ||
[checking at runtime](https://github.com/cloudflare/miniflare/blob/master/packages/shared/src/context.ts#L21-L36) | ||
to see if you're using those APIs during the request context. | ||
|
||
In certain environments `AsyncLocalStorage` is unavailable, such as in a | ||
[webcontainer](https://github.com/stackblitz/webcontainer-core). | ||
This function figures out if we're able to run those "request context" checks | ||
and returns [a set of options](https://miniflare.dev/core/standards#global-functionality-limits) | ||
that indicate to miniflare whether to run the checks or not. |
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,40 @@ | ||
import type { MiniflareOptions } from "miniflare"; | ||
|
||
/** | ||
* Certain runtime APIs are only available to workers during the "request context", | ||
* which is any code that returns after receiving a request and before returning | ||
* a response. | ||
* | ||
* Miniflare emulates this behavior by using an [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage) and | ||
* [checking at runtime](https://github.com/cloudflare/miniflare/blob/master/packages/shared/src/context.ts#L21-L36) | ||
* to see if you're using those APIs during the request context. | ||
* | ||
* In certain environments `AsyncLocalStorage` is unavailable, such as in a | ||
* [webcontainer](https://github.com/stackblitz/webcontainer-core). | ||
* This function figures out if we're able to run those "request context" checks | ||
* and returns [a set of options](https://miniflare.dev/core/standards#global-functionality-limits) | ||
* that indicate to miniflare whether to run the checks or not. | ||
*/ | ||
export const getRequestContextCheckOptions = async (): Promise< | ||
Pick<MiniflareOptions, "globalAsyncIO" | "globalTimers" | "globalRandom"> | ||
> => { | ||
// check that there's an implementation of AsyncLocalStorage | ||
let hasAsyncLocalStorage = true; | ||
try { | ||
// ripped from the example here https://nodejs.org/api/async_context.html#class-asynclocalstorage | ||
const { AsyncLocalStorage } = await import("node:async_hooks"); | ||
const storage = new AsyncLocalStorage<string>(); | ||
|
||
hasAsyncLocalStorage = storage.run("some-value", () => { | ||
return storage.getStore() === "some-value"; | ||
}); | ||
} catch (e) { | ||
hasAsyncLocalStorage = false; | ||
} | ||
|
||
return { | ||
globalAsyncIO: hasAsyncLocalStorage, | ||
globalRandom: hasAsyncLocalStorage, | ||
globalTimers: hasAsyncLocalStorage, | ||
}; | ||
}; |
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