Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 2dab039

Browse files
committed
Only send request log requests at INFO level or higher
Building on the previous commit, avoid sending requests that might be aborted in tests, because they're `waitUntil`ed. Without this, we could end up with an incomplete JSON body that can't be parsed.
1 parent fc7eddc commit 2dab039

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

packages/tre/src/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,7 @@ export class Miniflare {
540540
request
541541
);
542542
} else if (url.pathname === "/core/log") {
543-
if (this.#log.level < LogLevel.INFO) {
544-
return;
545-
}
546-
547-
const response = await formatResponse(request);
548-
this.#log.log(response);
543+
this.#log.info(await formatResponse(request));
549544
} else {
550545
// TODO: check for proxying/outbound fetch header first (with plans for fetch mocking)
551546
response = await this.#handleLoopbackPlugins(request, url);
@@ -700,6 +695,7 @@ export class Miniflare {
700695
allWorkerRoutes,
701696
fallbackWorkerName: this.#workerOpts[0].core.name,
702697
loopbackPort,
698+
log: this.#log,
703699
});
704700
for (const service of globalServices) {
705701
// Global services should all have unique names

packages/tre/src/plugins/core/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import {
1010
Worker_Module,
1111
supportedCompatibilityDate,
1212
} from "../../runtime";
13-
import { Awaitable, JsonSchema, Log, MiniflareCoreError } from "../../shared";
13+
import {
14+
Awaitable,
15+
JsonSchema,
16+
Log,
17+
LogLevel,
18+
MiniflareCoreError,
19+
} from "../../shared";
1420
import { getCacheServiceName } from "../cache";
1521
import { DURABLE_OBJECTS_STORAGE_SERVICE_NAME } from "../do";
1622
import {
@@ -102,6 +108,7 @@ const BINDING_SERVICE_USER_FALLBACK = "MINIFLARE_USER_FALLBACK";
102108
const BINDING_TEXT_CUSTOM_SERVICE = "MINIFLARE_CUSTOM_SERVICE";
103109
const BINDING_JSON_CF_BLOB = "CF_BLOB";
104110
const BINDING_JSON_ROUTES = "MINIFLARE_ROUTES";
111+
const BINDING_JSON_LOG_LEVEL = "MINIFLARE_LOG_LEVEL";
105112
const BINDING_DATA_LIVE_RELOAD_SCRIPT = "MINIFLARE_LIVE_RELOAD_SCRIPT";
106113

107114
const LIVE_RELOAD_SCRIPT_TEMPLATE = (
@@ -179,7 +186,8 @@ async function handleEvent(event) {
179186
}
180187
}
181188
182-
event.waitUntil(${BINDING_SERVICE_LOOPBACK}.fetch("http://localhost/core/log", {
189+
if (${BINDING_JSON_LOG_LEVEL} >= ${LogLevel.INFO}) {
190+
event.waitUntil(${BINDING_SERVICE_LOOPBACK}.fetch("http://localhost/core/log", {
183191
method: "POST",
184192
headers: { "Content-Type": "application/json" },
185193
body: JSON.stringify({
@@ -189,7 +197,8 @@ async function handleEvent(event) {
189197
"url": response.url,
190198
"time": Date.now() - startTime,
191199
}),
192-
}));
200+
}));
201+
}
193202
194203
const liveReloadScript = globalThis.${BINDING_DATA_LIVE_RELOAD_SCRIPT};
195204
if (
@@ -421,13 +430,15 @@ export interface GlobalServicesOptions {
421430
allWorkerRoutes: Map<string, string[]>;
422431
fallbackWorkerName: string | undefined;
423432
loopbackPort: number;
433+
log: Log;
424434
}
425435
export function getGlobalServices({
426436
optionsVersion,
427437
sharedOptions,
428438
allWorkerRoutes,
429439
fallbackWorkerName,
430440
loopbackPort,
441+
log,
431442
}: GlobalServicesOptions): Service[] {
432443
// Collect list of workers we could route to, then parse and sort all routes
433444
const routableWorkers = [...allWorkerRoutes.keys()];
@@ -439,6 +450,7 @@ export function getGlobalServices({
439450
{ name: BINDING_JSON_VERSION, json: optionsVersion.toString() },
440451
{ name: BINDING_JSON_ROUTES, json: JSON.stringify(routes) },
441452
{ name: BINDING_JSON_CF_BLOB, json: JSON.stringify(sharedOptions.cf) },
453+
{ name: BINDING_JSON_LOG_LEVEL, json: JSON.stringify(log.level) },
442454
{
443455
name: BINDING_SERVICE_USER_FALLBACK,
444456
service: { name: getUserServiceName(fallbackWorkerName) },

packages/tre/src/shared/log.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ export class Log {
8282
this.#suffix = suffix ? ":" + suffix : "";
8383
}
8484

85-
log(message: string): void {
85+
protected log(message: string): void {
8686
console.log(message);
8787
}
8888

89-
logWithLevel(level: LogLevel, message: string): void {
89+
protected logWithLevel(level: LogLevel, message: string): void {
9090
if (level <= this.level) {
9191
const prefix = `[${this.#prefix}${LEVEL_PREFIX[level]}${this.#suffix}]`;
9292
this.log(LEVEL_COLOUR[level](`${prefix} ${message}`));
@@ -127,21 +127,17 @@ export class Log {
127127
}
128128

129129
export class NoOpLog extends Log {
130-
log(): void {}
130+
constructor() {
131+
super(LogLevel.NONE);
132+
}
133+
134+
protected log(): void {}
131135

132136
error(message: Error): void {
133137
throw message;
134138
}
135139
}
136140

137-
export interface ResponseInfo {
138-
status: number;
139-
statusText: string;
140-
method: string;
141-
url: string;
142-
time: number;
143-
}
144-
145141
const ResponseInfoSchema = z.object({
146142
status: z.number(),
147143
statusText: z.string(),
@@ -154,13 +150,14 @@ export async function formatResponse(request: Request) {
154150
const info = ResponseInfoSchema.parse(await request.json());
155151
const url = new URL(info.url);
156152

157-
return [
153+
const lines = [
158154
`${bold(info.method)} ${url.pathname} `,
159155
colourFromHTTPStatus(info.status)(
160156
`${bold(info.status)} ${info.statusText} `
161157
),
162158
grey(`(${info.time}ms)`),
163-
].join("");
159+
];
160+
return reset(lines.join(""));
164161
}
165162

166163
function colourFromHTTPStatus(status: number): Colorize {

0 commit comments

Comments
 (0)