Skip to content

Commit

Permalink
feat: request anomaly detection (#135)
Browse files Browse the repository at this point in the history
Co-authored-by: Snazzah <7025343+Snazzah@users.noreply.github.com>
  • Loading branch information
blackmad and Snazzah authored Dec 4, 2023
1 parent 9c806ad commit 8612128
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"deepscan.enable": true,
"mochaExplorer.files": "test/**/*.ts",
"mochaExplorer.require": "ts-node/register"
"mochaExplorer.require": "ts-node/register",
"editor.formatOnSave": false
}
2 changes: 2 additions & 0 deletions src/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)[];

Expand Down
1 change: 1 addition & 0 deletions test/__util__/data/search/anomalyDetectionBlock.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down

0 comments on commit 8612128

Please sign in to comment.