@muhammara/native-core 7.1.0 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/Recipe.js CHANGED
@@ -390,14 +390,7 @@ class Recipe {
390
390
  }
391
391
  }
392
392
  if (this.needToEncrypt) {
393
- if (this.isBufferSrc) {
394
- // eslint-disable-next-line no-console
395
- console.log(
396
- "Feature: Encryption is not supported in Buffer Mode yet.",
397
- );
398
- } else {
399
- this._encrypt();
400
- }
393
+ this._encrypt();
401
394
  }
402
395
 
403
396
  if (this.isBufferSrc && this.output) {
@@ -1,5 +1,6 @@
1
1
  const fs = require("fs");
2
2
  const { Permission } = require("../recipe-constants");
3
+ const PDFWStreamForBuffer = require("../PDFWStreamForBuffer");
3
4
 
4
5
  /**
5
6
  * Encryption user access permissions
@@ -101,8 +102,9 @@ exports._getEncryptOptions = function _getEncryptOptions(
101
102
  * @param {string} [options.ownerPassword] - The password for editing.
102
103
  * @param {string} [options.userPassword] - The password for viewing & encryption.
103
104
  * @param {number} [options.userProtectionFlag] - The flag for the security level, see `permission()`.
104
- * @returns {Recipe} The recipe instance. The file is encrypted by `endPDF()`;
105
- * Buffer sources are not encrypted.
105
+ * @returns {Recipe} The recipe instance. The output is encrypted by `endPDF()`:
106
+ * the output file for a path source, and the Buffer passed to the callback
107
+ * (or written to the output path) for a Buffer source.
106
108
  */
107
109
  exports.encrypt = function encrypt(options = {}) {
108
110
  this.needToEncrypt = true;
@@ -113,18 +115,43 @@ exports.encrypt = function encrypt(options = {}) {
113
115
 
114
116
  // http://pdfhummus.com/post/147451287581/hummus-1058-and-pdf-writer-updates-encryption
115
117
  /**
116
- * Re-encrypt the finished output file with the encrypt() options.
118
+ * Re-encrypt the finished output with the encrypt() options. A path source
119
+ * re-encrypts the output file in place; a Buffer source re-encrypts the
120
+ * collected output bytes, so the callback and an output path receive the
121
+ * encrypted PDF. Nothing happens when encrypt() was given no password, as on
122
+ * Wasm. The finished output is opened with the password the Recipe was
123
+ * created with, or none for an empty user password, so a PDF whose
124
+ * constructor options already encrypted it can still be re-encrypted with new
125
+ * passwords.
117
126
  * @private
118
127
  * @returns {void}
119
128
  * @throws {Error} If the output cannot be renamed, re-encrypted or removed.
120
129
  */
121
130
  exports._encrypt = function _encrypt() {
122
- if (!this.encryption_) {
131
+ if (!this.encryption_ || Object.keys(this.encryption_).length === 0) {
132
+ return;
133
+ }
134
+ const recryptOptions = Object.assign({}, this.encryption_, {
135
+ password: this.encryptOptions.password,
136
+ });
137
+
138
+ if (this.isBufferSrc) {
139
+ const encrypted = new PDFWStreamForBuffer();
140
+ this.muhammara.recrypt(
141
+ new this.muhammara.PDFRStreamForBuffer(
142
+ this.outStream.buffer || Buffer.alloc(0),
143
+ ),
144
+ encrypted,
145
+ recryptOptions,
146
+ );
147
+ // Replace the collected bytes instead of the stream, so the unencrypted
148
+ // output is dropped rather than kept alive next to the encrypted one.
149
+ this.outStream.buffer = encrypted.buffer;
123
150
  return;
124
151
  }
125
152
 
126
153
  const tmp = this.output + ".tmp.pdf";
127
154
  fs.renameSync(this.output, tmp);
128
- this.muhammara.recrypt(tmp, this.output, this.encryption_);
155
+ this.muhammara.recrypt(tmp, this.output, recryptOptions);
129
156
  fs.unlinkSync(tmp);
130
157
  };
package/muhammara.d.ts CHANGED
@@ -4182,8 +4182,9 @@ declare namespace muhammara {
4182
4182
  * @param options.ownerPassword - The password for editing.
4183
4183
  * @param options.userPassword - The password for viewing & encryption.
4184
4184
  * @param options.userProtectionFlag - The flag for the security level, see `permission()`.
4185
- * @returns The recipe instance. The file is encrypted by `endPDF()`;
4186
- * Buffer sources are not encrypted.
4185
+ * @returns The recipe instance. The output is encrypted by `endPDF()`:
4186
+ * the output file for a path source, and the Buffer passed to the
4187
+ * callback (or written to the output path) for a Buffer source.
4187
4188
  */
4188
4189
  encrypt(options?: Recipe.EncryptOptions): Recipe;
4189
4190
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muhammara/native-core",
3
- "version": "7.1.0",
3
+ "version": "7.2.0",
4
4
  "description": "Shared JavaScript API for MuhammaraJS native packages",
5
5
  "homepage": "https://muhammarajs.readthedocs.io/",
6
6
  "license": "Apache-2.0",