@comunica/actor-http-fetch 4.0.1-alpha.48.0 → 4.0.2

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
@@ -9,6 +9,7 @@ This module is part of the [Comunica framework](https://github.com/comunica/comu
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 global `fetch` implementation from the runtime or a polyfill is used.
12
13
 
13
14
  [Click here if you just want to query with Comunica](https://comunica.dev/docs/query/).
14
15
 
@@ -21,14 +22,12 @@ $ yarn add @comunica/actor-http-fetch
21
22
  ## Configure
22
23
 
23
24
  After installing, this package can be added to your engine's configuration as follows:
24
- ```text
25
+ ```json
25
26
  {
26
27
  "@context": [
27
- ...
28
28
  "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-http-fetch/^4.0.0/components/context.jsonld"
29
29
  ],
30
30
  "actors": [
31
- ...
32
31
  {
33
32
  "@id": "urn:comunica:default:http/actors#fetch",
34
33
  "@type": "ActorHttpFetch"
@@ -39,4 +38,4 @@ After installing, this package can be added to your engine's configuration as fo
39
38
 
40
39
  ### Config Parameters
41
40
 
42
- * `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)_
@@ -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",
@@ -148,33 +147,33 @@
148
147
  }
149
148
  ],
150
149
  "memberFields": [
151
- {
152
- "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_userAgent",
153
- "memberFieldName": "userAgent"
154
- },
155
150
  {
156
151
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_fetchInitPreprocessor",
157
152
  "memberFieldName": "fetchInitPreprocessor"
158
153
  },
159
154
  {
160
- "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_constructor",
161
- "memberFieldName": "constructor"
155
+ "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_userAgent",
156
+ "memberFieldName": "userAgent"
162
157
  },
163
158
  {
164
- "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_createUserAgent",
165
- "memberFieldName": "createUserAgent"
159
+ "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_constructor",
160
+ "memberFieldName": "constructor"
166
161
  },
167
162
  {
168
163
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_test",
169
164
  "memberFieldName": "test"
170
165
  },
171
- {
172
- "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_getResponse",
173
- "memberFieldName": "getResponse"
174
- },
175
166
  {
176
167
  "@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_run",
177
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"
178
177
  }
179
178
  ],
180
179
  "constructorArguments": [
@@ -2,28 +2,26 @@ import type { IActionHttp, IActorHttpOutput, IActorHttpArgs } from '@comunica/bu
2
2
  import { ActorHttp } from '@comunica/bus-http';
3
3
  import type { TestResult } from '@comunica/core';
4
4
  import type { IMediatorTypeTime } from '@comunica/mediatortype-time';
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
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
  /**
@@ -4,156 +4,83 @@ 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
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
16
  return (0, core_1.passTest)({ time: Number.POSITIVE_INFINITY });
26
17
  }
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
- }
76
- }
77
18
  async run(action) {
78
- // Prepare headers
79
- const headers = new Headers(action.init?.headers);
80
- if (!headers.has('User-Agent')) {
81
- headers.set('User-Agent', this.userAgent);
82
- }
83
- const authString = action.context.get(context_entries_1.KeysHttp.auth);
84
- if (authString) {
85
- headers.set('Authorization', `Basic ${Buffer.from(authString).toString('base64')}`);
86
- }
87
- action.init = { ...action.init, headers };
88
- // Log request
89
- this.logInfo(action.context, `Requesting ${typeof action.input === 'string' ?
90
- action.input :
91
- action.input.url}`, () => ({
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}`, () => ({
92
22
  headers: bus_http_1.ActorHttp.headersToHash(headers),
93
- method: action.init.method ?? 'GET',
23
+ method: init.method,
94
24
  }));
95
25
  // TODO: remove this workaround once this has a fix: https://github.com/inrupt/solid-client-authn-js/issues/1708
96
- if (action.init?.headers && 'append' in action.init.headers && action.context.has(context_entries_1.KeysHttp.fetch)) {
97
- action.init.headers = bus_http_1.ActorHttp.headersToHash(headers);
26
+ if (action.context.has(context_entries_1.KeysHttp.fetch)) {
27
+ init.headers = bus_http_1.ActorHttp.headersToHash(headers);
98
28
  }
99
- let requestInit = { ...action.init };
100
29
  if (action.context.get(context_entries_1.KeysHttp.includeCredentials)) {
101
- requestInit.credentials = 'include';
30
+ init.credentials = 'include';
102
31
  }
103
- const httpTimeout = action.context?.get(context_entries_1.KeysHttp.httpTimeout);
104
- let requestTimeout;
105
- let onTimeout;
106
- if (httpTimeout !== undefined) {
107
- const controller = new AbortController();
108
- requestInit.signal = controller.signal;
109
- onTimeout = () => controller.abort();
110
- 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);
111
43
  }
112
- try {
113
- requestInit = await this.fetchInitPreprocessor.handle(requestInit);
114
- // Number of retries to perform after a failed fetch.
115
- const retryCount = action.context?.get(context_entries_1.KeysHttp.httpRetryCount) ?? 0;
116
- const retryDelay = action.context?.get(context_entries_1.KeysHttp.httpRetryDelay) ?? 0;
117
- const retryOnSeverError = action.context?.get(context_entries_1.KeysHttp.httpRetryOnServerError) ?? false;
118
- const customFetch = action
119
- .context?.get(context_entries_1.KeysHttp.fetch);
120
- // Execute the fetch (with retries and timeouts, if applicable).
121
- const response = await ActorHttpFetch.getResponse(customFetch ?? fetch, action.input, requestInit, retryCount, retryDelay, retryOnSeverError);
122
- // We remove or update the timeout
123
- if (requestTimeout !== undefined) {
124
- const httpBodyTimeout = action.context?.get(context_entries_1.KeysHttp.httpBodyTimeout) ?? false;
125
- if (httpBodyTimeout && response.body) {
126
- // eslint-disable-next-line ts/no-misused-promises
127
- onTimeout = () => response.body?.cancel(new Error(`HTTP timeout when reading the body of ${response.url}.
128
- This error can be disabled by modifying the 'httpBodyTimeout' and/or 'httpTimeout' options.`));
129
- response.body.on('close', () => {
130
- clearTimeout(requestTimeout);
131
- });
132
- }
133
- else {
134
- clearTimeout(requestTimeout);
135
- }
136
- }
137
- // Node-fetch does not support body.cancel, while it is mandatory according to the fetch and readablestream api.
138
- // If it doesn't exist, we monkey-patch it.
139
- if (response.body && !response.body.cancel) {
140
- response.body.cancel = async (error) => {
141
- response.body.destroy(error);
142
- if (requestTimeout !== undefined) {
143
- // We make sure to remove the timeout if it is still enabled
144
- clearTimeout(requestTimeout);
145
- }
146
- };
147
- }
148
- return response;
44
+ const response = await fetchFunction(action.input, requestInit);
45
+ if (httpTimeout && (!httpBodyTimeout || !response.body)) {
46
+ clearTimeout(timeoutHandle);
149
47
  }
150
- catch (error) {
151
- if (requestTimeout !== undefined) {
152
- clearTimeout(requestTimeout);
153
- }
154
- 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);
155
64
  }
65
+ const authString = action.context.get(context_entries_1.KeysHttp.auth);
66
+ if (authString) {
67
+ headers.set('Authorization', `Basic ${ActorHttpFetch.stringToBase64(authString)}`);
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);
156
82
  }
157
83
  }
158
84
  exports.ActorHttpFetch = ActorHttpFetch;
85
+ ActorHttpFetch.userAgent = bus_http_1.ActorHttp.createUserAgent('ActorHttpFetch', package_json_1.version);
159
86
  //# sourceMappingURL=ActorHttpFetch.js.map
@@ -1 +1 @@
1
- {"version":3,"file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orHttpArgs } 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';\nimport type { Readable } from 'readable-stream';\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<TestResult<IMediatorTypeTime>> {\n return passTest({ 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 headers = new Headers(action.init?.headers);\n if (!headers.has('User-Agent')) {\n headers.set('User-Agent', this.userAgent);\n }\n const authString = action.context.get<string>(KeysHttp.auth);\n if (authString) {\n headers.set('Authorization', `Basic ${Buffer.from(authString).toString('base64')}`);\n }\n action.init = { ...action.init, headers };\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(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(headers);\n }\n\n let requestInit: 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 = new AbortController();\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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AC,MAAmB;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"]}
@@ -1,24 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FetchInitPreprocessor = void 0;
4
- const bus_http_1 = require("@comunica/bus-http");
5
4
  /**
6
5
  * Overrides things for fetch requests in browsers
7
6
  */
8
7
  class FetchInitPreprocessor {
9
8
  async handle(init) {
10
- // Remove overridden user-agent header within browsers to avoid CORS issues
11
- if (init.headers) {
12
- const headers = new Headers(init.headers);
13
- if (headers.has('user-agent')) {
14
- headers.delete('user-agent');
15
- }
16
- init.headers = headers;
17
- }
18
- // TODO: remove this workaround once this has a fix: https://github.com/inrupt/solid-client-authn-js/issues/1708
19
- if (init?.headers && 'append' in init.headers) {
20
- init.headers = bus_http_1.ActorHttp.headersToHash(init.headers);
21
- }
22
9
  // Browsers don't yet support passing ReadableStream as body to requests, see
23
10
  // https://bugs.chromium.org/p/chromium/issues/detail?id=688906
24
11
  // https://bugzilla.mozilla.org/show_bug.cgi?id=1387483
@@ -37,7 +24,7 @@ class FetchInitPreprocessor {
37
24
  init.body = chunks.join('');
38
25
  }
39
26
  // Only enable keepalive functionality if we are not sending a body (some browsers seem to trip over this)
40
- return { keepalive: !init.body, ...init };
27
+ return { ...init, keepalive: !init.body };
41
28
  }
42
29
  }
43
30
  exports.FetchInitPreprocessor = FetchInitPreprocessor;
@@ -1 +1 @@
1
- {"version":3,"file":"FetchInitPreprocessor-browser.js","sourceRoot":"","sources":["FetchInitPreprocessor-browser.ts"],"names":[],"mappings":";;;AAAA,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;CACF;AAtCD,sDAsCC","sourcesContent":["import { 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"]}
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,5 +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>;
10
+ handle(init: RequestInit): Promise<RequestInit & {
11
+ agent: (url: URL) => HttpAgent;
12
+ }>;
9
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;
26
- }
27
17
  // Add 'Accept-Encoding' headers
28
18
  const headers = new Headers(init.headers);
29
19
  if (!headers.has('Accept-Encoding')) {
30
20
  headers.set('Accept-Encoding', 'br,gzip,deflate');
31
21
  init = { ...init, headers };
32
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'
33
26
  return {
34
27
  ...init,
28
+ ...init.body ? { keepalive: false, duplex: 'half' } : { keepalive: true },
35
29
  agent: this.agent,
36
- keepalive: halfDuplex ? undefined : true,
37
- duplex: halfDuplex ? 'half' : undefined,
38
30
  };
39
31
  }
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,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,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;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 // 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 return <any> {\n ...init,\n agent: this.agent,\n keepalive: halfDuplex ? undefined : true,\n duplex: halfDuplex ? 'half' : undefined,\n };\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"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@comunica/actor-http-fetch",
3
- "version": "4.0.1-alpha.48.0",
4
- "description": "A node-fetch http actor",
3
+ "version": "4.0.2",
4
+ "description": "A fetch http actor",
5
5
  "lsd:module": true,
6
6
  "license": "MIT",
7
7
  "funding": {
@@ -39,13 +39,13 @@
39
39
  "build:components": "componentsjs-generator"
40
40
  },
41
41
  "dependencies": {
42
- "@comunica/bus-http": "4.0.1-alpha.48.0",
43
- "@comunica/context-entries": "4.0.1-alpha.48.0",
44
- "@comunica/core": "4.0.1-alpha.48.0",
45
- "@comunica/mediatortype-time": "4.0.1-alpha.48.0"
42
+ "@comunica/bus-http": "^4.0.2",
43
+ "@comunica/context-entries": "^4.0.2",
44
+ "@comunica/core": "^4.0.2",
45
+ "@comunica/mediatortype-time": "^4.0.2"
46
46
  },
47
47
  "browser": {
48
48
  "./lib/FetchInitPreprocessor.js": "./lib/FetchInitPreprocessor-browser.js"
49
49
  },
50
- "gitHead": "4cc37905eab2c94a2e1d9cee0f51c53604f07da1"
50
+ "gitHead": "b60deab76821557f8d027208ab743d3c66fc60ea"
51
51
  }