@whatwg-node/node-fetch 0.5.0 → 0.5.1

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
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.splitSetCookieHeader = exports.PonyfillHeaders = exports.isHeadersLike = void 0;
3
+ exports.PonyfillHeaders = exports.isHeadersLike = void 0;
4
4
  const node_util_1 = require("node:util");
5
5
  function isHeadersLike(headers) {
6
6
  return headers?.get && headers?.forEach;
@@ -11,18 +11,22 @@ class PonyfillHeaders {
11
11
  this.headersInit = headersInit;
12
12
  this.objectNormalizedKeysOfHeadersInit = [];
13
13
  this.objectOriginalKeysOfHeadersInit = [];
14
+ this._setCookies = [];
14
15
  }
15
16
  // perf: we don't need to build `this.map` for Requests, as we can access the headers directly
16
17
  _get(key) {
18
+ const normalized = key.toLowerCase();
19
+ if (normalized === 'set-cookie') {
20
+ return this._setCookies.join(', ');
21
+ }
17
22
  // If the map is built, reuse it
18
23
  if (this._map) {
19
- return this._map.get(key.toLowerCase()) || null;
24
+ return this._map.get(normalized) || null;
20
25
  }
21
26
  // If the map is not built, try to get the value from the this.headersInit
22
27
  if (this.headersInit == null) {
23
28
  return null;
24
29
  }
25
- const normalized = key.toLowerCase();
26
30
  if (Array.isArray(this.headersInit)) {
27
31
  return this.headersInit.find(header => header[0].toLowerCase() === normalized)?.[1] || null;
28
32
  }
@@ -54,11 +58,23 @@ class PonyfillHeaders {
54
58
  if (!this._map) {
55
59
  if (this.headersInit != null) {
56
60
  if (Array.isArray(this.headersInit)) {
57
- this._map = new Map(this.headersInit);
61
+ this._map = new Map();
62
+ this.headersInit.forEach(([key, value]) => {
63
+ const normalizedKey = key.toLowerCase();
64
+ if (normalizedKey === 'set-cookie') {
65
+ this._setCookies.push(value);
66
+ return;
67
+ }
68
+ this._map.set(normalizedKey, value);
69
+ });
58
70
  }
59
71
  else if (isHeadersLike(this.headersInit)) {
60
72
  this._map = new Map();
61
73
  this.headersInit.forEach((value, key) => {
74
+ if (key === 'set-cookie') {
75
+ this._setCookies.push(value);
76
+ return;
77
+ }
62
78
  this._map.set(key, value);
63
79
  });
64
80
  }
@@ -68,6 +84,10 @@ class PonyfillHeaders {
68
84
  const initValue = this.headersInit[initKey];
69
85
  if (initValue != null) {
70
86
  const normalizedKey = initKey.toLowerCase();
87
+ if (normalizedKey === 'set-cookie') {
88
+ this._setCookies.push(initValue);
89
+ continue;
90
+ }
71
91
  this._map.set(normalizedKey, initValue);
72
92
  }
73
93
  }
@@ -81,6 +101,10 @@ class PonyfillHeaders {
81
101
  }
82
102
  append(name, value) {
83
103
  const key = name.toLowerCase();
104
+ if (key === 'set-cookie') {
105
+ this._setCookies.push(value);
106
+ return;
107
+ }
84
108
  const existingValue = this.getMap().get(key);
85
109
  const finalValue = existingValue ? `${existingValue}, ${value}` : value;
86
110
  this.getMap().set(key, finalValue);
@@ -93,17 +117,31 @@ class PonyfillHeaders {
93
117
  return value;
94
118
  }
95
119
  has(name) {
120
+ if (name === 'set-cookie') {
121
+ return this._setCookies.length > 0;
122
+ }
96
123
  return !!this._get(name); // we might need to check if header exists and not just check if it's not nullable
97
124
  }
98
125
  set(name, value) {
99
126
  const key = name.toLowerCase();
127
+ if (key === 'set-cookie') {
128
+ this._setCookies = [value];
129
+ return;
130
+ }
100
131
  this.getMap().set(key, value);
101
132
  }
102
133
  delete(name) {
103
134
  const key = name.toLowerCase();
135
+ if (key === 'set-cookie') {
136
+ this._setCookies = [];
137
+ return;
138
+ }
104
139
  this.getMap().delete(key);
105
140
  }
106
141
  forEach(callback) {
142
+ this._setCookies.forEach(setCookie => {
143
+ callback(setCookie, 'set-cookie', this);
144
+ });
107
145
  if (!this._map) {
108
146
  if (this.headersInit) {
109
147
  if (Array.isArray(this.headersInit)) {
@@ -128,54 +166,64 @@ class PonyfillHeaders {
128
166
  callback(value, key, this);
129
167
  });
130
168
  }
131
- keys() {
169
+ *keys() {
170
+ if (this._setCookies.length) {
171
+ yield 'set-cookie';
172
+ }
132
173
  if (!this._map) {
133
174
  if (this.headersInit) {
134
175
  if (Array.isArray(this.headersInit)) {
135
- return this.headersInit.map(([key]) => key)[Symbol.iterator]();
176
+ yield* this.headersInit.map(([key]) => key)[Symbol.iterator]();
177
+ return;
136
178
  }
137
179
  if (isHeadersLike(this.headersInit)) {
138
- return this.headersInit.keys();
180
+ yield* this.headersInit.keys();
181
+ return;
139
182
  }
140
- return Object.keys(this.headersInit)[Symbol.iterator]();
183
+ yield* Object.keys(this.headersInit)[Symbol.iterator]();
184
+ return;
141
185
  }
142
186
  }
143
- return this.getMap().keys();
187
+ yield* this.getMap().keys();
144
188
  }
145
- values() {
189
+ *values() {
190
+ yield* this._setCookies;
146
191
  if (!this._map) {
147
192
  if (this.headersInit) {
148
193
  if (Array.isArray(this.headersInit)) {
149
- return this.headersInit.map(([, value]) => value)[Symbol.iterator]();
194
+ yield* this.headersInit.map(([, value]) => value)[Symbol.iterator]();
195
+ return;
150
196
  }
151
197
  if (isHeadersLike(this.headersInit)) {
152
- return this.headersInit.values();
198
+ yield* this.headersInit.values();
199
+ return;
153
200
  }
154
- return Object.values(this.headersInit)[Symbol.iterator]();
201
+ yield* Object.values(this.headersInit)[Symbol.iterator]();
202
+ return;
155
203
  }
156
204
  }
157
- return this.getMap().values();
205
+ yield* this.getMap().values();
158
206
  }
159
- entries() {
207
+ *entries() {
208
+ yield* this._setCookies.map(cookie => ['set-cookie', cookie]);
160
209
  if (!this._map) {
161
210
  if (this.headersInit) {
162
211
  if (Array.isArray(this.headersInit)) {
163
- return this.headersInit[Symbol.iterator]();
212
+ yield* this.headersInit;
213
+ return;
164
214
  }
165
215
  if (isHeadersLike(this.headersInit)) {
166
- return this.headersInit.entries();
216
+ yield* this.headersInit.entries();
217
+ return;
167
218
  }
168
- return Object.entries(this.headersInit)[Symbol.iterator]();
219
+ yield* Object.entries(this.headersInit);
220
+ return;
169
221
  }
170
222
  }
171
- return this.getMap().entries();
223
+ yield* this.getMap().entries();
172
224
  }
173
225
  getSetCookie() {
174
- const setCookieHeader = this.get('set-cookie');
175
- if (!setCookieHeader) {
176
- return [];
177
- }
178
- return splitSetCookieHeader(setCookieHeader);
226
+ return this._setCookies;
179
227
  }
180
228
  [Symbol.iterator]() {
181
229
  return this.entries();
@@ -184,7 +232,7 @@ class PonyfillHeaders {
184
232
  const record = {};
185
233
  this.forEach((value, key) => {
186
234
  if (key === 'set-cookie') {
187
- record['set-cookie'] = this.getSetCookie();
235
+ record['set-cookie'] = this._setCookies;
188
236
  }
189
237
  else {
190
238
  record[key] = value.includes(',') ? value.split(',').map(el => el.trim()) : value;
@@ -194,33 +242,3 @@ class PonyfillHeaders {
194
242
  }
195
243
  }
196
244
  exports.PonyfillHeaders = PonyfillHeaders;
197
- function splitSetCookieHeader(setCookieHeader) {
198
- const setCookieHeaders = [];
199
- let currentStr = '';
200
- let ignoreComma = false;
201
- for (const ch of setCookieHeader) {
202
- if (currentStr.endsWith('Expires=')) {
203
- ignoreComma = true;
204
- }
205
- if (ignoreComma) {
206
- if (ch === ';') {
207
- ignoreComma = false;
208
- }
209
- if (ch === ',' && currentStr.split('Expires=')[1].length > 3) {
210
- ignoreComma = false;
211
- }
212
- }
213
- if (ch === ',' && !ignoreComma) {
214
- setCookieHeaders.push(currentStr.trim());
215
- currentStr = '';
216
- }
217
- else {
218
- currentStr += ch;
219
- }
220
- }
221
- if (currentStr) {
222
- setCookieHeaders.push(currentStr.trim());
223
- }
224
- return setCookieHeaders;
225
- }
226
- exports.splitSetCookieHeader = splitSetCookieHeader;
package/esm/Headers.js CHANGED
@@ -7,18 +7,22 @@ export class PonyfillHeaders {
7
7
  this.headersInit = headersInit;
8
8
  this.objectNormalizedKeysOfHeadersInit = [];
9
9
  this.objectOriginalKeysOfHeadersInit = [];
10
+ this._setCookies = [];
10
11
  }
11
12
  // perf: we don't need to build `this.map` for Requests, as we can access the headers directly
12
13
  _get(key) {
14
+ const normalized = key.toLowerCase();
15
+ if (normalized === 'set-cookie') {
16
+ return this._setCookies.join(', ');
17
+ }
13
18
  // If the map is built, reuse it
14
19
  if (this._map) {
15
- return this._map.get(key.toLowerCase()) || null;
20
+ return this._map.get(normalized) || null;
16
21
  }
17
22
  // If the map is not built, try to get the value from the this.headersInit
18
23
  if (this.headersInit == null) {
19
24
  return null;
20
25
  }
21
- const normalized = key.toLowerCase();
22
26
  if (Array.isArray(this.headersInit)) {
23
27
  return this.headersInit.find(header => header[0].toLowerCase() === normalized)?.[1] || null;
24
28
  }
@@ -50,11 +54,23 @@ export class PonyfillHeaders {
50
54
  if (!this._map) {
51
55
  if (this.headersInit != null) {
52
56
  if (Array.isArray(this.headersInit)) {
53
- this._map = new Map(this.headersInit);
57
+ this._map = new Map();
58
+ this.headersInit.forEach(([key, value]) => {
59
+ const normalizedKey = key.toLowerCase();
60
+ if (normalizedKey === 'set-cookie') {
61
+ this._setCookies.push(value);
62
+ return;
63
+ }
64
+ this._map.set(normalizedKey, value);
65
+ });
54
66
  }
55
67
  else if (isHeadersLike(this.headersInit)) {
56
68
  this._map = new Map();
57
69
  this.headersInit.forEach((value, key) => {
70
+ if (key === 'set-cookie') {
71
+ this._setCookies.push(value);
72
+ return;
73
+ }
58
74
  this._map.set(key, value);
59
75
  });
60
76
  }
@@ -64,6 +80,10 @@ export class PonyfillHeaders {
64
80
  const initValue = this.headersInit[initKey];
65
81
  if (initValue != null) {
66
82
  const normalizedKey = initKey.toLowerCase();
83
+ if (normalizedKey === 'set-cookie') {
84
+ this._setCookies.push(initValue);
85
+ continue;
86
+ }
67
87
  this._map.set(normalizedKey, initValue);
68
88
  }
69
89
  }
@@ -77,6 +97,10 @@ export class PonyfillHeaders {
77
97
  }
78
98
  append(name, value) {
79
99
  const key = name.toLowerCase();
100
+ if (key === 'set-cookie') {
101
+ this._setCookies.push(value);
102
+ return;
103
+ }
80
104
  const existingValue = this.getMap().get(key);
81
105
  const finalValue = existingValue ? `${existingValue}, ${value}` : value;
82
106
  this.getMap().set(key, finalValue);
@@ -89,17 +113,31 @@ export class PonyfillHeaders {
89
113
  return value;
90
114
  }
91
115
  has(name) {
116
+ if (name === 'set-cookie') {
117
+ return this._setCookies.length > 0;
118
+ }
92
119
  return !!this._get(name); // we might need to check if header exists and not just check if it's not nullable
93
120
  }
94
121
  set(name, value) {
95
122
  const key = name.toLowerCase();
123
+ if (key === 'set-cookie') {
124
+ this._setCookies = [value];
125
+ return;
126
+ }
96
127
  this.getMap().set(key, value);
97
128
  }
98
129
  delete(name) {
99
130
  const key = name.toLowerCase();
131
+ if (key === 'set-cookie') {
132
+ this._setCookies = [];
133
+ return;
134
+ }
100
135
  this.getMap().delete(key);
101
136
  }
102
137
  forEach(callback) {
138
+ this._setCookies.forEach(setCookie => {
139
+ callback(setCookie, 'set-cookie', this);
140
+ });
103
141
  if (!this._map) {
104
142
  if (this.headersInit) {
105
143
  if (Array.isArray(this.headersInit)) {
@@ -124,54 +162,64 @@ export class PonyfillHeaders {
124
162
  callback(value, key, this);
125
163
  });
126
164
  }
127
- keys() {
165
+ *keys() {
166
+ if (this._setCookies.length) {
167
+ yield 'set-cookie';
168
+ }
128
169
  if (!this._map) {
129
170
  if (this.headersInit) {
130
171
  if (Array.isArray(this.headersInit)) {
131
- return this.headersInit.map(([key]) => key)[Symbol.iterator]();
172
+ yield* this.headersInit.map(([key]) => key)[Symbol.iterator]();
173
+ return;
132
174
  }
133
175
  if (isHeadersLike(this.headersInit)) {
134
- return this.headersInit.keys();
176
+ yield* this.headersInit.keys();
177
+ return;
135
178
  }
136
- return Object.keys(this.headersInit)[Symbol.iterator]();
179
+ yield* Object.keys(this.headersInit)[Symbol.iterator]();
180
+ return;
137
181
  }
138
182
  }
139
- return this.getMap().keys();
183
+ yield* this.getMap().keys();
140
184
  }
141
- values() {
185
+ *values() {
186
+ yield* this._setCookies;
142
187
  if (!this._map) {
143
188
  if (this.headersInit) {
144
189
  if (Array.isArray(this.headersInit)) {
145
- return this.headersInit.map(([, value]) => value)[Symbol.iterator]();
190
+ yield* this.headersInit.map(([, value]) => value)[Symbol.iterator]();
191
+ return;
146
192
  }
147
193
  if (isHeadersLike(this.headersInit)) {
148
- return this.headersInit.values();
194
+ yield* this.headersInit.values();
195
+ return;
149
196
  }
150
- return Object.values(this.headersInit)[Symbol.iterator]();
197
+ yield* Object.values(this.headersInit)[Symbol.iterator]();
198
+ return;
151
199
  }
152
200
  }
153
- return this.getMap().values();
201
+ yield* this.getMap().values();
154
202
  }
155
- entries() {
203
+ *entries() {
204
+ yield* this._setCookies.map(cookie => ['set-cookie', cookie]);
156
205
  if (!this._map) {
157
206
  if (this.headersInit) {
158
207
  if (Array.isArray(this.headersInit)) {
159
- return this.headersInit[Symbol.iterator]();
208
+ yield* this.headersInit;
209
+ return;
160
210
  }
161
211
  if (isHeadersLike(this.headersInit)) {
162
- return this.headersInit.entries();
212
+ yield* this.headersInit.entries();
213
+ return;
163
214
  }
164
- return Object.entries(this.headersInit)[Symbol.iterator]();
215
+ yield* Object.entries(this.headersInit);
216
+ return;
165
217
  }
166
218
  }
167
- return this.getMap().entries();
219
+ yield* this.getMap().entries();
168
220
  }
169
221
  getSetCookie() {
170
- const setCookieHeader = this.get('set-cookie');
171
- if (!setCookieHeader) {
172
- return [];
173
- }
174
- return splitSetCookieHeader(setCookieHeader);
222
+ return this._setCookies;
175
223
  }
176
224
  [Symbol.iterator]() {
177
225
  return this.entries();
@@ -180,7 +228,7 @@ export class PonyfillHeaders {
180
228
  const record = {};
181
229
  this.forEach((value, key) => {
182
230
  if (key === 'set-cookie') {
183
- record['set-cookie'] = this.getSetCookie();
231
+ record['set-cookie'] = this._setCookies;
184
232
  }
185
233
  else {
186
234
  record[key] = value.includes(',') ? value.split(',').map(el => el.trim()) : value;
@@ -189,32 +237,3 @@ export class PonyfillHeaders {
189
237
  return `Headers ${inspect(record)}`;
190
238
  }
191
239
  }
192
- export function splitSetCookieHeader(setCookieHeader) {
193
- const setCookieHeaders = [];
194
- let currentStr = '';
195
- let ignoreComma = false;
196
- for (const ch of setCookieHeader) {
197
- if (currentStr.endsWith('Expires=')) {
198
- ignoreComma = true;
199
- }
200
- if (ignoreComma) {
201
- if (ch === ';') {
202
- ignoreComma = false;
203
- }
204
- if (ch === ',' && currentStr.split('Expires=')[1].length > 3) {
205
- ignoreComma = false;
206
- }
207
- }
208
- if (ch === ',' && !ignoreComma) {
209
- setCookieHeaders.push(currentStr.trim());
210
- currentStr = '';
211
- }
212
- else {
213
- currentStr += ch;
214
- }
215
- }
216
- if (currentStr) {
217
- setCookieHeaders.push(currentStr.trim());
218
- }
219
- return setCookieHeaders;
220
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -5,6 +5,7 @@ export declare class PonyfillHeaders implements Headers {
5
5
  private _map;
6
6
  private objectNormalizedKeysOfHeadersInit;
7
7
  private objectOriginalKeysOfHeadersInit;
8
+ private _setCookies;
8
9
  constructor(headersInit?: PonyfillHeadersInit | undefined);
9
10
  private _get;
10
11
  private getMap;
@@ -20,4 +21,3 @@ export declare class PonyfillHeaders implements Headers {
20
21
  getSetCookie(): string[];
21
22
  [Symbol.iterator](): IterableIterator<[string, string]>;
22
23
  }
23
- export declare function splitSetCookieHeader(setCookieHeader: string): string[];
@@ -5,6 +5,7 @@ export declare class PonyfillHeaders implements Headers {
5
5
  private _map;
6
6
  private objectNormalizedKeysOfHeadersInit;
7
7
  private objectOriginalKeysOfHeadersInit;
8
+ private _setCookies;
8
9
  constructor(headersInit?: PonyfillHeadersInit | undefined);
9
10
  private _get;
10
11
  private getMap;
@@ -20,4 +21,3 @@ export declare class PonyfillHeaders implements Headers {
20
21
  getSetCookie(): string[];
21
22
  [Symbol.iterator](): IterableIterator<[string, string]>;
22
23
  }
23
- export declare function splitSetCookieHeader(setCookieHeader: string): string[];
package/typings/URL.d.cts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="packages/node-fetch/src/declarations.js" />
1
2
  import FastUrl from 'fast-url-parser';
2
3
  import { PonyfillURLSearchParams } from './URLSearchParams.cjs';
3
4
  export declare class PonyfillURL extends FastUrl implements URL {
package/typings/URL.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="packages/node-fetch/src/declarations.js" />
1
2
  import FastUrl from 'fast-url-parser';
2
3
  import { PonyfillURLSearchParams } from './URLSearchParams.js';
3
4
  export declare class PonyfillURL extends FastUrl implements URL {