@whatwg-node/node-fetch 0.7.9 → 0.7.10-alpha-20250220113249-0583b3e53c27f1e5d23782766f8ae80637457e0b

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/cjs/Headers.js CHANGED
@@ -12,14 +12,14 @@ class PonyfillHeaders {
12
12
  _map;
13
13
  objectNormalizedKeysOfHeadersInit = [];
14
14
  objectOriginalKeysOfHeadersInit = [];
15
- _setCookies = [];
15
+ _setCookies;
16
16
  constructor(headersInit) {
17
17
  this.headersInit = headersInit;
18
18
  }
19
19
  // perf: we don't need to build `this.map` for Requests, as we can access the headers directly
20
20
  _get(key) {
21
21
  const normalized = key.toLowerCase();
22
- if (normalized === 'set-cookie') {
22
+ if (normalized === 'set-cookie' && this._setCookies?.length) {
23
23
  return this._setCookies.join(', ');
24
24
  }
25
25
  // If the map is built, reuse it
@@ -31,7 +31,14 @@ class PonyfillHeaders {
31
31
  return null;
32
32
  }
33
33
  if (Array.isArray(this.headersInit)) {
34
- return this.headersInit.find(header => header[0].toLowerCase() === normalized)?.[1] || null;
34
+ const found = this.headersInit.filter(([headerKey]) => headerKey.toLowerCase() === normalized);
35
+ if (found.length === 0) {
36
+ return null;
37
+ }
38
+ if (found.length === 1) {
39
+ return found[0][1];
40
+ }
41
+ return found.map(([, value]) => value).join(', ');
35
42
  }
36
43
  else if (isHeadersLike(this.headersInit)) {
37
44
  return this.headersInit.get(normalized);
@@ -59,22 +66,24 @@ class PonyfillHeaders {
59
66
  // I could do a getter here, but I'm too lazy to type `getter`.
60
67
  getMap() {
61
68
  if (!this._map) {
69
+ this._setCookies = [];
62
70
  if (this.headersInit != null) {
63
71
  if (Array.isArray(this.headersInit)) {
64
72
  this._map = new Map();
65
- this.headersInit.forEach(([key, value]) => {
73
+ for (const [key, value] of this.headersInit) {
66
74
  const normalizedKey = key.toLowerCase();
67
75
  if (normalizedKey === 'set-cookie') {
68
76
  this._setCookies.push(value);
69
- return;
77
+ continue;
70
78
  }
71
79
  this._map.set(normalizedKey, value);
72
- });
80
+ }
73
81
  }
74
82
  else if (isHeadersLike(this.headersInit)) {
75
83
  this._map = new Map();
76
84
  this.headersInit.forEach((value, key) => {
77
85
  if (key === 'set-cookie') {
86
+ this._setCookies ||= [];
78
87
  this._setCookies.push(value);
79
88
  return;
80
89
  }
@@ -88,6 +97,7 @@ class PonyfillHeaders {
88
97
  if (initValue != null) {
89
98
  const normalizedKey = initKey.toLowerCase();
90
99
  if (normalizedKey === 'set-cookie') {
100
+ this._setCookies ||= [];
91
101
  this._setCookies.push(initValue);
92
102
  continue;
93
103
  }
@@ -105,6 +115,7 @@ class PonyfillHeaders {
105
115
  append(name, value) {
106
116
  const key = name.toLowerCase();
107
117
  if (key === 'set-cookie') {
118
+ this._setCookies ||= [];
108
119
  this._setCookies.push(value);
109
120
  return;
110
121
  }
@@ -117,11 +128,11 @@ class PonyfillHeaders {
117
128
  if (value == null) {
118
129
  return null;
119
130
  }
120
- return value;
131
+ return value.toString();
121
132
  }
122
133
  has(name) {
123
134
  if (name === 'set-cookie') {
124
- return this._setCookies.length > 0;
135
+ return !!this._setCookies?.length;
125
136
  }
126
137
  return !!this._get(name); // we might need to check if header exists and not just check if it's not nullable
127
138
  }
@@ -142,7 +153,7 @@ class PonyfillHeaders {
142
153
  this.getMap().delete(key);
143
154
  }
144
155
  forEach(callback) {
145
- this._setCookies.forEach(setCookie => {
156
+ this._setCookies?.forEach(setCookie => {
146
157
  callback(setCookie, 'set-cookie', this);
147
158
  });
148
159
  if (!this._map) {
@@ -170,7 +181,7 @@ class PonyfillHeaders {
170
181
  });
171
182
  }
172
183
  *_keys() {
173
- if (this._setCookies.length) {
184
+ if (this._setCookies?.length) {
174
185
  yield 'set-cookie';
175
186
  }
176
187
  if (!this._map) {
@@ -193,7 +204,9 @@ class PonyfillHeaders {
193
204
  return new IteratorObject_js_1.PonyfillIteratorObject(this._keys(), 'HeadersIterator');
194
205
  }
195
206
  *_values() {
196
- yield* this._setCookies;
207
+ if (this._setCookies?.length) {
208
+ yield* this._setCookies;
209
+ }
197
210
  if (!this._map) {
198
211
  if (this.headersInit) {
199
212
  if (Array.isArray(this.headersInit)) {
@@ -214,7 +227,9 @@ class PonyfillHeaders {
214
227
  return new IteratorObject_js_1.PonyfillIteratorObject(this._values(), 'HeadersIterator');
215
228
  }
216
229
  *_entries() {
217
- yield* this._setCookies.map(cookie => ['set-cookie', cookie]);
230
+ if (this._setCookies?.length) {
231
+ yield* this._setCookies.map(cookie => ['set-cookie', cookie]);
232
+ }
218
233
  if (!this._map) {
219
234
  if (this.headersInit) {
220
235
  if (Array.isArray(this.headersInit)) {
@@ -235,6 +250,9 @@ class PonyfillHeaders {
235
250
  return new IteratorObject_js_1.PonyfillIteratorObject(this._entries(), 'HeadersIterator');
236
251
  }
237
252
  getSetCookie() {
253
+ if (!this._setCookies) {
254
+ this.getMap();
255
+ }
238
256
  return this._setCookies;
239
257
  }
240
258
  [Symbol.iterator]() {
@@ -244,7 +262,7 @@ class PonyfillHeaders {
244
262
  const record = {};
245
263
  this.forEach((value, key) => {
246
264
  if (key === 'set-cookie') {
247
- record['set-cookie'] = this._setCookies;
265
+ record['set-cookie'] = this._setCookies || [];
248
266
  }
249
267
  else {
250
268
  record[key] = value?.includes(',') ? value.split(',').map(el => el.trim()) : value;
package/esm/Headers.js CHANGED
@@ -8,14 +8,14 @@ export class PonyfillHeaders {
8
8
  _map;
9
9
  objectNormalizedKeysOfHeadersInit = [];
10
10
  objectOriginalKeysOfHeadersInit = [];
11
- _setCookies = [];
11
+ _setCookies;
12
12
  constructor(headersInit) {
13
13
  this.headersInit = headersInit;
14
14
  }
15
15
  // perf: we don't need to build `this.map` for Requests, as we can access the headers directly
16
16
  _get(key) {
17
17
  const normalized = key.toLowerCase();
18
- if (normalized === 'set-cookie') {
18
+ if (normalized === 'set-cookie' && this._setCookies?.length) {
19
19
  return this._setCookies.join(', ');
20
20
  }
21
21
  // If the map is built, reuse it
@@ -27,7 +27,14 @@ export class PonyfillHeaders {
27
27
  return null;
28
28
  }
29
29
  if (Array.isArray(this.headersInit)) {
30
- return this.headersInit.find(header => header[0].toLowerCase() === normalized)?.[1] || null;
30
+ const found = this.headersInit.filter(([headerKey]) => headerKey.toLowerCase() === normalized);
31
+ if (found.length === 0) {
32
+ return null;
33
+ }
34
+ if (found.length === 1) {
35
+ return found[0][1];
36
+ }
37
+ return found.map(([, value]) => value).join(', ');
31
38
  }
32
39
  else if (isHeadersLike(this.headersInit)) {
33
40
  return this.headersInit.get(normalized);
@@ -55,22 +62,24 @@ export class PonyfillHeaders {
55
62
  // I could do a getter here, but I'm too lazy to type `getter`.
56
63
  getMap() {
57
64
  if (!this._map) {
65
+ this._setCookies = [];
58
66
  if (this.headersInit != null) {
59
67
  if (Array.isArray(this.headersInit)) {
60
68
  this._map = new Map();
61
- this.headersInit.forEach(([key, value]) => {
69
+ for (const [key, value] of this.headersInit) {
62
70
  const normalizedKey = key.toLowerCase();
63
71
  if (normalizedKey === 'set-cookie') {
64
72
  this._setCookies.push(value);
65
- return;
73
+ continue;
66
74
  }
67
75
  this._map.set(normalizedKey, value);
68
- });
76
+ }
69
77
  }
70
78
  else if (isHeadersLike(this.headersInit)) {
71
79
  this._map = new Map();
72
80
  this.headersInit.forEach((value, key) => {
73
81
  if (key === 'set-cookie') {
82
+ this._setCookies ||= [];
74
83
  this._setCookies.push(value);
75
84
  return;
76
85
  }
@@ -84,6 +93,7 @@ export class PonyfillHeaders {
84
93
  if (initValue != null) {
85
94
  const normalizedKey = initKey.toLowerCase();
86
95
  if (normalizedKey === 'set-cookie') {
96
+ this._setCookies ||= [];
87
97
  this._setCookies.push(initValue);
88
98
  continue;
89
99
  }
@@ -101,6 +111,7 @@ export class PonyfillHeaders {
101
111
  append(name, value) {
102
112
  const key = name.toLowerCase();
103
113
  if (key === 'set-cookie') {
114
+ this._setCookies ||= [];
104
115
  this._setCookies.push(value);
105
116
  return;
106
117
  }
@@ -113,11 +124,11 @@ export class PonyfillHeaders {
113
124
  if (value == null) {
114
125
  return null;
115
126
  }
116
- return value;
127
+ return value.toString();
117
128
  }
118
129
  has(name) {
119
130
  if (name === 'set-cookie') {
120
- return this._setCookies.length > 0;
131
+ return !!this._setCookies?.length;
121
132
  }
122
133
  return !!this._get(name); // we might need to check if header exists and not just check if it's not nullable
123
134
  }
@@ -138,7 +149,7 @@ export class PonyfillHeaders {
138
149
  this.getMap().delete(key);
139
150
  }
140
151
  forEach(callback) {
141
- this._setCookies.forEach(setCookie => {
152
+ this._setCookies?.forEach(setCookie => {
142
153
  callback(setCookie, 'set-cookie', this);
143
154
  });
144
155
  if (!this._map) {
@@ -166,7 +177,7 @@ export class PonyfillHeaders {
166
177
  });
167
178
  }
168
179
  *_keys() {
169
- if (this._setCookies.length) {
180
+ if (this._setCookies?.length) {
170
181
  yield 'set-cookie';
171
182
  }
172
183
  if (!this._map) {
@@ -189,7 +200,9 @@ export class PonyfillHeaders {
189
200
  return new PonyfillIteratorObject(this._keys(), 'HeadersIterator');
190
201
  }
191
202
  *_values() {
192
- yield* this._setCookies;
203
+ if (this._setCookies?.length) {
204
+ yield* this._setCookies;
205
+ }
193
206
  if (!this._map) {
194
207
  if (this.headersInit) {
195
208
  if (Array.isArray(this.headersInit)) {
@@ -210,7 +223,9 @@ export class PonyfillHeaders {
210
223
  return new PonyfillIteratorObject(this._values(), 'HeadersIterator');
211
224
  }
212
225
  *_entries() {
213
- yield* this._setCookies.map(cookie => ['set-cookie', cookie]);
226
+ if (this._setCookies?.length) {
227
+ yield* this._setCookies.map(cookie => ['set-cookie', cookie]);
228
+ }
214
229
  if (!this._map) {
215
230
  if (this.headersInit) {
216
231
  if (Array.isArray(this.headersInit)) {
@@ -231,6 +246,9 @@ export class PonyfillHeaders {
231
246
  return new PonyfillIteratorObject(this._entries(), 'HeadersIterator');
232
247
  }
233
248
  getSetCookie() {
249
+ if (!this._setCookies) {
250
+ this.getMap();
251
+ }
234
252
  return this._setCookies;
235
253
  }
236
254
  [Symbol.iterator]() {
@@ -240,7 +258,7 @@ export class PonyfillHeaders {
240
258
  const record = {};
241
259
  this.forEach((value, key) => {
242
260
  if (key === 'set-cookie') {
243
- record['set-cookie'] = this._setCookies;
261
+ record['set-cookie'] = this._setCookies || [];
244
262
  }
245
263
  else {
246
264
  record[key] = value?.includes(',') ? value.split(',').map(el => el.trim()) : value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.7.9",
3
+ "version": "0.7.10-alpha-20250220113249-0583b3e53c27f1e5d23782766f8ae80637457e0b",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -5,7 +5,7 @@ export declare class PonyfillHeaders implements Headers {
5
5
  private _map;
6
6
  private objectNormalizedKeysOfHeadersInit;
7
7
  private objectOriginalKeysOfHeadersInit;
8
- private _setCookies;
8
+ private _setCookies?;
9
9
  constructor(headersInit?: PonyfillHeadersInit | undefined);
10
10
  private _get;
11
11
  private getMap;
@@ -5,7 +5,7 @@ export declare class PonyfillHeaders implements Headers {
5
5
  private _map;
6
6
  private objectNormalizedKeysOfHeadersInit;
7
7
  private objectOriginalKeysOfHeadersInit;
8
- private _setCookies;
8
+ private _setCookies?;
9
9
  constructor(headersInit?: PonyfillHeadersInit | undefined);
10
10
  private _get;
11
11
  private getMap;