Skip to content

Commit e507301

Browse files
authored
feat: add AsyncLocalStorage snapshot polyfill and zlib inflateSync support (#317)
- Added asyncStorage global polyfill with snapshot method implementation for AsyncLocalStorage - Implemented zlib polyfill with inflateSync function using browserify-zlib - Registered new polyfills in unenv preset configuration with proper aliases
1 parent 4159111 commit e507301

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

packages/unenv-preset/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default {
1616
setInterval: `${polyfillsPath}/node/globals/set-interval.js`,
1717
clearInterval: `${polyfillsPath}/node/globals/clear-interval.js`,
1818
console: `${polyfillsPath}/node/globals/console.js`,
19+
asyncStorage: `${polyfillsPath}/node/globals/async-storage.js`,
1920
},
2021
alias: {
2122
'azion/utils': 'azion/utils',
@@ -29,6 +30,7 @@ export default {
2930
string_decoder: 'string_decoder/lib/string_decoder.js',
3031
timers: 'timers-browserify/',
3132
util: `${polyfillsPath}/node/util.js`,
33+
zlib: `${polyfillsPath}/node/zlib.js`,
3234
},
3335
external: ['node:async_hooks', 'node:fs/promises', 'node:stream', 'node:crypto'],
3436
polyfill: [
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import async_hooks from 'async_hooks';
2+
// Implement snapshot for AsyncLocalStorage
3+
if (async_hooks.AsyncLocalStorage && !async_hooks.AsyncLocalStorage.prototype.snapshot) {
4+
async_hooks.AsyncLocalStorage.prototype.snapshot = function () {
5+
const store = this.getStore();
6+
return () => store;
7+
};
8+
}
9+
// Also add snapshot as a static method if needed
10+
if (async_hooks.AsyncLocalStorage && !async_hooks.AsyncLocalStorage.snapshot) {
11+
async_hooks.AsyncLocalStorage.snapshot = () => {
12+
return (fn, ...args) => {
13+
if (typeof fn === 'function') {
14+
const result = fn(...args);
15+
return result;
16+
}
17+
return fn;
18+
};
19+
};
20+
}
21+
22+
export default async_hooks.AsyncLocalStorage;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { inflateSync as inflateSyncBrowserify } from 'browserify-zlib';
2+
import * as originalZlib from 'unenv/node/zlib';
3+
4+
export function inflateSync(buffer, options = {}) {
5+
return inflateSyncBrowserify(buffer, options);
6+
}
7+
8+
export default {
9+
...originalZlib.default,
10+
inflateSync,
11+
};

0 commit comments

Comments
 (0)