Skip to content

Commit 8a885d3

Browse files
authored
Merge branch 'main' into main
2 parents c3289b8 + a7b0dda commit 8a885d3

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ See the [official Pub/Sub API repo](https://github.com/developerforce/pub-sub-ap
1717
- [Work with flow control for high volumes of events](#work-with-flow-control-for-high-volumes-of-events)
1818
- [Handle gRPC stream lifecycle events](#handle-grpc-stream-lifecycle-events)
1919
- [Use a custom logger](#use-a-custom-logger)
20+
- [Common Issues](#common-issues)
2021
- [Reference](#reference)
2122
- [PubSubApiClient](#pubsubapiclient)
2223
- [PubSubEventEmitter](#pubsubeventemitter)
@@ -124,7 +125,18 @@ Here's an example that will get you started quickly. It listens to a single acco
124125
`(${eventEmitter.getReceivedEventCount()}/${eventEmitter.getRequestedEventCount()} ` +
125126
`events received so far)`
126127
);
127-
console.log(JSON.stringify(event, null, 2));
128+
// Safely log event as a JSON string
129+
console.log(
130+
JSON.stringify(
131+
event,
132+
(key, value) =>
133+
/* Convert BigInt values into strings and keep other types unchanged */
134+
typeof value === 'bigint'
135+
? value.toString()
136+
: value,
137+
2
138+
)
139+
);
128140
});
129141
} catch (error) {
130142
console.error(error);
@@ -345,6 +357,31 @@ const logger = pino();
345357
const client = new PubSubApiClient(logger);
346358
```
347359
360+
## Common Issues
361+
362+
### TypeError: Do not know how to serialize a BigInt
363+
364+
If you attempt to call `JSON.stringify` on an event you will likely see the following error:
365+
366+
> TypeError: Do not know how to serialize a BigInt
367+
368+
This happens when an integer value stored in an event field exceeds the range of the `Number` JS type (this typically happens with `commitNumber` values). In this case, we use a `BigInt` type to safely store the integer value. However, the `BigInt` type is not yet supported in standard JSON representation (see step 10 in the [BigInt TC39 spec](https://tc39.es/proposal-bigint/#sec-serializejsonproperty)) so this triggers a `TypeError`.
369+
370+
To avoid this error, use a replacer function to safely escape BigInt values so that they can be serialized as a string (or any other format of your choice) in JSON:
371+
372+
```js
373+
// Safely log event as a JSON string
374+
console.log(
375+
JSON.stringify(
376+
event,
377+
(key, value) =>
378+
/* Convert BigInt values into strings and keep other types unchanged */
379+
typeof value === 'bigint' ? value.toString() : value,
380+
2
381+
)
382+
);
383+
```
384+
348385
## Reference
349386
350387
### PubSubApiClient

dist/client.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class PubSubApiClient {
4040
* @returns {Promise<EventEmitter>} Promise that holds an emitter that allows you to listen to received events and stream lifecycle events
4141
* @memberof PubSubApiClient.prototype
4242
*/
43-
subscribeFromReplayId(topicName: string, replayId: number, numRequested?: number | null): Promise<EventEmitter>;
43+
subscribeFromReplayId(topicName: string, numRequested?: number | null, replayId: number): Promise<EventEmitter>;
4444
/**
4545
* Subscribes to a topic.
4646
* @param {string} topicName name of the topic that we're subscribing to

dist/client.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)