diff --git a/packages/start-client-core/src/createCsrfMiddleware.ts b/packages/start-client-core/src/createCsrfMiddleware.ts index b7152d44a9..6cdd1c0d13 100644 --- a/packages/start-client-core/src/createCsrfMiddleware.ts +++ b/packages/start-client-core/src/createCsrfMiddleware.ts @@ -1,5 +1,4 @@ -import { createIsomorphicFn } from '@tanstack/start-fn-stubs' -import { createMiddleware } from './createMiddleware' +import { createIsomorphicFn, createMiddleware } from '@tanstack/start-fn-stubs' import type { RequestMiddlewareAfterServer, RequestServerOptions, @@ -77,9 +76,9 @@ type CreateCsrfMiddleware = ( opts?: CsrfMiddlewareOptions, ) => RequestMiddlewareAfterServer<{}, undefined, undefined> -const innerCreateCsrfMiddleware: CreateCsrfMiddleware = (opts = {}) => { - const middleware = createMiddleware().server(async (ctx) => { - const csrfCtx = ctx as RequestServerOptions & typeof ctx +const innerCreateCsrfMiddleware = ((opts: any = {}) => { + const middleware = createMiddleware().server(async (ctx: any) => { + const csrfCtx = ctx as RequestServerOptions if (opts.filter && !(await opts.filter(csrfCtx))) { return ctx.next() @@ -97,7 +96,7 @@ const innerCreateCsrfMiddleware: CreateCsrfMiddleware = (opts = {}) => { } return middleware -} +}) as any as CreateCsrfMiddleware export const createCsrfMiddleware: CreateCsrfMiddleware = createIsomorphicFn().server(innerCreateCsrfMiddleware) as CreateCsrfMiddleware diff --git a/packages/start-fn-stubs/src/createMiddleware.ts b/packages/start-fn-stubs/src/createMiddleware.ts new file mode 100644 index 0000000000..6c21a57d93 --- /dev/null +++ b/packages/start-fn-stubs/src/createMiddleware.ts @@ -0,0 +1,16 @@ +export function createMiddleware(opts?: any, opts2?: any): any { + const options = { ...(opts2 || opts) } + const proxy = new Proxy( + {}, + { + get(_, prop) { + if (prop === 'options') return options + return (value: any) => { + options[prop] = value + return proxy + } + }, + }, + ) + return proxy +} diff --git a/packages/start-fn-stubs/src/index.ts b/packages/start-fn-stubs/src/index.ts index 5ca646d9d1..1cd9d1f96d 100644 --- a/packages/start-fn-stubs/src/index.ts +++ b/packages/start-fn-stubs/src/index.ts @@ -5,5 +5,6 @@ export { type ClientOnlyFn, type IsomorphicFnBase, } from './createIsomorphicFn' +export { createMiddleware } from './createMiddleware' export { createServerOnlyFn, createClientOnlyFn } from './envOnly' diff --git a/packages/start-plugin-core/src/start-compiler/compiler.ts b/packages/start-plugin-core/src/start-compiler/compiler.ts index 9a950c65e2..5b7459f86a 100644 --- a/packages/start-plugin-core/src/start-compiler/compiler.ts +++ b/packages/start-plugin-core/src/start-compiler/compiler.ts @@ -694,6 +694,7 @@ export class StartCompiler { ['createIsomorphicFn', 'IsomorphicFn'], ['createServerOnlyFn', 'ServerOnlyFn'], ['createClientOnlyFn', 'ClientOnlyFn'], + ['createMiddleware', 'Middleware'], ]), ) @@ -705,6 +706,7 @@ export class StartCompiler { ['createIsomorphicFn', 'IsomorphicFn'], ['createServerOnlyFn', 'ServerOnlyFn'], ['createClientOnlyFn', 'ClientOnlyFn'], + ['createMiddleware', 'Middleware'], ]), ) diff --git a/packages/start-plugin-core/src/vite/start-compiler-plugin/plugin.ts b/packages/start-plugin-core/src/vite/start-compiler-plugin/plugin.ts index d423798547..3130ca47c1 100644 --- a/packages/start-plugin-core/src/vite/start-compiler-plugin/plugin.ts +++ b/packages/start-plugin-core/src/vite/start-compiler-plugin/plugin.ts @@ -1,4 +1,3 @@ -import { AsyncLocalStorage } from 'node:async_hooks' import { VIRTUAL_MODULES } from '@tanstack/start-server-core' import { resolve as resolvePath } from 'pathe' import { @@ -47,20 +46,6 @@ type ModuleInvalidationEnvironment = { } } -type StartCompilerPluginContext = { - environment: { - name: string - mode: string - transformRequest: (url: string) => Promise - } - load: (options: { id: string }) => Promise<{ code?: string | null }> - resolve: ( - source: string, - importer?: string, - ) => Promise<{ id: string; external?: boolean | string } | null> - error: (message: string) => never -} - function invalidateMatchingFileModules( environment: ModuleInvalidationEnvironment, ids: Iterable, @@ -196,17 +181,6 @@ export function startCompilerPlugin( opts: StartCompilerPluginOptions, ): PluginOption { const compilers = new Map>() - const compilerContextStorage = - new AsyncLocalStorage() - - const getCompilerContext = () => { - const context = compilerContextStorage.getStore() - if (!context) { - throw new Error('Start compiler Vite context is unavailable.') - } - - return context - } // Shared registry of server functions across all environments const serverFnsById: Record = {} @@ -288,30 +262,27 @@ export function startCompilerPlugin( ? createViteDevServerFnModuleSpecifierEncoder(root) : undefined, loadModule: async (id: string) => { - const compilerContext = getCompilerContext() - if (mode === 'build') { - const loaded = await compilerContext.load({ id }) + const loaded = await this.load({ id }) const code = loaded.code ?? '' compiler!.ingestModule({ code, id }) return } - if (compilerContext.environment.mode !== 'dev') { - compilerContext.error( - `could not load module ${id}: unknown environment mode ${compilerContext.environment.mode}`, + if (this.environment.mode !== 'dev') { + this.error( + `could not load module ${id}: unknown environment mode ${this.environment.mode}`, ) } - await compilerContext.environment.transformRequest( + await this.environment.transformRequest( `${id}?${SERVER_FN_LOOKUP}`, ) }, resolveId: async (source: string, importer?: string) => { - const compilerContext = getCompilerContext() - const r = await compilerContext.resolve(source, importer) + const r = await this.resolve(source, importer) if (r) { if (!r.external) { @@ -331,15 +302,11 @@ export function startCompilerPlugin( compilerTransforms, }) - const result = await compilerContextStorage.run( - this as unknown as StartCompilerPluginContext, - () => - compiler.compile({ - id, - code, - detectedKinds, - }), - ) + const result = await compiler.compile({ + id, + code, + detectedKinds, + }) return result }, },