From d2a43ea0cb640cee14f951f0bc6ae94fd2225005 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Mon, 27 Sep 2021 18:23:16 -0700 Subject: [PATCH 1/7] feat: add native logging with headers and body to CloudEvent Signed-off-by: Grant Timmerman --- src/event/cloudevent.ts | 8 ++++++++ test/integration/cloud_event_test.ts | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/src/event/cloudevent.ts b/src/event/cloudevent.ts index 870d8660..999ff98e 100644 --- a/src/event/cloudevent.ts +++ b/src/event/cloudevent.ts @@ -9,6 +9,7 @@ import { Emitter } from ".."; import { CloudEventV1, CloudEventV1Attributes, CloudEventV1OptionalAttributes } from "./interfaces"; import { validateCloudEvent } from "./spec"; import { ValidationError, isBinary, asBase64, isValidType } from "./validation"; +import * as util from 'util'; /** * An enum representing the CloudEvent specification version @@ -195,4 +196,11 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`); ): CloudEvent { return new CloudEvent(Object.assign({}, this.toJSON(), options) as CloudEvent, strict); } + + /** + * The native `console.log` value of the CloudEvent. + */ + [util.inspect.custom](depth: any, opts: any) { + return this.toString(); + } } diff --git a/test/integration/cloud_event_test.ts b/test/integration/cloud_event_test.ts index 1c7ee422..e3f8759d 100644 --- a/test/integration/cloud_event_test.ts +++ b/test/integration/cloud_event_test.ts @@ -55,6 +55,11 @@ describe("A CloudEvent", () => { expect(new CloudEvent(JSON.parse(JSON.stringify(ce)))).to.deep.equal(ce); }); + it("serializes as JSON with raw log", () => { + const ce = new CloudEvent({ ...fixture, data: { lunch: "tacos" } }); + expect(ce.toString()).to.deep.equal(JSON.stringify(ce)); + }); + it("Throw a validation error for invalid extension names", () => { expect(() => { new CloudEvent({ "ext-1": "extension1", ...fixture }); From 125cdffa803d2a540298c1c9d8abd6fa403672c3 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Mon, 27 Sep 2021 18:28:18 -0700 Subject: [PATCH 2/7] ci: simplify Signed-off-by: Grant Timmerman --- src/event/cloudevent.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/event/cloudevent.ts b/src/event/cloudevent.ts index 999ff98e..63d0c896 100644 --- a/src/event/cloudevent.ts +++ b/src/event/cloudevent.ts @@ -9,7 +9,6 @@ import { Emitter } from ".."; import { CloudEventV1, CloudEventV1Attributes, CloudEventV1OptionalAttributes } from "./interfaces"; import { validateCloudEvent } from "./spec"; import { ValidationError, isBinary, asBase64, isValidType } from "./validation"; -import * as util from 'util'; /** * An enum representing the CloudEvent specification version @@ -200,7 +199,7 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`); /** * The native `console.log` value of the CloudEvent. */ - [util.inspect.custom](depth: any, opts: any) { + [Symbol.for('nodejs.util.inspect.custom')](depth: any, opts: any) { return this.toString(); } } From a8e1b135ca95fd0974dfb73a6f2da5cf7cd7020b Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Mon, 27 Sep 2021 18:32:50 -0700 Subject: [PATCH 3/7] ci: simplify Signed-off-by: Grant Timmerman --- src/event/cloudevent.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/event/cloudevent.ts b/src/event/cloudevent.ts index 63d0c896..1688abf0 100644 --- a/src/event/cloudevent.ts +++ b/src/event/cloudevent.ts @@ -198,8 +198,9 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`); /** * The native `console.log` value of the CloudEvent. + * @returns The string representation of the CloudEvent. */ - [Symbol.for('nodejs.util.inspect.custom')](depth: any, opts: any) { + [Symbol.for("nodejs.util.inspect.custom")]() { return this.toString(); } } From c990448d143b8ea07f4da176d463653dec1a7700 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Tue, 28 Sep 2021 10:28:21 -0700 Subject: [PATCH 4/7] ci: returns to return Signed-off-by: Grant Timmerman --- src/event/cloudevent.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/event/cloudevent.ts b/src/event/cloudevent.ts index 1688abf0..2a94c1d7 100644 --- a/src/event/cloudevent.ts +++ b/src/event/cloudevent.ts @@ -198,7 +198,7 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`); /** * The native `console.log` value of the CloudEvent. - * @returns The string representation of the CloudEvent. + * @return The string representation of the CloudEvent. */ [Symbol.for("nodejs.util.inspect.custom")]() { return this.toString(); From a41a7c7c155ef290b1fdd578ab2b4dcc5aaaf899 Mon Sep 17 00:00:00 2001 From: Grant Timmerman <744973+grant@users.noreply.github.com> Date: Wed, 29 Sep 2021 12:17:54 -0700 Subject: [PATCH 5/7] ci: Update test/integration/cloud_event_test.ts Co-authored-by: Lance Ball --- test/integration/cloud_event_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/cloud_event_test.ts b/test/integration/cloud_event_test.ts index e3f8759d..d6f065c3 100644 --- a/test/integration/cloud_event_test.ts +++ b/test/integration/cloud_event_test.ts @@ -57,7 +57,7 @@ describe("A CloudEvent", () => { it("serializes as JSON with raw log", () => { const ce = new CloudEvent({ ...fixture, data: { lunch: "tacos" } }); - expect(ce.toString()).to.deep.equal(JSON.stringify(ce)); + expect(ce.toString()).to.deep.equal(ce[Symbol.for("nodejs.util.inspect.custom")]()); }); it("Throw a validation error for invalid extension names", () => { From 7da6533da6f67bb0de8d27b113eadc146c3e584b Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 29 Sep 2021 12:29:35 -0700 Subject: [PATCH 6/7] ci: revert symbol change Signed-off-by: Grant Timmerman --- test/integration/cloud_event_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/cloud_event_test.ts b/test/integration/cloud_event_test.ts index d6f065c3..e3f8759d 100644 --- a/test/integration/cloud_event_test.ts +++ b/test/integration/cloud_event_test.ts @@ -57,7 +57,7 @@ describe("A CloudEvent", () => { it("serializes as JSON with raw log", () => { const ce = new CloudEvent({ ...fixture, data: { lunch: "tacos" } }); - expect(ce.toString()).to.deep.equal(ce[Symbol.for("nodejs.util.inspect.custom")]()); + expect(ce.toString()).to.deep.equal(JSON.stringify(ce)); }); it("Throw a validation error for invalid extension names", () => { From f5965bc9d992a6b6d4b3260f5edde4a9b11e1cc6 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Thu, 30 Sep 2021 13:32:05 -0700 Subject: [PATCH 7/7] refactor: improve ts for toString Signed-off-by: Grant Timmerman --- src/event/cloudevent.ts | 4 ++-- test/integration/cloud_event_test.ts | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/event/cloudevent.ts b/src/event/cloudevent.ts index 2a94c1d7..7febb91e 100644 --- a/src/event/cloudevent.ts +++ b/src/event/cloudevent.ts @@ -198,9 +198,9 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`); /** * The native `console.log` value of the CloudEvent. - * @return The string representation of the CloudEvent. + * @return {string} The string representation of the CloudEvent. */ - [Symbol.for("nodejs.util.inspect.custom")]() { + [Symbol.for("nodejs.util.inspect.custom")](): string { return this.toString(); } } diff --git a/test/integration/cloud_event_test.ts b/test/integration/cloud_event_test.ts index e3f8759d..4435f26c 100644 --- a/test/integration/cloud_event_test.ts +++ b/test/integration/cloud_event_test.ts @@ -57,7 +57,9 @@ describe("A CloudEvent", () => { it("serializes as JSON with raw log", () => { const ce = new CloudEvent({ ...fixture, data: { lunch: "tacos" } }); - expect(ce.toString()).to.deep.equal(JSON.stringify(ce)); + const inspectSymbol = (Symbol.for("nodejs.util.inspect.custom") as unknown) as string; + const ceToString = (ce[inspectSymbol] as CallableFunction).bind(ce); + expect(ce.toString()).to.deep.equal(ceToString()); }); it("Throw a validation error for invalid extension names", () => {