From c4a725fe981fb0d1f1b5ece7cad378027d25fe98 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 19 Mar 2023 21:49:27 +0100 Subject: [PATCH] Fix the `transfer` parameter, for `structuredClone`, in the `LoopbackPort` The way that we handle the `transfer` parameter is unfortunately wrong, ever since PR 14392 which introduced the code, given that the MDN article originally contained incorrect information; please see https://github.com/mdn/content/pull/23164 By updating the `structuredClone` call such that it works correctly, we can enable more unit-tests in Node.js environments; please refer to https://developer.mozilla.org/en-US/docs/Web/API/structuredClone#parameters --- src/display/api.js | 4 ++-- test/unit/api_spec.js | 36 +++++++++++++----------------------- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index 2fe2c2d5a..b570778e5 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -1943,9 +1943,9 @@ class LoopbackPort { #deferred = Promise.resolve(); - postMessage(obj, transfers) { + postMessage(obj, transfer) { const event = { - data: structuredClone(obj, transfers), + data: structuredClone(obj, transfer ? { transfer } : null), }; this.#deferred.then(() => { diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 2203eb7f9..a49cd2e43 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -208,10 +208,8 @@ describe("api", function () { expect(data[0] instanceof PDFDocumentProxy).toEqual(true); expect(data[1].loaded / data[1].total).toEqual(1); - if (!isNodeJS) { - // Check that the TypedArray was transferred. - expect(typedArrayPdf.length).toEqual(0); - } + // Check that the TypedArray was transferred. + expect(typedArrayPdf.length).toEqual(0); await loadingTask.destroy(); }); @@ -240,10 +238,8 @@ describe("api", function () { expect(data[0] instanceof PDFDocumentProxy).toEqual(true); expect(data[1].loaded / data[1].total).toEqual(1); - if (!isNodeJS) { - // Check that the ArrayBuffer was transferred. - expect(arrayBufferPdf.byteLength).toEqual(0); - } + // Check that the ArrayBuffer was transferred. + expect(arrayBufferPdf.byteLength).toEqual(0); await loadingTask.destroy(); }); @@ -3368,11 +3364,9 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`) expect(pdfPage.rotate).toEqual(0); expect(fetches).toBeGreaterThan(2); - if (!isNodeJS) { - // Check that the TypedArrays were transferred. - for (const array of subArrays) { - expect(array.length).toEqual(0); - } + // Check that the TypedArrays were transferred. + for (const array of subArrays) { + expect(array.length).toEqual(0); } await loadingTask.destroy(); @@ -3417,11 +3411,9 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`) waitSome(resolve); }); - if (!isNodeJS) { - // Check that the TypedArrays were transferred. - for (const array of subArrays) { - expect(array.length).toEqual(0); - } + // Check that the TypedArrays were transferred. + for (const array of subArrays) { + expect(array.length).toEqual(0); } await loadingTask.destroy(); @@ -3458,11 +3450,9 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`) expect(pdfPage.rotate).toEqual(0); expect(fetches).toEqual(0); - if (!isNodeJS) { - // Check that the TypedArrays were transferred. - for (const array of subArrays) { - expect(array.length).toEqual(0); - } + // Check that the TypedArrays were transferred. + for (const array of subArrays) { + expect(array.length).toEqual(0); } await loadingTask.destroy();