Deno 1.4 was released a few days ago with lots of changes, here are the actually interesting ones. Including some breaking ones that might catch you by surprise!

On September 13th, the team behind Deno released their biggest update to date: version 1.4. This new version has some very interesting features which work towards improving Deno’s stability as well as its chances of being used for a proper software project and not just for some minor scripting.

Now, don’t get me wrong here, I’m all up for using Deno, but we also have to remember that this runtime is not even a year old, and because of that, it is still suffering major changes, like some of the ones I’m going to be listing here.

Also note that since then, version 1.4.1 has also been released with a bunch of minor fixes and refactors, nothing really worth mentioning here.

Finally, if you want to test this new release, you can update your current version of Deno with: deno upgrade

So, what’s actually interesting about 1.4?

Out of all the changes released as part of 1.4, the following capabilities were added (or removed in some cases) to Deno which I think are note-worthy:

Web Standard WebSocket API

Now with the WebSocket API implemented and fully working, you can safely write both, your servers and clients following the same standards, just remember that you have to allow for network connections to be used in order to test your code.

As you can see in the following example, the code is following the standard to the letter, so the normal MDN documentation you’d use for the browser or Node can be used here as well:


const decoder = new TextDecoder('utf-8');
let text:string;
let lines:string[];
try {
  text = decoder.decode(await Deno.readFile("./test.txt"))
  lines = text.split("\n")
} catch(e) {
  console.error(e)
  Deno.exit(1)
}

// Start the connection to the WebSocket server at echo.websocket.org
const ws = new WebSocket("ws://echo.websocket.org/");

// Register event listeners for the open, close, and message events
ws.onopen = () => {
  console.log("WebSocket ready!");

  lines.forEach( (l:string) => {
    console.log(l)
    ws.send(l)
  } )
};
ws.onmessage = (message) => {
  // Log the message we recieve:
  console.log("Received data:", message.data);

  // Close the websocket after receiving the "exit" message (the last line)
  if(message.data == "exit") ws.close();
};

ws.onclose = () => console.log("WebSocket closed!");
ws.onerror = (err) => console.log("WebSocket error:", err);

Just to quickly go over the code: it’ll read a text file, split it’s lines into an list of lines and it’ll send each one through the socket connection established with echo.websocket.org . The output would look something like this (consider everything before the first “exit” to be the content of your file):

one line
second line
this is another line
the final line of the file
exit
Received data: one line
Received data: second line
Received data: this is another line
Received data: the final line of the file
Received data: exit
WebSocket closed!

And just to make sure, in order to execute this script, you’d have to use the following line:

$ deno run --allow-read --allow-net=echo.websocket.org sockets-demo.ts

You can either do that, or you can use this little library I wrote a while ago, which allows you to avoid the “allow-flags”: deno-runner.

#deno #programming #javascript #typescript #node

Deno 1.4: The Interesting Parts
3.05 GEEK