Skip to content

Commit 9d57ecb

Browse files
committed
fix: support for 64bit long values
1 parent fbd5e4b commit 9d57ecb

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/client.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,39 @@ import SalesforceAuth from './utils/auth.js';
3939
* @protected
4040
*/
4141

42+
/**
43+
* Custom Long Avro type used for deserializing large numbers with BitInt.
44+
* This fixes a deserialization bug with Avro not supporting large values.
45+
* @private
46+
*/
47+
const CUSTOM_LONG_AVRO_TYPE = avro.types.LongType.using({
48+
fromBuffer: (buf) => {
49+
const big = buf.readBigInt64LE();
50+
if (big > Number.MAX_SAFE_INTEGER) {
51+
return big;
52+
}
53+
return Number(BigInt.asIntN(64, big));
54+
},
55+
toBuffer: (n) => {
56+
const buf = Buffer.alloc(8);
57+
if (n instanceof BigInt) {
58+
buf.writeBigInt64LE(n);
59+
} else {
60+
buf.writeBigInt64LE(BigInt(n));
61+
}
62+
return buf;
63+
},
64+
fromJSON: BigInt,
65+
toJSON: Number,
66+
isValid: (n) => {
67+
const type = typeof n;
68+
return type === 'bigint' || type === 'number';
69+
},
70+
compare: (n1, n2) => {
71+
return n1 === n2 ? 0 : n1 < n2 ? -1 : 1;
72+
}
73+
});
74+
4275
/**
4376
* Client for the Salesforce Pub/Sub API
4477
* @alias PubSubApiClient
@@ -285,7 +318,7 @@ export default class PubSubApiClient {
285318
// eslint-disable-next-line no-empty
286319
} catch (error) {}
287320
const message = replayId
288-
? `Failed to parse event with replay ID ${this.replayId}`
321+
? `Failed to parse event with replay ID ${replayId}`
289322
: `Failed to parse event with unknown replay ID (latest replay ID was ${latestReplayId})`;
290323
const parseError = new EventParseError(
291324
message,
@@ -295,6 +328,7 @@ export default class PubSubApiClient {
295328
latestReplayId
296329
);
297330
eventEmitter.emit('error', parseError);
331+
this.#logger.error(parseError);
298332
}
299333
// Emit a 'lastevent' event when reaching the last requested event count
300334
if (
@@ -434,7 +468,9 @@ export default class PubSubApiClient {
434468
if (schemaError) {
435469
reject(schemaError);
436470
} else {
437-
const schemaType = avro.parse(res.schemaJson);
471+
const schemaType = avro.parse(res.schemaJson, {
472+
registry: { long: CUSTOM_LONG_AVRO_TYPE }
473+
});
438474
this.#logger.info(
439475
`Topic schema loaded: ${topicName}`
440476
);

0 commit comments

Comments
 (0)