question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Core-http and potentially storage-blob do not work from web worker

See original GitHub issue
  • Package Name: core-http and possibly storage-blob
  • Package Version: @azure/core-http@1.1.7, @azure/storage-blob@12.2.0-preview.1
  • Operating system: macOS 10.15.6
  • nodejs
    • version:
  • browser
    • name/version: Chrome 85.0.4183.83
  • typescript
    • version:
  • Is the bug related to documentation in

Describe the bug Unable to use @azure/storage-blob package in a context of web worker. Attempting to load it fails with error

Uncaught ReferenceError: document is not defined
    at Module../node_modules/@azure/core-http/es/src/util/xml.browser.js (xml.browser.ts:5)
    at __webpack_require__ (bootstrap:19)
    at Module../node_modules/@azure/core-http/es/src/policies/deserializationPolicy.js (deserializationPolicy.ts:1)
    at __webpack_require__ (bootstrap:19)
    at Module../node_modules/@azure/core-http/es/src/serviceClient.js (serviceClient.ts:1)
    at __webpack_require__ (bootstrap:19)
    at Module../node_modules/@azure/core-http/es/src/coreHttp.js (coreHttp.ts:1)
    at __webpack_require__ (bootstrap:19)
    at Module../node_modules/@azure/storage-blob/dist-esm/storage-blob/src/index.browser.js (index.browser.ts:1)
    at __webpack_require__ (bootstrap:19)

The cause of the issue is that document is not available inside WebWorkers.

To Reproduce

Steps to reproduce the behavior: I created a sample repo showcasing the issue https://github.com/Obi-Dann/azure-storage-blob-worker-issue based on Create React App

  1. Clone the repository locally
  2. Run yarn install
  3. Run yarn start which will open a web browser eventually
  4. Open browser devtools and observe the error

Expected behavior I should be able to use @azure/storage-blob package running it inside a web worker context.

Additional context I have a web worker process compressing images in background and then uploading them to Azure storage blob. I can only think of sending messages with file buffers/links to the main app from the worker in order to get the files uploaded, but that does not seem very maintainable in my case because the worker needs to do more work once the file is uploaded.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:18 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
Obi-Danncommented, Sep 8, 2020

Thanks @salqueng! There’s also an issue with https://github.com/Azure/azure-sdk-for-js/blob/99f531906125f2d22a613957571a32ca792d009d/sdk/core/core-http/src/policies/msRestUserAgentPolicy.browser.ts#L21 which should be quite easy to fix by replacing window.navigator with self.navigator

2reactions
salquengcommented, Sep 8, 2020

A workaround is below here. Import or place this code at the top of your web worker code. The idea is using 3rd party DOM parser library(xmldom), which is compatible with browser APIs, and mocking document and window.

import { DOMParser, DOMImplementation, XMLSerializer } from "xmldom";

self.DOMParser = DOMParser;
self.XMLSerializer = XMLSerializer;

self.document = {
  implementation: new DOMImplementation(),
};

self.window = {
  navigator,
};

There is a workaround, but I want to use @azure/storage-blob without any strange mocking and 3rd party library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - web worker doesn't work
Web Workers are asynchronous JavaScript processing environments that do not have access to their host environment: the DOM. In web workers ...
Read more >
Understanding and Using Web Workers
To be clear, Web Workers are not exactly brand new. ... what your application does now that you can potentially spawn off to...
Read more >
Threading the web with module workers - web.dev
This is intuitive and works well for lots of cases on the web, but can ... Since there was no module system when...
Read more >
Real-time processing with web workers
It may sound strange because that means JavaScript code is not fully exploiting your machine's computing power. In addition, this model may ...
Read more >
Understanding web workers
The browser is no exception. Web workers can be used to run operations in the background in a browser, keeping your UI responsive....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found