Skip to content

Commit

Permalink
refactor: BridgeServerDevEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 6, 2024
1 parent 55bce8a commit 4de2cee
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions examples/child-process/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,11 @@ export default defineConfig((_env) => ({
},
}));

// TODO
// can we abstract away child process? FetchBridgeDevEnvironment?
// multiple children per env (like Vitest)? need different API?
export class ChildProcessFetchDevEnvironment extends DevEnvironment {
class BridgeServerDevEnvironment extends DevEnvironment {
public bridge!: http.Server;
public bridgeUrl!: string;
public child!: childProcess.ChildProcess;
public childUrl!: string;
public childUrlPromise!: PromiseWithResolvers<string>;

constructor(
public extraOptions: { command: string[] },
...args: ConstructorParameters<typeof DevEnvironment>
) {
super(...args);
}

override init: DevEnvironment["init"] = async (...args) => {
override async init(...args: Parameters<DevEnvironment["init"]>) {
await super.init(...args);

const listener = webToNodeHandler(async (request) => {
Expand Down Expand Up @@ -130,8 +117,29 @@ export class ChildProcessFetchDevEnvironment extends DevEnvironment {
reject(e);
});
});
}

override async close(...args: Parameters<DevEnvironment["close"]>) {
await super.close(...args);
this.bridge?.close();
}
}

export class ChildProcessFetchDevEnvironment extends BridgeServerDevEnvironment {
public child!: childProcess.ChildProcess;
public childUrl!: string;
public childUrlPromise!: PromiseWithResolvers<string>;

constructor(
public extraOptions: { command: string[] },
...args: ConstructorParameters<typeof DevEnvironment>
) {
super(...args);
}

override async init(...args: Parameters<DevEnvironment["init"]>) {
await super.init(...args);

// TODO: separate child process concern?
this.childUrlPromise = PromiseWithReoslvers();
const command = this.extraOptions.command;
const child = childProcess.spawn(
Expand Down Expand Up @@ -161,13 +169,12 @@ export class ChildProcessFetchDevEnvironment extends DevEnvironment {
bridgeUrl: this.bridgeUrl,
childUrl: this.childUrl,
});
};
}

override close: DevEnvironment["close"] = async (...args) => {
override async close(...args: Parameters<DevEnvironment["close"]>) {
await super.close(...args);
this.child?.kill();
this.bridge?.close();
};
}

async dispatchFetch(entry: string, request: Request): Promise<Response> {
const headers = new Headers(request.headers);
Expand Down

0 comments on commit 4de2cee

Please sign in to comment.