It seems that you're using an outdated browser. Some things may not work as they should (or don't work at all).
We suggest you upgrade newer and better browser like: Chrome, Firefox, Internet Explorer or Opera

×
Here is a task, and I am wondering if I can do this in JavaScript.

The basic idea is simple: The program reads in a binary file, chosen by the user, modifies it in some way (for example, XORing every byte with 0xcc, but it could be something more complex, like image processing or (de)compression), and then returns the result to the user (perhaps as a file that would then be downloaded).

Here is where it gets interesting. The program needs to be able to run:
* In a modern-ish web browser, completely on the client (in other words, must run with networking disabled).
* On the server, with JavaScript disabled on the client, ideally using the same form as the client-side example.
* As a stand-alone command from the command line. Ideally, the filename should be specifiable over the command line, and the program should also have the option to work with standard input and output (so it can be used in a pipeline on the shell).

I would like as much code to be shared between these versions. In particular, I would like to share the code for input and output, as well as all the logic involved in the transformation done to the file, with the only differences being how the user chooses what file to process.

Is JavaScript suitable for this task, or should I look into something like WebAssembly (which would have the advantage of maybe also being portable to embedded)?

Now, as for browser support requirements:
* All current LTS versions of major browsers, as well as more recent versions. Ideally, it should work on future versions as well (so no relying on deprecated features).
* The versions of firefox and chromium in debian stable. (At least I'm not requiring the oldest supported version of RHEL.)
* The server-side program should work with browsers that do not support JavaScript, such as lynx.
* For the command-line version, it's reasonable to require the user to install node,js and some npm libraries, as long as those libraries are well-maintained and under a permissive open source license.
The actual work code should be portable regardless of which JavaScript runtime you run, but you'll need different surrounding code for the entrypoint/UX, depending on whether it runs in a browser, terminal, or on the server (terminal and server could use the same or very similar if the server code executes it as any other arbitrary external application).

Not that I have built for anything but web browsers in JavaScript, but yes, it can definitely be done, and provided you don't use anything specific to your environment (like the DOM in a browser), it is definitely portable.