dropbox-sdk-js icon indicating copy to clipboard operation
dropbox-sdk-js copied to clipboard

built-in `fetch` in node 20 issues error

Open eouia opened this issue 2 years ago • 8 comments

Describe the bug In node version 20 (v20.8.0), built-in fetch() could issue an error. Version 18 (v18.5.0) has no issue.

To Reproduce

/examples/javascript/PKCE-backend/code_flow_example.js

//const fetch = require('node-fetch');
const app = require('express')();

To use built-in fetch, I comment out node-fetch.

Expected Behavior In V18, fetch and node-fetch worked without issue.

Actual Behavior In V20, built-in fetch claims the issue after app consent.

% node examples/javascript/PKCE-backend/code_flow_example.js
code:-mlcRpj5b6sAA....
DropboxResponseError: Response failed with a 429 code
    at /Users/eouia/Workspace/dropbox-sdk-js/cjs/src/response.js:34:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  status: 429,
  headers: HeadersList {
    cookies: null,
    [Symbol(headers map)]: Map(6) {
      'vary' => [Object],
      'x-dropbox-response-origin' => [Object],
      'date' => [Object],
      'server' => [Object],
      'x-dropbox-request-id' => [Object],
      'content-length' => [Object]
    },
    [Symbol(headers map sorted)]: null
  },
  error: ''
}

Versions

  • SDK version 10.34.0
  • Node v20 (v20.8.0)
  • What platform are you using? => Developing in MacOS Sonoma but the final product will be deployed on Raspbian OS.

eouia avatar Oct 17 '23 15:10 eouia

Thanks for the report! We're looking into it. I'll follow up here once I have an update for you.

greg-db avatar Oct 17 '23 20:10 greg-db

We've updated the server configuration. Can you try again now and let me know if you're still seeing this issue?

greg-db avatar Oct 18 '23 12:10 greg-db

Thanks. I think this issue is solved now.

eouia avatar Oct 18 '23 13:10 eouia

Sorry for reopening. I found filesDownload also met fetch related issue.

const { result } = await dbx.filesDownload({ path: item.path_lower })
TypeError: res.buffer is not a function
    at /.../node_modules/dropbox/cjs/src/response.js:67:11
    at new Promise (<anonymous>)
    at parseDownloadResponse (/.../node_modules/dropbox/cjs/src/response.js:61:10)
    at /.../node_modules/dropbox/cjs/src/dropbox.js:146:52
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

The built-in fetch of nodeJS (in V18 and V20) has this problem and node-fetch has no issue. It seems to be using .buffer() method of node-fetch, but the fetch would have .arrayBuffer() instead. As far as I know, node-fetch also serve .arrayBuffer() to solve this issue.

I think the built-in fetch is not compatible with this SDK completely at this moment.

eouia avatar Oct 18 '23 15:10 eouia

Thanks! I'll raise this with the team.

greg-db avatar Oct 18 '23 17:10 greg-db

@eouia This code snippet is a TypeScript function that utilizes the Dropbox SDK

and this is how I solved the fetch error.

import { Dropbox } from "dropbox";

import { env } from "@/validation/env";
import { Account } from "@/types/db";

export default function dropbox(account: Account) {
  if (!account) {
    throw new Error("Dropbox account not found");
  }

  const customFetch = async (input: RequestInfo, init?: RequestInit): Promise<Response> => {
    const response = await globalThis.fetch(input, init);

    return Object.defineProperty(response, "buffer", {
      value: response.arrayBuffer,
    });
  };

  return new Dropbox({
    fetch: customFetch,
    clientId: env.DROPBOX_ID,
    clientSecret: env.DROPBOX_SECRET,
    accessToken: account.access_token!,
    refreshToken: account.refresh_token!,
  });
}

remember to treat the buffer as an arrayBuffer and not buffer if you want to save to fs.

const nodeBuffer = Buffer.from(arrBuffer);

solo-samurai avatar Nov 25 '23 16:11 solo-samurai

I suspect the old node-fetch dependency is also triggering a punycode deprecation warning via and old whatwg-url dependency

Downchuck avatar Aug 13 '24 05:08 Downchuck

@Downchuck If you're seeing an issue, please an open issue and share the details, e.g., the steps to reproduce it and the output you get, so we can look into it. Thanks in advance!

greg-db avatar Aug 13 '24 14:08 greg-db