Skip to content
Closed
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
40 changes: 39 additions & 1 deletion planet/js/RequestManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,44 @@
exported RequestManager
*/

function stableStringify(value) {
const inProgress = new WeakSet();

function normalize(v) {
if (Array.isArray(v)) {
if (inProgress.has(v)) {
throw new TypeError("Converting circular structure to JSON");
}
inProgress.add(v);
try {
return v.map(normalize);
} finally {
inProgress.delete(v);
}
}

if (v && typeof v === "object" && v.constructor === Object) {
if (inProgress.has(v)) {
throw new TypeError("Converting circular structure to JSON");
}
inProgress.add(v);
try {
const sorted = {};
for (const key of Object.keys(v).sort()) {
sorted[key] = normalize(v[key]);
}
return sorted;
} finally {
inProgress.delete(v);
}
}

return v;
}

return JSON.stringify(normalize(value));
}

/**
* RequestManager - Handles rate limiting, request throttling, and retry logic
* for Planet API calls. Prevents API abuse and improves reliability.
Expand Down Expand Up @@ -61,7 +99,7 @@ class RequestManager {
const action = data.action || "unknown";
const params = { ...data };
delete params["api-key"];
return `${action}:${JSON.stringify(params)}`;
return `${action}:${stableStringify(params)}`;
}

/**
Expand Down
64 changes: 64 additions & 0 deletions planet/js/__tests__/RequestManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,45 @@ describe("RequestManager", () => {
});
expect(key1).toBe(key2);
});

it("should generate same key regardless of property insertion order", () => {
const key1 = requestManager.generateRequestKey({
action: "getProject",
id: 1,
foo: "x"
});

// Same logical payload, different insertion order
const key2 = requestManager.generateRequestKey({
foo: "x",
id: 1,
action: "getProject"
});

expect(key1).toBe(key2);
});

it("should generate same key for nested objects with different key order", () => {
const key1 = requestManager.generateRequestKey({
action: "search",
filters: {
b: 2,
a: 1
},
list: [{ y: 2, x: 1 }]
});

const key2 = requestManager.generateRequestKey({
list: [{ x: 1, y: 2 }],
filters: {
a: 1,
b: 2
},
action: "search"
});

expect(key1).toBe(key2);
});
});

describe("throttledRequest", () => {
Expand Down Expand Up @@ -118,6 +157,31 @@ describe("RequestManager", () => {
expect(callCount).toBe(1);
});

it("should deduplicate concurrent requests even when object key order differs", async () => {
let callCount = 0;
const mockResult = { success: true, data: "test" };

const mockRequestFn = jest.fn(callback => {
callCount++;
setTimeout(() => callback(mockResult), 50);
});

const promise1 = requestManager.throttledRequest(
{ action: "test", id: 1, foo: "x" },
mockRequestFn
);
const promise2 = requestManager.throttledRequest(
{ foo: "x", id: 1, action: "test" },
mockRequestFn
);

const [result1, result2] = await Promise.all([promise1, promise2]);

expect(result1).toEqual(mockResult);
expect(result2).toEqual(mockResult);
expect(callCount).toBe(1);
});

it("should increment cached responses for deduplicated requests", async () => {
const mockResult = { success: true };
const mockRequestFn = callback => {
Expand Down
Loading