是否可以从字符串创建共享工作者并在不共享工作者URL的情况下使用它?

huangapple go评论74阅读模式
英文:

Is it possible to create shared worker from a string and to use it without sharing the worker URL?

问题

这可能不太可能,但我很乐意听听有关替代方案的建议。

我有一个大型的JS脚本,我想在一个SharedWorker中运行,但在运行之前不上传到服务器(它包含在一个我想要分发的库中)。
使用Dedicated Worker,我可以简单地创建一个Blob。但Blob URL是唯一的,而且它的生命周期与创建它的窗口的生命周期相连。

我需要的是一种通过string创建SharedWorker的方式,但这个Worker应该有一个任何其他页面都能够精确计算的URL。Base64字符串有大小限制,所以我的问题是是否有任何替代方法。

英文:

Probably this is not possible but I would be glad to hear about alternatives.

I have a large JS script which I want to run in a SharedWorker, but without uploading it to a server before running (it's included in a library which I want to distribute).
With Dedicated Worker I can simply make a Blob. But the Blob URL is unique and also his lifetime is tied to the lifetime of the window that creates it.

What I need is a way to create SharedWorker from a string but this worker should have a URL that any other page should be able to compute exactly. Base64 strings are limited in size, so my question is if there are any alternatives to it.

答案1

得分: 1

如果您想从一个字符串创建一个WebWorker,并且这个字符串可能非常大。而不是创建一个非常大的数据URI或Blob URL。您可以使用该选项首先引导一个WebWorker。在WebWorker内部,您可以等待通过postMessage发送的包含要执行的大型代码的消息。

需要注意的一点是:您将需要WebWorker来使用new Functioneval来执行代码,因此请注意任何XSS攻击向量。根据您的描述,这似乎不是一个问题,但我认为值得指出一下。

另外还发现了这个链接 -> https://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string 这里也提供了一个使用eval的备选方案,可能也会有所帮助。

英文:

If you are wanting to create a WebWorker from a string, and this string could be very large. Instead of creating a very large data URI, or Blob URL. You could use that option to bootstrap a WebWorker first. Inside the WebWorker you could then wait for a message sent via postMessage that contained the large code to execute.

One thing to note: you are going to require the webWorker to then new Function or eval the code, so be aware of any XSS attack vectors. From what you are describing this sounds like it's not an issue, but I thought it might be worth pointing out.

Also just found -> https://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string That also has a fallback that uses eval so that might also help.

huangapple
  • 本文由 发表于 2023年7月10日 23:58:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定