Skip to content

Commit

Permalink
Add encoding tests And implementation for circular references
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidWeiss2 committed Feb 20, 2024
1 parent 5a94583 commit 74d205c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 12 deletions.
62 changes: 62 additions & 0 deletions spec/common/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,68 @@ describe("encoding/decoding", () => {
});
});

it("encodes object with self reference", () => {
class TestClass {
foo: string;
bar: number;
self: TestClass;
constructor(foo: string, bar: number) {
this.foo = foo;
this.bar = bar;
this.self = this;
}
}
const testObject = new TestClass("hello", 1);
expect(https.encode(testObject)).to.deep.equal({
foo: "hello",
bar: 1,
self: { foo: "hello", bar: 1 },
});
});

it("encodes object with circular reference", () => {
class TestClass {
foo: string;
bar: number;
self: TestClass;
constructor(foo: string, bar: number) {
this.foo = foo;
this.bar = bar;
this.self = this;
}
}
const testObject1 = new TestClass("hello", 1);
const testObject2 = new TestClass("world", 2);
testObject1.self = testObject2;
testObject2.self = testObject1;
expect(https.encode(testObject1)).to.deep.equal({
foo: "hello",
bar: 1,
self: { foo: "world", bar: 2, self: { foo: "hello", bar: 1 } },
});
})

it("encodes object with circular reference in nested object", () => {
class TestClass {
foo: string;
bar: number;
nested: {
self: TestClass;
};
constructor(foo: string, bar: number) {
this.foo = foo;
this.bar = bar;
this.nested = { self: this };
}
}
const testObject = new TestClass("hello", 1);
expect(https.encode(testObject)).to.deep.equal({
foo: "hello",
bar: 1,
nested: { self: { foo: "hello", bar: 1 } },
});
});

it("encodes function as an empty object", () => {
expect(https.encode(() => "foo")).to.deep.equal({});
});
Expand Down
38 changes: 26 additions & 12 deletions src/common/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,16 @@ const LONG_TYPE = "type.googleapis.com/google.protobuf.Int64Value";
/** @hidden */
const UNSIGNED_LONG_TYPE = "type.googleapis.com/google.protobuf.UInt64Value";

/** @hidden */
const SELF_REFERENCE_WEAKMAP = new WeakMap<Object,Object>();
/**
* Encodes arbitrary data in our special format for JSON.
* This is exposed only for testing.
*/
/** @hidden */
export function encode(data: any): any {
export function encode(
data: unknown
): any {
if (data === null || typeof data === "undefined") {
return null;
}
Expand All @@ -421,18 +425,28 @@ export function encode(data: any): any {
if (Array.isArray(data)) {
return data.map(encode);
}
if (typeof data === "object" || typeof data === "function") {
// Sadly we don't have Object.fromEntries in Node 10, so we can't use a single
// list comprehension
const obj: Record<string, any> = {};
for (const [k, v] of Object.entries(data)) {
obj[k] = encode(v);
}
return obj;
const isObjectOrFunction = typeof data === "object" || typeof data === "function";

if (!isObjectOrFunction) {
// If we got this far, the data is not encodable.
logger.error("Data cannot be encoded in JSON.", data);
throw new Error(`Data cannot be encoded in JSON: ${data}`);
}
// implementation of an hidden argument to keep track of objects that have been encoded
if (SELF_REFERENCE_WEAKMAP.has(data)) {
return { ...SELF_REFERENCE_WEAKMAP.get(data) }; // return a shallow copy of the object
}

// Sadly we don't have Object.fromEntries in Node 10, so we can't use a single
// list comprehension
const obj: Record<string, any> = {};

SELF_REFERENCE_WEAKMAP.set(data, obj);

for (const [k, v] of Object.entries(data)) {
obj[k] = encode(v);
}
// If we got this far, the data is not encodable.
logger.error("Data cannot be encoded in JSON.", data);
throw new Error(`Data cannot be encoded in JSON: ${data}`);
return obj;
}

/**
Expand Down

0 comments on commit 74d205c

Please sign in to comment.