diff --git a/.vscode/settings.json b/.vscode/settings.json index d86cdf6..3429b2e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "deepscan.enable": true, "mochaExplorer.files": "test/**/*.ts", - "mochaExplorer.require": "ts-node/register" + "mochaExplorer.require": "ts-node/register", + "editor.formatOnSave": false } diff --git a/src/search/search.ts b/src/search/search.ts index 3cf76f4..72b4c8a 100644 --- a/src/search/search.ts +++ b/src/search/search.ts @@ -162,6 +162,8 @@ export async function search(query: string, options?: SearchOptions, needleOptio const response = await needle('get', `https://links.duckduckgo.com/d.js?${queryString(queryObject)}`, needleOptions); if ((response.body as string).includes('DDG.deep.is506')) throw new Error('A server error occurred!'); + if (response.body.toString().includes('DDG.deep.anomalyDetectionBlock')) + throw new Error('DDG detected an anomaly in the request, you are likely making requests too quickly.'); const searchResults = JSON.parse(SEARCH_REGEX.exec(response.body)![1].replace(/\t/g, ' ')) as (CallbackSearchResult | CallbackNextSearch)[]; diff --git a/test/__util__/data/search/anomalyDetectionBlock.js b/test/__util__/data/search/anomalyDetectionBlock.js new file mode 100644 index 0000000..34132ef --- /dev/null +++ b/test/__util__/data/search/anomalyDetectionBlock.js @@ -0,0 +1 @@ +DDG.deep.anomalyDetectionBlock({gk:'nac7b3a5f507fb907b94049aa855db990',q:'Friche%20la%20Belle%20de%20Mai',iadb:1,sc:1,e:0,p:'80-386-283-234-262-245-251-127-263',cc:'sre',o:'lTCdmsFCzbUSUhPvj7hnXN%2BlaWGPLfIPMY%2F0kNT0Tzw%3D%0A',er:'',s:'index', r:'euw'}) \ No newline at end of file diff --git a/test/search/search.ts b/test/search/search.ts index 1d998e4..2a4ff09 100644 --- a/test/search/search.ts +++ b/test/search/search.ts @@ -189,6 +189,16 @@ describe('search/search', () => { await expect(search('node', { vqd: SEARCH_QUERY_VQD })).to.eventually.be.rejectedWith(Error, 'A server error occurred!'); scope.done(); }); + + it('should throw on server anomalies', async () => { + const scope = makeSearchNock(DEFAULT_QUERY, 'anomalyDetectionBlock.js'); + + await expect(search('node', { vqd: SEARCH_QUERY_VQD })).to.eventually.be.rejectedWith( + Error, + 'DDG detected an anomaly in the request, you are likely making requests too quickly.' + ); + scope.done(); + }); }); describe('autocomplete()', () => {