@tanstack/start-storage-context 1.141.8 → 1.142.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,10 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
- const startStorage = new AsyncLocalStorage();
2
+ const GLOBAL_STORAGE_KEY = Symbol.for("tanstack-start:start-storage-context");
3
+ const globalObj = globalThis;
4
+ if (!globalObj[GLOBAL_STORAGE_KEY]) {
5
+ globalObj[GLOBAL_STORAGE_KEY] = new AsyncLocalStorage();
6
+ }
7
+ const startStorage = globalObj[GLOBAL_STORAGE_KEY];
3
8
  async function runWithStartContext(context, fn) {
4
9
  return startStorage.run(context, fn);
5
10
  }
@@ -1 +1 @@
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}\n\nconst startStorage = new AsyncLocalStorage<StartStorageContext>()\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":";AAYA,MAAM,eAAe,IAAI,kBAAA;AAEzB,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;"}
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}\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":";AAcA,MAAM,qBAAqB,OAAO,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;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/start-storage-context",
3
- "version": "1.141.8",
3
+ "version": "1.142.1",
4
4
  "description": "Modern and scalable routing for React applications",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -10,7 +10,19 @@ export interface StartStorageContext {
10
10
  contextAfterGlobalMiddlewares: any
11
11
  }
12
12
 
13
- const startStorage = new AsyncLocalStorage<StartStorageContext>()
13
+ // Use a global symbol to ensure the same AsyncLocalStorage instance is shared
14
+ // across different bundles that may each bundle this module.
15
+ const GLOBAL_STORAGE_KEY = Symbol.for('tanstack-start:start-storage-context')
16
+
17
+ const globalObj = globalThis as typeof globalThis & {
18
+ [GLOBAL_STORAGE_KEY]?: AsyncLocalStorage<StartStorageContext>
19
+ }
20
+
21
+ if (!globalObj[GLOBAL_STORAGE_KEY]) {
22
+ globalObj[GLOBAL_STORAGE_KEY] = new AsyncLocalStorage<StartStorageContext>()
23
+ }
24
+
25
+ const startStorage = globalObj[GLOBAL_STORAGE_KEY]
14
26
 
15
27
  export async function runWithStartContext<T>(
16
28
  context: StartStorageContext,