Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing FCM sendAll() API #453

Merged
merged 24 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c634e1f
Initial implementation for batch send
hiranya911 Feb 5, 2019
0ea4631
Unit tests for BatchRequestClient
hiranya911 Feb 5, 2019
26932e1
Finished sendAll() implementation
hiranya911 Feb 6, 2019
e063f4f
Fixed some lint errors
hiranya911 Feb 6, 2019
e68bf31
Fixed messaging test imports
hiranya911 Feb 6, 2019
ff24eee
Adding more tests
hiranya911 Feb 6, 2019
d3cc374
Increased test coverage
hiranya911 Feb 6, 2019
2b54313
Updated tests
hiranya911 Feb 6, 2019
2f96e3f
Implemented multipart parsing with dicer for performance
hiranya911 Feb 7, 2019
e65b8d1
Increased test coverage for HttpClient
hiranya911 Feb 7, 2019
ae23539
Added a test case for zlib
hiranya911 Feb 7, 2019
35c7131
Removed http-message-parser frm required dependencies
hiranya911 Feb 7, 2019
3c3c8d1
Added some documentation
hiranya911 Feb 7, 2019
3168d0e
Updated comments
hiranya911 Feb 7, 2019
b9a80af
Trigger CI
hiranya911 Feb 7, 2019
434bfe5
Fixed some typos; Reduced batch size limit to 100
hiranya911 Feb 19, 2019
80c44ba
Merge branch 'master' into hkj-fcm-batch
hiranya911 Feb 21, 2019
64eb6e6
More documentation and clean up
hiranya911 Mar 4, 2019
3addf55
Updated docs; Other code review feedback
hiranya911 Mar 11, 2019
b705eb5
Merge branch 'master' into hkj-fcm-batch
hiranya911 Mar 11, 2019
e701d4f
Handling malformed responses in parseHttpResponse()
hiranya911 Mar 11, 2019
997ff94
Implementing the sendMulticast() API for FCM (#473)
hiranya911 Mar 12, 2019
824d5bb
Merge branch 'master' of github.com:firebase/firebase-admin-node into…
hiranya911 Mar 14, 2019
146c7d6
Merge branch 'hkj-fcm-batch' of github.com:firebase/firebase-admin-no…
hiranya911 Mar 14, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 67 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@firebase/app": "^0.3.4",
"@firebase/database": "^0.3.6",
"@types/node": "^8.0.53",
"dicer": "^0.3.0",
"jsonwebtoken": "8.1.0",
"node-forge": "0.7.4"
},
Expand Down Expand Up @@ -89,6 +90,7 @@
"gulp-header": "^1.8.8",
"gulp-replace": "^0.5.4",
"gulp-typescript": "^3.2.4",
"http-message-parser": "^0.0.34",
"lodash": "^4.17.5",
"merge2": "^1.2.1",
"minimist": "^1.2.0",
Expand Down
16 changes: 16 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,26 @@ declare namespace admin.messaging {
errors: admin.FirebaseArrayIndexError[];
};

type BatchResponse = {
responses: admin.messaging.SendResponse[];
successCount: number;
failureCount: number;
}

type SendResponse = {
success: boolean;
messageId?: string;
error?: admin.FirebaseError;
};

interface Messaging {
app: admin.app.App;

send(message: admin.messaging.Message, dryRun?: boolean): Promise<string>;
sendAll(
messages: Array<admin.messaging.Message>,
dryRun?: boolean
): Promise<admin.messaging.BatchResponse>;
sendToDevice(
registrationToken: string | string[],
payload: admin.messaging.MessagingPayload,
Expand Down
114 changes: 114 additions & 0 deletions src/messaging/batch-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*!
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
HttpClient, HttpRequestConfig, HttpResponse, parseHttpResponse,
} from '../utils/api-request';

const PART_DELIMITER: string = '__END_OF_PART__';
const TEN_SECONDS_IN_MILLIS = 10000;

/**
* An HTTP client that can be used to make batch requests. This client is not tied to any service
* (FCM or otherwise). Therefore it can be used to make batch requests to any service that allows
* it. If this requirement ever arises we can move this implementation to the utils module
* where it can be easily shared among other modules.
*/
export class BatchRequestClient {

/**
* @param {HttpClient} httpClient The client that will be used to make HTTP calls.
* @param {string} batchUrl The URL that accepts batch requests.
* @param {object=} commonHeaders Optional headers that will be included in all requests.
*
* @constructor
*/
constructor(
private readonly httpClient: HttpClient,
private readonly batchUrl: string,
private readonly commonHeaders?: object) {
}

/**
* Sends the given array of sub requests as a single batch, and parses the results into an array
* of HttpResponse objects.
*
* @param {SubRequest[]} requests An array of sub requests to send.
* @return {Promise<HttpResponse[]>} A promise that resolves when the send operation is complete.
*/
public send(requests: SubRequest[]): Promise<HttpResponse[]> {
const requestHeaders = {
'Content-Type': `multipart/mixed; boundary=${PART_DELIMITER}`,
};
const request: HttpRequestConfig = {
method: 'POST',
url: this.batchUrl,
data: this.getMultipartPayload(requests),
headers: Object.assign({}, this.commonHeaders, requestHeaders),
timeout: TEN_SECONDS_IN_MILLIS,
};
return this.httpClient.send(request).then((response) => {
return response.multipart.map((buff) => {
return parseHttpResponse(buff, request);
});
});
}

private getMultipartPayload(requests: SubRequest[]): Buffer {
let buffer: string = '';
requests.forEach((request: SubRequest, idx: number) => {
buffer += createPart(request, PART_DELIMITER, idx);
});
buffer += `--${PART_DELIMITER}--\r\n`;
return Buffer.from(buffer, 'utf-8');
}
}

/**
* Represents a request that can be sent as part of an HTTP batch request.
*/
export interface SubRequest {
url: string;
body: object;
headers?: {[key: string]: any};
}
hiranya911 marked this conversation as resolved.
Show resolved Hide resolved

function createPart(request: SubRequest, delim: string, idx: number): string {
hiranya911 marked this conversation as resolved.
Show resolved Hide resolved
const serializedRequest: string = serializeSubRequest(request);
let part: string = `--${delim}\r\n`;
part += `Content-Length: ${serializedRequest.length}\r\n`;
part += 'Content-Type: application/http\r\n';
part += `content-id: ${idx + 1}\r\n`;
part += 'content-transfer-encoding: binary\r\n';
part += '\r\n';
part += `${serializedRequest}\r\n`;
return part;
}

function serializeSubRequest(request: SubRequest): string {
hiranya911 marked this conversation as resolved.
Show resolved Hide resolved
const requestBody: string = JSON.stringify(request.body);
let messagePayload: string = `POST ${request.url} HTTP/1.1\r\n`;
messagePayload += `Content-Length: ${requestBody.length}\r\n`;
messagePayload += 'Content-Type: application/json; charset=UTF-8\r\n';
if (request.headers) {
Object.keys(request.headers).forEach((key) => {
messagePayload += `${key}: ${request.headers[key]}\r\n`;
});
}
messagePayload += '\r\n';
messagePayload += requestBody;
return messagePayload;
}
Loading