Appening 6 – Rescript NodeJS Environment

(Originally posted 2018-09-09.)

I have another flight where my Inbox is surprisingly close to empty so I’m writing about a nice iOS app that should be of interest to both mainframers and non-mainframers. This app is Rescript by Matteo Villa and it is a javascript programming environment for iPad and iPhone. It, most particularly, allows you to run Node.js on iOS. You can develop and test your scripts anywhere, including at 37,000 feet with no network.

The current version includes Node.js runtime version 8.6.0. I’m expecting the developer will update the app as new releases of Node become available.

The free version of the app allows you to run lots of Node apps. With an in-app purchase you can, most notably, use additional UI and Share modules. These talk to iOS specifically, so your scripts can interact with the device and other apps. The free app is ad-supported, so that might be another reason to pay a small amount of money to the developer.

What Is Node?

Node is a server-side javascript runtime and set of modules. This means that, unlike browser-side javascript, it runs on a server. Which in this case could even be a phone. 🙂 If you’re used to client/browser-side javascript it shouldn’t be much of a stretch to embrace Node.

The programming model, as distinct from the javascript language, is quite different from that you’d experience if you were developing for the browser. But that isn’t a difficult transition.

Here’s a short example that implements a simple web server, from a sample included with Rescript:

const http = require('http')

let server = http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/html'});

    let content = ‘
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
    <h1>Hello from Rescript!</h1>
    </body>
    </html>
    ‘
    res.write(content);
    res.end();
})

server.listen(8080,() => {
    console.log('Node.js server is ready, listening on http://localhost:8080');
})

Without taking you through it line by line, there being many Node tutorials around, I would note the vast majority of this code is a character string full of the HTML that would actually be served.

In almost any environment, you could point your. browser at localhost (or 127.0.0.1) and the HTML would be served.

So far I’ve installed Node on Linux, on my iPhone, my iPad, my MacBook Pro, and my Raspberry Pi. It’s simple to install and lots of modules are available for it.

The javascript implementation is based on Google’s V8 interpreter which was designed to be fast. I won’t get into a “browser / javascript engine wars” discussion but all sensible modern javascript interpreters are much faster than they once were. For most applications, javascript is plenty fast enough.

It’s also worth pointing out that Node can act as a web client, for example using the node-fetch module. It can also handle server-side things like files.

You can also create your own modules and, through Node Package Manager (NPM), distribute them.

Rescript

Rescript is a very new app at the time of writing, but it shows lots of promise.

The app comprises three panes:

  • On the left a document picker.
  • In the middle the editing pane.
  • On the right a combination Console / Output / Help pane.

While the examples are a little sparse, there are plenty of examples and tutorials on the web. Plus there is a book “Node.js Up And Running” published by O’Reilly. I have it open side by side on my iPad with Rescript. So, that’s a nice use of Split Screen – a key modern iPad feature.

However, this reliance on the web is a little problematic at 37,000 feet (or wherever you find yourself out of Internet). I mention this because in the Settings dialog are links to a number of open source libraries included in Rescript. But these links are to the web. Also the help is a mixture of built-in information and links to the web. For example the javascript and Node.js links are to the web and don’t work in an aeroplane (for most of us). It would be nice to see a bit more information – really for beginners – locally cached. I realise this is a nit but it might be important to a beginner’s experience. Actually the built in information is nice. And the modules provided by Rescript are indeed available within the app, without network connectivity.

I didn’t find a way to see the source of the Node libraries included with Rescript. I don’t think this is a problem, however, as you’re supposed to code to the APIs, rather than examining the source code. But if you were intent on contributing to these libraries you might well feel different.

I wouldn’t say that Rescript is a full function IDE but it does have syntax colouring, which helps catch errors. But, frankly, most of my javascript development has been done with nothing more than a text editor. Some people like code folding, for example hiding the body of a javascript function. Personally I don’t tend to use it, perhaps as my slabs of code tend to be quite small, but I think it would be a nice edition.

Another thing – available in a full IDE – is code completion based on the Node modules brought in by require(). I think this is a tall order, though.

So, my experience of Rescript is good, despite the nits I mentioned above.

While I haven’t developed any User-Interface (UI) based apps I’ve run the samples and nosed through the code. Unless you point your browser at localhost you’re not going to see HTML and might well be building scripts that use the UI module. You might also use some of the iOS-specific modules.

One I did try was the iOS share sheet capability – which is only available in the paid version.

I mentioned NPM above. I would like to see some NPM capability, if that’s possible.

The help mentions a number of keyboard shortcuts. If you have an iPad you might not have an external keyboard so these wouldn’t help you. More than 99% of the time I use my iPad with an external keyboard so I appreciate these.

The help also tells me I can invoke a Rescript script from a share sheet in another app. This is nice to see. What I haven’t seen is any support for URL schemes. I’m thinking of x-callback-url in particular. I hope this comes as it would allow Rescript to be invoked from other apps as part of some sophisticated automation.

Another nit is that I didn’t find a way to resize the side panels. Particularly for the help, I would have liked that.

Integration With Other iOS Apps – Via The Share Sheet

As I said, I experimented with the share module.

First, I tried Rescript as a “client” – where a Node script pops up the iOS share sheet. Here is a very simple example:

let share=require('share')

share.shareText('Blah\nblah\nblah')

If you run this very short script it does indeed pop up the share sheet, and the text in the shareText call is indeed passed in.

Now here’s Rescript as a “server” – where a Node script accepts text.

let share=require('share')

console.log(share.getText())

As with most share sheet extensions you have to enable them in the share sheet – but that’s merely flipping a switch.

In Workflow (being reborn as Shortcuts in iOS 12) you nominate a workflow to be usable in the share sheet within the Workflow app – when you edit the workflow. In contrast you nominate Rescript scripts to be usable in the share sheet when you invoke the Rescript extension. But it remembers what you did so it’s a one time setup.

Anyway, when you open the iOS share sheet from another app with some text selected you can pass it to a specific Rescript script. With the console.logfunction it writes the output to a pop up window. There are two things you can do with that output, if you select some or all of the text:

  • You can cut the selected text to the iOS clipboard.
  • You can invoke the share sheet again with the selected text.

The net of this is that Rescript scripts can participate nicely in workflows involving other apps and the iOS share sheet.

By the way, the clipboard module allows you to read from and write to the clipboard.

Drafts And Rescript

In Appening 5 – Drafts On iOS I talked about another javascript environment on iOS. There are some key differences. Which you want depends on what you’re trying to do. Personally I have both and use them for distinctly different things:

  • Drafts is all about capturing text and processing it, with javascript supporting that through automation.
  • Rescript is all about Node.js and could also be used for automation. If your interest is primarily Node then you’d probably want Rescript.

I would say that javascript is a language that is really worth learning now, with many environments for running it. An increasing number are on iOS. And if you can get to both the relevant book and the development environment being on screen at the same time – through Split Screen – that’s a nice position to be in. Even if you are at 37,000 feet.

Conclusion

I’m very happy with this app but think, as with all new apps, it could get even better. As to whether to pay for the app, I think think the £3.99 I spent was good value for money – particularly as the modules included with the paid version add significant function. But, if your interest in Node is very light, you might be happy with the free version.

In any case, I hope Matteo (@mttvll on Twitter) keeps developing Rescript. My areas of improvements are, I think nits, and I’m just bowled over to be able to run Node on my iPhone and especially my iPad. And to have the nice “iOS integration” extension modules he’s built.

Published by Martin Packer

I'm a mainframe performance guy and have been for the past 35 years. But I play with lots of other technologies as well.

One thought on “Appening 6 – Rescript NodeJS Environment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: