From fe69cb882d936467e91f79edec49fac8faa3c590 Mon Sep 17 00:00:00 2001
From: Daniel Huigens <d.huigens@protonmail.com>
Date: Fri, 4 Jan 2019 17:23:22 +0100
Subject: [PATCH] Zero-copy transfer buffers when passing streams to workers

---
 src/util.js             | 8 ++++++--
 test/general/openpgp.js | 2 +-
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/util.js b/src/util.js
index a526d5e0..c94cfd61 100644
--- a/src/util.js
+++ b/src/util.js
@@ -79,8 +79,12 @@ export default {
               const reader = stream.getReader(readable);
               const { port1, port2 } = new MessageChannel();
               port1.onmessage = async function({ data: { action } }) {
-                if (action === 'read') port1.postMessage(await reader.read());
-                else if (action === 'cancel') port1.postMessage(await transformed.cancel());
+                if (action === 'read') {
+                  const result = await reader.read();
+                  port1.postMessage(result, util.getTransferables(result, true));
+                } else if (action === 'cancel') {
+                  port1.postMessage(await transformed.cancel());
+                }
               };
               obj[key] = port2;
               collection.push(port2);
diff --git a/test/general/openpgp.js b/test/general/openpgp.js
index b31dfb27..cfead605 100644
--- a/test/general/openpgp.js
+++ b/test/general/openpgp.js
@@ -1741,7 +1741,7 @@ describe('[Sauce Labs Group 2] OpenPGP.js public api tests', function() {
                 if (i++ < 4) {
                   let randomBytes = await openpgp.crypto.random.getRandomBytes(10);
                   controller.enqueue(randomBytes);
-                  plaintext.push(randomBytes);
+                  plaintext.push(randomBytes.slice());
                 } else {
                   controller.close();
                 }