@whatwg-node/node-fetch 0.4.4 → 0.4.5-alpha-20230613171450-41cad7c

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 CHANGED
@@ -231,27 +231,29 @@ function processBodyInit(bodyInit) {
231
231
  contentLength: null,
232
232
  };
233
233
  }
234
- if (bodyInit instanceof Buffer) {
235
- const contentLength = bodyInit.length;
234
+ if (typeof bodyInit === 'string') {
235
+ const buffer = Buffer.from(bodyInit);
236
+ const contentLength = buffer.byteLength;
236
237
  return {
237
- bodyType: BodyInitType.Buffer,
238
+ bodyType: BodyInitType.String,
239
+ contentType: 'text/plain;charset=UTF-8',
238
240
  contentLength,
239
- contentType: null,
240
241
  bodyFactory() {
241
- const readable = stream_1.Readable.from(bodyInit);
242
- const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
243
- return body;
242
+ const readable = stream_1.Readable.from(buffer);
243
+ return new ReadableStream_js_1.PonyfillReadableStream(readable);
244
244
  },
245
245
  };
246
246
  }
247
- if (typeof bodyInit === 'string') {
247
+ if (bodyInit instanceof Buffer) {
248
+ const contentLength = bodyInit.byteLength;
248
249
  return {
249
- bodyType: BodyInitType.String,
250
- contentType: 'text/plain;charset=UTF-8',
251
- contentLength: Buffer.byteLength(bodyInit),
250
+ bodyType: BodyInitType.Buffer,
251
+ contentLength,
252
+ contentType: null,
252
253
  bodyFactory() {
253
254
  const readable = stream_1.Readable.from(bodyInit);
254
- return new ReadableStream_js_1.PonyfillReadableStream(readable);
255
+ const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
256
+ return body;
255
257
  },
256
258
  };
257
259
  }
package/cjs/Response.js CHANGED
@@ -60,25 +60,14 @@ class PonyfillResponse extends Body_js_1.PonyfillBody {
60
60
  });
61
61
  }
62
62
  static json(data, init = {}) {
63
- let bodyInit = JSON.stringify(data);
64
- if (init.headers != null) {
65
- init.headers = new Headers_js_1.PonyfillHeaders(init.headers);
66
- if (!init.headers.has('content-type')) {
67
- init.headers.set('content-type', JSON_CONTENT_TYPE);
68
- }
69
- if (!init.headers.has('content-length')) {
70
- bodyInit = Buffer.from(bodyInit);
71
- init.headers.set('content-length', bodyInit.byteLength.toString());
72
- }
73
- }
74
- else {
75
- bodyInit = Buffer.from(bodyInit);
76
- init.headers = [
77
- ['content-type', JSON_CONTENT_TYPE],
78
- ['content-length', bodyInit.byteLength.toString()],
79
- ];
63
+ init.headers =
64
+ init?.headers && (0, Headers_js_1.isHeadersLike)(init.headers)
65
+ ? init.headers
66
+ : new Headers_js_1.PonyfillHeaders(init?.headers);
67
+ if (!init.headers.has('content-type')) {
68
+ init.headers.set('content-type', JSON_CONTENT_TYPE);
80
69
  }
81
- return new PonyfillResponse(bodyInit, init);
70
+ return new PonyfillResponse(JSON.stringify(data), init);
82
71
  }
83
72
  }
84
73
  exports.PonyfillResponse = PonyfillResponse;
package/esm/Body.js CHANGED
@@ -226,27 +226,29 @@ function processBodyInit(bodyInit) {
226
226
  contentLength: null,
227
227
  };
228
228
  }
229
- if (bodyInit instanceof Buffer) {
230
- const contentLength = bodyInit.length;
229
+ if (typeof bodyInit === 'string') {
230
+ const buffer = Buffer.from(bodyInit);
231
+ const contentLength = buffer.byteLength;
231
232
  return {
232
- bodyType: BodyInitType.Buffer,
233
+ bodyType: BodyInitType.String,
234
+ contentType: 'text/plain;charset=UTF-8',
233
235
  contentLength,
234
- contentType: null,
235
236
  bodyFactory() {
236
- const readable = Readable.from(bodyInit);
237
- const body = new PonyfillReadableStream(readable);
238
- return body;
237
+ const readable = Readable.from(buffer);
238
+ return new PonyfillReadableStream(readable);
239
239
  },
240
240
  };
241
241
  }
242
- if (typeof bodyInit === 'string') {
242
+ if (bodyInit instanceof Buffer) {
243
+ const contentLength = bodyInit.byteLength;
243
244
  return {
244
- bodyType: BodyInitType.String,
245
- contentType: 'text/plain;charset=UTF-8',
246
- contentLength: Buffer.byteLength(bodyInit),
245
+ bodyType: BodyInitType.Buffer,
246
+ contentLength,
247
+ contentType: null,
247
248
  bodyFactory() {
248
249
  const readable = Readable.from(bodyInit);
249
- return new PonyfillReadableStream(readable);
250
+ const body = new PonyfillReadableStream(readable);
251
+ return body;
250
252
  },
251
253
  };
252
254
  }
package/esm/Response.js CHANGED
@@ -57,24 +57,13 @@ export class PonyfillResponse extends PonyfillBody {
57
57
  });
58
58
  }
59
59
  static json(data, init = {}) {
60
- let bodyInit = JSON.stringify(data);
61
- if (init.headers != null) {
62
- init.headers = new PonyfillHeaders(init.headers);
63
- if (!init.headers.has('content-type')) {
64
- init.headers.set('content-type', JSON_CONTENT_TYPE);
65
- }
66
- if (!init.headers.has('content-length')) {
67
- bodyInit = Buffer.from(bodyInit);
68
- init.headers.set('content-length', bodyInit.byteLength.toString());
69
- }
70
- }
71
- else {
72
- bodyInit = Buffer.from(bodyInit);
73
- init.headers = [
74
- ['content-type', JSON_CONTENT_TYPE],
75
- ['content-length', bodyInit.byteLength.toString()],
76
- ];
60
+ init.headers =
61
+ init?.headers && isHeadersLike(init.headers)
62
+ ? init.headers
63
+ : new PonyfillHeaders(init?.headers);
64
+ if (!init.headers.has('content-type')) {
65
+ init.headers.set('content-type', JSON_CONTENT_TYPE);
77
66
  }
78
- return new PonyfillResponse(bodyInit, init);
67
+ return new PonyfillResponse(JSON.stringify(data), init);
79
68
  }
80
69
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.4.4",
3
+ "version": "0.4.5-alpha-20230613171450-41cad7c",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {