Skip to content

Commit

Permalink
feat: Add prettier formatter
Browse files Browse the repository at this point in the history
Reformat all files with prettier
  • Loading branch information
nielm committed May 21, 2024
1 parent 9f99e65 commit 5010a16
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 180 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/eslint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ jobs:
- name: ESlint
working-directory: cloudrun-malware-scanner/
run: npm run eslint

- name: Execute "npm run check-format"
working-directory: cloudrun-malware-scanner/
run: npm run check-format -- --log-level warn
8 changes: 8 additions & 0 deletions cloudrun-malware-scanner/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cvds
.next
next-env.d.ts
node_modules
yarn.lock
package-lock.json
public
*.md
62 changes: 62 additions & 0 deletions cloudrun-malware-scanner/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const shared = {
printWidth: 80,
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: true,
quoteProps: 'preserve',
bracketSpacing: false,
trailingComma: 'all',
arrowParens: 'always',
embeddedLanguageFormatting: 'off',
bracketSameLine: true,
singleAttributePerLine: false,
jsxSingleQuote: false,
htmlWhitespaceSensitivity: 'strict',
};

module.exports = {
overrides: [
{
/** TSX/TS/JS-specific configuration. */
files: '*.tsx',
options: shared,
},
{
files: '*.ts',
options: shared,
},
{
files: '*.js',
options: shared,
},
{
/** Sass-specific configuration. */
files: '*.scss',
options: {
singleQuote: true,
},
},
{
files: '*.html',
options: {
printWidth: 100,
},
},
{
files: '*.acx.html',
options: {
parser: 'angular',
singleQuote: true,
},
},
{
files: '*.ng.html',
options: {
parser: 'angular',
singleQuote: true,
printWidth: 100,
},
},
],
};
45 changes: 28 additions & 17 deletions cloudrun-malware-scanner/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const {Storage} = require('@google-cloud/storage');
const {logger} = require('./logger.js');
const pkgJson = require('./package.json');


/**
* Configuration object.
*
Expand All @@ -37,8 +36,9 @@ const pkgJson = require('./package.json');
*/
const Config = null;

const storage = new Storage({userAgent: `cloud-solutions/${
pkgJson.name}-usage-v${pkgJson.version}`});
const storage = new Storage({
userAgent: `cloud-solutions/${pkgJson.name}-usage-v${pkgJson.version}`,
});

/**
* Read configuration from JSON configuration file, verify
Expand All @@ -51,7 +51,6 @@ const storage = new Storage({userAgent: `cloud-solutions/${
async function readAndVerifyConfig(configFile) {
logger.info(`Using configuration file: ${configFile}`);


/** @type {Config} */
let config;

Expand All @@ -75,21 +74,32 @@ async function readAndVerifyConfig(configFile) {
for (let x = 0; x < config.buckets.length; x++) {
const buckets = config.buckets[x];
for (const bucketType of ['unscanned', 'clean', 'quarantined']) {
if (!(await checkBucketExists(
buckets[bucketType], `config.buckets[${x}].${bucketType}`))) {
if (
!(await checkBucketExists(
buckets[bucketType],
`config.buckets[${x}].${bucketType}`,
))
) {
success = false;
}
}
if (buckets.unscanned === buckets.clean ||
buckets.unscanned === buckets.quarantined ||
buckets.clean === buckets.quarantined) {
if (
buckets.unscanned === buckets.clean ||
buckets.unscanned === buckets.quarantined ||
buckets.clean === buckets.quarantined
) {
logger.fatal(
`Error in ${configFile} buckets[${x}]: bucket names are not unique`);
`Error in ${configFile} buckets[${x}]: bucket names are not unique`,
);
success = false;
}
}
if (!(await checkBucketExists(
config.ClamCvdMirrorBucket, 'ClamCvdMirrorBucket'))) {
if (
!(await checkBucketExists(
config.ClamCvdMirrorBucket,
'ClamCvdMirrorBucket',
))
) {
success = false;
}

Expand All @@ -99,7 +109,6 @@ async function readAndVerifyConfig(configFile) {
return config;
}


/**
* Check that given bucket exists. Returns true on success
*
Expand All @@ -117,12 +126,14 @@ async function checkBucketExists(bucketName, configName) {
// This is used in place of Bucket.exists() to avoid the need for
// Project/viewer permission.
try {
await storage.bucket(bucketName)
.getFiles({maxResults: 1, prefix: 'zzz', autoPaginate: false});
await storage
.bucket(bucketName)
.getFiles({maxResults: 1, prefix: 'zzz', autoPaginate: false});
return true;
} catch (e) {
logger.fatal(`Error in config: cannot view files in "${configName}" : ${
bucketName} : ${e.message}`);
logger.fatal(
`Error in config: cannot view files in "${configName}" : ${bucketName} : ${e.message}`,
);
logger.debug({err: e});
return false;
}
Expand Down
36 changes: 23 additions & 13 deletions cloudrun-malware-scanner/gcs-proxy-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const TOKEN_REFRESH_THRESHOLD_MILLIS = 60000;

const googleAuth = new GoogleAuth();


// access token for GCS requests - will be refreshed shortly before it expires
let accessToken;
let accessTokenRefreshTimeout;
Expand All @@ -44,19 +43,28 @@ async function accessTokenRefresh() {
}

const client = await googleAuth.getClient();
if (!client.credentials?.expiry_date ||
client.credentials.expiry_date <=
new Date().getTime() + TOKEN_REFRESH_THRESHOLD_MILLIS) {
if (
!client.credentials?.expiry_date ||
client.credentials.expiry_date <=
new Date().getTime() + TOKEN_REFRESH_THRESHOLD_MILLIS
) {
accessToken = await googleAuth.getAccessToken();
logger.info(`Refreshed Access token; expires at ${
new Date(client.credentials.expiry_date).toISOString()}`);
logger.info(
`Refreshed Access token; expires at ${new Date(
client.credentials.expiry_date,
).toISOString()}`,
);
}
const nextCheckDate =
new Date(client.credentials.expiry_date - TOKEN_REFRESH_THRESHOLD_MILLIS);
const nextCheckDate = new Date(
client.credentials.expiry_date - TOKEN_REFRESH_THRESHOLD_MILLIS,
);
logger.debug(
`Next access token refresh check at ${nextCheckDate.toISOString()}`);
`Next access token refresh check at ${nextCheckDate.toISOString()}`,
);
accessTokenRefreshTimeout = setTimeout(
accessTokenRefresh, nextCheckDate.getTime() - new Date().getTime());
accessTokenRefresh,
nextCheckDate.getTime() - new Date().getTime(),
);
}

/**
Expand All @@ -68,7 +76,8 @@ async function accessTokenRefresh() {
*/
function handleProxyError(err, req, res) {
logger.error(
`Failed to proxy to GCS for path ${req.url}, returning code 500: ${err}`);
`Failed to proxy to GCS for path ${req.url}, returning code 500: ${err}`,
);
res.writeHead(500, {
'Content-Type': 'text/plain',
});
Expand Down Expand Up @@ -114,8 +123,9 @@ async function setupGcsReverseProxy() {
const PROXY_PORT = process.env.PROXY_PORT || 8888;

proxy.listen(PROXY_PORT, 'localhost');
logger.info(`GCS authenticating reverse proxy listenting on port ${
PROXY_PORT} for requests to ${clamCvdMirrorBucket}`);
logger.info(
`GCS authenticating reverse proxy listenting on port ${PROXY_PORT} for requests to ${clamCvdMirrorBucket}`,
);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions cloudrun-malware-scanner/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,5 @@ const loggingBunyan = new LoggingBunyan({

exports.logger = bunyan.createLogger({
name: pkgJson.name,
streams: [
loggingBunyan.stream('info'),
],
streams: [loggingBunyan.stream('info')],
});
Loading

0 comments on commit 5010a16

Please sign in to comment.