Skip to content
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bloxwap/hyperliquid",
"version": "0.1.5",
"version": "0.1.6",
"description": "Blazing fast TypeScript Hyperliquid SDK.",
"license": "MIT",
"type": "module",
Expand Down
7 changes: 7 additions & 0 deletions src/transport/websocket/_id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ export function normalize(value: unknown): unknown {
const result: Record<string, unknown> = {};
for (const key of Object.keys(value).sort()) {
const item = normalize((value as Record<string, unknown>)[key]);
// Drop `undefined`-valued keys, matching what `JSON.stringify` does to this object on its
// way to the wire. Keeping them would leave the id and the echo carrying a field the server
// never receives and so can never echo back — and `isSubset` requires every key of the
// pending request to be present in the response, so such a subscription could never be
// matched to its own confirmation. A payload built as `{ dex: params.dex || undefined }`
// is the shape that hits this.
if (item === undefined) continue;
// `__proto__` is the one key with a setter on `Object.prototype`: plain assignment would
// invoke it and silently drop an own `__proto__` key a `JSON.parse`d payload can carry,
// collapsing two logically distinct payloads onto the same id. Define it as a data
Expand Down
11 changes: 11 additions & 0 deletions tests/transport/websocket/_id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ describe("requestToId", () => {
assert(requestToId([1, 2]) !== requestToId([2, 1]));
});

test("drops undefined-valued keys, matching what reaches the wire", () => {
// `JSON.stringify` omits an `undefined` value, so a payload built as
// `{ dex: params.dex || undefined }` goes out as `{"type":"allMids"}`. Keeping the key in the
// normalized form left the pending request demanding a field the server can never echo back,
// and `isSubset` then refused to match the subscription to its own confirmation — `allMids()`
// never resolved. The normalized form has to describe what was actually sent.
assertEquals(requestToId({ type: "allMids", dex: undefined }), requestToId({ type: "allMids" }));
assertEquals(requestToId({ type: "allMids", dex: undefined }), '{"type":"allMids"}');
assert(isSubset(normalize({ type: "allMids", dex: undefined }), { type: "allMids" }));
});

test("keeps an own __proto__ key from a parsed payload", () => {
const payload = JSON.parse('{"type":"l2Book","__proto__":{"coin":"BTC"}}') as Record<string, unknown>;
assertEquals(requestToId(payload), '{"__proto__":{"coin":"BTC"},"type":"l2Book"}');
Expand Down
Loading