-
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
Showing
15 changed files
with
156 additions
and
28 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
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
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,38 @@ | ||
import { factoryCallMethod } from '#compiler/helpers/call.js'; | ||
import { | ||
getTypeOfNode, | ||
isFalseKeyword, | ||
isNumberType, | ||
isTrueKeyword, | ||
} from '#compiler/helpers/checker.js'; | ||
import { factoryIfReturn } from '#compiler/helpers/statement.js'; | ||
|
||
export function makeCache(decor, req, res, ctx) { | ||
if (!decor?.expression) return; | ||
|
||
const arg = decor.expression.arguments[0]; | ||
const name = decor.expression.expression; | ||
const type = getTypeOfNode(arg); | ||
|
||
if (isTrueKeyword(arg)) { | ||
return { | ||
check: factoryIfReturn( | ||
factoryCallMethod(name, 'checkImmutable', [req, res]) | ||
), | ||
preset: factoryCallMethod(name, 'setImmutable', [ctx]), | ||
}; | ||
} else if (isFalseKeyword(arg)) { | ||
return { | ||
preset: factoryCallMethod(name, 'setNoStore', [ctx]), | ||
}; | ||
} else if (isNumberType(type)) { | ||
return { | ||
check: factoryIfReturn(factoryCallMethod(name, 'checkAge', [req, res])), | ||
preset: factoryCallMethod(name, 'setAge', [ctx, arg]), | ||
}; | ||
} | ||
} | ||
|
||
export function Cache(node) { | ||
return node; | ||
} |
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 @@ | ||
interface CacheOptions { | ||
maxAge?: number; | ||
immutable?: boolean; | ||
revalidate?: () => Promise<boolean>; | ||
} | ||
|
||
export declare function Cache( | ||
options: boolean | number | CacheOptions | ||
): ( | ||
target: (payload?: any) => any, | ||
context: ClassMethodDecoratorContext | ||
) => void; |
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,41 @@ | ||
import { respondNotModified } from './response/respondCache.js'; | ||
|
||
export class Cache { | ||
static checkImmutable(req, res) { | ||
if (req.getHeader('if-none-match')) { | ||
respondNotModified(res); | ||
return true; | ||
} else return false; | ||
} | ||
|
||
static setImmutable(context) { | ||
context.response.headers.push( | ||
'cache-control', | ||
'max-age=31536000, immutable', | ||
'etag', | ||
'1' | ||
); | ||
} | ||
|
||
static setNoStore(context) { | ||
context.response.headers.push('cache-control', 'no-store'); | ||
} | ||
|
||
static checkAge(req, res) { | ||
const eTag = req.getHeader('if-none-match'); | ||
|
||
if (eTag && +eTag > Date.now()) { | ||
respondNotModified(res); | ||
return true; | ||
} else return false; | ||
} | ||
|
||
static setAge(context, age) { | ||
context.response.headers.push( | ||
'cache-control', | ||
'max-age=' + age, | ||
'etag', | ||
(Date.now() + age * 1000).toString() | ||
); | ||
} | ||
} |
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,5 @@ | ||
export function respondNotModified(res) { | ||
res.cork(() => { | ||
res.writeStatus('304').end(); | ||
}); | ||
} |
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