web-worker

Can someone explain the webworker-thread example?

∥☆過路亽.° 提交于 2019-12-21 06:17:08
问题 var Worker = require('webworker-threads').Worker; require('http').createServer(function (req,res) { var fibo = new Worker(function() { function fibo (n) { return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1; } // which onmessage does this this refer to? onmessage = function (event) { //reference 1 postMessage(fibo(event.data)); } }); fibo.onmessage = function (event) { //reference 2 res.end('fib(40) = ' + event.data); }; fibo.postMessage(40); }).listen(port); This is the code found as an example for

Web workers inside promise causing crash

醉酒当歌 提交于 2019-12-21 06:06:17
问题 The expected workflow of my code is getting the data from getData . getData calls the worker that will do ImageUtil.getHex on the input. ImageUtil.getHex is a heavy function that needs to iterate every pixel of an image area area , so that's why I want to create it runs in the background and done in multithreading. The function is also independent, which I guess is a good candidate to put in the worker. This is the chunk of code that needs the getData function: class Mosaic { // ... build() {

Get Cloudflare's HTTP_CF_IPCOUNTRY header with javascript?

眉间皱痕 提交于 2019-12-21 04:55:24
问题 There are many SO questions how to get http headers with javascript, but for some reason they don't show up HTTP_CF_IPCOUNTRY header. If I try to do with php echo $_SERVER["HTTP_CF_IPCOUNTRY"]; , it works, so CF is working just fine. Is it possible to get this header with javascript? 回答1: Assuming you are talking about client side JavaScript: no, it isn't possible. The browser makes an HTTP request to the server. The server notices what IP address the request came from The server looks up

Web worker won't start in IE unless the cache is cleared

百般思念 提交于 2019-12-20 23:24:39
问题 I'm having a really weird bug in my HTML5 script. I wrote a sharepoint app completely in OData which uses a few HTML 5 webworker to do the number crunching in the background. This works perfect on all major browsers (FF, IE10+ Chrome, ...). However, when I perform a refresh or browse to the page again. The script still works as intended on FF and Chrome, but hangs on IE. In my network view I see a request for the Worker.js file, but with a 304 NOT MODIFIED response. IE then just hangs there

Are web workers a secure way to sandbox untrusted javascript code

夙愿已清 提交于 2019-12-20 21:02:04
问题 I was wondering if a web worker would be a secure way to sandbox untrusted javascript code. Let's say for example in the context of a drawing application where developers can implement new drawing tools, you could put their code inside a webworker, and any time the user clicks on the canvas, send them a JSON message containing the cursor position, and an array of image data, and when the script is done, it passes a message back containing the new image data. Would this be secure, or are there

Are web workers a secure way to sandbox untrusted javascript code

大城市里の小女人 提交于 2019-12-20 21:01:34
问题 I was wondering if a web worker would be a secure way to sandbox untrusted javascript code. Let's say for example in the context of a drawing application where developers can implement new drawing tools, you could put their code inside a webworker, and any time the user clicks on the canvas, send them a JSON message containing the cursor position, and an array of image data, and when the script is done, it passes a message back containing the new image data. Would this be secure, or are there

JavaScript: How to know if a connection with a shared worker is still alive?

我怕爱的太早我们不能终老 提交于 2019-12-20 09:34:56
问题 I'm trying to use a shared worker to maintain a list of all the windows/tabs of a web application. Therefore following code is used: //lives in shared-worker.js var connections=[];//this represents the list of all windows/tabs onconnect=function(e){ connections.push(e.ports[0]); }; Everytime a window is created a connection is established with the shared-worker.js worker and the worker adds the connection with the window to the connections list. When a user closes a window its connection with

Why can't Web Worker call a function directly?

China☆狼群 提交于 2019-12-20 09:30:05
问题 We can use the web worker in HTML5 like this: var worker = new Worker('worker.js'); but why can't we call a function like this? var worker = new Worker(function(){ //do something }); 回答1: This is the way web workers are designed. They must have their own external JS file and their own environment initialized by that file. They cannot share an environment with your regular global JS space for multi-threading conflict reasons. One reason that web workers are not allowed direct access to your

Web workers and Canvas data

孤者浪人 提交于 2019-12-20 07:47:17
问题 I have seen a lot of threads about web workers and <canvas> data passing, and I'm not sure if my scenario is implementable. I want to create a small site for users to learn to code in Javascript. When users click a button, the code is run in a web worker. To help them, I have also created methods for "printing" (which basically just adds text to a <pre> element. // get code from user and prepend helper "functions" var userCode = editor.getValue(); var codeWithMessages = "\n\ function print

Assign __proto__ to an interface to turn it into an object - clever or dangerous?

允我心安 提交于 2019-12-20 05:43:08
问题 When using postMessage() to copy data to/from a web worker, you can post an object that's a class. But on the far side, the received object is just the data (ie an interface, not a class). One person suggested assign the correct __proto__ to that object on the receive side to turn it back into a class. This works. But is this solid & safe or is it asking for trouble? I am not asking for opinion here (a big no-no), I'm asking are there specific technical issues that might bite me in the