Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions packages/start-client-core/src/createCsrfMiddleware.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -77,9 +76,9 @@ type CreateCsrfMiddleware = <TRegister, TMiddlewares>(
opts?: CsrfMiddlewareOptions<TRegister, TMiddlewares>,
) => RequestMiddlewareAfterServer<{}, undefined, undefined>

const innerCreateCsrfMiddleware: CreateCsrfMiddleware = (opts = {}) => {
const middleware = createMiddleware().server(async (ctx) => {
const csrfCtx = ctx as RequestServerOptions<any, any> & typeof ctx
const innerCreateCsrfMiddleware = ((opts: any = {}) => {
const middleware = createMiddleware().server(async (ctx: any) => {
const csrfCtx = ctx as RequestServerOptions<any, any>

if (opts.filter && !(await opts.filter(csrfCtx))) {
return ctx.next()
Expand All @@ -97,7 +96,7 @@ const innerCreateCsrfMiddleware: CreateCsrfMiddleware = (opts = {}) => {
}

return middleware
}
}) as any as CreateCsrfMiddleware

export const createCsrfMiddleware: CreateCsrfMiddleware =
createIsomorphicFn().server(innerCreateCsrfMiddleware) as CreateCsrfMiddleware
Expand Down
16 changes: 16 additions & 0 deletions packages/start-fn-stubs/src/createMiddleware.ts
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions packages/start-fn-stubs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export {
type ClientOnlyFn,
type IsomorphicFnBase,
} from './createIsomorphicFn'
export { createMiddleware } from './createMiddleware'

export { createServerOnlyFn, createClientOnlyFn } from './envOnly'
2 changes: 2 additions & 0 deletions packages/start-plugin-core/src/start-compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ export class StartCompiler {
['createIsomorphicFn', 'IsomorphicFn'],
['createServerOnlyFn', 'ServerOnlyFn'],
['createClientOnlyFn', 'ClientOnlyFn'],
['createMiddleware', 'Middleware'],
]),
)

Expand All @@ -705,6 +706,7 @@ export class StartCompiler {
['createIsomorphicFn', 'IsomorphicFn'],
['createServerOnlyFn', 'ServerOnlyFn'],
['createClientOnlyFn', 'ClientOnlyFn'],
['createMiddleware', 'Middleware'],
]),
)

Expand Down
55 changes: 11 additions & 44 deletions packages/start-plugin-core/src/vite/start-compiler-plugin/plugin.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -47,20 +46,6 @@ type ModuleInvalidationEnvironment = {
}
}

type StartCompilerPluginContext = {
environment: {
name: string
mode: string
transformRequest: (url: string) => Promise<unknown>
}
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<string>,
Expand Down Expand Up @@ -196,17 +181,6 @@ export function startCompilerPlugin(
opts: StartCompilerPluginOptions,
): PluginOption {
const compilers = new Map<string, ReturnType<typeof createStartCompiler>>()
const compilerContextStorage =
new AsyncLocalStorage<StartCompilerPluginContext>()

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<string, ServerFn> = {}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
},
},
Expand Down
Loading