[quickjs-devel] Re: tcp/udp sockets

  • From: Pranav Samvith Bharadwaja <pranav@xxxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Wed, 1 Feb 2023 22:24:18 +0530

Hi Jeremy,
  I found your suggestion to use esbuild very helpful. I have spent
days using browserify, rollup etc with partial success, but esbuild
was very painless.
  I have applied the sed patches too, as instructed. However the
resulting bundle still doesn't run in qjs.  It says

SyntaxError: invalid use of 'super'
    at ./commonListener.esbuild.js:5

But there is no super in line 5. line 5 through 10 defines __require
method. I have used --format=esm and --platform=node. Perhaps I should
use --platform=neutral?  If i use neutral, then the requires of
'http', 'debug', 'fs', etc. It looks like I will still need to write c
modules for 'fs' and 'net' for qjs.

Thanks.

On Tue, Jan 31, 2023 at 1:24 PM Pranav Samvith Bharadwaja
<pranav@xxxxxxxxxx> wrote:


Jeremy, Jerry, guest271314,
  Thanks for your inputs.
   Jeremy, no I havent tried making esbuild, I will do that. I have
been trying to use browserify and other tools to bundle it all into
one file, since my nodejs app has many dependencies, including
mongoose, socketio and express to name a few. While that is still an
unsolved problem, that is not relevant to quickjs. I am working on
"minor patching" for mongoose to make a bundle that runs on nodejs
without any dependencies. I presume that it will also run on quickjs
if it runs on node (except for fs,path,net etc built-ins being
unavailable, needing more patching to replace with what does exist for
quickjs, as mentioned by guest271314). I did try parcel and rollup but
ran into some issues, I think my best bet is to tweak the source code
of mongoose etc and run browserify. I dont remember if I have tried
esbuild. My codebase uses commonJS requires, not es modules. Again,
bundling, while being a problem, is a different problem, I wouldn't
want to impose it on qjs community.
  guest271314, thanks for the resources you have sent in the thread, I
will go through them. The answers you have given very patiently does
reflect that I hadn't done enough research about qjs on github before
asking. I am sorry for that.
  I have seen the gwsocket solution, and I think it is good for a work
around. I do need websockets (with socketio wrapping) but it is not a
replacement for raw tcp sockets, but the gwsockets gives me an idea to
find something similar for tcp sockets. perhaps using netcat as a
server to receive tcp connections and invoking that via pipes to qjs
environment. I would still like native support for sockets because if
there are multiple clients to an os.exec("nc",...) invoked server then
i wont be able to maintain states of each client. I would instead like
to implement a c module to be included in the qjs build so that the
qjs runtime and qjsc will have a builtin module for tcp sockets. I
think the quickwebserver will be of help to me in this regard, I will
try to understand how it works and implement a C shared library to
provide tcp socket server and client. Perhaps filesystem ops can also
be done in this way.

It will take me a few days, perhaps weeks to make progress on this,
and i hope the results will be helpful to other people as well. Again,
thanks for pointing me in the right direction.


On Tue, Jan 31, 2023 at 7:45 AM guest271314 <guest271314@xxxxxxxxx> wrote:

If tcp sockets are not implemented at all, is there any way in which I 
might go about implementing a sockets built-in module for the qjs engine?

There are a few ways to do this. See https://github.com/ratboy666/qjs-ws, ;
https://github.com/QuickJS-Web-project/quickwebserver, ;
https://github.com/scriptableorg/quickjs-http; et al.

Similarly I would like to implement a way to load local files and send 
them to a remote client.

You can use WebRTC datachannels. 
https://github.com/blackgate/qjsWebRtcClient, ;
https://github.com/paullouisageneau/libdatachannel.

This resource also claims that qjs uses select instead of libuv. is there 
a build option using which I can choose whether to use select/poll/epoll 
or libuv?

See https://github.com/saghul/txiki.js.

I use std.popen() in a QuickJS Native Messaging host to call an application 
and stream the data to the browser where I write the data to a 
MediaStreamTrackGenerator which can be streamed to other peers using 
WebRTC, see 
https://github.com/guest271314/captureSystemAudio/blob/master/native_messaging/capture_system_audio/capture_system_audio.js.

I too am interested in using Deno's listenTls() 
https://github.com/guest271314/native-messaging-espeak-ng/blob/deno-server/deno_server.js
 in QuickJS - without carrying around the entire V8, et al.

I'll have a closer look at Calling JavaScript from C/C++ using QuickJS.

On Mon, Jan 30, 2023 at 3:14 PM Jeremy DeJournett <jcdejournett@xxxxxxxxx> 
wrote:

Pranav,

Have you tried building your Node module in esbuild? With some minor 
patching, you can then compile it to QuickJS bytecode which you can embed 
in a native binary alongside the interpreter.

Here's how I did something similar a few months ago: 
https://github.com/ijustlovemath/jescx/blob/master/README.md

There's a few other ways to do this, including adding your very own net 
and fs objects to the global context, but I found this way to be the most 
frictionless.

Best,

Jeremy

On Mon, Jan 30, 2023, 10:57 Pranav Samvith Bharadwaja <pranav@xxxxxxxxxx> 
wrote:

Update:
  I found a resource on medium.
https://calbertts.medium.com/how-to-create-asynchronous-apis-for-quickjs-8aca5488bb2e
  but in this resource they use the process module to invoke the curl 
command to make a http request. Perhaps the medium resource is missing 
something, or they just decided to use a simple tool (curl) instead of 
opening a socket and sending uri and headers?
  If tcp sockets are not implemented at all, is there any way in which I 
might go about implementing a sockets built-in module for the qjs engine?
  Similarly I would like to implement a way to load local files and send 
them to a remote client.
  This resource also claims that qjs uses select instead of libuv. is 
there a build option using which I can choose whether to use 
select/poll/epoll or libuv? or would i need to implement that as an 
option in the qjs engine code as well? Is select much slower than libuv 
for an application that mostly deals with io over tcp sockets and rarely 
ever needs to read/write files?
I would appreciate any advice in this matter.
Thanks,
Pranav

On Mon, Jan 30, 2023 at 12:18 PM Pranav Samvith Bharadwaja 
<pranav@xxxxxxxxxx> wrote:


Greetings,
  I have seen this mailchain
https://www.freelists.org/post/quickjs-devel/Newbie-Getting-something-like-require-to-work,1
  I think it is reasonable that net, fs and path are not implemented in 
qjs. But is there an alternative by which we could create tcp/udp 
sockets?
  I am trying to compile a node.js project and with some custom edits to 
some node modules, able to bundle them all into a single file (because 
require doesnt work with qjs). A database driver would inevitably use 
the net built-in module. If an alternative is available i could make 
customization for qjs backend versus pre-existing nodejs. Any help is 
appreciated.
--
Thanks and Regards,
Pranav.


Other related posts: