1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-11-01 03:21:21 +00:00

add prettier

This commit is contained in:
Yusuf Yilmaz 2022-06-06 19:44:11 +02:00
parent 21aa23dd28
commit a5b0a98b3f
19 changed files with 94 additions and 92 deletions

View file

@ -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"
}
}

View file

@ -1,5 +1,5 @@
const DEFAULT_KEY_LENGTH = 10
export default {
DEFAULT_KEY_LENGTH,
DEFAULT_KEY_LENGTH
}

9
src/global.d.ts vendored
View file

@ -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
}

View file

@ -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

View file

@ -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
}

View file

@ -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 => {

View file

@ -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', () => {

View file

@ -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)
})

View file

@ -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)

View file

@ -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)
},
}
)
})
}

View file

@ -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?.()
},
}
)
})
}

View file

@ -35,9 +35,11 @@ class RedisDocumentStore implements Store {
const port = options.port || 6379
const index = options.db || 0
const connectionParameters = url ? {
const connectionParameters = url
? {
url
}: {
}
: {
host,
port
}
@ -46,7 +48,7 @@ class RedisDocumentStore implements Store {
...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))

View file

@ -22,7 +22,7 @@ class RethinkDBStore {
port: options.port || 28015,
db: options.db || 'haste',
user: options.user || 'admin',
password: options.password || '',
password: options.password || ''
})
}

View file

@ -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

View file

@ -4,7 +4,8 @@ import type { Config } from 'src/types/config'
const build = async (config: Config): Promise<KeyGenerator> => {
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

View file

@ -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, () => {

View file

@ -78,4 +78,3 @@ export interface KeyGeneratorConfig {
keyspace?: string
path?: string
}

View file

@ -8,6 +8,6 @@ export interface Store {
key: string,
data: string,
callback: Callback,
skipExpire?: boolean,
skipExpire?: boolean
) => void
}