From 2faf35b760655aa299bd20cfbd4a98cddeeeecb2 Mon Sep 17 00:00:00 2001 From: Eddie Stanley Date: Sat, 11 Jun 2022 18:52:52 +1200 Subject: [PATCH 1/3] [GH-788] Allowed tests to use new ECMA syntax. The actual deployable library still targets ECMAScript 5 --- tests/.eslintrc.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/.eslintrc.js b/tests/.eslintrc.js index 8c8fc0c9..f75898e0 100644 --- a/tests/.eslintrc.js +++ b/tests/.eslintrc.js @@ -1,5 +1,8 @@ module.exports = { "extends": ["../.eslintrc.js"], + "parserOptions": { + "ecmaVersion": 8 + }, "env": { "mocha": true }, From fc15dab9ca343c650736d82744cec44169c0a147 Mon Sep 17 00:00:00 2001 From: "eddie.stanley" Date: Wed, 8 Jun 2022 13:42:52 +1200 Subject: [PATCH 2/3] [GH-788] Made the check to determine whether the current web-worker is owned by PapaParse more reliable - this fixes #788 Previously, the assumption was if we're in a web-worker AND the worker was loaded from a blob, the worker *must* be ours. --- papaparse.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/papaparse.js b/papaparse.js index 1b9ea513..60ee489b 100755 --- a/papaparse.js +++ b/papaparse.js @@ -49,11 +49,12 @@ License: MIT function getWorkerBlob() { var URL = global.URL || global.webkitURL || null; var code = moduleFactory.toString(); - return Papa.BLOB_URL || (Papa.BLOB_URL = URL.createObjectURL(new Blob(['(', code, ')();'], {type: 'text/javascript'}))); + return Papa.BLOB_URL || (Papa.BLOB_URL = URL.createObjectURL(new Blob(["var global = (function() { if (typeof self !== 'undefined') { return self; } if (typeof window !== 'undefined') { return window; } if (typeof global !== 'undefined') { return global; } return {}; })(); global.IS_PAPA_WORKER=true; ", '(', code, ')();'], {type: 'text/javascript'}))); } var IS_WORKER = !global.document && !!global.postMessage, - IS_PAPA_WORKER = IS_WORKER && /blob:/i.test((global.location || {}).protocol); + IS_PAPA_WORKER = global.IS_PAPA_WORKER || false; + var workers = {}, workerIdCounter = 0; var Papa = {}; From 085778e2a60a63f0c567d8cda2aa9bfa57ea3aca Mon Sep 17 00:00:00 2001 From: "eddie.stanley" Date: Fri, 10 Jun 2022 10:04:15 +1200 Subject: [PATCH 3/3] [GH-788] Added a test case to prove that #788 has been fixed (this fails prior to f2fe81fcd793381ae4bf327e69910a06188d39c2) Added inside a new describe block for browser-specific tests (looks for existence of `window`). This test is browser-specific due to the way the script is being loaded into a web-worker using importScripts() --- tests/test-cases.js | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/test-cases.js b/tests/test-cases.js index 1b0ee33a..75a20db4 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -2649,3 +2649,52 @@ describe('Custom Tests', function() { generateTest(CUSTOM_TESTS[i]); } }); + +(typeof window !== "undefined" ? describe : describe.skip)("Browser Tests", () => { + it("When parsing synchronously inside a web-worker not owned by PapaParse we should not invoke postMessage", async() => { + // Arrange + const papaParseScriptPath = new URL("../papaparse.js", window.document.baseURI).href; + + // Define our custom web-worker that loads PapaParse and executes a synchronous parse + const blob = new Blob([ + ` + importScripts('${papaParseScriptPath}'); + + self.addEventListener("message", function(event) { + if (event.data === "ExecuteParse") { + // Perform our synchronous parse, as requested + const results = Papa.parse('x\\ny\\n'); + postMessage({type: "ParseExecutedSuccessfully", results}); + } else { + // Otherwise, send whatever we received back. We shouldn't be hitting this (!) If we're reached + // this it means PapaParse thinks it is running inside a web-worker that it owns + postMessage(event.data); + } + }); + ` + ], {type: 'text/javascript'}); + + const blobURL = window.URL.createObjectURL(blob); + const webWorker = new Worker(blobURL); + + const receiveMessagePromise = new Promise((resolve, reject) => { + webWorker.addEventListener("message", (event) => { + if (event.data.type === "ParseExecutedSuccessfully") { + resolve(event.data); + } else { + const error = new Error(`Received unexpected message: ${JSON.stringify(event.data, null, 2)}`); + error.data = event.data; + reject(error); + } + }); + }); + + // Act + webWorker.postMessage("ExecuteParse"); + const webWorkerMessage = await receiveMessagePromise; + + // Assert + assert.equal("ParseExecutedSuccessfully", webWorkerMessage.type); + assert.equal(3, webWorkerMessage.results.data.length); + }); +});