@@ -4,33 +4,54 @@ import * as SSM from 'aws-sdk/clients/ssm';
4
4
import * as HttpErrors from 'http-errors' ;
5
5
import * as Koa from 'koa' ; // http://koajs.cn
6
6
import * as bodyParser from 'koa-bodyparser' ;
7
+ import * as koaCash from 'koa-cash' ;
7
8
import * as logger from 'koa-logger' ;
8
9
import * as Router from 'koa-router' ;
10
+ import { LRUCache } from 'lru-cache' ;
9
11
import * as sharp from 'sharp' ;
10
12
import config from './config' ;
11
13
import debug from './debug' ;
12
14
import { bufferStore , getProcessor , parseRequest , setMaxGifSizeMB , setMaxGifPages } from './default' ;
13
15
import * as is from './is' ;
14
16
import { IHttpHeaders , InvalidArgument } from './processor' ;
15
17
18
+ const MB = 1048576 ;
19
+
16
20
const ssm = new SSM ( { region : config . region } ) ;
17
21
const smclient = new SecretsManager ( { region : config . region } ) ;
18
22
19
23
const DefaultBufferStore = bufferStore ( ) ;
20
24
const app = new Koa ( ) ;
21
25
const router = new Router ( ) ;
26
+ const lruCache = new LRUCache < string , CacheObject > ( {
27
+ max : config . CACHE_MAX_ITEMS ,
28
+ maxSize : config . CACHE_MAX_SIZE_MB * MB ,
29
+ ttl : config . CACHE_TTL_SEC * 1000 ,
30
+ sizeCalculation : ( value ) => {
31
+ return value . body . length ;
32
+ } ,
33
+ } ) ;
22
34
23
35
sharp . cache ( { items : 1000 , files : 200 , memory : 2000 } ) ;
24
36
25
37
app . use ( logger ( ) ) ;
26
38
app . use ( errorHandler ( ) ) ;
27
39
app . use ( bodyParser ( ) ) ;
40
+ app . use ( koaCash ( {
41
+ setCachedHeader : true ,
42
+ get : ( key ) => {
43
+ return Promise . resolve ( lruCache . get ( key ) ) ;
44
+ } ,
45
+ set : ( key , value ) => {
46
+ lruCache . set ( key , value as CacheObject ) ;
47
+ return Promise . resolve ( ) ;
48
+ } ,
49
+ } ) ) ;
28
50
29
51
router . post ( '/images' , async ( ctx ) => {
30
52
console . log ( 'post request body=' , ctx . request . body ) ;
31
53
32
54
const opt = await validatePostRequest ( ctx ) ;
33
- console . log ( opt ) ;
34
55
ctx . path = opt . sourceObject ;
35
56
ctx . query [ 'x-oss-process' ] = opt . params ;
36
57
ctx . headers [ 'x-bucket' ] = opt . sourceBucket ;
@@ -61,12 +82,14 @@ router.get(['/', '/ping'], async (ctx) => {
61
82
} ) ;
62
83
63
84
router . get ( [ '/debug' , '/_debug' ] , async ( ctx ) => {
64
- console . log ( debug ( ) ) ;
85
+ console . log ( JSON . stringify ( debug ( lruCache ) ) ) ;
65
86
ctx . status = 400 ;
66
87
ctx . body = 'Please check your server logs for more details!' ;
67
88
} ) ;
68
89
69
90
router . get ( '/(.*)' , async ( ctx ) => {
91
+ if ( await ctx . cashed ( ) ) return ;
92
+
70
93
const queue = sharp . counters ( ) . queue ;
71
94
if ( queue > config . sharpQueueLimit ) {
72
95
ctx . body = { message : 'Too many requests, please try again later.' } ;
@@ -89,7 +112,7 @@ app.on('error', (err: Error) => {
89
112
90
113
app . listen ( config . port , ( ) => {
91
114
console . log ( `Server running on port ${ config . port } ` ) ;
92
- console . log ( 'Config:' , config ) ;
115
+ console . log ( 'Config:' , JSON . stringify ( config ) ) ;
93
116
} ) ;
94
117
95
118
function errorHandler ( ) : Koa . Middleware < Koa . DefaultState , Koa . DefaultContext , any > {
0 commit comments