@whatwg-node/fetch 0.4.0 → 0.4.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @whatwg-node/fetch
2
2
 
3
+ ## 0.4.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [`c9f05f2`](https://github.com/ardatan/whatwg-node/commit/c9f05f21fb96f63bc22359e3b7981cb9b3b727b5) Thanks [@ardatan](https://github.com/ardatan)! - Add ponyfills for Response.redirect, Response.json and Response.error
8
+
9
+ ## 0.4.2
10
+
11
+ ### Patch Changes
12
+
13
+ - [`7f37b6d`](https://github.com/ardatan/whatwg-node/commit/7f37b6dbeb76cfa54e0ed8672812bf016c1df4fa) Thanks [@ardatan](https://github.com/ardatan)! - fix(fetch): respect filesLimit even with fieldsFirst
14
+
15
+ ## 0.4.1
16
+
17
+ ### Patch Changes
18
+
19
+ - [`53753bb`](https://github.com/ardatan/whatwg-node/commit/53753bb5dd83fbc1e7253784b02f2b1f2e02fdb9) Thanks [@ardatan](https://github.com/ardatan)! - fix(fetch): fix formData function
20
+
3
21
  ## 0.4.0
4
22
 
5
23
  ### Minor Changes
@@ -319,5 +319,34 @@ module.exports = function createNodePonyfill(opts = {}) {
319
319
 
320
320
  }
321
321
  }
322
+
323
+ if (!ponyfills.Response.redirect) {
324
+ ponyfills.Response.redirect = function (url, status = 302) {
325
+ return new ponyfills.Response(null, {
326
+ status,
327
+ headers: {
328
+ Location: url,
329
+ },
330
+ });
331
+ };
332
+ }
333
+ if (!ponyfills.Response.json) {
334
+ ponyfills.Response.json = function (data, init = {}) {
335
+ return new ponyfills.Response(JSON.stringify(data), {
336
+ ...init,
337
+ headers: {
338
+ "Content-Type": "application/json",
339
+ ...init.headers,
340
+ },
341
+ });
342
+ };
343
+ }
344
+ if (!ponyfills.Response.error) {
345
+ ponyfills.Response.error = function () {
346
+ return new ponyfills.Response(null, {
347
+ status: 500,
348
+ });
349
+ };
350
+ }
322
351
  return ponyfills;
323
352
  }
@@ -10,9 +10,6 @@ module.exports = function getFormDataMethod(File, limits) {
10
10
  fileStream,
11
11
  formData,
12
12
  }) {
13
- if (fileStream._consumedAsFile) {
14
- return Promise.resolve(formData.get(name));
15
- }
16
13
  return new Promise((resolve, reject) => {
17
14
  const chunks = [];
18
15
  fileStream.on('limit', () => {
@@ -22,9 +19,11 @@ module.exports = function getFormDataMethod(File, limits) {
22
19
  chunks.push(chunk);
23
20
  })
24
21
  fileStream.on('close', () => {
22
+ if (fileStream.truncated) {
23
+ reject(new Error(`File size limit exceeded: ${limits.fileSize} bytes`));
24
+ }
25
25
  const file = new File(chunks, filename, { type: mimeType });
26
26
  formData.set(name, file);
27
- fileStream._consumedAsFile = true;
28
27
  resolve(file);
29
28
  });
30
29
  })
@@ -58,7 +57,8 @@ module.exports = function getFormDataMethod(File, limits) {
58
57
  reject(new Error(`Fields limit exceeded: ${limits.fields}`));
59
58
  })
60
59
  bb.on('file', (name, fileStream, { filename, mimeType }) => {
61
- if (limits.fieldsFirst) {
60
+ let file$;
61
+ if (limits && limits.fieldsFirst) {
62
62
  resolve(formData);
63
63
  const fakeFileObj = {
64
64
  name: filename,
@@ -80,24 +80,32 @@ module.exports = function getFormDataMethod(File, limits) {
80
80
  throw new Error(`Cannot slice file before consuming the stream.`);
81
81
  case 'text':
82
82
  case 'arrayBuffer':
83
- return () => consumeStreamAsFile({
84
- name,
85
- filename,
86
- mimeType,
87
- fileStream,
88
- formData,
89
- }).then(file => file[prop]())
83
+ return () => {
84
+ if (!file$) {
85
+ file$ = consumeStreamAsFile({
86
+ name,
87
+ filename,
88
+ mimeType,
89
+ fileStream,
90
+ formData,
91
+ })
92
+ }
93
+ return file$.then(file => file[prop]());
94
+ }
90
95
  }
91
96
  },
92
97
  }))
93
98
  } else {
94
- consumeStreamAsFile({
95
- name,
96
- filename,
97
- mimeType,
98
- fileStream,
99
- formData,
100
- }).catch(err => reject(err));
99
+ if (!file$) {
100
+ file$ = consumeStreamAsFile({
101
+ name,
102
+ filename,
103
+ mimeType,
104
+ fileStream,
105
+ formData,
106
+ })
107
+ }
108
+ file$.catch(reject);
101
109
  }
102
110
  })
103
111
  bb.on('filesLimit', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/fetch",
3
- "version": "0.4.0",
3
+ "version": "0.4.3",
4
4
  "description": "Cross Platform Smart Fetch Ponyfill",
5
5
  "author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
6
6
  "repository": {