webworker.js 1.2KB

12345678910111213141516171819202122232425262728293031
  1. var fs = require('fs')
  2. var test = require('tape')
  3. var UAParser = require('ua-parser-js')
  4. var url = require('url')
  5. var work = require('webworkify')
  6. var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
  7. var browserName = browser.name
  8. var browserVersion = browser.major
  9. // Skip browsers with poor or nonexistant WebWorker support
  10. var skip = ((browserName === 'IE' && browserVersion <= 10) ||
  11. (browserName === 'Safari' && browserVersion <= 5) ||
  12. (browserName === 'WebKit' && browserVersion <= 534) || // Old mobile safari
  13. (browserName === 'Android Browser' && browserVersion <= 4))
  14. var reference = fs.readFileSync(__dirname + '/../server/static/browserify.png')
  15. test('binary download in WebWorker', {
  16. skip: skip
  17. }, function (t) {
  18. // We have to use a global url, since webworkify puts the worker in a Blob,
  19. // which doesn't have a proper location
  20. var testUrl = url.resolve(global.location.href, '/browserify.png')
  21. var worker = work(require('./lib/webworker-worker.js'))
  22. worker.addEventListener('message', function (ev) {
  23. var data = new Buffer(new Uint8Array(ev.data))
  24. t.ok(reference.equals(data), 'contents match')
  25. t.end()
  26. })
  27. worker.postMessage(testUrl)
  28. })