Skip to content

Commit 84a6814

Browse files
Callbacks
1 parent d0f844c commit 84a6814

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

index.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
import { config, updateConfig, loadHandler } from "./src/config"
22
import { RouteCheck } from "./src/RouteCheck"
3+
import { ConfigParameters } from "./src/interface";
34

4-
export default (options: any = {}) => {
5-
// TODO: options callbacks
6-
/*
7-
options = {
8-
callbacks: {
9-
before_serve(key: string, body: string, headers: any): void {
10-
11-
},
12-
before_cache(route: any, cache: Buffer[], headers: any): void {
13-
14-
},
15-
after_cache(route: any, body: Buffer, headers: any): void {
16-
17-
}
18-
}
19-
}
20-
*/
21-
5+
export default (options: ConfigParameters = {}) => {
226
updateConfig(options);
237

248
const module = loadHandler(config);
@@ -41,7 +25,9 @@ export default (options: any = {}) => {
4125
return next();
4226
}
4327
else if (reply) {
44-
// TODO: callback before_serve
28+
if(config.callbacks && config.callbacks.before_serve) {
29+
config.callbacks.before_serve(key, reply.content, reply.header, reply.encoding);
30+
}
4531

4632
res.set(reply.header);
4733
return res.end(reply.content, reply.encoding);
@@ -66,9 +52,11 @@ export default (options: any = {}) => {
6652
return;
6753
}
6854

69-
// TESTEEEE
70-
// TODO: callback before_cache
71-
cache.push(Buffer.from("<hr /><b>cac<font color='red'>hed</font></b>", encoding));
55+
56+
let originalHeaders = Object.assign({}, res._headers);
57+
if(config.callbacks && config.callbacks.before_cache) {
58+
config.callbacks.before_cache(route, cache, originalHeaders, encoding);
59+
}
7260

7361
const now = new Date();
7462
let dateExpire = new Date();
@@ -78,11 +66,11 @@ export default (options: any = {}) => {
7866
const buf = Buffer.concat(cache);
7967

8068
const headers = route.mergeHeaders(
81-
res._headers,
69+
originalHeaders,
8270
{
8371
"Content-Length": buf.length,
8472
"Last-Modified": now.toUTCString(),
85-
"Cache-Control": "max-age=" + (dateExpire.getTime() / 1000)
73+
"Cache-Control": "max-age=" + (dateExpire.getTime() / 1000 >> 0)
8674
}
8775
);
8876

@@ -92,8 +80,11 @@ export default (options: any = {}) => {
9280
headers,
9381
rawUrl,
9482
expire
95-
);
96-
// TODO: callback after_cache
83+
, (key) => {
84+
if(config.callbacks && config.callbacks.after_cache) {
85+
config.callbacks.after_cache(key, route, buf, headers, encoding);
86+
}
87+
});
9788
end.call(res);
9889
};
9990
return next();

src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { ConfigParameters } from "./interface";
12
import * as path from "path";
23
import * as fs from "fs"
34

45
export let config: any = {}
56

6-
export function updateConfig(options: any) {
7+
export function updateConfig(options: ConfigParameters) {
78
this.config = Object.assign(this.config, options);
89
}
910

src/handler/InMemory.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class Handler {
1616

1717
return callback(null, null);
1818
}
19-
set(key: string, content: Buffer, header: any, rawUrl: string, expire: number = null) {
19+
set(key: string, content: Buffer, header: any, rawUrl: string, expire: number = null, callback?: Function) {
2020
if (this.config.prefix) {
2121
key = this.config.prefix + "_" + key;
2222
}
@@ -30,6 +30,8 @@ export class Handler {
3030
rawUrl,
3131
expire
3232
}
33+
34+
callback && callback(key);
3335
}
3436

3537
del(key: string) {

src/interface.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export declare interface ConfigCallbacks {
2+
before_serve?(key: string, body: string, headers: any, encoding?: string): void;
3+
before_cache?(route: any, cache: Buffer[], headers: any, encoding?: string): void;
4+
after_cache?(key: string, route: any, body: Buffer, headers: any, encoding?: string): void;
5+
}
6+
export declare interface ConfigParameters {
7+
handler?: string;
8+
isCacheable?(req: any): boolean;
9+
callbacks?: ConfigCallbacks;
10+
}

0 commit comments

Comments
 (0)