Skip to content

Commit

Permalink
fix: memory leak during inline webworker creation, added test webpack…
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-lipski committed Jul 5, 2019
1 parent 7e3d022 commit 0fca94d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ module.exports = {
root: true,
plugins: ['prettier'],
extends: ['@webpack-contrib/eslint-config-webpack'],
env: {
'browser': true,
'node': true
},
rules: {
'prettier/prettier': [
'error',
{ singleQuote: true, trailingComma: 'es5', arrowParens: 'always' },
],
},
};
};
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@
"webpack"
],
"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"globals": {
"window": {}
}
},
"eslintConfig": {
"globals": {
"window": true
}
},
"pre-commit": "lint-staged",
"lint-staged": {
Expand Down
11 changes: 5 additions & 6 deletions src/workers/InlineWorker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// http://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string

var URL = window.URL || window.webkitURL;

module.exports = function (content, url) {
try {
try {
Expand All @@ -21,22 +19,23 @@ module.exports = function (content, url) {
blob = blob.getBlob();
} catch (e) {
// The proposed API
blob = new Blob([content]);
blob = new window.Blob([content]);
}

var URL = window.URL || window.webkitURL;
var objectURL = URL.createObjectURL(blob);
var worker = new Worker(objectURL);
var worker = new window.Worker(objectURL);
URL.revokeObjectURL(objectURL);

return worker;
} catch (e) {
return new Worker('data:application/javascript,' + encodeURIComponent(content));
return new window.Worker('data:application/javascript,' + encodeURIComponent(content));
}
} catch (e) {
if (!url) {
throw Error('Inline worker is not supported');
}

return new Worker(url);
return new window.Worker(url);
}
};
24 changes: 24 additions & 0 deletions test/InlineWorker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import InlineWorker from './../src/workers/InlineWorker';

describe('Inline Worker', () => {
const objectUrlCreatedDuringWorkerCreation = () => {
const createdObjectUrl = 'OBJECT_URL';
window.URL = {
createObjectURL: jest.fn().mockReturnValue(createdObjectUrl),
revokeObjectURL: jest.fn(),
};
window.Worker = jest.fn();
window.Blob = jest.fn();
window.BlobBuilder = jest.fn();

return createdObjectUrl;
};

test('calls revokeObjectURL after creating worker to prevent memory leaks', () => {
const createdObjectUrl = objectUrlCreatedDuringWorkerCreation();

InlineWorker('some_content', 'some_url');

expect(window.URL.revokeObjectURL).toHaveBeenCalledWith(createdObjectUrl);
});
});

0 comments on commit 0fca94d

Please sign in to comment.