Skip to content

Commit

Permalink
feat: improve inspection/console logging
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Jun 28, 2021
1 parent 0ea977b commit bfbf061
Show file tree
Hide file tree
Showing 19 changed files with 216 additions and 4 deletions.
9 changes: 7 additions & 2 deletions application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,7 @@ export class Application<AS extends State = Record<string, any>>
context.response.destroy(false);
return response;
} catch (err) {
// deno-lint-ignore no-unreachable
this.#handleError(context, err);
// deno-lint-ignore no-unreachable
throw err;
}
}) as HandleMethod;
Expand Down Expand Up @@ -553,4 +551,11 @@ export class Application<AS extends State = Record<string, any>>
// deno-lint-ignore no-explicit-any
return this as Application<any>;
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
const { keys, proxy, state } = this;
return `${this.constructor.name} ${
inspect({ "#middleware": this.#middleware, keys, proxy, state })
}`;
}
}
10 changes: 10 additions & 0 deletions application_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,3 +768,13 @@ test({
});
},
});

test({
name: "Application - inspecting",
fn() {
assertEquals(
Deno.inspect(new Application()),
`Application { "#middleware": [], keys: undefined, proxy: false, state: {} }`,
);
},
});
25 changes: 25 additions & 0 deletions context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,29 @@ export class Context<
this.respond = false;
return this.#socket;
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
const {
app,
cookies,
isUpgradable,
respond,
request,
response,
socket,
state,
} = this;
return `${this.constructor.name} ${
inspect({
app,
cookies,
isUpgradable,
respond,
request,
response,
socket,
state,
})
}`;
}
}
15 changes: 15 additions & 0 deletions context_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ function createMockApp<S extends State = Record<string, any>>(
return {
state,
dispatchEvent() {},
[Symbol.for("Deno.customInspect")]() {
return `MockApplication {}`;
},
} as any;
}

Expand Down Expand Up @@ -256,3 +259,15 @@ test({
assertEquals(context.state, { a: "a", b: "b" });
},
});

test({
name: "Context - inspecting",
fn() {
const app = createMockApp();
const req = createMockServerRequest();
assertEquals(
Deno.inspect(new Context(app, req), { depth: 1 }),
`Context {\n app: MockApplication {},\n cookies: Cookies [],\n isUpgradable: false,\n respond: true,\n request: Request {\n hasBody: false,\n headers: Headers { host: "localhost" },\n ip: "",\n ips: [],\n method: "GET",\n secure: false,\n url: URL {\n href: "http://localhost/",\n origin: "http://localhost",\n protocol: "http:",\n username: "",\n password: "",\n host: "localhost",\n hostname: "localhost",\n port: "",\n pathname: "/",\n hash: "",\n search: ""\n}\n},\n response: Response { body: undefined, headers: Headers {}, status: 404, type: undefined, writable: true },\n socket: undefined,\n state: {}\n}`,
);
},
});
4 changes: 4 additions & 0 deletions cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,8 @@ export class Cookies {
}
}
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
return `${this.constructor.name} ${inspect([...this.entries()])}`;
}
}
14 changes: 14 additions & 0 deletions cookies_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,17 @@ test({
assertEquals([...cookies], [["bar", "foo"]]);
},
});

test({
name: "Cookies - inspecting",
fn() {
const request = createMockRequest(
["bar=foo", "foo=baz", "baz=1234"],
);
const response = createMockResponse();
assertEquals(
Deno.inspect(new Cookies(request, response)),
`Cookies [ [ "bar", "foo" ], [ "foo", "baz" ], [ "baz", "1234" ] ]`,
);
},
});
2 changes: 1 addition & 1 deletion http_server_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class NativeRequest {
}

get remoteAddr(): string | undefined {
return (this.#conn?.remoteAddr as Deno.NetAddr).hostname;
return (this.#conn?.remoteAddr as Deno.NetAddr)?.hostname;
}

get request(): Request {
Expand Down
12 changes: 12 additions & 0 deletions keyStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const replacements: Record<string, string> = {
export class KeyStack {
#keys: Key[];

get length(): number {
return this.#keys.length;
}

/** A class which accepts an array of keys that are used to sign and verify
* data and allows easy key rotation without invalidation of previously signed
* data.
Expand Down Expand Up @@ -69,4 +73,12 @@ export class KeyStack {
}
return -1;
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
return `${this.constructor.name} ${
inspect({
length: this.length,
})
}`;
}
}
10 changes: 10 additions & 0 deletions keyStack_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,13 @@ test({
assert(keyStack.verify(data2, keyStack.sign(data1)));
},
});

test({
name: "KeyStack - inspecting",
fn() {
assertEquals(
Deno.inspect(new KeyStack(["abcdef"])),
`KeyStack { length: 1 }`,
);
},
});
4 changes: 4 additions & 0 deletions multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,8 @@ export class FormDataReader {
}
}
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
return `${this.constructor.name} ${inspect({})}`;
}
}
11 changes: 11 additions & 0 deletions multipart_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,14 @@ test({
assertEquals(file.size, 3);
},
});

test({
name: "FormDataReader - inspecting",
fn() {
const body = createBody(fixture);
assertEquals(
Deno.inspect(new FormDataReader(fixtureContentType, body)),
`FormDataReader {}`,
);
},
});
17 changes: 16 additions & 1 deletion request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Request {
#getRemoteAddr(): string {
return this.#serverRequest instanceof NativeRequest
? this.#serverRequest.remoteAddr ?? ""
: (this.#serverRequest.conn.remoteAddr as Deno.NetAddr).hostname ?? "";
: (this.#serverRequest?.conn?.remoteAddr as Deno.NetAddr)?.hostname ?? "";
}

/** Is `true` if the request has a body, otherwise `false`. */
Expand Down Expand Up @@ -236,4 +236,19 @@ export class Request {
body(options: BodyOptions = {}): Body | BodyReader | BodyStream {
return this.#body.get(options);
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
const { hasBody, headers, ip, ips, method, secure, url } = this;
return `${this.constructor.name} ${
inspect({
hasBody,
headers,
ip,
ips,
method,
secure,
url: url.toString(),
})
}`;
}
}
12 changes: 12 additions & 0 deletions request_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,15 @@ test({
);
},
});

test({
name: "Request - inspecting",
fn() {
assertEquals(
Deno.inspect(
new Request(createMockServerRequest({ url: "/foo?bar=baz&qat=qux" })),
),
`Request {\n hasBody: false,\n headers: Headers { host: "localhost" },\n ip: "",\n ips: [],\n method: "GET",\n secure: false,\n url: "http://localhost/foo?bar=baz&qat=qux"\n}`,
);
},
});
7 changes: 7 additions & 0 deletions response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,11 @@ export class Response {
status: this.status,
};
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
const { body, headers, status, type, writable } = this;
return `${this.constructor.name} ${
inspect({ body, headers, status, type, writable })
}`;
}
}
10 changes: 10 additions & 0 deletions response_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,13 @@ test({
assertEquals(serverResponse.status, Status.OK);
},
});

test({
name: "Response - inspecting",
fn() {
assertEquals(
Deno.inspect(new Response(createMockRequest())),
`Response { body: undefined, headers: Headers {}, status: 404, type: undefined, writable: true }`,
);
},
});
19 changes: 19 additions & 0 deletions router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,19 @@ class Layer<
options: { ...this.#opts },
};
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
return `${this.constructor.name} ${
inspect({
methods: this.methods,
middleware: this.stack,
options: this.#opts,
paramNames: this.#paramNames.map((key) => key.name),
path: this.path,
regexp: this.#regexp,
})
}`;
}
}

/** An interface for registering middleware that will run when certain HTTP
Expand Down Expand Up @@ -1056,4 +1069,10 @@ export class Router<
): string {
return toUrl(path, params, options);
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
return `${this.constructor.name} ${
inspect({ "#params": this.#params, "#stack": this.#stack })
}`;
}
}
10 changes: 10 additions & 0 deletions server_sent_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ export class SSEStreamTarget extends EventTarget
}
return dispatched;
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
return `${this.constructor.name} ${
inspect({ "#closed": this.#closed, "#context": this.#context })
}`;
}
}

export class SSEStdLibTarget extends EventTarget
Expand Down Expand Up @@ -424,4 +430,8 @@ export class SSEStdLibTarget extends EventTarget
}
return dispatched;
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
return `${this.constructor.name} ${inspect({ "closed": this.closed })}`;
}
}
23 changes: 23 additions & 0 deletions server_sent_event_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,26 @@ test({
assertEquals(env.appErrorEvents.length, 0);
},
});

test({
name: "SSEStdLibTarget - inspecting",
fn() {
const context = new Context(createMockApp(), createMockServerRequest());
assertEquals(
Deno.inspect(new SSEStdLibTarget(context)),
`SSEStdLibTarget { closed: false }`,
);
},
});

test({
name: "SSEStreamTarget - inspecting",
fn() {
const request = createMockNativeRequest();
const context = new Context(createMockApp(), request);
assertEquals(
Deno.inspect(new SSEStreamTarget(context)),
`SSEStreamTarget {\n "#closed": false,\n "#context": Context {\n app: EventTarget {},\n cookies: Cookies [],\n isUpgradable: false,\n respond: true,\n request: Request {\n hasBody: false,\n headers: Headers {},\n ip: "",\n ips: [],\n method: "GET",\n secure: false,\n url: "http://localhost:8000/"\n},\n response: Response {\n body: ReadableStream { locked: false },\n headers: Headers {\n "cache-control": "no-cache",\n connection: "Keep-Alive",\n "content-type": "text/event-stream",\n "keep-alive": "timeout=9007199254740991"\n},\n status: 200,\n type: undefined,\n writable: true\n},\n socket: undefined,\n state: undefined\n}\n}`,
);
},
});
6 changes: 6 additions & 0 deletions testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export function createMockApp<
use() {
return app;
},
[Symbol.for("Deno.customInspect")]() {
return `MockApplication {}`;
},
} as any;
return app;
}
Expand Down Expand Up @@ -146,6 +149,9 @@ export function createMockContext<
}
throw err;
},
[Symbol.for("Deno.customInspect")]() {
return `MockContext {}`;
},
} as unknown) as RouterContext<P, S>;
}

Expand Down

0 comments on commit bfbf061

Please sign in to comment.