Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(experimental): add fetchable environment interface #19664

Merged
merged 5 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ export default tseslint.config(
'n/no-exports-assign': 'error',
'n/no-unpublished-bin': 'error',
'n/no-unsupported-features/es-builtins': 'error',
'n/no-unsupported-features/node-builtins': 'error',
'n/no-unsupported-features/node-builtins': [
'error',
{
// TODO: remove this when we don't support Node 18 anymore
ignores: ['Response', 'Request', 'fetch'],
},
],
'n/process-exit-as-throw': 'error',
'n/hashbang': 'error',

Expand Down
2 changes: 2 additions & 0 deletions packages/vite/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const disallowedVariables = [
'createServerModuleRunner',
'createServerModuleRunnerTransport',
'isRunnableDevEnvironment',
'createFetchableDevEnvironment',
'isFetchableDevEnvironment',
]
disallowedVariables.forEach((name) => {
Object.defineProperty(module.exports, name, {
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export {
type RunnableDevEnvironment,
type RunnableDevEnvironmentContext,
} from './server/environments/runnableEnvironment'
export {
createFetchableDevEnvironment,
isFetchableDevEnvironment,
type FetchableDevEnvironment,
type FetchableDevEnvironmentContext,
} from './server/environments/fetchableEnvironments'
export {
DevEnvironment,
type DevEnvironmentContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { ResolvedConfig } from '../../config'
import type { DevEnvironmentContext } from '../environment'
import { DevEnvironment } from '../environment'
import type { Environment } from '../../environment'

export interface FetchableDevEnvironmentContext extends DevEnvironmentContext {
handleRequest(request: Request): Promise<Response> | Response
}

export function createFetchableDevEnvironment(
name: string,
config: ResolvedConfig,
context: FetchableDevEnvironmentContext,
): FetchableDevEnvironment {
if (typeof Request === 'undefined' || typeof Response === 'undefined') {
throw new TypeError(
'FetchableDevEnvironment requires a global `Request` and `Response` object.',
)
}

if (!context.handleRequest) {
throw new TypeError(
'FetchableDevEnvironment requires a `handleRequest` method during initialisation.',
)
}

return new FetchableDevEnvironment(name, config, context)
}

export function isFetchableDevEnvironment(
environment: Environment,
): environment is FetchableDevEnvironment {
return environment instanceof FetchableDevEnvironment
}

class FetchableDevEnvironment extends DevEnvironment {
private _handleRequest: (request: Request) => Promise<Response> | Response

constructor(
name: string,
config: ResolvedConfig,
context: FetchableDevEnvironmentContext,
) {
super(name, config, context as DevEnvironmentContext)
this._handleRequest = context.handleRequest
}

public async dispatchFetch(request: Request): Promise<Response> {
const response = await this._handleRequest(request)
if (!(response instanceof Response)) {
throw new TypeError(
'FetchableDevEnvironment `context.handleRequest` must return a `Response` object.',
)
}
return response
}
}

export type { FetchableDevEnvironment }