udacity / cloudflare-typescript-workers

Types and mocks for building a tested Typescript Cloudflare Worker, generates three NPM packages
Apache License 2.0
139 stars 12 forks source link

typing for headers are incompatible with web workers #4

Closed joonhocho closed 5 years ago

13rac1 commented 5 years ago

Can you give an example or explain further? These NPM packages are designed for use with Cloudflare Workers which are a subset of the Web Worker spec, but not for use with actual Web Workers.

joonhocho commented 5 years ago

@13rac1 yeah.

The following is what I am using for actual Cloudflare worker request / response headers

export interface IHeaders {
  append(key: string, value: string): void;
  set(key: string, value: string): void;
  delete(key: string): void;
  get(key: string): string | undefined;
  has(key: string): boolean;
  keys(): IterableIterator<string>;
  entries(): IterableIterator<[string, string]>;
}

These are what's in typescript/lib/lib.webworker.d.ts

interface Headers {
    append(name: string, value: string): void;
    delete(name: string): void;
    get(name: string): string | null;
    has(name: string): boolean;
    set(name: string, value: string): void;
    forEach(callbackfn: (value: string, key: string, parent: Headers) => void, thisArg?: any): void;
}
joonhocho commented 5 years ago

I first tried to use headers.forEach and it threw error so I looked into it and they were not compatible.

13rac1 commented 5 years ago

Ah yes, I documented this problem about a month ago: https://github.com/microsoft/TSJS-lib-generator/issues/729

Cloudflare's documentation says they follow the Fetch API spec: https://workers.cloudflare.com/docs/reference/runtime/apis/fetch/

Headers The Headers class matches the documentation provided by MDN. If you expect Unicode values in your headers, URL or Base64 encode your header values before adding them to a Headers object.

Both forEach() and entries() are in the Fetch API spec which means the TypeScript source is out of date and/or incorrect: https://github.com/microsoft/TypeScript/blob/b0f050f4ee791606514114f82068563694ca8bde/lib/lib.webworker.d.ts