Skip to content

Commit

Permalink
[GH-788] Made the check to determine whether the current web-worker i…
Browse files Browse the repository at this point in the history
…s owned by PapaParse more reliable (#937)

Co-authored-by: eddie.stanley <[email protected]>
  • Loading branch information
flakey-bit and eddie.stanley authored Jun 14, 2022
1 parent c19cd2d commit 0772457
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
5 changes: 3 additions & 2 deletions papaparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
3 changes: 3 additions & 0 deletions tests/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = {
"extends": ["../.eslintrc.js"],
"parserOptions": {
"ecmaVersion": 8
},
"env": {
"mocha": true
},
Expand Down
49 changes: 49 additions & 0 deletions tests/test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 0772457

Please sign in to comment.