-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support transport options to communicate between the environmen…
…t and the runner (#16209)
- Loading branch information
1 parent
d9ed857
commit dbcc375
Showing
17 changed files
with
236 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import type { FetchFunction, FetchResult } from './types' | ||
|
||
export interface RunnerTransport { | ||
fetchModule: FetchFunction | ||
} | ||
|
||
// TODO: tests | ||
export class RemoteRunnerTransport implements RunnerTransport { | ||
private rpcPromises = new Map< | ||
string, | ||
{ | ||
resolve: (data: any) => void | ||
reject: (data: any) => void | ||
timeoutId?: NodeJS.Timeout | ||
} | ||
>() | ||
|
||
constructor( | ||
private readonly options: { | ||
send: (data: any) => void | ||
onMessage: (handler: (data: any) => void) => void | ||
timeout?: number | ||
}, | ||
) { | ||
this.options.onMessage(async (data) => { | ||
if (typeof data !== 'object' || !data || !data._v) return | ||
|
||
const promise = this.rpcPromises.get(data.i) | ||
if (!promise) return | ||
|
||
if (promise.timeoutId) clearTimeout(promise.timeoutId) | ||
|
||
this.rpcPromises.delete(data.i) | ||
|
||
if (data.e) { | ||
promise.reject(data.e) | ||
} else { | ||
promise.resolve(data.r) | ||
} | ||
}) | ||
} | ||
|
||
private resolve<T>(method: string, ...args: any[]) { | ||
const promiseId = nanoid() | ||
this.options.send({ | ||
_v: true, | ||
m: method, | ||
a: args, | ||
i: promiseId, | ||
}) | ||
|
||
return new Promise<T>((resolve, reject) => { | ||
const timeout = this.options.timeout ?? 60000 | ||
let timeoutId | ||
if (timeout > 0) { | ||
timeoutId = setTimeout(() => { | ||
this.rpcPromises.delete(promiseId) | ||
reject( | ||
new Error( | ||
`${method}(${args.map((arg) => JSON.stringify(arg)).join(', ')}) timed out after ${timeout}ms`, | ||
), | ||
) | ||
}, timeout) | ||
timeoutId?.unref?.() | ||
} | ||
this.rpcPromises.set(promiseId, { resolve, reject, timeoutId }) | ||
}) | ||
} | ||
|
||
fetchModule(id: string, importer?: string): Promise<FetchResult> { | ||
return this.resolve<FetchResult>('fetchModule', id, importer) | ||
} | ||
} | ||
|
||
// port from nanoid | ||
// https://github.com/ai/nanoid | ||
const urlAlphabet = | ||
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict' | ||
function nanoid(size = 21) { | ||
let id = '' | ||
let i = size | ||
while (i--) id += urlAlphabet[(Math.random() * 64) | 0] | ||
return id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { DevEnvironment } from './environment' | ||
|
||
export class RemoteEnvironmentTransport { | ||
constructor( | ||
private readonly options: { | ||
send: (data: any) => void | ||
onMessage: (handler: (data: any) => void) => void | ||
}, | ||
) {} | ||
|
||
initialize(environment: DevEnvironment): void { | ||
this.options.onMessage(async (data) => { | ||
if (typeof data !== 'object' || !data || !data.__v) return | ||
|
||
const method = data.m as 'fetchModule' | ||
const parameters = data.a as [string, string] | ||
|
||
try { | ||
const result = await environment[method](...parameters) | ||
this.options.send({ | ||
__v: true, | ||
r: result, | ||
i: data.i, | ||
}) | ||
} catch (error) { | ||
this.options.send({ | ||
__v: true, | ||
e: { | ||
name: error.name, | ||
message: error.message, | ||
stack: error.stack, | ||
}, | ||
i: data.i, | ||
}) | ||
} | ||
}) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/vite/src/node/server/environments/ssrEnvironment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { DevEnvironment } from '../environment' | ||
import type { ServerHMRChannel } from '../hmr' | ||
import type { ViteDevServer } from '../index' | ||
import { asyncFunctionDeclarationPaddingLineCount } from '../../../shared/utils' | ||
|
||
export function createSsrEnvironment( | ||
server: ViteDevServer, | ||
name: string, | ||
hotChannel: ServerHMRChannel, | ||
): DevEnvironment { | ||
return new DevEnvironment(server, name, { | ||
hot: hotChannel, | ||
runner: { | ||
processSourceMap(map) { | ||
// this assumes that "new AsyncFunction" is used to create the module | ||
return Object.assign({}, map, { | ||
mappings: | ||
';'.repeat(asyncFunctionDeclarationPaddingLineCount) + map.mappings, | ||
}) | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.