Blob is not defined

worker.js:1 Uncaught (in promise) ReferenceError: Blob is not defined

Blob is super important for manipulating files (javascript, images, …etc.).
It works in Chrome.
It should be available in CloudFlare Workers.

The code uploaded is a slighly modified version of this

Hi Denis,

Looking at the code you linked, it appears to do this:

  1. Construct a Blob containing ES module code.
  2. Generate an Object URL for that Blob.
  3. Import the Object URL, thus loading the ES module.

We actually plan to support Blobs soon, mainly for use with the FormData API. But that only gets you part 1 of the above.

Part 3 – loading ES modules from arbitrary URLs – is something we’re unlikely ever to support. Cloudflare Workers does not support loading code dynamically at runtime. We disable the eval() function, and our import can’t import external URLs; it can only import paths of modules that you uploaded via our API. This is important to us for security; we want to ensure we have a complete record of all code that runs on our edge.

Part 2 (generating an Object URL pointing to a blob) also isn’t likely to be supported, mostly because there isn’t much use for it in Workers.

I’m not sure if I completely understand your use case, but I suspect you don’t actually need any of this for what you want. It looks like you are trying to build some sort of a bundler, putting a bunch of ES modules into a JSON dictionary so that they can be loaded on-demand? If you use the new modules-based Worker representation (which is enabled with the Durable Objects beta), that already lets you upload a set of modules as separate files which can then import each other. See the Durable Objects documentation for some hints on how to use that new format. (We’ll be adding better support in wrangler later on…)


As you said, the new modules based workers will fix this.
I’m eager to try this new feature, as soon it is released ! :slight_smile:

Blob and URL are often used together, paired with FileReader, Image, OffscreenCanvas.

I think all of these APIs would be super handy for reading / converting / resizing / optimizing images, such WebP.

Image manipulation is also a thing when you start to deal with Tensorflow.

Currently I do it on client-side, but some people might prefer to achieve these objectives on server, for security or logic centralization purposes.

When you say support Blob, what do you mean exactly ?
Do you have to mimic all browser APIs, one by one ?
Can’t you just activate flags since you are V8 / Chrome based ?

I mean the ability to construct Blob and File objects. The only real use case for now is for use with FormData for manipulating uploaded files.

V8 includes ECMAScript built-in types like String or Promise, but it does not include browser APIs like fetch() or Blob. We have to implement those ourselves. We don’t use any Chrome code other than V8.

I could imagine supporting these someday, but OffscreenCanvas is a pretty complicated API that might call for hardware acceleration, so that would be a big project.

Object URLs are pretty awkward to support because it’s hard to tell when the blob can be garbage collected. I think we’d probably want to avoid supporting that, and instead offer alternative APIs that accept Blob objects directly instead of URLs.

1 Like