nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
106.58k stars 29.06k forks source link

Being able to do `import(blobUrls)` #47573

Open jimmywarting opened 1 year ago

jimmywarting commented 1 year ago

What is the problem this feature will solve?

tried doing:

code = 'console.log("hi")'
blob = new Blob([code], { type: 'text/javascript' })
url = URL.createObjectURL(blob)
import(url)

but failed with:

Only URLs with a scheme in: file and data are supported by the default ESM loader. Received protocol 'blob:'

What is the feature you are proposing to solve the problem?

being able to import dynamic generated code...

What alternatives have you considered?

converting it to a data: url works as a work around...

bnoordhuis commented 1 year ago

Does importing a blob work in other JS runtimes? It doesn't seem to in Firefox. Are there WPT tests that cover this?

bnoordhuis commented 1 year ago

Are there WPT tests that cover this?

To answer my own question: yes, there are

jimmywarting commented 1 year ago

Does importing a blob work in other JS runtimes?

Yes... i have done it many times in browsers to create dynamic code that i then import...

there are many other developer who dose this too. things like sql.js and JSZip think both are creating a hybrid solution that takes it's own minified source code and create a separate thread in order to not block havy calculations.

kinda like:

function ctx() { ... }
const blob = blob([ctx.toString(), `${ctx.name}()`])
const url = URL.createObjectURL(blob)

import(url)
new Worker(url)

works OK, as long as the function is "pure" and don't references any variable outside of it's own function But i guess it's a bit unusual to do: import(blobURL) then to create dynamic web workers.

JSFiddle and other code playgrounds certainly benefit of using import(blobUrl) if there is code that uses import x from xyz syntax


I just worked around this issue in my web worker polyfill I created a custom loader and basically run NodeJS with: node --loader ./resolver.js app.js

And b/c blob urls are not working via cross threads: https://github.com/nodejs/node/issues/46557 then i had to resort to using BroadcastChannel to request all threads if they have any references to those blobs.

so i guess ☝️ is a blocker...

jimmywarting commented 1 year ago

I could add to that i would also like to be able to do:

const url = URL.createObjectURL(blob)
new worker_threads.Worker(url, { type: 'module' })

but that maybe will be a own issue in itself...

github-actions[bot] commented 10 months ago

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

github-actions[bot] commented 9 months ago

There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.

For more information on how the project manages feature requests, please consult the feature request management document.

Ambyjkl commented 3 weeks ago

@RedYetiDev I would like this as well. All web browsers support this, Deno supports this, Bun supports this. Node is the black sheep

RedYetiDev commented 3 weeks ago

Hey, I don't think I've interacted with this issue yet, was there a specific reason for pinging me?

Ambyjkl commented 2 weeks ago

Hey, I don't think I've interacted with this issue yet, was there a specific reason for pinging me?

It said that you removed this from Node.js feature requests, so I thought you would be the one to contact

RedYetiDev commented 2 weeks ago

Ahh okay, no problem, I was doing a bit of cleanup.

RedYetiDev commented 2 weeks ago

I've been working on this a bit today, and I think I almost have it down. I've assigned myself to prevent other users from doing work thats already been done, I'd hate to see anyone's time go to waste.

RedYetiDev commented 2 weeks ago

54647