- support for web worker. #11

Changed
- throw error if input type is incorrect.
- prevent webpack to require dependencies.
This commit is contained in:
Chen, Yi-Cyuan
2017-07-14 13:10:27 +08:00
parent 8b9a23ce32
commit c00df3af37
15 changed files with 275 additions and 82 deletions
+47 -1
View File
@@ -1,5 +1,8 @@
// Node.js env
expect = require('expect.js');
Worker = require('webworker-threads').Worker;
// Node.js env
BUFFER = true;
md5 = require('../src/md5.js');
require('./test.js');
@@ -9,6 +12,7 @@ md5 = null
// Webpack browser env
JS_MD5_NO_NODE_JS = true;
BUFFER = false;
window = global;
md5 = require('../src/md5.js');
require('./test.js');
@@ -20,6 +24,7 @@ md5 = null;
// browser env
JS_MD5_NO_NODE_JS = true;
JS_MD5_NO_COMMON_JS = true;
BUFFER = false;
window = global;
require('../src/md5.js');
require('./test.js');
@@ -32,6 +37,7 @@ md5 = null;
JS_MD5_NO_NODE_JS = true;
JS_MD5_NO_COMMON_JS = true;
JS_MD5_NO_ARRAY_BUFFER = true;
BUFFER = false;
window = global;
require('../src/md5.js');
require('./test.js');
@@ -44,6 +50,7 @@ md5 = null;
JS_MD5_NO_NODE_JS = true;
JS_MD5_NO_COMMON_JS = true;
JS_MD5_NO_ARRAY_BUFFER = false;
BUFFER = false;
window = global;
define = function (func) {
md5 = func();
@@ -52,3 +59,42 @@ define = function (func) {
define.amd = true;
require('../src/md5.js');
delete require.cache[require.resolve('../src/md5.js')];
delete require.cache[require.resolve('./test.js')];
md5 = null;
// webworker
WORKER = 'tests/worker.js';
SOURCE = 'src/md5.js';
require('./worker-test.js');
// cover webworker
JS_MD5_NO_WINDOW = true;
JS_MD5_NO_NODE_JS = true;
JS_MD5_NO_COMMON_JS = false;
JS_MD5_NO_ARRAY_BUFFER = false;
BUFFER = false;
WORKER = './worker.js';
SOURCE = '../src/md5.js';
window = global;
self = global;
Worker = function (file) {
require(file);
currentWorker = this;
this.postMessage = function (data) {
onmessage({data: data});
};
}
postMessage = function (data) {
currentWorker.onmessage({data: data});
}
importScripts = function () {};
md5 = require('../src/md5.js');
require('./worker-test.js');
+45 -32
View File
@@ -42,29 +42,30 @@
'd41d8cd98f00b204e9800998ecf8427e': [],
'93b885adfe0da089cdf634904fd59f71': [0],
'9e107d9d372bb6826bd81d3542a419d6': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]
},
'Uint8Array': {
'9e107d9d372bb6826bd81d3542a419d6': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
},
'Int8Array': {
'9e107d9d372bb6826bd81d3542a419d6': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
},
'ArrayBuffer': {
'93b885adfe0da089cdf634904fd59f71': new ArrayBuffer(1)
},
'Object': {
'd41d8cd98f00b204e9800998ecf8427e': {},
'd41d8cd98f00b204e9800998ecf8427e': {what: 'ever'}
}
};
if (typeof process == 'object') {
if (!(typeof JS_MD5_NO_ARRAY_BUFFER === 'boolean' && JS_MD5_NO_ARRAY_BUFFER)) {
testCases['Uint8Array'] = {
'9e107d9d372bb6826bd81d3542a419d6': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases['Int8Array'] = {
'9e107d9d372bb6826bd81d3542a419d6': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases['ArrayBuffer'] = {
'93b885adfe0da089cdf634904fd59f71': new ArrayBuffer(1)
};
}
if (typeof BUFFER === 'boolean' && BUFFER) {
testCases['Buffer'] = {
'd41d8cd98f00b204e9800998ecf8427e': new Buffer(0),
'9e107d9d372bb6826bd81d3542a419d6': new Buffer(new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]))
}
}
var errorTestCases = [null, undefined, { length: 0 }, 0, 1, false, true, NaN, Infinity, function () {}];
var methods = [
{
name: 'md5',
@@ -154,26 +155,26 @@
}
];
methods.forEach(function (method) {
describe('#' + method.name, function () {
for (var testCaseName in testCases) {
(function (testCaseName) {
var testCase = testCases[testCaseName];
context('when ' + testCaseName, function () {
for (var hash in testCase) {
(function (message, hash) {
it('should be equal', function () {
expect(method.call(message)).to.be(hash);
});
})(testCase[hash], hash);
}
});
})(testCaseName);
}
describe('md5', function () {
methods.forEach(function (method) {
describe('#' + method.name, function () {
for (var testCaseName in testCases) {
(function (testCaseName) {
var testCase = testCases[testCaseName];
context('when ' + testCaseName, function () {
for (var hash in testCase) {
(function (message, hash) {
it('should be equal', function () {
expect(method.call(message)).to.be(hash);
});
})(testCase[hash], hash);
}
});
})(testCaseName);
}
});
});
});
describe('Md5', function () {
classMethods.forEach(function (method) {
describe('#' + method.name, function () {
for (var testCaseName in testCases) {
@@ -192,5 +193,17 @@
}
});
});
describe('#md5', function () {
errorTestCases.forEach(function (testCase) {
context('when ' + testCase, function () {
it('should throw error', function () {
expect(function () {
md5(testCase);
}).to.throwError(/input is invalid type/);
});
});
});
});
});
})(md5);
+21
View File
@@ -0,0 +1,21 @@
(function () {
var cases = {
'd41d8cd98f00b204e9800998ecf8427e': '',
'9e107d9d372bb6826bd81d3542a419d6': 'The quick brown fox jumps over the lazy dog',
'e4d909c290d0fb1ca068ffaddf22cbd0': 'The quick brown fox jumps over the lazy dog.'
};
describe('#md5', function () {
Object.keys(cases).forEach(function (hash) {
it('should be equal', function (done) {
var worker = new Worker(WORKER);
worker.onmessage = function(event) {
expect(event.data).to.be(hash);
done();
};
worker.postMessage(SOURCE);
worker.postMessage(cases[hash]);
});
});
});
})();
+26
View File
@@ -0,0 +1,26 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MD5</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/expect.js/index.js"></script>
<script src="../src/md5.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
WORKER = 'worker.js';
SOURCE = '../src/md5.js';
mocha.setup('bdd');
</script>
<script src="worker-test.js"></script>
<script>
mocha.checkLeaks();
mocha.run();
</script>
<script>
</script>
</body>
</html>
+12
View File
@@ -0,0 +1,12 @@
var imported = false;
onmessage = function(e) {
if (imported) {
postMessage(md5(e.data));
if (typeof exports !== 'undefined') {
imported = false;
}
} else {
imported = true;
importScripts(e.data);
}
}