Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/utils/Client.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export function createConfigurationType(configDict: { [key: string]: Configurati
* If the data is a valid Cloud Event, the Content-Type is "application/cloudevents+json".
* If the data is a JSON object, the Content-Type is "application/json".
* If the data is a string, the Content-Type is "text/plain".
* If the data is binary (Buffer, typed array, ArrayBuffer, Blob), the Content-Type is "application/octet-stream".
* Any other object, such as a class instance or an object without a prototype, is treated as JSON.
* Otherwise, the Content-Type is "application/octet-stream".
*
* @param data input data
Expand All @@ -169,21 +171,38 @@ export function getContentType(data: any): string {
switch (type) {
case "Array":
case "Object":
if (isCloudEvent(data as object)) {
return "application/cloudevents+json";
} else {
return "application/json";
}
return getObjectContentType(data);
case "Boolean":
case "Number":
case "String":
return "text/plain";
// Uint8Array, Int8Array, Buffer, SlowBuffer, Blob, etc.
default:
if (isBinaryData(data)) {
return "application/octet-stream";
}
// Class instances and objects without a prototype can't be sent as raw bytes:
// the serializers would call Buffer.from() on them (gRPC) or send "[object Object]" (HTTP).
if (data !== null && typeof data === "object") {
return getObjectContentType(data);
}
return "application/octet-stream";
}
}

function getObjectContentType(data: object): string {
return isCloudEvent(data) ? "application/cloudevents+json" : "application/json";
}

function isBinaryData(data: unknown): boolean {
return (
data instanceof ArrayBuffer ||
ArrayBuffer.isView(data) ||
(typeof SharedArrayBuffer !== "undefined" && data instanceof SharedArrayBuffer) ||
(typeof Blob !== "undefined" && data instanceof Blob)
);
}

/**
* Get the entries for bulk publish request.
* If entryIDs are missing, generate UUIDs for them.
Expand Down Expand Up @@ -264,7 +283,8 @@ export function getBulkPublishResponse(
function getType(o: any) {
// If the type is set in the constructor name, return the name
// e.g., Uint8Array, Int8Array, Buffer, SlowBuffer, Blob, etc.
if (o.constructor.name) {
// Objects created with Object.create(null) have no constructor.
if (o?.constructor?.name) {
return o.constructor.name;
}

Expand Down
33 changes: 31 additions & 2 deletions test/unit/utils/Client.util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,43 @@ describe("Client.util", () => {
},
);

it("falls back to binary content for an unnamed class instance", () => {
it("returns application/json for an unnamed class instance", () => {
const value = new (class {
value = 1;
})();
expect(value.constructor.name).toBe("");
expect(getContentType(value)).toBe("application/octet-stream");
expect(getContentType(value)).toBe("application/json");
});

it("returns application/json for a named class instance", () => {
class Order {
constructor(public id: number) {}
}
expect(getContentType(new Order(1))).toBe("application/json");
});

it("returns application/json for an object without a prototype", () => {
const value = Object.assign(Object.create(null), { foo: "bar" });
expect(getContentType(value)).toBe("application/json");
});

it("returns application/cloudevents+json for a CloudEvent class instance", () => {
class MyEvent {
id = "id";
source = "source";
type = "type";
specversion = "1.0";
}
expect(getContentType(new MyEvent())).toBe("application/cloudevents+json");
});

it.each([new Uint8Array([1]), new DataView(new ArrayBuffer(1)), new Blob(["a"])])(
"keeps application/octet-stream for binary data %p",
(value) => {
expect(getContentType(value)).toBe("application/octet-stream");
},
);

it("should return text/plain for a string", () => {
expect(getContentType("foobar")).toEqual("text/plain");
});
Expand Down
18 changes: 18 additions & 0 deletions test/unit/utils/Serializer.util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ describe("serializer", () => {
expect(data.contentType).toEqual("application/cloudevents+json");
});

describe("class instances", () => {
class Order {
constructor(public id: number, public item: string) {}
}

it("grpc/should serialize a class instance as JSON", () => {
const data = SerializerUtil.serializeGrpc(new Order(1, "book"));
expect(data.serializedData.toString()).toEqual('{"id":1,"item":"book"}');
expect(data.contentType).toEqual("application/json");
});

it("http/should serialize a class instance as JSON", () => {
const data = SerializerUtil.serializeHttp(new Order(1, "book"));
expect(data.serializedData).toEqual('{"id":1,"item":"book"}');
expect(data.contentType).toEqual("application/json");
});
});

runIt("should serialize a String to a Buffer of type text/plain", () => {
const data = SerializerUtil.serializeGrpc("hello-world");
expect(data.serializedData).toEqual(Buffer.from("hello-world"));
Expand Down
Loading