1
- function HttpBatchAdapter ( $document , $window , httpBatchConfig ) {
2
- var self = this ,
3
- constants = {
4
- httpVersion : 'HTTP/1.1' ,
5
- contentType : 'Content-Type' ,
6
- newline : '\r\n' ,
7
- emptyString : '' ,
8
- singleSpace : ' ' ,
9
- forwardSlash : '/' ,
10
- doubleDash : '--' ,
11
- colon : ':'
12
- } ;
1
+ function NodeJsMultiFetchAdapter ( ) {
2
+ var self = this ;
13
3
14
- self . key = 'httpBatchAdapter ' ;
4
+ self . key = 'nodeJsMultiFetchAdapter ' ;
15
5
self . buildRequest = buildRequestFn ;
16
6
self . parseResponse = parseResponseFn ;
17
7
self . canBatchRequest = canBatchRequestFn ;
@@ -28,53 +18,31 @@ function HttpBatchAdapter($document, $window, httpBatchConfig) {
28
18
* @returns {object } - a http config object.
29
19
*/
30
20
function buildRequestFn ( requests , config ) {
31
- var boundary = httpBatchConfig . calculateBoundary ( ) ,
32
- httpConfig = {
33
- method : 'POST' ,
34
- url : config . batchEndpointUrl ,
21
+ var httpConfig = {
22
+ method : 'GET' ,
23
+ url : config . batchEndpointUrl + '?' ,
35
24
cache : false ,
36
25
headers : config . batchRequestHeaders || { }
37
26
} ,
38
- batchBody = [ ] ,
39
- urlInfo , i , request , header ;
40
-
41
- httpConfig . headers [ constants . contentType ] = 'multipart/mixed; boundary=' + boundary ;
27
+ encodedUrl , i , request ,
28
+ urlParts ;
42
29
43
30
for ( i = 0 ; i < requests . length ; i += 1 ) {
44
31
request = requests [ i ] ;
45
- urlInfo = getUrlInfo ( request . url ) ;
46
-
47
- batchBody . push ( constants . doubleDash + boundary ) ;
48
- if ( config . batchPartRequestHeaders ) {
49
- for ( header in config . batchPartRequestHeaders ) {
50
- batchBody . push ( header + constants . colon + constants . singleSpace + config . batchPartRequestHeaders [ header ] ) ;
51
- }
52
- }
53
-
54
- batchBody . push ( 'Content-Type: application/http; msgtype=request' , constants . emptyString ) ;
55
-
56
- batchBody . push ( request . method + ' ' + urlInfo . relativeUrl + ' ' + constants . httpVersion ) ;
57
- batchBody . push ( 'Host: ' + urlInfo . host ) ;
32
+ urlParts = request . url . split ( '?' ) ;
58
33
59
- for ( header in request . headers ) {
60
- batchBody . push ( header + constants . colon + constants . singleSpace + request . headers [ header ] ) ;
34
+ encodedUrl = urlParts [ 0 ] . replace ( config . serviceUrl , '' ) ;
35
+ if ( urlParts . length > 1 ) {
36
+ encodedUrl += '?' + encodeURIComponent ( urlParts [ 1 ] ) ;
61
37
}
62
38
63
- if ( config . sendCookies === true && $document [ 0 ] . cookie && $document [ 0 ] . cookie . length > 0 ) {
64
- batchBody . push ( 'Cookie: ' + $document [ 0 ] . cookie ) ;
39
+ if ( i > 0 ) {
40
+ httpConfig . url += '&' ;
65
41
}
66
42
67
- batchBody . push ( constants . emptyString ) ;
68
-
69
- if ( request . data ) {
70
- batchBody . push ( request . data ) ;
71
- }
72
-
73
- batchBody . push ( constants . emptyString ) ;
43
+ httpConfig . url += i . toString ( ) + '=' + encodedUrl ;
74
44
}
75
45
76
- batchBody . push ( constants . doubleDash + boundary + constants . doubleDash ) ;
77
- httpConfig . data = batchBody . join ( constants . newline ) ;
78
46
return httpConfig ;
79
47
}
80
48
@@ -87,18 +55,20 @@ function HttpBatchAdapter($document, $window, httpBatchConfig) {
87
55
*/
88
56
function parseResponseFn ( requests , rawResponse , config ) {
89
57
var batchResponses = [ ] ,
90
- boundaryToken = findResponseBoundary ( rawResponse . headers ( ) [ 'content-type' ] ) ,
91
- parts = rawResponse . data . split ( constants . doubleDash + boundaryToken + constants . newline ) ,
92
- i ,
93
- part ,
94
- responseCount = 0 ;
58
+ i , request ,
59
+ responseData = rawResponse . data ,
60
+ dataPart ;
95
61
96
- for ( i = 0 ; i < parts . length ; i += 1 ) {
97
- part = parts [ i ] ;
98
- if ( part !== constants . emptyString ) {
99
- batchResponses . push ( processResponse ( part , requests [ responseCount ] , boundaryToken ) ) ;
100
- responseCount += 1 ;
101
- }
62
+ for ( i = 0 ; i < requests . length ; i += 1 ) {
63
+ request = requests [ i ] ;
64
+ dataPart = responseData [ i . toString ( ) ] ;
65
+
66
+ batchResponses . push ( new window . ahb . HttpBatchResponseData (
67
+ request ,
68
+ dataPart . statusCode ,
69
+ '' ,
70
+ dataPart . body ,
71
+ dataPart . headers ) ) ;
102
72
}
103
73
104
74
return batchResponses ;
@@ -111,132 +81,8 @@ function HttpBatchAdapter($document, $window, httpBatchConfig) {
111
81
* @returns {boolean } false to indicate the request type is not supported.
112
82
*/
113
83
function canBatchRequestFn ( request , config ) {
114
- return true ;
115
- }
116
-
117
- /**
118
- * mainly here to polyfill ie8 :-(
119
- */
120
- function trim ( data ) {
121
- if ( data . trim ) {
122
- data = data . trim ( ) ;
123
- } else {
124
- data = data . replace ( / ^ \s + | \s + $ / g, '' ) ;
125
- }
126
-
127
- return data ;
128
- }
129
-
130
- function getUrlInfo ( url ) {
131
- var protocol ,
132
- host ,
133
- relativeUrl ,
134
- protocolEndIndex ,
135
- urlParts ;
136
-
137
- if ( url . indexOf ( './' ) > - 1 || url . indexOf ( '../' ) > - 1 ) {
138
- // we have a complex relative url i.e. './api/products' or '../api/products
139
- var parser = document . createElement ( 'a' ) ;
140
- parser . href = url ;
141
- url = parser . href ;
142
- }
143
-
144
- if ( url . indexOf ( '://' ) > - 1 ) {
145
- protocolEndIndex = url . indexOf ( '://' ) + 3 ;
146
- urlParts = url . slice ( protocolEndIndex ) . split ( constants . forwardSlash ) ;
147
- // we have an absolute url
148
- protocol = url . substring ( 0 , protocolEndIndex ) ;
149
- // Get the host portion of the url from '://' to the next'/'
150
- // [https://www.somedomain.com/]api/messages
151
- host = urlParts [ 0 ] ;
152
- relativeUrl = ( function ( ) {
153
- delete urlParts [ 0 ] ;
154
- return urlParts . join ( constants . forwardSlash ) ;
155
- } ( ) ) ;
156
- } else {
157
- //we have a relative url
158
- relativeUrl = url ;
159
- protocol = $window . location . protocol ;
160
- host = $window . location . host ;
161
- }
162
-
163
- return {
164
- protocol : protocol ,
165
- host : host ,
166
- relativeUrl : relativeUrl
167
- } ;
168
- }
169
-
170
- function findResponseBoundary ( contentType ) {
171
- var boundaryText = 'boundary=' ,
172
- startIndex = contentType . indexOf ( boundaryText ) ,
173
- boundary = contentType . substring ( startIndex + boundaryText . length ) ;
174
-
175
- // the boundary might be quoted so remove the quotes
176
- boundary = boundary . replace ( / " / g, constants . emptyString ) ;
177
- return boundary ;
178
- }
179
-
180
- function convertDataToCorrectType ( contentType , dataStr ) {
181
- var data = dataStr ;
182
- contentType = contentType . toLowerCase ( ) ;
183
-
184
- if ( contentType . indexOf ( 'json' ) > - 1 ) {
185
- data = angular . fromJson ( dataStr ) ;
186
- }
187
-
188
- return data ;
189
- }
190
-
191
- function processResponse ( part , request , boundaryToken ) {
192
- var responseParts = part . split ( constants . newline ) ,
193
- result = {
194
- headers : { }
195
- } ,
196
- responsePart ,
197
- i , j , regex , lineParts , headerParts , parsedSpaceBetweenHeadersAndMessage = false ;
198
-
199
- for ( i = 0 ; i < responseParts . length ; i += 1 ) {
200
- responsePart = responseParts [ i ] ;
201
- if ( responsePart === constants . emptyString ) {
202
- parsedSpaceBetweenHeadersAndMessage = result . contentType !== undefined ;
203
- continue ;
204
- }
205
-
206
- if ( result . contentType === undefined && responsePart . indexOf ( '-Type' ) !== - 1 && responsePart . indexOf ( '; msgtype=response' ) === - 1 ) {
207
- result . contentType = responsePart . split ( constants . forwardSlash ) [ 1 ] ;
208
- } else if ( result . contentType !== undefined && parsedSpaceBetweenHeadersAndMessage === false ) {
209
- headerParts = responsePart . split ( constants . colon ) ;
210
- result . headers [ headerParts [ 0 ] ] = trim ( headerParts [ 1 ] ) ;
211
- } else if ( result . statusCode === undefined && responsePart . indexOf ( constants . httpVersion ) !== - 1 ) {
212
- lineParts = responsePart . split ( constants . singleSpace ) ;
213
- result . statusCode = parseInt ( lineParts [ 1 ] , 10 ) ;
214
- result . statusText = lineParts . slice ( 2 ) . join ( constants . singleSpace ) ;
215
- } else if ( result . data === undefined && parsedSpaceBetweenHeadersAndMessage ) {
216
- // need to get all the lines left apart from the last multipart seperator.
217
- result . data = '' ;
218
- j = 1 ;
219
- regex = new RegExp ( '--' + boundaryToken + '--' , 'i' ) ;
220
- while ( regex . test ( responsePart ) === false && ( ( i + j ) <= responseParts . length ) ) {
221
- result . data += responsePart ;
222
- responsePart = responseParts [ i + j ] ;
223
- j += 1 ;
224
- }
225
-
226
- result . data = convertDataToCorrectType ( result . contentType , result . data ) ;
227
- break ;
228
- }
229
- }
230
-
231
- result . headers [ constants . contentType ] = result . contentType ;
232
- return new window . ahb . HttpBatchResponseData ( request , result . statusCode , result . statusText , result . data , result . headers ) ;
84
+ return request . method === 'GET' ;
233
85
}
234
86
}
235
87
236
- HttpBatchAdapter . $inject = [
237
- '$document' ,
238
- '$window' ,
239
- 'httpBatchConfig'
240
- ] ;
241
-
242
- angular . module ( window . ahb . name ) . service ( 'httpBatchAdapter' , HttpBatchAdapter ) ;
88
+ angular . module ( window . ahb . name ) . service ( 'nodeJsMultiFetchAdapter' , NodeJsMultiFetchAdapter ) ;
0 commit comments