9

我正在尝试订阅来自 Kinesis Shard 的事件。但是执行SubscribeToShardCommand挂起5分钟(订阅超时)然后抛出错误:

(node:2667) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token  in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/mjpolak/Documents/company/project/node_modules/@aws-sdk/client-kinesis/protocols/Aws_json1_1.ts:4020:19
    at processTicksAndRejections (internal/process/task_queues.js:88:5)
    at Object.deserializeAws_json1_1SubscribeToShardCommand (/home/mjpolak/Documents/company/project/node_modules/@aws-sdk/client-kinesis/protocols/Aws_json1_1.ts:2583:21)
    at /home/mjpolak/Documents/company/project/node_modules/@aws-sdk/middleware-serde/src/deserializerMiddleware.ts:20:18
    at /home/mjpolak/Documents/company/project/node_modules/@aws-sdk/middleware-signing/src/middleware.ts:26:22
    at StandardRetryStrategy.retry (/home/mjpolak/Documents/company/project/node_modules/@aws-sdk/middleware-retry/src/defaultStrategy.ts:125:38)
    at /home/mjpolak/Documents/company/project/node_modules/@aws-sdk/middleware-logger/src/loggerMiddleware.ts:21:20
    at /home/mjpolak/Documents/company/project/kinesis-subscribe.ts:85:29

我正在寻找可以让我成功订阅的帮助。

完整代码:

const { StreamDescription } = await client.send(
        new DescribeStreamCommand({
            StreamName: 'streamName',
        }),
    );
    const { StreamARN } = StreamDescription;

    const { Consumers } = await client.send(
        new ListStreamConsumersCommand({
            StreamARN,
        }),
    );

    let Consumer = Consumers.find((x) => x.ConsumerName == 'tester');

    if (Consumer == null) {
        const consumerData = await client.send(
            new RegisterStreamConsumerCommand({
                ConsumerName: 'tester',
                StreamARN,
            }),
        );
        Consumer = consumerData.Consumer;
    }

    console.log(`StreaARM: ${StreamARN}`);

    console.log(`Consumer: ${Consumer.ConsumerARN}`);

    const { EventStream } = await client.send(
        new SubscribeToShardCommand({
            ConsumerARN: Consumer.ConsumerARN,
            ShardId: 'shardId-000000000000',
            StartingPosition: {
                Type: 'LATEST'
            },
        }),
    );
    console.log("Consuming");

通过调查通过 Wireshark 的通信,我可以确认 HTTP 连接已建立,并且 AWS 将一些数据推送到我的客户端。所以我猜测客户端库中有某种错误会保留块而不是返回结果。

我还在 SDK 存储库上创建了错误: https ://github.com/aws/aws-sdk-js-v3/issues/2418

4

1 回答 1

0

日志显示 JSON 解析会引发错误,当您收到 HTML 或 XML 格式的响应但预期是 JSON 响应时会发生这种情况。尝试在控制台上记录响应并查看其格式是否正确。

于 2021-05-26T16:35:14.387 回答