Files
Maor LegerandGitHub c91350217a Add sample for using storage-blob in web workers (#14809)
## What

- Adds a helpful error message about missing global DOM APIs when using core-xml / core-http in web workers 
- Adds a sample and document demonstrating possible workarounds and polyfills

## Why

A customer raised an issue in #11067 when trying to use @azure/storage-blob from a web worker. Because we use DOM APIs which are not available for web workers, customers using Storage Blob (and any other library that depends on XML) in workers will see a few errors:

1. `document` is not defined
2. `window` is not defined (fixed in #13873)

We decided in a previous PR (see https://github.com/Azure/azure-sdk-for-js/pull/13977#issuecomment-786273058) on some guidelines for supporting platforms other than browsers and node out of the box. And bundling a heavy XML library such as `xml2js` to support web workers did not meet the criteria of support we want to provide. 

What this change offers is a better error message when we detect global dependencies are missing and a sample demonstrating how I might use an XML / DOM library to provide this functionality in web workers.

Resolves #11067
2021-04-21 10:27:42 -07:00

20 lines
363 B
JavaScript

const { JSDOM } = require("jsdom");
const { window } = new JSDOM();
if (!globalThis.document) {
globalThis.document = window.document;
}
if (!globalThis.DOMParser) {
globalThis.DOMParser = window.DOMParser;
}
if (!globalThis.XMLSerializer) {
globalThis.XMLSerializer = window.XMLSerializer;
}
if (!globalThis.Node) {
globalThis.Node = window.Node;
}