@comunica/actor-http-fetch 2.6.7 → 2.6.9
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/lib/ActorHttpFetch.js
CHANGED
|
@@ -17,9 +17,9 @@ class ActorHttpFetch extends bus_http_1.ActorHttp {
|
|
|
17
17
|
this.fetchInitPreprocessor = new FetchInitPreprocessor_1.FetchInitPreprocessor(args.agentOptions);
|
|
18
18
|
}
|
|
19
19
|
static createUserAgent() {
|
|
20
|
-
return `Comunica/actor-http-fetch (${typeof
|
|
20
|
+
return `Comunica/actor-http-fetch (${typeof globalThis.navigator === 'undefined' ?
|
|
21
21
|
`Node.js ${process.version}; ${process.platform}` :
|
|
22
|
-
`Browser-${
|
|
22
|
+
`Browser-${globalThis.navigator.userAgent}`})`;
|
|
23
23
|
}
|
|
24
24
|
async test(action) {
|
|
25
25
|
return { time: Number.POSITIVE_INFINITY };
|
|
@@ -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,IAAI,CAAC,SAAS,GAAG,cAAc,CAAC,eAAe,EAAE,CAAC;QAClD,IAAI,CAAC,qBAAqB,GAAG,IAAI,6CAAqB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAC5E,CAAC;IAEM,MAAM,CAAC,eAAe;QAC3B,OAAO,8BAA8B,OAAO,MAAM,CAAC,SAAS,KAAK,WAAW,CAAC,CAAC;YAC5E,WAAW,OAAO,CAAC,OAAO,KAAK,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;YACnD,WAAW,MAAM,CAAC,SAAS,CAAC,SAAS,EAAE,GAA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QAAQ,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE;gBAC1C,QAAQ,CAAC,IAAI,CAAC,MAAM,GAAG,KAAK,EAAC,KAAa,EAAE,EAAE;oBAC5B,QAAQ,CAAC,IAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;oBAC9C,IAAI,cAAc,KAAK,SAAS,EAAE;wBAChC,4DAA4D;wBAC5D,YAAY,CAAC,cAAc,CAAC,CAAC;qBAC9B;gBACH,CAAC,CAAC;aACH;YAED,OAAO,QAAQ,CAAC;SACjB;QAAC,OAAO,KAAc,EAAE;YACvB,IAAI,cAAc,KAAK,SAAS,EAAE;gBAChC,YAAY,CAAC,cAAc,CAAC,CAAC;aAC9B;YACD,MAAM,KAAK,CAAC;SACb;IACH,CAAC;CACF;AAxKD,wCAwKC","sourcesContent":["import type { IActionHttp, IActorHttpOutput, IActorHttpArgs } from '@comunica/bus-http';\nimport { ActorHttp } from '@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"]}
|
|
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,IAAI,CAAC,SAAS,GAAG,cAAc,CAAC,eAAe,EAAE,CAAC;QAClD,IAAI,CAAC,qBAAqB,GAAG,IAAI,6CAAqB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAC5E,CAAC;IAEM,MAAM,CAAC,eAAe;QAC3B,OAAO,8BAA8B,OAAO,UAAU,CAAC,SAAS,KAAK,WAAW,CAAC,CAAC;YAChF,WAAW,OAAO,CAAC,OAAO,KAAK,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;YACnD,WAAW,UAAU,CAAC,SAAS,CAAC,SAAS,EAAE,GAAG,CAAC;IACnD,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,M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AC,MAAM,EAAE;gBAC1C,QAAQ,CAAC,IAAI,CAAC,MAAM,GAAG,KAAK,EAAC,KAAa,EAAE,EAAE;oBAC5B,QAAQ,CAAC,IAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;oBAC9C,IAAI,cAAc,KAAK,SAAS,EAAE;wBAChC,4DAA4D;wBAC5D,YAAY,CAAC,cAAc,CAAC,CAAC;qBAC9B;gBACH,CAAC,CAAC;aACH;YAED,OAAO,QAAQ,CAAC;SACjB;QAAC,OAAO,KAAc,EAAE;YACvB,IAAI,cAAc,KAAK,SAAS,EAAE;gBAChC,YAAY,CAAC,cAAc,CAAC,CAAC;aAC9B;YACD,MAAM,KAAK,CAAC;SACb;IACH,CAAC;CACF;AAxKD,wCAwKC","sourcesContent":["import type { IActionHttp, IActorHttpOutput, IActorHttpArgs } from '@comunica/bus-http';\nimport { ActorHttp } from '@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, 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"]}
|
|
@@ -24,7 +24,7 @@ class FetchInitPreprocessor {
|
|
|
24
24
|
async createAbortController() {
|
|
25
25
|
// Fallback to abort-controller for Node 14 backward compatibility
|
|
26
26
|
/* istanbul ignore next */
|
|
27
|
-
const AbortController =
|
|
27
|
+
const AbortController = globalThis.AbortController || await Promise.resolve().then(() => require('abort-controller'));
|
|
28
28
|
return new AbortController();
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -1 +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,SA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Aa,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,UAAU,CAAC,eAAe,IAAI,2CAAa,kBAAkB,EAAC,CAAC;QACvF,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 = globalThis.AbortController || await import('abort-controller');\n return new AbortController();\n }\n}\n/* eslint-enable import/no-nodejs-modules */\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@comunica/actor-http-fetch",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.9",
|
|
4
4
|
"description": "A node-fetch http actor",
|
|
5
5
|
"lsd:module": true,
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"lib/**/*.js.map"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@comunica/bus-http": "^2.6.
|
|
34
|
-
"@comunica/context-entries": "^2.6.
|
|
35
|
-
"@comunica/mediatortype-time": "^2.6.
|
|
33
|
+
"@comunica/bus-http": "^2.6.9",
|
|
34
|
+
"@comunica/context-entries": "^2.6.8",
|
|
35
|
+
"@comunica/mediatortype-time": "^2.6.8",
|
|
36
36
|
"abort-controller": "^3.0.0",
|
|
37
37
|
"cross-fetch": "^3.1.5"
|
|
38
38
|
},
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"browser": {
|
|
45
45
|
"./lib/FetchInitPreprocessor.js": "./lib/FetchInitPreprocessor-browser.js"
|
|
46
46
|
},
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "739f55dbe1b95743866f4e393ffe7a62f0410436"
|
|
48
48
|
}
|