|
1 | 1 | /*
|
2 |
| - * angular-http-batcher - v1.11.1 - 2015-08-11 |
| 2 | + * angular-http-batcher - v1.11.2 - 2015-08-21 |
3 | 3 | * https://github.com/jonsamwell/angular-http-batcher
|
4 | 4 | * Copyright (c) 2015 Jon Samwell
|
5 | 5 | */
|
@@ -174,6 +174,355 @@ function HttpBatchConfigFn() {
|
174 | 174 |
|
175 | 175 | angular.module(window.ahb.name).provider('httpBatchConfig', HttpBatchConfigFn);
|
176 | 176 |
|
| 177 | +/** |
| 178 | + * |
| 179 | + * @param request |
| 180 | + * @param statusCode |
| 181 | + * @param statusText |
| 182 | + * @param data |
| 183 | + * @param headers - object or string |
| 184 | + * @constructor |
| 185 | + */ |
| 186 | +function HttpBatchResponseData(request, statusCode, statusText, data, headers) { |
| 187 | + this.request = request; |
| 188 | + this.statusCode = statusCode; |
| 189 | + this.statusText = statusText; |
| 190 | + this.data = data; |
| 191 | + this.headers = headers; |
| 192 | +} |
| 193 | + |
| 194 | +window.ahb.HttpBatchResponseData = HttpBatchResponseData; |
| 195 | + |
| 196 | +function HttpBatchAdapter($document, $window, httpBatchConfig) { |
| 197 | + var self = this, |
| 198 | + constants = { |
| 199 | + httpVersion: 'HTTP/1.1', |
| 200 | + contentType: 'Content-Type', |
| 201 | + newline: '\r\n', |
| 202 | + emptyString: '', |
| 203 | + singleSpace: ' ', |
| 204 | + forwardSlash: '/', |
| 205 | + doubleDash: '--', |
| 206 | + colon: ':' |
| 207 | + }; |
| 208 | + |
| 209 | + self.key = 'httpBatchAdapter'; |
| 210 | + self.buildRequest = buildRequestFn; |
| 211 | + self.parseResponse = parseResponseFn; |
| 212 | + self.canBatchRequest = canBatchRequestFn; |
| 213 | + |
| 214 | + /** |
| 215 | + * Builds the single batch request from the given batch of pending requests. |
| 216 | + * Returns a standard angular httpConfig object that will be use to invoke the $http service. |
| 217 | + * See: |
| 218 | + * https://developers.google.com/storage/docs/json_api/v1/how-tos/batch |
| 219 | + * http://blogs.msdn.com/b/webdev/archive/2013/11/01/introducing-batch-support-in-web-api-and-web-api-odata.aspx |
| 220 | + * |
| 221 | + * @param requests - the collection of pending http request to build into a single http batch request. |
| 222 | + * @param config - the http batch config. |
| 223 | + * @returns {object} - a http config object. |
| 224 | + */ |
| 225 | + function buildRequestFn(requests, config) { |
| 226 | + var boundary = httpBatchConfig.calculateBoundary(), |
| 227 | + httpConfig = { |
| 228 | + method: 'POST', |
| 229 | + url: config.batchEndpointUrl, |
| 230 | + cache: false, |
| 231 | + headers: config.batchRequestHeaders || {} |
| 232 | + }, |
| 233 | + batchBody = [], |
| 234 | + urlInfo, i, request, header; |
| 235 | + |
| 236 | + httpConfig.headers[constants.contentType] = 'multipart/mixed; boundary=' + boundary; |
| 237 | + |
| 238 | + for (i = 0; i < requests.length; i += 1) { |
| 239 | + request = requests[i]; |
| 240 | + urlInfo = getUrlInfo(request.url); |
| 241 | + |
| 242 | + batchBody.push(constants.doubleDash + boundary); |
| 243 | + if (config.batchPartRequestHeaders) { |
| 244 | + for (header in config.batchPartRequestHeaders) { |
| 245 | + batchBody.push(header + constants.colon + constants.singleSpace + config.batchPartRequestHeaders[header]); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + batchBody.push('Content-Type: application/http; msgtype=request', constants.emptyString); |
| 250 | + |
| 251 | + batchBody.push(request.method + ' ' + urlInfo.relativeUrl + ' ' + constants.httpVersion); |
| 252 | + batchBody.push('Host: ' + urlInfo.host); |
| 253 | + |
| 254 | + for (header in request.headers) { |
| 255 | + batchBody.push(header + constants.colon + constants.singleSpace + request.headers[header]); |
| 256 | + } |
| 257 | + |
| 258 | + if (config.sendCookies === true && $document[0].cookie && $document[0].cookie.length > 0) { |
| 259 | + batchBody.push('Cookie: ' + $document[0].cookie); |
| 260 | + } |
| 261 | + |
| 262 | + batchBody.push(constants.emptyString); |
| 263 | + |
| 264 | + if (request.data) { |
| 265 | + batchBody.push(request.data); |
| 266 | + } |
| 267 | + |
| 268 | + batchBody.push(constants.emptyString); |
| 269 | + } |
| 270 | + |
| 271 | + batchBody.push(constants.doubleDash + boundary + constants.doubleDash); |
| 272 | + httpConfig.data = batchBody.join(constants.newline); |
| 273 | + return httpConfig; |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * Parses the raw response into an array of HttpBatchResponseData objects. If is this methods job |
| 278 | + * to parse the response and match it up with the orginal request object. |
| 279 | + * @param rawResponse |
| 280 | + * @param config |
| 281 | + * @returns {Array.HttpBatchResponseData[]} |
| 282 | + */ |
| 283 | + function parseResponseFn(requests, rawResponse, config) { |
| 284 | + var batchResponses = [], |
| 285 | + boundaryToken = findResponseBoundary(rawResponse.headers()['content-type']), |
| 286 | + parts = rawResponse.data.split(constants.doubleDash + boundaryToken + constants.newline), |
| 287 | + i, |
| 288 | + part, |
| 289 | + responseCount = 0; |
| 290 | + |
| 291 | + for (i = 0; i < parts.length; i += 1) { |
| 292 | + part = parts[i]; |
| 293 | + if (part !== constants.emptyString) { |
| 294 | + batchResponses.push(processResponse(part, requests[responseCount], boundaryToken)); |
| 295 | + responseCount += 1; |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + return batchResponses; |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * Gaurd method to ensure the adapter supports this given request. |
| 304 | + * @param request |
| 305 | + * @param config |
| 306 | + * @returns {boolean} false to indicate the request type is not supported. |
| 307 | + */ |
| 308 | + function canBatchRequestFn(request, config) { |
| 309 | + return true; |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * mainly here to polyfill ie8 :-( |
| 314 | + */ |
| 315 | + function trim(data) { |
| 316 | + if (data.trim) { |
| 317 | + data = data.trim(); |
| 318 | + } else { |
| 319 | + data = data.replace(/^\s+|\s+$/g, ''); |
| 320 | + } |
| 321 | + |
| 322 | + return data; |
| 323 | + } |
| 324 | + |
| 325 | + function getUrlInfo(url) { |
| 326 | + var protocol, |
| 327 | + host, |
| 328 | + relativeUrl, |
| 329 | + protocolEndIndex, |
| 330 | + urlParts; |
| 331 | + |
| 332 | + if (url.indexOf('./') > -1 || url.indexOf('../') > -1) { |
| 333 | + // we have a complex relative url i.e. './api/products' or '../api/products |
| 334 | + var parser = document.createElement('a'); |
| 335 | + parser.href = url; |
| 336 | + url = parser.href; |
| 337 | + } |
| 338 | + |
| 339 | + if (url.indexOf('://') > -1) { |
| 340 | + protocolEndIndex = url.indexOf('://') + 3; |
| 341 | + urlParts = url.slice(protocolEndIndex).split(constants.forwardSlash); |
| 342 | + // we have an absolute url |
| 343 | + protocol = url.substring(0, protocolEndIndex); |
| 344 | + // Get the host portion of the url from '://' to the next'/' |
| 345 | + // [https://www.somedomain.com/]api/messages |
| 346 | + host = urlParts[0]; |
| 347 | + relativeUrl = (function () { |
| 348 | + delete urlParts[0]; |
| 349 | + return urlParts.join(constants.forwardSlash); |
| 350 | + }()); |
| 351 | + } else { |
| 352 | + //we have a relative url |
| 353 | + relativeUrl = url; |
| 354 | + protocol = $window.location.protocol; |
| 355 | + host = $window.location.host; |
| 356 | + } |
| 357 | + |
| 358 | + return { |
| 359 | + protocol: protocol, |
| 360 | + host: host, |
| 361 | + relativeUrl: relativeUrl |
| 362 | + }; |
| 363 | + } |
| 364 | + |
| 365 | + function findResponseBoundary(contentType) { |
| 366 | + var boundaryText = 'boundary=', |
| 367 | + startIndex = contentType.indexOf(boundaryText), |
| 368 | + boundary = contentType.substring(startIndex + boundaryText.length); |
| 369 | + |
| 370 | + // the boundary might be quoted so remove the quotes |
| 371 | + boundary = boundary.replace(/"/g, constants.emptyString); |
| 372 | + return boundary; |
| 373 | + } |
| 374 | + |
| 375 | + function convertDataToCorrectType(contentType, dataStr) { |
| 376 | + var data = dataStr; |
| 377 | + contentType = contentType.toLowerCase(); |
| 378 | + |
| 379 | + if (contentType.indexOf('json') > -1) { |
| 380 | + data = angular.fromJson(dataStr); |
| 381 | + } |
| 382 | + |
| 383 | + return data; |
| 384 | + } |
| 385 | + |
| 386 | + function processResponse(part, request, boundaryToken) { |
| 387 | + var responseParts = part.split(constants.newline), |
| 388 | + result = { |
| 389 | + headers: {} |
| 390 | + }, |
| 391 | + responsePart, |
| 392 | + i, j, regex, lineParts, headerParts, parsedSpaceBetweenHeadersAndMessage = false; |
| 393 | + |
| 394 | + for (i = 0; i < responseParts.length; i += 1) { |
| 395 | + responsePart = responseParts[i]; |
| 396 | + if (responsePart === constants.emptyString) { |
| 397 | + parsedSpaceBetweenHeadersAndMessage = result.contentType !== undefined; |
| 398 | + continue; |
| 399 | + } |
| 400 | + |
| 401 | + if (result.contentType === undefined && responsePart.indexOf('-Type') !== -1 && responsePart.indexOf('; msgtype=response') === -1) { |
| 402 | + result.contentType = responsePart.split(constants.forwardSlash)[1]; |
| 403 | + } else if (result.contentType !== undefined && parsedSpaceBetweenHeadersAndMessage === false) { |
| 404 | + headerParts = responsePart.split(constants.colon); |
| 405 | + result.headers[headerParts[0]] = trim(headerParts[1]); |
| 406 | + } else if (result.statusCode === undefined && responsePart.indexOf(constants.httpVersion) !== -1) { |
| 407 | + lineParts = responsePart.split(constants.singleSpace); |
| 408 | + result.statusCode = parseInt(lineParts[1], 10); |
| 409 | + result.statusText = lineParts.slice(2).join(constants.singleSpace); |
| 410 | + } else if (result.data === undefined && parsedSpaceBetweenHeadersAndMessage) { |
| 411 | + // need to get all the lines left apart from the last multipart seperator. |
| 412 | + result.data = ''; |
| 413 | + j = 1; |
| 414 | + regex = new RegExp('--' + boundaryToken + '--', 'i'); |
| 415 | + while (regex.test(responsePart) === false && ((i + j) <= responseParts.length)) { |
| 416 | + result.data += responsePart; |
| 417 | + responsePart = responseParts[i + j]; |
| 418 | + j += 1; |
| 419 | + } |
| 420 | + |
| 421 | + result.data = convertDataToCorrectType(result.contentType, result.data); |
| 422 | + break; |
| 423 | + } |
| 424 | + } |
| 425 | + |
| 426 | + result.headers[constants.contentType] = result.contentType; |
| 427 | + return new window.ahb.HttpBatchResponseData(request, result.statusCode, result.statusText, result.data, result.headers); |
| 428 | + } |
| 429 | +} |
| 430 | + |
| 431 | +HttpBatchAdapter.$inject = [ |
| 432 | + '$document', |
| 433 | + '$window', |
| 434 | + 'httpBatchConfig' |
| 435 | +]; |
| 436 | + |
| 437 | +angular.module(window.ahb.name).service('httpBatchAdapter', HttpBatchAdapter); |
| 438 | + |
| 439 | +function NodeJsMultiFetchAdapter() { |
| 440 | + var self = this; |
| 441 | + |
| 442 | + self.key = 'nodeJsMultiFetchAdapter'; |
| 443 | + self.buildRequest = buildRequestFn; |
| 444 | + self.parseResponse = parseResponseFn; |
| 445 | + self.canBatchRequest = canBatchRequestFn; |
| 446 | + |
| 447 | + /** |
| 448 | + * Builds the single batch request from the given batch of pending requests. |
| 449 | + * Returns a standard angular httpConfig object that will be use to invoke the $http service. |
| 450 | + * See: |
| 451 | + * https://developers.google.com/storage/docs/json_api/v1/how-tos/batch |
| 452 | + * http://blogs.msdn.com/b/webdev/archive/2013/11/01/introducing-batch-support-in-web-api-and-web-api-odata.aspx |
| 453 | + * |
| 454 | + * @param requests - the collection of pending http request to build into a single http batch request. |
| 455 | + * @param config - the http batch config. |
| 456 | + * @returns {object} - a http config object. |
| 457 | + */ |
| 458 | + function buildRequestFn(requests, config) { |
| 459 | + var httpConfig = { |
| 460 | + method: 'GET', |
| 461 | + url: config.batchEndpointUrl + '?', |
| 462 | + cache: false, |
| 463 | + headers: config.batchRequestHeaders || {} |
| 464 | + }, |
| 465 | + encodedUrl, i, request, |
| 466 | + urlParts; |
| 467 | + |
| 468 | + for (i = 0; i < requests.length; i += 1) { |
| 469 | + request = requests[i]; |
| 470 | + urlParts = request.url.split('?'); |
| 471 | + |
| 472 | + encodedUrl = urlParts[0].replace(config.serviceUrl, ''); |
| 473 | + if (urlParts.length > 1) { |
| 474 | + encodedUrl += '?' + encodeURIComponent(urlParts[1]); |
| 475 | + } |
| 476 | + |
| 477 | + if (i > 0) { |
| 478 | + httpConfig.url += '&'; |
| 479 | + } |
| 480 | + |
| 481 | + httpConfig.url += i.toString() + '=' + encodedUrl; |
| 482 | + } |
| 483 | + |
| 484 | + return httpConfig; |
| 485 | + } |
| 486 | + |
| 487 | + /** |
| 488 | + * Parses the raw response into an array of HttpBatchResponseData objects. If is this methods job |
| 489 | + * to parse the response and match it up with the orginal request object. |
| 490 | + * @param rawResponse |
| 491 | + * @returns {Array.HttpBatchResponseData[]} |
| 492 | + */ |
| 493 | + function parseResponseFn(requests, rawResponse) { |
| 494 | + var batchResponses = [], |
| 495 | + i, request, |
| 496 | + responseData = rawResponse.data, |
| 497 | + dataPart; |
| 498 | + |
| 499 | + for (i = 0; i < requests.length; i += 1) { |
| 500 | + request = requests[i]; |
| 501 | + dataPart = responseData[i.toString()]; |
| 502 | + |
| 503 | + batchResponses.push(new window.ahb.HttpBatchResponseData( |
| 504 | + request, |
| 505 | + dataPart.statusCode, |
| 506 | + '', |
| 507 | + dataPart.body, |
| 508 | + dataPart.headers)); |
| 509 | + } |
| 510 | + |
| 511 | + return batchResponses; |
| 512 | + } |
| 513 | + |
| 514 | + /** |
| 515 | + * Gaurd method to ensure the adapter supports this given request. |
| 516 | + * @param request |
| 517 | + * @returns {boolean} false to indicate the request type is not supported. |
| 518 | + */ |
| 519 | + function canBatchRequestFn(request) { |
| 520 | + return request.method === 'GET'; |
| 521 | + } |
| 522 | +} |
| 523 | + |
| 524 | +angular.module(window.ahb.name).service('nodeJsMultiFetchAdapter', NodeJsMultiFetchAdapter); |
| 525 | + |
177 | 526 | function convertHeadersToString(headers) {
|
178 | 527 | var property,
|
179 | 528 | result = '';
|
|
0 commit comments