@whatwg-node/node-fetch 0.4.4-alpha-20230613145642-e02eaf5 → 0.4.4-alpha-20230613154123-72c31a9
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/Body.js +13 -13
- package/cjs/Headers.js +32 -43
- package/cjs/Request.js +17 -15
- package/cjs/fetch.js +1 -2
- package/esm/Body.js +13 -13
- package/esm/Headers.js +32 -43
- package/esm/Request.js +17 -15
- package/esm/fetch.js +1 -2
- package/package.json +1 -1
- package/typings/Headers.d.cts +2 -3
- package/typings/Headers.d.ts +2 -3
package/cjs/Body.js
CHANGED
@@ -231,6 +231,19 @@ function processBodyInit(bodyInit) {
|
|
231
231
|
contentLength: null,
|
232
232
|
};
|
233
233
|
}
|
234
|
+
if (bodyInit instanceof Buffer) {
|
235
|
+
const contentLength = bodyInit.length;
|
236
|
+
return {
|
237
|
+
bodyType: BodyInitType.Buffer,
|
238
|
+
contentLength,
|
239
|
+
contentType: null,
|
240
|
+
bodyFactory() {
|
241
|
+
const readable = stream_1.Readable.from(bodyInit);
|
242
|
+
const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
|
243
|
+
return body;
|
244
|
+
},
|
245
|
+
};
|
246
|
+
}
|
234
247
|
if (typeof bodyInit === 'string') {
|
235
248
|
return {
|
236
249
|
bodyType: BodyInitType.String,
|
@@ -260,19 +273,6 @@ function processBodyInit(bodyInit) {
|
|
260
273
|
},
|
261
274
|
};
|
262
275
|
}
|
263
|
-
if (bodyInit instanceof Buffer) {
|
264
|
-
const contentLength = bodyInit.length;
|
265
|
-
return {
|
266
|
-
bodyType: BodyInitType.Buffer,
|
267
|
-
contentLength,
|
268
|
-
contentType: null,
|
269
|
-
bodyFactory() {
|
270
|
-
const readable = stream_1.Readable.from(bodyInit);
|
271
|
-
const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
|
272
|
-
return body;
|
273
|
-
},
|
274
|
-
};
|
275
|
-
}
|
276
276
|
if (bodyInit instanceof Uint8Array) {
|
277
277
|
const contentLength = bodyInit.byteLength;
|
278
278
|
return {
|
package/cjs/Headers.js
CHANGED
@@ -8,16 +8,14 @@ exports.isHeadersLike = isHeadersLike;
|
|
8
8
|
class PonyfillHeaders {
|
9
9
|
constructor(headersInit) {
|
10
10
|
this.headersInit = headersInit;
|
11
|
-
this.map = new Map();
|
12
|
-
this.mapIsBuilt = false;
|
13
11
|
this.objectNormalizedKeysOfHeadersInit = [];
|
14
12
|
this.objectOriginalKeysOfHeadersInit = [];
|
15
13
|
}
|
16
14
|
// perf: we don't need to build `this.map` for Requests, as we can access the headers directly
|
17
15
|
_get(key) {
|
18
16
|
// If the map is built, reuse it
|
19
|
-
if (this.
|
20
|
-
return this.
|
17
|
+
if (this._map) {
|
18
|
+
return this._map.get(key.toLowerCase()) || null;
|
21
19
|
}
|
22
20
|
// If the map is not built, try to get the value from the this.headersInit
|
23
21
|
if (this.headersInit == null) {
|
@@ -25,7 +23,7 @@ class PonyfillHeaders {
|
|
25
23
|
}
|
26
24
|
const normalized = key.toLowerCase();
|
27
25
|
if (Array.isArray(this.headersInit)) {
|
28
|
-
return this.headersInit.find(header => header[0] === normalized);
|
26
|
+
return this.headersInit.find(header => header[0] === normalized)?.[1] || null;
|
29
27
|
}
|
30
28
|
else if (isHeadersLike(this.headersInit)) {
|
31
29
|
return this.headersInit.get(normalized);
|
@@ -52,31 +50,33 @@ class PonyfillHeaders {
|
|
52
50
|
// perf: Build the map of headers lazily, only when we need to access all headers or write to it.
|
53
51
|
// I could do a getter here, but I'm too lazy to type `getter`.
|
54
52
|
getMap() {
|
55
|
-
if (this.
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
53
|
+
if (!this._map) {
|
54
|
+
if (this.headersInit != null) {
|
55
|
+
if (Array.isArray(this.headersInit)) {
|
56
|
+
this._map = new Map(this.headersInit);
|
57
|
+
}
|
58
|
+
else if (isHeadersLike(this.headersInit)) {
|
59
|
+
this._map = new Map();
|
60
|
+
this.headersInit.forEach((value, key) => {
|
61
|
+
this._map.set(key, value);
|
62
|
+
});
|
63
|
+
}
|
64
|
+
else {
|
65
|
+
this._map = new Map();
|
66
|
+
for (const initKey in this.headersInit) {
|
67
|
+
const initValue = this.headersInit[initKey];
|
68
|
+
if (initValue != null) {
|
69
|
+
const normalizedKey = initKey.toLowerCase();
|
70
|
+
this._map.set(normalizedKey, initValue);
|
71
|
+
}
|
74
72
|
}
|
75
73
|
}
|
76
74
|
}
|
75
|
+
else {
|
76
|
+
this._map = new Map();
|
77
|
+
}
|
77
78
|
}
|
78
|
-
this.
|
79
|
-
return this.map;
|
79
|
+
return this._map;
|
80
80
|
}
|
81
81
|
append(name, value) {
|
82
82
|
const key = name.toLowerCase();
|
@@ -85,19 +85,14 @@ class PonyfillHeaders {
|
|
85
85
|
this.getMap().set(key, finalValue);
|
86
86
|
}
|
87
87
|
get(name) {
|
88
|
-
const
|
89
|
-
const value = this._get(key);
|
88
|
+
const value = this._get(name);
|
90
89
|
if (value == null) {
|
91
90
|
return null;
|
92
91
|
}
|
93
|
-
if (Array.isArray(value)) {
|
94
|
-
return value.join(', ');
|
95
|
-
}
|
96
92
|
return value;
|
97
93
|
}
|
98
94
|
has(name) {
|
99
|
-
|
100
|
-
return !!this._get(key); // we might need to check if header exists and not just check if it's not nullable
|
95
|
+
return !!this._get(name); // we might need to check if header exists and not just check if it's not nullable
|
101
96
|
}
|
102
97
|
set(name, value) {
|
103
98
|
const key = name.toLowerCase();
|
@@ -108,7 +103,7 @@ class PonyfillHeaders {
|
|
108
103
|
this.getMap().delete(key);
|
109
104
|
}
|
110
105
|
forEach(callback) {
|
111
|
-
if (!this.
|
106
|
+
if (!this._map) {
|
112
107
|
if (this.headersInit) {
|
113
108
|
if (Array.isArray(this.headersInit)) {
|
114
109
|
this.headersInit.forEach(([key, value]) => {
|
@@ -122,12 +117,6 @@ class PonyfillHeaders {
|
|
122
117
|
}
|
123
118
|
Object.entries(this.headersInit).forEach(([key, value]) => {
|
124
119
|
if (value != null) {
|
125
|
-
if (Array.isArray(value)) {
|
126
|
-
value.forEach(v => {
|
127
|
-
callback(v, key, this);
|
128
|
-
});
|
129
|
-
return;
|
130
|
-
}
|
131
120
|
callback(value, key, this);
|
132
121
|
}
|
133
122
|
});
|
@@ -139,7 +128,7 @@ class PonyfillHeaders {
|
|
139
128
|
});
|
140
129
|
}
|
141
130
|
keys() {
|
142
|
-
if (!this.
|
131
|
+
if (!this._map) {
|
143
132
|
if (this.headersInit) {
|
144
133
|
if (Array.isArray(this.headersInit)) {
|
145
134
|
return this.headersInit.map(([key]) => key)[Symbol.iterator]();
|
@@ -153,7 +142,7 @@ class PonyfillHeaders {
|
|
153
142
|
return this.getMap().keys();
|
154
143
|
}
|
155
144
|
values() {
|
156
|
-
if (!this.
|
145
|
+
if (!this._map) {
|
157
146
|
if (this.headersInit) {
|
158
147
|
if (Array.isArray(this.headersInit)) {
|
159
148
|
return this.headersInit.map(([, value]) => value)[Symbol.iterator]();
|
@@ -167,7 +156,7 @@ class PonyfillHeaders {
|
|
167
156
|
return this.getMap().values();
|
168
157
|
}
|
169
158
|
entries() {
|
170
|
-
if (!this.
|
159
|
+
if (!this._map) {
|
171
160
|
if (this.headersInit) {
|
172
161
|
if (Array.isArray(this.headersInit)) {
|
173
162
|
return this.headersInit[Symbol.iterator]();
|
package/cjs/Request.js
CHANGED
@@ -47,23 +47,25 @@ class PonyfillRequest extends Body_js_1.PonyfillBody {
|
|
47
47
|
this.url = url || '';
|
48
48
|
this.destination = 'document';
|
49
49
|
this.priority = 'auto';
|
50
|
-
|
51
|
-
|
52
|
-
if (
|
53
|
-
|
50
|
+
if (this.method !== 'GET' && this.method !== 'HEAD') {
|
51
|
+
const contentTypeInHeaders = this.headers.get('content-type');
|
52
|
+
if (!contentTypeInHeaders) {
|
53
|
+
if (this.contentType) {
|
54
|
+
this.headers.set('content-type', this.contentType);
|
55
|
+
}
|
54
56
|
}
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
57
|
+
else {
|
58
|
+
this.contentType = contentTypeInHeaders;
|
59
|
+
}
|
60
|
+
const contentLengthInHeaders = this.headers.get('content-length');
|
61
|
+
if (!contentLengthInHeaders) {
|
62
|
+
if (this.contentLength) {
|
63
|
+
this.headers.set('content-length', this.contentLength.toString());
|
64
|
+
}
|
65
|
+
}
|
66
|
+
else {
|
67
|
+
this.contentLength = parseInt(contentLengthInHeaders, 10);
|
63
68
|
}
|
64
|
-
}
|
65
|
-
else {
|
66
|
-
this.contentLength = parseInt(contentLengthInHeaders, 10);
|
67
69
|
}
|
68
70
|
}
|
69
71
|
get signal() {
|
package/cjs/fetch.js
CHANGED
@@ -125,11 +125,10 @@ function fetchPonyfill(info, init) {
|
|
125
125
|
return;
|
126
126
|
}
|
127
127
|
}
|
128
|
-
const responseHeaders = nodeResponse.headers;
|
129
128
|
const ponyfillResponse = new Response_js_1.PonyfillResponse(responseBody, {
|
130
129
|
status: nodeResponse.statusCode,
|
131
130
|
statusText: nodeResponse.statusMessage,
|
132
|
-
headers:
|
131
|
+
headers: nodeResponse.headers,
|
133
132
|
url: info.url,
|
134
133
|
});
|
135
134
|
resolve(ponyfillResponse);
|
package/esm/Body.js
CHANGED
@@ -226,6 +226,19 @@ function processBodyInit(bodyInit) {
|
|
226
226
|
contentLength: null,
|
227
227
|
};
|
228
228
|
}
|
229
|
+
if (bodyInit instanceof Buffer) {
|
230
|
+
const contentLength = bodyInit.length;
|
231
|
+
return {
|
232
|
+
bodyType: BodyInitType.Buffer,
|
233
|
+
contentLength,
|
234
|
+
contentType: null,
|
235
|
+
bodyFactory() {
|
236
|
+
const readable = Readable.from(bodyInit);
|
237
|
+
const body = new PonyfillReadableStream(readable);
|
238
|
+
return body;
|
239
|
+
},
|
240
|
+
};
|
241
|
+
}
|
229
242
|
if (typeof bodyInit === 'string') {
|
230
243
|
return {
|
231
244
|
bodyType: BodyInitType.String,
|
@@ -255,19 +268,6 @@ function processBodyInit(bodyInit) {
|
|
255
268
|
},
|
256
269
|
};
|
257
270
|
}
|
258
|
-
if (bodyInit instanceof Buffer) {
|
259
|
-
const contentLength = bodyInit.length;
|
260
|
-
return {
|
261
|
-
bodyType: BodyInitType.Buffer,
|
262
|
-
contentLength,
|
263
|
-
contentType: null,
|
264
|
-
bodyFactory() {
|
265
|
-
const readable = Readable.from(bodyInit);
|
266
|
-
const body = new PonyfillReadableStream(readable);
|
267
|
-
return body;
|
268
|
-
},
|
269
|
-
};
|
270
|
-
}
|
271
271
|
if (bodyInit instanceof Uint8Array) {
|
272
272
|
const contentLength = bodyInit.byteLength;
|
273
273
|
return {
|
package/esm/Headers.js
CHANGED
@@ -4,16 +4,14 @@ export function isHeadersLike(headers) {
|
|
4
4
|
export class PonyfillHeaders {
|
5
5
|
constructor(headersInit) {
|
6
6
|
this.headersInit = headersInit;
|
7
|
-
this.map = new Map();
|
8
|
-
this.mapIsBuilt = false;
|
9
7
|
this.objectNormalizedKeysOfHeadersInit = [];
|
10
8
|
this.objectOriginalKeysOfHeadersInit = [];
|
11
9
|
}
|
12
10
|
// perf: we don't need to build `this.map` for Requests, as we can access the headers directly
|
13
11
|
_get(key) {
|
14
12
|
// If the map is built, reuse it
|
15
|
-
if (this.
|
16
|
-
return this.
|
13
|
+
if (this._map) {
|
14
|
+
return this._map.get(key.toLowerCase()) || null;
|
17
15
|
}
|
18
16
|
// If the map is not built, try to get the value from the this.headersInit
|
19
17
|
if (this.headersInit == null) {
|
@@ -21,7 +19,7 @@ export class PonyfillHeaders {
|
|
21
19
|
}
|
22
20
|
const normalized = key.toLowerCase();
|
23
21
|
if (Array.isArray(this.headersInit)) {
|
24
|
-
return this.headersInit.find(header => header[0] === normalized);
|
22
|
+
return this.headersInit.find(header => header[0] === normalized)?.[1] || null;
|
25
23
|
}
|
26
24
|
else if (isHeadersLike(this.headersInit)) {
|
27
25
|
return this.headersInit.get(normalized);
|
@@ -48,31 +46,33 @@ export class PonyfillHeaders {
|
|
48
46
|
// perf: Build the map of headers lazily, only when we need to access all headers or write to it.
|
49
47
|
// I could do a getter here, but I'm too lazy to type `getter`.
|
50
48
|
getMap() {
|
51
|
-
if (this.
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
}
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
49
|
+
if (!this._map) {
|
50
|
+
if (this.headersInit != null) {
|
51
|
+
if (Array.isArray(this.headersInit)) {
|
52
|
+
this._map = new Map(this.headersInit);
|
53
|
+
}
|
54
|
+
else if (isHeadersLike(this.headersInit)) {
|
55
|
+
this._map = new Map();
|
56
|
+
this.headersInit.forEach((value, key) => {
|
57
|
+
this._map.set(key, value);
|
58
|
+
});
|
59
|
+
}
|
60
|
+
else {
|
61
|
+
this._map = new Map();
|
62
|
+
for (const initKey in this.headersInit) {
|
63
|
+
const initValue = this.headersInit[initKey];
|
64
|
+
if (initValue != null) {
|
65
|
+
const normalizedKey = initKey.toLowerCase();
|
66
|
+
this._map.set(normalizedKey, initValue);
|
67
|
+
}
|
70
68
|
}
|
71
69
|
}
|
72
70
|
}
|
71
|
+
else {
|
72
|
+
this._map = new Map();
|
73
|
+
}
|
73
74
|
}
|
74
|
-
this.
|
75
|
-
return this.map;
|
75
|
+
return this._map;
|
76
76
|
}
|
77
77
|
append(name, value) {
|
78
78
|
const key = name.toLowerCase();
|
@@ -81,19 +81,14 @@ export class PonyfillHeaders {
|
|
81
81
|
this.getMap().set(key, finalValue);
|
82
82
|
}
|
83
83
|
get(name) {
|
84
|
-
const
|
85
|
-
const value = this._get(key);
|
84
|
+
const value = this._get(name);
|
86
85
|
if (value == null) {
|
87
86
|
return null;
|
88
87
|
}
|
89
|
-
if (Array.isArray(value)) {
|
90
|
-
return value.join(', ');
|
91
|
-
}
|
92
88
|
return value;
|
93
89
|
}
|
94
90
|
has(name) {
|
95
|
-
|
96
|
-
return !!this._get(key); // we might need to check if header exists and not just check if it's not nullable
|
91
|
+
return !!this._get(name); // we might need to check if header exists and not just check if it's not nullable
|
97
92
|
}
|
98
93
|
set(name, value) {
|
99
94
|
const key = name.toLowerCase();
|
@@ -104,7 +99,7 @@ export class PonyfillHeaders {
|
|
104
99
|
this.getMap().delete(key);
|
105
100
|
}
|
106
101
|
forEach(callback) {
|
107
|
-
if (!this.
|
102
|
+
if (!this._map) {
|
108
103
|
if (this.headersInit) {
|
109
104
|
if (Array.isArray(this.headersInit)) {
|
110
105
|
this.headersInit.forEach(([key, value]) => {
|
@@ -118,12 +113,6 @@ export class PonyfillHeaders {
|
|
118
113
|
}
|
119
114
|
Object.entries(this.headersInit).forEach(([key, value]) => {
|
120
115
|
if (value != null) {
|
121
|
-
if (Array.isArray(value)) {
|
122
|
-
value.forEach(v => {
|
123
|
-
callback(v, key, this);
|
124
|
-
});
|
125
|
-
return;
|
126
|
-
}
|
127
116
|
callback(value, key, this);
|
128
117
|
}
|
129
118
|
});
|
@@ -135,7 +124,7 @@ export class PonyfillHeaders {
|
|
135
124
|
});
|
136
125
|
}
|
137
126
|
keys() {
|
138
|
-
if (!this.
|
127
|
+
if (!this._map) {
|
139
128
|
if (this.headersInit) {
|
140
129
|
if (Array.isArray(this.headersInit)) {
|
141
130
|
return this.headersInit.map(([key]) => key)[Symbol.iterator]();
|
@@ -149,7 +138,7 @@ export class PonyfillHeaders {
|
|
149
138
|
return this.getMap().keys();
|
150
139
|
}
|
151
140
|
values() {
|
152
|
-
if (!this.
|
141
|
+
if (!this._map) {
|
153
142
|
if (this.headersInit) {
|
154
143
|
if (Array.isArray(this.headersInit)) {
|
155
144
|
return this.headersInit.map(([, value]) => value)[Symbol.iterator]();
|
@@ -163,7 +152,7 @@ export class PonyfillHeaders {
|
|
163
152
|
return this.getMap().values();
|
164
153
|
}
|
165
154
|
entries() {
|
166
|
-
if (!this.
|
155
|
+
if (!this._map) {
|
167
156
|
if (this.headersInit) {
|
168
157
|
if (Array.isArray(this.headersInit)) {
|
169
158
|
return this.headersInit[Symbol.iterator]();
|
package/esm/Request.js
CHANGED
@@ -44,23 +44,25 @@ export class PonyfillRequest extends PonyfillBody {
|
|
44
44
|
this.url = url || '';
|
45
45
|
this.destination = 'document';
|
46
46
|
this.priority = 'auto';
|
47
|
-
|
48
|
-
|
49
|
-
if (
|
50
|
-
|
47
|
+
if (this.method !== 'GET' && this.method !== 'HEAD') {
|
48
|
+
const contentTypeInHeaders = this.headers.get('content-type');
|
49
|
+
if (!contentTypeInHeaders) {
|
50
|
+
if (this.contentType) {
|
51
|
+
this.headers.set('content-type', this.contentType);
|
52
|
+
}
|
51
53
|
}
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
54
|
+
else {
|
55
|
+
this.contentType = contentTypeInHeaders;
|
56
|
+
}
|
57
|
+
const contentLengthInHeaders = this.headers.get('content-length');
|
58
|
+
if (!contentLengthInHeaders) {
|
59
|
+
if (this.contentLength) {
|
60
|
+
this.headers.set('content-length', this.contentLength.toString());
|
61
|
+
}
|
62
|
+
}
|
63
|
+
else {
|
64
|
+
this.contentLength = parseInt(contentLengthInHeaders, 10);
|
60
65
|
}
|
61
|
-
}
|
62
|
-
else {
|
63
|
-
this.contentLength = parseInt(contentLengthInHeaders, 10);
|
64
66
|
}
|
65
67
|
}
|
66
68
|
get signal() {
|
package/esm/fetch.js
CHANGED
@@ -122,11 +122,10 @@ export function fetchPonyfill(info, init) {
|
|
122
122
|
return;
|
123
123
|
}
|
124
124
|
}
|
125
|
-
const responseHeaders = nodeResponse.headers;
|
126
125
|
const ponyfillResponse = new PonyfillResponse(responseBody, {
|
127
126
|
status: nodeResponse.statusCode,
|
128
127
|
statusText: nodeResponse.statusMessage,
|
129
|
-
headers:
|
128
|
+
headers: nodeResponse.headers,
|
130
129
|
url: info.url,
|
131
130
|
});
|
132
131
|
resolve(ponyfillResponse);
|
package/package.json
CHANGED
package/typings/Headers.d.cts
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
export type PonyfillHeadersInit = [string, string][] | Record<string, string
|
1
|
+
export type PonyfillHeadersInit = [string, string][] | Record<string, string> | Headers;
|
2
2
|
export declare function isHeadersLike(headers: any): headers is Headers;
|
3
3
|
export declare class PonyfillHeaders implements Headers {
|
4
4
|
private headersInit?;
|
5
|
-
private
|
6
|
-
private mapIsBuilt;
|
5
|
+
private _map;
|
7
6
|
private objectNormalizedKeysOfHeadersInit;
|
8
7
|
private objectOriginalKeysOfHeadersInit;
|
9
8
|
constructor(headersInit?: PonyfillHeadersInit | undefined);
|
package/typings/Headers.d.ts
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
export type PonyfillHeadersInit = [string, string][] | Record<string, string
|
1
|
+
export type PonyfillHeadersInit = [string, string][] | Record<string, string> | Headers;
|
2
2
|
export declare function isHeadersLike(headers: any): headers is Headers;
|
3
3
|
export declare class PonyfillHeaders implements Headers {
|
4
4
|
private headersInit?;
|
5
|
-
private
|
6
|
-
private mapIsBuilt;
|
5
|
+
private _map;
|
7
6
|
private objectNormalizedKeysOfHeadersInit;
|
8
7
|
private objectOriginalKeysOfHeadersInit;
|
9
8
|
constructor(headersInit?: PonyfillHeadersInit | undefined);
|