@comunica/actor-http-fetch 2.2.1 → 2.2.2-alpha.13.0

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.
@@ -47,22 +47,58 @@ class ActorHttpFetch extends bus_http_1.ActorHttp {
47
47
  if (action.init?.headers && 'append' in action.init.headers && action.context.has(context_entries_1.KeysHttp.fetch)) {
48
48
  action.init.headers = bus_http_1.ActorHttp.headersToHash(action.init.headers);
49
49
  }
50
- // Perform request
51
- const customFetch = action
52
- .context?.get(context_entries_1.KeysHttp.fetch);
53
- return await (customFetch || fetch)(action.input, await this.fetchInitPreprocessor.handle({
54
- ...action.init,
55
- ...action.context.get(context_entries_1.KeysHttp.includeCredentials) ? { credentials: 'include' } : {},
56
- })).then(response => {
50
+ let requestInit = { ...action.init };
51
+ if (action.context.get(context_entries_1.KeysHttp.includeCredentials)) {
52
+ requestInit.credentials = 'include';
53
+ }
54
+ const httpTimeout = action.context?.get(context_entries_1.KeysHttp.httpTimeout);
55
+ let requestTimeout;
56
+ let onTimeout;
57
+ if (httpTimeout !== undefined) {
58
+ const controller = await this.fetchInitPreprocessor.createAbortController();
59
+ requestInit.signal = controller.signal;
60
+ onTimeout = () => controller.abort();
61
+ requestTimeout = setTimeout(() => onTimeout(), httpTimeout);
62
+ }
63
+ try {
64
+ requestInit = await this.fetchInitPreprocessor.handle(requestInit);
65
+ // Perform request
66
+ const customFetch = action
67
+ .context?.get(context_entries_1.KeysHttp.fetch);
68
+ const response = await (customFetch || fetch)(action.input, requestInit);
69
+ // We remove or update the timeout
70
+ if (requestTimeout !== undefined) {
71
+ const httpBodyTimeout = action.context?.get(context_entries_1.KeysHttp.httpBodyTimeout) || false;
72
+ if (httpBodyTimeout && response.body) {
73
+ onTimeout = () => response.body?.cancel(new Error(`HTTP timeout when reading the body of ${response.url}.
74
+ This error can be disabled by modifying the 'httpBodyTimeout' and/or 'httpTimeout' options.`));
75
+ response.body.on('close', () => {
76
+ clearTimeout(requestTimeout);
77
+ });
78
+ }
79
+ else {
80
+ clearTimeout(requestTimeout);
81
+ }
82
+ }
57
83
  // Node-fetch does not support body.cancel, while it is mandatory according to the fetch and readablestream api.
58
84
  // If it doesn't exist, we monkey-patch it.
59
85
  if (response.body && !response.body.cancel) {
60
86
  response.body.cancel = async (error) => {
61
87
  response.body.destroy(error);
88
+ if (requestTimeout !== undefined) {
89
+ // We make sure to remove the timeout if it is still enabled
90
+ clearTimeout(requestTimeout);
91
+ }
62
92
  };
63
93
  }
64
94
  return response;
65
- });
95
+ }
96
+ catch (error) {
97
+ if (requestTimeout !== undefined) {
98
+ clearTimeout(requestTimeout);
99
+ }
100
+ throw error;
101
+ }
66
102
  }
67
103
  }
68
104
  exports.ActorHttpFetch = ActorHttpFetch;
@@ -4,4 +4,5 @@ import type { IFetchInitPreprocessor } from './IFetchInitPreprocessor';
4
4
  */
5
5
  export declare class FetchInitPreprocessor implements IFetchInitPreprocessor {
6
6
  handle(init: RequestInit): Promise<RequestInit>;
7
+ createAbortController(): Promise<AbortController>;
7
8
  }
@@ -35,6 +35,9 @@ class FetchInitPreprocessor {
35
35
  // Only enable keepalive functionality if we are not sending a body (some browsers seem to trip over this)
36
36
  return { keepalive: !init.body, ...init };
37
37
  }
38
+ async createAbortController() {
39
+ return new AbortController();
40
+ }
38
41
  }
39
42
  exports.FetchInitPreprocessor = FetchInitPreprocessor;
40
43
  //# sourceMappingURL=FetchInitPreprocessor-browser.js.map
@@ -6,4 +6,5 @@ export declare class FetchInitPreprocessor implements IFetchInitPreprocessor {
6
6
  private readonly agent;
7
7
  constructor(agentOptions: any);
8
8
  handle(init: RequestInit): Promise<RequestInit>;
9
+ createAbortController(): Promise<AbortController>;
9
10
  }
@@ -20,6 +20,12 @@ class FetchInitPreprocessor {
20
20
  }
21
21
  return { ...init, agent: this.agent };
22
22
  }
23
+ async createAbortController() {
24
+ // Fallback to abort-controller for Node 14 backward compatibility
25
+ /* istanbul ignore next */
26
+ const AbortController = global.AbortController || await Promise.resolve().then(() => require('abort-controller'));
27
+ return new AbortController();
28
+ }
23
29
  }
24
30
  exports.FetchInitPreprocessor = FetchInitPreprocessor;
25
31
  //# sourceMappingURL=FetchInitPreprocessor.js.map
@@ -3,4 +3,5 @@
3
3
  */
4
4
  export interface IFetchInitPreprocessor {
5
5
  handle: (init: RequestInit) => Promise<RequestInit>;
6
+ createAbortController: () => Promise<AbortController>;
6
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@comunica/actor-http-fetch",
3
- "version": "2.2.1",
3
+ "version": "2.2.2-alpha.13.0",
4
4
  "description": "A node-fetch http actor",
5
5
  "lsd:module": true,
6
6
  "main": "lib/index.js",
@@ -29,10 +29,10 @@
29
29
  "lib/**/*.js"
30
30
  ],
31
31
  "dependencies": {
32
- "@comunica/bus-http": "^2.2.0",
33
- "@comunica/context-entries": "^2.2.0",
34
- "@comunica/core": "^2.2.0",
35
- "@comunica/mediatortype-time": "^2.2.0",
32
+ "@comunica/bus-http": "2.2.2-alpha.13.0",
33
+ "@comunica/context-entries": "2.2.2-alpha.13.0",
34
+ "@comunica/mediatortype-time": "2.2.2-alpha.13.0",
35
+ "abort-controller": "^3.0.0",
36
36
  "cross-fetch": "^3.0.5"
37
37
  },
38
38
  "scripts": {
@@ -43,5 +43,5 @@
43
43
  "browser": {
44
44
  "./lib/FetchInitPreprocessor.js": "./lib/FetchInitPreprocessor-browser.js"
45
45
  },
46
- "gitHead": "b2a0bee9640e2bcf70492fd7c497bab3d0e14113"
46
+ "gitHead": "f3a79b04bcf5f96767847cc335145491ccba68ff"
47
47
  }