Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/actions/receive-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ async function _extractZipFromResponse(
async function _extractJsonFromArrayBuffer(
arrayBuffer: ArrayBuffer
): Promise<unknown> {
let profileBytes: Uint8Array<ArrayBufferLike> = new Uint8Array(arrayBuffer);
let profileBytes = new Uint8Array(arrayBuffer);
// Check for the gzip magic number in the header.
if (isGzip(profileBytes)) {
profileBytes = await decompress(profileBytes);
Expand Down
4 changes: 1 addition & 3 deletions src/profile-logic/process-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1980,9 +1980,7 @@ export async function unserializeProfileOfArbitraryFormat(
// object is constructed from an ArrayBuffer in a different context... which
// happens in our tests.
if (String(arbitraryFormat) === '[object ArrayBuffer]') {
// Obviously Flow doesn't understand that this is correct, so let's help
// Flow here.
let arrayBuffer: ArrayBufferLike = arbitraryFormat as any;
let arrayBuffer = arbitraryFormat as ArrayBuffer;

// Check for the gzip magic number in the header. If we find it, decompress
// the data first.
Expand Down
2 changes: 1 addition & 1 deletion src/test/store/receive-profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('actions/receive-profile', function () {
return states;
}

function encode(string: string): Uint8Array {
function encode(string: string): Uint8Array<ArrayBuffer> {
return new TextEncoder().encode(string);
}

Expand Down
20 changes: 4 additions & 16 deletions src/utils/gz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,8 @@ async function readableStreamToBuffer(
return result;
}

// The Streams API doesn't support SAB, and so we need to copy from these to
// use these API's.
function copyBufferIfShared(buffer: Uint8Array): Uint8Array<ArrayBuffer> {
// Check if the buffer is a view into a larger ArrayBuffer (shared)
if (buffer.buffer instanceof SharedArrayBuffer) {
// Create a new buffer with its own ArrayBuffer
return new Uint8Array(buffer);
}
// Return the original buffer if it's not shared
return buffer as Uint8Array<ArrayBuffer>;
}

export async function compress(
data: string | Uint8Array
data: string | Uint8Array<ArrayBuffer>
): Promise<Uint8Array<ArrayBuffer>> {
// Encode the data if it's a string
const arrayData =
Expand All @@ -56,22 +44,22 @@ export async function compress(

// Write the data to the compression stream
const writer = compressionStream.writable.getWriter();
writer.write(copyBufferIfShared(arrayData));
writer.write(arrayData);
writer.close();

// Read the compressed data back into a buffer
return readableStreamToBuffer(compressionStream.readable);
}

export async function decompress(
data: Uint8Array
data: Uint8Array<ArrayBuffer>
): Promise<Uint8Array<ArrayBuffer>> {
// Create a gzip compression stream
const decompressionStream = new DecompressionStream('gzip');

// Write the data to the compression stream
const writer = decompressionStream.writable.getWriter();
writer.write(copyBufferIfShared(data));
writer.write(data);
writer.close();

// Read the compressed data back into a buffer
Expand Down