Skip to content

Commit

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

class BridgeServerDevEnvironment extends DevEnvironment {
// TODO
// can we abstract away child process? FetchBridgeDevEnvironment?
// multiple children per env (like Vitest)? need different API?
export class ChildProcessFetchDevEnvironment 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 async init(...args: Parameters<DevEnvironment["init"]>) {
override init: DevEnvironment["init"] = async (...args) => {
await super.init(...args);

const listener = webToNodeHandler(async (request) => {
Expand Down Expand Up @@ -117,29 +130,8 @@ class BridgeServerDevEnvironment 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 @@ -169,12 +161,13 @@ export class ChildProcessFetchDevEnvironment extends BridgeServerDevEnvironment
bridgeUrl: this.bridgeUrl,
childUrl: this.childUrl,
});
}
};

override async close(...args: Parameters<DevEnvironment["close"]>) {
override close: DevEnvironment["close"] = async (...args) => {
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 3391247

Please sign in to comment.