1- import DuplexPair from ' duplexpair' ;
2- import http from ' http' ;
1+ import DuplexPair from " duplexpair" ;
2+ import http from " http" ;
33
44// Exact values specified by RFC6749 ;)
5- const oauthToken = { access_token : ' 2YotnFZFEjr1zCsicMWpAA' , expires_in : 3600 } ;
5+ const oauthToken = { access_token : " 2YotnFZFEjr1zCsicMWpAA" , expires_in : 3600 } ;
66
7- type RequestData = { url : string , body : string } ;
7+ type RequestData = { url : string ; body : string } ;
88type HandlerFunction = ( data : RequestData ) => any ;
9- type HandlerList = { host : RegExp , handler : HandlerFunction } [ ] ;
9+ type HandlerList = { host : RegExp ; handler : HandlerFunction } [ ] ;
1010type Duplex = NodeJS . ReadableStream & NodeJS . WritableStream ;
1111
1212// Return a Duplex stream that behaves like an HTTP stream, with the 'server'
1313// being provided by the handler function in this case (which is expected
1414// to return JSON).
15- export function makeFakeHTTPConnection ( handlerList : HandlerList ) : Duplex & { requests : http . IncomingMessage [ ] } {
15+ export function makeFakeHTTPConnection (
16+ handlerList : HandlerList
17+ ) : Duplex & { requests : http . IncomingMessage [ ] } {
1618 const { socket1, socket2 } = new DuplexPair ( ) ;
1719 const server = makeFakeHTTPServer ( handlerList ) ;
18- server . emit ( ' connection' , socket2 ) ;
20+ server . emit ( " connection" , socket2 ) ;
1921 return Object . assign ( socket1 , { requests : server . requests } ) ;
2022}
2123
@@ -26,29 +28,31 @@ export function makeFakeHTTPServer(handlerList: HandlerList): FakeHTTPServer {
2628 const server = http . createServer ( ( req , res ) => {
2729 ( server as FakeHTTPServer ) . requests . push ( req ) ;
2830 let foundHandler : HandlerFunction | undefined ;
29- const host = req . headers [ ' host' ] ;
31+ const host = req . headers [ " host" ] ;
3032 for ( const potentialHandler of handlerList ) {
31- if ( potentialHandler . host . test ( host ?? '' ) ) {
33+ if ( potentialHandler . host . test ( host ?? "" ) ) {
3234 foundHandler = potentialHandler . handler ;
3335 break ;
3436 }
3537 }
3638 if ( ! foundHandler ) {
3739 res . writeHead ( 404 , {
38- ' Content-Type' : ' text/plain'
40+ " Content-Type" : " text/plain" ,
3941 } ) ;
4042 res . end ( `Host ${ host } not found` ) ;
4143 return ;
4244 }
4345 const handler = foundHandler ; // Makes TS happy
4446
45- let body = '' ;
46- req . setEncoding ( 'utf8' ) . on ( 'data' , chunk => { body += chunk ; } ) ;
47- req . on ( 'end' , ( ) => {
47+ let body = "" ;
48+ req . setEncoding ( "utf8" ) . on ( "data" , ( chunk ) => {
49+ body += chunk ;
50+ } ) ;
51+ req . on ( "end" , ( ) => {
4852 res . writeHead ( 200 , {
49- ' Content-Type' : ' application/json'
53+ " Content-Type" : " application/json" ,
5054 } ) ;
51- res . end ( JSON . stringify ( handler ( { url : req . url ?? '' , body } ) ) ) ;
55+ res . end ( JSON . stringify ( handler ( { url : req . url ?? "" , body } ) ) ) ;
5256 } ) ;
5357 } ) ;
5458 return Object . assign ( server , { requests : [ ] } ) ;
@@ -57,7 +61,7 @@ export function makeFakeHTTPServer(handlerList: HandlerList): FakeHTTPServer {
5761export const fakeAWSHandlers : HandlerList = [
5862 { host : / \. a m a z o n a w s \. c o m $ / , handler : awsHandler } ,
5963 { host : / \. m i c r o s o f t o n l i n e .c o m $ | \. a z u r e .n e t $ / , handler : azureHandler } ,
60- { host : / \. g o o g l e a p i s .c o m $ / , handler : gcpHandler }
64+ { host : / \. g o o g l e a p i s .c o m $ / , handler : gcpHandler } ,
6165] ;
6266
6367function awsHandler ( { body } : RequestData ) : any {
@@ -68,31 +72,35 @@ function awsHandler({ body }: RequestData): any {
6872 // both KeyId and Plaintext so that they are available for generating
6973 // the decryption response, which also provides the KeyId and Plaintext
7074 // based on the CiphertextBlob alone.
71- const CiphertextBlob = Buffer . from ( request . KeyId + '\0' + request . Plaintext ) . toString ( 'base64' )
75+ const CiphertextBlob = Buffer . from (
76+ request . KeyId + "\0" + request . Plaintext
77+ ) . toString ( "base64" ) ;
7278 return {
7379 CiphertextBlob,
74- EncryptionAlgorithm : ' SYMMETRIC_DEFAULT' ,
75- KeyId : request . KeyId
80+ EncryptionAlgorithm : " SYMMETRIC_DEFAULT" ,
81+ KeyId : request . KeyId ,
7682 } ;
7783 } else {
78- let [ KeyId , Plaintext ] = Buffer . from ( request . CiphertextBlob , 'base64' ) . toString ( ) . split ( '\0' ) ;
84+ let [ KeyId , Plaintext ] = Buffer . from ( request . CiphertextBlob , "base64" )
85+ . toString ( )
86+ . split ( "\0" ) ;
7987 // Do not return invalid base64 https://jira.mongodb.org/browse/MONGOCRYPT-525
80- if ( Buffer . from ( KeyId , ' base64' ) . toString ( ' base64' ) !== KeyId ) {
81- KeyId = ' invalid0' ;
88+ if ( Buffer . from ( KeyId , " base64" ) . toString ( " base64" ) !== KeyId ) {
89+ KeyId = " invalid0" ;
8290 }
83- if ( Buffer . from ( Plaintext , ' base64' ) . toString ( ' base64' ) !== Plaintext ) {
84- Plaintext = ' invalid1' ;
91+ if ( Buffer . from ( Plaintext , " base64" ) . toString ( " base64" ) !== Plaintext ) {
92+ Plaintext = " invalid1" ;
8593 }
8694 return {
8795 Plaintext,
88- EncryptionAlgorithm : ' SYMMETRIC_DEFAULT' ,
89- KeyId
96+ EncryptionAlgorithm : " SYMMETRIC_DEFAULT" ,
97+ KeyId,
9098 } ;
9199 }
92100}
93101
94102function azureHandler ( { body, url } : RequestData ) : any {
95- if ( url . endsWith ( ' /token' ) ) {
103+ if ( url . endsWith ( " /token" ) ) {
96104 return oauthToken ;
97105 } else if ( url . match ( / \/ ( u n ) ? w r a p k e y / ) ) {
98106 // Just act as if this was encrypted.
@@ -101,12 +109,12 @@ function azureHandler({ body, url }: RequestData): any {
101109}
102110
103111function gcpHandler ( { body, url } : RequestData ) : any {
104- if ( url . endsWith ( ' /token' ) ) {
112+ if ( url . endsWith ( " /token" ) ) {
105113 return oauthToken ;
106- } else if ( url . endsWith ( ' :encrypt' ) ) {
114+ } else if ( url . endsWith ( " :encrypt" ) ) {
107115 // Here we also just perform noop encryption.
108116 return { ciphertext : JSON . parse ( body ) . plaintext } ;
109- } else if ( url . endsWith ( ' :decrypt' ) ) {
117+ } else if ( url . endsWith ( " :decrypt" ) ) {
110118 return { plaintext : JSON . parse ( body ) . ciphertext } ;
111119 }
112120}
0 commit comments