From 6366be1f38f88903209ad2f88d89a7e769cc6c13 Mon Sep 17 00:00:00 2001 From: valadaptive Date: Wed, 23 Jul 2025 09:11:16 -0400 Subject: [PATCH] Fix blob support in Node --- lib/utils.js | 36 ++++++++++++++++++++++-------------- test/asserts/file.js | 6 ++++-- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index fe585f40..f9067904 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -454,21 +454,29 @@ exports.prepareContent = function(name, inputData, isBinary, isOptimizedBinarySt var isBlob = support.blob && (data instanceof Blob || ["[object File]", "[object Blob]"].indexOf(Object.prototype.toString.call(data)) !== -1); - if (isBlob && typeof FileReader !== "undefined") { - return new external.Promise(function (resolve, reject) { - var reader = new FileReader(); - - reader.onload = function(e) { - resolve(e.target.result); - }; - reader.onerror = function(e) { - reject(e.target.error); - }; - reader.readAsArrayBuffer(data); - }); - } else { - return data; + if (isBlob) { + if (typeof Blob.prototype.arrayBuffer !== "undefined") { + return data.arrayBuffer(); + } else if (typeof FileReader !== "undefined") { + return new external.Promise(function (resolve, reject) { + var reader = new FileReader(); + + reader.onload = function(e) { + resolve(e.target.result); + }; + reader.onerror = function(e) { + reject(e.target.error); + }; + reader.readAsArrayBuffer(data); + }); + } else { + return external.Promise.reject( + new Error(name + " is a Blob, but we have no way of reading it.") + ); + } } + + return data; }); return promise.then(function(data) { diff --git a/test/asserts/file.js b/test/asserts/file.js index 1d36ff0f..5a84fec3 100644 --- a/test/asserts/file.js +++ b/test/asserts/file.js @@ -244,8 +244,10 @@ QUnit.module("file", function () { if (JSZip.support.blob) { QUnit.assert.equal(err, null, testName + "no error"); QUnit.assert.ok(blob instanceof Blob, testName + "the result is a instance of Blob"); - QUnit.assert.equal(blob.type, "", testName + "the result has the right mime type"); - QUnit.assert.equal(blob.size, opts.rawData.length, testName + "the result has the right length"); + if (blob instanceof Blob) { + QUnit.assert.equal(blob.type, "", testName + "the result has the right mime type"); + QUnit.assert.equal(blob.size, opts.rawData.length, testName + "the result has the right length"); + } } else { QUnit.assert.equal(blob, null, testName + "no data"); QUnit.assert.ok(err.message.match("not supported by this platform"), testName + "the error message is useful");