Skip to content

Commit

Permalink
Tests: load stream ponyfills if needed (fix Safari 13), add README no…
Browse files Browse the repository at this point in the history
…te about polyfilling requirement

Since v7.2.2, the web-stream-tools version used by pmcrypto diverges from the one of OpenPGP.js,
and it does not automatically polyfill streams.
Without polyfilling, some of the lib functions do not work on legacy browsers when streamed inputs are used.
Affected functions include those that internally process streams (independently of OpenPGP.js),
such as `encryptMessage`.
  • Loading branch information
larabr committed Feb 20, 2024
1 parent 10f05f6 commit 165be37
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"globals": {
"window": "readonly",
"btoa": "readonly",
"atob": "readonly"
"atob": "readonly",
"globalThis": "readonly"
},
"env": {
"es6": true,
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ const { data: decryptedData, verified } = await decryptMessage({
**For streamed inputs:**
to encrypt (and/or sign), pass the stream to `textData` or `binaryData` based on the streamed data type. Similarly, to decrypt and verify, the input options are the same as the non-streaming case. However, if `armoredMessage` (or `binaryMessage`) is a stream, the decryption result needs to be handled differently:
```js
// explicitly loading stream polyfills for legacy browsers is required since v7.2.2
if (!globalThis.TransformStream) {
await import('web-streams-polyfill/es6');
}

const { data: dataStream, verified: verifiedPromise } = await decryptMessage({
message: await readMessage({ armoredMessage: streamedArmoredMessage }),
... // other options
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"playwright": "^1.40.0",
"ts-loader": "^9.4.1",
"typescript": "^4.9.5",
"web-streams-polyfill": "^3.3.3",
"webpack": "^5.66.0",
"webpack-cli": "^4.9.2"
}
Expand Down
13 changes: 11 additions & 2 deletions test/message/encryptMessage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ const generateStreamOfData = (): { stream: WebStream<string>, data: string } =>

describe('message encryption and decryption', () => {
const { minRSABits } = globalConfig;
before('downgrade openpgp config', () => {

before('downgrade openpgp config and load stream polyfills', async () => {
globalConfig.minRSABits = 512;

// load stream ponyfills if needed
if (!globalThis.TransformStream) {
await import('web-streams-polyfill/es6');
}
});
after('restore openpgp config', async () => {
after('restore openpgp config and stream globals', async () => {
globalConfig.minRSABits = minRSABits;
// undo polyfilling
globalThis.TransformStream = TransformStream;
globalThis.WritableStream = WritableStream;
});

it('it can encrypt and decrypt a text message', async () => {
Expand Down

0 comments on commit 165be37

Please sign in to comment.