import { CookieHandler , UniversalCookies } from '../interfaces' ; import * as http from 'http' ; import * as NodeCookie from 'cookie' ; export class NodeHttpHandler implements CookieHandler { private req : http . ServerRequest ; private res : http . ServerResponse ; constructor ( req , res ) { this . req = req ; this . res = res ; } get ( key ) { if ( typeof this . req . headers [ 'cookie' ] === 'string' ) { const cookies = NodeCookie . parse ( this . req . headers [ 'cookie' ] as string ) ; if ( cookies . hasOwnProperty ( key ) ) { return cookies [ key ] ; } } return undefined ; } set ( key , value , options ) { const filled = { ... UniversalCookies . Options . DEFAULT , ... options } ; const rawHeader = this . res . getHeader ( 'Set-Cookie' ) ; let headers ; switch ( typeof rawHeader ) { case 'number' : headers = [ ( rawHeader as number ) . toString ( ) ] ; break ; case 'string' : headers = [ rawHeader as string ] ; break ; default : headers = rawHeader ? ( rawHeader as string [ ] ) : [ ] ; } this . res . setHeader ( 'Set-Cookie' , new Array < string > ( ) . concat ( headers ) . concat ( [ NodeCookie . serialize ( key , value , ( { expires : filled . expires , path : filled . path , domain : filled . domain , secure : filled . secure , } as NodeCookie . CookieSerializeOptions ) ) ] ) ) ; } remove ( key , options ) { this . set ( key , '' , { expires : new Date ( 0 , 0 ) , ... options , } ) ; } }