@whatwg-node/node-fetch 0.0.1-alpha-20221225175918-b8ec28d → 0.0.1-alpha-20221228080239-7c1fd2a

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.
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { Readable } from 'stream';
2
+ import { Readable, Writable } from 'stream';
3
3
  export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
4
4
  readable: Readable;
5
5
  constructor(underlyingSource?: UnderlyingSource<T> | Readable | ReadableStream<T> | PonyfillReadableStream<T>);
@@ -16,4 +16,6 @@ export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
16
16
  writable: WritableStream<T>;
17
17
  readable: ReadableStream<T2>;
18
18
  }): ReadableStream<T2>;
19
+ pipe(writable: Writable): Writable;
20
+ on(event: string, listener: (...args: any[]) => void): this;
19
21
  }
package/index.js CHANGED
@@ -54,28 +54,36 @@ class PonyfillAbortController {
54
54
  }
55
55
 
56
56
  function createController(desiredSize, readable) {
57
- let enqueued = false;
57
+ let chunks = [];
58
58
  return {
59
59
  desiredSize,
60
60
  enqueue(chunk) {
61
- enqueued = true;
62
- readable.push(chunk);
61
+ chunks.push(Buffer.from(chunk));
63
62
  },
64
63
  close() {
64
+ if (chunks.length > 0) {
65
+ this._flush();
66
+ }
65
67
  readable.push(null);
66
68
  },
67
69
  error(error) {
70
+ if (chunks.length > 0) {
71
+ this._flush();
72
+ }
68
73
  readable.destroy(error);
69
74
  },
70
- get enqueued() {
71
- return enqueued;
75
+ _flush() {
76
+ if (chunks.length > 0) {
77
+ const concatenated = Buffer.concat(chunks);
78
+ readable.push(concatenated);
79
+ chunks = [];
80
+ }
72
81
  }
73
82
  };
74
83
  }
75
84
  class PonyfillReadableStream {
76
85
  constructor(underlyingSource) {
77
86
  this.locked = false;
78
- let started = false;
79
87
  if (underlyingSource instanceof PonyfillReadableStream) {
80
88
  this.readable = underlyingSource.readable;
81
89
  }
@@ -83,8 +91,17 @@ class PonyfillReadableStream {
83
91
  this.readable = underlyingSource;
84
92
  }
85
93
  else if (underlyingSource && 'getReader' in underlyingSource) {
86
- const reader = underlyingSource.getReader();
94
+ let reader;
87
95
  this.readable = new stream.Readable({
96
+ construct(callback) {
97
+ try {
98
+ reader = underlyingSource.getReader();
99
+ callback(null);
100
+ }
101
+ catch (err) {
102
+ callback(err);
103
+ }
104
+ },
88
105
  read() {
89
106
  reader
90
107
  .read()
@@ -106,28 +123,23 @@ class PonyfillReadableStream {
106
123
  });
107
124
  }
108
125
  else {
109
- let waitingForPull = false;
110
126
  this.readable = new stream.Readable({
111
- async read(desiredSize) {
112
- var _a, _b;
113
- if (waitingForPull) {
114
- return;
115
- }
116
- waitingForPull = true;
127
+ async construct(callback) {
128
+ var _a;
117
129
  try {
118
- const controller = createController(desiredSize, this);
119
- if (!started) {
120
- started = true;
121
- await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
122
- }
123
- if (!controller.enqueued) {
124
- await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
125
- }
130
+ await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, createController(0, this)));
131
+ callback(null);
126
132
  }
127
- finally {
128
- waitingForPull = false;
133
+ catch (err) {
134
+ callback(err);
129
135
  }
130
136
  },
137
+ async read(desiredSize) {
138
+ var _a;
139
+ const controller = createController(desiredSize, this);
140
+ await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
141
+ controller._flush();
142
+ },
131
143
  async destroy(err, callback) {
132
144
  var _a;
133
145
  try {
@@ -186,6 +198,14 @@ class PonyfillReadableStream {
186
198
  this.pipeTo(writable);
187
199
  return readable;
188
200
  }
201
+ // Trick Fastify
202
+ pipe(writable) {
203
+ return this.readable.pipe(writable);
204
+ }
205
+ on(event, listener) {
206
+ this.readable.on(event, listener);
207
+ return this;
208
+ }
189
209
  }
190
210
 
191
211
  // Will be removed after v14 reaches EOL
@@ -596,18 +616,35 @@ function getHeadersObj(headers) {
596
616
  return obj;
597
617
  }
598
618
 
619
+ function getResponseForFile(url$1) {
620
+ const path = url.fileURLToPath(url$1);
621
+ const readable = fs.createReadStream(path);
622
+ return new PonyfillResponse(readable);
623
+ }
624
+ function getRequestFnForProtocol(protocol) {
625
+ switch (protocol) {
626
+ case 'http':
627
+ return http.request;
628
+ case 'https':
629
+ return https.request;
630
+ }
631
+ throw new Error(`Unsupported protocol: ${protocol}`);
632
+ }
599
633
  const fetchPonyfill = (info, init) => {
600
634
  if (typeof info === 'string' || info instanceof URL) {
601
- return fetchPonyfill(new PonyfillRequest(info, init));
635
+ const ponyfillRequest = new PonyfillRequest(info, init);
636
+ return fetchPonyfill(ponyfillRequest);
602
637
  }
603
638
  const fetchRequest = info;
604
639
  return new Promise((resolve, reject) => {
605
640
  try {
606
- if (fetchRequest.url.startsWith('file://')) {
607
- resolve(new PonyfillResponse(fs.createReadStream(url.fileURLToPath(fetchRequest.url))));
641
+ const protocol = fetchRequest.url.split('://')[0];
642
+ if (protocol === 'file') {
643
+ const response = getResponseForFile(fetchRequest.url);
644
+ resolve(response);
608
645
  return;
609
646
  }
610
- const requestFn = fetchRequest.url.startsWith('https') ? https.request : http.request;
647
+ const requestFn = getRequestFnForProtocol(protocol);
611
648
  const nodeReadable = fetchRequest.readable();
612
649
  const nodeHeaders = getHeadersObj(fetchRequest.headers);
613
650
  const abortListener = function abortListener(event) {
@@ -622,26 +659,30 @@ const fetchPonyfill = (info, init) => {
622
659
  }, nodeResponse => {
623
660
  if (nodeResponse.headers.location) {
624
661
  if (fetchRequest.redirect === 'error') {
625
- reject(new Error('Redirects are not allowed'));
662
+ const redirectError = new Error('Redirects are not allowed');
663
+ reject(redirectError);
626
664
  nodeResponse.resume();
627
665
  return;
628
666
  }
629
667
  if (fetchRequest.redirect === 'follow') {
630
- resolve(fetchPonyfill(new URL(nodeResponse.headers.location, info.url), info).then(resp => {
631
- resp.redirected = true;
632
- return resp;
668
+ const redirectedUrl = new URL(nodeResponse.headers.location, fetchRequest.url);
669
+ const redirectResponse$ = fetchPonyfill(redirectedUrl, info);
670
+ resolve(redirectResponse$.then(redirectResponse => {
671
+ redirectResponse.redirected = true;
672
+ return redirectResponse;
633
673
  }));
634
674
  nodeResponse.resume();
635
675
  return;
636
676
  }
637
677
  }
638
678
  const responseHeaders = nodeResponse.headers;
639
- resolve(new PonyfillResponse(nodeResponse, {
679
+ const ponyfillResponse = new PonyfillResponse(nodeResponse, {
640
680
  status: nodeResponse.statusCode,
641
681
  statusText: nodeResponse.statusMessage,
642
682
  headers: responseHeaders,
643
683
  url: info.url,
644
- }));
684
+ });
685
+ resolve(ponyfillResponse);
645
686
  });
646
687
  nodeRequest.on('error', reject);
647
688
  if (nodeReadable) {
package/index.mjs CHANGED
@@ -50,28 +50,36 @@ class PonyfillAbortController {
50
50
  }
51
51
 
52
52
  function createController(desiredSize, readable) {
53
- let enqueued = false;
53
+ let chunks = [];
54
54
  return {
55
55
  desiredSize,
56
56
  enqueue(chunk) {
57
- enqueued = true;
58
- readable.push(chunk);
57
+ chunks.push(Buffer.from(chunk));
59
58
  },
60
59
  close() {
60
+ if (chunks.length > 0) {
61
+ this._flush();
62
+ }
61
63
  readable.push(null);
62
64
  },
63
65
  error(error) {
66
+ if (chunks.length > 0) {
67
+ this._flush();
68
+ }
64
69
  readable.destroy(error);
65
70
  },
66
- get enqueued() {
67
- return enqueued;
71
+ _flush() {
72
+ if (chunks.length > 0) {
73
+ const concatenated = Buffer.concat(chunks);
74
+ readable.push(concatenated);
75
+ chunks = [];
76
+ }
68
77
  }
69
78
  };
70
79
  }
71
80
  class PonyfillReadableStream {
72
81
  constructor(underlyingSource) {
73
82
  this.locked = false;
74
- let started = false;
75
83
  if (underlyingSource instanceof PonyfillReadableStream) {
76
84
  this.readable = underlyingSource.readable;
77
85
  }
@@ -79,8 +87,17 @@ class PonyfillReadableStream {
79
87
  this.readable = underlyingSource;
80
88
  }
81
89
  else if (underlyingSource && 'getReader' in underlyingSource) {
82
- const reader = underlyingSource.getReader();
90
+ let reader;
83
91
  this.readable = new Readable({
92
+ construct(callback) {
93
+ try {
94
+ reader = underlyingSource.getReader();
95
+ callback(null);
96
+ }
97
+ catch (err) {
98
+ callback(err);
99
+ }
100
+ },
84
101
  read() {
85
102
  reader
86
103
  .read()
@@ -102,28 +119,23 @@ class PonyfillReadableStream {
102
119
  });
103
120
  }
104
121
  else {
105
- let waitingForPull = false;
106
122
  this.readable = new Readable({
107
- async read(desiredSize) {
108
- var _a, _b;
109
- if (waitingForPull) {
110
- return;
111
- }
112
- waitingForPull = true;
123
+ async construct(callback) {
124
+ var _a;
113
125
  try {
114
- const controller = createController(desiredSize, this);
115
- if (!started) {
116
- started = true;
117
- await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
118
- }
119
- if (!controller.enqueued) {
120
- await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
121
- }
126
+ await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, createController(0, this)));
127
+ callback(null);
122
128
  }
123
- finally {
124
- waitingForPull = false;
129
+ catch (err) {
130
+ callback(err);
125
131
  }
126
132
  },
133
+ async read(desiredSize) {
134
+ var _a;
135
+ const controller = createController(desiredSize, this);
136
+ await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
137
+ controller._flush();
138
+ },
127
139
  async destroy(err, callback) {
128
140
  var _a;
129
141
  try {
@@ -182,6 +194,14 @@ class PonyfillReadableStream {
182
194
  this.pipeTo(writable);
183
195
  return readable;
184
196
  }
197
+ // Trick Fastify
198
+ pipe(writable) {
199
+ return this.readable.pipe(writable);
200
+ }
201
+ on(event, listener) {
202
+ this.readable.on(event, listener);
203
+ return this;
204
+ }
185
205
  }
186
206
 
187
207
  // Will be removed after v14 reaches EOL
@@ -592,18 +612,35 @@ function getHeadersObj(headers) {
592
612
  return obj;
593
613
  }
594
614
 
615
+ function getResponseForFile(url) {
616
+ const path = fileURLToPath(url);
617
+ const readable = createReadStream(path);
618
+ return new PonyfillResponse(readable);
619
+ }
620
+ function getRequestFnForProtocol(protocol) {
621
+ switch (protocol) {
622
+ case 'http':
623
+ return request$1;
624
+ case 'https':
625
+ return request;
626
+ }
627
+ throw new Error(`Unsupported protocol: ${protocol}`);
628
+ }
595
629
  const fetchPonyfill = (info, init) => {
596
630
  if (typeof info === 'string' || info instanceof URL) {
597
- return fetchPonyfill(new PonyfillRequest(info, init));
631
+ const ponyfillRequest = new PonyfillRequest(info, init);
632
+ return fetchPonyfill(ponyfillRequest);
598
633
  }
599
634
  const fetchRequest = info;
600
635
  return new Promise((resolve, reject) => {
601
636
  try {
602
- if (fetchRequest.url.startsWith('file://')) {
603
- resolve(new PonyfillResponse(createReadStream(fileURLToPath(fetchRequest.url))));
637
+ const protocol = fetchRequest.url.split('://')[0];
638
+ if (protocol === 'file') {
639
+ const response = getResponseForFile(fetchRequest.url);
640
+ resolve(response);
604
641
  return;
605
642
  }
606
- const requestFn = fetchRequest.url.startsWith('https') ? request : request$1;
643
+ const requestFn = getRequestFnForProtocol(protocol);
607
644
  const nodeReadable = fetchRequest.readable();
608
645
  const nodeHeaders = getHeadersObj(fetchRequest.headers);
609
646
  const abortListener = function abortListener(event) {
@@ -618,26 +655,30 @@ const fetchPonyfill = (info, init) => {
618
655
  }, nodeResponse => {
619
656
  if (nodeResponse.headers.location) {
620
657
  if (fetchRequest.redirect === 'error') {
621
- reject(new Error('Redirects are not allowed'));
658
+ const redirectError = new Error('Redirects are not allowed');
659
+ reject(redirectError);
622
660
  nodeResponse.resume();
623
661
  return;
624
662
  }
625
663
  if (fetchRequest.redirect === 'follow') {
626
- resolve(fetchPonyfill(new URL(nodeResponse.headers.location, info.url), info).then(resp => {
627
- resp.redirected = true;
628
- return resp;
664
+ const redirectedUrl = new URL(nodeResponse.headers.location, fetchRequest.url);
665
+ const redirectResponse$ = fetchPonyfill(redirectedUrl, info);
666
+ resolve(redirectResponse$.then(redirectResponse => {
667
+ redirectResponse.redirected = true;
668
+ return redirectResponse;
629
669
  }));
630
670
  nodeResponse.resume();
631
671
  return;
632
672
  }
633
673
  }
634
674
  const responseHeaders = nodeResponse.headers;
635
- resolve(new PonyfillResponse(nodeResponse, {
675
+ const ponyfillResponse = new PonyfillResponse(nodeResponse, {
636
676
  status: nodeResponse.statusCode,
637
677
  statusText: nodeResponse.statusMessage,
638
678
  headers: responseHeaders,
639
679
  url: info.url,
640
- }));
680
+ });
681
+ resolve(ponyfillResponse);
641
682
  });
642
683
  nodeRequest.on('error', reject);
643
684
  if (nodeReadable) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.0.1-alpha-20221225175918-b8ec28d",
3
+ "version": "0.0.1-alpha-20221228080239-7c1fd2a",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {