@whatwg-node/node-fetch 0.0.7-alpha-20230208150005-0bd8aaf → 0.0.7-alpha-20230209144328-8bdce99

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/Blob.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { Blob as NodeBlob } from 'buffer';
3
- declare const BaseBlob: typeof NodeBlob;
4
- export declare class PonyfillBlob extends BaseBlob implements Blob {
3
+ declare const PonyfillBlob_base: typeof NodeBlob;
4
+ export declare class PonyfillBlob extends PonyfillBlob_base implements Blob {
5
5
  stream(): any;
6
6
  slice(...args: any[]): any;
7
7
  }
package/Headers.d.ts CHANGED
@@ -1,12 +1,7 @@
1
1
  export type PonyfillHeadersInit = [string, string][] | Record<string, string | string[] | undefined> | Headers;
2
2
  export declare class PonyfillHeaders implements Headers {
3
- private headersInit?;
4
3
  private map;
5
- private mapIsBuilt;
6
- private objectKeysOfeadersInit;
7
- constructor(headersInit?: PonyfillHeadersInit | undefined);
8
- private _get;
9
- private getMap;
4
+ constructor(headersInit?: PonyfillHeadersInit);
10
5
  append(name: string, value: string): void;
11
6
  get(name: string): string | null;
12
7
  has(name: string): boolean;
package/index.js CHANGED
@@ -33,10 +33,17 @@ class PonyfillAbortError extends Error {
33
33
  function createController(desiredSize, readable) {
34
34
  let chunks = [];
35
35
  let _closed = false;
36
+ let flushed = false;
36
37
  return {
37
38
  desiredSize,
38
39
  enqueue(chunk) {
39
- chunks.push(Buffer.from(chunk));
40
+ const buf = typeof chunk === 'string' ? Buffer.from(chunk) : chunk;
41
+ if (!flushed) {
42
+ chunks.push(buf);
43
+ }
44
+ else {
45
+ readable.push(buf);
46
+ }
40
47
  },
41
48
  close() {
42
49
  if (chunks.length > 0) {
@@ -55,6 +62,7 @@ function createController(desiredSize, readable) {
55
62
  return _closed;
56
63
  },
57
64
  _flush() {
65
+ flushed = true;
58
66
  if (chunks.length > 0) {
59
67
  const concatenated = Buffer.concat(chunks);
60
68
  readable.push(concatenated);
@@ -102,18 +110,29 @@ class PonyfillReadableStream {
102
110
  }
103
111
  else {
104
112
  let started = false;
113
+ let ongoing = false;
105
114
  this.readable = new stream.Readable({
106
- async read(desiredSize) {
107
- var _a, _b;
108
- const controller = createController(desiredSize, this);
109
- if (!started) {
110
- started = true;
111
- await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
115
+ read(desiredSize) {
116
+ if (ongoing) {
117
+ return;
112
118
  }
113
- if (!controller._closed) {
119
+ ongoing = true;
120
+ return Promise.resolve().then(async () => {
121
+ var _a, _b;
122
+ if (!started) {
123
+ const controller = createController(desiredSize, this);
124
+ started = true;
125
+ await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
126
+ controller._flush();
127
+ if (controller._closed) {
128
+ return;
129
+ }
130
+ }
131
+ const controller = createController(desiredSize, this);
114
132
  await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
115
- }
116
- controller._flush();
133
+ controller._flush();
134
+ ongoing = false;
135
+ });
117
136
  },
118
137
  async destroy(err, callback) {
119
138
  var _a;
@@ -184,10 +203,9 @@ class DummyBlob {
184
203
  throw new Error('Blob is not supported in this environment, if you are using an older version of Node v14, please upgrade to v14.17.0 or higher');
185
204
  }
186
205
  }
187
- const BaseBlob = buffer.Blob || DummyBlob;
188
206
  // Will be removed after v14 reaches EOL
189
207
  // Needed because v14 doesn't have .stream() implemented
190
- class PonyfillBlob extends BaseBlob {
208
+ class PonyfillBlob extends (buffer.Blob || DummyBlob) {
191
209
  stream() {
192
210
  return new PonyfillReadableStream({
193
211
  start: async (controller) => {
@@ -562,15 +580,6 @@ class PonyfillBody {
562
580
  const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength);
563
581
  return buffer.toString('utf-8');
564
582
  }
565
- // perf: avoid reading the stream twice (we read from Readable to create Blob and then we read from Blob to create a string)
566
- const _body = this.generateBody();
567
- if (_body) {
568
- const chunks = [];
569
- for await (const chunk of _body.readable) {
570
- chunks.push(chunk);
571
- }
572
- return Buffer.concat(chunks).toString('utf-8' /* use this.contentType soon */);
573
- }
574
583
  const blob = await this.blob();
575
584
  return blob.text();
576
585
  }
@@ -728,61 +737,19 @@ function isHeadersLike(headers) {
728
737
  }
729
738
  class PonyfillHeaders {
730
739
  constructor(headersInit) {
731
- this.headersInit = headersInit;
732
740
  this.map = new Map();
733
- this.mapIsBuilt = false;
734
- this.objectKeysOfeadersInit = [];
735
- }
736
- // perf: we don't need to build `this.map` for Requests, as we can access the headers directly
737
- _get(key) {
738
- // If the map is built, reuse it
739
- if (this.mapIsBuilt) {
740
- return this.map.get(key.toLowerCase()) || null;
741
- }
742
- // If the map is not built, try to get the value from the this.headersInit
743
- if (this.headersInit == null) {
744
- return null;
745
- }
746
- const normalized = key.toLowerCase();
747
- if (Array.isArray(this.headersInit)) {
748
- return this.headersInit.find(header => header[0] === normalized);
749
- }
750
- else if (isHeadersLike(this.headersInit)) {
751
- return this.headersInit.get(normalized);
752
- }
753
- else {
754
- const initValue = this.headersInit[key] || this.headersInit[normalized];
755
- if (initValue != null) {
756
- return initValue;
757
- }
758
- if (!this.objectKeysOfeadersInit.length) {
759
- this.objectKeysOfeadersInit = Object.keys(this.headersInit).map(k => k.toLowerCase());
741
+ if (headersInit != null) {
742
+ if (Array.isArray(headersInit)) {
743
+ this.map = new Map(headersInit);
760
744
  }
761
- const index = this.objectKeysOfeadersInit.indexOf(normalized);
762
- if (index === -1) {
763
- return null;
764
- }
765
- return this.headersInit[index];
766
- }
767
- }
768
- // perf: Build the map of headers lazily, only when we need to access all headers or write to it.
769
- // I could do a getter here, but I'm too lazy to type `getter`.
770
- getMap() {
771
- if (this.mapIsBuilt) {
772
- return this.map;
773
- }
774
- if (this.headersInit != null) {
775
- if (Array.isArray(this.headersInit)) {
776
- this.map = new Map(this.headersInit);
777
- }
778
- else if (isHeadersLike(this.headersInit)) {
779
- this.headersInit.forEach((value, key) => {
745
+ else if (isHeadersLike(headersInit)) {
746
+ headersInit.forEach((value, key) => {
780
747
  this.map.set(key, value);
781
748
  });
782
749
  }
783
750
  else {
784
- for (const initKey in this.headersInit) {
785
- const initValue = this.headersInit[initKey];
751
+ for (const initKey in headersInit) {
752
+ const initValue = headersInit[initKey];
786
753
  if (initValue != null) {
787
754
  const normalizedValue = Array.isArray(initValue) ? initValue.join(', ') : initValue;
788
755
  const normalizedKey = initKey.toLowerCase();
@@ -791,48 +758,39 @@ class PonyfillHeaders {
791
758
  }
792
759
  }
793
760
  }
794
- this.mapIsBuilt = true;
795
- return this.map;
796
761
  }
797
762
  append(name, value) {
798
763
  const key = name.toLowerCase();
799
- const existingValue = this.getMap().get(key);
764
+ const existingValue = this.map.get(key);
800
765
  const finalValue = existingValue ? `${existingValue}, ${value}` : value;
801
- this.getMap().set(key, finalValue);
766
+ this.map.set(key, finalValue);
802
767
  }
803
768
  get(name) {
804
769
  const key = name.toLowerCase();
805
- const value = this._get(key);
806
- if (value == null) {
807
- return null;
808
- }
809
- if (Array.isArray(value)) {
810
- return value.join(', ');
811
- }
812
- return value;
770
+ return this.map.get(key) || null;
813
771
  }
814
772
  has(name) {
815
773
  const key = name.toLowerCase();
816
- return !!this._get(key); // we might need to check if header exists and not just check if it's not nullable
774
+ return this.map.has(key);
817
775
  }
818
776
  set(name, value) {
819
777
  const key = name.toLowerCase();
820
- this.getMap().set(key, value);
778
+ this.map.set(key, value);
821
779
  }
822
780
  delete(name) {
823
781
  const key = name.toLowerCase();
824
- this.getMap().delete(key);
782
+ this.map.delete(key);
825
783
  }
826
784
  forEach(callback) {
827
- this.getMap().forEach((value, key) => {
785
+ this.map.forEach((value, key) => {
828
786
  callback(value, key, this);
829
787
  });
830
788
  }
831
789
  entries() {
832
- return this.getMap().entries();
790
+ return this.map.entries();
833
791
  }
834
792
  [Symbol.iterator]() {
835
- return this.getMap().entries();
793
+ return this.map.entries();
836
794
  }
837
795
  }
838
796
 
package/index.mjs CHANGED
@@ -27,10 +27,17 @@ class PonyfillAbortError extends Error {
27
27
  function createController(desiredSize, readable) {
28
28
  let chunks = [];
29
29
  let _closed = false;
30
+ let flushed = false;
30
31
  return {
31
32
  desiredSize,
32
33
  enqueue(chunk) {
33
- chunks.push(Buffer.from(chunk));
34
+ const buf = typeof chunk === 'string' ? Buffer.from(chunk) : chunk;
35
+ if (!flushed) {
36
+ chunks.push(buf);
37
+ }
38
+ else {
39
+ readable.push(buf);
40
+ }
34
41
  },
35
42
  close() {
36
43
  if (chunks.length > 0) {
@@ -49,6 +56,7 @@ function createController(desiredSize, readable) {
49
56
  return _closed;
50
57
  },
51
58
  _flush() {
59
+ flushed = true;
52
60
  if (chunks.length > 0) {
53
61
  const concatenated = Buffer.concat(chunks);
54
62
  readable.push(concatenated);
@@ -96,18 +104,29 @@ class PonyfillReadableStream {
96
104
  }
97
105
  else {
98
106
  let started = false;
107
+ let ongoing = false;
99
108
  this.readable = new Readable({
100
- async read(desiredSize) {
101
- var _a, _b;
102
- const controller = createController(desiredSize, this);
103
- if (!started) {
104
- started = true;
105
- await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
109
+ read(desiredSize) {
110
+ if (ongoing) {
111
+ return;
106
112
  }
107
- if (!controller._closed) {
113
+ ongoing = true;
114
+ return Promise.resolve().then(async () => {
115
+ var _a, _b;
116
+ if (!started) {
117
+ const controller = createController(desiredSize, this);
118
+ started = true;
119
+ await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
120
+ controller._flush();
121
+ if (controller._closed) {
122
+ return;
123
+ }
124
+ }
125
+ const controller = createController(desiredSize, this);
108
126
  await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
109
- }
110
- controller._flush();
127
+ controller._flush();
128
+ ongoing = false;
129
+ });
111
130
  },
112
131
  async destroy(err, callback) {
113
132
  var _a;
@@ -178,10 +197,9 @@ class DummyBlob {
178
197
  throw new Error('Blob is not supported in this environment, if you are using an older version of Node v14, please upgrade to v14.17.0 or higher');
179
198
  }
180
199
  }
181
- const BaseBlob = Blob || DummyBlob;
182
200
  // Will be removed after v14 reaches EOL
183
201
  // Needed because v14 doesn't have .stream() implemented
184
- class PonyfillBlob extends BaseBlob {
202
+ class PonyfillBlob extends (Blob || DummyBlob) {
185
203
  stream() {
186
204
  return new PonyfillReadableStream({
187
205
  start: async (controller) => {
@@ -556,15 +574,6 @@ class PonyfillBody {
556
574
  const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength);
557
575
  return buffer.toString('utf-8');
558
576
  }
559
- // perf: avoid reading the stream twice (we read from Readable to create Blob and then we read from Blob to create a string)
560
- const _body = this.generateBody();
561
- if (_body) {
562
- const chunks = [];
563
- for await (const chunk of _body.readable) {
564
- chunks.push(chunk);
565
- }
566
- return Buffer.concat(chunks).toString('utf-8' /* use this.contentType soon */);
567
- }
568
577
  const blob = await this.blob();
569
578
  return blob.text();
570
579
  }
@@ -722,61 +731,19 @@ function isHeadersLike(headers) {
722
731
  }
723
732
  class PonyfillHeaders {
724
733
  constructor(headersInit) {
725
- this.headersInit = headersInit;
726
734
  this.map = new Map();
727
- this.mapIsBuilt = false;
728
- this.objectKeysOfeadersInit = [];
729
- }
730
- // perf: we don't need to build `this.map` for Requests, as we can access the headers directly
731
- _get(key) {
732
- // If the map is built, reuse it
733
- if (this.mapIsBuilt) {
734
- return this.map.get(key.toLowerCase()) || null;
735
- }
736
- // If the map is not built, try to get the value from the this.headersInit
737
- if (this.headersInit == null) {
738
- return null;
739
- }
740
- const normalized = key.toLowerCase();
741
- if (Array.isArray(this.headersInit)) {
742
- return this.headersInit.find(header => header[0] === normalized);
743
- }
744
- else if (isHeadersLike(this.headersInit)) {
745
- return this.headersInit.get(normalized);
746
- }
747
- else {
748
- const initValue = this.headersInit[key] || this.headersInit[normalized];
749
- if (initValue != null) {
750
- return initValue;
751
- }
752
- if (!this.objectKeysOfeadersInit.length) {
753
- this.objectKeysOfeadersInit = Object.keys(this.headersInit).map(k => k.toLowerCase());
735
+ if (headersInit != null) {
736
+ if (Array.isArray(headersInit)) {
737
+ this.map = new Map(headersInit);
754
738
  }
755
- const index = this.objectKeysOfeadersInit.indexOf(normalized);
756
- if (index === -1) {
757
- return null;
758
- }
759
- return this.headersInit[index];
760
- }
761
- }
762
- // perf: Build the map of headers lazily, only when we need to access all headers or write to it.
763
- // I could do a getter here, but I'm too lazy to type `getter`.
764
- getMap() {
765
- if (this.mapIsBuilt) {
766
- return this.map;
767
- }
768
- if (this.headersInit != null) {
769
- if (Array.isArray(this.headersInit)) {
770
- this.map = new Map(this.headersInit);
771
- }
772
- else if (isHeadersLike(this.headersInit)) {
773
- this.headersInit.forEach((value, key) => {
739
+ else if (isHeadersLike(headersInit)) {
740
+ headersInit.forEach((value, key) => {
774
741
  this.map.set(key, value);
775
742
  });
776
743
  }
777
744
  else {
778
- for (const initKey in this.headersInit) {
779
- const initValue = this.headersInit[initKey];
745
+ for (const initKey in headersInit) {
746
+ const initValue = headersInit[initKey];
780
747
  if (initValue != null) {
781
748
  const normalizedValue = Array.isArray(initValue) ? initValue.join(', ') : initValue;
782
749
  const normalizedKey = initKey.toLowerCase();
@@ -785,48 +752,39 @@ class PonyfillHeaders {
785
752
  }
786
753
  }
787
754
  }
788
- this.mapIsBuilt = true;
789
- return this.map;
790
755
  }
791
756
  append(name, value) {
792
757
  const key = name.toLowerCase();
793
- const existingValue = this.getMap().get(key);
758
+ const existingValue = this.map.get(key);
794
759
  const finalValue = existingValue ? `${existingValue}, ${value}` : value;
795
- this.getMap().set(key, finalValue);
760
+ this.map.set(key, finalValue);
796
761
  }
797
762
  get(name) {
798
763
  const key = name.toLowerCase();
799
- const value = this._get(key);
800
- if (value == null) {
801
- return null;
802
- }
803
- if (Array.isArray(value)) {
804
- return value.join(', ');
805
- }
806
- return value;
764
+ return this.map.get(key) || null;
807
765
  }
808
766
  has(name) {
809
767
  const key = name.toLowerCase();
810
- return !!this._get(key); // we might need to check if header exists and not just check if it's not nullable
768
+ return this.map.has(key);
811
769
  }
812
770
  set(name, value) {
813
771
  const key = name.toLowerCase();
814
- this.getMap().set(key, value);
772
+ this.map.set(key, value);
815
773
  }
816
774
  delete(name) {
817
775
  const key = name.toLowerCase();
818
- this.getMap().delete(key);
776
+ this.map.delete(key);
819
777
  }
820
778
  forEach(callback) {
821
- this.getMap().forEach((value, key) => {
779
+ this.map.forEach((value, key) => {
822
780
  callback(value, key, this);
823
781
  });
824
782
  }
825
783
  entries() {
826
- return this.getMap().entries();
784
+ return this.map.entries();
827
785
  }
828
786
  [Symbol.iterator]() {
829
- return this.getMap().entries();
787
+ return this.map.entries();
830
788
  }
831
789
  }
832
790
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.0.7-alpha-20230208150005-0bd8aaf",
3
+ "version": "0.0.7-alpha-20230209144328-8bdce99",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {