Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

built-in fetch in node 20 issues error #1130

Open
eouia opened this issue Oct 17, 2023 · 6 comments
Open

built-in fetch in node 20 issues error #1130

eouia opened this issue Oct 17, 2023 · 6 comments
Labels

Comments

@eouia
Copy link

eouia commented Oct 17, 2023

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 eouia added the bug label Oct 17, 2023
@greg-db
Copy link
Contributor

greg-db commented Oct 17, 2023

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

@greg-db
Copy link
Contributor

greg-db commented Oct 18, 2023

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

@eouia
Copy link
Author

eouia commented Oct 18, 2023

Thanks. I think this issue is solved now.

@eouia eouia closed this as completed Oct 18, 2023
@eouia
Copy link
Author

eouia commented Oct 18, 2023

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 eouia reopened this Oct 18, 2023
@greg-db
Copy link
Contributor

greg-db commented Oct 18, 2023

Thanks! I'll raise this with the team.

@decovicdev
Copy link

decovicdev commented Nov 25, 2023

@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);

coderaiser added a commit to coderaiser/dropbox-sdk-js that referenced this issue Mar 16, 2024
@decovicdev decovicdev mentioned this issue Apr 23, 2024
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants