dfinity / agent-js

A collection of libraries and tools for building software around the Internet Computer, in JavaScript.

Home Page:https://agent-js.icp.xyz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Headers is not used in nodejs env.

neeboo opened this issue · comments

headers: new Headers({

Problem:
Headers of browser is not used in nodejs environment. If we use agent in nodejs, it will be giving error like following:

ReferenceError: Headers is not defined
    at HttpAgent.call 
...

Proposal:
Consider to use cross-fetch and switch Headers between different env.

import { fetch, Headers } from 'cross-fetch';

if (!globalThis.fetch) {
  (globalThis as any).fetch = fetch;
  (globalThis as any).Headers = Headers;
}

if (!global.fetch) {
  (global as any).fetch = fetch;
  (global as any).Headers = Headers;
}


 new Headers()
...

Okay, I can revert the Headers implementation and go back to a basic object!

For what is worth, I had the Headers is not defined isssue too with NodeJS v16 but, do not face the issue with NodeJS LTS - i.e. are Headers implemented as of v18?

My reversion is in using them in the implementation - Headers will still be supported by agent-js in contexts where it's available

I'm also getting the Headers is not defined error.
My environment:

  • Hardware: Raspberry Pi 4
  • OS: Ubuntu 22.04 LTS (aarch64)
  • Node.js: v16.19.1
  • @dfinity/agent versions: v0.15.5,v0.15.4,v0.15.3,v0.15.2

On version v0.15.1 instead I don't get that error.