Skip to content

Commit 76732c4

Browse files
committed
add conditional decoding
1 parent 39789d8 commit 76732c4

File tree

3 files changed

+70
-36
lines changed

3 files changed

+70
-36
lines changed

sdk/bun.lock

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,15 @@
6060
},
6161
},
6262
"overrides": {
63+
"debug": "<4.4.2",
64+
"supports-color": "7.2.0",
6365
"ansi-regex": "5.0.1",
66+
"color-convert": "<3.1.1",
6467
"ansi-styles": "4.3.0",
65-
"backslash": "<0.2.1",
68+
"wrap-ansi": "7.0.0",
6669
"chalk": "4.1.2",
67-
"chalk-template": "<1.1.1",
68-
"color-convert": "<3.1.1",
69-
"color-name": "<2.0.1",
70-
"color-string": "<2.1.1",
71-
"debug": "<4.4.2",
72-
"error-ex": "<1.3.3",
73-
"has-ansi": "<6.0.1",
74-
"is-arrayish": "<0.3.3",
75-
"simple-swizzle": "<0.2.3",
76-
"slice-ansi": "3.0.0",
7770
"strip-ansi": "6.0.1",
78-
"supports-color": "7.2.0",
79-
"supports-hyperlinks": "<4.1.1",
80-
"wrap-ansi": "7.0.0",
71+
"color-name": "<2.0.1",
8172
},
8273
"packages": {
8374
"@babel/code-frame": ["@babel/[email protected]", "", { "dependencies": { "@babel/helper-validator-identifier": "^7.25.9", "js-tokens": "^4.0.0", "picocolors": "^1.0.0" } }, "sha512-RJlIHRueQgwWitWgF8OdFYGZX328Ax5BCemNGlqHfplnRT9ESi8JkFlvaVYbS+UubVY6dpv87Fs2u5M29iNFVQ=="],

sdk/src/constituentMap/constituentMap.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,17 @@ export class ConstituentMap implements ConstituentMapInterface {
124124

125125
public async sync(): Promise<void> {
126126
try {
127+
// Check if zstddec is available (it's disabled in browser environments)
128+
const canUseZstd =
129+
typeof ZSTDDecoder !== 'undefined' && (ZSTDDecoder as any) !== false;
130+
const encoding = canUseZstd ? 'base64+zstd' : 'base64';
131+
127132
const rpcRequestArgs = [
128133
this.driftClient.program.programId.toBase58(),
129134
{
130135
commitment: this.commitment,
131136
filters: this.getFilters(),
132-
encoding: 'base64+zstd',
137+
encoding,
133138
withContext: true,
134139
},
135140
];
@@ -146,15 +151,32 @@ export class ConstituentMap implements ConstituentMapInterface {
146151

147152
const promises = rpcResponseAndContext.value.map(
148153
async (programAccount) => {
149-
const compressedUserData = Buffer.from(
150-
programAccount.account.data[0],
151-
'base64'
152-
);
153-
const decoder = new ZSTDDecoder();
154-
await decoder.init();
155-
const buffer = Buffer.from(
156-
decoder.decode(compressedUserData, MAX_CONSTITUENT_SIZE_BYTES)
157-
);
154+
let buffer: Buffer;
155+
156+
if (canUseZstd) {
157+
// Decompress zstd data
158+
const compressedData = Buffer.from(
159+
programAccount.account.data[0],
160+
'base64'
161+
);
162+
// Handle both default and named exports for browser/webpack compatibility
163+
let DecoderClass = ZSTDDecoder;
164+
if (
165+
typeof ZSTDDecoder === 'object' &&
166+
(ZSTDDecoder as any).default
167+
) {
168+
DecoderClass = (ZSTDDecoder as any).default;
169+
}
170+
const decoder = new (DecoderClass as any)();
171+
await decoder.init();
172+
buffer = Buffer.from(
173+
decoder.decode(compressedData, MAX_CONSTITUENT_SIZE_BYTES)
174+
);
175+
} else {
176+
// Use regular base64 data (not compressed)
177+
buffer = Buffer.from(programAccount.account.data[0], 'base64');
178+
}
179+
158180
const key = programAccount.pubkey.toString();
159181
const currAccountWithSlot = this.getWithSlot(key);
160182

sdk/src/userMap/userMap.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,17 @@ export class UserMap implements UserMapInterface {
434434
});
435435

436436
try {
437+
// Check if zstddec is available (it's disabled in browser environments)
438+
const canUseZstd =
439+
typeof ZSTDDecoder !== 'undefined' && (ZSTDDecoder as any) !== false;
440+
const encoding = canUseZstd ? 'base64+zstd' : 'base64';
441+
437442
const rpcRequestArgs = [
438443
this.driftClient.program.programId.toBase58(),
439444
{
440445
commitment: this.commitment,
441446
filters: this.getFilters(),
442-
encoding: 'base64+zstd',
447+
encoding,
443448
withContext: true,
444449
},
445450
];
@@ -459,19 +464,35 @@ export class UserMap implements UserMapInterface {
459464
const programAccountBufferMap = new Map<string, Buffer>();
460465
const decodingPromises = rpcResponseAndContext.value.map(
461466
async (programAccount) => {
462-
const compressedUserData = Buffer.from(
463-
programAccount.account.data[0],
464-
'base64'
465-
);
466-
const decoder = new ZSTDDecoder();
467-
await decoder.init();
468-
const userBuffer = decoder.decode(
469-
compressedUserData,
470-
MAX_USER_ACCOUNT_SIZE_BYTES
471-
);
467+
let userBuffer: Buffer;
468+
469+
if (canUseZstd) {
470+
// Decompress zstd data
471+
const compressedUserData = Buffer.from(
472+
programAccount.account.data[0],
473+
'base64'
474+
);
475+
// Handle both default and named exports for browser/webpack compatibility
476+
let DecoderClass = ZSTDDecoder;
477+
if (
478+
typeof ZSTDDecoder === 'object' &&
479+
(ZSTDDecoder as any).default
480+
) {
481+
DecoderClass = (ZSTDDecoder as any).default;
482+
}
483+
const decoder = new (DecoderClass as any)();
484+
await decoder.init();
485+
userBuffer = Buffer.from(
486+
decoder.decode(compressedUserData, MAX_USER_ACCOUNT_SIZE_BYTES)
487+
);
488+
} else {
489+
// Use regular base64 data (not compressed)
490+
userBuffer = Buffer.from(programAccount.account.data[0], 'base64');
491+
}
492+
472493
programAccountBufferMap.set(
473494
programAccount.pubkey.toString(),
474-
Buffer.from(userBuffer)
495+
userBuffer
475496
);
476497
}
477498
);

0 commit comments

Comments
 (0)