在 Node.js 应用程序中使用 Google Cloud Pub/Sub 时,我遇到了 RangeError [ERR_BUFFER_OUT_OF_BOUNDS]:“length”超出缓冲区边界错误。以下是我正在使用的服务的简化版本:
@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> {
....
}
}
当我向 Pod 发送订阅请求时,它似乎失败了,但它确实打印了之前的日志:
订阅活动。
订阅活动。
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.
这可能是您的 Node 版本存在错误。如果您(或您的部署)在最新版本上运行,则似乎与 GSM 库存在冲突。
[Github 上记录的问题] (https://github.com/nodejs/node/issues/54518#issuecomment-2307687124)
为节点指定一个较低的版本,应该就可以了。该错误将在新版本的库中修复。