Assiph Rolant
Assiph Rolant

Reputation: 59

RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: "length" is outside of buffer bounds in Node.js Pub/Sub

I'm encountering a RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: "length" is outside of buffer bounds error while working with Google Cloud Pub/Sub in a Node.js application. Below is a simplified version of the service I'm using:

@Controller()
export class EventController {
    private subscription; 

    @Get('subscribe')
    public subscribe(): void {
        console.log('Subscribing to event');
        this.stringSubscriber.subscribe();
        console.log('Subscribed to event');
    }

    private stringSubscriber = {
        subscribe: (): void => {
            try {
                this.subscription.on('message', async (message: Message): Promise<void> => {
                    logger.info('[subscribe] [onMessage] message', {
                        message: message.data.toString(),
                        deliveryAttempt: message.deliveryAttempt,
                    });
                    await this.handleEvent({ message });
                });
            } catch (error) {
                logger.error('[subscribe] Error while subscribing', { error });
                throw error;
            }
        }
    };

    private async handleEvent(event: { message: Message }): Promise<void> {
        ....
    }
}

it seems to fail when I send a subscribe request to the pod, however it does print the logs before:

Subscribing to event.

Subscribed to event.

node:events:498
       throw er; // Unhandled 'error' event
       ^
RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: "length" is outside of buffer bounds
    at proto.utf8Write (node:internal/buffer:1066:13)
    at Op.writeStringBuffer [as fn] (file:///usr/app/apps/demo5/dist/dummy.js:38758:13)
    at BufferWriter.finish (file:///usr/app/apps/demo5/dist/dummy.js:38708:14)
    at file:///usr/app/apps/demo5/dist/dummy.js:45199:113
    at Array.map (<anonymous>)
    at createPackageDefinition (file:///usr/app/apps/demo5/dist/dummy.js:45199:41)
    at Object.fromJSON (file:///usr/app/apps/demo5/dist/dummy.js:45226:14)
    at _GrpcClient.loadProtoJSON (file:///usr/app/apps/demo5/dist/dummy.js:68212:51)
    at new IamClient (file:///usr/app/apps/demo5/dist/dummy.js:84620:37)
    at new SubscriberClient (file:///usr/app/apps/demo5/dist/dummy.js:88646:26)



* Ensured that the subscription is properly created and initialized.
* Verified that the message handling logic is correct and does not introduce any buffer issues.

Upvotes: 5

Views: 3179

Answers (3)

Nicodemus Ngufuli
Nicodemus Ngufuli

Reputation: 121

I was facing same issue with node v23 Had to downgrade to node v20.18.1 and now it works just fine.

Upvotes: 0

Chrisdev3001
Chrisdev3001

Reputation: 71

try to update to last node version, in my case 22.8 works like a charm

Upvotes: 4

Samuel Padilla
Samuel Padilla

Reputation: 81

This is probably an error with your Node version. If you (or your deployment) is running on the latest version there appears to be a conflict with the GSM library.

Similar issue

[Issue documented on Github] (https://github.com/nodejs/node/issues/54518#issuecomment-2307687124)

Specify a lower version for node and should be good to go. The error will be fixed in a newer version of the library.

Upvotes: 7

Related Questions