|
| 1 | +/*! |
| 2 | + * Copyright 2019 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { |
| 18 | + HttpClient, HttpRequestConfig, HttpResponse, parseHttpResponse, |
| 19 | +} from '../utils/api-request'; |
| 20 | + |
| 21 | +const PART_BOUNDARY: string = '__END_OF_PART__'; |
| 22 | +const TEN_SECONDS_IN_MILLIS = 10000; |
| 23 | + |
| 24 | +/** |
| 25 | + * Represents a request that can be sent as part of an HTTP batch request. |
| 26 | + */ |
| 27 | +export interface SubRequest { |
| 28 | + url: string; |
| 29 | + body: object; |
| 30 | + headers?: {[key: string]: any}; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * An HTTP client that can be used to make batch requests. This client is not tied to any service |
| 35 | + * (FCM or otherwise). Therefore it can be used to make batch requests to any service that allows |
| 36 | + * it. If this requirement ever arises we can move this implementation to the utils module |
| 37 | + * where it can be easily shared among other modules. |
| 38 | + */ |
| 39 | +export class BatchRequestClient { |
| 40 | + |
| 41 | + /** |
| 42 | + * @param {HttpClient} httpClient The client that will be used to make HTTP calls. |
| 43 | + * @param {string} batchUrl The URL that accepts batch requests. |
| 44 | + * @param {object=} commonHeaders Optional headers that will be included in all requests. |
| 45 | + * |
| 46 | + * @constructor |
| 47 | + */ |
| 48 | + constructor( |
| 49 | + private readonly httpClient: HttpClient, |
| 50 | + private readonly batchUrl: string, |
| 51 | + private readonly commonHeaders?: object) { |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Sends the given array of sub requests as a single batch, and parses the results into an array |
| 56 | + * of HttpResponse objects. |
| 57 | + * |
| 58 | + * @param {SubRequest[]} requests An array of sub requests to send. |
| 59 | + * @return {Promise<HttpResponse[]>} A promise that resolves when the send operation is complete. |
| 60 | + */ |
| 61 | + public send(requests: SubRequest[]): Promise<HttpResponse[]> { |
| 62 | + const requestHeaders = { |
| 63 | + 'Content-Type': `multipart/mixed; boundary=${PART_BOUNDARY}`, |
| 64 | + }; |
| 65 | + const request: HttpRequestConfig = { |
| 66 | + method: 'POST', |
| 67 | + url: this.batchUrl, |
| 68 | + data: this.getMultipartPayload(requests), |
| 69 | + headers: Object.assign({}, this.commonHeaders, requestHeaders), |
| 70 | + timeout: TEN_SECONDS_IN_MILLIS, |
| 71 | + }; |
| 72 | + return this.httpClient.send(request).then((response) => { |
| 73 | + return response.multipart.map((buff) => { |
| 74 | + return parseHttpResponse(buff, request); |
| 75 | + }); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + private getMultipartPayload(requests: SubRequest[]): Buffer { |
| 80 | + let buffer: string = ''; |
| 81 | + requests.forEach((request: SubRequest, idx: number) => { |
| 82 | + buffer += createPart(request, PART_BOUNDARY, idx); |
| 83 | + }); |
| 84 | + buffer += `--${PART_BOUNDARY}--\r\n`; |
| 85 | + return Buffer.from(buffer, 'utf-8'); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Creates a single part in a multipart HTTP request body. The part consists of several headers |
| 91 | + * followed by the serialized sub request as the body. As per the requirements of the FCM batch |
| 92 | + * API, sets the content-type header to application/http, and the content-transfer-encoding to |
| 93 | + * binary. |
| 94 | + * |
| 95 | + * @param {SubRequest} request A sub request that will be used to populate the part. |
| 96 | + * @param {string} boundary Multipart boundary string. |
| 97 | + * @param {number} idx An index number that is used to set the content-id header. |
| 98 | + * @return {string} The part as a string that can be included in the HTTP body. |
| 99 | + */ |
| 100 | +function createPart(request: SubRequest, boundary: string, idx: number): string { |
| 101 | + const serializedRequest: string = serializeSubRequest(request); |
| 102 | + let part: string = `--${boundary}\r\n`; |
| 103 | + part += `Content-Length: ${serializedRequest.length}\r\n`; |
| 104 | + part += 'Content-Type: application/http\r\n'; |
| 105 | + part += `content-id: ${idx + 1}\r\n`; |
| 106 | + part += 'content-transfer-encoding: binary\r\n'; |
| 107 | + part += '\r\n'; |
| 108 | + part += `${serializedRequest}\r\n`; |
| 109 | + return part; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Serializes a sub request into a string that can be embedded in a multipart HTTP request. The |
| 114 | + * format of the string is the wire format of a typical HTTP request, consisting of a header and a |
| 115 | + * body. |
| 116 | + * |
| 117 | + * @param request {SubRequest} The sub request to be serialized. |
| 118 | + * @return {string} String representation of the SubRequest. |
| 119 | + */ |
| 120 | +function serializeSubRequest(request: SubRequest): string { |
| 121 | + const requestBody: string = JSON.stringify(request.body); |
| 122 | + let messagePayload: string = `POST ${request.url} HTTP/1.1\r\n`; |
| 123 | + messagePayload += `Content-Length: ${requestBody.length}\r\n`; |
| 124 | + messagePayload += 'Content-Type: application/json; charset=UTF-8\r\n'; |
| 125 | + if (request.headers) { |
| 126 | + Object.keys(request.headers).forEach((key) => { |
| 127 | + messagePayload += `${key}: ${request.headers[key]}\r\n`; |
| 128 | + }); |
| 129 | + } |
| 130 | + messagePayload += '\r\n'; |
| 131 | + messagePayload += requestBody; |
| 132 | + return messagePayload; |
| 133 | +} |
0 commit comments