From a5b0a98b3f0873b9375a04415abd4e9bf1e9f434 Mon Sep 17 00:00:00 2001 From: Yusuf Yilmaz Date: Mon, 6 Jun 2022 19:44:11 +0200 Subject: [PATCH] add prettier --- package.json | 3 ++- src/constants/index.ts | 2 +- src/global.d.ts | 9 +++---- src/lib/document-handler/builder.ts | 4 +-- src/lib/document-handler/index.ts | 10 +++---- src/lib/document-stores/amazon-s3.ts | 8 +++--- src/lib/document-stores/file.ts | 4 +-- src/lib/document-stores/google-datastore.ts | 24 ++++++++--------- src/lib/document-stores/memcached.ts | 6 ++--- src/lib/document-stores/mongo.ts | 24 ++++++++--------- src/lib/document-stores/postgres.ts | 14 +++++----- src/lib/document-stores/redis.ts | 20 +++++++------- src/lib/document-stores/rethinkdb.ts | 2 +- src/lib/helpers/config.ts | 2 +- src/lib/key-generators/builder.ts | 3 ++- src/server.ts | 30 ++++++++++----------- src/types/config.ts | 1 - src/types/log.ts | 18 ++++++------- src/types/store.ts | 2 +- 19 files changed, 94 insertions(+), 92 deletions(-) diff --git a/package.json b/package.json index d129109..73896f3 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "dev": "nodemon src/server.ts", "start": "node dist/src/server.js", "lint": "eslint src --fix", - "types:check": "tsc --noEmit --pretty" + "types:check": "tsc --noEmit --pretty", + "pretty": "prettier --write src" } } diff --git a/src/constants/index.ts b/src/constants/index.ts index 035bb1e..cc8a23b 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1,5 +1,5 @@ const DEFAULT_KEY_LENGTH = 10 export default { - DEFAULT_KEY_LENGTH, + DEFAULT_KEY_LENGTH } diff --git a/src/global.d.ts b/src/global.d.ts index 9bc7e70..9e8d4d4 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -30,11 +30,11 @@ declare module 'rethinkdbdash' { declare module 'connect-ratelimit' { function connectRateLimit( - as: RateLimits, + as: RateLimits ): ( req: express.Request, res: express.Response, - next: express.NextFunction, + next: express.NextFunction ) => void export = connectRateLimit @@ -47,11 +47,10 @@ declare namespace Express { } declare module 'st' { - type ConnectSt = { path: string - content: { maxAge : number } - passthrough? : boolean + content: { maxAge: number } + passthrough?: boolean index: boolean | string } diff --git a/src/lib/document-handler/builder.ts b/src/lib/document-handler/builder.ts index 285ea0f..7cc9f19 100644 --- a/src/lib/document-handler/builder.ts +++ b/src/lib/document-handler/builder.ts @@ -1,7 +1,7 @@ import buildGenerator from 'src/lib/key-generators/builder' import type { Config } from 'src/types/config' import buildStore from 'src/lib/document-stores/builder' -import DocumentHandler from "./index" +import DocumentHandler from './index' const build = async (config: Config) => { const storage = await buildStore(config) @@ -12,7 +12,7 @@ const build = async (config: Config) => { config, maxLength: config.maxLength, keyLength: config.keyLength, - keyGenerator, + keyGenerator }) return documentHandler diff --git a/src/lib/document-handler/index.ts b/src/lib/document-handler/index.ts index 612ff42..e375d1b 100644 --- a/src/lib/document-handler/index.ts +++ b/src/lib/document-handler/index.ts @@ -51,7 +51,7 @@ class DocumentHandler { } } }, - skipExpire, + skipExpire ) } @@ -68,7 +68,7 @@ class DocumentHandler { winston.warn('document >maxLength', { maxLength: this.maxLength }) response.writeHead(400, { 'content-type': 'application/json' }) response.end( - JSON.stringify({ message: 'Document exceeds maximum length.' }), + JSON.stringify({ message: 'Document exceeds maximum length.' }) ) return } @@ -131,7 +131,7 @@ class DocumentHandler { if (ret) { winston.verbose('retrieved raw document', { key }) response.writeHead(200, { - 'content-type': 'text/plain; charset=UTF-8', + 'content-type': 'text/plain; charset=UTF-8' }) if (request.method === 'HEAD') { response.end() @@ -148,7 +148,7 @@ class DocumentHandler { } } }, - skipExpire, + skipExpire ) } @@ -166,7 +166,7 @@ class DocumentHandler { callback(key) } }, - true, + true ) // Don't bump expirations when key searching } diff --git a/src/lib/document-stores/amazon-s3.ts b/src/lib/document-stores/amazon-s3.ts index 061443f..0414990 100644 --- a/src/lib/document-stores/amazon-s3.ts +++ b/src/lib/document-stores/amazon-s3.ts @@ -22,7 +22,7 @@ class AmazonS3DocumentStore implements Store { get = ( key: string, callback: Callback, - skipExpire?: boolean | undefined, + skipExpire?: boolean | undefined ): void => { if (!this.bucket) { callback(false) @@ -31,7 +31,7 @@ class AmazonS3DocumentStore implements Store { const req = { Bucket: this.bucket, - Key: key, + Key: key } this.client.getObject(req, (err, data) => { @@ -50,7 +50,7 @@ class AmazonS3DocumentStore implements Store { key: string, data: string, callback: Callback, - skipExpire?: boolean | undefined, + skipExpire?: boolean | undefined ): void => { if (!this.bucket) { callback(false) @@ -61,7 +61,7 @@ class AmazonS3DocumentStore implements Store { Bucket: this.bucket, Key: key, Body: data as AWS.S3.PutObjectOutput, - ContentType: 'text/plain', + ContentType: 'text/plain' } this.client.putObject(req, err => { diff --git a/src/lib/document-stores/file.ts b/src/lib/document-stores/file.ts index bc6caaf..1e7a8c4 100644 --- a/src/lib/document-stores/file.ts +++ b/src/lib/document-stores/file.ts @@ -33,7 +33,7 @@ class FileDocumentStore implements Store { get = ( key: string, callback: Callback, - skipExpire?: boolean | undefined, + skipExpire?: boolean | undefined ): void => { const fn = `${this.basePath}/${md5(key)}` fs.readFile(fn, 'utf8', (err, data) => { @@ -54,7 +54,7 @@ class FileDocumentStore implements Store { key: string, data: string, callback: Callback, - skipExpire?: boolean | undefined, + skipExpire?: boolean | undefined ): void => { try { fs.mkdir(this.basePath, '700', () => { diff --git a/src/lib/document-stores/google-datastore.ts b/src/lib/document-stores/google-datastore.ts index fb2c731..321b051 100644 --- a/src/lib/document-stores/google-datastore.ts +++ b/src/lib/document-stores/google-datastore.ts @@ -26,7 +26,7 @@ class GoogleDatastoreDocumentStore implements Store { key: PathType, data: string, callback: Callback, - skipExpire?: boolean, + skipExpire?: boolean ) => { const expireTime = skipExpire || this.expire === undefined @@ -40,13 +40,13 @@ class GoogleDatastoreDocumentStore implements Store { { name: 'value', value: data, - excludeFromIndexes: true, + excludeFromIndexes: true }, { name: 'expiration', - value: expireTime, - }, - ], + value: expireTime + } + ] } this.datastore @@ -72,7 +72,7 @@ class GoogleDatastoreDocumentStore implements Store { winston.info('document expired', { key, expiration: entity[0].expiration, - check: new Date(), + check: new Date() }) callback(false) } else { @@ -83,15 +83,15 @@ class GoogleDatastoreDocumentStore implements Store { { name: 'value', value: entity[0].value, - excludeFromIndexes: true, + excludeFromIndexes: true }, { name: 'expiration', value: new Date( - Date.now() + (this.expire ? this.expire * 1000 : 0), - ), - }, - ], + Date.now() + (this.expire ? this.expire * 1000 : 0) + ) + } + ] } this.datastore .update(task) @@ -104,7 +104,7 @@ class GoogleDatastoreDocumentStore implements Store { }) .catch(err => { winston.error('Error retrieving value from Google Datastore', { - error: err, + error: err }) callback(false) }) diff --git a/src/lib/document-stores/memcached.ts b/src/lib/document-stores/memcached.ts index bbce7ea..d7b90fb 100644 --- a/src/lib/document-stores/memcached.ts +++ b/src/lib/document-stores/memcached.ts @@ -36,7 +36,7 @@ class MemcachedDocumentStore implements Store { get = ( key: string, callback: Callback, - skipExpire?: boolean | undefined, + skipExpire?: boolean | undefined ): void => { this.client?.get(key, (error, data: string) => { const value = error ? false : data @@ -53,7 +53,7 @@ class MemcachedDocumentStore implements Store { winston.error('failed to update expiration on GET', { key }) } }, - skipExpire, + skipExpire ) } }) @@ -64,7 +64,7 @@ class MemcachedDocumentStore implements Store { key: string, data: string, callback: Callback, - skipExpire?: boolean | undefined, + skipExpire?: boolean | undefined ): void => { this.client?.set(key, data, skipExpire ? 0 : this.expire || 0, error => { callback(!error) diff --git a/src/lib/document-stores/mongo.ts b/src/lib/document-stores/mongo.ts index 9e58014..d6582cc 100644 --- a/src/lib/document-stores/mongo.ts +++ b/src/lib/document-stores/mongo.ts @@ -33,7 +33,7 @@ class MongoDocumentStore implements Store { get = ( key: string, callback: Callback, - skipExpire?: boolean | undefined, + skipExpire?: boolean | undefined ): void => { const now = Math.floor(new Date().getTime() / 1000) @@ -46,7 +46,7 @@ class MongoDocumentStore implements Store { .findOne( { entry_id: key, - $or: [{ expiration: -1 }, { expiration: { $gt: now } }], + $or: [{ expiration: -1 }, { expiration: { $gt: now } }] }, (error?: Error, entry?) => { if (error) { @@ -67,20 +67,20 @@ class MongoDocumentStore implements Store { .collection('entries') .update( { - entry_id: key, + entry_id: key }, { $set: { - expiration: this.expire + now, - }, + expiration: this.expire + now + } }, {}, - () => {}, + () => {} ) } return true - }, + } ) }) } @@ -89,7 +89,7 @@ class MongoDocumentStore implements Store { key: string, data: string, callback: Callback, - skipExpire?: boolean | undefined, + skipExpire?: boolean | undefined ): void => { const now = Math.floor(new Date().getTime() / 1000) @@ -102,15 +102,15 @@ class MongoDocumentStore implements Store { .update( { entry_id: key, - $or: [{ expiration: -1 }, { expiration: { $gt: now } }], + $or: [{ expiration: -1 }, { expiration: { $gt: now } }] }, { entry_id: key, value: data, - expiration: this.expire && !skipExpire ? this.expire + now : -1, + expiration: this.expire && !skipExpire ? this.expire + now : -1 }, { - upsert: true, + upsert: true }, (error?: Error) => { if (error) { @@ -119,7 +119,7 @@ class MongoDocumentStore implements Store { } return callback(true) - }, + } ) }) } diff --git a/src/lib/document-stores/postgres.ts b/src/lib/document-stores/postgres.ts index 5aab2f4..7f84e09 100644 --- a/src/lib/document-stores/postgres.ts +++ b/src/lib/document-stores/postgres.ts @@ -7,7 +7,7 @@ import type { PostgresStoreConfig } from 'src/types/config' type ConnectCallback = ( error?: Error, client?: PoolClient, - done?: () => void, + done?: () => void ) => void // A postgres document store @@ -42,7 +42,7 @@ class PostgresDocumentStore implements Store { get = ( key: string, callback: Callback, - skipExpire?: boolean | undefined, + skipExpire?: boolean | undefined ): void => { const now = Math.floor(new Date().getTime() / 1000) this.safeConnect((err, client, done): void => { @@ -56,7 +56,7 @@ class PostgresDocumentStore implements Store { (error: Error, result) => { if (error) { winston.error('error retrieving value from postgres', { - error, + error }) return callback(false) } @@ -71,12 +71,12 @@ class PostgresDocumentStore implements Store { } return callback(false) - }, + } ) } return done?.() - }, + } ) }) } @@ -86,7 +86,7 @@ class PostgresDocumentStore implements Store { key: string, data: string, callback: Callback, - skipExpire?: boolean | undefined, + skipExpire?: boolean | undefined ): void => { const now = Math.floor(new Date().getTime() / 1000) this.safeConnect((err, client, done) => { @@ -103,7 +103,7 @@ class PostgresDocumentStore implements Store { } callback(true) return done?.() - }, + } ) }) } diff --git a/src/lib/document-stores/redis.ts b/src/lib/document-stores/redis.ts index 9da621d..b63ad80 100644 --- a/src/lib/document-stores/redis.ts +++ b/src/lib/document-stores/redis.ts @@ -35,18 +35,20 @@ class RedisDocumentStore implements Store { const port = options.port || 6379 const index = options.db || 0 - const connectionParameters = url ? { - url - }: { - host, - port - } + const connectionParameters = url + ? { + url + } + : { + host, + port + } const config = { ...connectionParameters, database: index as number, ...(options.username ? { username: options.username } : {}), - ...(options.password ? { username: options.username } : {}), + ...(options.password ? { username: options.username } : {}) } this.client = createClient(config) @@ -63,7 +65,7 @@ class RedisDocumentStore implements Store { }) .catch(err => { winston.error(`error connecting to redis index ${index}`, { - error: err, + error: err }) process.exit(1) }) @@ -86,7 +88,7 @@ class RedisDocumentStore implements Store { key: string, data: string, callback: Callback, - skipExpire?: boolean | undefined, + skipExpire?: boolean | undefined ): void => { this.client ?.set(key, data, this.getExpire(skipExpire)) diff --git a/src/lib/document-stores/rethinkdb.ts b/src/lib/document-stores/rethinkdb.ts index 069e55b..d714461 100644 --- a/src/lib/document-stores/rethinkdb.ts +++ b/src/lib/document-stores/rethinkdb.ts @@ -22,7 +22,7 @@ class RethinkDBStore { port: options.port || 28015, db: options.db || 'haste', user: options.user || 'admin', - password: options.password || '', + password: options.password || '' }) } diff --git a/src/lib/helpers/config.ts b/src/lib/helpers/config.ts index 492baca..4a131bf 100644 --- a/src/lib/helpers/config.ts +++ b/src/lib/helpers/config.ts @@ -7,7 +7,7 @@ const getConfig = (): Config => { const configPath = process.argv.length <= 2 ? 'project-config.js' : process.argv[2] const config = JSON.parse( - fs.readFileSync(path.join('config', configPath), 'utf8'), + fs.readFileSync(path.join('config', configPath), 'utf8') ) config.port = (process.env.PORT || config.port || 7777) as number diff --git a/src/lib/key-generators/builder.ts b/src/lib/key-generators/builder.ts index 6a61044..f0179a3 100644 --- a/src/lib/key-generators/builder.ts +++ b/src/lib/key-generators/builder.ts @@ -4,7 +4,8 @@ import type { Config } from 'src/types/config' const build = async (config: Config): Promise => { const pwOptions = config.keyGenerator pwOptions.type = pwOptions.type || 'random' - const Generator = (await import(`../key-generators/${pwOptions.type}`)).default + const Generator = (await import(`../key-generators/${pwOptions.type}`)) + .default const keyGenerator = new Generator(pwOptions) return keyGenerator diff --git a/src/server.ts b/src/server.ts index 87eb2da..f7b7feb 100644 --- a/src/server.ts +++ b/src/server.ts @@ -11,7 +11,7 @@ import DocumentHandler from 'src/lib/document-handler' import buildDocumenthandler from 'src/lib/document-handler/builder' import { getStaticDirectory, - getStaticItemDirectory, + getStaticItemDirectory } from 'src/lib/helpers/directory' const config: Config = getConfig() @@ -33,17 +33,17 @@ buildDocumenthandler(config) ) { const dest = `${item.substring( 0, - item.length - 3, + item.length - 3 )}.min${item.substring(item.length - 3)}` const origCode = fs.readFileSync( getStaticItemDirectory(__dirname, item), - 'utf8', + 'utf8' ) fs.writeFileSync( getStaticItemDirectory(__dirname, dest), uglify.minify(origCode).code, - 'utf8', + 'utf8' ) winston.info(`compressed ${item} into ${dest}`) } @@ -66,12 +66,12 @@ buildDocumenthandler(config) cb => { winston.debug('loaded static document', { success: cb }) }, - true, + true ) } else { winston.warn('failed to load static document', { name, - path: documentPath, + path: documentPath }) } }) @@ -86,25 +86,25 @@ buildDocumenthandler(config) // get raw documents - support getting with extension app.get('/raw/:id', async (request, response) => - documentHandler.handleRawGet(request, response), + documentHandler.handleRawGet(request, response) ) app.head('/raw/:id', (request, response) => - documentHandler.handleRawGet(request, response), + documentHandler.handleRawGet(request, response) ) // // add documents app.post('/documents', (request, response) => - documentHandler.handlePost(request, response), + documentHandler.handlePost(request, response) ) // get documents app.get('/documents/:id', (request, response) => - documentHandler.handleGet(request, response), + documentHandler.handleGet(request, response) ) app.head('/documents/:id', (request, response) => - documentHandler.handleGet(request, response), + documentHandler.handleGet(request, response) ) // Otherwise, try to match static files @@ -113,8 +113,8 @@ buildDocumenthandler(config) path: getStaticDirectory(__dirname), content: { maxAge: config.staticMaxAge }, passthrough: true, - index: false, - }), + index: false + }) ) // Then we can loop back - and everything else should be a token, @@ -129,8 +129,8 @@ buildDocumenthandler(config) connectSt({ path: getStaticDirectory(__dirname), content: { maxAge: config.staticMaxAge }, - index: 'index.html', - }), + index: 'index.html' + }) ) app.listen(config.port, config.host, () => { diff --git a/src/types/config.ts b/src/types/config.ts index 7544f29..c6b87b5 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -78,4 +78,3 @@ export interface KeyGeneratorConfig { keyspace?: string path?: string } - diff --git a/src/types/log.ts b/src/types/log.ts index 480afcc..77bfcf4 100644 --- a/src/types/log.ts +++ b/src/types/log.ts @@ -1,11 +1,11 @@ export interface Logging { - level: string - type: - | 'File' - | 'Console' - | 'Loggly' - | 'DailyRotateFile' - | 'Http' - | 'Memory' - | 'Webhook' + level: string + type: + | 'File' + | 'Console' + | 'Loggly' + | 'DailyRotateFile' + | 'Http' + | 'Memory' + | 'Webhook' } diff --git a/src/types/store.ts b/src/types/store.ts index f43c1fd..5890321 100644 --- a/src/types/store.ts +++ b/src/types/store.ts @@ -8,6 +8,6 @@ export interface Store { key: string, data: string, callback: Callback, - skipExpire?: boolean, + skipExpire?: boolean ) => void }