@tanstack/start-storage-context
Advanced tools
| import { AsyncLocalStorage } from "node:async_hooks"; | ||
| const GLOBAL_STORAGE_KEY = /* @__PURE__ */ Symbol.for("tanstack-start:start-storage-context"); | ||
| const globalObj = globalThis; | ||
| if (!globalObj[GLOBAL_STORAGE_KEY]) { | ||
| globalObj[GLOBAL_STORAGE_KEY] = new AsyncLocalStorage(); | ||
| } | ||
| const startStorage = globalObj[GLOBAL_STORAGE_KEY]; | ||
| //#region src/async-local-storage.ts | ||
| var GLOBAL_STORAGE_KEY = Symbol.for("tanstack-start:start-storage-context"); | ||
| var globalObj = globalThis; | ||
| if (!globalObj[GLOBAL_STORAGE_KEY]) globalObj[GLOBAL_STORAGE_KEY] = new AsyncLocalStorage(); | ||
| var startStorage = globalObj[GLOBAL_STORAGE_KEY]; | ||
| async function runWithStartContext(context, fn) { | ||
| return startStorage.run(context, fn); | ||
| return startStorage.run(context, fn); | ||
| } | ||
| function getStartContext(opts) { | ||
| const context = startStorage.getStore(); | ||
| if (!context && opts?.throwIfNotFound !== false) { | ||
| throw new Error( | ||
| `No Start context found in AsyncLocalStorage. Make sure you are using the function within the server runtime.` | ||
| ); | ||
| } | ||
| return context; | ||
| const context = startStorage.getStore(); | ||
| if (!context && opts?.throwIfNotFound !== false) throw new Error(`No Start context found in AsyncLocalStorage. Make sure you are using the function within the server runtime.`); | ||
| return context; | ||
| } | ||
| export { | ||
| getStartContext, | ||
| runWithStartContext | ||
| }; | ||
| //# sourceMappingURL=async-local-storage.js.map | ||
| //#endregion | ||
| export { getStartContext, runWithStartContext }; | ||
| //# sourceMappingURL=async-local-storage.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"async-local-storage.js","sources":["../../src/async-local-storage.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks'\nimport type { Awaitable, RegisteredRouter } from '@tanstack/router-core'\n\nexport interface StartStorageContext {\n getRouter: () => Awaitable<RegisteredRouter>\n request: Request\n // TODO type this properly\n startOptions: /* AnyStartInstanceOptions*/ any\n\n contextAfterGlobalMiddlewares: any\n // Track middlewares that have already executed in the request phase\n // to prevent duplicate execution\n executedRequestMiddlewares: Set<any>\n}\n\n// Use a global symbol to ensure the same AsyncLocalStorage instance is shared\n// across different bundles that may each bundle this module.\nconst GLOBAL_STORAGE_KEY = Symbol.for('tanstack-start:start-storage-context')\n\nconst globalObj = globalThis as typeof globalThis & {\n [GLOBAL_STORAGE_KEY]?: AsyncLocalStorage<StartStorageContext>\n}\n\nif (!globalObj[GLOBAL_STORAGE_KEY]) {\n globalObj[GLOBAL_STORAGE_KEY] = new AsyncLocalStorage<StartStorageContext>()\n}\n\nconst startStorage = globalObj[GLOBAL_STORAGE_KEY]\n\nexport async function runWithStartContext<T>(\n context: StartStorageContext,\n fn: () => T | Promise<T>,\n): Promise<T> {\n return startStorage.run(context, fn)\n}\n\nexport function getStartContext<TThrow extends boolean = true>(opts?: {\n throwIfNotFound?: TThrow\n}): TThrow extends false\n ? StartStorageContext | undefined\n : StartStorageContext {\n const context = startStorage.getStore()\n if (!context && opts?.throwIfNotFound !== false) {\n throw new Error(\n `No Start context found in AsyncLocalStorage. Make sure you are using the function within the server runtime.`,\n )\n }\n return context as any\n}\n"],"names":[],"mappings":";AAiBA,MAAM,qBAAqB,uBAAO,IAAI,sCAAsC;AAE5E,MAAM,YAAY;AAIlB,IAAI,CAAC,UAAU,kBAAkB,GAAG;AAClC,YAAU,kBAAkB,IAAI,IAAI,kBAAA;AACtC;AAEA,MAAM,eAAe,UAAU,kBAAkB;AAEjD,eAAsB,oBACpB,SACA,IACY;AACZ,SAAO,aAAa,IAAI,SAAS,EAAE;AACrC;AAEO,SAAS,gBAA+C,MAIvC;AACtB,QAAM,UAAU,aAAa,SAAA;AAC7B,MAAI,CAAC,WAAW,MAAM,oBAAoB,OAAO;AAC/C,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAEJ;AACA,SAAO;AACT;"} | ||
| {"version":3,"file":"async-local-storage.js","names":[],"sources":["../../src/async-local-storage.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks'\nimport type { Awaitable, RegisteredRouter } from '@tanstack/router-core'\n\nexport interface StartStorageContext {\n getRouter: () => Awaitable<RegisteredRouter>\n request: Request\n // TODO type this properly\n startOptions: /* AnyStartInstanceOptions*/ any\n\n contextAfterGlobalMiddlewares: any\n // Track middlewares that have already executed in the request phase\n // to prevent duplicate execution\n executedRequestMiddlewares: Set<any>\n}\n\n// Use a global symbol to ensure the same AsyncLocalStorage instance is shared\n// across different bundles that may each bundle this module.\nconst GLOBAL_STORAGE_KEY = Symbol.for('tanstack-start:start-storage-context')\n\nconst globalObj = globalThis as typeof globalThis & {\n [GLOBAL_STORAGE_KEY]?: AsyncLocalStorage<StartStorageContext>\n}\n\nif (!globalObj[GLOBAL_STORAGE_KEY]) {\n globalObj[GLOBAL_STORAGE_KEY] = new AsyncLocalStorage<StartStorageContext>()\n}\n\nconst startStorage = globalObj[GLOBAL_STORAGE_KEY]\n\nexport async function runWithStartContext<T>(\n context: StartStorageContext,\n fn: () => T | Promise<T>,\n): Promise<T> {\n return startStorage.run(context, fn)\n}\n\nexport function getStartContext<TThrow extends boolean = true>(opts?: {\n throwIfNotFound?: TThrow\n}): TThrow extends false\n ? StartStorageContext | undefined\n : StartStorageContext {\n const context = startStorage.getStore()\n if (!context && opts?.throwIfNotFound !== false) {\n throw new Error(\n `No Start context found in AsyncLocalStorage. Make sure you are using the function within the server runtime.`,\n )\n }\n return context as any\n}\n"],"mappings":";;AAiBA,IAAM,qBAAqB,OAAO,IAAI,uCAAuC;AAE7E,IAAM,YAAY;AAIlB,IAAI,CAAC,UAAU,oBACb,WAAU,sBAAsB,IAAI,mBAAwC;AAG9E,IAAM,eAAe,UAAU;AAE/B,eAAsB,oBACpB,SACA,IACY;AACZ,QAAO,aAAa,IAAI,SAAS,GAAG;;AAGtC,SAAgB,gBAA+C,MAIvC;CACtB,MAAM,UAAU,aAAa,UAAU;AACvC,KAAI,CAAC,WAAW,MAAM,oBAAoB,MACxC,OAAM,IAAI,MACR,+GACD;AAEH,QAAO"} |
| import { getStartContext, runWithStartContext } from "./async-local-storage.js"; | ||
| export { | ||
| getStartContext, | ||
| runWithStartContext | ||
| }; | ||
| //# sourceMappingURL=index.js.map | ||
| export { getStartContext, runWithStartContext }; |
+2
-2
| { | ||
| "name": "@tanstack/start-storage-context", | ||
| "version": "1.166.9", | ||
| "version": "1.166.10", | ||
| "description": "Modern and scalable routing for React applications", | ||
@@ -46,3 +46,3 @@ "author": "Tanner Linsley", | ||
| "dependencies": { | ||
| "@tanstack/router-core": "1.167.1" | ||
| "@tanstack/router-core": "1.167.2" | ||
| }, | ||
@@ -49,0 +49,0 @@ "devDependencies": { |
| {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"} |
9300
-2.06%10
-9.09%75
-13.79%+ Added
+ Added
- Removed
- Removed