Skip to content

Commit ff9b44d

Browse files
committed
refactor: using async storage in the local runtime context and folder structure
1 parent 5a08c6c commit ff9b44d

File tree

9 files changed

+38
-262
lines changed

9 files changed

+38
-262
lines changed

lib/build/bundlers/polyfills/polyfills-manager.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ class PolyfillsManager {
146146
this.setAlias('util', require.resolve('util/'));
147147
this.setAlias('process', `${nodePolyfillsPath}/globals/process.js`);
148148

149-
this.setExternal('async_hooks', `${externalPolyfillsPath}/async_hooks.js`);
149+
this.setExternal(
150+
'async_hooks',
151+
`${externalPolyfillsPath}/async_hooks/index.js`,
152+
);
150153

151154
return {
152155
libs: this.libs,

lib/env/polyfills/async_hooks.js

-257
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-disable */
2+
import * as async_hooks from 'async_hooks';
3+
4+
export class AsyncLocalStorage extends async_hooks.AsyncLocalStorage {}
5+
export class AsyncResource extends async_hooks.AsyncResource {}
6+
7+
export default {
8+
AsyncLocalStorage,
9+
AsyncResource,
10+
};
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint-disable */
2+
/**
3+
* ASYNC_LOCAL_STORAGE is defined in runtime.env.js for use on the local server
4+
*/
5+
6+
export class AsyncLocalStorage extends ASYNC_LOCAL_STORAGE.AsyncLocalStorage {}
7+
export class AsyncResource extends ASYNC_LOCAL_STORAGE.AsyncResource {}
8+
9+
export default {
10+
AsyncLocalStorage,
11+
AsyncResource,
12+
};

lib/env/polyfills/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import fetchPolyfill from './fetch.polyfills.js';
2-
import FetchEventPolyfill from './FetchEvent.polyfills.js';
1+
import fetchPolyfill from './fetch/fetch.polyfills.js';
2+
import FetchEventPolyfill from './fetch/FetchEvent.polyfills.js';
3+
import AsyncHooks from './async_hooks/context/index.js';
34

4-
export { fetchPolyfill, FetchEventPolyfill };
5+
export { fetchPolyfill, FetchEventPolyfill, AsyncHooks };

lib/env/runtime.env.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { EdgeRuntime } from 'edge-runtime';
22

3-
import { fetchPolyfill, FetchEventPolyfill } from './polyfills/index.js';
3+
import {
4+
fetchPolyfill,
5+
FetchEventPolyfill,
6+
AsyncHooks,
7+
} from './polyfills/index.js';
48

59
/**
610
* Executes the specified JavaScript code within a sandbox environment,
@@ -56,6 +60,9 @@ function runtime(code) {
5660
context.WebSocket = undefined;
5761
/* ========================================================== */
5862

63+
// Async Hooks
64+
context.ASYNC_LOCAL_STORAGE = AsyncHooks;
65+
5966
return context;
6067
};
6168

0 commit comments

Comments
 (0)