@whatwg-node/node-fetch 0.7.5 → 0.8.0-alpha-20241212151314-570907b87da5f77c7c6a2d7476cd10a1bdd2b3dc

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.
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PonyfillAbortController = void 0;
4
+ const disposablestack_1 = require("@whatwg-node/disposablestack");
5
+ const AbortSignal_js_1 = require("./AbortSignal.js");
6
+ class PonyfillAbortController {
7
+ signal;
8
+ constructor() {
9
+ this.signal = new AbortSignal_js_1.PonyfillAbortSignal();
10
+ }
11
+ abort(reason) {
12
+ return this.signal.sendAbort(reason);
13
+ }
14
+ [disposablestack_1.DisposableSymbols.dispose]() {
15
+ return this.abort();
16
+ }
17
+ }
18
+ exports.PonyfillAbortController = PonyfillAbortController;
package/cjs/AbortError.js CHANGED
@@ -1,16 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PonyfillAbortError = void 0;
4
- class PonyfillAbortError extends Error {
4
+ class PonyfillAbortError extends DOMException {
5
5
  constructor(reason) {
6
6
  let message = 'The operation was aborted';
7
7
  if (reason) {
8
8
  message += ` reason: ${reason}`;
9
9
  }
10
- super(message, {
11
- cause: reason,
12
- });
13
- this.name = 'AbortError';
10
+ super(message, 'AbortError');
11
+ this.cause = reason;
14
12
  }
15
13
  get reason() {
16
14
  return this.cause;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PonyfillAbortSignal = void 0;
4
+ const disposablestack_1 = require("@whatwg-node/disposablestack");
5
+ const AbortError_js_1 = require("./AbortError.js");
6
+ class PonyfillAbortSignal extends EventTarget {
7
+ aborted = false;
8
+ _onabort = null;
9
+ reason;
10
+ constructor(...signals) {
11
+ super();
12
+ if (signals.length) {
13
+ return this.any(signals);
14
+ }
15
+ }
16
+ throwIfAborted() {
17
+ if (this.aborted) {
18
+ throw this.reason;
19
+ }
20
+ }
21
+ sendAbort(reason) {
22
+ if (!this.aborted) {
23
+ this.reason = reason || new AbortError_js_1.PonyfillAbortError();
24
+ this.aborted = true;
25
+ const event = new Event('abort');
26
+ this.dispatchEvent(event);
27
+ }
28
+ }
29
+ get onabort() {
30
+ return this._onabort;
31
+ }
32
+ set onabort(value) {
33
+ this._onabort = value;
34
+ if (value) {
35
+ this.addEventListener('abort', value);
36
+ }
37
+ else {
38
+ this.removeEventListener('abort', value);
39
+ }
40
+ }
41
+ any(signals) {
42
+ function onAbort(ev) {
43
+ const signal = ev.target || this;
44
+ this.sendAbort(signal.reason);
45
+ for (const signal of signals) {
46
+ signal.removeEventListener('abort', onAbort);
47
+ signal.reason = this.reason;
48
+ if (signal.sendAbort) {
49
+ signal.sendAbort(this.reason);
50
+ }
51
+ signal.aborted = true;
52
+ }
53
+ }
54
+ for (const signal of signals) {
55
+ signal.addEventListener('abort', onAbort, { once: true });
56
+ }
57
+ return this;
58
+ }
59
+ static timeout(ms) {
60
+ const signal = new PonyfillAbortSignal();
61
+ const timeout = setTimeout(() => {
62
+ signal.sendAbort();
63
+ signal.removeEventListener('abort', onAbort);
64
+ }, ms);
65
+ function onAbort() {
66
+ clearTimeout(timeout);
67
+ signal.removeEventListener('abort', onAbort);
68
+ }
69
+ signal.addEventListener('abort', onAbort, { once: true });
70
+ return signal;
71
+ }
72
+ static any(signals) {
73
+ return new PonyfillAbortSignal(...signals);
74
+ }
75
+ [disposablestack_1.DisposableSymbols.dispose]() {
76
+ return this.sendAbort();
77
+ }
78
+ }
79
+ exports.PonyfillAbortSignal = PonyfillAbortSignal;
package/cjs/Request.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PonyfillRequest = void 0;
4
4
  const http_1 = require("http");
5
5
  const https_1 = require("https");
6
+ const AbortController_js_1 = require("./AbortController.js");
6
7
  const Body_js_1 = require("./Body.js");
7
8
  const Headers_js_1 = require("./Headers.js");
8
9
  const URL_js_1 = require("./URL.js");
@@ -122,7 +123,7 @@ class PonyfillRequest extends Body_js_1.PonyfillBody {
122
123
  // Create a new signal only if needed
123
124
  // Because the creation of signal is expensive
124
125
  if (!this._signal) {
125
- this._signal = new AbortController().signal;
126
+ this._signal = new AbortController_js_1.PonyfillAbortController().signal;
126
127
  }
127
128
  return this._signal;
128
129
  }
package/cjs/fetchCurl.js CHANGED
@@ -112,6 +112,23 @@ function fetchCurl(fetchRequest) {
112
112
  }
113
113
  })
114
114
  .catch(deferredPromise.reject);
115
+ if (fetchRequest['_signal']) {
116
+ outputStream.once('error', () => {
117
+ if (!fetchRequest['_signal']?.aborted) {
118
+ fetchRequest['_signal']?.sendAbort?.();
119
+ }
120
+ });
121
+ outputStream.once('close', () => {
122
+ if (!fetchRequest['_signal']?.aborted) {
123
+ fetchRequest['_signal']?.sendAbort?.();
124
+ }
125
+ });
126
+ outputStream.once('destroy', () => {
127
+ if (!fetchRequest['_signal']?.aborted) {
128
+ fetchRequest['_signal']?.sendAbort?.();
129
+ }
130
+ });
131
+ }
115
132
  const headersFlat = headersBuf
116
133
  .toString('utf8')
117
134
  .split(/\r?\n|\r/g)
@@ -108,6 +108,23 @@ function fetchNodeHttp(fetchRequest) {
108
108
  }
109
109
  })
110
110
  .catch(reject);
111
+ if (fetchRequest['_signal']) {
112
+ outputStream.once('error', () => {
113
+ if (!fetchRequest['_signal']?.aborted) {
114
+ fetchRequest['_signal']?.sendAbort?.();
115
+ }
116
+ });
117
+ outputStream.once('close', () => {
118
+ if (!fetchRequest['_signal']?.aborted) {
119
+ fetchRequest['_signal']?.sendAbort?.();
120
+ }
121
+ });
122
+ outputStream.once('destroy', () => {
123
+ if (!fetchRequest['_signal']?.aborted) {
124
+ fetchRequest['_signal']?.sendAbort?.();
125
+ }
126
+ });
127
+ }
111
128
  const ponyfillResponse = new Response_js_1.PonyfillResponse(outputStream, {
112
129
  status: nodeResponse.statusCode,
113
130
  statusText: nodeResponse.statusMessage,
package/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TextEncoderStream = exports.TextDecoderStream = exports.IteratorObject = exports.DecompressionStream = exports.CompressionStream = exports.TransformStream = exports.WritableStream = exports.URLSearchParams = exports.URL = exports.btoa = exports.TextDecoder = exports.TextEncoder = exports.Blob = exports.FormData = exports.File = exports.ReadableStream = exports.Response = exports.Request = exports.Body = exports.Headers = exports.fetch = void 0;
3
+ exports.AbortError = exports.AbortSignal = exports.AbortController = exports.TextEncoderStream = exports.TextDecoderStream = exports.IteratorObject = exports.DecompressionStream = exports.CompressionStream = exports.TransformStream = exports.WritableStream = exports.URLSearchParams = exports.URL = exports.btoa = exports.TextDecoder = exports.TextEncoder = exports.Blob = exports.FormData = exports.File = exports.ReadableStream = exports.Response = exports.Request = exports.Body = exports.Headers = exports.fetch = void 0;
4
4
  var fetch_js_1 = require("./fetch.js");
5
5
  Object.defineProperty(exports, "fetch", { enumerable: true, get: function () { return fetch_js_1.fetchPonyfill; } });
6
6
  var Headers_js_1 = require("./Headers.js");
@@ -40,3 +40,9 @@ Object.defineProperty(exports, "IteratorObject", { enumerable: true, get: functi
40
40
  var TextEncoderDecoderStream_js_1 = require("./TextEncoderDecoderStream.js");
41
41
  Object.defineProperty(exports, "TextDecoderStream", { enumerable: true, get: function () { return TextEncoderDecoderStream_js_1.PonyfillTextDecoderStream; } });
42
42
  Object.defineProperty(exports, "TextEncoderStream", { enumerable: true, get: function () { return TextEncoderDecoderStream_js_1.PonyfillTextEncoderStream; } });
43
+ var AbortController_js_1 = require("./AbortController.js");
44
+ Object.defineProperty(exports, "AbortController", { enumerable: true, get: function () { return AbortController_js_1.PonyfillAbortController; } });
45
+ var AbortSignal_js_1 = require("./AbortSignal.js");
46
+ Object.defineProperty(exports, "AbortSignal", { enumerable: true, get: function () { return AbortSignal_js_1.PonyfillAbortSignal; } });
47
+ var AbortError_js_1 = require("./AbortError.js");
48
+ Object.defineProperty(exports, "AbortError", { enumerable: true, get: function () { return AbortError_js_1.PonyfillAbortError; } });
@@ -0,0 +1,14 @@
1
+ import { DisposableSymbols } from '@whatwg-node/disposablestack';
2
+ import { PonyfillAbortSignal } from './AbortSignal.js';
3
+ export class PonyfillAbortController {
4
+ signal;
5
+ constructor() {
6
+ this.signal = new PonyfillAbortSignal();
7
+ }
8
+ abort(reason) {
9
+ return this.signal.sendAbort(reason);
10
+ }
11
+ [DisposableSymbols.dispose]() {
12
+ return this.abort();
13
+ }
14
+ }
package/esm/AbortError.js CHANGED
@@ -1,13 +1,11 @@
1
- export class PonyfillAbortError extends Error {
1
+ export class PonyfillAbortError extends DOMException {
2
2
  constructor(reason) {
3
3
  let message = 'The operation was aborted';
4
4
  if (reason) {
5
5
  message += ` reason: ${reason}`;
6
6
  }
7
- super(message, {
8
- cause: reason,
9
- });
10
- this.name = 'AbortError';
7
+ super(message, 'AbortError');
8
+ this.cause = reason;
11
9
  }
12
10
  get reason() {
13
11
  return this.cause;
@@ -0,0 +1,75 @@
1
+ import { DisposableSymbols } from '@whatwg-node/disposablestack';
2
+ import { PonyfillAbortError } from './AbortError.js';
3
+ export class PonyfillAbortSignal extends EventTarget {
4
+ aborted = false;
5
+ _onabort = null;
6
+ reason;
7
+ constructor(...signals) {
8
+ super();
9
+ if (signals.length) {
10
+ return this.any(signals);
11
+ }
12
+ }
13
+ throwIfAborted() {
14
+ if (this.aborted) {
15
+ throw this.reason;
16
+ }
17
+ }
18
+ sendAbort(reason) {
19
+ if (!this.aborted) {
20
+ this.reason = reason || new PonyfillAbortError();
21
+ this.aborted = true;
22
+ const event = new Event('abort');
23
+ this.dispatchEvent(event);
24
+ }
25
+ }
26
+ get onabort() {
27
+ return this._onabort;
28
+ }
29
+ set onabort(value) {
30
+ this._onabort = value;
31
+ if (value) {
32
+ this.addEventListener('abort', value);
33
+ }
34
+ else {
35
+ this.removeEventListener('abort', value);
36
+ }
37
+ }
38
+ any(signals) {
39
+ function onAbort(ev) {
40
+ const signal = ev.target || this;
41
+ this.sendAbort(signal.reason);
42
+ for (const signal of signals) {
43
+ signal.removeEventListener('abort', onAbort);
44
+ signal.reason = this.reason;
45
+ if (signal.sendAbort) {
46
+ signal.sendAbort(this.reason);
47
+ }
48
+ signal.aborted = true;
49
+ }
50
+ }
51
+ for (const signal of signals) {
52
+ signal.addEventListener('abort', onAbort, { once: true });
53
+ }
54
+ return this;
55
+ }
56
+ static timeout(ms) {
57
+ const signal = new PonyfillAbortSignal();
58
+ const timeout = setTimeout(() => {
59
+ signal.sendAbort();
60
+ signal.removeEventListener('abort', onAbort);
61
+ }, ms);
62
+ function onAbort() {
63
+ clearTimeout(timeout);
64
+ signal.removeEventListener('abort', onAbort);
65
+ }
66
+ signal.addEventListener('abort', onAbort, { once: true });
67
+ return signal;
68
+ }
69
+ static any(signals) {
70
+ return new PonyfillAbortSignal(...signals);
71
+ }
72
+ [DisposableSymbols.dispose]() {
73
+ return this.sendAbort();
74
+ }
75
+ }
package/esm/Request.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Agent as HTTPAgent } from 'http';
2
2
  import { Agent as HTTPSAgent } from 'https';
3
+ import { PonyfillAbortController } from './AbortController.js';
3
4
  import { PonyfillBody } from './Body.js';
4
5
  import { isHeadersLike, PonyfillHeaders } from './Headers.js';
5
6
  import { PonyfillURL } from './URL.js';
@@ -119,7 +120,7 @@ export class PonyfillRequest extends PonyfillBody {
119
120
  // Create a new signal only if needed
120
121
  // Because the creation of signal is expensive
121
122
  if (!this._signal) {
122
- this._signal = new AbortController().signal;
123
+ this._signal = new PonyfillAbortController().signal;
123
124
  }
124
125
  return this._signal;
125
126
  }
package/esm/fetchCurl.js CHANGED
@@ -109,6 +109,23 @@ export function fetchCurl(fetchRequest) {
109
109
  }
110
110
  })
111
111
  .catch(deferredPromise.reject);
112
+ if (fetchRequest['_signal']) {
113
+ outputStream.once('error', () => {
114
+ if (!fetchRequest['_signal']?.aborted) {
115
+ fetchRequest['_signal']?.sendAbort?.();
116
+ }
117
+ });
118
+ outputStream.once('close', () => {
119
+ if (!fetchRequest['_signal']?.aborted) {
120
+ fetchRequest['_signal']?.sendAbort?.();
121
+ }
122
+ });
123
+ outputStream.once('destroy', () => {
124
+ if (!fetchRequest['_signal']?.aborted) {
125
+ fetchRequest['_signal']?.sendAbort?.();
126
+ }
127
+ });
128
+ }
112
129
  const headersFlat = headersBuf
113
130
  .toString('utf8')
114
131
  .split(/\r?\n|\r/g)
@@ -105,6 +105,23 @@ export function fetchNodeHttp(fetchRequest) {
105
105
  }
106
106
  })
107
107
  .catch(reject);
108
+ if (fetchRequest['_signal']) {
109
+ outputStream.once('error', () => {
110
+ if (!fetchRequest['_signal']?.aborted) {
111
+ fetchRequest['_signal']?.sendAbort?.();
112
+ }
113
+ });
114
+ outputStream.once('close', () => {
115
+ if (!fetchRequest['_signal']?.aborted) {
116
+ fetchRequest['_signal']?.sendAbort?.();
117
+ }
118
+ });
119
+ outputStream.once('destroy', () => {
120
+ if (!fetchRequest['_signal']?.aborted) {
121
+ fetchRequest['_signal']?.sendAbort?.();
122
+ }
123
+ });
124
+ }
108
125
  const ponyfillResponse = new PonyfillResponse(outputStream, {
109
126
  status: nodeResponse.statusCode,
110
127
  statusText: nodeResponse.statusMessage,
package/esm/index.js CHANGED
@@ -16,3 +16,6 @@ export { PonyfillCompressionStream as CompressionStream } from './CompressionStr
16
16
  export { PonyfillDecompressionStream as DecompressionStream } from './DecompressionStream.js';
17
17
  export { PonyfillIteratorObject as IteratorObject } from './IteratorObject.js';
18
18
  export { PonyfillTextDecoderStream as TextDecoderStream, PonyfillTextEncoderStream as TextEncoderStream, } from './TextEncoderDecoderStream.js';
19
+ export { PonyfillAbortController as AbortController } from './AbortController.js';
20
+ export { PonyfillAbortSignal as AbortSignal } from './AbortSignal.js';
21
+ export { PonyfillAbortError as AbortError } from './AbortError.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.7.5",
3
+ "version": "0.8.0-alpha-20241212151314-570907b87da5f77c7c6a2d7476cd10a1bdd2b3dc",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -0,0 +1,8 @@
1
+ import { DisposableSymbols } from '@whatwg-node/disposablestack';
2
+ import { PonyfillAbortSignal } from './AbortSignal.cjs';
3
+ export declare class PonyfillAbortController implements AbortController {
4
+ signal: PonyfillAbortSignal;
5
+ constructor();
6
+ abort(reason?: any): void;
7
+ [DisposableSymbols.dispose](): void;
8
+ }
@@ -0,0 +1,8 @@
1
+ import { DisposableSymbols } from '@whatwg-node/disposablestack';
2
+ import { PonyfillAbortSignal } from './AbortSignal.js';
3
+ export declare class PonyfillAbortController implements AbortController {
4
+ signal: PonyfillAbortSignal;
5
+ constructor();
6
+ abort(reason?: any): void;
7
+ [DisposableSymbols.dispose](): void;
8
+ }
@@ -1,4 +1,4 @@
1
- export declare class PonyfillAbortError extends Error {
1
+ export declare class PonyfillAbortError extends DOMException {
2
2
  constructor(reason?: any);
3
3
  get reason(): unknown;
4
4
  }
@@ -1,4 +1,4 @@
1
- export declare class PonyfillAbortError extends Error {
1
+ export declare class PonyfillAbortError extends DOMException {
2
2
  constructor(reason?: any);
3
3
  get reason(): unknown;
4
4
  }
@@ -0,0 +1,15 @@
1
+ import { DisposableSymbols } from '@whatwg-node/disposablestack';
2
+ export declare class PonyfillAbortSignal extends EventTarget implements AbortSignal {
3
+ aborted: boolean;
4
+ private _onabort;
5
+ reason: any;
6
+ constructor(...signals: PonyfillAbortSignal[]);
7
+ throwIfAborted(): void;
8
+ sendAbort(reason?: any): void;
9
+ get onabort(): ((this: AbortSignal, ev: Event) => any) | null;
10
+ set onabort(value: ((this: AbortSignal, ev: Event) => any) | null);
11
+ any(signals: Iterable<PonyfillAbortSignal>): PonyfillAbortSignal;
12
+ static timeout(ms: number): AbortSignal;
13
+ static any(signals: Iterable<PonyfillAbortSignal>): PonyfillAbortSignal;
14
+ [DisposableSymbols.dispose](): void;
15
+ }
@@ -0,0 +1,15 @@
1
+ import { DisposableSymbols } from '@whatwg-node/disposablestack';
2
+ export declare class PonyfillAbortSignal extends EventTarget implements AbortSignal {
3
+ aborted: boolean;
4
+ private _onabort;
5
+ reason: any;
6
+ constructor(...signals: PonyfillAbortSignal[]);
7
+ throwIfAborted(): void;
8
+ sendAbort(reason?: any): void;
9
+ get onabort(): ((this: AbortSignal, ev: Event) => any) | null;
10
+ set onabort(value: ((this: AbortSignal, ev: Event) => any) | null);
11
+ any(signals: Iterable<PonyfillAbortSignal>): PonyfillAbortSignal;
12
+ static timeout(ms: number): AbortSignal;
13
+ static any(signals: Iterable<PonyfillAbortSignal>): PonyfillAbortSignal;
14
+ [DisposableSymbols.dispose](): void;
15
+ }
@@ -16,3 +16,6 @@ export { PonyfillCompressionStream as CompressionStream } from './CompressionStr
16
16
  export { PonyfillDecompressionStream as DecompressionStream } from './DecompressionStream.cjs';
17
17
  export { PonyfillIteratorObject as IteratorObject } from './IteratorObject.cjs';
18
18
  export { PonyfillTextDecoderStream as TextDecoderStream, PonyfillTextEncoderStream as TextEncoderStream, } from './TextEncoderDecoderStream.cjs';
19
+ export { PonyfillAbortController as AbortController } from './AbortController.cjs';
20
+ export { PonyfillAbortSignal as AbortSignal } from './AbortSignal.cjs';
21
+ export { PonyfillAbortError as AbortError } from './AbortError.cjs';
@@ -16,3 +16,6 @@ export { PonyfillCompressionStream as CompressionStream } from './CompressionStr
16
16
  export { PonyfillDecompressionStream as DecompressionStream } from './DecompressionStream.js';
17
17
  export { PonyfillIteratorObject as IteratorObject } from './IteratorObject.js';
18
18
  export { PonyfillTextDecoderStream as TextDecoderStream, PonyfillTextEncoderStream as TextEncoderStream, } from './TextEncoderDecoderStream.js';
19
+ export { PonyfillAbortController as AbortController } from './AbortController.js';
20
+ export { PonyfillAbortSignal as AbortSignal } from './AbortSignal.js';
21
+ export { PonyfillAbortError as AbortError } from './AbortError.js';