Skip to content

Commit 7a802d9

Browse files
committed
Resolve gemini review comments and add a unit test
1 parent 9e5d4cf commit 7a802d9

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

src/messaging/messaging-errors-internal.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ export function createFirebaseError(err: RequestResponseError): FirebaseMessagin
3333
const errorMessage = getErrorMessage(json);
3434
if (errorCode === 'UNREGISTERED' || errorCode === 'NOT_FOUND') {
3535
let requestData = err.response.config && err.response.config.data;
36-
if (typeof requestData === 'string') {
36+
if (requestData && (typeof requestData === 'string' || Buffer.isBuffer(requestData))) {
3737
try {
38-
requestData = JSON.parse(requestData);
38+
const strData = typeof requestData === 'string' ? requestData : requestData.toString('utf-8');
39+
requestData = JSON.parse(strData);
3940
} catch (e) {
4041
// Ignore parsing errors.
4142
}

src/messaging/messaging.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,18 @@ export class Messaging {
324324

325325
const { tokens, fids, ...baseMessage } = copy;
326326

327-
const tokenList: string[] = tokens || [];
328-
const fidList: string[] = fids || [];
329-
330-
if ('tokens' in copy && !validator.isArray(copy.tokens)) {
327+
if (tokens !== undefined && !validator.isArray(tokens)) {
331328
throw new FirebaseMessagingError(
332329
MessagingClientErrorCode.INVALID_ARGUMENT, 'tokens must be a valid array');
333330
}
334-
if ('fids' in copy && !validator.isArray(copy.fids)) {
331+
if (fids !== undefined && !validator.isArray(fids)) {
335332
throw new FirebaseMessagingError(
336333
MessagingClientErrorCode.INVALID_ARGUMENT, 'fids must be a valid array');
337334
}
335+
336+
const tokenList: string[] = tokens || [];
337+
const fidList: string[] = fids || [];
338+
338339
if (tokenList.length === 0 && fidList.length === 0) {
339340
throw new FirebaseMessagingError(
340341
MessagingClientErrorCode.INVALID_ARGUMENT, 'Either tokens or fids must be a non-empty array');

test/unit/messaging/messaging.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,18 @@ describe('Messaging', () => {
12241224
});
12251225
});
12261226

1227+
it('should handle explicitly undefined tokens or fids gracefully without throwing', () => {
1228+
stub = sinon.stub(messaging, 'sendEach').resolves(mockResponse);
1229+
return messaging.sendEachForMulticast({ fids: ['f1'], tokens: undefined } as any)
1230+
.then((response: BatchResponse) => {
1231+
expect(response).to.deep.equal(mockResponse);
1232+
expect(stub).to.have.been.calledOnce;
1233+
const messages: Message[] = stub!.args[0][0];
1234+
expect(messages.length).to.equal(1);
1235+
expect((messages[0] as FidMessage).fid).to.equal('f1');
1236+
});
1237+
});
1238+
12271239
it('should create multiple messages using mixed tokens and fids', () => {
12281240
stub = sinon.stub(messaging, 'sendEach').resolves(mockResponse);
12291241
const tokens = ['t1', 't2'];

0 commit comments

Comments
 (0)