diff --git a/src/test/store/receive-profile.test.ts b/src/test/store/receive-profile.test.ts index 392fb2e833..ac8d174e2b 100644 --- a/src/test/store/receive-profile.test.ts +++ b/src/test/store/receive-profile.test.ts @@ -88,6 +88,22 @@ function simulateSymbolStoreHasNoCache() { })); } +// Returns an ArrayBuffer which contains only the bytes that +// are covered by the Uint8Array, making a copy if needed. +function extractArrayBuffer(bufferView: Uint8Array): ArrayBuffer { + if ( + bufferView.byteOffset === 0 && + bufferView.byteLength === bufferView.buffer.byteLength + ) { + return bufferView.buffer; + } + + // There was extra data at the start or at the end. Make a copy. + const copy = new Uint8Array(bufferView.byteLength); + copy.set(bufferView); + return copy.buffer; +} + describe('actions/receive-profile', function () { beforeEach(() => { // The SymbolStore requires the use of IndexedDB, ensure that it exists so that @@ -676,10 +692,11 @@ describe('actions/receive-profile', function () { case 'json': return profileJSON; case 'arraybuffer': - return toUint8Array(profileJSON).buffer as ArrayBuffer; + return extractArrayBuffer(toUint8Array(profileJSON)); case 'gzip': - return (await compress(toUint8Array(profileJSON))) - .buffer as ArrayBuffer; + return extractArrayBuffer( + await compress(toUint8Array(profileJSON)) + ); default: throw new Error('unknown profiler format'); } @@ -1314,7 +1331,7 @@ describe('actions/receive-profile', function () { const profileOrZip = await _fetchProfile(args); expect(profileOrZip).toEqual({ responseType: 'PROFILE', - profile: profile.buffer, + profile: extractArrayBuffer(profile), }); } catch (error) { userFacingError = error; @@ -1446,7 +1463,7 @@ describe('actions/receive-profile', function () { const { getState, view } = await setupTestWithFile({ type: '', - payload: (await compress(serializeProfile(profile))).buffer, + payload: extractArrayBuffer(await compress(serializeProfile(profile))), }); expect(view.phase).toBe('DATA_LOADED'); expect(ProfileViewSelectors.getProfile(getState()).meta.product).toEqual( @@ -1478,7 +1495,7 @@ describe('actions/receive-profile', function () { const { getState, view } = await setupTestWithFile({ type: 'application/gzip', - payload: (await compress(serializeProfile(profile))).buffer, + payload: extractArrayBuffer(await compress(serializeProfile(profile))), }); expect(view.phase).toBe('DATA_LOADED'); expect(ProfileViewSelectors.getProfile(getState()).meta.product).toEqual( @@ -1492,7 +1509,7 @@ describe('actions/receive-profile', function () { const { getState, view } = await setupTestWithFile({ type: 'application/json', - payload: (await compress(serializeProfile(profile))).buffer, + payload: extractArrayBuffer(await compress(serializeProfile(profile))), }); expect(view.phase).toBe('DATA_LOADED'); expect(ProfileViewSelectors.getProfile(getState()).meta.product).toEqual( @@ -1506,7 +1523,7 @@ describe('actions/receive-profile', function () { .mockImplementation(() => {}); const { view } = await setupTestWithFile({ type: 'application/gzip', - payload: (await compress('{}')).buffer, + payload: extractArrayBuffer(await compress('{}')), }); expect(view.phase).toBe('FATAL_ERROR'); @@ -1525,14 +1542,9 @@ describe('actions/receive-profile', function () { zip.file(fileName, serializedProfile); const array = await zip.generateAsync({ type: 'uint8array' }); - // Create a new ArrayBuffer instance and copy the data into it, in order - // to work around https://github.com/facebook/jest/issues/6248 - const bufferCopy = new ArrayBuffer(array.buffer.byteLength); - new Uint8Array(bufferCopy).set(new Uint8Array(array.buffer)); - return setupTestWithFile({ type: 'application/zip', - payload: bufferCopy, + payload: extractArrayBuffer(array as Uint8Array), }); } diff --git a/src/utils/gz.ts b/src/utils/gz.ts index 9dcc11c1f3..60c4a7202e 100644 --- a/src/utils/gz.ts +++ b/src/utils/gz.ts @@ -43,7 +43,7 @@ export async function compress( if (errorOrNull) { reject(errorOrNull); } else { - resolve(new Uint8Array(result.buffer as ArrayBuffer)); + resolve(result); } }); }); @@ -67,7 +67,7 @@ export async function decompress( if (errorOrNull) { reject(errorOrNull); } else { - resolve(new Uint8Array(result.buffer as ArrayBuffer)); + resolve(result); } }); });