Skip to content

Commit

Permalink
feat: include duration in handled event
Browse files Browse the repository at this point in the history
**BREAKING CHANGE**

Previously, the handled event included the performance measure, but
now just includes a single property of duration which is the highest
resolution timing in milliseconds.

This is change is because not all platform acorn supports provides the
full performance measurement API.
  • Loading branch information
kitsonk committed Jun 6, 2024
1 parent b6023f8 commit b6cbe17
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 43 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oak/acorn",
"version": "0.7.0-alpha.2",
"version": "0.7.0-alpha.3",
"exports": {
".": "./mod.ts",
"./context": "./context.ts",
Expand Down
66 changes: 24 additions & 42 deletions router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,16 @@ export interface RouteOptionsWithHandler<
handler: RouteHandler<ResponseType, BodyType, Params>;
}

const HTTP_VERBS = [
"DELETE",
"GET",
"HEAD",
"OPTIONS",
"PATCH",
"POST",
"PUT",
] as const;

const HANDLE_START = "handle start";

let RequestEventCtor: typeof CloudFlareRequestEvent | undefined;

type HTTPVerbs = typeof HTTP_VERBS[number];
type HTTPVerbs =
| "DELETE"
| "GET"
| "HEAD"
| "OPTIONS"
| "PATCH"
| "POST"
| "PUT";

/** A string that represents a range of HTTP response {@linkcode Status} codes:
*
Expand Down Expand Up @@ -302,7 +297,7 @@ export class NotFoundEvent extends Event {
}

interface HandledEventInit extends EventInit {
measure: PerformanceMeasure;
duration: number;
request: Request;
response: Response;
route?: Route;
Expand All @@ -312,15 +307,15 @@ interface HandledEventInit extends EventInit {
*
* This can be used to provide logging and reporting for the router. */
export class HandledEvent extends Event {
#measure: PerformanceMeasure;
#duration: number;
#request: Request;
#response: Response;
#route?: Route;

/** The performance measure from the start of handling the route until it
* finished, which can provide timing information about the processing. */
get measure(): PerformanceMeasure {
return this.#measure;
/** Highest resolution timing available on the platform of the time to handle
* the request/response by acorn in milliseconds. */
get duration(): number {
return this.#duration;
}

/** The {@linkcode Request} that was handled. */
Expand All @@ -343,7 +338,7 @@ export class HandledEvent extends Event {
this.#request = eventInitDict.request;
this.#response = eventInitDict.response;
this.#route = eventInitDict.route;
this.#measure = eventInitDict.measure;
this.#duration = eventInitDict.duration;
}
}

Expand Down Expand Up @@ -910,8 +905,7 @@ export class Router extends EventTarget {
}

async #handle(requestEvent: RequestEvent): Promise<void> {
const uid = this.#uid++;
performance.mark(`${HANDLE_START} ${uid}`);
const start = performance.now();
const { promise, resolve } = createPromiseWithResolvers<Response>();
this.#handling.add(promise);
requestEvent.respond(promise);
Expand Down Expand Up @@ -960,12 +954,9 @@ export class Router extends EventTarget {
response,
);
resolve(result ?? response);
const measure = performance.measure(
`handle ${uid}`,
`${HANDLE_START} ${uid}`,
);
const duration = performance.now() - start;
this.dispatchEvent(
new HandledEvent({ request, route, response, measure }),
new HandledEvent({ request, route, response, duration }),
);
return;
}
Expand All @@ -985,12 +976,9 @@ export class Router extends EventTarget {
}
resolve(response);
this.#handling.delete(promise);
const measure = performance.measure(
`handle ${uid}`,
`${HANDLE_START} ${uid}`,
);
const duration = performance.now() - start;
this.dispatchEvent(
new HandledEvent({ request, route, response, measure }),
new HandledEvent({ request, route, response, duration }),
);
return;
}
Expand All @@ -1006,11 +994,8 @@ export class Router extends EventTarget {
);
resolve(result ?? response);
this.#handling.delete(promise);
const measure = performance.measure(
`handle ${uid}`,
`${HANDLE_START} ${uid}`,
);
this.dispatchEvent(new HandledEvent({ request, response, measure }));
const duration = performance.now() - start;
this.dispatchEvent(new HandledEvent({ request, response, duration }));
return;
}
let response = await this.#handleStatus(
Expand All @@ -1023,11 +1008,8 @@ export class Router extends EventTarget {
}
resolve(response);
this.#handling.delete(promise);
const measure = performance.measure(
`handle ${uid}`,
`${HANDLE_START} ${uid}`,
);
this.dispatchEvent(new HandledEvent({ request, response, measure }));
const duration = performance.now() - start;
this.dispatchEvent(new HandledEvent({ request, response, duration }));
}

#notFound(request: Request): Response {
Expand Down

0 comments on commit b6cbe17

Please sign in to comment.