@whatwg-node/node-fetch 0.7.21-alpha-20250520112227-d65d7be6ee1807534bdd15c2f3f4a1ca18fb93e9 → 0.7.21-alpha-20250520143422-ffbac774c074784de6e135e38979f3a14ba4982f

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/Response.js CHANGED
@@ -49,34 +49,59 @@ class PonyfillResponse extends Body_js_1.PonyfillBody {
49
49
  });
50
50
  }
51
51
  static json(data, init) {
52
+ const bodyInit = JSON.stringify(data);
52
53
  if (!init) {
53
54
  init = {
54
55
  headers: {
55
56
  'content-type': JSON_CONTENT_TYPE,
57
+ 'content-length': Buffer.byteLength(bodyInit).toString(),
56
58
  },
57
59
  };
58
60
  }
59
61
  else if (!init.headers) {
60
62
  init.headers = {
61
63
  'content-type': JSON_CONTENT_TYPE,
64
+ 'content-length': Buffer.byteLength(bodyInit).toString(),
62
65
  };
63
66
  }
64
67
  else if ((0, Headers_js_1.isHeadersLike)(init.headers)) {
65
68
  if (!init.headers.has('content-type')) {
66
69
  init.headers.set('content-type', JSON_CONTENT_TYPE);
67
70
  }
71
+ if (!init.headers.has('content-length')) {
72
+ init.headers.set('content-length', Buffer.byteLength(bodyInit).toString());
73
+ }
68
74
  }
69
75
  else if (Array.isArray(init.headers)) {
70
- if (!init.headers.some(([key]) => key.toLowerCase() === 'content-type')) {
76
+ let contentTypeExists = false;
77
+ let contentLengthExists = false;
78
+ for (const [key] of init.headers) {
79
+ if (contentLengthExists && contentTypeExists) {
80
+ break;
81
+ }
82
+ if (!contentTypeExists && key.toLowerCase() === 'content-type') {
83
+ contentTypeExists = true;
84
+ }
85
+ else if (!contentLengthExists && key.toLowerCase() === 'content-length') {
86
+ contentLengthExists = true;
87
+ }
88
+ }
89
+ if (!contentTypeExists) {
71
90
  init.headers.push(['content-type', JSON_CONTENT_TYPE]);
72
91
  }
92
+ if (!contentLengthExists) {
93
+ init.headers.push(['content-length', Buffer.byteLength(bodyInit).toString()]);
94
+ }
73
95
  }
74
96
  else if (typeof init.headers === 'object') {
75
97
  if (init.headers?.['content-type'] == null) {
76
98
  init.headers['content-type'] = JSON_CONTENT_TYPE;
77
99
  }
100
+ if (init.headers?.['content-length'] == null) {
101
+ init.headers['content-length'] = Buffer.byteLength(bodyInit).toString();
102
+ }
78
103
  }
79
- return new PonyfillResponse(JSON.stringify(data), init);
104
+ return new PonyfillResponse(bodyInit, init);
80
105
  }
81
106
  [Symbol.toStringTag] = 'Response';
82
107
  }
package/esm/Response.js CHANGED
@@ -46,34 +46,59 @@ export class PonyfillResponse extends PonyfillBody {
46
46
  });
47
47
  }
48
48
  static json(data, init) {
49
+ const bodyInit = JSON.stringify(data);
49
50
  if (!init) {
50
51
  init = {
51
52
  headers: {
52
53
  'content-type': JSON_CONTENT_TYPE,
54
+ 'content-length': Buffer.byteLength(bodyInit).toString(),
53
55
  },
54
56
  };
55
57
  }
56
58
  else if (!init.headers) {
57
59
  init.headers = {
58
60
  'content-type': JSON_CONTENT_TYPE,
61
+ 'content-length': Buffer.byteLength(bodyInit).toString(),
59
62
  };
60
63
  }
61
64
  else if (isHeadersLike(init.headers)) {
62
65
  if (!init.headers.has('content-type')) {
63
66
  init.headers.set('content-type', JSON_CONTENT_TYPE);
64
67
  }
68
+ if (!init.headers.has('content-length')) {
69
+ init.headers.set('content-length', Buffer.byteLength(bodyInit).toString());
70
+ }
65
71
  }
66
72
  else if (Array.isArray(init.headers)) {
67
- if (!init.headers.some(([key]) => key.toLowerCase() === 'content-type')) {
73
+ let contentTypeExists = false;
74
+ let contentLengthExists = false;
75
+ for (const [key] of init.headers) {
76
+ if (contentLengthExists && contentTypeExists) {
77
+ break;
78
+ }
79
+ if (!contentTypeExists && key.toLowerCase() === 'content-type') {
80
+ contentTypeExists = true;
81
+ }
82
+ else if (!contentLengthExists && key.toLowerCase() === 'content-length') {
83
+ contentLengthExists = true;
84
+ }
85
+ }
86
+ if (!contentTypeExists) {
68
87
  init.headers.push(['content-type', JSON_CONTENT_TYPE]);
69
88
  }
89
+ if (!contentLengthExists) {
90
+ init.headers.push(['content-length', Buffer.byteLength(bodyInit).toString()]);
91
+ }
70
92
  }
71
93
  else if (typeof init.headers === 'object') {
72
94
  if (init.headers?.['content-type'] == null) {
73
95
  init.headers['content-type'] = JSON_CONTENT_TYPE;
74
96
  }
97
+ if (init.headers?.['content-length'] == null) {
98
+ init.headers['content-length'] = Buffer.byteLength(bodyInit).toString();
99
+ }
75
100
  }
76
- return new PonyfillResponse(JSON.stringify(data), init);
101
+ return new PonyfillResponse(bodyInit, init);
77
102
  }
78
103
  [Symbol.toStringTag] = 'Response';
79
104
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.7.21-alpha-20250520112227-d65d7be6ee1807534bdd15c2f3f4a1ca18fb93e9",
3
+ "version": "0.7.21-alpha-20250520143422-ffbac774c074784de6e135e38979f3a14ba4982f",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {