@whatwg-node/fetch 0.2.9-alpha-20220809151343-3774b4b → 0.3.1

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,10 +1,24 @@
1
1
  # @whatwg-node/fetch
2
2
 
3
- ## 0.2.9-alpha-20220809151343-3774b4b
3
+ ## 0.3.1
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - [#75](https://github.com/ardatan/whatwg-node/pull/75) [`68074e8`](https://github.com/ardatan/whatwg-node/commit/68074e87d7607586d61d8e1d1cbc87f058c4f0e0) Thanks [@saihaj](https://github.com/saihaj)! - temp
7
+ - [`a3bc171`](https://github.com/ardatan/whatwg-node/commit/a3bc17120fbdf641e4363d08ba79955005f5b3d6) Thanks [@ardatan](https://github.com/ardatan)! - fix btoa ponyfill
8
+
9
+ ## 0.3.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [`8a431d3`](https://github.com/ardatan/whatwg-node/commit/8a431d309271c0d1ff7248ec26afe293ccc01bf6) Thanks [@ardatan](https://github.com/ardatan)! - Add "btoa" ponyfill for Node 14
14
+
15
+ * [`8a431d3`](https://github.com/ardatan/whatwg-node/commit/8a431d309271c0d1ff7248ec26afe293ccc01bf6) Thanks [@ardatan](https://github.com/ardatan)! - Support different encodings in TextEncoder and TextDecoder
16
+
17
+ ## 0.2.9
18
+
19
+ ### Patch Changes
20
+
21
+ - [`9a8d873`](https://github.com/ardatan/whatwg-node/commit/9a8d8731ff07ea585b1e561718584fbe5edeb963) Thanks [@ardatan](https://github.com/ardatan)! - Workaround for a potential leak on Node 18
8
22
 
9
23
  ## 0.2.3
10
24
 
@@ -23,7 +23,7 @@ module.exports = function createNodePonyfill(opts = {}) {
23
23
  if (!globalThis.Event || !globalThis.EventTarget) {
24
24
  require('event-target-polyfill');
25
25
  }
26
-
26
+
27
27
  ponyfills.Event = globalThis.Event;
28
28
  ponyfills.EventTarget = globalThis.EventTarget;
29
29
 
@@ -58,19 +58,22 @@ module.exports = function createNodePonyfill(opts = {}) {
58
58
  }
59
59
  }
60
60
 
61
- ponyfills.TextEncoder = function TextEncoder() {
61
+ ponyfills.btoa = globalThis.btoa
62
+ if (!ponyfills.btoa) {
63
+ ponyfills.btoa = function btoa(data) {
64
+ return Buffer.from(data).toString('base64');
65
+ };
66
+ }
67
+
68
+ ponyfills.TextEncoder = function TextEncoder(encoding = 'utf-8') {
62
69
  return {
63
70
  encode(str) {
64
- return Buffer.from(str, "utf8");
71
+ return Buffer.from(str, encoding);
65
72
  }
66
73
  }
67
74
  }
68
75
 
69
- ponyfills.TextDecoder = function TextDecoder(label, opts) {
70
- let encoding = 'utf-8'
71
- if (opts && opts.encoding) {
72
- encoding = opts.encoding
73
- }
76
+ ponyfills.TextDecoder = function TextDecoder(encoding = 'utf-8') {
74
77
  return {
75
78
  decode(buf) {
76
79
  return Buffer.from(buf).toString(encoding);
@@ -198,7 +201,8 @@ module.exports = function createNodePonyfill(opts = {}) {
198
201
 
199
202
  const fetch = function (requestOrUrl, options) {
200
203
  if (typeof requestOrUrl === "string") {
201
- return fetch(new Request(requestOrUrl, options));
204
+ // We cannot use our ctor because it leaks on Node 18's global fetch
205
+ return originalFetch(requestOrUrl, options);
202
206
  }
203
207
  if (requestOrUrl.url.startsWith('file:')) {
204
208
  return handleFileRequest(requestOrUrl.url, ponyfills.Response);
@@ -10,6 +10,7 @@ const TransformStream = globalThis.TransformStream;
10
10
  const Blob = globalThis.Blob;
11
11
  const File = globalThis.File;
12
12
  const crypto = globalThis.crypto;
13
+ const btoa = globalThis.btoa;
13
14
  const TextDecoder = globalThis.TextDecoder;
14
15
  const TextEncoder = globalThis.TextEncoder;
15
16
  const Event = globalThis.Event;
@@ -29,6 +30,7 @@ export {
29
30
  Blob,
30
31
  File,
31
32
  crypto,
33
+ btoa,
32
34
  TextDecoder,
33
35
  TextEncoder,
34
36
  Event,
@@ -10,6 +10,7 @@ module.exports.TransformStream = globalThis.TransformStream;
10
10
  module.exports.Blob = globalThis.Blob;
11
11
  module.exports.File = globalThis.File;
12
12
  module.exports.crypto = globalThis.crypto;
13
+ module.exports.btoa = globalThis.btoa;
13
14
  module.exports.TextEncoder = globalThis.TextEncoder;
14
15
  module.exports.TextDecoder = globalThis.TextDecoder;
15
16
  module.exports.Event = globalThis.Event;
package/dist/index.d.ts CHANGED
@@ -12,6 +12,7 @@ declare const _TransformStream: typeof TransformStream;
12
12
  declare const _Blob: typeof Blob;
13
13
  declare const _File: typeof File;
14
14
  declare const _crypto: typeof crypto;
15
+ declare const _btoa: typeof btoa;
15
16
  declare const _TextEncoder: typeof TextEncoder;
16
17
  declare const _TextDecoder: typeof TextDecoder;
17
18
  declare const _Event: typeof Event;
@@ -30,6 +31,7 @@ declare module "@whatwg-node/fetch" {
30
31
  export const Blob: typeof _Blob;
31
32
  export const File: typeof _File;
32
33
  export const crypto: typeof _crypto;
34
+ export const btoa: typeof _btoa;
33
35
  export const TextDecoder: typeof _TextDecoder;
34
36
  export const TextEncoder: typeof _TextEncoder;
35
37
  export const Event: typeof _Event;
@@ -62,7 +64,8 @@ declare module "@whatwg-node/fetch" {
62
64
  TransformStream: typeof _TransformStream,
63
65
  Blob: typeof _Blob,
64
66
  File: typeof _File,
65
- crypto: typeof _crypto
67
+ crypto: typeof _crypto,
68
+ btoa: typeof _btoa,
66
69
  TextEncoder: typeof _TextEncoder,
67
70
  TextDecoder: typeof _TextDecoder,
68
71
  Event: typeof _Event,
@@ -14,6 +14,7 @@ module.exports.TransformStream = ponyfills.TransformStream;
14
14
  module.exports.Blob = ponyfills.Blob;
15
15
  module.exports.File = ponyfills.File;
16
16
  module.exports.crypto = ponyfills.crypto;
17
+ module.exports.btoa = ponyfills.btoa;
17
18
  module.exports.TextEncoder = ponyfills.TextEncoder;
18
19
  module.exports.TextDecoder = ponyfills.TextDecoder;
19
20
  module.exports.Event = ponyfills.Event;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/fetch",
3
- "version": "0.2.9-alpha-20220809151343-3774b4b",
3
+ "version": "0.3.1",
4
4
  "description": "Cross Platform Smart Fetch Ponyfill",
5
5
  "author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
6
6
  "repository": {
@@ -0,0 +1,4 @@
1
+ import { btoa } from '@whatwg-node/fetch';
2
+ it('should work as expected', () => {
3
+ expect(btoa('Hello, world!')).toBe('SGVsbG8sIHdvcmxkIQ==');
4
+ })