@whatwg-node/node-fetch 0.1.0 → 0.1.1-alpha-20230210102023-48114dc
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 +7 -1
- package/index.js +72 -16
- package/index.mjs +72 -16
- package/package.json +1 -1
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
|
-
|
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
@@ -838,19 +838,66 @@ function isHeadersLike(headers) {
|
|
838
838
|
}
|
839
839
|
class PonyfillHeaders {
|
840
840
|
constructor(headersInit) {
|
841
|
+
this.headersInit = headersInit;
|
841
842
|
this.map = new Map();
|
842
|
-
|
843
|
-
|
844
|
-
|
843
|
+
this.mapIsBuilt = false;
|
844
|
+
this.objectNormalizedKeysOfHeadersInit = [];
|
845
|
+
this.objectOriginalKeysOfHeadersInit = [];
|
846
|
+
}
|
847
|
+
// perf: we don't need to build `this.map` for Requests, as we can access the headers directly
|
848
|
+
_get(key) {
|
849
|
+
// If the map is built, reuse it
|
850
|
+
if (this.mapIsBuilt) {
|
851
|
+
return this.map.get(key.toLowerCase()) || null;
|
852
|
+
}
|
853
|
+
// If the map is not built, try to get the value from the this.headersInit
|
854
|
+
if (this.headersInit == null) {
|
855
|
+
return null;
|
856
|
+
}
|
857
|
+
const normalized = key.toLowerCase();
|
858
|
+
if (Array.isArray(this.headersInit)) {
|
859
|
+
return this.headersInit.find(header => header[0] === normalized);
|
860
|
+
}
|
861
|
+
else if (isHeadersLike(this.headersInit)) {
|
862
|
+
return this.headersInit.get(normalized);
|
863
|
+
}
|
864
|
+
else {
|
865
|
+
const initValue = this.headersInit[key] || this.headersInit[normalized];
|
866
|
+
if (initValue != null) {
|
867
|
+
return initValue;
|
868
|
+
}
|
869
|
+
if (!this.objectNormalizedKeysOfHeadersInit.length) {
|
870
|
+
Object.keys(this.headersInit).forEach(k => {
|
871
|
+
this.objectOriginalKeysOfHeadersInit.push(k);
|
872
|
+
this.objectNormalizedKeysOfHeadersInit.push(k.toLowerCase());
|
873
|
+
});
|
845
874
|
}
|
846
|
-
|
847
|
-
|
875
|
+
const index = this.objectNormalizedKeysOfHeadersInit.indexOf(normalized);
|
876
|
+
if (index === -1) {
|
877
|
+
return null;
|
878
|
+
}
|
879
|
+
const originalKey = this.objectOriginalKeysOfHeadersInit[index];
|
880
|
+
return this.headersInit[originalKey];
|
881
|
+
}
|
882
|
+
}
|
883
|
+
// perf: Build the map of headers lazily, only when we need to access all headers or write to it.
|
884
|
+
// I could do a getter here, but I'm too lazy to type `getter`.
|
885
|
+
getMap() {
|
886
|
+
if (this.mapIsBuilt) {
|
887
|
+
return this.map;
|
888
|
+
}
|
889
|
+
if (this.headersInit != null) {
|
890
|
+
if (Array.isArray(this.headersInit)) {
|
891
|
+
this.map = new Map(this.headersInit);
|
892
|
+
}
|
893
|
+
else if (isHeadersLike(this.headersInit)) {
|
894
|
+
this.headersInit.forEach((value, key) => {
|
848
895
|
this.map.set(key, value);
|
849
896
|
});
|
850
897
|
}
|
851
898
|
else {
|
852
|
-
for (const initKey in headersInit) {
|
853
|
-
const initValue = headersInit[initKey];
|
899
|
+
for (const initKey in this.headersInit) {
|
900
|
+
const initValue = this.headersInit[initKey];
|
854
901
|
if (initValue != null) {
|
855
902
|
const normalizedValue = Array.isArray(initValue) ? initValue.join(', ') : initValue;
|
856
903
|
const normalizedKey = initKey.toLowerCase();
|
@@ -859,39 +906,48 @@ class PonyfillHeaders {
|
|
859
906
|
}
|
860
907
|
}
|
861
908
|
}
|
909
|
+
this.mapIsBuilt = true;
|
910
|
+
return this.map;
|
862
911
|
}
|
863
912
|
append(name, value) {
|
864
913
|
const key = name.toLowerCase();
|
865
|
-
const existingValue = this.
|
914
|
+
const existingValue = this.getMap().get(key);
|
866
915
|
const finalValue = existingValue ? `${existingValue}, ${value}` : value;
|
867
|
-
this.
|
916
|
+
this.getMap().set(key, finalValue);
|
868
917
|
}
|
869
918
|
get(name) {
|
870
919
|
const key = name.toLowerCase();
|
871
|
-
|
920
|
+
const value = this._get(key);
|
921
|
+
if (value == null) {
|
922
|
+
return null;
|
923
|
+
}
|
924
|
+
if (Array.isArray(value)) {
|
925
|
+
return value.join(', ');
|
926
|
+
}
|
927
|
+
return value;
|
872
928
|
}
|
873
929
|
has(name) {
|
874
930
|
const key = name.toLowerCase();
|
875
|
-
return this.
|
931
|
+
return !!this._get(key); // we might need to check if header exists and not just check if it's not nullable
|
876
932
|
}
|
877
933
|
set(name, value) {
|
878
934
|
const key = name.toLowerCase();
|
879
|
-
this.
|
935
|
+
this.getMap().set(key, value);
|
880
936
|
}
|
881
937
|
delete(name) {
|
882
938
|
const key = name.toLowerCase();
|
883
|
-
this.
|
939
|
+
this.getMap().delete(key);
|
884
940
|
}
|
885
941
|
forEach(callback) {
|
886
|
-
this.
|
942
|
+
this.getMap().forEach((value, key) => {
|
887
943
|
callback(value, key, this);
|
888
944
|
});
|
889
945
|
}
|
890
946
|
entries() {
|
891
|
-
return this.
|
947
|
+
return this.getMap().entries();
|
892
948
|
}
|
893
949
|
[Symbol.iterator]() {
|
894
|
-
return this.
|
950
|
+
return this.getMap().entries();
|
895
951
|
}
|
896
952
|
}
|
897
953
|
|
package/index.mjs
CHANGED
@@ -832,19 +832,66 @@ function isHeadersLike(headers) {
|
|
832
832
|
}
|
833
833
|
class PonyfillHeaders {
|
834
834
|
constructor(headersInit) {
|
835
|
+
this.headersInit = headersInit;
|
835
836
|
this.map = new Map();
|
836
|
-
|
837
|
-
|
838
|
-
|
837
|
+
this.mapIsBuilt = false;
|
838
|
+
this.objectNormalizedKeysOfHeadersInit = [];
|
839
|
+
this.objectOriginalKeysOfHeadersInit = [];
|
840
|
+
}
|
841
|
+
// perf: we don't need to build `this.map` for Requests, as we can access the headers directly
|
842
|
+
_get(key) {
|
843
|
+
// If the map is built, reuse it
|
844
|
+
if (this.mapIsBuilt) {
|
845
|
+
return this.map.get(key.toLowerCase()) || null;
|
846
|
+
}
|
847
|
+
// If the map is not built, try to get the value from the this.headersInit
|
848
|
+
if (this.headersInit == null) {
|
849
|
+
return null;
|
850
|
+
}
|
851
|
+
const normalized = key.toLowerCase();
|
852
|
+
if (Array.isArray(this.headersInit)) {
|
853
|
+
return this.headersInit.find(header => header[0] === normalized);
|
854
|
+
}
|
855
|
+
else if (isHeadersLike(this.headersInit)) {
|
856
|
+
return this.headersInit.get(normalized);
|
857
|
+
}
|
858
|
+
else {
|
859
|
+
const initValue = this.headersInit[key] || this.headersInit[normalized];
|
860
|
+
if (initValue != null) {
|
861
|
+
return initValue;
|
862
|
+
}
|
863
|
+
if (!this.objectNormalizedKeysOfHeadersInit.length) {
|
864
|
+
Object.keys(this.headersInit).forEach(k => {
|
865
|
+
this.objectOriginalKeysOfHeadersInit.push(k);
|
866
|
+
this.objectNormalizedKeysOfHeadersInit.push(k.toLowerCase());
|
867
|
+
});
|
839
868
|
}
|
840
|
-
|
841
|
-
|
869
|
+
const index = this.objectNormalizedKeysOfHeadersInit.indexOf(normalized);
|
870
|
+
if (index === -1) {
|
871
|
+
return null;
|
872
|
+
}
|
873
|
+
const originalKey = this.objectOriginalKeysOfHeadersInit[index];
|
874
|
+
return this.headersInit[originalKey];
|
875
|
+
}
|
876
|
+
}
|
877
|
+
// perf: Build the map of headers lazily, only when we need to access all headers or write to it.
|
878
|
+
// I could do a getter here, but I'm too lazy to type `getter`.
|
879
|
+
getMap() {
|
880
|
+
if (this.mapIsBuilt) {
|
881
|
+
return this.map;
|
882
|
+
}
|
883
|
+
if (this.headersInit != null) {
|
884
|
+
if (Array.isArray(this.headersInit)) {
|
885
|
+
this.map = new Map(this.headersInit);
|
886
|
+
}
|
887
|
+
else if (isHeadersLike(this.headersInit)) {
|
888
|
+
this.headersInit.forEach((value, key) => {
|
842
889
|
this.map.set(key, value);
|
843
890
|
});
|
844
891
|
}
|
845
892
|
else {
|
846
|
-
for (const initKey in headersInit) {
|
847
|
-
const initValue = headersInit[initKey];
|
893
|
+
for (const initKey in this.headersInit) {
|
894
|
+
const initValue = this.headersInit[initKey];
|
848
895
|
if (initValue != null) {
|
849
896
|
const normalizedValue = Array.isArray(initValue) ? initValue.join(', ') : initValue;
|
850
897
|
const normalizedKey = initKey.toLowerCase();
|
@@ -853,39 +900,48 @@ class PonyfillHeaders {
|
|
853
900
|
}
|
854
901
|
}
|
855
902
|
}
|
903
|
+
this.mapIsBuilt = true;
|
904
|
+
return this.map;
|
856
905
|
}
|
857
906
|
append(name, value) {
|
858
907
|
const key = name.toLowerCase();
|
859
|
-
const existingValue = this.
|
908
|
+
const existingValue = this.getMap().get(key);
|
860
909
|
const finalValue = existingValue ? `${existingValue}, ${value}` : value;
|
861
|
-
this.
|
910
|
+
this.getMap().set(key, finalValue);
|
862
911
|
}
|
863
912
|
get(name) {
|
864
913
|
const key = name.toLowerCase();
|
865
|
-
|
914
|
+
const value = this._get(key);
|
915
|
+
if (value == null) {
|
916
|
+
return null;
|
917
|
+
}
|
918
|
+
if (Array.isArray(value)) {
|
919
|
+
return value.join(', ');
|
920
|
+
}
|
921
|
+
return value;
|
866
922
|
}
|
867
923
|
has(name) {
|
868
924
|
const key = name.toLowerCase();
|
869
|
-
return this.
|
925
|
+
return !!this._get(key); // we might need to check if header exists and not just check if it's not nullable
|
870
926
|
}
|
871
927
|
set(name, value) {
|
872
928
|
const key = name.toLowerCase();
|
873
|
-
this.
|
929
|
+
this.getMap().set(key, value);
|
874
930
|
}
|
875
931
|
delete(name) {
|
876
932
|
const key = name.toLowerCase();
|
877
|
-
this.
|
933
|
+
this.getMap().delete(key);
|
878
934
|
}
|
879
935
|
forEach(callback) {
|
880
|
-
this.
|
936
|
+
this.getMap().forEach((value, key) => {
|
881
937
|
callback(value, key, this);
|
882
938
|
});
|
883
939
|
}
|
884
940
|
entries() {
|
885
|
-
return this.
|
941
|
+
return this.getMap().entries();
|
886
942
|
}
|
887
943
|
[Symbol.iterator]() {
|
888
|
-
return this.
|
944
|
+
return this.getMap().entries();
|
889
945
|
}
|
890
946
|
}
|
891
947
|
|