@@ -2,28 +2,29 @@ import { computeHash, hmacSign, splitBy } from "../shared/util.ts";
22import { SessionImpl } from "./session.ts" ;
33import type { ConnInfo , Context , HTMLRewriterHandlers , Session , SessionOptions } from "./types.ts" ;
44
5- type ContextOptions = {
6- connInfo ?: ConnInfo ;
7- session ?: SessionOptions ;
5+ export const NEXT = Symbol ( ) ;
6+ export const CUSTOM_HTML_REWRITER = Symbol ( ) ;
7+
8+ export type ContextInit = {
9+ req : Request ;
10+ connInfo : ConnInfo ;
11+ sessionOptions ?: SessionOptions ;
812} ;
913
1014/** create a context object */
11- export function createContext (
12- req : Request ,
13- next : ( ) => Promise < Response > | Response ,
14- options ?: ContextOptions ,
15- ) : Context {
15+ export function createContext ( next : ( ) => Promise < Response > | Response , init : ContextInit ) : Context {
1616 let cookies : Map < string , string > | null = null ;
1717 let session : Session < Record < string , unknown > > | null = null ;
18+ const customHtmlRewriter : [ string , HTMLRewriterHandlers ] [ ] = [ ] ;
19+ const extension = { } ;
1820 const ctx : Context = {
19- connInfo : options ? .connInfo ,
21+ connInfo : init . connInfo ,
2022 params : { } ,
21- headers : new Headers ( ) ,
2223 cookies : {
2324 get ( name : string ) {
24- if ( cookies === null ) {
25+ if ( ! cookies ) {
2526 cookies = new Map < string , string > ( ) ;
26- const cookieHeader = req . headers . get ( "Cookie" ) ;
27+ const cookieHeader = init . req . headers . get ( "Cookie" ) ;
2728 if ( cookieHeader ) {
2829 for ( const cookie of cookieHeader . split ( ";" ) ) {
2930 const [ key , value ] = splitBy ( cookie , "=" ) ;
@@ -37,12 +38,13 @@ export function createContext(
3738 // deno-lint-ignore ban-ts-comment
3839 // @ts -ignore
3940 async getSession ( ) : Promise < Session < Record < string , unknown > > > {
40- if ( session !== null ) {
41+ if ( session ) {
4142 return session ;
4243 }
4344
44- const cookieName = options ?. session ?. cookie ?. name ?? "session" ;
45- const secret = options ?. session ?. secret ?? "-" ;
45+ const { sessionOptions } = init ;
46+ const cookieName = sessionOptions ?. cookie ?. name ?? "session" ;
47+ const secret = sessionOptions ?. secret ?? "-" ;
4648 let sid = ctx . cookies . get ( cookieName ) ;
4749 let skipInit = false ;
4850 if ( sid ) {
@@ -57,23 +59,44 @@ export function createContext(
5759 skipInit = true ;
5860 }
5961
60- const sessionImpl = new SessionImpl < Record < string , unknown > > (
61- sid ,
62- options ?. session ,
63- ) ;
62+ const sessionImpl = new SessionImpl < Record < string , unknown > > ( sid , sessionOptions ) ;
6463 session = sessionImpl ;
6564 if ( ! skipInit ) {
6665 await sessionImpl . init ( ) ;
6766 }
6867 return session ;
6968 } ,
70- __htmlRewriterHandlers : [ ] ,
7169 htmlRewriter : {
7270 on : ( selector : string , handlers : HTMLRewriterHandlers ) => {
73- ( ctx . __htmlRewriterHandlers as unknown [ ] ) . push ( [ selector , handlers ] ) ;
71+ customHtmlRewriter . push ( [ selector , handlers ] ) ;
7472 } ,
7573 } ,
7674 next,
7775 } ;
78- return ctx ;
76+ return new Proxy ( Object . create ( { } ) , {
77+ get ( _target , prop ) {
78+ if ( prop === CUSTOM_HTML_REWRITER ) {
79+ return customHtmlRewriter ;
80+ }
81+ if ( Reflect . has ( ctx , prop ) ) {
82+ return Reflect . get ( ctx , prop ) ;
83+ }
84+ return Reflect . get ( extension , prop ) ;
85+ } ,
86+ set ( _target , prop , value ) {
87+ if ( prop === NEXT ) {
88+ Reflect . set ( ctx , "next" , value ) ;
89+ }
90+ if ( ! Reflect . has ( ctx , prop ) ) {
91+ return Reflect . set ( extension , prop , value ) ;
92+ }
93+ return false ;
94+ } ,
95+ deleteProperty ( _target , prop ) {
96+ if ( ! Reflect . has ( ctx , prop ) ) {
97+ return Reflect . deleteProperty ( extension , prop ) ;
98+ }
99+ return false ;
100+ } ,
101+ } ) ;
79102}
0 commit comments