Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
fix: cjs & esm support for http-proxy (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
fubhy committed May 1, 2023
1 parent adb9858 commit c6d71a3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-zebras-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@viem/anvil": patch
---

Fixed `http-proxy` import for both cjs & esm
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Creates and starts a proxy server that spawns anvil instance on demand (e.g. per
```ts
import { createProxy, createPool } from "@viem/anvil";

const server = const createProxy({
const server = await createProxy({
pool: createPool(),
options: {
forkUrl: "https://eth-mainnet.alchemyapi.io/v2/<API_KEY>",
Expand Down
21 changes: 17 additions & 4 deletions packages/anvil.js/src/proxy/createProxy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CreateAnvilOptions } from "../anvil/createAnvil.js";
import { type Pool } from "../pool/createPool.js";
import { type InstanceRequestContext, parseRequest } from "./parseRequest.js";
import { createProxyServer } from "http-proxy";
import httpProxy from "http-proxy";
import { IncomingMessage, ServerResponse, createServer } from "node:http";
import type { Awaitable } from "vitest";

Expand Down Expand Up @@ -63,7 +63,7 @@ export type CreateProxyOptions = {
* ```
* import { createProxy, createPool } from "@viem/anvil";
*
* const server = const createProxy({
* const server = createProxy({
* pool: createPool(),
* options: {
* forkUrl: "https://eth-mainnet.alchemyapi.io/v2/<API_KEY>",
Expand All @@ -76,8 +76,21 @@ export type CreateProxyOptions = {
* });
* ```
*/
export function createProxy({ pool, options, fallback }: CreateProxyOptions) {
const proxy = createProxyServer({
export async function createProxy({
pool,
options,
fallback,
}: CreateProxyOptions) {
let httpProxyWithCjsFallback: typeof httpProxy = httpProxy;

if (httpProxyWithCjsFallback === undefined) {
httpProxyWithCjsFallback = await import("http-proxy").then(
// rome-ignore lint/suspicious/noExplicitAny: required to support both esm & cjs.
(module) => (module as any).default,
);
}

const proxy = await httpProxyWithCjsFallback.createProxyServer({
ignorePath: true,
ws: true,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/anvil.js/src/proxy/startProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function startProxy({
}: StartProxyOptions = {}) {
// rome-ignore lint/suspicious/noAsyncPromiseExecutor: this is fine ...
const server = await new Promise<Server>(async (resolve, reject) => {
const server = createProxy({ pool, ...rest });
const server = await createProxy({ pool, ...rest });

server.on("listening", () => resolve(server));
server.on("error", (error) => reject(error));
Expand Down

0 comments on commit c6d71a3

Please sign in to comment.