@whatwg-node/node-fetch 0.2.0-alpha-20230210095043-408a558 → 0.2.0-alpha-20230210105523-366e74c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Headers.d.ts CHANGED
@@ -1,7 +1,13 @@
1
1
  export type PonyfillHeadersInit = [string, string][] | Record<string, string | string[] | undefined> | Headers;
2
2
  export declare class PonyfillHeaders implements Headers {
3
+ private headersInit?;
3
4
  private map;
4
- constructor(headersInit?: PonyfillHeadersInit);
5
+ private mapIsBuilt;
6
+ private objectNormalizedKeysOfHeadersInit;
7
+ private objectOriginalKeysOfHeadersInit;
8
+ constructor(headersInit?: PonyfillHeadersInit | undefined);
9
+ private _get;
10
+ private getMap;
5
11
  append(name: string, value: string): void;
6
12
  get(name: string): string | null;
7
13
  has(name: string): boolean;
package/index.js CHANGED
@@ -840,19 +840,66 @@ function isHeadersLike(headers) {
840
840
  }
841
841
  class PonyfillHeaders {
842
842
  constructor(headersInit) {
843
+ this.headersInit = headersInit;
843
844
  this.map = new Map();
844
- if (headersInit != null) {
845
- if (Array.isArray(headersInit)) {
846
- this.map = new Map(headersInit);
845
+ this.mapIsBuilt = false;
846
+ this.objectNormalizedKeysOfHeadersInit = [];
847
+ this.objectOriginalKeysOfHeadersInit = [];
848
+ }
849
+ // perf: we don't need to build `this.map` for Requests, as we can access the headers directly
850
+ _get(key) {
851
+ // If the map is built, reuse it
852
+ if (this.mapIsBuilt) {
853
+ return this.map.get(key.toLowerCase()) || null;
854
+ }
855
+ // If the map is not built, try to get the value from the this.headersInit
856
+ if (this.headersInit == null) {
857
+ return null;
858
+ }
859
+ const normalized = key.toLowerCase();
860
+ if (Array.isArray(this.headersInit)) {
861
+ return this.headersInit.find(header => header[0] === normalized);
862
+ }
863
+ else if (isHeadersLike(this.headersInit)) {
864
+ return this.headersInit.get(normalized);
865
+ }
866
+ else {
867
+ const initValue = this.headersInit[key] || this.headersInit[normalized];
868
+ if (initValue != null) {
869
+ return initValue;
870
+ }
871
+ if (!this.objectNormalizedKeysOfHeadersInit.length) {
872
+ Object.keys(this.headersInit).forEach(k => {
873
+ this.objectOriginalKeysOfHeadersInit.push(k);
874
+ this.objectNormalizedKeysOfHeadersInit.push(k.toLowerCase());
875
+ });
876
+ }
877
+ const index = this.objectNormalizedKeysOfHeadersInit.indexOf(normalized);
878
+ if (index === -1) {
879
+ return null;
847
880
  }
848
- else if (isHeadersLike(headersInit)) {
849
- headersInit.forEach((value, key) => {
881
+ const originalKey = this.objectOriginalKeysOfHeadersInit[index];
882
+ return this.headersInit[originalKey];
883
+ }
884
+ }
885
+ // perf: Build the map of headers lazily, only when we need to access all headers or write to it.
886
+ // I could do a getter here, but I'm too lazy to type `getter`.
887
+ getMap() {
888
+ if (this.mapIsBuilt) {
889
+ return this.map;
890
+ }
891
+ if (this.headersInit != null) {
892
+ if (Array.isArray(this.headersInit)) {
893
+ this.map = new Map(this.headersInit);
894
+ }
895
+ else if (isHeadersLike(this.headersInit)) {
896
+ this.headersInit.forEach((value, key) => {
850
897
  this.map.set(key, value);
851
898
  });
852
899
  }
853
900
  else {
854
- for (const initKey in headersInit) {
855
- const initValue = headersInit[initKey];
901
+ for (const initKey in this.headersInit) {
902
+ const initValue = this.headersInit[initKey];
856
903
  if (initValue != null) {
857
904
  const normalizedValue = Array.isArray(initValue) ? initValue.join(', ') : initValue;
858
905
  const normalizedKey = initKey.toLowerCase();
@@ -861,39 +908,48 @@ class PonyfillHeaders {
861
908
  }
862
909
  }
863
910
  }
911
+ this.mapIsBuilt = true;
912
+ return this.map;
864
913
  }
865
914
  append(name, value) {
866
915
  const key = name.toLowerCase();
867
- const existingValue = this.map.get(key);
916
+ const existingValue = this.getMap().get(key);
868
917
  const finalValue = existingValue ? `${existingValue}, ${value}` : value;
869
- this.map.set(key, finalValue);
918
+ this.getMap().set(key, finalValue);
870
919
  }
871
920
  get(name) {
872
921
  const key = name.toLowerCase();
873
- return this.map.get(key) || null;
922
+ const value = this._get(key);
923
+ if (value == null) {
924
+ return null;
925
+ }
926
+ if (Array.isArray(value)) {
927
+ return value.join(', ');
928
+ }
929
+ return value;
874
930
  }
875
931
  has(name) {
876
932
  const key = name.toLowerCase();
877
- return this.map.has(key);
933
+ return !!this._get(key); // we might need to check if header exists and not just check if it's not nullable
878
934
  }
879
935
  set(name, value) {
880
936
  const key = name.toLowerCase();
881
- this.map.set(key, value);
937
+ this.getMap().set(key, value);
882
938
  }
883
939
  delete(name) {
884
940
  const key = name.toLowerCase();
885
- this.map.delete(key);
941
+ this.getMap().delete(key);
886
942
  }
887
943
  forEach(callback) {
888
- this.map.forEach((value, key) => {
944
+ this.getMap().forEach((value, key) => {
889
945
  callback(value, key, this);
890
946
  });
891
947
  }
892
948
  entries() {
893
- return this.map.entries();
949
+ return this.getMap().entries();
894
950
  }
895
951
  [Symbol.iterator]() {
896
- return this.map.entries();
952
+ return this.getMap().entries();
897
953
  }
898
954
  }
899
955
 
package/index.mjs CHANGED
@@ -834,19 +834,66 @@ function isHeadersLike(headers) {
834
834
  }
835
835
  class PonyfillHeaders {
836
836
  constructor(headersInit) {
837
+ this.headersInit = headersInit;
837
838
  this.map = new Map();
838
- if (headersInit != null) {
839
- if (Array.isArray(headersInit)) {
840
- this.map = new Map(headersInit);
839
+ this.mapIsBuilt = false;
840
+ this.objectNormalizedKeysOfHeadersInit = [];
841
+ this.objectOriginalKeysOfHeadersInit = [];
842
+ }
843
+ // perf: we don't need to build `this.map` for Requests, as we can access the headers directly
844
+ _get(key) {
845
+ // If the map is built, reuse it
846
+ if (this.mapIsBuilt) {
847
+ return this.map.get(key.toLowerCase()) || null;
848
+ }
849
+ // If the map is not built, try to get the value from the this.headersInit
850
+ if (this.headersInit == null) {
851
+ return null;
852
+ }
853
+ const normalized = key.toLowerCase();
854
+ if (Array.isArray(this.headersInit)) {
855
+ return this.headersInit.find(header => header[0] === normalized);
856
+ }
857
+ else if (isHeadersLike(this.headersInit)) {
858
+ return this.headersInit.get(normalized);
859
+ }
860
+ else {
861
+ const initValue = this.headersInit[key] || this.headersInit[normalized];
862
+ if (initValue != null) {
863
+ return initValue;
864
+ }
865
+ if (!this.objectNormalizedKeysOfHeadersInit.length) {
866
+ Object.keys(this.headersInit).forEach(k => {
867
+ this.objectOriginalKeysOfHeadersInit.push(k);
868
+ this.objectNormalizedKeysOfHeadersInit.push(k.toLowerCase());
869
+ });
870
+ }
871
+ const index = this.objectNormalizedKeysOfHeadersInit.indexOf(normalized);
872
+ if (index === -1) {
873
+ return null;
841
874
  }
842
- else if (isHeadersLike(headersInit)) {
843
- headersInit.forEach((value, key) => {
875
+ const originalKey = this.objectOriginalKeysOfHeadersInit[index];
876
+ return this.headersInit[originalKey];
877
+ }
878
+ }
879
+ // perf: Build the map of headers lazily, only when we need to access all headers or write to it.
880
+ // I could do a getter here, but I'm too lazy to type `getter`.
881
+ getMap() {
882
+ if (this.mapIsBuilt) {
883
+ return this.map;
884
+ }
885
+ if (this.headersInit != null) {
886
+ if (Array.isArray(this.headersInit)) {
887
+ this.map = new Map(this.headersInit);
888
+ }
889
+ else if (isHeadersLike(this.headersInit)) {
890
+ this.headersInit.forEach((value, key) => {
844
891
  this.map.set(key, value);
845
892
  });
846
893
  }
847
894
  else {
848
- for (const initKey in headersInit) {
849
- const initValue = headersInit[initKey];
895
+ for (const initKey in this.headersInit) {
896
+ const initValue = this.headersInit[initKey];
850
897
  if (initValue != null) {
851
898
  const normalizedValue = Array.isArray(initValue) ? initValue.join(', ') : initValue;
852
899
  const normalizedKey = initKey.toLowerCase();
@@ -855,39 +902,48 @@ class PonyfillHeaders {
855
902
  }
856
903
  }
857
904
  }
905
+ this.mapIsBuilt = true;
906
+ return this.map;
858
907
  }
859
908
  append(name, value) {
860
909
  const key = name.toLowerCase();
861
- const existingValue = this.map.get(key);
910
+ const existingValue = this.getMap().get(key);
862
911
  const finalValue = existingValue ? `${existingValue}, ${value}` : value;
863
- this.map.set(key, finalValue);
912
+ this.getMap().set(key, finalValue);
864
913
  }
865
914
  get(name) {
866
915
  const key = name.toLowerCase();
867
- return this.map.get(key) || null;
916
+ const value = this._get(key);
917
+ if (value == null) {
918
+ return null;
919
+ }
920
+ if (Array.isArray(value)) {
921
+ return value.join(', ');
922
+ }
923
+ return value;
868
924
  }
869
925
  has(name) {
870
926
  const key = name.toLowerCase();
871
- return this.map.has(key);
927
+ return !!this._get(key); // we might need to check if header exists and not just check if it's not nullable
872
928
  }
873
929
  set(name, value) {
874
930
  const key = name.toLowerCase();
875
- this.map.set(key, value);
931
+ this.getMap().set(key, value);
876
932
  }
877
933
  delete(name) {
878
934
  const key = name.toLowerCase();
879
- this.map.delete(key);
935
+ this.getMap().delete(key);
880
936
  }
881
937
  forEach(callback) {
882
- this.map.forEach((value, key) => {
938
+ this.getMap().forEach((value, key) => {
883
939
  callback(value, key, this);
884
940
  });
885
941
  }
886
942
  entries() {
887
- return this.map.entries();
943
+ return this.getMap().entries();
888
944
  }
889
945
  [Symbol.iterator]() {
890
- return this.map.entries();
946
+ return this.getMap().entries();
891
947
  }
892
948
  }
893
949
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.2.0-alpha-20230210095043-408a558",
3
+ "version": "0.2.0-alpha-20230210105523-366e74c",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {