Skip to content

Commit

Permalink
Merge pull request #114 from snyk/fix/network-tolerance
Browse files Browse the repository at this point in the history
[COD-571] fix: refactored API calls to make them tolerate network failures and retry
  • Loading branch information
Arvi3d authored Nov 8, 2021
2 parents d0cdb11 + 0d1c3a0 commit 097ed94
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 270 deletions.
8 changes: 3 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
"plugins": ["@typescript-eslint", "prettier", "import"],
"extends": [
"eslint:recommended",
"airbnb-base",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:prettier/recommended",
"prettier",
"prettier/@typescript-eslint"
"prettier"
],
"globals": {
"Atomics": "readonly",
Expand All @@ -29,14 +27,14 @@
"require-jsdoc": "off",
"space-before-function-paren": "off",
"comma-dangle": "off",
"object-curly-spacing": "warn",
"object-curly-spacing": ["error", "always"],
"padded-blocks": "off",
"camelcase": "warn",
"object-property-newline": "off",
"prefer-const": "warn",
"import/no-absolute-path": "off",
"no-prototype-builtins": "off",
"indent": "warn",
"indent": ["warn", 2],
"quote-props": ["warn", "as-needed"],
"lines-between-class-members": "off",
"prefer-destructuring": ["warn", {"object": true, "array": false}],
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"@typescript-eslint/eslint-plugin": "^4.31.2",
"@typescript-eslint/parser": "^4.0.1",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-prettier": "^4.0.0",
Expand All @@ -76,7 +75,6 @@
"multimatch": "^5.0.0",
"needle": "^3.0.0",
"p-map": "^3.0.0",
"queue": "^6.0.2",
"uuid": "^8.3.2",
"yaml": "^1.10.2"
}
Expand Down
31 changes: 14 additions & 17 deletions src/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
ConnectionOptions,
GetAnalysisOptions,
} from './http';
import { fromEntries } from './lib/utils';
import { createBundleFromFolders, FileBundle, remoteBundleFactory } from './bundles';
import { emitter } from './emitter';
import { AnalysisResult, AnalysisResultLegacy, AnalysisResultSarif, AnalysisFiles, Suggestion } from './interfaces/analysis-result.interface';
Expand Down Expand Up @@ -78,12 +77,11 @@ export async function analyzeBundle(options: GetAnalysisOptions): Promise<Analys

function normalizeResultFiles(files: AnalysisFiles, baseDir: string): AnalysisFiles {
if (baseDir) {
return fromEntries(
Object.entries(files).map(([path, positions]) => {
const filePath = resolveBundleFilePath(baseDir, path);
return [filePath, positions];
}),
);
return Object.entries(files).reduce((obj, [path, positions]) => {
const filePath = resolveBundleFilePath(baseDir, path);
obj[filePath] = positions;
return obj;
}, {});
}
return files;
}
Expand Down Expand Up @@ -216,11 +214,11 @@ const moveSuggestionIndexes = <T>(
suggestions: { [index: string]: T },
): { [index: string]: T } => {
const entries = Object.entries(suggestions);
return fromEntries(
entries.map(([i, s]) => {
return [`${parseInt(i, 10) + suggestionIndex + 1}`, s];
}),
);

return entries.reduce((obj, [i, s]) => {
obj[`${parseInt(i, 10) + suggestionIndex + 1}`] = s;
return obj;
}, {});
};

function mergeLegacyResults(
Expand All @@ -241,11 +239,10 @@ function mergeLegacyResults(
const newSuggestions = moveSuggestionIndexes<Suggestion>(suggestionIndex, newAnalysisResults.suggestions);
const suggestions = { ...oldAnalysisResults.suggestions, ...newSuggestions };

const newFiles = fromEntries(
Object.entries(newAnalysisResults.files).map(([fn, s]) => {
return [fn, moveSuggestionIndexes(suggestionIndex, s)];
}),
);
const newFiles = Object.entries(newAnalysisResults.files).reduce((obj, [fn, s]) => {
obj[fn] = moveSuggestionIndexes(suggestionIndex, s);
return obj;
}, {});

// expand relative file names to absolute ones only for legacy results
const changedFiles = [...limitToFiles, ...removedFiles].map(path => resolveBundleFilePath(baseDir, path));
Expand Down
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ export enum ErrorCodes {
timeout = 504,
}

export const NETWORK_ERRORS = {
ETIMEDOUT: ErrorCodes.timeout,
ECONNREFUSED: ErrorCodes.connectionRefused,
ECONNRESET: ErrorCodes.connectionRefused,
ENETUNREACH: ErrorCodes.connectionRefused,
ENOTFOUND: ErrorCodes.dnsNotFound,
};

export const DEFAULT_ERROR_MESSAGES: { [P in ErrorCodes]: string } = {
[ErrorCodes.serverError]: 'Unexpected server error', // 500
[ErrorCodes.badGateway]: 'Bad gateway', // 502
Expand Down
Loading

0 comments on commit 097ed94

Please sign in to comment.