fix(core): update
This commit is contained in:
parent
c9cb7f8057
commit
a0f1590a8f
6
package-lock.json
generated
6
package-lock.json
generated
@ -1450,6 +1450,12 @@
|
|||||||
"integrity": "sha1-ICtIAhoMTL3i34DeFaF0Q8i0OYA=",
|
"integrity": "sha1-ICtIAhoMTL3i34DeFaF0Q8i0OYA=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"node-fetch": {
|
||||||
|
"version": "2.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.3.0.tgz",
|
||||||
|
"integrity": "sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"object-assign": {
|
"object-assign": {
|
||||||
"version": "4.1.1",
|
"version": "4.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
"@pushrocks/smartexpress": "^2.0.4",
|
"@pushrocks/smartexpress": "^2.0.4",
|
||||||
"@pushrocks/tapbundle": "^3.0.7",
|
"@pushrocks/tapbundle": "^3.0.7",
|
||||||
"@types/node": "^10.11.7",
|
"@types/node": "^10.11.7",
|
||||||
|
"node-fetch": "^2.3.0",
|
||||||
"tslint": "^5.11.0",
|
"tslint": "^5.11.0",
|
||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
|
44
test/test.ts
44
test/test.ts
@ -1,5 +1,16 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as webrequest from '../ts/index';
|
import * as webrequest from '../ts/index';
|
||||||
|
import * as fetch from 'node-fetch';
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
namespace NodeJS {
|
||||||
|
interface Global {
|
||||||
|
fetch: any;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
global.fetch = fetch;
|
||||||
|
|
||||||
|
|
||||||
// test dependencies
|
// test dependencies
|
||||||
import * as smartexpress from '@pushrocks/smartexpress';
|
import * as smartexpress from '@pushrocks/smartexpress';
|
||||||
@ -10,7 +21,7 @@ tap.test('setup test server', async () => {
|
|||||||
testServer = new smartexpress.Server({
|
testServer = new smartexpress.Server({
|
||||||
cors: false,
|
cors: false,
|
||||||
forceSsl: false,
|
forceSsl: false,
|
||||||
port: 1234
|
port: 2345
|
||||||
});
|
});
|
||||||
|
|
||||||
testServer.addRoute('/apiroute1', new smartexpress.Handler("GET", (req, res) => {
|
testServer.addRoute('/apiroute1', new smartexpress.Handler("GET", (req, res) => {
|
||||||
@ -18,11 +29,38 @@ tap.test('setup test server', async () => {
|
|||||||
res.end();
|
res.end();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
testServer
|
testServer.addRoute('/apiroute2', new smartexpress.Handler("GET", (req, res) => {
|
||||||
|
res.status(500);
|
||||||
|
res.end();
|
||||||
|
}));
|
||||||
|
|
||||||
|
testServer.addRoute('/apiroute3', new smartexpress.Handler("GET", (req, res) => {
|
||||||
|
res.status(200);
|
||||||
|
res.send({
|
||||||
|
hithere: 'hi'
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
await testServer.start()
|
||||||
})
|
})
|
||||||
|
|
||||||
tap.test('first test', async (tools) => {
|
tap.test('first test', async (tools) => {
|
||||||
console.log(webrequest)
|
const response = await (new webrequest.WebRequest()).request([
|
||||||
|
'http://localhost:2345/apiroute1',
|
||||||
|
'http://localhost:2345/apiroute2',
|
||||||
|
'http://localhost:2345/apiroute4',
|
||||||
|
'http://localhost:2345/apiroute3'
|
||||||
|
], {
|
||||||
|
method: 'GET'
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(response);
|
||||||
|
|
||||||
|
expect(response).property('hithere').to.equal('hi');
|
||||||
|
})
|
||||||
|
|
||||||
|
tap.test('tear down server', async () => {
|
||||||
|
testServer.stop();
|
||||||
})
|
})
|
||||||
|
|
||||||
tap.start()
|
tap.start()
|
||||||
|
33
ts/index.ts
33
ts/index.ts
@ -1,7 +1,5 @@
|
|||||||
import * as plugins from './webrequest.plugins';
|
import * as plugins from './webrequest.plugins';
|
||||||
|
|
||||||
type TRequestHistoryEntry = 'timedout' | '429' | '5xx';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* web request
|
* web request
|
||||||
*/
|
*/
|
||||||
@ -40,21 +38,24 @@ export class WebRequest {
|
|||||||
allUrls = [urlArg];
|
allUrls = [urlArg];
|
||||||
}
|
}
|
||||||
|
|
||||||
const requestHistory: TRequestHistoryEntry[] = []; // keep track of the request history
|
const requestHistory: string[] = []; // keep track of the request history
|
||||||
|
|
||||||
const doHistoryCheck = async ( // check history for a
|
const doHistoryCheck = async ( // check history for a
|
||||||
historyEntryTypeArg: TRequestHistoryEntry
|
historyEntryTypeArg: string
|
||||||
) => {
|
) => {
|
||||||
requestHistory.push(historyEntryTypeArg);
|
requestHistory.push(historyEntryTypeArg);
|
||||||
await plugins.smartdelay.delayFor(
|
if (historyEntryTypeArg === '429') {
|
||||||
Math.floor(Math.random() * (2000 - 1000 +1)) + 1000
|
console.log('got 429, so waiting a little bit.')
|
||||||
); // wait between 1 and 10 seconds
|
await plugins.smartdelay.delayFor(
|
||||||
|
Math.floor(Math.random() * (2000 - 1000 +1)) + 1000
|
||||||
|
); // wait between 1 and 10 seconds
|
||||||
|
}
|
||||||
|
|
||||||
let numOfHistoryType = 0;
|
let numOfHistoryType = 0;
|
||||||
for (const entry of requestHistory) {
|
for (const entry of requestHistory) {
|
||||||
if (entry === historyEntryTypeArg) numOfHistoryType++;
|
if (entry === historyEntryTypeArg) numOfHistoryType++;
|
||||||
}
|
}
|
||||||
if (numOfHistoryType > 2 * allUrls.length * usedUrlIndex) {
|
if (numOfHistoryType > (2 * allUrls.length * usedUrlIndex)) {
|
||||||
usedUrlIndex++;
|
usedUrlIndex++;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -64,23 +65,25 @@ export class WebRequest {
|
|||||||
if (!urlToUse) {
|
if (!urlToUse) {
|
||||||
throw new Error('request failed permanently');
|
throw new Error('request failed permanently');
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(urlToUse, {
|
const response = await fetch(urlToUse, {
|
||||||
method: optionsArg.method,
|
method: optionsArg.method,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
console.log(`${urlToUse} answers with status: ${response.status}`);
|
||||||
|
|
||||||
if (response.status >= 200 && response.status < 300) {
|
if (response.status >= 200 && response.status < 300) {
|
||||||
return JSON.parse(await response.text());
|
return response;
|
||||||
} else if (response.status === 429) {
|
} else {
|
||||||
await doHistoryCheck('429');
|
await doHistoryCheck(response.status.toString());
|
||||||
return await doRequest(allUrls[usedUrlIndex]);
|
|
||||||
} else if (response.status >= 500 && response.status < 600) {
|
|
||||||
await doHistoryCheck('5xx');
|
|
||||||
return await doRequest(allUrls[usedUrlIndex]);
|
return await doRequest(allUrls[usedUrlIndex]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const finalResponse = await doRequest(urlArg[usedUrlIndex]);
|
const finalResponse: Response = await doRequest(urlArg[usedUrlIndex]);
|
||||||
|
console.log(finalResponse)
|
||||||
|
return JSON.parse(await finalResponse.text());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user