Skip to content

Commit aa24850

Browse files
authored
Fix batch sync protobuf request body (#363)
1 parent f42258c commit aa24850

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { collab } from '@/proto/messages';
2+
import { getAxios } from '@/application/services/js-services/http/core';
3+
4+
import { collabFullSyncBatch } from '../collab-api';
5+
6+
jest.mock('@/application/services/js-services/device-id', () => ({
7+
getOrCreateDeviceId: jest.fn(() => 'test-device-id'),
8+
}));
9+
10+
jest.mock('@/application/services/js-services/http/core', () => ({
11+
executeAPIRequest: jest.fn(),
12+
executeAPIVoidRequest: jest.fn(),
13+
getAxios: jest.fn(),
14+
parseRetryAfterSecs: jest.fn(),
15+
}));
16+
17+
const mockGetAxios = getAxios as unknown as jest.Mock;
18+
19+
describe('collabFullSyncBatch', () => {
20+
beforeEach(() => {
21+
jest.clearAllMocks();
22+
});
23+
24+
it('sends the encoded protobuf view instead of the pooled backing buffer', async () => {
25+
const responseBody = collab.CollabBatchSyncResponse.encode(
26+
collab.CollabBatchSyncResponse.create({
27+
results: [],
28+
responseCompression: collab.PayloadCompressionType.COMPRESSION_NONE,
29+
})
30+
).finish();
31+
const post = jest.fn().mockResolvedValue({
32+
status: 200,
33+
data: responseBody,
34+
headers: {},
35+
});
36+
37+
mockGetAxios.mockReturnValue({ post });
38+
39+
await collabFullSyncBatch('workspace-id', [
40+
{
41+
objectId: 'object-id',
42+
collabType: 0,
43+
stateVector: new Uint8Array([1]),
44+
docState: new Uint8Array([2]),
45+
},
46+
]);
47+
48+
const [, requestBody, config] = post.mock.calls[0];
49+
50+
expect(ArrayBuffer.isView(requestBody)).toBe(true);
51+
expect(requestBody.byteLength).toBeLessThan(requestBody.buffer.byteLength);
52+
expect(config.transformRequest[0](requestBody)).toBe(requestBody);
53+
});
54+
});

src/application/services/js-services/http/collab-api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ export async function collabFullSyncBatch(
100100
'device-id': deviceId,
101101
},
102102
responseType: 'arraybuffer',
103+
// Axios' default transform sends typed-array `.buffer`, which can include
104+
// protobufjs' pooled zero padding beyond this Uint8Array view.
105+
transformRequest: [(data: Uint8Array) => data],
103106
validateStatus: (status) => status === 200 || status === 429 || status === 503,
104107
});
105108

0 commit comments

Comments
 (0)