-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ade Yahya Prasetyo
authored and
mirza adipradhana
committed
Apr 8, 2021
1 parent
a77d440
commit 8cd81ec
Showing
16 changed files
with
138 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
import express from 'express'; | ||
|
||
import { parserMiddleware } from './middleware'; | ||
|
||
export * from './types'; | ||
export const app = express(); | ||
|
||
// body parser and multer | ||
app.use(parserMiddleware); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,35 @@ | ||
import { Reader } from 'fp-ts/lib/Reader'; | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { IO } from 'fp-ts/lib/IO'; | ||
import LRU, { LRUOption } from './lru'; | ||
import * as E from 'fp-ts/Either'; | ||
import { fromNullable, Option } from 'fp-ts/lib/Option'; | ||
|
||
import { sortURLSearchParams } from '../utils/url'; | ||
import { toString, hashStr } from '../utils/generic'; | ||
import LMDB, { LMDBOptions } from './lmdb'; | ||
|
||
// @TODO Cache suppose to be modular, where we can select whether the cache will be stored on the memory, db, or file | ||
// type CacheOption = { | ||
// store: 'memory' | 'db' | 'file', | ||
// max: number, | ||
// ttl: number, | ||
// logger: () => void, | ||
// } | ||
|
||
export interface ICacheDependency { | ||
export interface ICache { | ||
has: (key: string) => boolean; | ||
get: (key: string) => string; | ||
get: (key: string) => string | undefined; | ||
set: (key: string, value: string) => boolean; | ||
reset: () => E.Either<Error, null> | void; | ||
reset: () => E.Either<Error, null>; | ||
close?: () => void; | ||
} | ||
|
||
export interface ICache { | ||
setItem: <T>(key: string, value: unknown) => E.Either<unknown, T>; | ||
getItem: <T>(key: string) => E.Either<unknown, T>; | ||
export interface MemoryOption { | ||
type: 'memory'; | ||
options: LRUOption; | ||
} | ||
|
||
export const getItem = ( | ||
key: string, | ||
): Reader<ICacheDependency, IO<Option<string>>> => (deps) => () => | ||
fromNullable(deps.get(key)); | ||
|
||
export const hasItem = (key: string): Reader<ICacheDependency, IO<boolean>> => ( | ||
deps, | ||
) => () => deps.has(key); | ||
|
||
export const setItem = ( | ||
key: string, | ||
value: string, | ||
): Reader<ICacheDependency, IO<boolean>> => (deps) => () => | ||
deps.set(key, value); | ||
export interface FileOption { | ||
type: 'file'; | ||
options: LMDBOptions; | ||
} | ||
|
||
export const getGetRequest = (url: URL) => | ||
pipe(sortURLSearchParams(url), toString, hashStr, (key) => (deps) => | ||
getItem(key)(deps), | ||
); | ||
export type CacheOptions = MemoryOption | FileOption; | ||
|
||
export const hasGetRequest = (url: URL) => | ||
pipe(sortURLSearchParams(url), toString, hashStr, hasItem); | ||
export const createCacheInstance = (options: CacheOptions): ICache => { | ||
if (options.type === 'memory') { | ||
return new LRU(options.options); | ||
} | ||
if (options.type === 'file') { | ||
return new LMDB(options.options); | ||
} | ||
|
||
export const setGetRequest = (url: URL, responsePayload: string) => | ||
pipe(sortURLSearchParams(url), toString, hashStr, (key) => (deps) => | ||
setItem(key, responsePayload)(deps), | ||
); | ||
return new LRU({ max: 50 }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import LRUBase from 'lru-cache'; | ||
import * as E from 'fp-ts/Either'; | ||
import { ICache } from './cache'; | ||
|
||
export interface LRUOption { | ||
max: number; | ||
} | ||
|
||
export default class LRU implements ICache { | ||
env: LRUBase<string, string>; | ||
|
||
constructor(options: LRUOption) { | ||
this.env = new LRUBase(options.max); | ||
} | ||
|
||
public get = (key: string) => { | ||
return this.env.get(key); | ||
}; | ||
|
||
public set = (key: string, value: string) => { | ||
return this.env.set(key, value); | ||
}; | ||
|
||
public has = (key: string) => { | ||
return this.env.has(key); | ||
}; | ||
|
||
public reset = () => { | ||
this.env.reset(); | ||
return E.right(null); | ||
}; | ||
|
||
public close = () => { | ||
return null; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './parser'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Router } from 'express'; | ||
import bodyParser from 'body-parser'; | ||
import multer from 'multer'; | ||
|
||
const forms = multer(); | ||
|
||
const router = Router(); | ||
router.use(forms.any()); | ||
router.use(bodyParser.urlencoded({ extended: true })); | ||
router.use(bodyParser.json()); | ||
|
||
export const parserMiddleware = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as O from 'fp-ts/Option'; | ||
|
||
import { ICache } from './libs/cache'; | ||
import { Config } from './types'; | ||
|
||
export interface Store { | ||
config: Config; | ||
cacheStorage: ICache; | ||
locals: Record<string, O.Option<unknown>>; | ||
getLocal: (key: string) => O.Option<unknown>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10112,6 +10112,11 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/husky/-/husky-5.0.9.tgz#6d38706643d66ed395bcd4ee952d02e3f15eb3a3" | ||
integrity sha512-0SjcaY21a+IRdx7p7r/X33Vc09UR2m8SbP8yfkhUX2/jAmwcz+GR7i9jXkp2pP3GfX23JhMkVP6SWwXB18uXtg== | ||
|
||
hyper-ts@^0.6.1: | ||
version "0.6.1" | ||
resolved "https://registry.yarnpkg.com/hyper-ts/-/hyper-ts-0.6.1.tgz#a219ec6a170b5d6c7d665561c0a5b1e0b50c758b" | ||
integrity sha512-rhtf4s/VdDQSxKRvgFm4AVMPyuyOdAyeoZ641r3+fHnHyrRk9oSwHzVI8KjR1FFN9fUvSZQdGzgA7/p8eV+I6Q== | ||
|
||
[email protected], iconv-lite@^0.4.24: | ||
version "0.4.24" | ||
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b" | ||
|
@@ -17396,7 +17401,7 @@ uuid@^3.3.2, uuid@^3.4.0: | |
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" | ||
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A== | ||
|
||
uuid@^8.3.0: | ||
uuid@^8.3.0, uuid@^8.3.2: | ||
version "8.3.2" | ||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" | ||
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== | ||
|