mirror of
https://github.com/openpgpjs/openpgpjs.git
synced 2025-06-05 21:56:49 +00:00

Mocha v10 requires the lib to be esm compliant. ESM mandates the use of file extensions in imports, so to minimize the changes (for now), we rely on the flag `experimental-specifier-resolution=node` and on `ts-node` (needed only for Node 20). Breaking changes: downstream bundlers might be affected by the package.json changes depending on how they load the library. NB: legacy package.json entrypoints are still available.
33 lines
1012 B
JavaScript
33 lines
1012 B
JavaScript
/* globals tryTests */
|
|
|
|
import { expect } from 'chai';
|
|
|
|
export default () => tryTests('Application Worker', tests, {
|
|
if: typeof window !== 'undefined' && window.Worker && window.MessageChannel
|
|
});
|
|
|
|
function tests() {
|
|
|
|
it('Should support loading OpenPGP.js from inside a Web Worker', async function() {
|
|
const worker = new Worker('./worker/worker_example.js');
|
|
async function delegate(action, message) {
|
|
return new Promise((resolve, reject) => {
|
|
const channel = new MessageChannel();
|
|
channel.port1.onmessage = function({ data }) {
|
|
if (data.error !== undefined) {
|
|
reject(new Error(data.error));
|
|
} else {
|
|
resolve(data.result);
|
|
}
|
|
};
|
|
worker.postMessage({ action, message }, [channel.port2]);
|
|
});
|
|
}
|
|
const encrypted = await delegate('encrypt', 'Hello World!');
|
|
const decrypted = await delegate('decrypt', encrypted);
|
|
expect(decrypted).to.equal('Hello World!');
|
|
worker.terminate();
|
|
});
|
|
|
|
}
|