turbopuffer / turbopuffer-typescript

Official Typescript API client library for turbopuffer.com
MIT License
8 stars 1 forks source link

Error: this.agent.request is not a function #28

Open mrturck opened 2 months ago

mrturck commented 2 months ago

Hello I am getting this error when using the Typescript SDK, full error below

Error processing source: 174 |     }
175 | }
176 | /** An error class for errors returned by the turbopuffer API. */
177 | class TurbopufferError extends Error {
178 |     constructor(error, { status, cause }) {
179 |         super(error, { cause: cause });
              ^
error: fetch failed: this.agent.request is not a function. (In 'this.agent.request({
            origin: this.origin,
            path,
            method,
            headers,
            body: requestBody
          })', 'this.agent.request' is undefined)
      at new TurbopufferError (/Users/mturck/wxr/worker/node_modules/@turbopuffer/turbopuffer/dist/httpClient.js:179:9)
      at /Users/mturck/wxr/worker/node_modules/@turbopuffer/turbopuffer/dist/httpClient.js:109:33

86 |         let response_start;
87 |         for (let attempt = 0; attempt < maxAttempts; attempt++) {
88 |             error = null;
89 |             request_start = performance.now();
90 |             try {
91 |                 response = await this.agent.request({

Details:

pushrax commented 2 months ago

Can you check your package.lock file to see which version of the undici dependency was installed? As far as I know this isn't a new API in undici, but good to double check that.

mrturck commented 2 months ago

Using bun, but here's what bun.lockb has:

"@turbopuffer/turbopuffer@^0.5.10": 
  version "0.5.10"
  resolved "https://registry.npmjs.org/@turbopuffer/turbopuffer/-/turbopuffer-0.5.10.tgz"
  integrity sha512-tQgouaKZPEMSf1y1p6lksmgRZ+Toj248AkPxiCdsZNoHVDWVTZgXHLet9cie4cz4oAaaVE0NWkoMyO0KafFuRA==
  dependencies: 
    pako "^2.1.0"
    undici "^6.19.8"
pushrax commented 2 months ago

Got it. The SDK definitely works in node, but I was able to reproduce the issue in bun.

It looks like bun is trying to polyfill undici in their environment, but it only has partial support. For example, they implemented undici.request in https://github.com/oven-sh/bun/pull/2136 but only on the module level, not on the Agent/Dispatcher.

The reason we use undici.Agent.request is that this allows fine tuned configuration of the connection pool and request options, and in our customer's large scale node deployments we've been able to achieve a significant performance improvement over both node's built in fetch and undici.fetch

I'm guessing that bun's fetch implementation is just a lot better than node's. This issue boils down to just using fetch in non-node environments https://github.com/turbopuffer/turbopuffer-typescript/issues/20

pushrax commented 2 months ago

By the way, the official stance of bun is to be compatible directly with node https://twitter.com/jarredsumner/status/1700497869160268155

It looks like other libraries are using undici.Pool.request, which is also unimplemented https://github.com/NomicFoundation/hardhat/issues/4383 https://github.com/oven-sh/bun/issues/7920, and it's an open issue to fix in bun.

But we will work around it in the turbopuffer client anyway, as we want to support all JS environments, not just node.