@@ -2,7 +2,14 @@ const V03Binary = require("./receiver_binary_0_3");
22const V03Structured = require ( "./receiver_structured_0_3.js" ) ;
33const V1Binary = require ( "./receiver_binary_1.js" ) ;
44const V1Structured = require ( "./receiver_structured_1.js" ) ;
5- const constants = require ( "./constants" ) ;
5+ const {
6+ SPEC_V03 ,
7+ SPEC_V1 ,
8+ HEADER_CONTENT_TYPE ,
9+ MIME_CE ,
10+ BINARY_HEADERS_1 ,
11+ DEFAULT_SPEC_VERSION_HEADER
12+ } = require ( "./constants" ) ;
613
714class HTTPReceiver {
815 constructor ( ) {
@@ -22,33 +29,37 @@ class HTTPReceiver {
2229 const mode = getMode ( headers ) ;
2330 const version = getVersion ( mode , headers , body ) ;
2431 switch ( version ) {
25- case constants . SPEC_V1 :
32+ case SPEC_V1 :
2633 return this . receivers . v1 [ mode ] . parse ( body , headers ) ;
27- case constants . SPEC_V03 :
34+ case SPEC_V03 :
2835 return this . receivers . v03 [ mode ] . parse ( body , headers ) ;
2936 default :
3037 console . error (
31- `Unknown spec version ${ version } . Default to ${ constants . SPEC_V1 } ` ) ;
38+ `Unknown spec version ${ version } . Default to ${ SPEC_V1 } ` ) ;
3239 return this . receivers . v1 [ mode ] . parse ( body , headers ) ;
3340 }
3441 }
3542}
3643
3744function getMode ( headers ) {
38- let mode = "binary " ;
39- const contentType = headers [ constants . HEADER_CONTENT_TYPE ] ;
40- if ( contentType && contentType . startsWith ( constants . MIME_CE ) ) {
45+ let mode = "unknown " ;
46+ const contentType = headers [ HEADER_CONTENT_TYPE ] ;
47+ if ( contentType && contentType . startsWith ( MIME_CE ) ) {
4148 mode = "structured" ;
49+ } else if ( headers [ BINARY_HEADERS_1 . ID ] ) {
50+ mode = "binary" ;
51+ } else {
52+ throw new TypeError ( "no cloud event detected" ) ;
4253 }
4354 return mode ;
4455}
4556
4657function getVersion ( mode , headers , body ) {
47- let version = constants . SPEC_V1 ; // default to 1.0
58+ let version = SPEC_V1 ; // default to 1.0
4859
4960 if ( mode === "binary" ) {
5061 // Check the headers for the version
51- const versionHeader = headers [ constants . DEFAULT_SPEC_VERSION_HEADER ] ;
62+ const versionHeader = headers [ DEFAULT_SPEC_VERSION_HEADER ] ;
5263 if ( versionHeader ) { version = versionHeader ; }
5364 } else {
5465 // structured mode - the version is in the body
0 commit comments