@comunica/actor-http-fetch 3.3.0 → 4.0.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/README.md CHANGED
@@ -3,13 +3,13 @@
3
3
  [![npm version](https://badge.fury.io/js/%40comunica%2Factor-http-fetch.svg)](https://www.npmjs.com/package/@comunica/actor-http-fetch)
4
4
 
5
5
  An [HTTP](https://github.com/comunica/comunica/tree/master/packages/bus-http) actor that
6
- uses [cross-fetch](https://www.npmjs.com/package/cross-fetch) to perform HTTP requests.
6
+ uses [fetch](https://fetch.spec.whatwg.org/) to perform HTTP requests.
7
7
 
8
8
  This module is part of the [Comunica framework](https://github.com/comunica/comunica),
9
9
  and should only be used by [developers that want to build their own query engine](https://comunica.dev/docs/modify/).
10
10
 
11
11
  When this actor is used, a custom fetch implementation may be provided via the context (`fetch`).
12
- If none is provided, the default fallback fetch implementation will be used (`node-fetch` in Node.js, and the browser's `fetch` in browser environments).
12
+ If none is provided, the global `fetch` implementation from the runtime or a polyfill is used.
13
13
 
14
14
  [Click here if you just want to query with Comunica](https://comunica.dev/docs/query/).
15
15
 
@@ -22,14 +22,12 @@ $ yarn add @comunica/actor-http-fetch
22
22
  ## Configure
23
23
 
24
24
  After installing, this package can be added to your engine's configuration as follows:
25
- ```text
25
+ ```json
26
26
  {
27
27
  "@context": [
28
- ...
29
- "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-http-fetch/^3.0.0/components/context.jsonld"
28
+ "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-http-fetch/^4.0.0/components/context.jsonld"
30
29
  ],
31
30
  "actors": [
32
- ...
33
31
  {
34
32
  "@id": "urn:comunica:default:http/actors#fetch",
35
33
  "@type": "ActorHttpFetch"
@@ -40,4 +38,4 @@ After installing, this package can be added to your engine's configuration as fo
40
38
 
41
39
  ### Config Parameters
42
40
 
43
- * `agentOptions`: The agent JSON options for the HTTP agent. _(optional)_
41
+ * `agentOptions`: The agent JSON options for the HTTP agent in Node.js environments. _(optional)_
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "@context": [
3
- "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-http-fetch/^3.0.0/components/context.jsonld",
4
- "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/bus-http/^3.0.0/components/context.jsonld",
5
- "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/core/^3.0.0/components/context.jsonld"
3
+ "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-http-fetch/^4.0.0/components/context.jsonld",
4
+ "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/bus-http/^4.0.0/components/context.jsonld",
5
+ "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/core/^4.0.0/components/context.jsonld"
6
6
  ],
7
7
  "@id": "npmd:@comunica/actor-http-fetch",
8
8
  "components": [
@@ -13,7 +13,6 @@
13
13
  "extends": [
14
14
  "cbh:components/ActorHttp.jsonld#ActorHttp"
15
15
  ],
16
- "comment": "A node-fetch actor that listens on the 'init' bus. It will call `fetch` with either action.input or action.url.",
17
16
  "parameters": [
18
17
  {
19
18
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch_args_agentOptions",
@@ -55,29 +54,37 @@
55
54
  "genericTypeInstances": [
56
55
  {
57
56
  "@type": "ParameterRangeGenericTypeReference",
58
- "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_I"
57
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_I"
59
58
  },
60
59
  {
61
60
  "@type": "ParameterRangeGenericTypeReference",
62
- "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_T"
61
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_T"
63
62
  },
64
63
  {
65
64
  "@type": "ParameterRangeGenericTypeReference",
66
- "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
65
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
66
+ },
67
+ {
68
+ "@type": "ParameterRangeGenericTypeReference",
69
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_TS"
67
70
  }
68
71
  ]
69
72
  },
70
73
  {
71
74
  "@type": "ParameterRangeGenericTypeReference",
72
- "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_I"
75
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_I"
76
+ },
77
+ {
78
+ "@type": "ParameterRangeGenericTypeReference",
79
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_T"
73
80
  },
74
81
  {
75
82
  "@type": "ParameterRangeGenericTypeReference",
76
- "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_T"
83
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
77
84
  },
78
85
  {
79
86
  "@type": "ParameterRangeGenericTypeReference",
80
- "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
87
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_TS"
81
88
  }
82
89
  ]
83
90
  },
@@ -87,6 +94,20 @@
87
94
  },
88
95
  "comment": "The bus this actor subscribes to."
89
96
  },
97
+ {
98
+ "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch_args_busFailMessage",
99
+ "range": {
100
+ "@type": "ParameterRangeUnion",
101
+ "parameterRangeElements": [
102
+ "xsd:string",
103
+ {
104
+ "@type": "ParameterRangeUndefined"
105
+ }
106
+ ]
107
+ },
108
+ "default": "HTTP request failed: none of the configured actors were able to handle ${action.input}",
109
+ "comment": "The message that will be configured in the bus for reporting failures. This message may be a template string that contains references to the executed `action`. For example, the following templated string is allowed: \"RDF dereferencing failed: no actors could handle ${action.handle.mediaType}\""
110
+ },
90
111
  {
91
112
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch_args_beforeActors",
92
113
  "range": {
@@ -100,15 +121,19 @@
100
121
  "genericTypeInstances": [
101
122
  {
102
123
  "@type": "ParameterRangeGenericTypeReference",
103
- "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_I"
124
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_I"
104
125
  },
105
126
  {
106
127
  "@type": "ParameterRangeGenericTypeReference",
107
- "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_T"
128
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_T"
108
129
  },
109
130
  {
110
131
  "@type": "ParameterRangeGenericTypeReference",
111
- "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
132
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
133
+ },
134
+ {
135
+ "@type": "ParameterRangeGenericTypeReference",
136
+ "parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_TS"
112
137
  }
113
138
  ]
114
139
  }
@@ -122,33 +147,33 @@
122
147
  }
123
148
  ],
124
149
  "memberFields": [
125
- {
126
- "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_userAgent",
127
- "memberFieldName": "userAgent"
128
- },
129
150
  {
130
151
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_fetchInitPreprocessor",
131
152
  "memberFieldName": "fetchInitPreprocessor"
132
153
  },
133
154
  {
134
- "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_constructor",
135
- "memberFieldName": "constructor"
155
+ "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_userAgent",
156
+ "memberFieldName": "userAgent"
136
157
  },
137
158
  {
138
- "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_createUserAgent",
139
- "memberFieldName": "createUserAgent"
159
+ "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_constructor",
160
+ "memberFieldName": "constructor"
140
161
  },
141
162
  {
142
163
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_test",
143
164
  "memberFieldName": "test"
144
165
  },
145
- {
146
- "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_getResponse",
147
- "memberFieldName": "getResponse"
148
- },
149
166
  {
150
167
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_run",
151
168
  "memberFieldName": "run"
169
+ },
170
+ {
171
+ "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_prepareRequestHeaders",
172
+ "memberFieldName": "prepareRequestHeaders"
173
+ },
174
+ {
175
+ "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_stringToBase64",
176
+ "memberFieldName": "stringToBase64"
152
177
  }
153
178
  ],
154
179
  "constructorArguments": [
@@ -173,6 +198,12 @@
173
198
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch_args_bus"
174
199
  }
175
200
  },
201
+ {
202
+ "keyRaw": "busFailMessage",
203
+ "value": {
204
+ "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch_args_busFailMessage"
205
+ }
206
+ },
176
207
  {
177
208
  "keyRaw": "beforeActors",
178
209
  "value": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "@context": [
3
- "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-http-fetch/^3.0.0/components/context.jsonld"
3
+ "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-http-fetch/^4.0.0/components/context.jsonld"
4
4
  ],
5
5
  "@id": "npmd:@comunica/actor-http-fetch",
6
6
  "@type": "Module",
@@ -3,7 +3,7 @@
3
3
  "https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^6.0.0/components/context.jsonld",
4
4
  {
5
5
  "npmd": "https://linkedsoftwaredependencies.org/bundles/npm/",
6
- "cahf": "npmd:@comunica/actor-http-fetch/^3.0.0/",
6
+ "cahf": "npmd:@comunica/actor-http-fetch/^4.0.0/",
7
7
  "ActorHttpFetch": {
8
8
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch",
9
9
  "@prefix": true,
@@ -17,6 +17,9 @@
17
17
  "args_bus": {
18
18
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch_args_bus"
19
19
  },
20
+ "args_busFailMessage": {
21
+ "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch_args_busFailMessage"
22
+ },
20
23
  "args_beforeActors": {
21
24
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch_args_beforeActors",
22
25
  "@container": "@list"
@@ -30,6 +33,9 @@
30
33
  "bus": {
31
34
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch_args_bus"
32
35
  },
36
+ "busFailMessage": {
37
+ "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch_args_busFailMessage"
38
+ },
33
39
  "beforeActors": {
34
40
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch_args_beforeActors",
35
41
  "@container": "@list"
@@ -1,29 +1,27 @@
1
1
  import type { IActionHttp, IActorHttpOutput, IActorHttpArgs } from '@comunica/bus-http';
2
2
  import { ActorHttp } from '@comunica/bus-http';
3
+ import type { TestResult } from '@comunica/core';
3
4
  import type { IMediatorTypeTime } from '@comunica/mediatortype-time';
4
- import 'cross-fetch/polyfill';
5
- /**
6
- * A node-fetch actor that listens on the 'init' bus.
7
- *
8
- * It will call `fetch` with either action.input or action.url.
9
- */
10
5
  export declare class ActorHttpFetch extends ActorHttp {
11
- private readonly userAgent;
12
6
  private readonly fetchInitPreprocessor;
7
+ private static readonly userAgent;
13
8
  constructor(args: IActorHttpFetchArgs);
14
- static createUserAgent(): string;
15
- test(_action: IActionHttp): Promise<IMediatorTypeTime>;
9
+ test(_action: IActionHttp): Promise<TestResult<IMediatorTypeTime>>;
10
+ run(action: IActionHttp): Promise<IActorHttpOutput>;
16
11
  /**
17
- * Perform a fetch request, taking care of retries
18
- * @param fetchFn
19
- * @param requestInput Url or RequestInfo to pass to fetchFn
20
- * @param requestInit RequestInit to pass to fetch function
21
- * @param retryCount Maximum retries after which to abort
22
- * @param retryDelay Time in milliseconds to wait between retries
23
- * @returns a fetch `Response` object
12
+ * Prepares the request headers, taking into account the environment.
13
+ * @param {IActionHttp} action The HTTP action
14
+ * @returns {Headers} Headers
24
15
  */
25
- private static getResponse;
26
- run(action: IActionHttp): Promise<IActorHttpOutput>;
16
+ prepareRequestHeaders(action: IActionHttp): Headers;
17
+ /**
18
+ * Converts a string, including ones with Unicode symbols, to Base64 encoding.
19
+ * This function was adapted from the MDN example function here:
20
+ * https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
21
+ * @param {string} value The string value to encode
22
+ * @returns {string} The Base64-encoded value
23
+ */
24
+ static stringToBase64(value: string): string;
27
25
  }
28
26
  export interface IActorHttpFetchArgs extends IActorHttpArgs {
29
27
  /**
@@ -3,158 +3,84 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ActorHttpFetch = void 0;
4
4
  const bus_http_1 = require("@comunica/bus-http");
5
5
  const context_entries_1 = require("@comunica/context-entries");
6
- require("cross-fetch/polyfill");
6
+ const core_1 = require("@comunica/core");
7
+ // eslint-disable-next-line import/extensions
8
+ const package_json_1 = require("../package.json");
7
9
  const FetchInitPreprocessor_1 = require("./FetchInitPreprocessor");
8
- /**
9
- * A node-fetch actor that listens on the 'init' bus.
10
- *
11
- * It will call `fetch` with either action.input or action.url.
12
- */
13
10
  class ActorHttpFetch extends bus_http_1.ActorHttp {
14
11
  constructor(args) {
15
12
  super(args);
16
- this.userAgent = ActorHttpFetch.createUserAgent();
17
13
  this.fetchInitPreprocessor = new FetchInitPreprocessor_1.FetchInitPreprocessor(args.agentOptions);
18
14
  }
19
- static createUserAgent() {
20
- return `Comunica/actor-http-fetch (${typeof globalThis.navigator === 'undefined' ?
21
- `Node.js ${process.version}; ${process.platform}` :
22
- `Browser-${globalThis.navigator.userAgent}`})`;
23
- }
24
15
  async test(_action) {
25
- return { time: Number.POSITIVE_INFINITY };
26
- }
27
- /**
28
- * Perform a fetch request, taking care of retries
29
- * @param fetchFn
30
- * @param requestInput Url or RequestInfo to pass to fetchFn
31
- * @param requestInit RequestInit to pass to fetch function
32
- * @param retryCount Maximum retries after which to abort
33
- * @param retryDelay Time in milliseconds to wait between retries
34
- * @returns a fetch `Response` object
35
- */
36
- static async getResponse(fetchFn, requestInput, requestInit, retryCount, retryDelay, throwOnServerError) {
37
- let lastError;
38
- // The retryCount is 0-based. Therefore, add 1 to triesLeft.
39
- let triesLeft = retryCount + 1;
40
- // When retry count is greater than 0, repeat fetch.
41
- while (triesLeft-- > 0) {
42
- try {
43
- const response = await fetchFn(requestInput, requestInit);
44
- // Check, if server sent a 5xx error response.
45
- if (throwOnServerError && response.status >= 500 && response.status < 600) {
46
- throw new Error(`Server replied with response code ${response.status}: ${response.statusText}`);
47
- }
48
- return response;
49
- }
50
- catch (error) {
51
- lastError = error;
52
- // If the fetch was aborted by timeout, we won't retry.
53
- if (requestInit.signal?.aborted) {
54
- throw error;
55
- }
56
- if (triesLeft > 0) {
57
- // Wait for specified delay, before retrying.
58
- await new Promise((resolve, reject) => {
59
- setTimeout(resolve, retryDelay);
60
- // Cancel waiting, if timeout is reached.
61
- requestInit.signal?.addEventListener('abort', () => {
62
- reject(new Error('Fetch aborted by timeout.'));
63
- });
64
- });
65
- }
66
- }
67
- }
68
- // The fetch was not successful. We throw.
69
- if (retryCount > 0) {
70
- // Feedback the last error, if there were retry attempts.
71
- throw new Error(`Number of fetch retries (${retryCount}) exceeded. Last error: ${String(lastError)}`);
72
- }
73
- else {
74
- throw lastError;
75
- }
16
+ return (0, core_1.passTest)({ time: Number.POSITIVE_INFINITY });
76
17
  }
77
18
  async run(action) {
78
- // Prepare headers
79
- const initHeaders = action.init?.headers ?? {};
80
- action.init = action.init ?? {};
81
- action.init.headers = new Headers(initHeaders);
82
- if (!action.init.headers.has('user-agent')) {
83
- action.init.headers.append('user-agent', this.userAgent);
84
- }
85
- const authString = action.context.get(context_entries_1.KeysHttp.auth);
86
- if (authString) {
87
- action.init.headers.append('Authorization', `Basic ${Buffer.from(authString).toString('base64')}`);
88
- }
89
- // Log request
90
- this.logInfo(action.context, `Requesting ${typeof action.input === 'string' ?
91
- action.input :
92
- action.input.url}`, () => ({
93
- headers: bus_http_1.ActorHttp.headersToHash(new Headers(action.init.headers)),
94
- method: action.init.method ?? 'GET',
19
+ const headers = this.prepareRequestHeaders(action);
20
+ const init = { method: 'GET', ...action.init, headers };
21
+ this.logInfo(action.context, `Requesting ${bus_http_1.ActorHttp.getInputUrl(action.input).href}`, () => ({
22
+ headers: bus_http_1.ActorHttp.headersToHash(headers),
23
+ method: init.method,
95
24
  }));
96
25
  // TODO: remove this workaround once this has a fix: https://github.com/inrupt/solid-client-authn-js/issues/1708
97
- if (action.init?.headers && 'append' in action.init.headers && action.context.has(context_entries_1.KeysHttp.fetch)) {
98
- action.init.headers = bus_http_1.ActorHttp.headersToHash(action.init.headers);
26
+ if (action.context.has(context_entries_1.KeysHttp.fetch)) {
27
+ init.headers = bus_http_1.ActorHttp.headersToHash(headers);
99
28
  }
100
- let requestInit = { ...action.init };
101
29
  if (action.context.get(context_entries_1.KeysHttp.includeCredentials)) {
102
- requestInit.credentials = 'include';
30
+ init.credentials = 'include';
103
31
  }
104
- const httpTimeout = action.context?.get(context_entries_1.KeysHttp.httpTimeout);
105
- let requestTimeout;
106
- let onTimeout;
107
- if (httpTimeout !== undefined) {
108
- const controller = await this.fetchInitPreprocessor.createAbortController();
109
- requestInit.signal = controller.signal;
110
- onTimeout = () => controller.abort();
111
- requestTimeout = setTimeout(() => onTimeout(), httpTimeout);
32
+ const httpTimeout = action.context.get(context_entries_1.KeysHttp.httpTimeout);
33
+ const httpBodyTimeout = action.context.get(context_entries_1.KeysHttp.httpBodyTimeout);
34
+ const fetchFunction = action.context.get(context_entries_1.KeysHttp.fetch) ?? fetch;
35
+ const requestInit = await this.fetchInitPreprocessor.handle(init);
36
+ let timeoutCallback;
37
+ let timeoutHandle;
38
+ if (httpTimeout) {
39
+ const abortController = new AbortController();
40
+ requestInit.signal = abortController.signal;
41
+ timeoutCallback = () => abortController.abort(new Error(`Fetch timed out for ${bus_http_1.ActorHttp.getInputUrl(action.input).href} after ${httpTimeout} ms`));
42
+ timeoutHandle = setTimeout(() => timeoutCallback(), httpTimeout);
112
43
  }
113
- try {
114
- requestInit = await this.fetchInitPreprocessor.handle(requestInit);
115
- // Number of retries to perform after a failed fetch.
116
- const retryCount = action.context?.get(context_entries_1.KeysHttp.httpRetryCount) ?? 0;
117
- const retryDelay = action.context?.get(context_entries_1.KeysHttp.httpRetryDelay) ?? 0;
118
- const retryOnSeverError = action.context?.get(context_entries_1.KeysHttp.httpRetryOnServerError) ?? false;
119
- const customFetch = action
120
- .context?.get(context_entries_1.KeysHttp.fetch);
121
- // Execute the fetch (with retries and timeouts, if applicable).
122
- const response = await ActorHttpFetch.getResponse(customFetch ?? fetch, action.input, requestInit, retryCount, retryDelay, retryOnSeverError);
123
- // We remove or update the timeout
124
- if (requestTimeout !== undefined) {
125
- const httpBodyTimeout = action.context?.get(context_entries_1.KeysHttp.httpBodyTimeout) || false;
126
- if (httpBodyTimeout && response.body) {
127
- // eslint-disable-next-line ts/no-misused-promises
128
- onTimeout = () => response.body?.cancel(new Error(`HTTP timeout when reading the body of ${response.url}.
129
- This error can be disabled by modifying the 'httpBodyTimeout' and/or 'httpTimeout' options.`));
130
- response.body.on('close', () => {
131
- clearTimeout(requestTimeout);
132
- });
133
- }
134
- else {
135
- clearTimeout(requestTimeout);
136
- }
137
- }
138
- // Node-fetch does not support body.cancel, while it is mandatory according to the fetch and readablestream api.
139
- // If it doesn't exist, we monkey-patch it.
140
- if (response.body && !response.body.cancel) {
141
- response.body.cancel = async (error) => {
142
- response.body.destroy(error);
143
- if (requestTimeout !== undefined) {
144
- // We make sure to remove the timeout if it is still enabled
145
- clearTimeout(requestTimeout);
146
- }
147
- };
148
- }
149
- return response;
44
+ const response = await fetchFunction(action.input, requestInit);
45
+ if (httpTimeout && (!httpBodyTimeout || !response.body)) {
46
+ clearTimeout(timeoutHandle);
150
47
  }
151
- catch (error) {
152
- if (requestTimeout !== undefined) {
153
- clearTimeout(requestTimeout);
154
- }
155
- throw error;
48
+ return response;
49
+ }
50
+ /**
51
+ * Prepares the request headers, taking into account the environment.
52
+ * @param {IActionHttp} action The HTTP action
53
+ * @returns {Headers} Headers
54
+ */
55
+ prepareRequestHeaders(action) {
56
+ const headers = new Headers(action.init?.headers);
57
+ if (bus_http_1.ActorHttp.isBrowser()) {
58
+ // When running in a browser, the User-Agent header should never be set
59
+ headers.delete('user-agent');
60
+ }
61
+ else if (!headers.has('user-agent')) {
62
+ // Otherwise, if no header value is provided, use the actor one
63
+ headers.set('user-agent', ActorHttpFetch.userAgent);
64
+ }
65
+ const authString = action.context.get(context_entries_1.KeysHttp.auth);
66
+ if (authString) {
67
+ headers.set('Authorization', `Basic ${ActorHttpFetch.stringToBase64(authString)}`);
156
68
  }
69
+ return headers;
70
+ }
71
+ /**
72
+ * Converts a string, including ones with Unicode symbols, to Base64 encoding.
73
+ * This function was adapted from the MDN example function here:
74
+ * https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
75
+ * @param {string} value The string value to encode
76
+ * @returns {string} The Base64-encoded value
77
+ */
78
+ static stringToBase64(value) {
79
+ const bytes = new TextEncoder().encode(value);
80
+ const binString = Array.from(bytes, byte => String.fromCodePoint(byte)).join('');
81
+ return btoa(binString);
157
82
  }
158
83
  }
159
84
  exports.ActorHttpFetch = ActorHttpFetch;
85
+ ActorHttpFetch.userAgent = bus_http_1.ActorHttp.createUserAgent('ActorHttpFetch', package_json_1.version);
160
86
  //# sourceMappingURL=ActorHttpFetch.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ActorHttpFetch.js","sourceRoot":"","sources":["ActorHttpFetch.ts"],"names":[],"mappings":";;;AACA,iDAA+C;AAC/C,+DAAqD;AAGrD,gCAA8B;AAC9B,mEAAgE;AAGhE;;;;GAIG;AACH,MAAa,cAAe,SAAQ,oBAAS;IAI3C,YAAmB,IAAyB;QAC1C,KAAK,CAAC,IAAI,CAAC,CAAC;QACZ,I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'@comunica/bus-http';\nimport { KeysHttp } from '@comunica/context-entries';\nimport type { IMediatorTypeTime } from '@comunica/mediatortype-time';\nimport type { Readable } from 'readable-stream';\nimport 'cross-fetch/polyfill';\nimport { FetchInitPreprocessor } from './FetchInitPreprocessor';\nimport type { IFetchInitPreprocessor } from './IFetchInitPreprocessor';\n\n/**\n * A node-fetch actor that listens on the 'init' bus.\n *\n * It will call `fetch` with either action.input or action.url.\n */\nexport class ActorHttpFetch extends ActorHttp {\n private readonly userAgent: string;\n private readonly fetchInitPreprocessor: IFetchInitPreprocessor;\n\n public constructor(args: IActorHttpFetchArgs) {\n super(args);\n this.userAgent = ActorHttpFetch.createUserAgent();\n this.fetchInitPreprocessor = new FetchInitPreprocessor(args.agentOptions);\n }\n\n public static createUserAgent(): string {\n return `Comunica/actor-http-fetch (${typeof globalThis.navigator === 'undefined' ?\n `Node.js ${process.version}; ${process.platform}` :\n `Browser-${globalThis.navigator.userAgent}`})`;\n }\n\n public async test(_action: IActionHttp): Promise<IMediatorTypeTime> {\n return { time: Number.POSITIVE_INFINITY };\n }\n\n /**\n * Perform a fetch request, taking care of retries\n * @param fetchFn\n * @param requestInput Url or RequestInfo to pass to fetchFn\n * @param requestInit RequestInit to pass to fetch function\n * @param retryCount Maximum retries after which to abort\n * @param retryDelay Time in milliseconds to wait between retries\n * @returns a fetch `Response` object\n */\n private static async getResponse(\n fetchFn: (input: RequestInfo | URL, init?: RequestInit | undefined) => Promise<Response>,\n requestInput: RequestInfo | URL,\n requestInit: RequestInit,\n retryCount: number,\n retryDelay: number,\n throwOnServerError: boolean,\n ): Promise<Response> {\n let lastError: unknown;\n // The retryCount is 0-based. Therefore, add 1 to triesLeft.\n let triesLeft = retryCount + 1;\n\n // When retry count is greater than 0, repeat fetch.\n while (triesLeft-- > 0) {\n try {\n const response = await fetchFn(requestInput, requestInit);\n // Check, if server sent a 5xx error response.\n if (throwOnServerError && response.status >= 500 && response.status < 600) {\n throw new Error(`Server replied with response code ${response.status}: ${response.statusText}`);\n }\n return response;\n } catch (error: unknown) {\n lastError = error;\n // If the fetch was aborted by timeout, we won't retry.\n if (requestInit.signal?.aborted) {\n throw error;\n }\n\n if (triesLeft > 0) {\n // Wait for specified delay, before retrying.\n await new Promise((resolve, reject) => {\n setTimeout(resolve, retryDelay);\n // Cancel waiting, if timeout is reached.\n requestInit.signal?.addEventListener('abort', () => {\n reject(new Error('Fetch aborted by timeout.'));\n });\n });\n }\n }\n }\n // The fetch was not successful. We throw.\n if (retryCount > 0) {\n // Feedback the last error, if there were retry attempts.\n throw new Error(`Number of fetch retries (${retryCount}) exceeded. Last error: ${String(lastError)}`);\n } else {\n throw lastError;\n }\n }\n\n public async run(action: IActionHttp): Promise<IActorHttpOutput> {\n // Prepare headers\n const initHeaders = action.init?.headers ?? {};\n action.init = action.init ?? {};\n action.init.headers = new Headers(initHeaders);\n if (!action.init.headers.has('user-agent')) {\n action.init.headers.append('user-agent', this.userAgent);\n }\n const authString: string | undefined = action.context.get(KeysHttp.auth);\n if (authString) {\n action.init.headers.append('Authorization', `Basic ${Buffer.from(authString).toString('base64')}`);\n }\n\n // Log request\n this.logInfo(action.context, `Requesting ${typeof action.input === 'string' ?\n action.input :\n action.input.url}`, () => ({\n headers: ActorHttp.headersToHash(new Headers(action.init!.headers)),\n method: action.init!.method ?? 'GET',\n }));\n\n // TODO: remove this workaround once this has a fix: https://github.com/inrupt/solid-client-authn-js/issues/1708\n if (action.init?.headers && 'append' in action.init.headers && action.context.has(KeysHttp.fetch)) {\n action.init.headers = ActorHttp.headersToHash(action.init.headers);\n }\n\n let requestInit = { ...action.init };\n\n if (action.context.get(KeysHttp.includeCredentials)) {\n requestInit.credentials = 'include';\n }\n\n const httpTimeout: number | undefined = action.context?.get(KeysHttp.httpTimeout);\n let requestTimeout: NodeJS.Timeout | undefined;\n let onTimeout: (() => void) | undefined;\n if (httpTimeout !== undefined) {\n const controller = await this.fetchInitPreprocessor.createAbortController();\n requestInit.signal = controller.signal;\n onTimeout = () => controller.abort();\n requestTimeout = setTimeout(() => onTimeout!(), httpTimeout);\n }\n\n try {\n requestInit = await this.fetchInitPreprocessor.handle(requestInit);\n // Number of retries to perform after a failed fetch.\n const retryCount: number = action.context?.get(KeysHttp.httpRetryCount) ?? 0;\n const retryDelay: number = action.context?.get(KeysHttp.httpRetryDelay) ?? 0;\n const retryOnSeverError: boolean = action.context?.get(KeysHttp.httpRetryOnServerError) ?? false;\n const customFetch: ((input: RequestInfo | URL, init?: RequestInit) => Promise<Response>) | undefined = action\n .context?.get(KeysHttp.fetch);\n\n // Execute the fetch (with retries and timeouts, if applicable).\n const response = await ActorHttpFetch.getResponse(\n customFetch ?? fetch,\n action.input,\n requestInit,\n retryCount,\n retryDelay,\n retryOnSeverError,\n );\n\n // We remove or update the timeout\n if (requestTimeout !== undefined) {\n const httpBodyTimeout = action.context?.get(KeysHttp.httpBodyTimeout) || false;\n if (httpBodyTimeout && response.body) {\n // eslint-disable-next-line ts/no-misused-promises\n onTimeout = () => response.body?.cancel(new Error(`HTTP timeout when reading the body of ${response.url}.\nThis error can be disabled by modifying the 'httpBodyTimeout' and/or 'httpTimeout' options.`));\n (<Readable><any>response.body).on('close', () => {\n clearTimeout(requestTimeout);\n });\n } else {\n clearTimeout(requestTimeout);\n }\n }\n\n // Node-fetch does not support body.cancel, while it is mandatory according to the fetch and readablestream api.\n // If it doesn't exist, we monkey-patch it.\n if (response.body && !response.body.cancel) {\n response.body.cancel = async(error?: Error) => {\n (<Readable><any>response.body).destroy(error);\n if (requestTimeout !== undefined) {\n // We make sure to remove the timeout if it is still enabled\n clearTimeout(requestTimeout);\n }\n };\n }\n\n return response;\n } catch (error: unknown) {\n if (requestTimeout !== undefined) {\n clearTimeout(requestTimeout);\n }\n throw error;\n }\n }\n}\n\nexport interface IActorHttpFetchArgs extends IActorHttpArgs {\n /**\n * The agent options for the HTTP agent\n * @range {json}\n * @default {{ \"keepAlive\": true, \"maxSockets\": 5 }}\n */\n agentOptions?: Record<string, any>;\n}\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;QAC9C,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAElD,IAAI,oBAAS,CAAC,SAAS,EAAE,EAAE,CAAC;YAC1B,uEAAuE;YACvE,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;QAC/B,CAAC;aAAM,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;YACtC,+DAA+D;YAC/D,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,cAAc,CAAC,SAAU,CAAC,CAAC;QACvD,CAAC;QAED,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,0BAAQ,CAAC,IAAI,CAAC,CAAC;QACrD,IAAI,UAAU,EAAE,CAAC;YACf,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,SAAS,cAAc,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;QACrF,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,cAAc,CAAC,KAAa;QACxC,MAAM,KAAK,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC9C,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACjF,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC;IACzB,CAAC;;AA5FH,wCA6FC;AA1FyB,wBAAS,GAAG,oBAAS,CAAC,eAAe,CAAC,gBAAgB,EAAE,sBAAY,CAAC,CAAC","sourcesContent":["import type { IActionHttp, IActorHttpOutput, IActorHttpArgs } from '@comunica/bus-http';\nimport { ActorHttp } from '@comunica/bus-http';\nimport { KeysHttp } from '@comunica/context-entries';\nimport type { TestResult } from '@comunica/core';\nimport { passTest } from '@comunica/core';\nimport type { IMediatorTypeTime } from '@comunica/mediatortype-time';\n\n// eslint-disable-next-line import/extensions\nimport { version as actorVersion } from '../package.json';\n\nimport { FetchInitPreprocessor } from './FetchInitPreprocessor';\nimport type { IFetchInitPreprocessor } from './IFetchInitPreprocessor';\n\nexport class ActorHttpFetch extends ActorHttp {\n private readonly fetchInitPreprocessor: IFetchInitPreprocessor;\n\n private static readonly userAgent = ActorHttp.createUserAgent('ActorHttpFetch', actorVersion);\n\n public constructor(args: IActorHttpFetchArgs) {\n super(args);\n this.fetchInitPreprocessor = new FetchInitPreprocessor(args.agentOptions);\n }\n\n public async test(_action: IActionHttp): Promise<TestResult<IMediatorTypeTime>> {\n return passTest({ time: Number.POSITIVE_INFINITY });\n }\n\n public async run(action: IActionHttp): Promise<IActorHttpOutput> {\n const headers = this.prepareRequestHeaders(action);\n\n const init: RequestInit = { method: 'GET', ...action.init, headers };\n\n this.logInfo(action.context, `Requesting ${ActorHttp.getInputUrl(action.input).href}`, () => ({\n headers: ActorHttp.headersToHash(headers),\n method: init.method,\n }));\n\n // TODO: remove this workaround once this has a fix: https://github.com/inrupt/solid-client-authn-js/issues/1708\n if (action.context.has(KeysHttp.fetch)) {\n init.headers = ActorHttp.headersToHash(headers);\n }\n\n if (action.context.get(KeysHttp.includeCredentials)) {\n init.credentials = 'include';\n }\n\n const httpTimeout = action.context.get(KeysHttp.httpTimeout);\n const httpBodyTimeout = action.context.get(KeysHttp.httpBodyTimeout);\n const fetchFunction = action.context.get(KeysHttp.fetch) ?? fetch;\n const requestInit = await this.fetchInitPreprocessor.handle(init);\n\n let timeoutCallback: () => void;\n let timeoutHandle: NodeJS.Timeout | undefined;\n\n if (httpTimeout) {\n const abortController = new AbortController();\n requestInit.signal = abortController.signal;\n timeoutCallback = () => abortController.abort(new Error(`Fetch timed out for ${ActorHttp.getInputUrl(action.input).href} after ${httpTimeout} ms`));\n timeoutHandle = setTimeout(() => timeoutCallback(), httpTimeout);\n }\n\n const response = await fetchFunction(action.input, requestInit);\n\n if (httpTimeout && (!httpBodyTimeout || !response.body)) {\n clearTimeout(timeoutHandle);\n }\n\n return response;\n }\n\n /**\n * Prepares the request headers, taking into account the environment.\n * @param {IActionHttp} action The HTTP action\n * @returns {Headers} Headers\n */\n public prepareRequestHeaders(action: IActionHttp): Headers {\n const headers = new Headers(action.init?.headers);\n\n if (ActorHttp.isBrowser()) {\n // When running in a browser, the User-Agent header should never be set\n headers.delete('user-agent');\n } else if (!headers.has('user-agent')) {\n // Otherwise, if no header value is provided, use the actor one\n headers.set('user-agent', ActorHttpFetch.userAgent!);\n }\n\n const authString = action.context.get(KeysHttp.auth);\n if (authString) {\n headers.set('Authorization', `Basic ${ActorHttpFetch.stringToBase64(authString)}`);\n }\n\n return headers;\n }\n\n /**\n * Converts a string, including ones with Unicode symbols, to Base64 encoding.\n * This function was adapted from the MDN example function here:\n * https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem\n * @param {string} value The string value to encode\n * @returns {string} The Base64-encoded value\n */\n public static stringToBase64(value: string): string {\n const bytes = new TextEncoder().encode(value);\n const binString = Array.from(bytes, byte => String.fromCodePoint(byte)).join('');\n return btoa(binString);\n }\n}\n\nexport interface IActorHttpFetchArgs extends IActorHttpArgs {\n /**\n * The agent options for the HTTP agent\n * @range {json}\n * @default {{ \"keepAlive\": true, \"maxSockets\": 5 }}\n */\n agentOptions?: Record<string, any>;\n}\n"]}
@@ -4,5 +4,4 @@ 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>;
8
7
  }
@@ -1,26 +1,11 @@
1
1
  "use strict";
2
- /* eslint-disable unicorn/filename-case */
3
- /* eslint-enable unicorn/filename-case */
4
2
  Object.defineProperty(exports, "__esModule", { value: true });
5
3
  exports.FetchInitPreprocessor = void 0;
6
- const bus_http_1 = require("@comunica/bus-http");
7
4
  /**
8
5
  * Overrides things for fetch requests in browsers
9
6
  */
10
7
  class FetchInitPreprocessor {
11
8
  async handle(init) {
12
- // Remove overridden user-agent header within browsers to avoid CORS issues
13
- if (init.headers) {
14
- const headers = new Headers(init.headers);
15
- if (headers.has('user-agent')) {
16
- headers.delete('user-agent');
17
- }
18
- init.headers = headers;
19
- }
20
- // TODO: remove this workaround once this has a fix: https://github.com/inrupt/solid-client-authn-js/issues/1708
21
- if (init?.headers && 'append' in init.headers) {
22
- init.headers = bus_http_1.ActorHttp.headersToHash(init.headers);
23
- }
24
9
  // Browsers don't yet support passing ReadableStream as body to requests, see
25
10
  // https://bugs.chromium.org/p/chromium/issues/detail?id=688906
26
11
  // https://bugzilla.mozilla.org/show_bug.cgi?id=1387483
@@ -39,10 +24,7 @@ class FetchInitPreprocessor {
39
24
  init.body = chunks.join('');
40
25
  }
41
26
  // Only enable keepalive functionality if we are not sending a body (some browsers seem to trip over this)
42
- return { keepalive: !init.body, ...init };
43
- }
44
- async createAbortController() {
45
- return new AbortController();
27
+ return { ...init, keepalive: !init.body };
46
28
  }
47
29
  }
48
30
  exports.FetchInitPreprocessor = FetchInitPreprocessor;
@@ -1 +1 @@
1
- {"version":3,"file":"FetchInitPreprocessor-browser.js","sourceRoot":"","sources":["FetchInitPreprocessor-browser.ts"],"names":[],"mappings":";AAAA,0CAA0C;AAC1C,yCAAyC;;;AAEzC,iDAA+C;AAG/C;;GAEG;AACH,MAAa,qBAAqB;IACzB,KAAK,CAAC,MAAM,CAAC,IAAiB;QACnC,2EAA2E;QAC3E,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAC1C,IAAI,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;gBAC9B,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;YAC/B,CAAC;YACD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACzB,CAAC;QAED,gHAAgH;QAChH,IAAI,IAAI,EAAE,OAAO,IAAI,QAAQ,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAC9C,IAAI,CAAC,OAAO,GAAG,oBAAS,CAAC,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACvD,CAAC;QAED,6EAA6E;QAC7E,+DAA+D;QAC/D,uDAAuD;QACvD,qDAAqD;QACrD,oEAAoE;QACpE,IAAI,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,WAAW,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YAC3E,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACrC,MAAM,MAAM,GAAG,EAAE,CAAC;YAElB,OAAO,IAAI,EAAE,CAAC;gBACZ,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;gBAC5C,IAAI,IAAI,EAAE,CAAC;oBACT,MAAM;gBACR,CAAC;gBACD,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACrB,CAAC;YACD,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC9B,CAAC;QAED,0GAA0G;QAC1G,OAAO,EAAE,SAAS,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,EAAE,CAAC;IAC5C,CAAC;IAEM,KAAK,CAAC,qBAAqB;QAChC,OAAO,IAAI,eAAe,EAAE,CAAC;IAC/B,CAAC;CACF;AA1CD,sDA0CC","sourcesContent":["/* eslint-disable unicorn/filename-case */\n/* eslint-enable unicorn/filename-case */\n\nimport { ActorHttp } from '@comunica/bus-http';\nimport type { IFetchInitPreprocessor } from './IFetchInitPreprocessor';\n\n/**\n * Overrides things for fetch requests in browsers\n */\nexport class FetchInitPreprocessor implements IFetchInitPreprocessor {\n public async handle(init: RequestInit): Promise<RequestInit> {\n // Remove overridden user-agent header within browsers to avoid CORS issues\n if (init.headers) {\n const headers = new Headers(init.headers);\n if (headers.has('user-agent')) {\n headers.delete('user-agent');\n }\n init.headers = headers;\n }\n\n // TODO: remove this workaround once this has a fix: https://github.com/inrupt/solid-client-authn-js/issues/1708\n if (init?.headers && 'append' in init.headers) {\n init.headers = ActorHttp.headersToHash(init.headers);\n }\n\n // Browsers don't yet support passing ReadableStream as body to requests, see\n // https://bugs.chromium.org/p/chromium/issues/detail?id=688906\n // https://bugzilla.mozilla.org/show_bug.cgi?id=1387483\n // As such, we convert those bodies to a plain string\n // TODO: remove this once browser support ReadableStream in requests\n if (init.body && typeof init.body !== 'string' && 'getReader' in init.body) {\n const reader = init.body.getReader();\n const chunks = [];\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n chunks.push(value);\n }\n init.body = chunks.join('');\n }\n\n // Only enable keepalive functionality if we are not sending a body (some browsers seem to trip over this)\n return { keepalive: !init.body, ...init };\n }\n\n public async createAbortController(): Promise<AbortController> {\n return new AbortController();\n }\n}\n"]}
1
+ {"version":3,"file":"FetchInitPreprocessor-browser.js","sourceRoot":"","sources":["FetchInitPreprocessor-browser.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACH,MAAa,qBAAqB;IACzB,KAAK,CAAC,MAAM,CAAC,IAAiB;QACnC,6EAA6E;QAC7E,+DAA+D;QAC/D,uDAAuD;QACvD,qDAAqD;QACrD,oEAAoE;QACpE,IAAI,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,WAAW,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YAC3E,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACrC,MAAM,MAAM,GAAG,EAAE,CAAC;YAElB,OAAO,IAAI,EAAE,CAAC;gBACZ,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;gBAC5C,IAAI,IAAI,EAAE,CAAC;oBACT,MAAM;gBACR,CAAC;gBACD,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACrB,CAAC;YACD,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC9B,CAAC;QAED,0GAA0G;QAC1G,OAAO,EAAE,GAAG,IAAI,EAAE,SAAS,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;IAC5C,CAAC;CACF;AAxBD,sDAwBC","sourcesContent":["import type { IFetchInitPreprocessor } from './IFetchInitPreprocessor';\n\n/**\n * Overrides things for fetch requests in browsers\n */\nexport class FetchInitPreprocessor implements IFetchInitPreprocessor {\n public async handle(init: RequestInit): Promise<RequestInit> {\n // Browsers don't yet support passing ReadableStream as body to requests, see\n // https://bugs.chromium.org/p/chromium/issues/detail?id=688906\n // https://bugzilla.mozilla.org/show_bug.cgi?id=1387483\n // As such, we convert those bodies to a plain string\n // TODO: remove this once browser support ReadableStream in requests\n if (init.body && typeof init.body !== 'string' && 'getReader' in init.body) {\n const reader = init.body.getReader();\n const chunks = [];\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n chunks.push(value);\n }\n init.body = chunks.join('');\n }\n\n // Only enable keepalive functionality if we are not sending a body (some browsers seem to trip over this)\n return { ...init, keepalive: !init.body };\n }\n}\n"]}
@@ -1,3 +1,5 @@
1
+ /// <reference types="node" />
2
+ import { Agent as HttpAgent } from 'node:http';
1
3
  import type { IFetchInitPreprocessor } from './IFetchInitPreprocessor';
2
4
  /**
3
5
  * Overrides the HTTP agent to perform better in Node.js.
@@ -5,6 +7,7 @@ import type { IFetchInitPreprocessor } from './IFetchInitPreprocessor';
5
7
  export declare class FetchInitPreprocessor implements IFetchInitPreprocessor {
6
8
  private readonly agent;
7
9
  constructor(agentOptions: any);
8
- handle(init: RequestInit): Promise<RequestInit>;
9
- createAbortController(): Promise<AbortController>;
10
+ handle(init: RequestInit): Promise<RequestInit & {
11
+ agent: (url: URL) => HttpAgent;
12
+ }>;
10
13
  }
@@ -4,7 +4,6 @@ exports.FetchInitPreprocessor = void 0;
4
4
  /* eslint-disable import/no-nodejs-modules */
5
5
  const node_http_1 = require("node:http");
6
6
  const node_https_1 = require("node:https");
7
- const bus_http_1 = require("@comunica/bus-http");
8
7
  /**
9
8
  * Overrides the HTTP agent to perform better in Node.js.
10
9
  */
@@ -15,29 +14,21 @@ class FetchInitPreprocessor {
15
14
  this.agent = (_parsedURL) => _parsedURL.protocol === 'http:' ? httpAgent : httpsAgent;
16
15
  }
17
16
  async handle(init) {
18
- // Convert body Web stream to Node stream, as node-fetch does not support Web streams
19
- let halfDuplex = false;
20
- if (init.body && typeof init.body !== 'string' && 'getReader' in init.body) {
21
- init.body = bus_http_1.ActorHttp.toNodeReadable(init.body);
22
- // The Fetch API requires specific options to be set when sending body streams:
23
- // - 'keepalive' can not be true
24
- // - 'duplex' must be set to 'half'
25
- halfDuplex = true;
17
+ // Add 'Accept-Encoding' headers
18
+ const headers = new Headers(init.headers);
19
+ if (!headers.has('Accept-Encoding')) {
20
+ headers.set('Accept-Encoding', 'br,gzip,deflate');
21
+ init = { ...init, headers };
26
22
  }
23
+ // The Fetch API requires specific options to be set when sending body streams:
24
+ // - 'keepalive' can not be true
25
+ // - 'duplex' must be set to 'half'
27
26
  return {
28
27
  ...init,
28
+ ...init.body ? { keepalive: false, duplex: 'half' } : { keepalive: true },
29
29
  agent: this.agent,
30
- keepalive: halfDuplex ? undefined : true,
31
- duplex: halfDuplex ? 'half' : undefined,
32
30
  };
33
31
  }
34
- async createAbortController() {
35
- // Fallback to abort-controller for Node 14 backward compatibility
36
- /* istanbul ignore next */
37
- const AbortController = globalThis.AbortController || await Promise.resolve().then(() => require('abort-controller'));
38
- return new AbortController();
39
- }
40
32
  }
41
33
  exports.FetchInitPreprocessor = FetchInitPreprocessor;
42
- /* eslint-enable import/no-nodejs-modules */
43
34
  //# sourceMappingURL=FetchInitPreprocessor.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"FetchInitPreprocessor.js","sourceRoot":"","sources":["FetchInitPreprocessor.ts"],"names":[],"mappings":";;;AAAA,6CAA6C;AAC7C,yCAA+C;AAC/C,2CAAiD;AACjD,iDAA+C;AAG/C;;GAEG;AACH,MAAa,qBAAqB;IAGhC,YAAmB,YAAiB;QAClC,MAAM,SAAS,GAAG,IAAI,iBAAS,CAAC,YAAY,CAAC,CAAC;QAC9C,MAAM,UAAU,GAAG,IAAI,kBAAU,CAAC,YAAY,CAAC,CAAC;QAChD,IAAI,CAAC,KAAK,GAAG,CAAC,UAAe,EAAa,EAAE,CAAC,UAAU,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC;IACxG,CAAC;IAEM,KAAK,CAAC,MAAM,CAAC,IAAiB;QACnC,qFAAqF;QACrF,IAAI,UAAU,GAAG,KAAK,CAAC;QACvB,IAAI,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,WAAW,IAAU,IAAI,CAAC,IAAI,EAAE,CAAC;YACjF,IAAI,CAAC,IAAI,GAAS,oBAAS,CAAC,cAAc,CAAO,IAAI,CAAC,IAAI,CAAC,CAAC;YAC5D,+EAA+E;YAC/E,gCAAgC;YAChC,mCAAmC;YACnC,UAAU,GAAG,IAAI,CAAC;QACpB,CAAC;QAED,OAAa;YACX,GAAG,IAAI;YACP,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI;YACxC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS;SACxC,CAAC;IACJ,CAAC;IAEM,KAAK,CAAC,qBAAqB;QAChC,kEAAkE;QAClE,0BAA0B;QAC1B,MAAM,eAAe,GAAG,UAAU,CAAC,eAAe,IAAI,2CAAa,kBAAkB,EAAC,CAAC;QACvF,OAAO,IAAI,eAAe,EAAE,CAAC;IAC/B,CAAC;CACF;AAlCD,sDAkCC;AACD,4CAA4C","sourcesContent":["/* eslint-disable import/no-nodejs-modules */\nimport { Agent as HttpAgent } from 'node:http';\nimport { Agent as HttpsAgent } from 'node:https';\nimport { ActorHttp } from '@comunica/bus-http';\nimport type { IFetchInitPreprocessor } from './IFetchInitPreprocessor';\n\n/**\n * Overrides the HTTP agent to perform better in Node.js.\n */\nexport class FetchInitPreprocessor implements IFetchInitPreprocessor {\n private readonly agent: (url: URL) => HttpAgent;\n\n public constructor(agentOptions: any) {\n const httpAgent = new HttpAgent(agentOptions);\n const httpsAgent = new HttpsAgent(agentOptions);\n this.agent = (_parsedURL: URL): HttpAgent => _parsedURL.protocol === 'http:' ? httpAgent : httpsAgent;\n }\n\n public async handle(init: RequestInit): Promise<RequestInit> {\n // Convert body Web stream to Node stream, as node-fetch does not support Web streams\n let halfDuplex = false;\n if (init.body && typeof init.body !== 'string' && 'getReader' in <any> init.body) {\n init.body = <any> ActorHttp.toNodeReadable(<any> init.body);\n // The Fetch API requires specific options to be set when sending body streams:\n // - 'keepalive' can not be true\n // - 'duplex' must be set to 'half'\n halfDuplex = true;\n }\n\n return <any> {\n ...init,\n agent: this.agent,\n keepalive: halfDuplex ? undefined : true,\n duplex: halfDuplex ? 'half' : undefined,\n };\n }\n\n public async createAbortController(): Promise<AbortController> {\n // Fallback to abort-controller for Node 14 backward compatibility\n /* istanbul ignore next */\n const AbortController = globalThis.AbortController || await import('abort-controller');\n return new AbortController();\n }\n}\n/* eslint-enable import/no-nodejs-modules */\n"]}
1
+ {"version":3,"file":"FetchInitPreprocessor.js","sourceRoot":"","sources":["FetchInitPreprocessor.ts"],"names":[],"mappings":";;;AAAA,6CAA6C;AAC7C,yCAA+C;AAC/C,2CAAiD;AAKjD;;GAEG;AACH,MAAa,qBAAqB;IAGhC,YAAmB,YAAiB;QAClC,MAAM,SAAS,GAAG,IAAI,iBAAS,CAAC,YAAY,CAAC,CAAC;QAC9C,MAAM,UAAU,GAAG,IAAI,kBAAU,CAAC,YAAY,CAAC,CAAC;QAChD,IAAI,CAAC,KAAK,GAAG,CAAC,UAAe,EAAa,EAAE,CAAC,UAAU,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC;IACxG,CAAC;IAEM,KAAK,CAAC,MAAM,CAAC,IAAiB;QACnC,gCAAgC;QAChC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC1C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,EAAE,CAAC;YACpC,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE,iBAAiB,CAAC,CAAC;YAClD,IAAI,GAAG,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,CAAC;QAC9B,CAAC;QAED,+EAA+E;QAC/E,gCAAgC;QAChC,mCAAmC;QACnC,OAAO;YACL,GAAG,IAAI;YACP,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE;YACzE,KAAK,EAAE,IAAI,CAAC,KAAK;SAClB,CAAC;IACJ,CAAC;CACF;AA1BD,sDA0BC","sourcesContent":["/* eslint-disable import/no-nodejs-modules */\nimport { Agent as HttpAgent } from 'node:http';\nimport { Agent as HttpsAgent } from 'node:https';\n\n/* eslint-enable import/no-nodejs-modules */\nimport type { IFetchInitPreprocessor } from './IFetchInitPreprocessor';\n\n/**\n * Overrides the HTTP agent to perform better in Node.js.\n */\nexport class FetchInitPreprocessor implements IFetchInitPreprocessor {\n private readonly agent: (url: URL) => HttpAgent;\n\n public constructor(agentOptions: any) {\n const httpAgent = new HttpAgent(agentOptions);\n const httpsAgent = new HttpsAgent(agentOptions);\n this.agent = (_parsedURL: URL): HttpAgent => _parsedURL.protocol === 'http:' ? httpAgent : httpsAgent;\n }\n\n public async handle(init: RequestInit): Promise<RequestInit & { agent: (url: URL) => HttpAgent }> {\n // Add 'Accept-Encoding' headers\n const headers = new Headers(init.headers);\n if (!headers.has('Accept-Encoding')) {\n headers.set('Accept-Encoding', 'br,gzip,deflate');\n init = { ...init, headers };\n }\n\n // The Fetch API requires specific options to be set when sending body streams:\n // - 'keepalive' can not be true\n // - 'duplex' must be set to 'half'\n return {\n ...init,\n ...init.body ? { keepalive: false, duplex: 'half' } : { keepalive: true },\n agent: this.agent,\n };\n }\n}\n"]}
@@ -3,5 +3,4 @@
3
3
  */
4
4
  export interface IFetchInitPreprocessor {
5
5
  handle: (init: RequestInit) => Promise<RequestInit>;
6
- createAbortController: () => Promise<AbortController>;
7
6
  }
@@ -1 +1 @@
1
- {"version":3,"file":"IFetchInitPreprocessor.js","sourceRoot":"","sources":["IFetchInitPreprocessor.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Can modify a fetch init object.\n */\nexport interface IFetchInitPreprocessor {\n handle: (init: RequestInit) => Promise<RequestInit>;\n createAbortController: () => Promise<AbortController>;\n}\n"]}
1
+ {"version":3,"file":"IFetchInitPreprocessor.js","sourceRoot":"","sources":["IFetchInitPreprocessor.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Can modify a fetch init object.\n */\nexport interface IFetchInitPreprocessor {\n handle: (init: RequestInit) => Promise<RequestInit>;\n}\n"]}
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "@comunica/actor-http-fetch",
3
- "version": "3.3.0",
4
- "description": "A node-fetch http actor",
3
+ "version": "4.0.1",
4
+ "description": "A fetch http actor",
5
5
  "lsd:module": true,
6
6
  "license": "MIT",
7
+ "funding": {
8
+ "type": "opencollective",
9
+ "url": "https://opencollective.com/comunica-association"
10
+ },
7
11
  "homepage": "https://comunica.dev/",
8
12
  "repository": {
9
13
  "type": "git",
@@ -35,14 +39,13 @@
35
39
  "build:components": "componentsjs-generator"
36
40
  },
37
41
  "dependencies": {
38
- "@comunica/bus-http": "^3.3.0",
39
- "@comunica/context-entries": "^3.3.0",
40
- "@comunica/mediatortype-time": "^3.3.0",
41
- "abort-controller": "^3.0.0",
42
- "cross-fetch": "^4.0.0"
42
+ "@comunica/bus-http": "^4.0.1",
43
+ "@comunica/context-entries": "^4.0.1",
44
+ "@comunica/core": "^4.0.1",
45
+ "@comunica/mediatortype-time": "^4.0.1"
43
46
  },
44
47
  "browser": {
45
48
  "./lib/FetchInitPreprocessor.js": "./lib/FetchInitPreprocessor-browser.js"
46
49
  },
47
- "gitHead": "02bde397d206f1f5a523643a6a604c89e792e2f9"
50
+ "gitHead": "67831ba79e45ea1ced41fe3f83c5d6e8802dbeaa"
48
51
  }