Skip to content

Commit 0772457

Browse files
flakey-biteddie.stanley
andauthored
[GH-788] Made the check to determine whether the current web-worker is owned by PapaParse more reliable (#937)
Co-authored-by: eddie.stanley <[email protected]>
1 parent c19cd2d commit 0772457

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

papaparse.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ License: MIT
4949
function getWorkerBlob() {
5050
var URL = global.URL || global.webkitURL || null;
5151
var code = moduleFactory.toString();
52-
return Papa.BLOB_URL || (Papa.BLOB_URL = URL.createObjectURL(new Blob(['(', code, ')();'], {type: 'text/javascript'})));
52+
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'})));
5353
}
5454

5555
var IS_WORKER = !global.document && !!global.postMessage,
56-
IS_PAPA_WORKER = IS_WORKER && /blob:/i.test((global.location || {}).protocol);
56+
IS_PAPA_WORKER = global.IS_PAPA_WORKER || false;
57+
5758
var workers = {}, workerIdCounter = 0;
5859

5960
var Papa = {};

tests/.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module.exports = {
22
"extends": ["../.eslintrc.js"],
3+
"parserOptions": {
4+
"ecmaVersion": 8
5+
},
36
"env": {
47
"mocha": true
58
},

tests/test-cases.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2649,3 +2649,52 @@ describe('Custom Tests', function() {
26492649
generateTest(CUSTOM_TESTS[i]);
26502650
}
26512651
});
2652+
2653+
(typeof window !== "undefined" ? describe : describe.skip)("Browser Tests", () => {
2654+
it("When parsing synchronously inside a web-worker not owned by PapaParse we should not invoke postMessage", async() => {
2655+
// Arrange
2656+
const papaParseScriptPath = new URL("../papaparse.js", window.document.baseURI).href;
2657+
2658+
// Define our custom web-worker that loads PapaParse and executes a synchronous parse
2659+
const blob = new Blob([
2660+
`
2661+
importScripts('${papaParseScriptPath}');
2662+
2663+
self.addEventListener("message", function(event) {
2664+
if (event.data === "ExecuteParse") {
2665+
// Perform our synchronous parse, as requested
2666+
const results = Papa.parse('x\\ny\\n');
2667+
postMessage({type: "ParseExecutedSuccessfully", results});
2668+
} else {
2669+
// Otherwise, send whatever we received back. We shouldn't be hitting this (!) If we're reached
2670+
// this it means PapaParse thinks it is running inside a web-worker that it owns
2671+
postMessage(event.data);
2672+
}
2673+
});
2674+
`
2675+
], {type: 'text/javascript'});
2676+
2677+
const blobURL = window.URL.createObjectURL(blob);
2678+
const webWorker = new Worker(blobURL);
2679+
2680+
const receiveMessagePromise = new Promise((resolve, reject) => {
2681+
webWorker.addEventListener("message", (event) => {
2682+
if (event.data.type === "ParseExecutedSuccessfully") {
2683+
resolve(event.data);
2684+
} else {
2685+
const error = new Error(`Received unexpected message: ${JSON.stringify(event.data, null, 2)}`);
2686+
error.data = event.data;
2687+
reject(error);
2688+
}
2689+
});
2690+
});
2691+
2692+
// Act
2693+
webWorker.postMessage("ExecuteParse");
2694+
const webWorkerMessage = await receiveMessagePromise;
2695+
2696+
// Assert
2697+
assert.equal("ParseExecutedSuccessfully", webWorkerMessage.type);
2698+
assert.equal(3, webWorkerMessage.results.data.length);
2699+
});
2700+
});

0 commit comments

Comments
 (0)