Skip to content

Commit

Permalink
Merge branch 'main' into W-16440795-never-remap-set-and-map
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron authored Aug 12, 2024
2 parents 1ff1121 + e179fa8 commit aa8268d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
47 changes: 47 additions & 0 deletions test/membrane/binary-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,50 @@ describe('FileSaver library', () => {
`);
});
});

describe('Identity Discontinuity', () => {
beforeEach(createEnvironmentThatAlwaysRemapsTypedArray);

function getBlueBlob() {
return new Blob([new Uint8Array([1, 2, 3])], { type: 'text/plain;charset=utf-8' });
}

it('can save blue blob', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect, getBlueBlob }),
});

env.evaluate(`
${untrusted('FileSaver.js')}
const transferBlueBlob = false;
const expectedToThrow = false;
${untrusted('FileSaver-blue-blob.js')}
`);
});
it('fails to save blue blob, when maxPerfMode is true and blob is not transferred to red', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect, getBlueBlob }),
maxPerfMode: true,
});

env.evaluate(`
${untrusted('FileSaver.js')}
const transferBlueBlob = false;
const expectedToThrow = true;
${untrusted('FileSaver-blue-blob.js')}
`);
});
it('can save blue blob, when maxPerfMode is true and blob is transferred to red', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect, getBlueBlob }),
maxPerfMode: true,
});

env.evaluate(`
${untrusted('FileSaver.js')}
const transferBlueBlob = true;
const expectedToThrow = false;
${untrusted('FileSaver-blue-blob.js')}
`);
});
});
46 changes: 46 additions & 0 deletions test/membrane/untrusted/binary-data/FileSaver-blue-blob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function blueBlobToRedBlob(blueBlob) {
const reader = blueBlob.stream().getReader();
let result = new Uint8Array(0);
let resolve, reject;
const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
const handler = ({ done, value }) => {
try {
if (done) {
const redBlob = new Blob([result.buffer], { type: blueBlob.type });
resolve(redBlob);
return;
}
const merge = new Uint8Array(result.length + value.length);
merge.set(result);
merge.set(value, result.length);
result = merge;
return reader.read().then(handler, reject);
} catch (e) {
reject(e);
}
};
reader.read().then(handler, reject);
return promise;
}

const blueBlob = getBlueBlob();

const blobToSavePromise = transferBlueBlob
? blueBlobToRedBlob(blueBlob)
: Promise.resolve(blueBlob);

blobToSavePromise.then((blobToSave) => {
if (expectedToThrow) {
expect(() => {
saveAs(blobToSave, 'blue-binary.txt');
}).toThrow();
} else {
expect(() => {
saveAs(blobToSave, 'blue-binary.txt');
}).not.toThrow();
}
done();
});

0 comments on commit aa8268d

Please sign in to comment.