node-polyfill-fetch.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. var _nodeFetch = _interopRequireWildcard(require("next/dist/compiled/node-fetch"));
  3. function _getRequireWildcardCache() {
  4. if (typeof WeakMap !== "function") return null;
  5. var cache = new WeakMap();
  6. _getRequireWildcardCache = function() {
  7. return cache;
  8. };
  9. return cache;
  10. }
  11. function _interopRequireWildcard(obj) {
  12. if (obj && obj.__esModule) {
  13. return obj;
  14. }
  15. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  16. return {
  17. default: obj
  18. };
  19. }
  20. var cache = _getRequireWildcardCache();
  21. if (cache && cache.has(obj)) {
  22. return cache.get(obj);
  23. }
  24. var newObj = {};
  25. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  26. for(var key in obj){
  27. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  28. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  29. if (desc && (desc.get || desc.set)) {
  30. Object.defineProperty(newObj, key, desc);
  31. } else {
  32. newObj[key] = obj[key];
  33. }
  34. }
  35. }
  36. newObj.default = obj;
  37. if (cache) {
  38. cache.set(obj, newObj);
  39. }
  40. return newObj;
  41. }
  42. // Polyfill fetch() in the Node.js environment
  43. if (!global.fetch) {
  44. const agent = ({ protocol })=>protocol === "http:" ? global.__NEXT_HTTP_AGENT : global.__NEXT_HTTPS_AGENT;
  45. const fetchWithAgent = (url, opts, ...rest)=>{
  46. if (!opts) {
  47. opts = {
  48. agent
  49. };
  50. } else if (!opts.agent) {
  51. opts.agent = agent;
  52. }
  53. return (0, _nodeFetch).default(url, opts, ...rest);
  54. };
  55. global.fetch = fetchWithAgent;
  56. global.Headers = _nodeFetch.Headers;
  57. global.Request = _nodeFetch.Request;
  58. global.Response = _nodeFetch.Response;
  59. }
  60. //# sourceMappingURL=node-polyfill-fetch.js.map