CesiumGS / cesium

An open-source JavaScript library for world-class 3D globes and maps :earth_americas:
https://cesium.com/cesiumjs/
Apache License 2.0
12.86k stars 3.47k forks source link

Plan to remove `request` lib #11095

Open onsummer opened 1 year ago

onsummer commented 1 year ago

Hi, I am planning to remove the old dependency request, since it is deprecated for 2 years, and I prefer to use node-fetch to instead of it.

Here was an discussion about what libs can alternative with request: Alternative libraries to request #3143

There are two things confused me:

// server.js
request.get(
  {
    url: remoteUrl.toString(),
    headers: filterHeaders(req, req.headers),
    encoding: null,
    proxy: proxy,
  },
  //eslint-disable-next-line no-unused-vars
  function (error, response, body) {
    let code = 500;

    if (response) {
      code = response.statusCode;
      res.header(filterHeaders(req, response.headers));
    }

    res.status(code).send(body);
  }
);

Will be:

import fetch from "node-fetch";

// ...

let statusCode = 500;
fetch(remoteUrl.toString(), {
  headers: filterHeaders(req, req.headers),
  agent: proxy // I do not know whether correct or not to set proxy here
}).then((responseStream) => {
  statusCode = responseStream.status;
  res.header(filterHeaders(req, responseStream.headers));
  return responseStream.arrayBuffer(); // use `arrayBuffer()` to pipe responseBody in raw bytes format
}).then((responseBody) => {
  res.status(statusCode).send(responseBody);
}).catch((err) => {
  // How to handle error?
})

Thanks!

onsummer commented 1 year ago

For code preview: server.js#L474

ggetz commented 1 year ago

Hi @onsummer! We agree node-fetch would be the best replacement here, which was used as a replacement for request in gulpfile.js.