Is it possible to spawn a local server in browserified web application - javascript

I'm currently try to run my electron application into a browser.
To do so, I'm using browserify.
The main issue is that I use a proxy to treat some data between my video stream client and my ipfs endpoints.
The proxy is made with the node module express but it appears that you can't use it in a browser.
I've checked for some alternative modules, but the problem is apparently not the module but the fact to spawn a server in the browser itself.
Here is the last reference I found that confirms the issue:
Here is the error I get from the browser:
login-bundle.js:73697 Uncaught TypeError: Cannot read property 'prototype' of undefined
at Object.<anonymous> (login-bundle.js:73697)
at Object.261../utils (login-bundle.js:74799)
at o (login-bundle.js:1)
at login-bundle.js:1
at Object.257../application (login-bundle.js:72936)
at o (login-bundle.js:1)
at login-bundle.js:1
at Object.255../lib/express (login-bundle.js:72264)
at o (login-bundle.js:1)
at login-bundle.js:1
refering to the following code:
var res = Object.create(http.ServerResponse.prototype)
where ServerResponse is undefined
That is why I found out that it was incompatible with the browser
I would like to know if anyone knows any solution to spawn a server into my browserified application.

Related

Fetch local files with Node.js

In a browser environment fetching a local file is quite trivial: one just need to start a server (using MAMP, XAMP, Mac's Python server, etc...), and then doing:
fetch("./foo.txt").then(etc...)
However, in Node.js this simple task has been a challenge. I've tried the same snippet using Node 18 (which comes with an experimental fetch API), but I'm always getting an invalid URL error:
TypeError: Failed to parse URL from foo.bar
[cause]: TypeError [ERR_INVALID_URL]: Invalid URL
I've tried installing node-fetch, but I'm getting the same error. I could start a local server for node like http-server, but it tells me to go to http://localhost:8080 to see the server, that is, using the browser, but the issue is that I can do that without node, using only a node build is the whole point.
My question is: is it possible to fetch a local file in a node build (Sublime Text, VS Code etc...), without using a browser? (note: I can do it with fs, but in my question I'd like to discuss fetch only)
My question is: is it possible to fetch a local file in a node build (Sublime Text, VS Code etc...), without using a browser? (note: I can do it with fs, but in my question I'd like to discuss fetch only)
The Node.js implementation of fetch does not currently support file: scheme URLs…
fetch("file:///tmp/123").then(r => r.text()).then(d => console.log(d));
reports:
Error: not implemented... yet...
…nor (it appears) does it resolve a relative path to a file: scheme URI.
You should use the fs module.

How to get SubtleCrypto work with testcafe?

I am generating SHA256 using SubtleCrypt Web API on client-side as following:
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
Everything works fine except for when I execute tests via testcafe. The captured console error says TypeError: Cannot read properties of undefined (reading 'digest') meaning crypto.subtle is undefined.
I know that SubtleCrypto is available only in secure contexts which also includes localhost and I am using localhost to run my end-to-end testcafe tests.
What am I doing wrong?
You need to run TestCafe over HTTPS protocol. See more information in the following help topic: test-https-websites.

TypeError when testing with Jest and Formidable

I have wrote a cusom express middleware to pars an incoming multipart form.
The middleware works fine when launched in local or deployed but the test with jest fails with the error
TypeError: Cannot set property domain of [object process] which has only a getter
The issues seems to come from the package asap used by formidable.
I think that Jest is putting some limitations on the process object since the line where everything breaks is
domain.active = process.domain = null;
in the file raw.js
Does anyone has any workaround or solution for this issue? I cannot migrate to formidable V3 at the moment so that's not an option

How to get search domain of local Chromebook and NS records of that domain for Chrome app?

I would like to get the search domain from chromeOS notebook and then NS records of the search domain using javascript for chrome app. The chromeapp will be running on chromeOS.
I am able to get the records using resolveNs method of NodeJs "dns" library but it needs nodeJS. And for that I need to package nodeJS with chrome App? I am new to chrome os.
I tried to browserify that but got several issues. Is there any way to get the search domain and all NS records of a domain using JavaScript?
Or is there a way to browserify the 'dns' module of NodeJS. In my try, browserify does the job but when I open the javascript in chrome, it says: fs.readFile is not a function:
Uncaught TypeError: fs.readFile is not a function
at Platform.parseResolv (bundle.js:4295)
at Platform._populate (bundle.js:4248)
at new Platform (bundle.js:4201)
at Object.<anonymous> (bundle.js:4409)
at Object.require.18../utils (bundle.js:4411)
at s (bundle.js:1)
at bundle.js:1
at Object.<anonymous> (bundle.js:3155)
at Object.require.15../packet (bundle.js:3815)
at s (bundle.js:1)
Same happens when I replace fs by browserify-fs. My code in nodeJS is (it works fine when I say node filename.js):
var dnsresolve = require('native-dns/lib/client').resolve;
dnsresolve('xxx.pb1.lan', 'NS', function (err, addresses) {
if (err) throw err;
console.log('addresses: ' + JSON.stringify(addresses));
});

Available modules in node script

why do I get different results when trying to find out more about the http module in node.js in the following to ways?
If I enter the node REPL and then print the content of the http module, i.e. if I run
me#mymachine:~> node
> console.log(http)
I get all the details of the http object:
{ IncomingMessage:
{ [Function: IncomingMessage]
super_:
{
...
If I write a script file called, say, script.js containing the following single line
console.log(http);
and execute it by running
node script.js
I get
ReferenceError: http is not defined
I would have expected both cases to behave in the same way - either the http module is preloaded or not. Why is there a difference? What am I getting wrong here?
I thought I could 'fix' this by preloading module http by running (in version 2)
node -r http script.js
Shouldn't this preload module http and thus avoid the reference error?
Looking forward to your input!
Repl has all the standard Node.js core modules required by default.
https://nodejs.org/api/repl.html#repl_accessing_core_node_js_modules

Categories