@@ -2,7 +2,14 @@ const V03Binary = require("./receiver_binary_0_3");
2
2
const V03Structured = require ( "./receiver_structured_0_3.js" ) ;
3
3
const V1Binary = require ( "./receiver_binary_1.js" ) ;
4
4
const 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" ) ;
6
13
7
14
class HTTPReceiver {
8
15
constructor ( ) {
@@ -22,33 +29,37 @@ class HTTPReceiver {
22
29
const mode = getMode ( headers ) ;
23
30
const version = getVersion ( mode , headers , body ) ;
24
31
switch ( version ) {
25
- case constants . SPEC_V1 :
32
+ case SPEC_V1 :
26
33
return this . receivers . v1 [ mode ] . parse ( body , headers ) ;
27
- case constants . SPEC_V03 :
34
+ case SPEC_V03 :
28
35
return this . receivers . v03 [ mode ] . parse ( body , headers ) ;
29
36
default :
30
37
console . error (
31
- `Unknown spec version ${ version } . Default to ${ constants . SPEC_V1 } ` ) ;
38
+ `Unknown spec version ${ version } . Default to ${ SPEC_V1 } ` ) ;
32
39
return this . receivers . v1 [ mode ] . parse ( body , headers ) ;
33
40
}
34
41
}
35
42
}
36
43
37
44
function 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 ) ) {
41
48
mode = "structured" ;
49
+ } else if ( headers [ BINARY_HEADERS_1 . ID ] ) {
50
+ mode = "binary" ;
51
+ } else {
52
+ throw new TypeError ( "no cloud event detected" ) ;
42
53
}
43
54
return mode ;
44
55
}
45
56
46
57
function getVersion ( mode , headers , body ) {
47
- let version = constants . SPEC_V1 ; // default to 1.0
58
+ let version = SPEC_V1 ; // default to 1.0
48
59
49
60
if ( mode === "binary" ) {
50
61
// Check the headers for the version
51
- const versionHeader = headers [ constants . DEFAULT_SPEC_VERSION_HEADER ] ;
62
+ const versionHeader = headers [ DEFAULT_SPEC_VERSION_HEADER ] ;
52
63
if ( versionHeader ) { version = versionHeader ; }
53
64
} else {
54
65
// structured mode - the version is in the body
0 commit comments