fix(core): update
This commit is contained in:
parent
f4374942ad
commit
14cca11c55
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/webrequest',
|
||||
version: '3.0.6',
|
||||
version: '3.0.7',
|
||||
description: 'securely request from browsers'
|
||||
}
|
||||
|
14
ts/index.ts
14
ts/index.ts
@ -49,7 +49,7 @@ export class WebRequest {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
body: plugins.smartjson.stringify(requestBody),
|
||||
method: 'POST',
|
||||
useCache: useCacheArg
|
||||
useCache: useCacheArg,
|
||||
});
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
@ -103,12 +103,12 @@ export class WebRequest {
|
||||
let cachedResponseDeferred = plugins.smartpromise.defer<Response>();
|
||||
let cacheUsed = false;
|
||||
if (optionsArg.useCache && this.cacheStore.check(urlArg)) {
|
||||
const responseBuffer: ArrayBuffer = await this.cacheStore.get(urlArg)
|
||||
const responseBuffer: ArrayBuffer = await this.cacheStore.get(urlArg);
|
||||
cachedResponseDeferred.resolve(new Response(responseBuffer, {}));
|
||||
} else {
|
||||
cachedResponseDeferred.resolve(null);
|
||||
}
|
||||
const response: Response = await fetch(urlArg, {
|
||||
let response: Response = await fetch(urlArg, {
|
||||
signal: controller.signal,
|
||||
method: optionsArg.method,
|
||||
headers: {
|
||||
@ -116,8 +116,8 @@ export class WebRequest {
|
||||
...(optionsArg.headers || {}),
|
||||
},
|
||||
body: optionsArg.body,
|
||||
}).catch(async err => {
|
||||
if (optionsArg.useCache && await cachedResponseDeferred.promise) {
|
||||
}).catch(async (err) => {
|
||||
if (optionsArg.useCache && (await cachedResponseDeferred.promise)) {
|
||||
cacheUsed = true;
|
||||
const cachedResponse = cachedResponseDeferred.promise;
|
||||
return cachedResponse;
|
||||
@ -125,6 +125,10 @@ export class WebRequest {
|
||||
return err;
|
||||
}
|
||||
});
|
||||
if (optionsArg.useCache && (await cachedResponseDeferred.promise) && response.status === 500) {
|
||||
cacheUsed = true;
|
||||
response = await cachedResponseDeferred.promise;
|
||||
}
|
||||
if (!cacheUsed && optionsArg.useCache && response.status < 300) {
|
||||
const buffer = await response.clone().arrayBuffer();
|
||||
await this.cacheStore.set(urlArg, buffer);
|
||||
|
Loading…
Reference in New Issue
Block a user