From 08f48bfc2ca377d6e9ea50b18c44ffa71a7c1e62 Mon Sep 17 00:00:00 2001
From: Daniel Huigens <d.huigens@protonmail.com>
Date: Fri, 2 Nov 2018 17:29:47 +0100
Subject: [PATCH] Switch to seek-bzip

---
 Gruntfile.js             |  8 +-------
 README.md                |  3 +--
 npm-shrinkwrap.json      | 40 ++++++++++++++++++++--------------------
 package.json             |  2 +-
 src/packet/compressed.js | 20 +++++++-------------
 test/general/openpgp.js  |  3 +++
 6 files changed, 33 insertions(+), 43 deletions(-)

diff --git a/Gruntfile.js b/Gruntfile.js
index b53ed301..79737952 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -192,12 +192,6 @@ module.exports = function(grunt) {
         src: ['mocha/mocha.css', 'mocha/mocha.js'],
         dest: 'test/lib/'
       },
-      bzip2: {
-        expand: true,
-        cwd: 'node_modules/compressjs/bin/',
-        src: ['bzip2.build.js'],
-        dest: 'src/compression/'
-      },
       openpgp_compat: {
         expand: true,
         cwd: 'dist/',
@@ -305,7 +299,7 @@ module.exports = function(grunt) {
   // Build tasks
   grunt.registerTask('version', ['replace:openpgp']);
   grunt.registerTask('replace_min', ['replace:openpgp_min', 'replace:worker_min']);
-  grunt.registerTask('build', ['copy:bzip2', 'browserify:openpgp', 'browserify:worker', 'version', 'uglify', 'replace_min']);
+  grunt.registerTask('build', ['browserify:openpgp', 'browserify:worker', 'version', 'uglify', 'replace_min']);
   grunt.registerTask('documentation', ['jsdoc']);
   grunt.registerTask('default', ['build']);
   // Test/Dev tasks
diff --git a/README.md b/README.md
index 78d84354..d760c924 100644
--- a/README.md
+++ b/README.md
@@ -259,13 +259,12 @@ ciphertext = await openpgp.encrypt(options);     // use ciphertext
 Or, override the config to enable compression:
 
 ```js
-openpgp.config.compression = openpgp.enums.compression.zip
+openpgp.config.compression = openpgp.enums.compression.zlib
 ```
 
 Where the value can be any of:
  * `openpgp.enums.compression.zip`
  * `openpgp.enums.compression.zlib`
- * `openpgp.enums.compression.bzip2`
 
 
 #### Streaming encrypt *Uint8Array* data with a password
diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json
index d49987d5..f792331c 100644
--- a/npm-shrinkwrap.json
+++ b/npm-shrinkwrap.json
@@ -169,7 +169,8 @@
     "amdefine": {
       "version": "1.0.1",
       "resolved": "https://registry.npmjs.org/amdefine/-/amdefine-1.0.1.tgz",
-      "integrity": "sha1-SlKCrBZHKek2Gbz9OtFR+BfOkfU="
+      "integrity": "sha1-SlKCrBZHKek2Gbz9OtFR+BfOkfU=",
+      "dev": true
     },
     "ansi-escapes": {
       "version": "3.0.0",
@@ -1829,24 +1830,6 @@
       "integrity": "sha512-BFnaq5ZOGcDN7FlrtBT4xxkgIToalIIxwjxLWVJ8bGTpe1LroqMiqQXdA7ygc7CRvaYS+9zfPGFnJqFSayx+AA==",
       "dev": true
     },
-    "compressjs": {
-      "version": "github:openpgpjs/compressjs#ce5ccbede61f075926081e29573c8a79ddf10d88",
-      "from": "github:openpgpjs/compressjs",
-      "requires": {
-        "amdefine": "~1.0.0",
-        "commander": "~2.8.1"
-      },
-      "dependencies": {
-        "commander": {
-          "version": "2.8.1",
-          "resolved": "https://registry.npmjs.org/commander/-/commander-2.8.1.tgz",
-          "integrity": "sha1-Br42f+v9oMMwqh4qBy09yXYkJdQ=",
-          "requires": {
-            "graceful-readlink": ">= 1.0.0"
-          }
-        }
-      }
-    },
     "concat-map": {
       "version": "0.0.1",
       "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
@@ -6547,6 +6530,23 @@
         "https-proxy-agent": "^1.0.0"
       }
     },
+    "seek-bzip": {
+      "version": "github:openpgpjs/seek-bzip#3aca608ffedc055a1da1d898ecb244804ef32209",
+      "from": "github:openpgpjs/seek-bzip",
+      "requires": {
+        "commander": "~2.8.1"
+      },
+      "dependencies": {
+        "commander": {
+          "version": "2.8.1",
+          "resolved": "http://registry.npmjs.org/commander/-/commander-2.8.1.tgz",
+          "integrity": "sha1-Br42f+v9oMMwqh4qBy09yXYkJdQ=",
+          "requires": {
+            "graceful-readlink": ">= 1.0.0"
+          }
+        }
+      }
+    },
     "semver": {
       "version": "5.4.1",
       "resolved": "https://registry.npmjs.org/semver/-/semver-5.4.1.tgz",
@@ -7417,7 +7417,7 @@
       }
     },
     "web-stream-tools": {
-      "version": "github:openpgpjs/web-stream-tools#37c3542b11455856012b5ad87c39088457e2b3a1",
+      "version": "github:openpgpjs/web-stream-tools#9ab800d46add161db496506d67338202ad0114ce",
       "from": "github:openpgpjs/web-stream-tools"
     },
     "websocket-driver": {
diff --git a/package.json b/package.json
index 3a76e529..c0aa3b59 100644
--- a/package.json
+++ b/package.json
@@ -77,12 +77,12 @@
     "asn1.js": "^5.0.0",
     "bn.js": "^4.11.8",
     "buffer": "^5.0.8",
-    "compressjs": "github:openpgpjs/compressjs",
     "elliptic": "github:openpgpjs/elliptic",
     "hash.js": "^1.1.3",
     "node-fetch": "^2.1.2",
     "node-localstorage": "~1.3.0",
     "pako": "^1.0.6",
+    "seek-bzip": "github:openpgpjs/seek-bzip",
     "web-stream-tools": "github:openpgpjs/web-stream-tools"
   },
   "repository": {
diff --git a/src/packet/compressed.js b/src/packet/compressed.js
index cd6bb44f..b87b7194 100644
--- a/src/packet/compressed.js
+++ b/src/packet/compressed.js
@@ -25,11 +25,11 @@
  */
 
 import pako from 'pako';
+import Bunzip from 'seek-bzip';
 import stream from 'web-stream-tools';
 import config from '../config';
 import enums from '../enums';
 import util from '../util';
-import Bzip2 from '../compression/bzip2.build.js';
 
 /**
  * Implementation of the Compressed Data Packet (Tag 8)
@@ -103,7 +103,7 @@ Compressed.prototype.write = function () {
 Compressed.prototype.decompress = async function () {
 
   if (!decompress_fns[this.algorithm]) {
-    throw new Error("Compression algorithm unknown :" + this.algorithm);
+    throw new Error(this.algorithm + ' decompression not supported');
   }
 
   await this.packets.read(decompress_fns[this.algorithm](this.compressed));
@@ -115,7 +115,7 @@ Compressed.prototype.decompress = async function () {
 Compressed.prototype.compress = function () {
 
   if (!compress_fns[this.algorithm]) {
-    throw new Error("Compression algorithm unknown :" + this.algorithm);
+    throw new Error(this.algorithm + ' compression not supported');
   }
 
   this.compressed = compress_fns[this.algorithm](this.packets.write());
@@ -160,30 +160,24 @@ let compress_fns;
 let decompress_fns;
 if (nodeZlib) { // Use Node native zlib for DEFLATE compression/decompression
   compress_fns = {
-    // eslint-disable-next-line no-sync
     zip: node_zlib(nodeZlib.createDeflateRaw, { level: config.deflate_level }),
-    // eslint-disable-next-line no-sync
-    zlib: node_zlib(nodeZlib.createDeflate, { level: config.deflate_level }),
-    bzip2: bzip2(Bzip2.compressFile)
+    zlib: node_zlib(nodeZlib.createDeflate, { level: config.deflate_level })
   };
 
   decompress_fns = {
-    // eslint-disable-next-line no-sync
     zip: node_zlib(nodeZlib.createInflateRaw),
-    // eslint-disable-next-line no-sync
     zlib: node_zlib(nodeZlib.createInflate),
-    bzip2: bzip2(Bzip2.decompressFile)
+    bzip2: bzip2(Bunzip.decode)
   };
 } else { // Use JS fallbacks
   compress_fns = {
     zip: pako_zlib(pako.Deflate, { raw: true, level: config.deflate_level }),
-    zlib: pako_zlib(pako.Deflate, { level: config.deflate_level }),
-    bzip2: bzip2(Bzip2.compressFile)
+    zlib: pako_zlib(pako.Deflate, { level: config.deflate_level })
   };
 
   decompress_fns = {
     zip: pako_zlib(pako.Inflate, { raw: true }),
     zlib: pako_zlib(pako.Inflate),
-    bzip2: bzip2(Bzip2.decompressFile)
+    bzip2: bzip2(Bunzip.decode)
   };
 }
diff --git a/test/general/openpgp.js b/test/general/openpgp.js
index 3564a6e0..b31dfb27 100644
--- a/test/general/openpgp.js
+++ b/test/general/openpgp.js
@@ -309,6 +309,9 @@ function withCompression(tests) {
 
   compressionTypes.forEach(function (compression) {
     const compressionName = openpgp.enums.read(openpgp.enums.compression, compression);
+    if (compressionName === 'bzip2') {
+      return; // bzip2 compression is not supported.
+    }
     const group = `compression - ${compressionName}`;
 
     describe(group, function() {