@nextwrappers/async-local-storage
Version:
Middleware wrapper for Next.js Route Handlers that executes requests within an AsyncLocalStorage context.
125 lines (117 loc) • 3.78 kB
JavaScript
var async_hooks = require('async_hooks');
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// ../core/dist/index.js
var require_dist = __commonJS({
"../core/dist/index.js"(exports) {
function wrapper2(cb) {
return function(handler) {
return (req, ext) => {
return cb(
(_req, _ext) => handler(
_req || req,
_ext || ext
),
req,
ext
);
};
};
}
function merge(a, b) {
return function(handler) {
return a(b(handler));
};
}
function stack(a) {
const _stack = (handler) => a(handler);
_stack.kind = "stack";
_stack.with = (b) => stack(merge(a, b));
return _stack;
}
function chain(a) {
const _chain = (handler) => a(handler);
_chain.kind = "chain";
_chain.with = (b) => chain(merge(b, a));
return _chain;
}
function wrapperM(cb) {
return function(middleware) {
return (req) => {
return cb(
(_req) => middleware(_req || req),
req
);
};
};
}
function mergeM(a, b) {
return function(middleware) {
return a(b(middleware));
};
}
function stackM(a) {
const _stack = (middleware) => a(middleware);
_stack.kind = "stack";
_stack.with = (b) => stackM(mergeM(a, b));
return _stack;
}
function chainM(a) {
const _chain = (middleware) => a(middleware);
_chain.kind = "chain";
_chain.with = (b) => chainM(mergeM(b, a));
return _chain;
}
exports.chain = chain;
exports.chainM = chainM;
exports.merge = merge;
exports.mergeM = mergeM;
exports.stack = stack;
exports.stackM = stackM;
exports.wrapper = wrapper2;
exports.wrapperM = wrapperM;
}
});
// src/index.ts
var import_core = __toESM(require_dist());
// src/shared.ts
function runWithAsyncLocalStorage(storage, store, callback, args) {
return storage.run(store, callback, ...args);
}
// src/index.ts
function asyncLocalStorage(options) {
const { initialize, storage = new async_hooks.AsyncLocalStorage() } = options;
return {
storage,
getStore: () => storage.getStore(),
wrapper: (0, import_core.wrapper)((next, req, ext) => {
const store = initialize?.(req, ext);
return runWithAsyncLocalStorage(storage, store, next, [req, ext]);
})
};
}
exports.asyncLocalStorage = asyncLocalStorage;
;