如何使用TypeScript解码通过axios获取的Protobuf数据?

huangapple go评论63阅读模式
英文:

How to decode protobuf obtained via axios using typescript

问题

我是新手typescript,我正在尝试理解如何使用axios下载实时gtfs数据(以protobuf形式)。当我尝试运行此代码时,出现了非法缓冲错误。我已在下面完整地发布了错误。

英文:

I'm new to typescript, and I'm trying to understand how to use axios to download real-time gtfs data (which is in protobuf form). I'm getting an illegal buffer error when I try to run this code. I've posted the error in full below.

import axios from "axios";
import * as GtfsRealtimeBindings from "gtfs-realtime-bindings";

const url = "https://www.rtd-denver.com/files/gtfs-rt/TripUpdate.pb";
export type RealtimeGtfsUrl = string;

export async function decodeRealtimeGtfs(
  url: RealtimeGtfsUrl,
): Promise<GtfsRealtimeBindings.transit_realtime.FeedMessage> {
  return await axios.get(url).then((response) => {
    return GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(
      response.data,
    );
  });
}

const result = decodeRealtimeGtfs(url);

/Users/.../playground/node_modules/protobufjs/src/reader.js:47
throw Error("illegal buffer");
^

Error: illegal buffer
at create_typed_array (/Users/.../playground/node_modules/protobufjs/src/reader.js:47:15)
at create_buffer (/Users/.../playground/node_modules/protobufjs/src/reader.js:63:23)
at Function.create_buffer_setup (/Users/.../playground/node_modules/protobufjs/src/reader.js:64:15)
at Function.decode (/Users/.../playground/node_modules/gtfs-realtime-bindings/gtfs-realtime.js:134:34)
at /Users/.../playground/test.cjs:48:82
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

答案1

得分: 1

The response is in a binary format where Axios defaults to expecting JSON / plain text.

The decoder expects an ArrayBuffer so all you should need to do is tell Axios what response type to expect.

Also, don't mix await with .then(); it just makes for confusing code.

export async function decodeRealtimeGtfs(
  url: RealtimeGtfsUrl,
): Promise<GtfsRealtimeBindings.transit_realtime.FeedMessage> {
  const { data } = await axios.get(url, { responseType: "arraybuffer" });
  return GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(data);
}
英文:

The response is in a binary format where Axios defaults to expecting JSON / plain text.

The decoder expects an ArrayBuffer so all you should need to do is tell Axios what response type to expect.

Also, don't mix await with .then(); it just makes for confusing code.

export async function decodeRealtimeGtfs(
  url: RealtimeGtfsUrl,
): Promise&lt;GtfsRealtimeBindings.transit_realtime.FeedMessage&gt; {
  const { data } = await axios.get(url, { responseType: &quot;arraybuffer&quot; });
  return GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(data);
}

huangapple
  • 本文由 发表于 2023年5月11日 10:00:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定