// node-fetch shim for CF Workers — forwards to the runtime's native fetch.
// node-fetch is pulled in transitively by @apple/app-store-server-library (IAP
// JWS verification). On Node it drags in a polyfill chain that is dead weight on
// Workers (which has native fetch/URL/TextDecoder):
//   encoding (iconv-lite CJK code tables) 481KB + tr46 (IDNA map) 259KB
//   + whatwg-url 22KB + node-fetch 19KB ≈ 781KB, never executed at runtime.
// Forwarding node-fetch → native fetch drops that whole chain (~754KB raw).
// Only implements the node-fetch v2 export surface actually referenced.

export default globalThis.fetch.bind(globalThis);

export const Headers = globalThis.Headers;
export const Request = globalThis.Request;
export const Response = globalThis.Response;
export const FormData = globalThis.FormData;
export const Blob = globalThis.Blob;

export class FetchError extends Error {
  type: string;
  constructor(message: string, type = 'system') {
    super(message);
    this.name = 'FetchError';
    this.type = type;
  }
}

export class AbortError extends Error {
  type = 'aborted';
  constructor(message: string) {
    super(message);
    this.name = 'AbortError';
  }
}

export const isRedirect = (code: number) => [301, 302, 303, 307, 308].includes(code);
