mirror of
https://github.com/bitwarden/clients.git
synced 2026-11-16 07:03:29 +00:00
[SRE-2516] Do not event upload request bodies of unlimited size (#15190)
* Do not event upload request bodies of unlimited size we're seeing 10s of MB event upload payloads, chunk to 300 events per upload. Event sizes are roughly 150 bytes, so this limits to about 50 Kb. There may be rate limiting impacts here initially for clients that have build up a gigantic backlog and those are worked down. * Test event upload logic * Prompt users to prefer workspace ts version
This commit is contained in:
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -60,5 +60,6 @@
|
||||
"rust-analyzer.linkedProjects": ["apps/desktop/desktop_native/Cargo.toml"],
|
||||
"eslint.useFlatConfig": true,
|
||||
"tailwindCSS.experimental.classRegex": ["[\"'`]([^\"'`\\n]*tw-[^\"'`\\n]*)[\"'`]"],
|
||||
"js/ts.tsdk.path": "node_modules/typescript/lib"
|
||||
"js/ts.tsdk.path": "node_modules/typescript/lib",
|
||||
"js/ts.tsdk.promptToUseWorkspaceVersion": true
|
||||
}
|
||||
|
||||
@@ -431,8 +431,9 @@ export abstract class ApiService {
|
||||
* Posts events for a user
|
||||
* @param request The array of events to upload
|
||||
* @param userId The optional user id the events belong to. If no user id is provided the active user id is used.
|
||||
* @returns The list of events that failed to upload, or an empty array if all events were uploaded successfully.
|
||||
*/
|
||||
abstract postEventsCollect(request: EventRequest[], userId?: UserId): Promise<any>;
|
||||
abstract postEventsCollect(request: EventRequest[], userId?: UserId): Promise<EventRequest[]>;
|
||||
|
||||
abstract deleteSsoUser(organizationId: string): Promise<void>;
|
||||
abstract getSsoUserIdentifier(): Promise<string>;
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
import { mock, MockProxy } from "jest-mock-extended";
|
||||
import { of } from "rxjs";
|
||||
|
||||
import { FakeAccountService, FakeStateProvider, mockAccountServiceWith } from "../../../../spec";
|
||||
import { ApiService } from "../../../abstractions/api.service";
|
||||
import { AuthService } from "../../../auth/abstractions/auth.service";
|
||||
import { AuthenticationStatus } from "../../../auth/enums/authentication-status";
|
||||
import { LogService } from "../../../platform/abstractions/log.service";
|
||||
import { TaskSchedulerService } from "../../../platform/scheduling";
|
||||
import { UserId } from "../../../types/guid";
|
||||
import { EventType } from "../enums/event-type.enum";
|
||||
import { EventData } from "../models/data/event.data";
|
||||
|
||||
import { EventUploadService } from "./event-upload.service";
|
||||
import { EVENT_COLLECTION } from "./key-definitions";
|
||||
|
||||
describe("event upload service", () => {
|
||||
let eventUploadService: EventUploadService;
|
||||
let apiService: MockProxy<ApiService>;
|
||||
let stateProvider: FakeStateProvider;
|
||||
let logService: MockProxy<LogService>;
|
||||
let authService: MockProxy<AuthService>;
|
||||
let taskSchedulerService: MockProxy<TaskSchedulerService>;
|
||||
const userId = "00000000-0000-0000-0000-000000000001" as UserId;
|
||||
let accountService: FakeAccountService;
|
||||
|
||||
beforeEach(() => {
|
||||
apiService = mock();
|
||||
accountService = mockAccountServiceWith(userId);
|
||||
stateProvider = new FakeStateProvider(accountService);
|
||||
logService = mock();
|
||||
authService = mock();
|
||||
taskSchedulerService = mock();
|
||||
|
||||
eventUploadService = new EventUploadService(
|
||||
apiService,
|
||||
stateProvider,
|
||||
logService,
|
||||
authService,
|
||||
taskSchedulerService,
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
describe("uploadEvents", () => {
|
||||
describe("unlocked user", () => {
|
||||
beforeEach(() => {
|
||||
authService.authStatusFor$.mockReturnValue(of(AuthenticationStatus.Unlocked));
|
||||
});
|
||||
|
||||
it("attempts upload of all existing events", async () => {
|
||||
const events = makeEventData(3);
|
||||
await stateProvider.setUserState(EVENT_COLLECTION, events, userId);
|
||||
|
||||
await eventUploadService.uploadEvents(userId);
|
||||
|
||||
expect(apiService.postEventsCollect).toHaveBeenCalledWith(
|
||||
events.map((e) => eventUploadService.eventDataToEventRequest(e)),
|
||||
userId,
|
||||
);
|
||||
});
|
||||
|
||||
it("re-stores events returned from api upload", async () => {
|
||||
const events = makeEventData(3);
|
||||
await stateProvider.setUserState(EVENT_COLLECTION, events, userId);
|
||||
|
||||
const response = events.slice(1);
|
||||
apiService.postEventsCollect.mockResolvedValueOnce(response);
|
||||
|
||||
await eventUploadService.uploadEvents(userId);
|
||||
|
||||
expect(stateProvider.mock.setUserState).toHaveBeenCalledWith(
|
||||
EVENT_COLLECTION,
|
||||
response,
|
||||
userId,
|
||||
);
|
||||
});
|
||||
|
||||
it("re-stores all events if api upload throws", async () => {
|
||||
const events = makeEventData(3);
|
||||
await stateProvider.setUserState(EVENT_COLLECTION, events, userId);
|
||||
|
||||
apiService.postEventsCollect.mockRejectedValueOnce(new Error("API error"));
|
||||
|
||||
await eventUploadService.uploadEvents(userId);
|
||||
|
||||
expect(stateProvider.mock.setUserState).toHaveBeenCalledWith(
|
||||
EVENT_COLLECTION,
|
||||
events,
|
||||
userId,
|
||||
);
|
||||
});
|
||||
|
||||
it("does not store events if api upload is successful and no events are returned", async () => {
|
||||
const events = makeEventData(3);
|
||||
await stateProvider.setUserState(EVENT_COLLECTION, events, userId);
|
||||
// remove the above call to the mock
|
||||
stateProvider.mock.setUserState.mockClear();
|
||||
|
||||
apiService.postEventsCollect.mockResolvedValueOnce([]);
|
||||
|
||||
await eventUploadService.uploadEvents(userId);
|
||||
|
||||
expect(stateProvider.mock.setUserState).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function makeEventData(count: number): EventData[] {
|
||||
const events: EventData[] = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
events.push(
|
||||
EventData.fromJSON({
|
||||
type: EventType.Cipher_ClientViewed,
|
||||
date: new Date().toISOString(),
|
||||
cipherId: "00000000-0000-0000-0000-000000000001",
|
||||
organizationId: null!,
|
||||
}),
|
||||
);
|
||||
}
|
||||
return events;
|
||||
}
|
||||
@@ -70,21 +70,39 @@ export class EventUploadService implements EventUploadServiceAbstraction {
|
||||
if (eventCollection == null || eventCollection.length === 0) {
|
||||
return;
|
||||
}
|
||||
const request = eventCollection.map((e) => {
|
||||
const req = new EventRequest();
|
||||
req.type = e.type;
|
||||
req.cipherId = e.cipherId;
|
||||
req.date = e.date;
|
||||
req.organizationId = e.organizationId;
|
||||
return req;
|
||||
const eventRequests = eventCollection.map((e) => {
|
||||
return this.eventDataToEventRequest(e);
|
||||
});
|
||||
|
||||
let failedEvents: EventRequest[];
|
||||
try {
|
||||
await this.apiService.postEventsCollect(request, userId);
|
||||
failedEvents = await this.apiService.postEventsCollect(eventRequests, userId);
|
||||
} catch (e) {
|
||||
this.logService.error(e);
|
||||
// Add the events back to state if there was an error and they were not uploaded.
|
||||
await this.stateProvider.setUserState(EVENT_COLLECTION, eventCollection, userId);
|
||||
failedEvents = eventRequests;
|
||||
}
|
||||
|
||||
// Add any events that failed to upload back to state.
|
||||
if (failedEvents && failedEvents.length > 0) {
|
||||
const failedEventData = failedEvents.map((e) => {
|
||||
const eventData = new EventData();
|
||||
eventData.type = e.type;
|
||||
eventData.cipherId = e.cipherId;
|
||||
eventData.date = e.date;
|
||||
eventData.organizationId = e.organizationId;
|
||||
return eventData;
|
||||
});
|
||||
await this.stateProvider.setUserState(EVENT_COLLECTION, failedEventData, userId);
|
||||
}
|
||||
}
|
||||
|
||||
eventDataToEventRequest(event: EventData): EventRequest {
|
||||
const req = new EventRequest();
|
||||
req.type = event.type;
|
||||
req.cipherId = event.cipherId;
|
||||
req.date = event.date;
|
||||
req.organizationId = event.organizationId;
|
||||
return req;
|
||||
}
|
||||
|
||||
/** Return user's events and then clear them from state
|
||||
|
||||
@@ -1250,4 +1250,42 @@ describe("Utils Service", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("chunkArray", () => {
|
||||
it("handles empty array", () => {
|
||||
const result = Utils.chunkArray([], 3);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it("handles null array", () => {
|
||||
const result = Utils.chunkArray(null!, 3);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it.each([0, -1, null!])("handles errors on invalid chunk sizes", (chunkSize: number) => {
|
||||
expect(() => Utils.chunkArray([1, 2, 3], chunkSize)).toThrow(
|
||||
`Chunk size must be greater than 0`,
|
||||
);
|
||||
});
|
||||
|
||||
it("handles chunk size of 1", () => {
|
||||
const result = Utils.chunkArray([1, 2, 3], 1);
|
||||
expect(result).toEqual([[1], [2], [3]]);
|
||||
});
|
||||
|
||||
it("handles chunk size equal to array length", () => {
|
||||
const result = Utils.chunkArray([1, 2, 3], 3);
|
||||
expect(result).toEqual([[1, 2, 3]]);
|
||||
});
|
||||
|
||||
it("handles chunk size greater than array length", () => {
|
||||
const result = Utils.chunkArray([1, 2, 3], 5);
|
||||
expect(result).toEqual([[1, 2, 3]]);
|
||||
});
|
||||
|
||||
it("handles chunk size less than array length", () => {
|
||||
const result = Utils.chunkArray([1, 2, 3, 4, 5], 2);
|
||||
expect(result).toEqual([[1, 2], [3, 4], [5]]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import * as path from "path";
|
||||
import { Buffer as BufferLib } from "buffer/";
|
||||
import { Observable, of, switchMap } from "rxjs";
|
||||
import { getHostname, parse } from "tldts";
|
||||
import { Merge } from "type-fest";
|
||||
import { Merge, NonNegative } from "type-fest";
|
||||
|
||||
import "core-js/proposals/array-buffer-base64";
|
||||
|
||||
@@ -848,6 +848,21 @@ export class Utils {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static chunkArray<T>(a: T[], chunkSize: NonNegative<number>): T[][] {
|
||||
if (chunkSize <= 0) {
|
||||
throw new Error("Chunk size must be greater than 0");
|
||||
}
|
||||
if (a == null || a.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const res = [];
|
||||
for (let i = 0; i < a.length; i += chunkSize) {
|
||||
res.push(a.slice(i, i + chunkSize));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
Utils.init();
|
||||
|
||||
@@ -4,11 +4,14 @@ import { ObservedValueOf, of } from "rxjs";
|
||||
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import { LogoutReason } from "@bitwarden/auth/common";
|
||||
import { newGuid } from "@bitwarden/guid";
|
||||
import { UserId } from "@bitwarden/user-core";
|
||||
|
||||
import { mockAccountInfoWith } from "../../spec";
|
||||
import { AccountService } from "../auth/abstractions/account.service";
|
||||
import { TokenService } from "../auth/abstractions/token.service";
|
||||
import { EventRequest } from "../dirt/event-logs";
|
||||
import { EventType } from "../dirt/event-logs/enums/event-type.enum";
|
||||
import { DeviceType } from "../enums";
|
||||
import {
|
||||
VaultTimeoutAction,
|
||||
@@ -22,7 +25,7 @@ import { LogService } from "../platform/abstractions/log.service";
|
||||
import { PlatformUtilsService } from "../platform/abstractions/platform-utils.service";
|
||||
|
||||
import { InsecureUrlNotAllowedError } from "./api-errors";
|
||||
import { ApiService, HttpOperations } from "./api.service";
|
||||
import { ApiService, EventUploadBatchSize, HttpOperations } from "./api.service";
|
||||
|
||||
describe("ApiService", () => {
|
||||
let tokenService: MockProxy<TokenService>;
|
||||
@@ -64,6 +67,17 @@ describe("ApiService", () => {
|
||||
} satisfies ObservedValueOf<AccountService["activeAccount$"]>);
|
||||
|
||||
httpOperations = mock();
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal ?? undefined,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
sut = new ApiService(
|
||||
tokenService,
|
||||
@@ -92,18 +106,6 @@ describe("ApiService", () => {
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
tokenService.getAccessToken.mockResolvedValue("access_token");
|
||||
tokenService.tokenNeedsRefresh.mockResolvedValue(false);
|
||||
|
||||
@@ -152,18 +154,6 @@ describe("ApiService", () => {
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
tokenService.getAccessToken
|
||||
.calledWith(testInactiveUser)
|
||||
.mockResolvedValue("inactive_access_token");
|
||||
@@ -277,18 +267,6 @@ describe("ApiService", () => {
|
||||
)
|
||||
.mockResolvedValue({ accessToken: `${expectedEffectiveUser}_refreshed_access_token` });
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
const nativeFetch = jest.fn<Promise<Response>, [request: Request]>();
|
||||
|
||||
nativeFetch.mockImplementation((request) => {
|
||||
@@ -387,18 +365,6 @@ describe("ApiService", () => {
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
const nativeFetch = jest.fn<Promise<Response>, [request: Request]>();
|
||||
|
||||
nativeFetch.mockImplementation((request) => {
|
||||
@@ -424,18 +390,6 @@ describe("ApiService", () => {
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal ?? undefined,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
const nativeFetch = jest.fn<Promise<Response>, [request: Request]>();
|
||||
nativeFetch.mockImplementation((request) => {
|
||||
return Promise.resolve({
|
||||
@@ -468,17 +422,6 @@ describe("ApiService", () => {
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
tokenService.getAccessToken.calledWith(testActiveUser).mockResolvedValue("access_token");
|
||||
// First call (initial request): token doesn't need refresh yet
|
||||
// Subsequent calls (after 401): token needs refresh, triggering the refresh flow
|
||||
@@ -576,18 +519,6 @@ describe("ApiService", () => {
|
||||
getApiUrl: () => "https://example.com",
|
||||
} satisfies Partial<Environment> as Environment);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
const nativeFetch = jest.fn<Promise<Response>, [request: Request]>();
|
||||
|
||||
nativeFetch.mockImplementation((request) => {
|
||||
@@ -619,18 +550,6 @@ describe("ApiService", () => {
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
tokenService.getAccessToken.calledWith(testActiveUser).mockResolvedValue("valid_token");
|
||||
tokenService.tokenNeedsRefresh.calledWith(testActiveUser).mockResolvedValue(false);
|
||||
|
||||
@@ -662,18 +581,6 @@ describe("ApiService", () => {
|
||||
getApiUrl: () => "https://example.com",
|
||||
} satisfies Partial<Environment> as Environment);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
const nativeFetch = jest.fn<Promise<Response>, [request: Request]>();
|
||||
|
||||
nativeFetch.mockImplementation((request) => {
|
||||
@@ -708,18 +615,6 @@ describe("ApiService", () => {
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
tokenService.getAccessToken.calledWith(testActiveUser).mockResolvedValue("expired_token");
|
||||
tokenService.tokenNeedsRefresh.calledWith(testActiveUser).mockResolvedValue(false);
|
||||
|
||||
@@ -776,18 +671,6 @@ describe("ApiService", () => {
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
tokenService.getAccessToken
|
||||
.calledWith(testActiveUser)
|
||||
.mockResolvedValue("active_access_token");
|
||||
@@ -916,18 +799,6 @@ describe("ApiService", () => {
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
tokenService.getAccessToken.calledWith(testActiveUser).mockResolvedValue("access_token");
|
||||
// First call (initial request): token doesn't need refresh yet
|
||||
// Subsequent calls (after 401): token needs refresh, triggering the refresh flow
|
||||
@@ -1037,18 +908,6 @@ describe("ApiService", () => {
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
tokenService.getAccessToken.calledWith(testActiveUser).mockResolvedValue("expired_token");
|
||||
|
||||
// First two calls: token doesn't need refresh yet
|
||||
@@ -1176,18 +1035,6 @@ describe("ApiService", () => {
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
tokenService.getAccessToken.calledWith(testActiveUser).mockResolvedValue("valid_token");
|
||||
tokenService.tokenNeedsRefresh.calledWith(testActiveUser).mockResolvedValue(false);
|
||||
|
||||
@@ -1218,18 +1065,6 @@ describe("ApiService", () => {
|
||||
getApiUrl: () => "https://example.com",
|
||||
} satisfies Partial<Environment> as Environment);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
|
||||
const nativeFetch = jest.fn<Promise<Response>, [request: Request]>();
|
||||
|
||||
nativeFetch.mockImplementation((request) => {
|
||||
@@ -1367,18 +1202,6 @@ describe("ApiService", () => {
|
||||
.mockReturnValue(of(VaultTimeoutStringType.Never));
|
||||
|
||||
tokenService.setTokens.mockResolvedValue({ accessToken: refreshedAccessToken } as any);
|
||||
|
||||
httpOperations.createRequest.mockImplementation((url, request) => {
|
||||
return {
|
||||
url: url,
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
method: request.method,
|
||||
mode: request.mode,
|
||||
signal: request.signal,
|
||||
headers: new Headers(request.headers),
|
||||
} satisfies Partial<Request> as unknown as Request;
|
||||
});
|
||||
});
|
||||
|
||||
it("returns new access token on success", async () => {
|
||||
@@ -1447,4 +1270,41 @@ describe("ApiService", () => {
|
||||
await expect((sut as any).refreshAccessToken(userId)).rejects.toBeInstanceOf(ErrorResponse);
|
||||
});
|
||||
});
|
||||
|
||||
describe("postEventsCollect", () => {
|
||||
beforeEach(() => {
|
||||
environmentService.environment$ = of({
|
||||
getEventsUrl: () => "https://example.com",
|
||||
} satisfies Partial<Environment> as Environment);
|
||||
|
||||
environmentService.getEnvironment$.mockReturnValue(
|
||||
of({
|
||||
getEventsUrl: () => "https://example.com",
|
||||
} satisfies Partial<Environment> as Environment),
|
||||
);
|
||||
});
|
||||
|
||||
it("returns all failed events", async () => {
|
||||
sut.nativeFetch = jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce({
|
||||
status: 200,
|
||||
})
|
||||
.mockResolvedValue({
|
||||
status: 500,
|
||||
});
|
||||
// create events
|
||||
const events = [...Array(1000).keys()].map((i) => {
|
||||
return {
|
||||
type: EventType.Cipher_ClientAutofilled,
|
||||
cipherId: newGuid(),
|
||||
date: new Date().toISOString(),
|
||||
organizationId: newGuid(),
|
||||
} as EventRequest;
|
||||
});
|
||||
await expect(sut.postEventsCollect(events)).resolves.toEqual(
|
||||
events.slice(EventUploadBatchSize),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -121,6 +121,8 @@ export type HttpOperations = {
|
||||
createRequest: (url: string, request: RequestInit) => Request;
|
||||
};
|
||||
|
||||
export const EventUploadBatchSize = 100;
|
||||
|
||||
/**
|
||||
* @deprecated The `ApiService` class is deprecated and calls should be extracted into individual
|
||||
* api services. The `send` method is still allowed to be used within api services. For background
|
||||
@@ -1179,7 +1181,7 @@ export class ApiService implements ApiServiceAbstraction {
|
||||
return new ListResponse(r, EventResponse);
|
||||
}
|
||||
|
||||
async postEventsCollect(request: EventRequest[], userId?: UserId): Promise<any> {
|
||||
async postEventsCollect(requests: EventRequest[], userId?: UserId): Promise<EventRequest[]> {
|
||||
const authHeader = await this.tokenService.getAccessToken(userId);
|
||||
const headers = new Headers({
|
||||
"Device-Type": this.deviceType,
|
||||
@@ -1189,24 +1191,41 @@ export class ApiService implements ApiServiceAbstraction {
|
||||
if (this.customUserAgent != null) {
|
||||
headers.set("User-Agent", this.customUserAgent);
|
||||
}
|
||||
|
||||
const env = await firstValueFrom(
|
||||
userId == null
|
||||
? this.environmentService.environment$
|
||||
: this.environmentService.getEnvironment$(userId),
|
||||
);
|
||||
const response = await this.fetch(
|
||||
this.httpOperations.createRequest(env.getEventsUrl() + "/collect", {
|
||||
cache: "no-store",
|
||||
credentials: await this.getCredentials(env),
|
||||
method: "POST",
|
||||
body: JSON.stringify(request),
|
||||
headers: headers,
|
||||
}),
|
||||
);
|
||||
if (response.status !== 200) {
|
||||
return Promise.reject("Event post failed.");
|
||||
|
||||
// Break uploads into chunks of {EventUploadBatchSize} events
|
||||
let bail = false;
|
||||
const failedRequests: EventRequest[] = [];
|
||||
for (const eventRequests of Utils.chunkArray(requests, EventUploadBatchSize)) {
|
||||
// We only fail once per set of uploads
|
||||
if (bail) {
|
||||
failedRequests.push(...eventRequests);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await this.fetch(
|
||||
this.httpOperations.createRequest(env.getEventsUrl() + "/collect", {
|
||||
cache: "no-store",
|
||||
credentials: await this.getCredentials(env),
|
||||
method: "POST",
|
||||
body: JSON.stringify(eventRequests),
|
||||
headers: headers,
|
||||
}),
|
||||
);
|
||||
if (response.status !== 200) {
|
||||
throw new Error("Event post failed.");
|
||||
}
|
||||
} catch {
|
||||
bail = true;
|
||||
failedRequests.push(...eventRequests);
|
||||
}
|
||||
}
|
||||
return failedRequests;
|
||||
}
|
||||
|
||||
// User APIs
|
||||
|
||||
Reference in New Issue
Block a user