@comunica/actor-http-fetch 2.4.3 → 2.5.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/components/ActorHttpFetch.jsonld +4 -0
- package/lib/ActorHttpFetch.d.ts +10 -0
- package/lib/ActorHttpFetch.js +56 -2
- package/lib/ActorHttpFetch.js.map +1 -0
- package/lib/FetchInitPreprocessor-browser.js.map +1 -0
- package/lib/FetchInitPreprocessor.js.map +1 -0
- package/lib/IFetchInitPreprocessor.js.map +1 -0
- package/lib/index.js.map +1 -0
- package/package.json +8 -7
|
@@ -142,6 +142,10 @@
|
|
|
142
142
|
"@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_test",
|
|
143
143
|
"memberFieldName": "test"
|
|
144
144
|
},
|
|
145
|
+
{
|
|
146
|
+
"@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_getResponse",
|
|
147
|
+
"memberFieldName": "getResponse"
|
|
148
|
+
},
|
|
145
149
|
{
|
|
146
150
|
"@id": "cahf:components/ActorHttpFetch.jsonld#ActorHttpFetch__member_run",
|
|
147
151
|
"memberFieldName": "run"
|
package/lib/ActorHttpFetch.d.ts
CHANGED
|
@@ -13,6 +13,16 @@ export declare class ActorHttpFetch extends ActorHttp {
|
|
|
13
13
|
constructor(args: IActorHttpFetchArgs);
|
|
14
14
|
static createUserAgent(): string;
|
|
15
15
|
test(action: IActionHttp): Promise<IMediatorTypeTime>;
|
|
16
|
+
/**
|
|
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
|
|
24
|
+
*/
|
|
25
|
+
private static getResponse;
|
|
16
26
|
run(action: IActionHttp): Promise<IActorHttpOutput>;
|
|
17
27
|
}
|
|
18
28
|
export interface IActorHttpFetchArgs extends IActorHttpArgs {
|
package/lib/ActorHttpFetch.js
CHANGED
|
@@ -24,6 +24,56 @@ class ActorHttpFetch extends bus_http_1.ActorHttp {
|
|
|
24
24
|
async test(action) {
|
|
25
25
|
return { time: Number.POSITIVE_INFINITY };
|
|
26
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
|
+
}
|
|
76
|
+
}
|
|
27
77
|
async run(action) {
|
|
28
78
|
// Prepare headers
|
|
29
79
|
const initHeaders = action.init?.headers ?? {};
|
|
@@ -62,10 +112,14 @@ class ActorHttpFetch extends bus_http_1.ActorHttp {
|
|
|
62
112
|
}
|
|
63
113
|
try {
|
|
64
114
|
requestInit = await this.fetchInitPreprocessor.handle(requestInit);
|
|
65
|
-
//
|
|
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;
|
|
66
119
|
const customFetch = action
|
|
67
120
|
.context?.get(context_entries_1.KeysHttp.fetch);
|
|
68
|
-
|
|
121
|
+
// Execute the fetch (with retries and timeouts, if applicable).
|
|
122
|
+
const response = await ActorHttpFetch.getResponse(customFetch || fetch, action.input, requestInit, retryCount, retryDelay, retryOnSeverError);
|
|
69
123
|
// We remove or update the timeout
|
|
70
124
|
if (requestTimeout !== undefined) {
|
|
71
125
|
const httpBodyTimeout = action.context?.get(context_entries_1.KeysHttp.httpBodyTimeout) || false;
|
|
@@ -0,0 +1 @@
|
|
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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 global.navigator === 'undefined' ?\n `Node.js ${process.version}; ${process.platform}` :\n `Browser-${global.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, 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, action.input, requestInit, retryCount, retryDelay, 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 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}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FetchIn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C;SAC7B;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 // eslint-disable-next-line no-constant-condition\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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FetchInitPreprocessor.js","sourceRoot":"","sources":["FetchInitPreprocessor.ts"],"names":[],"mappings":";;;AAAA,6CAA6C;AAC7C,+BAA0C;AAC1C,iCAA4C;AAC5C,iDAA+C;AAG/C;;GAEG;AACH,MAAa,qBAAqB;IAGhC,YAAmB,YAAiB;QAClC,MAAM,SAAS,GAAG,IAAI,YAAS,CAAC,YAAY,CAAC,CAAC;QAC9C,MAAM,UAAU,GAAG,IAAI,aAAU,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,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,WAAW,IAAU,IAAI,CAAC,IAAI,EAAE;YAChF,IAAI,CAAC,IAAI,GAAS,oBAAS,CAAC,cAAc,CAAO,IAAI,CAAC,IAAI,CAAC,CAAC;SAC7D;QAED,OAAa,EAAE,GAAG,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;IAC9C,CAAC;IAEM,KAAK,CAAC,qBAAqB;QAChC,kEAAkE;QAClE,0BAA0B;QAC1B,MAAM,eAAe,GAAG,MAAM,CAAC,eAAe,IAAI,2CAAa,kBAAkB,EAAC,CAAC;QACnF,OAAO,IAAI,eAAe,EAAE,CAAC;IAC/B,CAAC;CACF;AAxBD,sDAwBC;AACD,4CAA4C","sourcesContent":["/* eslint-disable import/no-nodejs-modules */\nimport { Agent as HttpAgent } from 'http';\nimport { Agent as HttpsAgent } from '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 if (init.body && typeof init.body !== 'string' && 'getReader' in <any> init.body) {\n init.body = <any> ActorHttp.toNodeReadable(<any> init.body);\n }\n\n return <any> { ...init, agent: this.agent };\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 = global.AbortController || await import('abort-controller');\n return new AbortController();\n }\n}\n/* eslint-enable import/no-nodejs-modules */\n"]}
|
|
@@ -0,0 +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"]}
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC","sourcesContent":["export * from './ActorHttpFetch';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@comunica/actor-http-fetch",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"description": "A node-fetch http actor",
|
|
5
5
|
"lsd:module": true,
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -26,14 +26,15 @@
|
|
|
26
26
|
"files": [
|
|
27
27
|
"components",
|
|
28
28
|
"lib/**/*.d.ts",
|
|
29
|
-
"lib/**/*.js"
|
|
29
|
+
"lib/**/*.js",
|
|
30
|
+
"lib/**/*.js.map"
|
|
30
31
|
],
|
|
31
32
|
"dependencies": {
|
|
32
|
-
"@comunica/bus-http": "^2.
|
|
33
|
-
"@comunica/context-entries": "^2.
|
|
34
|
-
"@comunica/mediatortype-time": "^2.
|
|
33
|
+
"@comunica/bus-http": "^2.5.1",
|
|
34
|
+
"@comunica/context-entries": "^2.5.1",
|
|
35
|
+
"@comunica/mediatortype-time": "^2.5.1",
|
|
35
36
|
"abort-controller": "^3.0.0",
|
|
36
|
-
"cross-fetch": "^3.
|
|
37
|
+
"cross-fetch": "^3.1.5"
|
|
37
38
|
},
|
|
38
39
|
"scripts": {
|
|
39
40
|
"build": "npm run build:ts && npm run build:components",
|
|
@@ -43,5 +44,5 @@
|
|
|
43
44
|
"browser": {
|
|
44
45
|
"./lib/FetchInitPreprocessor.js": "./lib/FetchInitPreprocessor-browser.js"
|
|
45
46
|
},
|
|
46
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "d64e87fad4a616224c210c2d22b38f6f9b00e1ea"
|
|
47
48
|
}
|