Skip to content

Commit a532eea

Browse files
authored
refactor(miniflare): use CorePaths for local explorer routes (#13090)
1 parent f05f2da commit a532eea

13 files changed

Lines changed: 49 additions & 56 deletions

File tree

.changeset/warm-foxes-shave.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"miniflare": patch
3+
---
4+
5+
Remove `LOCAL_EXPLORER_BASE_PATH` and `LOCAL_EXPLORER_API_PATH` constants in favor of `CorePaths.EXPLORER`
6+
7+
These were redundant aliases introduced before `CorePaths` was centralized. All internal consumers now use `CorePaths.EXPLORER` directly.

fixtures/worker-with-resources/tests/index.test.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { resolve } from "path";
2-
import { LOCAL_EXPLORER_API_PATH, LOCAL_EXPLORER_BASE_PATH } from "miniflare";
2+
import { CorePaths } from "miniflare";
33
import { afterAll, assert, beforeAll, describe, it } from "vitest";
44
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived";
55

6+
const EXPLORER_API_PATH = `${CorePaths.EXPLORER}/api`;
7+
68
describe("local explorer", () => {
79
let ip: string;
810
let port: number;
@@ -19,11 +21,11 @@ describe("local explorer", () => {
1921
await stop?.();
2022
});
2123

22-
it(`returns local explorer API response for ${LOCAL_EXPLORER_API_PATH}`, async ({
24+
it(`returns local explorer API response for ${EXPLORER_API_PATH}`, async ({
2325
expect,
2426
}) => {
2527
const response = await fetch(
26-
`http://${ip}:${port}${LOCAL_EXPLORER_API_PATH}/storage/kv/namespaces`
28+
`http://${ip}:${port}${EXPLORER_API_PATH}/storage/kv/namespaces`
2729
);
2830
expect(response.headers.get("Content-Type")).toBe("application/json");
2931
const json = await response.json();
@@ -53,12 +55,8 @@ describe("local explorer", () => {
5355
expect(text).toBe("Hello World!");
5456
});
5557

56-
it(`serves UI index.html at ${LOCAL_EXPLORER_BASE_PATH}`, async ({
57-
expect,
58-
}) => {
59-
const response = await fetch(
60-
`http://${ip}:${port}${LOCAL_EXPLORER_BASE_PATH}`
61-
);
58+
it(`serves UI index.html at ${CorePaths.EXPLORER}`, async ({ expect }) => {
59+
const response = await fetch(`http://${ip}:${port}${CorePaths.EXPLORER}`);
6260
expect(response.status).toBe(200);
6361
expect(response.headers.get("Content-Type")).toBe(
6462
"text/html; charset=utf-8"
@@ -68,12 +66,12 @@ describe("local explorer", () => {
6866
expect(text).toContain("Cloudflare Local Explorer");
6967
});
7068

71-
it(`serves UI assets at ${LOCAL_EXPLORER_BASE_PATH}/assets/*`, async ({
69+
it(`serves UI assets at ${CorePaths.EXPLORER}/assets/*`, async ({
7270
expect,
7371
}) => {
7472
// First get index.html to find the actual asset paths
7573
const indexResponse = await fetch(
76-
`http://${ip}:${port}${LOCAL_EXPLORER_BASE_PATH}`
74+
`http://${ip}:${port}${CorePaths.EXPLORER}`
7775
);
7876
const html = await indexResponse.text();
7977

@@ -85,7 +83,7 @@ describe("local explorer", () => {
8583

8684
// Fetch the JS asset
8785
const jsResponse = await fetch(
88-
`http://${ip}:${port}${LOCAL_EXPLORER_BASE_PATH}/${jsPath}`
86+
`http://${ip}:${port}${CorePaths.EXPLORER}/${jsPath}`
8987
);
9088
expect(jsResponse.status).toBe(200);
9189
expect(jsResponse.headers.get("Content-Type")).toMatch(
@@ -96,7 +94,7 @@ describe("local explorer", () => {
9694
it("serves UI with SPA fallback for unknown routes", async ({ expect }) => {
9795
// Request a route that doesn't exist as a file but should be handled by the SPA
9896
const response = await fetch(
99-
`http://${ip}:${port}${LOCAL_EXPLORER_BASE_PATH}/kv/some-namespace`
97+
`http://${ip}:${port}${CorePaths.EXPLORER}/kv/some-namespace`
10098
);
10199
expect(response.status).toBe(200);
102100
expect(response.headers.get("Content-Type")).toBe(
@@ -123,12 +121,10 @@ describe("local explorer", () => {
123121
await stop?.();
124122
});
125123

126-
it("returns worker response for LOCAL_EXPLORER_API_PATH", async ({
124+
it(`returns worker response for ${EXPLORER_API_PATH}`, async ({
127125
expect,
128126
}) => {
129-
const response = await fetch(
130-
`http://${ip}:${port}${LOCAL_EXPLORER_API_PATH}`
131-
);
127+
const response = await fetch(`http://${ip}:${port}${EXPLORER_API_PATH}`);
132128
const text = await response.text();
133129
expect(text).toBe("Hello World!");
134130
});
@@ -171,7 +167,7 @@ describe("local explorer", () => {
171167
expect,
172168
}) => {
173169
const response = await fetch(
174-
`http://${ip}:${port}${LOCAL_EXPLORER_API_PATH}/storage/kv/namespaces`
170+
`http://${ip}:${port}${EXPLORER_API_PATH}/storage/kv/namespaces`
175171
);
176172
expect(response.status).toBe(200);
177173
expect(response.headers.get("Content-Type")).toBe("application/json");
@@ -180,9 +176,7 @@ describe("local explorer", () => {
180176
});
181177

182178
it(`serves explorer UI`, async ({ expect }) => {
183-
const response = await fetch(
184-
`http://${ip}:${port}${LOCAL_EXPLORER_BASE_PATH}`
185-
);
179+
const response = await fetch(`http://${ip}:${port}${CorePaths.EXPLORER}`);
186180
expect(response.status).toBe(200);
187181
expect(response.headers.get("Content-Type")).toBe(
188182
"text/html; charset=utf-8"

packages/miniflare/src/plugins/core/constants.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { CorePaths } from "../../workers";
21
import type {
32
DoRawQueryResult,
43
DoSqlWithParams,
@@ -12,10 +11,6 @@ export const SERVICE_ENTRY = `${CORE_PLUGIN_NAME}:entry`;
1211
export const SERVICE_LOCAL_EXPLORER = `${CORE_PLUGIN_NAME}:local-explorer`;
1312
// Disk service for local explorer UI assets
1413
export const LOCAL_EXPLORER_DISK = `${CORE_PLUGIN_NAME}:local-explorer-disk`;
15-
// URL path prefix where the local explorer UI is served
16-
export const LOCAL_EXPLORER_BASE_PATH = CorePaths.EXPLORER;
17-
// URL path prefix for the local explorer API endpoints
18-
export const LOCAL_EXPLORER_API_PATH = `${CorePaths.EXPLORER}/api`;
1914
// Service prefix for all regular user workers
2015
const SERVICE_USER_PREFIX = `${CORE_PLUGIN_NAME}:user`;
2116
// Service prefix for `workerd`'s builtin services (network, external, disk)

packages/miniflare/src/plugins/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ export {
167167
CORE_PLUGIN,
168168
CORE_PLUGIN_NAME,
169169
SERVICE_ENTRY,
170-
LOCAL_EXPLORER_BASE_PATH,
171-
LOCAL_EXPLORER_API_PATH,
172170
CoreOptionsSchema,
173171
CoreSharedOptionsSchema,
174172
compileModuleRules,

packages/miniflare/src/workers/local-explorer/aggregation.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
* any one instance can aggregate data from all instances.
66
*/
77

8-
import { LOCAL_EXPLORER_API_PATH } from "../../plugins/core/constants";
8+
import { CorePaths } from "../core";
99
import type { WorkerRegistry } from "../../shared/dev-registry-types";
1010
import type { AppContext } from "./common";
1111

12+
const EXPLORER_API_PATH = `${CorePaths.EXPLORER}/api`;
13+
1214
/**
1315
* Header that indicates a request should not trigger further aggregation.
1416
* Used to prevent infinite recursion when instance A fetches from instance B.
@@ -60,7 +62,7 @@ export async function fetchFromPeer(
6062
init?: RequestInit
6163
): Promise<Response | null> {
6264
try {
63-
const url = new URL(`${LOCAL_EXPLORER_API_PATH}${apiPath}`, peerUrl);
65+
const url = new URL(`${EXPLORER_API_PATH}${apiPath}`, peerUrl);
6466
const response = await fetch(url.toString(), {
6567
...init,
6668
headers: {

packages/miniflare/src/workers/local-explorer/explorer.worker.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44
import { Hono } from "hono/tiny";
55
import mime from "mime";
6-
import {
7-
LOCAL_EXPLORER_API_PATH,
8-
LOCAL_EXPLORER_BASE_PATH,
9-
} from "../../plugins/core/constants";
10-
import { CoreBindings } from "../core";
6+
import { CoreBindings, CorePaths } from "../core";
117
import { errorResponse, validateQuery, validateRequestBody } from "./common";
128
import { wrapResponse } from "./common";
139
import {
@@ -68,7 +64,9 @@ export type Env = {
6864

6965
export type AppBindings = { Bindings: Env };
7066

71-
const app = new Hono<AppBindings>().basePath(LOCAL_EXPLORER_BASE_PATH);
67+
const EXPLORER_API_PATH = `${CorePaths.EXPLORER}/api`;
68+
69+
const app = new Hono<AppBindings>().basePath(CorePaths.EXPLORER);
7270

7371
// Global error handler - catches all uncaught errors and wraps them in an error response
7472
app.onError((err) => {
@@ -121,14 +119,13 @@ function getContentType(filePath: string): string {
121119
}
122120

123121
app.get("/*", async (c, next) => {
124-
if (c.req.path.startsWith(LOCAL_EXPLORER_API_PATH)) {
122+
if (c.req.path.startsWith(EXPLORER_API_PATH)) {
125123
// continue on to API routes
126124
return next();
127125
}
128126

129127
// Some simple asset path handling...
130-
let assetPath =
131-
c.req.path.replace(LOCAL_EXPLORER_BASE_PATH, "") || "/index.html";
128+
let assetPath = c.req.path.replace(CorePaths.EXPLORER, "") || "/index.html";
132129
if (assetPath === "/") {
133130
assetPath = "/index.html";
134131
}

packages/miniflare/test/plugins/local-explorer/aggregation.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import path from "node:path";
44
import { removeDirSync } from "@cloudflare/workers-utils";
55
import { getWorkerRegistry, Miniflare } from "miniflare";
66
import { afterAll, beforeAll, describe, test } from "vitest";
7-
import { LOCAL_EXPLORER_API_PATH } from "../../../src/plugins/core/constants";
7+
import { CorePaths } from "../../../src/workers/core/constants";
88
import { disposeWithRetry } from "../../test-shared";
99

10-
const BASE_URL = `http://localhost${LOCAL_EXPLORER_API_PATH}`;
10+
const BASE_URL = `http://localhost${CorePaths.EXPLORER}/api`;
1111

1212
/**
1313
* Poll the dev registry until all expected workers are registered.

packages/miniflare/test/plugins/local-explorer/d1.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Miniflare } from "miniflare";
22
import { afterAll, beforeAll, describe, test } from "vitest";
3-
import { LOCAL_EXPLORER_API_PATH } from "../../../src/plugins/core/constants";
3+
import { CorePaths } from "../../../src/workers/core/constants";
44
import {
55
zD1ApiResponseCommonFailure,
66
zD1ListDatabasesResponse,
@@ -9,7 +9,7 @@ import {
99
import { disposeWithRetry } from "../../test-shared";
1010
import { expectValidResponse } from "./helpers";
1111

12-
const BASE_URL = `http://localhost${LOCAL_EXPLORER_API_PATH}`;
12+
const BASE_URL = `http://localhost${CorePaths.EXPLORER}/api`;
1313

1414
describe("D1 API", () => {
1515
let mf: Miniflare;

packages/miniflare/test/plugins/local-explorer/do.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from "node:path";
33
import { removeDir } from "@cloudflare/workers-utils";
44
import { Miniflare } from "miniflare";
55
import { afterAll, beforeAll, describe, test } from "vitest";
6-
import { LOCAL_EXPLORER_API_PATH } from "../../../src/plugins/core/constants";
6+
import { CorePaths } from "../../../src/workers/core/constants";
77
import { disposeWithRetry, useTmp } from "../../test-shared";
88

99
interface DONamespace {
@@ -40,7 +40,7 @@ interface ListObjectsResponse {
4040
messages: Array<{ code: number; message: string }>;
4141
}
4242

43-
const BASE_URL = `http://localhost${LOCAL_EXPLORER_API_PATH}`;
43+
const BASE_URL = `http://localhost${CorePaths.EXPLORER}/api`;
4444

4545
describe("Durable Objects API", () => {
4646
let mf: Miniflare;

packages/miniflare/test/plugins/local-explorer/index.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import http from "node:http";
22
import { Miniflare } from "miniflare";
33
import { afterAll, beforeAll, describe, test } from "vitest";
4-
import { LOCAL_EXPLORER_API_PATH } from "../../../src/plugins/core/constants";
4+
import { CorePaths } from "../../../src/workers/core/constants";
55
import { disposeWithRetry } from "../../test-shared";
66

7-
const BASE_URL = `http://localhost${LOCAL_EXPLORER_API_PATH}`;
7+
const BASE_URL = `http://localhost${CorePaths.EXPLORER}/api`;
88

99
describe("Local Explorer API validation", () => {
1010
let mf: Miniflare;
@@ -234,7 +234,7 @@ describe("Local Explorer API validation", () => {
234234
const url = await mf.ready;
235235
const status = await new Promise<number>((resolve, reject) => {
236236
const req = http.get(
237-
`${url.origin}${LOCAL_EXPLORER_API_PATH}/storage/kv/namespaces`,
237+
`${url.origin}${CorePaths.EXPLORER}/api/storage/kv/namespaces`,
238238
{ setHost: false, headers: { Host: "evil.com" } },
239239
(res) => {
240240
res.resume();
@@ -333,7 +333,7 @@ describe("Local Explorer works with custom routes", () => {
333333
const url = await mf.ready;
334334
const status = await new Promise<number>((resolve, reject) => {
335335
const req = http.get(
336-
`${url.origin}${LOCAL_EXPLORER_API_PATH}/storage/kv/namespaces`,
336+
`${url.origin}${CorePaths.EXPLORER}/api/storage/kv/namespaces`,
337337
{ setHost: false, headers: { Host: "my-custom-site.com" } },
338338
(res) => {
339339
res.resume();
@@ -351,7 +351,7 @@ describe("Local Explorer works with custom routes", () => {
351351
const url = await mf.ready;
352352
const status = await new Promise<number>((resolve, reject) => {
353353
const req = http.get(
354-
`${url.origin}${LOCAL_EXPLORER_API_PATH}/storage/kv/namespaces`,
354+
`${url.origin}${CorePaths.EXPLORER}/api/storage/kv/namespaces`,
355355
{ setHost: false, headers: { Host: "evil.com" } },
356356
(res) => {
357357
res.resume();

0 commit comments

Comments
 (0)